diff --git a/src/common.h b/src/common.h index bf41e47..07dfb21 100644 --- a/src/common.h +++ b/src/common.h @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include diff --git a/src/main.c b/src/main.c index 1821bec..4cdcf3b 100644 --- a/src/main.c +++ b/src/main.c @@ -22,7 +22,7 @@ struct { 0, 0, "/etc/f2b/f2b.conf", "/var/log/f2b.log", - "/var/run/f2b.pid", + "", }; bool run = true; @@ -76,6 +76,11 @@ update_opts_from_config(f2b_config_section_t *section) { opts.daemon = true; } + if ((pa = f2b_config_param_find(section->param, "pidfile")) != NULL) { + strncpy(opts.pidfile_path, pa->value, sizeof(opts.pidfile_path)); + opts.pidfile_path[sizeof(opts.pidfile_path) - 1] = '\0'; + } + /* setup logging */ if ((pa = f2b_config_param_find(section->param, "loglevel")) != NULL) f2b_log_set_level(pa->value); @@ -168,6 +173,19 @@ int main(int argc, char *argv[]) { } } + if (opts.pidfile_path[0] != '\0') { + FILE *pidfile = NULL; + if ((pidfile = fopen(opts.pidfile_path, "w")) != NULL) { + if (flock(fileno(pidfile), LOCK_EX | LOCK_NB) != 0) { + f2b_log_msg(log_error, "can't lock pidfile: %s", strerror(errno)); + exit(EXIT_FAILURE); + } + fprintf(pidfile, "%d\n", getpid()); + } else { + f2b_log_msg(log_warn, "can't open pidfile: %s", strerror(errno)); + } + } + if (config.defaults) f2b_jail_set_defaults(config.defaults);