@@ -68,6 +68,8 @@ static const char *verbosity_colors[] = {
6868 [LOG_DEBUG ] = "\x1B[1;30m" ,
6969};
7070
71+ static bool daemonize = false;
72+
7173static enum log_importance log_importance = LOG_INFO ;
7274
7375void swayidle_log_init (enum log_importance verbosity ) {
@@ -827,7 +829,7 @@ static int parse_idlehint(int argc, char **argv) {
827829
828830static int parse_args (int argc , char * argv [], char * * config_path ) {
829831 int c ;
830- while ((c = getopt (argc , argv , "C:hdwS :" )) != -1 ) {
832+ while ((c = getopt (argc , argv , "C:hdfwS :" )) != -1 ) {
831833 switch (c ) {
832834 case 'C' :
833835 free (* config_path );
@@ -836,6 +838,9 @@ static int parse_args(int argc, char *argv[], char **config_path) {
836838 case 'd' :
837839 swayidle_log_init (LOG_DEBUG );
838840 break ;
841+ case 'f' :
842+ daemonize = true;
843+ break ;
839844 case 'w' :
840845 state .wait = true;
841846 break ;
@@ -848,6 +853,7 @@ static int parse_args(int argc, char *argv[], char **config_path) {
848853 printf (" -h\tthis help menu\n" );
849854 printf (" -C\tpath to config file\n" );
850855 printf (" -d\tdebug\n" );
856+ printf (" -f\tdaemonize after initialization\n" );
851857 printf (" -w\twait for command to finish\n" );
852858 printf (" -S\tpick the seat to work with\n" );
853859 return 1 ;
@@ -1016,6 +1022,25 @@ static int load_config(const char *config_path) {
10161022 return 0 ;
10171023}
10181024
1025+ int do_daemonize (void ) {
1026+ // don't close stdin, stdout, stderr
1027+ // just fork and setsid
1028+ pid_t child ;
1029+
1030+ child = fork ();
1031+ if (child < 0 ) {
1032+ return -1 ;
1033+ }
1034+
1035+ if (child == 0 ) {
1036+ if (setsid () == -1 )
1037+ return -1 ;
1038+ return 0 ;
1039+ } else {
1040+ swayidle_log (LOG_DEBUG , "Child forked, pid: %d" , child );
1041+ _exit (0 );
1042+ }
1043+ }
10191044
10201045int main (int argc , char * argv []) {
10211046 swayidle_init ();
@@ -1118,6 +1143,13 @@ int main(int argc, char *argv[]) {
11181143 display_event , NULL );
11191144 wl_event_source_check (source );
11201145
1146+ if (daemonize ) {
1147+ if (do_daemonize () != 0 ) {
1148+ swayidle_log_errno (LOG_ERROR , "Failed to daemonize, will exit!" );
1149+ sway_terminate (1 );
1150+ }
1151+ }
1152+
11211153 while (wl_event_loop_dispatch (state .event_loop , -1 ) != 1 ) {
11221154 // This space intentionally left blank
11231155 }
0 commit comments