Skip to content

Commit 684f19b

Browse files
authored
Merge pull request networkupstools#2896 from jimklimov/issue-2890
Update `upssched` and call it from NIT
2 parents 29fb7ee + 4ab678c commit 684f19b

File tree

9 files changed

+250
-14
lines changed

9 files changed

+250
-14
lines changed

NEWS.adoc

+9
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,15 @@ This requirement compromises usability of `make distcheck` on platforms without
389389
(UPS overloaded), `TRIM` and `BOOST` (adjusting for bad input voltage)
390390
states. [#1074]
391391

392+
- Revised `upssched` timer handler that can be called from `upsmon` as its
393+
`NOTIFYCMD` to not report confusing environment variable values of
394+
`NOTIFYTYPE` and `UPSNAME` from the original call when a timer eventually
395+
fires -- these values are irrelevant at that distant future. The NIT (NUT
396+
Integration Tests) suite was extended to configure and call this tool,
397+
facilitating its development and troubleshooting. Also the `upssched`
398+
timer daemon part can now save its PID file (so that NIT can terminate
399+
it after tests). [#2890]
400+
392401
- New `libupsclient` API methods added:
393402
* `upscli_str_add_unique_token()` and `upscli_str_contains_token()`,
394403
to help C NUT clients process `ups.status` and similarly structured

clients/upssched-cmd

+6-2
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,13 @@
1414

1515
# Feel free to remove or comment away these lines when you actually
1616
# customize the script for your deployment:
17-
echo "THIS IS A SAMPLE SCRIPT, PLEASE TAILOR IT FOR YOUR DEPLOYMENT OF NUT!" >&2
17+
echo "`date -u`: $0: THIS IS A SAMPLE SCRIPT, PLEASE TAILOR IT FOR YOUR DEPLOYMENT OF NUT!" >&2
1818
logger -t upssched-cmd "THIS IS A SAMPLE SCRIPT, PLEASE TAILOR IT FOR YOUR DEPLOYMENT OF NUT!"
19-
printf "NOTIFYTYPE='%s' args=%s\n" "$NOTIFYTYPE" "$@" | logger -t upssched-cmd-received-NOTIFYTYPE
19+
20+
printf "`date -u`: UPSNAME='%s'\tNOTIFYTYPE='%s'\targs=%s\n" "$UPSNAME" "$NOTIFYTYPE" "$@" >&2
21+
printf "UPSNAME='%s' NOTIFYTYPE='%s' args=%s\n" "$UPSNAME" "$NOTIFYTYPE" "$@" | logger -t upssched-cmd-received-NOTIFYTYPE
22+
23+
#set
2024

2125
# Handle received timer names, as defined in your upssched.conf (example):
2226
case "$1" in

clients/upssched.c

+21-2
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ static conn_t *connhead = NULL;
7171
static char *cmdscript = NULL, *pipefn = NULL, *lockfn = NULL;
7272

7373
/* ups name and notify type (string) as received from upsmon */
74-
static const char *upsname, *notify_type;
74+
static const char *upsname, *notify_type, *prog = NULL;
7575

7676
#ifdef WIN32
7777
static OVERLAPPED connect_overlapped;
@@ -873,6 +873,7 @@ static void start_daemon(TYPE_FD lockfd)
873873
# endif /* not HAVE_DUP */
874874
# endif /* not HAVE_DUP2 */
875875

876+
/* Still in child, non-WIN32 - work as timer daemon (infinite loop) */
876877
pipefd = open_sock();
877878

878879
if (nut_debug_level)
@@ -884,6 +885,13 @@ static void start_daemon(TYPE_FD lockfd)
884885
/* drop the lock now that the background is running */
885886
unlink(lockfn);
886887
close(lockfd);
888+
writepid(prog);
889+
890+
/* Whatever upsmon envvars were set when this daemon started, would be
891+
* irrelevant and only confusing at the moment a particular timer causes
892+
* CMDSCRIPT to run */
893+
unsetenv("NOTIFYTYPE");
894+
unsetenv("UPSNAME");
887895

888896
/* now watch for activity */
889897
upsdebugx(2, "Timer daemon waiting for connections on pipefd %d",
@@ -988,6 +996,13 @@ static void start_daemon(TYPE_FD lockfd)
988996
/* drop the lock now that the background is running */
989997
CloseHandle(lockfd);
990998
DeleteFile(lockfn);
999+
writepid(prog);
1000+
1001+
/* Whatever upsmon envvars were set when this daemon started, would be
1002+
* irrelevant and only confusing at the moment a particular timer causes
1003+
* CMDSCRIPT to run */
1004+
unsetenv("NOTIFYTYPE");
1005+
unsetenv("UPSNAME");
9911006

9921007
/* now watch for activity */
9931008

@@ -1513,9 +1528,13 @@ static void help(const char *arg_progname)
15131528

15141529
int main(int argc, char **argv)
15151530
{
1516-
const char *prog = xbasename(argv[0]);
15171531
int i;
15181532

1533+
if (argc > 0)
1534+
prog = xbasename(argv[0]);
1535+
if (!prog)
1536+
prog = "upssched";
1537+
15191538
while ((i = getopt(argc, argv, "+DVh")) != -1) {
15201539
switch (i) {
15211540
case 'D':

common/unsetenv.c

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/* unsetenv.c Jim Klimov <[email protected]> */
2+
#include "config.h" /* must be first */
3+
4+
#ifndef HAVE_UNSETENV
5+
#include <stdlib.h>
6+
#include <string.h>
7+
#include "common.h"
8+
#include "proto.h"
9+
10+
int nut_unsetenv(const char *name)
11+
{
12+
return setenv(name, "", 1);
13+
}
14+
#endif /* !HAVE_UNSETENV */

configure.ac

+1-1
Original file line numberDiff line numberDiff line change
@@ -1465,7 +1465,7 @@ AC_CHECK_FUNCS(vsnprintf snprintf, [], [
14651465
AC_TYPE_LONG_LONG_INT
14661466
])
14671467

1468-
AC_REPLACE_FUNCS(setenv strerror atexit)
1468+
AC_REPLACE_FUNCS(setenv unsetenv strerror atexit)
14691469

14701470
case ${target_os} in
14711471
solaris2* )

include/proto.h

+7
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,13 @@ static inline int setenv(const char *name, const char *value, int overwrite) {
9494
}
9595
#endif
9696

97+
#ifndef HAVE_UNSETENV
98+
int nut_unsetenv(const char *name);
99+
static inline int unsetenv(const char *name) {
100+
return nut_unsetenv(name);
101+
}
102+
#endif
103+
97104
#ifdef __hpux
98105
#ifdef HAVE_SYS_MODEM_H
99106
#include <sys/modem.h>

scripts/misc/notifyme-debug

+12-1
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,21 @@
88
# Sat Apr 6 17:23:06 UTC 2024 [uid=77(nut) gid=77(nut) groups=77(nut)] COMMBAD [eco650]: Communications with UPS eco650 lost (1 tokens)
99
# Sat Apr 6 17:28:01 UTC 2024 [uid=77(nut) gid=77(nut) groups=77(nut)] NOCOMM [eco650]: UPS eco650 is unavailable (1 tokens)
1010
#
11-
# Copyright (C) 2023-2024 by Jim Klimov <[email protected]>
11+
# Copyright (C) 2023-2025 by Jim Klimov <[email protected]>
1212
# Licensed under the terms of the Network UPS Tools source license (GPLv2+)
1313

1414
[ -n "${TEMPDIR-}" ] || TEMPDIR="${TMPDIR-}"
1515
[ -n "${TEMPDIR-}" ] || { [ -d "/dev/shm" && TEMPDIR="/dev/shm" || TEMPDIR="/tmp" ; }
1616

1717
printf '%s\t[%s]\t%s\t[%s]:\t%s\t(%s tokens)\n' "`date -u`" "`id`" "${NOTIFYTYPE-}" "${UPSNAME-}" "$*" "$#" >> "${TEMPDIR}/notifyme-`id -u`.log"
18+
19+
if [ -n "${TOP_BUILDDIR}" -a -x "${TOP_BUILDDIR}/clients/upssched" ] ; then
20+
if [ -n "${NUT_DEBUG_LEVEL_UPSSCHED}" ] ; then
21+
NUT_DEBUG_LEVEL="${NUT_DEBUG_LEVEL_UPSSCHED}"
22+
export NUT_DEBUG_LEVEL
23+
fi
24+
if [ "${NUT_DEBUG_LEVEL-}" -gt 0 ] 2>/dev/null ; then
25+
printf '%s: %s\t%s\t[%s]:\targs: %s\t(%s arg tokens)\n' "`date -u`" "$0" "${NOTIFYTYPE-}" "${UPSNAME-}" "$*" "$#" >&2
26+
fi
27+
"${TOP_BUILDDIR}/clients/upssched"
28+
fi

0 commit comments

Comments
 (0)