Skip to content

Commit de0957c

Browse files
pcloudsgitster
authored andcommitted
daemon: move daemonize() to libgit.a
Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 79fcbf7 commit de0957c

File tree

3 files changed

+29
-26
lines changed

3 files changed

+29
-26
lines changed

cache.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,7 @@ extern int set_git_dir_init(const char *git_dir, const char *real_git_dir, int);
434434
extern int init_db(const char *template_dir, unsigned int flags);
435435

436436
extern void sanitize_stdfds(void);
437+
extern int daemonize(void);
437438

438439
#define alloc_nr(x) (((x)+16)*3/2)
439440

daemon.c

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,11 +1056,6 @@ static void drop_privileges(struct credentials *cred)
10561056
/* nothing */
10571057
}
10581058

1059-
static void daemonize(void)
1060-
{
1061-
die("--detach not supported on this platform");
1062-
}
1063-
10641059
static struct credentials *prepare_credentials(const char *user_name,
10651060
const char *group_name)
10661061
{
@@ -1102,24 +1097,6 @@ static struct credentials *prepare_credentials(const char *user_name,
11021097

11031098
return &c;
11041099
}
1105-
1106-
static void daemonize(void)
1107-
{
1108-
switch (fork()) {
1109-
case 0:
1110-
break;
1111-
case -1:
1112-
die_errno("fork failed");
1113-
default:
1114-
exit(0);
1115-
}
1116-
if (setsid() == -1)
1117-
die_errno("setsid failed");
1118-
close(0);
1119-
close(1);
1120-
close(2);
1121-
sanitize_stdfds();
1122-
}
11231100
#endif
11241101

11251102
static void store_pid(const char *path)
@@ -1333,9 +1310,10 @@ int main(int argc, char **argv)
13331310
if (inetd_mode || serve_mode)
13341311
return execute();
13351312

1336-
if (detach)
1337-
daemonize();
1338-
else
1313+
if (detach) {
1314+
if (daemonize())
1315+
die("--detach not supported on this platform");
1316+
} else
13391317
sanitize_stdfds();
13401318

13411319
if (pid_file)

setup.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -787,3 +787,27 @@ void sanitize_stdfds(void)
787787
if (fd > 2)
788788
close(fd);
789789
}
790+
791+
int daemonize(void)
792+
{
793+
#ifdef NO_POSIX_GOODIES
794+
errno = ENOSYS;
795+
return -1;
796+
#else
797+
switch (fork()) {
798+
case 0:
799+
break;
800+
case -1:
801+
die_errno("fork failed");
802+
default:
803+
exit(0);
804+
}
805+
if (setsid() == -1)
806+
die_errno("setsid failed");
807+
close(0);
808+
close(1);
809+
close(2);
810+
sanitize_stdfds();
811+
return 0;
812+
#endif
813+
}

0 commit comments

Comments
 (0)