diff --git a/NEWS.adoc b/NEWS.adoc index c4ad725faf..89ac2a67b9 100644 --- a/NEWS.adoc +++ b/NEWS.adoc @@ -41,6 +41,12 @@ https://github.com/networkupstools/nut/milestone/9 - (expected) Bug fixes for fallout possible due to "fightwarn" effort in 2.8.0+ + - common code: + * Revised common `writepid()` to use `altpidpath()` as location for the + PID file creation, if the default `rootpidpath()` is not accessible + (e.g. daemon was not initially started as `root`). Likewise updated + short PID file based signal sending to consult both locations. [#1717] + PLANNED: Release notes for NUT 2.8.3 - what's new since 2.8.2 ------------------------------------------------------------- diff --git a/common/common.c b/common/common.c index a71cb70f1d..4ab8978513 100644 --- a/common/common.c +++ b/common/common.c @@ -1684,6 +1684,7 @@ void writepid(const char *name) char fn[SMALLBUF]; FILE *pidf; mode_t mask; + intmax_t initial_uid = getuid(); /* use full path if present, else build filename in PIDPATH */ if (*name == '/') @@ -1693,6 +1694,10 @@ void writepid(const char *name) mask = umask(022); pidf = fopen(fn, "w"); + if (!pidf && initial_uid) { + snprintf(fn, sizeof(fn), "%s/%s.pid", altpidpath(), name); + pidf = fopen(fn, "w"); + } if (pidf) { intmax_t pid = (intmax_t)getpid(); @@ -2060,9 +2065,17 @@ int snprintfcat(char *dst, size_t size, const char *fmt, ...) #ifndef WIN32 int sendsignal(const char *progname, int sig, int check_current_progname) { - char fn[SMALLBUF]; +#ifdef UNIX_PATH_MAX + char fn[UNIX_PATH_MAX]; +#else + char fn[PATH_MAX]; +#endif + struct stat st; snprintf(fn, sizeof(fn), "%s/%s.pid", rootpidpath(), progname); + if (stat(fn, &st) != 0) { + snprintf(fn, sizeof(fn), "%s/%s.pid", altpidpath(), progname); + } return sendsignalfn(fn, sig, progname, check_current_progname); } diff --git a/common/nutipc.cpp b/common/nutipc.cpp index d8bf131271..7494dbb0bc 100644 --- a/common/nutipc.cpp +++ b/common/nutipc.cpp @@ -44,6 +44,7 @@ #include "nutstream.hpp" #include +#include namespace nut { @@ -297,14 +298,21 @@ int Signal::send(Signal::enum_t signame, const std::string & pid_file) { int NutSignal::send(NutSignal::enum_t signame, const std::string & process) { std::string pid_file; + struct stat st; - // FIXME: What about ALTPIDPATH (for non-root daemons) - // and shouldn't we also consider it (e.g. try/catch)? pid_file += rootpidpath(); pid_file += '/'; pid_file += process; pid_file += ".pid"; + if (::stat(pid_file.c_str(), &st) != 0) { + // Consider ALTPIDPATH (used by non-root daemons) + pid_file = altpidpath(); + pid_file += '/'; + pid_file += process; + pid_file += ".pid"; + } + return Signal::send(signame, pid_file); } diff --git a/docs/nut.dict b/docs/nut.dict index 1cc4c8f6cf..4602c15147 100644 --- a/docs/nut.dict +++ b/docs/nut.dict @@ -1,4 +1,4 @@ -personal_ws-1.1 en 3270 utf-8 +personal_ws-1.1 en 3273 utf-8 AAC AAS ABI @@ -2740,6 +2740,7 @@ roadmap rootca rootfs rootfs'es +rootpidpath rq rqt rsa @@ -3215,6 +3216,7 @@ workspaceFolder workspaces writability writeinfo +writepid writeups ws xAAAA