Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support writepid() calls by non-root programs to try using altpidpath() if rootpidpath() is not accessible #2745

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions NEWS.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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
-------------------------------------------------------------
Expand Down
15 changes: 14 additions & 1 deletion common/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 == '/')
Expand All @@ -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();
Expand Down Expand Up @@ -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);
}
Expand Down
12 changes: 10 additions & 2 deletions common/nutipc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
#include "nutstream.hpp"

#include <iostream>
#include <sys/stat.h>


namespace nut {
Expand Down Expand Up @@ -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);
}

Expand Down
4 changes: 3 additions & 1 deletion docs/nut.dict
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
personal_ws-1.1 en 3270 utf-8
personal_ws-1.1 en 3273 utf-8
AAC
AAS
ABI
Expand Down Expand Up @@ -2740,6 +2740,7 @@ roadmap
rootca
rootfs
rootfs'es
rootpidpath
rq
rqt
rsa
Expand Down Expand Up @@ -3215,6 +3216,7 @@ workspaceFolder
workspaces
writability
writeinfo
writepid
writeups
ws
xAAAA
Expand Down
Loading