@matheusbsbs / matheus-bagliano / commits / a5262768e1
Fork 0

daemon: new option --detach to run git-daemon in background

Signed-off-by: Matthias Lederhofer <matled@gmx.net> Signed-off-by: Junio C Hamano <junkio@cox.net>

Matthias Lederhofer committed Jul 13, 2006 at 18:47 UTC a5262768e1e4af96bbca60ee712e97105f80de24
1 file changed +28 -1
daemon.c
+28 -1
@@ -674,6 +674,24 @@ static void sanitize_stdfds(void)
674 close(fd);
675 }
676
677 +static void daemonize(void)
678 +{
679 + switch (fork()) {
680 + case 0:
681 + break;
682 + case -1:
683 + die("fork failed: %s", strerror(errno));
684 + default:
685 + exit(0);
686 + }
687 + if (setsid() == -1)
688 + die("setsid failed: %s", strerror(errno));
689 + close(0);
690 + close(1);
691 + close(2);
692 + sanitize_stdfds();
693 +}
694 +
695 static void store_pid(const char *path)
696 {
697 FILE *f = fopen(path, "w");
@@ -699,6 +717,7 @@ int main(int argc, char **argv)
717 int port = DEFAULT_GIT_PORT;
718 int inetd_mode = 0;
719 const char *pid_file = NULL;
720 + int detach = 0;
721 int i;
722
723 /* Without this we cannot rely on waitpid() to tell
@@ -767,6 +786,11 @@ int main(int argc, char **argv)
786 pid_file = arg + 11;
787 continue;
788 }
789 + if (!strcmp(arg, "--detach")) {
790 + detach = 1;
791 + log_syslog = 1;
792 + continue;
793 + }
794 if (!strcmp(arg, "--")) {
795 ok_paths = &argv[i+1];
796 break;
@@ -799,7 +823,10 @@ int main(int argc, char **argv)
823 return execute(peer);
824 }
825
802 - sanitize_stdfds();
826 + if (detach)
827 + daemonize();
828 + else
829 + sanitize_stdfds();
830
831 if (pid_file)
832 store_pid(pid_file);