Skip to content

Commit c4edc56

Browse files
mobrembskiMichał Obrembski
authored andcommitted
Added daemon run mode
1 parent 6b8f9fe commit c4edc56

2 files changed

Lines changed: 35 additions & 2 deletions

File tree

src/config.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ const struct vpn_config invalid_cfg = {
8383
.user_agent = NULL,
8484
.hostcheck = NULL,
8585
.check_virtual_desktop = NULL,
86+
.daemonize = 0
8687
};
8788

8889
/*
@@ -449,6 +450,15 @@ int load_config(struct vpn_config *cfg, const char *filename)
449450
} else if (strcmp(key, "check-virtual-desktop") == 0) {
450451
free(cfg->check_virtual_desktop);
451452
cfg->check_virtual_desktop = strdup(val);
453+
} else if (strcmp(key, "daemonize") == 0) {
454+
int daemonize = strtob(val);
455+
456+
if (daemonize < 0) {
457+
log_warn("Bad daemonize in config file: \"%s\".\n",
458+
val);
459+
continue;
460+
}
461+
cfg->daemonize = daemonize;
452462
} else {
453463
log_warn("Bad key in config file: \"%s\".\n", key);
454464
goto err_free;
@@ -600,4 +610,6 @@ void merge_config(struct vpn_config *dst, struct vpn_config *src)
600610
dst->hostcheck = src->hostcheck;
601611
if (src->check_virtual_desktop != invalid_cfg.check_virtual_desktop)
602612
dst->check_virtual_desktop = src->check_virtual_desktop;
613+
if (src->daemonize != invalid_cfg.daemonize)
614+
dst->daemonize = src->daemonize;
603615
}

src/main.c

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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-
187187
int 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

Comments
 (0)