Added
Link Here
|
1 |
--- clients/upslog.c.orig 2022-08-21 09:28:11.630024000 -0700 |
2 |
+++ clients/upslog.c 2022-08-21 10:23:05.691271000 -0700 |
3 |
@@ -32,6 +32,10 @@ |
4 |
*/ |
5 |
|
6 |
#include "common.h" |
7 |
+#include <signal.h> |
8 |
+#include <unistd.h> |
9 |
+#include <sys/types.h> |
10 |
+#include <sys/wait.h> |
11 |
#include "nut_platform.h" |
12 |
#include "upsclient.h" |
13 |
|
14 |
@@ -41,17 +45,32 @@ |
15 |
#include "upslog.h" |
16 |
|
17 |
static int reopen_flag = 0, exit_flag = 0; |
18 |
+ static int wait_status, child_pid; |
19 |
static uint16_t port; |
20 |
static char *upsname, *hostname; |
21 |
static UPSCONN_t ups; |
22 |
|
23 |
static FILE *logfile; |
24 |
- static const char *logfn, *monhost; |
25 |
+ static char *logfn, *monhost; |
26 |
static sigset_t nut_upslog_sigmask; |
27 |
static char logbuffer[LARGEBUF], *logformat; |
28 |
|
29 |
static flist_t *fhead = NULL; |
30 |
+ struct monhost_child { |
31 |
+ char *monhost; |
32 |
+ char *logfn; |
33 |
+ char *pidfilebase; |
34 |
+ pid_t pid; |
35 |
+ struct monhost_child *next; |
36 |
+ }; |
37 |
+ static struct monhost_child *monhost_child_anchor = NULL; |
38 |
+ static struct monhost_child *monhost_child_current; |
39 |
+ static struct monhost_child *monhost_child_prev = NULL; |
40 |
+ static char *m_arg; |
41 |
+ static struct sigaction upslog_sigaction; |
42 |
+ static int trapped_signals[] = { SIGINT, SIGTERM, SIGCHLD }; |
43 |
|
44 |
+ |
45 |
#define DEFAULT_LOGFORMAT "%TIME @Y@m@d @H@M@S% %VAR battery.charge% " \ |
46 |
"%VAR input.voltage% %VAR ups.load% [%VAR ups.status%] " \ |
47 |
"%VAR ups.temperature% %VAR input.frequency%" |
48 |
@@ -393,21 +412,33 @@ |
49 |
* -u <username> |
50 |
*/ |
51 |
|
52 |
+static void term_handler(int signo) |
53 |
+{ |
54 |
+ if (signo != SIGCHLD && monhost_child_anchor != NULL) { |
55 |
+ for (monhost_child_current = monhost_child_anchor; |
56 |
+ monhost_child_current != NULL; |
57 |
+ monhost_child_current = monhost_child_current->next) |
58 |
+ kill(monhost_child_current->pid, signo); |
59 |
+ exit(signo); |
60 |
+ } |
61 |
+} |
62 |
+ |
63 |
int main(int argc, char **argv) |
64 |
{ |
65 |
int interval = 30, i, foreground = -1; |
66 |
+ size_t monhost_len; |
67 |
const char *prog = xbasename(argv[0]); |
68 |
time_t now, nextpoll = 0; |
69 |
const char *user = NULL; |
70 |
struct passwd *new_uid = NULL; |
71 |
- const char *pidfilebase = prog; |
72 |
+ char *pidfilebase = prog; |
73 |
|
74 |
logformat = DEFAULT_LOGFORMAT; |
75 |
user = RUN_AS_USER; |
76 |
|
77 |
printf("Network UPS Tools %s %s\n", prog, UPS_VERSION); |
78 |
|
79 |
- while ((i = getopt(argc, argv, "+hs:l:i:f:u:Vp:FB")) != -1) { |
80 |
+ while ((i = getopt(argc, argv, "+hs:l:i:f:u:Vp:FBm:")) != -1) { |
81 |
switch(i) { |
82 |
case 'h': |
83 |
help(prog); |
84 |
@@ -415,6 +446,19 @@ |
85 |
break; |
86 |
#endif |
87 |
|
88 |
+ case 'm': |
89 |
+ monhost_child_prev = monhost_child_current; |
90 |
+ monhost_child_current = malloc(sizeof(struct monhost_child)); |
91 |
+ if (monhost_child_anchor == NULL) |
92 |
+ monhost_child_anchor = monhost_child_current; |
93 |
+ else |
94 |
+ monhost_child_prev->next = monhost_child_current; |
95 |
+ monhost_child_current->next = NULL; |
96 |
+ m_arg = optarg; |
97 |
+ monhost_child_current->monhost = strsep(&m_arg, ":"); |
98 |
+ monhost_child_current->logfn = strsep(&m_arg, ":"); |
99 |
+ monhost_child_current->pidfilebase = strsep(&m_arg, ":"); |
100 |
+ break; |
101 |
case 's': |
102 |
monhost = optarg; |
103 |
break; |
104 |
@@ -477,6 +521,29 @@ |
105 |
|
106 |
for (i = 3; i < argc; i++) |
107 |
snprintfcat(logformat, LARGEBUF, "%s ", argv[i]); |
108 |
+ } |
109 |
+ |
110 |
+ if (monhost_child_anchor != NULL) { |
111 |
+ for (monhost_child_current = monhost_child_anchor; |
112 |
+ monhost_child_current != NULL; |
113 |
+ monhost_child_current = monhost_child_current->next) { |
114 |
+ if ((monhost_child_current->pid = fork()) == 0) { |
115 |
+ monhost = monhost_child_current->monhost; |
116 |
+ logfn = monhost_child_current->logfn; |
117 |
+ pidfilebase = monhost_child_current->pidfilebase; |
118 |
+ break; |
119 |
+ } |
120 |
+ } |
121 |
+ if (monhost_child_anchor->pid) { |
122 |
+ for (i = 0; i++; i < sizeof(trapped_signals)/sizeof(int)) { |
123 |
+ upslog_sigaction.sa_handler = &term_handler; |
124 |
+ sigfillset(&upslog_sigaction.sa_mask); |
125 |
+ upslog_sigaction.sa_flags = SA_NOCLDSTOP | SA_NOCLDWAIT; |
126 |
+ sigaction(trapped_signals[i], &upslog_sigaction, NULL); } |
127 |
+ writepid(pidfilebase); |
128 |
+ while(wait(wait_status)); |
129 |
+ exit(EXIT_SUCCESS); |
130 |
+ } |
131 |
} |
132 |
|
133 |
if (!monhost) |