Skip to content

Commit b5626fd

Browse files
mobrembskiMichał Obrembski
authored andcommitted
Added daemon run mode
1 parent 1156d2b commit b5626fd

3 files changed

Lines changed: 36 additions & 2 deletions

File tree

src/config.c

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

8990
/*
@@ -452,6 +453,15 @@ int load_config(struct vpn_config *cfg, const char *filename)
452453
} else if (strcmp(key, "check-virtual-desktop") == 0) {
453454
free(cfg->check_virtual_desktop);
454455
cfg->check_virtual_desktop = strdup(val);
456+
} else if (strcmp(key, "daemonize") == 0) {
457+
int daemonize = strtob(val);
458+
459+
if (daemonize < 0) {
460+
log_warn("Bad daemonize in config file: \"%s\".\n",
461+
val);
462+
continue;
463+
}
464+
cfg->daemonize = daemonize;
455465
} else {
456466
log_warn("Bad key in config file: \"%s\".\n", key);
457467
goto err_free;
@@ -604,4 +614,6 @@ void merge_config(struct vpn_config *dst, struct vpn_config *src)
604614
dst->hostcheck = src->hostcheck;
605615
if (src->check_virtual_desktop != invalid_cfg.check_virtual_desktop)
606616
dst->check_virtual_desktop = src->check_virtual_desktop;
617+
if (src->daemonize != invalid_cfg.daemonize)
618+
dst->daemonize = src->daemonize;
607619
}

src/config.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ struct vpn_config {
129129
char *user_agent;
130130
char *hostcheck;
131131
char *check_virtual_desktop;
132+
int daemonize;
132133
};
133134

134135
int add_trusted_cert(struct vpn_config *cfg, const char *digest);

src/main.c

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

Comments
 (0)