@@ -142,7 +142,8 @@ PPPD_USAGE \
142142" certificate will be matched against this value.\n" \
143143" <digest> is the X509 certificate's sha256 sum.\n" \
144144" This option can be used multiple times to trust\n" \
145- " several certificates.\n"
145+ " several certificates.\n" \
146+ " --daemonize Run in daemon mode.\n"
146147
147148#define help_options_part2 \
148149" --insecure-ssl Do not disable insecure SSL protocols/ciphers.\n" \
@@ -181,13 +182,13 @@ PPPD_USAGE \
181182" trusted-cert = certificatedigest4daa8c5fe6c...\n" \
182183" trusted-cert = othercertificatedigest6631bf...\n" \
183184" For a full-featured config see man openfortivpn(1).\n"
184-
185185int main (int argc , char * * argv )
186186{
187187 int ret = EXIT_FAILURE ;
188188 const char * config_file = SYSCONFDIR "/openfortivpn/config" ;
189189 const char * host ;
190190 char * port_str ;
191+ pid_t process_id = 0 ;
191192
192193 struct vpn_config cfg = {
193194 .gateway_host = {'\0' },
@@ -206,6 +207,7 @@ int main(int argc, char **argv)
206207 .use_syslog = 0 ,
207208 .half_internet_routes = 0 ,
208209 .persistent = 0 ,
210+ .daemonize = 0 ,
209211#if HAVE_RESOLVCONF
210212 .use_resolvconf = USE_RESOLVCONF ,
211213#endif
@@ -265,6 +267,7 @@ int main(int argc, char **argv)
265267 {"cipher-list" , required_argument , NULL , 0 },
266268 {"min-tls" , required_argument , NULL , 0 },
267269 {"seclevel-1" , no_argument , & cli_cfg .seclevel_1 , 1 },
270+ {"daemonize" , no_argument , & cli_cfg .daemonize , 1 },
268271#if HAVE_USR_SBIN_PPPD
269272 {"pppd-use-peerdns" , required_argument , NULL , 0 },
270273 {"pppd-no-peerdns" , no_argument , & cli_cfg .pppd_use_peerdns , 0 },
@@ -567,6 +570,24 @@ int main(int argc, char **argv)
567570
568571 // Then apply CLI config
569572 merge_config (& cfg , & cli_cfg );
573+ if (cfg .daemonize ) {
574+ if (cfg .use_syslog == 0 ) {
575+ log_info ("Sorry, only syslog is available when running in Daemon mode" );
576+ cfg .use_syslog = 1 ;
577+ }
578+ process_id = fork ();
579+ // Indication of fork() failure
580+ if (process_id < 0 ) {
581+ printf ("Forking failure! Cannot start daemon!\n" );
582+ exit (1 );
583+ }
584+ // PARENT PROCESS. Need to kill it.
585+ if (process_id > 0 ) {
586+ printf ("Started as daemon with PID: %u\n" , process_id );
587+ /* Killing parent process */
588+ exit (0 );
589+ }
590+ }
570591 set_syslog (cfg .use_syslog );
571592
572593 // Read host and port from the command line
0 commit comments