forked from networkupstools/nut
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'upstream/master' into issue-2392
Signed-off-by: Jim Klimov <[email protected]>
- Loading branch information
Showing
8 changed files
with
139 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ | |
1999 Russell Kroll <[email protected]> | ||
2005 - 2017 Arnaud Quette <[email protected]> | ||
2017 Eaton (author: Emilien Kia <[email protected]>) | ||
2017 - 2022 Jim Klimov <[email protected]> | ||
2017 - 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 | ||
|
@@ -2194,12 +2194,16 @@ int main(int argc, char **argv) | |
nut_upsdrvquery_debug_level = NUT_UPSDRVQUERY_DEBUG_LEVEL_DEFAULT; | ||
} | ||
|
||
/* Hush the fopen(pidfile) message but let "real errors" be seen */ | ||
nut_sendsignal_debug_level = NUT_SENDSIGNAL_DEBUG_LEVEL_FOPEN_PIDFILE - 1; | ||
|
||
#ifndef WIN32 | ||
/* Setup PID file to receive signals to communicate with this driver | ||
* instance once backgrounded, and to stop a competing older instance. | ||
* Or to send it a signal deliberately. | ||
* instance once backgrounded (or staying foregrounded with `-FF`), | ||
* and to stop a competing older instance. Or to send it a signal | ||
* deliberately. | ||
*/ | ||
if (cmd || ((foreground == 0) && (!do_forceshutdown))) { | ||
if (cmd || ((foreground == 0 || foreground == 2) && (!do_forceshutdown))) { | ||
char pidfnbuf[SMALLBUF]; | ||
|
||
snprintf(pidfnbuf, sizeof(pidfnbuf), "%s/%s-%s.pid", altpidpath(), progname, upsname); | ||
|
@@ -2237,21 +2241,21 @@ int main(int argc, char **argv) | |
/* if cmd was nontrivial - speak up below, else be quiet */ | ||
upsdebugx(1, "Just failed to send signal, no daemon was running"); | ||
break; | ||
} | ||
} /* switch (cmdret) */ | ||
|
||
/* We were signalling a daemon, successfully or not - exit now... | ||
* Modulo the possibility of a "reload-or-something" where we | ||
* effectively terminate the old driver and start a new one due | ||
* to configuration changes that were not reloadable. Such mode | ||
* is not implemented currently. | ||
*/ | ||
if (cmdret != 0) { | ||
/* sendsignal*() above might have logged more details | ||
* for troubleshooting, e.g. about lack of PID file | ||
/* We were signalling a daemon, successfully or not - exit now... | ||
* Modulo the possibility of a "reload-or-something" where we | ||
* effectively terminate the old driver and start a new one due | ||
* to configuration changes that were not reloadable. Such mode | ||
* is not implemented currently. | ||
*/ | ||
upslogx(LOG_NOTICE, "Failed to signal the currently running daemon (if any)"); | ||
if (cmdret != 0) { | ||
/* sendsignal*() above might have logged more details | ||
* for troubleshooting, e.g. about lack of PID file | ||
*/ | ||
upslogx(LOG_NOTICE, "Failed to signal the currently running daemon (if any)"); | ||
# ifdef HAVE_SYSTEMD | ||
switch (cmd) { | ||
switch (cmd) { | ||
case SIGCMD_RELOAD: | ||
upslogx(LOG_NOTICE, "Try something like " | ||
"'systemctl reload nut-driver@%s.service'%s", | ||
|
@@ -2276,7 +2280,7 @@ int main(int argc, char **argv) | |
upsname, | ||
(oldpid < 0 ? " or add '-P $PID' argument" : "")); | ||
break; | ||
} | ||
} /* switch (cmd) */ | ||
/* ... or edit nut-server.service locally to start `upsd -FF` | ||
* and so save the PID file for ability to manage the daemon | ||
* beside the service framework, possibly confusing things... | ||
|
@@ -2286,30 +2290,34 @@ int main(int argc, char **argv) | |
upslogx(LOG_NOTICE, "Try to add '-P $PID' argument"); | ||
} | ||
# endif /* HAVE_SYSTEMD */ | ||
} | ||
} /* if (cmdret != 0) */ | ||
|
||
exit((cmdret == 0) ? EXIT_SUCCESS : EXIT_FAILURE); | ||
} | ||
} /* if (cmd) */ | ||
|
||
/* Try to prevent that driver is started multiple times. If a PID file */ | ||
/* already exists, send a TERM signal to the process and try if it goes */ | ||
/* away. If not, retry a couple of times. */ | ||
for (i = 0; i < 3; i++) { | ||
struct stat st; | ||
int sigret; | ||
|
||
if (stat(pidfnbuf, &st) != 0) { | ||
/* PID file not found */ | ||
if ((sigret = stat(pidfnbuf, &st)) != 0) { | ||
upsdebugx(1, "PID file %s not found; stat() returned %d (errno=%d): %s", | ||
pidfnbuf, sigret, errno, strerror(errno)); | ||
break; | ||
} | ||
|
||
upslogx(LOG_WARNING, "Duplicate driver instance detected (PID file %s exists)! Terminating other driver!", pidfnbuf); | ||
|
||
if (sendsignalfn(pidfnbuf, SIGTERM) != 0) { | ||
/* Can't send signal to PID, assume invalid file */ | ||
if ((sigret = sendsignalfn(pidfnbuf, SIGTERM) != 0)) { | ||
upsdebugx(1, "Can't send signal to PID, assume invalid PID file %s; " | ||
"sendsignalfn() returned %d (errno=%d): %s", | ||
pidfnbuf, sigret, errno, strerror(errno)); | ||
break; | ||
} | ||
|
||
/* Allow driver some time to quit */ | ||
upsdebugx(1, "Signal sent without errors, allow the other driver instance some time to quit"); | ||
sleep(5); | ||
|
||
if (exit_flag) | ||
|
@@ -2387,6 +2395,9 @@ int main(int argc, char **argv) | |
} | ||
#endif /* WIN32 */ | ||
|
||
/* Restore the signal errors verbosity */ | ||
nut_sendsignal_debug_level = NUT_SENDSIGNAL_DEBUG_LEVEL_DEFAULT; | ||
|
||
/* clear out callback handler data */ | ||
memset(&upsh, '\0', sizeof(upsh)); | ||
|
||
|
@@ -2613,6 +2624,7 @@ int main(int argc, char **argv) | |
snprintf(pidfnbuf, sizeof(pidfnbuf), "%s/%s-%s.pid", altpidpath(), progname, upsname); | ||
pidfn = xstrdup(pidfnbuf); | ||
} | ||
/* Probably was saved above already, but better safe than sorry */ | ||
upslogx(LOG_WARNING, "Running as foreground process, but saving a PID file anyway"); | ||
writepid(pidfn); | ||
break; | ||
|
Oops, something went wrong.