X-Git-Url: http://git.madism.org/?a=blobdiff_plain;f=main-srsd.c;h=c3549240d2f3f2b4b1ce885faddb2a9995d723bf;hb=ab8bc5aee397b411abd0358171e990e8bc4647d6;hp=8d703aff9cb2ee74890c3f6433f824e394e04e8c;hpb=59b31c88179af4a9a9c9074a7b428841ce8661f8;p=apps%2Fpfixtools.git diff --git a/main-srsd.c b/main-srsd.c index 8d703af..c354924 100644 --- a/main-srsd.c +++ b/main-srsd.c @@ -42,8 +42,8 @@ #include "buffer.h" #define DAEMON_NAME "pfix-srsd" -#define DEFAULT_ENCODER_PORT 10000 -#define DEFAULT_DECODER_PORT 10001 +#define DEFAULT_ENCODER_PORT 10001 +#define DEFAULT_DECODER_PORT 10002 #define RUNAS_USER "nobody" #define RUNAS_GROUP "nogroup" @@ -221,6 +221,7 @@ void usage(void) " (default: "STR(DEFAULT_DECODER_PORT)")\n" " -p file to write our pid to\n" " -u unsafe mode: don't drop privilegies\n" + " -f stay in foreground\n" , stderr); } @@ -228,8 +229,6 @@ void usage(void) int main_loop(srs_t *srs, const char *domain, int port_enc, int port_dec) { - int exitcode = EXIT_SUCCESS; - if (start_listener(port_enc, false) < 0) return EXIT_FAILURE; if (start_listener(port_dec, true) < 0) @@ -243,8 +242,7 @@ int main_loop(srs_t *srs, const char *domain, int port_enc, int port_dec) if (n < 0) { if (errno != EAGAIN && errno != EINTR) { UNIXERR("epoll_wait"); - exitcode = EXIT_FAILURE; - break; + return EXIT_FAILURE; } continue; } @@ -318,7 +316,7 @@ int main_loop(srs_t *srs, const char *domain, int port_enc, int port_dec) } } - return exitcode; + return EXIT_SUCCESS; } static srs_t *srs_read_secrets(const char *sfile) @@ -365,19 +363,22 @@ static srs_t *srs_read_secrets(const char *sfile) int main(int argc, char *argv[]) { bool unsafe = false; + bool daemonize = true; int port_enc = DEFAULT_ENCODER_PORT; int port_dec = DEFAULT_DECODER_PORT; const char *pidfile = NULL; - FILE *f = NULL; int res; srs_t *srs; - for (int c = 0; (c = getopt(argc, argv, "hu" "e:d:p:")) >= 0; ) { + for (int c = 0; (c = getopt(argc, argv, "hfu" "e:d:p:")) >= 0; ) { switch (c) { case 'e': port_enc = atoi(optarg); break; + case 'f': + daemonize = false; + break; case 'd': port_dec = atoi(optarg); break; @@ -403,13 +404,9 @@ int main(int argc, char *argv[]) return EXIT_FAILURE; } - if (pidfile) { - f = fopen(pidfile, "w"); - if (!f) { - syslog(LOG_CRIT, "unable to write pidfile %s", pidfile); - } - fprintf(f, "%d\n", getpid()); - fflush(f); + if (pidfile_open(pidfile) < 0) { + syslog(LOG_CRIT, "unable to write pidfile %s", pidfile); + return EXIT_FAILURE; } if (!unsafe && drop_privileges(RUNAS_USER, RUNAS_GROUP) < 0) { @@ -417,24 +414,13 @@ int main(int argc, char *argv[]) return EXIT_FAILURE; } - if (daemon_detach() < 0) { + if (daemonize && daemon_detach() < 0) { syslog(LOG_CRIT, "unable to fork"); return EXIT_FAILURE; } - if (f) { - rewind(f); - ftruncate(fileno(f), 0); - fprintf(f, "%d\n", getpid()); - fflush(f); - } + pidfile_refresh(); res = main_loop(srs, argv[optind], port_enc, port_dec); - if (f) { - rewind(f); - ftruncate(fileno(f), 0); - fclose(f); - f = NULL; - } syslog(LOG_INFO, "Stopping..."); return res; }