From e007e857621489795b0d804aa3df2c509efadb4d Mon Sep 17 00:00:00 2001 From: john30 Date: Sat, 31 Jan 2015 11:39:52 +0100 Subject: [PATCH] added PID file location to autoconf (i.e. use localstatedir/var), fixed empty PID file and permissions --- configure.ac | 4 +++- src/ebusd/Makefile.am | 3 ++- src/ebusd/main.cpp | 13 +++++++++---- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/configure.ac b/configure.ac index f2ca611a..9ae994e8 100644 --- a/configure.ac +++ b/configure.ac @@ -31,7 +31,7 @@ AC_CHECK_FUNC([pselect], [AC_DEFINE(HAVE_PSELECT, [1], ["Define to 1 if pselect( AC_CHECK_FUNC([ppoll], [AC_DEFINE(HAVE_PPOLL, [1], ["Define to 1 if ppoll() is available."])]) AC_CONFIG_AUX_DIR([build]) -AC_CONFIG_SRCDIR([src/ebusd/ebusd.cpp]) +AC_CONFIG_SRCDIR([src/ebusd/main.cpp]) AC_CONFIG_HEADERS([config.h]) AC_CONFIG_FILES([Makefile docs/Makefile @@ -41,6 +41,8 @@ AC_CONFIG_FILES([Makefile src/ebusd/Makefile src/tools/Makefile]) +AC_DEFINE_UNQUOTED(PACKAGE_PIDFILE, LOCALSTATEDIR "/" PACKAGE ".pid", [The name of the PID file.]) + AC_CHECK_PROGS([HAVE_DOXYGEN], [doxygen]) if test -z "$HAVE_DOXYGEN"; then AC_MSG_WARN([Doxygen not found - continuing without Doxygen support.]) diff --git a/src/ebusd/Makefile.am b/src/ebusd/Makefile.am index ca01fd59..592eb51f 100644 --- a/src/ebusd/Makefile.am +++ b/src/ebusd/Makefile.am @@ -2,7 +2,8 @@ AM_CXXFLAGS = -fpic \ -Wall \ -Wextra \ -isystem$(top_srcdir)/src/lib/utils \ - -isystem$(top_srcdir)/src/lib/ebus + -isystem$(top_srcdir)/src/lib/ebus \ + -DLOCALSTATEDIR=\"$(localstatedir)\" bin_PROGRAMS = ebusd diff --git a/src/ebusd/main.cpp b/src/ebusd/main.cpp index cf0a0272..257708c6 100644 --- a/src/ebusd/main.cpp +++ b/src/ebusd/main.cpp @@ -34,7 +34,11 @@ #include /** the name of the PID file. */ +#ifdef PACKAGE_PIDFILE +#define PID_FILE_NAME PACKAGE_PIDFILE +#else #define PID_FILE_NAME "/var/run/ebusd.pid" +#endif /** the opened PID file, or NULL. */ static FILE* pidFile = NULL; @@ -297,7 +301,7 @@ void daemonize() // Change the current working directory. This prevents the current // directory from being locked; hence not being able to remove it. - if (chdir("/tmp") < 0) { // TODO + if (chdir("/tmp") < 0) { // TODO use constant logError(lf_main, "daemon chdir() failed"); exit(EXIT_FAILURE); } @@ -308,11 +312,12 @@ void daemonize() close(STDERR_FILENO); // create pid file and try to lock it - umask(077); // leads to pidFile created in mode 0600 - pidFile = fopen(PID_FILE_NAME, "w"); - umask(027); // Set file permissions 750 + pidFile = fopen(PID_FILE_NAME, "w+"); + + umask(S_IWGRP | S_IRWXO); // set permissions of newly created files to 750 if (pidFile != NULL) { + setbuf(pidFile, NULL); // disable buffering if (lockf(fileno(pidFile), F_TLOCK, 0) < 0 || fprintf(pidFile, "%d\n", getpid()) <=0) { fclose(pidFile);