Skip to content

Commit

Permalink
Merge pull request networkupstools#2407 from jimklimov/nut-upsmon
Browse files Browse the repository at this point in the history
Introduce a `rootpidpath()` method instead of direct use of `PIDPATH` macro (e.g. in `upsmon`)
  • Loading branch information
jimklimov authored Apr 18, 2024
2 parents e2ccc35 + aa1a31e commit d865f3e
Show file tree
Hide file tree
Showing 20 changed files with 285 additions and 42 deletions.
5 changes: 5 additions & 0 deletions NEWS.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ https://github.com/networkupstools/nut/milestone/11
* added an `OBLBDURATION` (seconds) setting to optionally delay raising
the alarm for immediate shutdown in critical situation. [#321]
- common code: root-owned daemons now use not the hard-coded `PIDPATH` value
set by the `configure` script during build, but can override it with a
`NUT_PIDPATH` environment variable in certain use-cases (such as tests).
[#2407]
Release notes for NUT 2.8.2 - what's new since 2.8.1
----------------------------------------------------
Expand Down
8 changes: 4 additions & 4 deletions clients/upsmon.c
Original file line number Diff line number Diff line change
Expand Up @@ -2100,9 +2100,9 @@ static void loadconfig(void)
upslogx(LOG_WARNING, "No POWERDOWNFLAG value was configured in %s!",
configfile);
upslogx(LOG_INFO,
"Should be a path to file that is normally writeable "
"for root user, and remains at least readable late "
"in shutdown after all unmounting completes.");
"POWERDOWNFLAG should be a path to file that is normally "
"writeable for root user, and remains at least readable "
"late in shutdown after all unmounting completes.");
}
}

Expand Down Expand Up @@ -3009,7 +3009,7 @@ int main(int argc, char *argv[])
* is running (error if it is).
*/
/* Hush the fopen(pidfile) message but let "real errors" be seen */
nut_sendsignal_debug_level = NUT_SENDSIGNAL_DEBUG_LEVEL_FOPEN_PIDFILE - 1;
nut_sendsignal_debug_level = NUT_SENDSIGNAL_DEBUG_LEVEL_KILL_SIG0PING - 1;
#ifndef WIN32
/* If cmd == 0 we are starting and check if a previous instance
* is running by sending signal '0' (i.e. 'kill <pid> 0' equivalent)
Expand Down
74 changes: 64 additions & 10 deletions common/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ void writepid(const char *name)
if (*name == '/')
snprintf(fn, sizeof(fn), "%s", name);
else
snprintf(fn, sizeof(fn), "%s/%s.pid", PIDPATH, name);
snprintf(fn, sizeof(fn), "%s/%s.pid", rootpidpath(), name);

mask = umask(022);
pidf = fopen(fn, "w");
Expand Down Expand Up @@ -487,7 +487,7 @@ int sendsignalpid(pid_t pid, int sig)
ret = kill(pid, 0);

if (ret < 0) {
if (nut_debug_level > 0 || nut_sendsignal_debug_level > 1)
if (nut_debug_level > 0 || nut_sendsignal_debug_level >= NUT_SENDSIGNAL_DEBUG_LEVEL_KILL_SIG0PING)
perror("kill");
return -1;
}
Expand Down Expand Up @@ -661,7 +661,7 @@ int sendsignal(const char *progname, int sig)
{
char fn[SMALLBUF];

snprintf(fn, sizeof(fn), "%s/%s.pid", PIDPATH, progname);
snprintf(fn, sizeof(fn), "%s/%s.pid", rootpidpath(), progname);

return sendsignalfn(fn, sig);
}
Expand Down Expand Up @@ -1446,7 +1446,13 @@ static void vupslog(int priority, const char *fmt, va_list va, int use_strerror)
/* Return the default path for the directory containing configuration files */
const char * confpath(void)
{
const char *path = getenv("NUT_CONFPATH");
static const char *path = NULL;

/* Cached by earlier calls? */
if (path)
return path;

path = getenv("NUT_CONFPATH");

#ifdef WIN32
if (path == NULL) {
Expand All @@ -1457,13 +1463,22 @@ const char * confpath(void)

/* We assume, here and elsewhere, that
* at least CONFPATH is always defined */
return (path != NULL && *path != '\0') ? path : CONFPATH;
if (path == NULL || *path == '\0')
path = CONFPATH;

return path;
}

/* Return the default path for the directory containing state files */
const char * dflt_statepath(void)
{
const char *path = getenv("NUT_STATEPATH");
static const char *path = NULL;

/* Cached by earlier calls? */
if (path)
return path;

path = getenv("NUT_STATEPATH");

#ifdef WIN32
if (path == NULL) {
Expand All @@ -1474,7 +1489,10 @@ const char * dflt_statepath(void)

/* We assume, here and elsewhere, that
* at least STATEPATH is always defined */
return (path != NULL && *path != '\0') ? path : STATEPATH;
if (path == NULL || *path == '\0')
path = STATEPATH;

return path;
}

/* Return the alternate path for pid files, for processes running as non-root
Expand All @@ -1485,7 +1503,11 @@ const char * dflt_statepath(void)
*/
const char * altpidpath(void)
{
const char * path;
static const char *path = NULL;

/* Cached by earlier calls? */
if (path)
return path;

path = getenv("NUT_ALTPIDPATH");
if ( (path == NULL) || (*path == '\0') ) {
Expand All @@ -1503,11 +1525,43 @@ const char * altpidpath(void)
return path;

#ifdef ALTPIDPATH
return ALTPIDPATH;
path = ALTPIDPATH;
#else
/* With WIN32 in the loop, this may be more than a fallback to STATEPATH: */
return dflt_statepath();
path = dflt_statepath();
#endif

return path;
}

/* Return the main path for pid files, for processes running as root, such
* as upsmon. Typically this is the built-in PIDPATH (from configure script)
* but certain use-cases such as the test suite can override it with the
* NUT_PIDPATH environment variable.
*/
const char * rootpidpath(void)
{
static const char *path = NULL;

/* Cached by earlier calls? */
if (path)
return path;

path = getenv("NUT_PIDPATH");

#ifdef WIN32
if (path == NULL) {
/* fall back to built-in pathname relative to binary/workdir */
path = getfullpath(PATH_ETC);
}
#endif

/* We assume, here and elsewhere, that
* at least PIDPATH is always defined */
if (path == NULL || *path == '\0')
path = PIDPATH;

return path;
}

/* Die with a standard message if socket filename is too long */
Expand Down
10 changes: 8 additions & 2 deletions common/nutipc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
Author: Vaclav Krpec <[email protected]>
Copyright (C) 2024 NUT Community
Author: Jim Klimov <[email protected]>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
Expand Down Expand Up @@ -35,6 +39,7 @@
# define HAVE_LOCALTIME_R 111
#endif

#include "common.h"
#include "nutipc.hpp"
#include "nutstream.hpp"

Expand Down Expand Up @@ -293,8 +298,9 @@ 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;

// TBD: What's ALTPIDPATH and shouldn't we also consider it?
pid_file += PIDPATH;
// 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";
Expand Down
1 change: 1 addition & 0 deletions common/nutstream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
Copyright (C)
2012 Vaclav Krpec <[email protected]>
2024 Jim Klimov <[email protected]>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand Down
2 changes: 1 addition & 1 deletion drivers/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -2118,7 +2118,7 @@ int main(int argc, char **argv)
*/

/* Hush the fopen(pidfile) message but let "real errors" be seen */
nut_sendsignal_debug_level = NUT_SENDSIGNAL_DEBUG_LEVEL_FOPEN_PIDFILE - 1;
nut_sendsignal_debug_level = NUT_SENDSIGNAL_DEBUG_LEVEL_KILL_SIG0PING - 1;

if (!cmd && (!do_forceshutdown)) {
ssize_t cmdret = -1;
Expand Down
4 changes: 2 additions & 2 deletions drivers/upsdrvctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ static void signal_driver_cmd(const ups_t *ups,
return;

/* Hush the fopen(pidfile) message but let "real errors" be seen */
nut_sendsignal_debug_level = NUT_SENDSIGNAL_DEBUG_LEVEL_FOPEN_PIDFILE - 1;
nut_sendsignal_debug_level = NUT_SENDSIGNAL_DEBUG_LEVEL_KILL_SIG0PING - 1;
#ifndef WIN32
if (ups->pid == -1) {
ret = sendsignalfn(pidfn, cmd);
Expand Down Expand Up @@ -377,7 +377,7 @@ static void stop_driver(const ups_t *ups)
return;

/* Hush the fopen(pidfile) message but let "real errors" be seen */
nut_sendsignal_debug_level = NUT_SENDSIGNAL_DEBUG_LEVEL_FOPEN_PIDFILE - 1;
nut_sendsignal_debug_level = NUT_SENDSIGNAL_DEBUG_LEVEL_KILL_SIG0PING - 1;

#ifndef WIN32
if (ups->pid == -1) {
Expand Down
6 changes: 5 additions & 1 deletion include/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -264,9 +264,12 @@ const char * confpath(void);
/* Return the default path for the directory containing state files */
const char * dflt_statepath(void);

/* Return the alternate path for pid files */
/* Return the alternate path for pid files (non-root daemons) */
const char * altpidpath(void);

/* Return the main path for pid files (root daemons like upsmon) */
const char * rootpidpath(void);

/* Die with a standard message if socket filename is too long */
void check_unix_socket_filename(const char *fn);

Expand Down Expand Up @@ -352,6 +355,7 @@ void nut_prepare_search_paths(void);
extern int nut_sendsignal_debug_level;
#define NUT_SENDSIGNAL_DEBUG_LEVEL_DEFAULT 6
#define NUT_SENDSIGNAL_DEBUG_LEVEL_FOPEN_PIDFILE 5
#define NUT_SENDSIGNAL_DEBUG_LEVEL_KILL_SIG0PING 4

extern int nut_debug_level;
extern int nut_log_level;
Expand Down
4 changes: 4 additions & 0 deletions include/nutipc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
Author: Vaclav Krpec <[email protected]>
Copyright (C) 2024 NUT Community
Author: Jim Klimov <[email protected]>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
Expand Down
1 change: 1 addition & 0 deletions include/nutstream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
Copyright (C)
2012 Vaclav Krpec <[email protected]>
2024 Jim Klimov <[email protected]>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand Down
1 change: 1 addition & 0 deletions include/nutwriter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
Copyright (C)
2012 Vaclav Krpec <[email protected]>
2024 Jim Klimov <[email protected]>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand Down
6 changes: 5 additions & 1 deletion server/upsd.c
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,8 @@ upstype_t *get_ups_ptr(const char *name)
upstype_t *tmp;

if (!name) {
upsdebugx(3, "%s: not a valid UPS: <null>",
__func__);
return NULL;
}

Expand All @@ -200,6 +202,8 @@ upstype_t *get_ups_ptr(const char *name)
}
}

upsdebugx(3, "%s: not a valid UPS: %s",
__func__, NUT_STRARG(name));
return NULL;
}

Expand Down Expand Up @@ -2033,7 +2037,7 @@ int main(int argc, char **argv)
* is running (error if it is).
*/
/* Hush the fopen(pidfile) message but let "real errors" be seen */
nut_sendsignal_debug_level = NUT_SENDSIGNAL_DEBUG_LEVEL_FOPEN_PIDFILE - 1;
nut_sendsignal_debug_level = NUT_SENDSIGNAL_DEBUG_LEVEL_KILL_SIG0PING - 1;
#ifndef WIN32
/* If cmd == 0 we are starting and check if a previous instance
* is running by sending signal '0' (i.e. 'kill <pid> 0' equivalent)
Expand Down
Loading

0 comments on commit d865f3e

Please sign in to comment.