Skip to content

Commit 9312fe6

Browse files
committed
common/common.c, common/nutipc.cpp, NEWS.adoc, docs/nut.dict: support writepid() calls by non-root programs [#1717]
Signed-off-by: Jim Klimov <jimklimov+nut@gmail.com>
1 parent bdc139d commit 9312fe6

File tree

4 files changed

+33
-4
lines changed

4 files changed

+33
-4
lines changed

NEWS.adoc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ https://github.com/networkupstools/nut/milestone/9
4141

4242
- (expected) Bug fixes for fallout possible due to "fightwarn" effort in 2.8.0+
4343

44+
- common code:
45+
* Revised common `writepid()` to use `altpidpath()` as location for the
46+
PID file creation, if the default `rootpidpath()` is not accessible
47+
(e.g. daemon was not initially started as `root`). Likewise updated
48+
short PID file based signal sending to consult both locations. [#1717]
49+
4450

4551
PLANNED: Release notes for NUT 2.8.3 - what's new since 2.8.2
4652
-------------------------------------------------------------

common/common.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1693,6 +1693,11 @@ void writepid(const char *name)
16931693

16941694
mask = umask(022);
16951695
pidf = fopen(fn, "w");
1696+
if (!pidf && initial_uid) {
1697+
intmax_t initial_uid = getuid();
1698+
snprintf(fn, sizeof(fn), "%s/%s.pid", altpidpath(), name);
1699+
pidf = fopen(fn, "w");
1700+
}
16961701

16971702
if (pidf) {
16981703
intmax_t pid = (intmax_t)getpid();
@@ -2060,9 +2065,17 @@ int snprintfcat(char *dst, size_t size, const char *fmt, ...)
20602065
#ifndef WIN32
20612066
int sendsignal(const char *progname, int sig, int check_current_progname)
20622067
{
2063-
char fn[SMALLBUF];
2068+
#ifdef UNIX_PATH_MAX
2069+
char fn[UNIX_PATH_MAX];
2070+
#else
2071+
char fn[PATH_MAX];
2072+
#endif
2073+
struct stat st;
20642074

20652075
snprintf(fn, sizeof(fn), "%s/%s.pid", rootpidpath(), progname);
2076+
if (stat(fn, &st) != 0) {
2077+
snprintf(fn, sizeof(fn), "%s/%s.pid", altpidpath(), progname);
2078+
}
20662079

20672080
return sendsignalfn(fn, sig, progname, check_current_progname);
20682081
}

common/nutipc.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
#include "nutstream.hpp"
4545

4646
#include <iostream>
47+
#include <sys/stat.h>
4748

4849

4950
namespace nut {
@@ -297,14 +298,21 @@ int Signal::send(Signal::enum_t signame, const std::string & pid_file) {
297298

298299
int NutSignal::send(NutSignal::enum_t signame, const std::string & process) {
299300
std::string pid_file;
301+
struct stat st;
300302

301-
// FIXME: What about ALTPIDPATH (for non-root daemons)
302-
// and shouldn't we also consider it (e.g. try/catch)?
303303
pid_file += rootpidpath();
304304
pid_file += '/';
305305
pid_file += process;
306306
pid_file += ".pid";
307307

308+
if (::stat(pid_file.c_str(), &st) != 0) {
309+
// Consider ALTPIDPATH (used by non-root daemons)
310+
pid_file = altpidpath();
311+
pid_file += '/';
312+
pid_file += process;
313+
pid_file += ".pid";
314+
}
315+
308316
return Signal::send(signame, pid_file);
309317
}
310318

docs/nut.dict

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
personal_ws-1.1 en 3270 utf-8
1+
personal_ws-1.1 en 3273 utf-8
22
AAC
33
AAS
44
ABI
@@ -2740,6 +2740,7 @@ roadmap
27402740
rootca
27412741
rootfs
27422742
rootfs'es
2743+
rootpidpath
27432744
rq
27442745
rqt
27452746
rsa
@@ -3215,6 +3216,7 @@ workspaceFolder
32153216
workspaces
32163217
writability
32173218
writeinfo
3219+
writepid
32183220
writeups
32193221
ws
32203222
xAAAA

0 commit comments

Comments
 (0)