@@ -144,7 +144,8 @@ PPPD_USAGE \
144144" certificate will be matched against this value.\n" \
145145" <digest> is the X509 certificate's sha256 sum.\n" \
146146" This option can be used multiple times to trust\n" \
147- " several certificates.\n"
147+ " several certificates.\n" \
148+ " --daemonize Run in daemon mode.\n"
148149
149150#define help_options_part2 \
150151" --insecure-ssl Do not disable insecure SSL protocols/ciphers.\n" \
@@ -183,13 +184,13 @@ PPPD_USAGE \
183184" trusted-cert = certificatedigest4daa8c5fe6c...\n" \
184185" trusted-cert = othercertificatedigest6631bf...\n" \
185186" For a full-featured config see man openfortivpn(1).\n"
186-
187187int main (int argc , char * * argv )
188188{
189189 int ret = EXIT_FAILURE ;
190190 const char * config_file = SYSCONFDIR "/openfortivpn/config" ;
191191 const char * host ;
192192 char * port_str ;
193+ pid_t process_id = 0 ;
193194
194195 struct vpn_config cfg = {
195196 .gateway_host = {'\0' },
@@ -208,6 +209,7 @@ int main(int argc, char **argv)
208209 .use_syslog = 0 ,
209210 .half_internet_routes = 0 ,
210211 .persistent = 0 ,
212+ .daemonize = 0 ,
211213#if HAVE_RESOLVCONF
212214 .use_resolvconf = USE_RESOLVCONF ,
213215#endif
@@ -267,6 +269,7 @@ int main(int argc, char **argv)
267269 {"cipher-list" , required_argument , NULL , 0 },
268270 {"min-tls" , required_argument , NULL , 0 },
269271 {"seclevel-1" , no_argument , & cli_cfg .seclevel_1 , 1 },
272+ {"daemonize" , no_argument , & cli_cfg .daemonize , 1 },
270273#if HAVE_USR_SBIN_PPPD
271274 {"pppd-use-peerdns" , required_argument , NULL , 0 },
272275 {"pppd-no-peerdns" , no_argument , & cli_cfg .pppd_use_peerdns , 0 },
@@ -563,6 +566,24 @@ int main(int argc, char **argv)
563566 }
564567 // Then apply CLI config
565568 merge_config (& cfg , & cli_cfg );
569+ if (cfg .daemonize ) {
570+ if (cfg .use_syslog == 0 ) {
571+ log_info ("Sorry, only syslog is available when running in Daemon mode" );
572+ cfg .use_syslog = 1 ;
573+ }
574+ process_id = fork ();
575+ // Indication of fork() failure
576+ if (process_id < 0 ) {
577+ printf ("Forking failure! Cannot start daemon!\n" );
578+ exit (1 );
579+ }
580+ // PARENT PROCESS. Need to kill it.
581+ if (process_id > 0 ) {
582+ printf ("Started as daemon with PID: %u\n" , process_id );
583+ /* Killing parent process */
584+ exit (0 );
585+ }
586+ }
566587 set_syslog (cfg .use_syslog );
567588
568589 // Read host and port from the command line
0 commit comments