File tree Expand file tree Collapse file tree 1 file changed +28
-1
lines changed Expand file tree Collapse file tree 1 file changed +28
-1
lines changed Original file line number Diff line number Diff line change @@ -674,6 +674,24 @@ static void sanitize_stdfds(void)
674
674
close (fd );
675
675
}
676
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
+
677
695
static void store_pid (const char * path )
678
696
{
679
697
FILE * f = fopen (path , "w" );
@@ -699,6 +717,7 @@ int main(int argc, char **argv)
699
717
int port = DEFAULT_GIT_PORT ;
700
718
int inetd_mode = 0 ;
701
719
const char * pid_file = NULL ;
720
+ int detach = 0 ;
702
721
int i ;
703
722
704
723
/* Without this we cannot rely on waitpid() to tell
@@ -767,6 +786,11 @@ int main(int argc, char **argv)
767
786
pid_file = arg + 11 ;
768
787
continue ;
769
788
}
789
+ if (!strcmp (arg , "--detach" )) {
790
+ detach = 1 ;
791
+ log_syslog = 1 ;
792
+ continue ;
793
+ }
770
794
if (!strcmp (arg , "--" )) {
771
795
ok_paths = & argv [i + 1 ];
772
796
break ;
@@ -799,7 +823,10 @@ int main(int argc, char **argv)
799
823
return execute (peer );
800
824
}
801
825
802
- sanitize_stdfds ();
826
+ if (detach )
827
+ daemonize ();
828
+ else
829
+ sanitize_stdfds ();
803
830
804
831
if (pid_file )
805
832
store_pid (pid_file );
You can’t perform that action at this time.
0 commit comments