Browse Source

* write pidfile if set

master
Alex 'AdUser' Z 9 years ago
parent
commit
c6adf086c0
  1. 1
      src/common.h
  2. 20
      src/main.c

1
src/common.h

@ -12,6 +12,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/file.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/types.h> #include <sys/types.h>
#include <time.h> #include <time.h>

20
src/main.c

@ -22,7 +22,7 @@ struct {
0, 0, 0, 0,
"/etc/f2b/f2b.conf", "/etc/f2b/f2b.conf",
"/var/log/f2b.log", "/var/log/f2b.log",
"/var/run/f2b.pid", "",
}; };
bool run = true; bool run = true;
@ -76,6 +76,11 @@ update_opts_from_config(f2b_config_section_t *section) {
opts.daemon = true; 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 */ /* setup logging */
if ((pa = f2b_config_param_find(section->param, "loglevel")) != NULL) if ((pa = f2b_config_param_find(section->param, "loglevel")) != NULL)
f2b_log_set_level(pa->value); 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) if (config.defaults)
f2b_jail_set_defaults(config.defaults); f2b_jail_set_defaults(config.defaults);

Loading…
Cancel
Save