summaryrefslogtreecommitdiffstats
path: root/plugins/runcmd.c
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/runcmd.c')
-rw-r--r--plugins/runcmd.c256
1 files changed, 124 insertions, 132 deletions
diff --git a/plugins/runcmd.c b/plugins/runcmd.c
index ed49bb9..7484314 100644
--- a/plugins/runcmd.c
+++ b/plugins/runcmd.c
@@ -1,63 +1,63 @@
1/***************************************************************************** 1/*****************************************************************************
2* 2 *
3* Monitoring run command utilities 3 * Monitoring run command utilities
4* 4 *
5* License: GPL 5 * License: GPL
6* Copyright (c) 2005-2006 Monitoring Plugins Development Team 6 * Copyright (c) 2005-2024 Monitoring Plugins Development Team
7* 7 *
8* Description : 8 * Description :
9* 9 *
10* A simple interface to executing programs from other programs, using an 10 * A simple interface to executing programs from other programs, using an
11* optimized and safe popen()-like implementation. It is considered safe 11 * optimized and safe popen()-like implementation. It is considered safe
12* in that no shell needs to be spawned and the environment passed to the 12 * in that no shell needs to be spawned and the environment passed to the
13* execve()'d program is essentially empty. 13 * execve()'d program is essentially empty.
14* 14 *
15* The code in this file is a derivative of popen.c which in turn was taken 15 * The code in this file is a derivative of popen.c which in turn was taken
16* from "Advanced Programming for the Unix Environment" by W. Richard Stevens. 16 * from "Advanced Programming for the Unix Environment" by W. Richard Stevens.
17* 17 *
18* Care has been taken to make sure the functions are async-safe. The one 18 * Care has been taken to make sure the functions are async-safe. The one
19* function which isn't is np_runcmd_init() which it doesn't make sense to 19 * function which isn't is np_runcmd_init() which it doesn't make sense to
20* call twice anyway, so the api as a whole should be considered async-safe. 20 * call twice anyway, so the api as a whole should be considered async-safe.
21* 21 *
22* 22 *
23* This program is free software: you can redistribute it and/or modify 23 * This program is free software: you can redistribute it and/or modify
24* it under the terms of the GNU General Public License as published by 24 * it under the terms of the GNU General Public License as published by
25* the Free Software Foundation, either version 3 of the License, or 25 * the Free Software Foundation, either version 3 of the License, or
26* (at your option) any later version. 26 * (at your option) any later version.
27* 27 *
28* This program is distributed in the hope that it will be useful, 28 * This program is distributed in the hope that it will be useful,
29* but WITHOUT ANY WARRANTY; without even the implied warranty of 29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31* GNU General Public License for more details. 31 * GNU General Public License for more details.
32* 32 *
33* You should have received a copy of the GNU General Public License 33 * You should have received a copy of the GNU General Public License
34* along with this program. If not, see <http://www.gnu.org/licenses/>. 34 * along with this program. If not, see <http://www.gnu.org/licenses/>.
35* 35 *
36* 36 *
37*****************************************************************************/ 37 *****************************************************************************/
38 38
39#define NAGIOSPLUG_API_C 1 39#define NAGIOSPLUG_API_C 1
40 40
41/** includes **/ 41/** includes **/
42#include "runcmd.h" 42#include "runcmd.h"
43#ifdef HAVE_SYS_WAIT_H 43#ifdef HAVE_SYS_WAIT_H
44# include <sys/wait.h> 44# include <sys/wait.h>
45#endif 45#endif
46 46
47#include "./utils.h" 47#include "./utils.h"
48 48
49/** macros **/ 49/** macros **/
50#ifndef WEXITSTATUS 50#ifndef WEXITSTATUS
51# define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8) 51# define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
52#endif 52#endif
53 53
54#ifndef WIFEXITED 54#ifndef WIFEXITED
55# define WIFEXITED(stat_val) (((stat_val) & 255) == 0) 55# define WIFEXITED(stat_val) (((stat_val)&255) == 0)
56#endif 56#endif
57 57
58/* 4.3BSD Reno <signal.h> doesn't define SIG_ERR */ 58/* 4.3BSD Reno <signal.h> doesn't define SIG_ERR */
59#if defined(SIG_IGN) && !defined(SIG_ERR) 59#if defined(SIG_IGN) && !defined(SIG_ERR)
60# define SIG_ERR ((Sigfunc *)-1) 60# define SIG_ERR ((Sigfunc *)-1)
61#endif 61#endif
62 62
63#include "../lib/maxfd.h" 63#include "../lib/maxfd.h"
@@ -72,33 +72,26 @@
72static pid_t *np_pids = NULL; 72static pid_t *np_pids = NULL;
73 73
74/** prototypes **/ 74/** prototypes **/
75static int np_runcmd_open(const char *, int *, int *) 75static int np_runcmd_open(const char *, int *, int *) __attribute__((__nonnull__(1, 2, 3)));
76 __attribute__((__nonnull__(1, 2, 3)));
77 76
78static int np_fetch_output(int, output *, int) 77static int np_fetch_output(int, output *, int) __attribute__((__nonnull__(2)));
79 __attribute__((__nonnull__(2)));
80 78
81static int np_runcmd_close(int); 79static int np_runcmd_close(int);
82 80
83/* prototype imported from utils.h */ 81/* prototype imported from utils.h */
84extern void die (int, const char *, ...) 82extern void die(int, const char *, ...) __attribute__((__noreturn__, __format__(__printf__, 2, 3)));
85 __attribute__((__noreturn__,__format__(__printf__, 2, 3)));
86
87 83
88/* this function is NOT async-safe. It is exported so multithreaded 84/* this function is NOT async-safe. It is exported so multithreaded
89 * plugins (or other apps) can call it prior to running any commands 85 * plugins (or other apps) can call it prior to running any commands
90 * through this api and thus achieve async-safeness throughout the api */ 86 * through this api and thus achieve async-safeness throughout the api */
91void np_runcmd_init(void) 87void np_runcmd_init(void) {
92{ 88 long maxfd = mp_open_max();
93 long maxfd = mp_open_max(); 89 if (!np_pids)
94 if(!np_pids) np_pids = calloc(maxfd, sizeof(pid_t)); 90 np_pids = calloc(maxfd, sizeof(pid_t));
95} 91}
96 92
97
98/* Start running a command */ 93/* Start running a command */
99static int 94static int np_runcmd_open(const char *cmdstring, int *pfd, int *pfderr) {
100np_runcmd_open(const char *cmdstring, int *pfd, int *pfderr)
101{
102 char *env[2]; 95 char *env[2];
103 char *cmd = NULL; 96 char *cmd = NULL;
104 char **argv = NULL; 97 char **argv = NULL;
@@ -112,7 +105,8 @@ np_runcmd_open(const char *cmdstring, int *pfd, int *pfderr)
112 105
113 int i = 0; 106 int i = 0;
114 107
115 if(!np_pids) NP_RUNCMD_INIT; 108 if (!np_pids)
109 NP_RUNCMD_INIT;
116 110
117 env[0] = strdup("LC_ALL=C"); 111 env[0] = strdup("LC_ALL=C");
118 env[1] = NULL; 112 env[1] = NULL;
@@ -120,49 +114,50 @@ np_runcmd_open(const char *cmdstring, int *pfd, int *pfderr)
120 /* make copy of command string so strtok() doesn't silently modify it */ 114 /* make copy of command string so strtok() doesn't silently modify it */
121 /* (the calling program may want to access it later) */ 115 /* (the calling program may want to access it later) */
122 cmdlen = strlen(cmdstring); 116 cmdlen = strlen(cmdstring);
123 if((cmd = malloc(cmdlen + 1)) == NULL) return -1; 117 if ((cmd = malloc(cmdlen + 1)) == NULL)
118 return -1;
124 memcpy(cmd, cmdstring, cmdlen); 119 memcpy(cmd, cmdstring, cmdlen);
125 cmd[cmdlen] = '\0'; 120 cmd[cmdlen] = '\0';
126 121
127 /* This is not a shell, so we don't handle "???" */ 122 /* This is not a shell, so we don't handle "???" */
128 if (strstr (cmdstring, "\"")) return -1; 123 if (strstr(cmdstring, "\""))
124 return -1;
129 125
130 /* allow single quotes, but only if non-whitesapce doesn't occur on both sides */ 126 /* allow single quotes, but only if non-whitesapce doesn't occur on both sides */
131 if (strstr (cmdstring, " ' ") || strstr (cmdstring, "'''")) 127 if (strstr(cmdstring, " ' ") || strstr(cmdstring, "'''"))
132 return -1; 128 return -1;
133 129
134 /* each arg must be whitespace-separated, so args can be a maximum 130 /* each arg must be whitespace-separated, so args can be a maximum
135 * of (len / 2) + 1. We add 1 extra to the mix for NULL termination */ 131 * of (len / 2) + 1. We add 1 extra to the mix for NULL termination */
136 argc = (cmdlen >> 1) + 2; 132 argc = (cmdlen >> 1) + 2;
137 argv = calloc(sizeof(char *), argc); 133 argv = calloc(argc, sizeof(char *));
138 134
139 if (argv == NULL) { 135 if (argv == NULL) {
140 printf ("%s\n", _("Could not malloc argv array in popen()")); 136 printf("%s\n", _("Could not malloc argv array in popen()"));
141 return -1; 137 return -1;
142 } 138 }
143 139
144 /* get command arguments (stupidly, but fairly quickly) */ 140 /* get command arguments (stupidly, but fairly quickly) */
145 while (cmd) { 141 while (cmd) {
146 str = cmd; 142 str = cmd;
147 str += strspn (str, " \t\r\n"); /* trim any leading whitespace */ 143 str += strspn(str, " \t\r\n"); /* trim any leading whitespace */
148 144
149 if (strstr (str, "'") == str) { /* handle SIMPLE quoted strings */ 145 if (strstr(str, "'") == str) { /* handle SIMPLE quoted strings */
150 str++; 146 str++;
151 if (!strstr (str, "'")) return -1; /* balanced? */ 147 if (!strstr(str, "'"))
152 cmd = 1 + strstr (str, "'"); 148 return -1; /* balanced? */
153 str[strcspn (str, "'")] = 0; 149 cmd = 1 + strstr(str, "'");
154 } 150 str[strcspn(str, "'")] = 0;
155 else { 151 } else {
156 if (strpbrk (str, " \t\r\n")) { 152 if (strpbrk(str, " \t\r\n")) {
157 cmd = 1 + strpbrk (str, " \t\r\n"); 153 cmd = 1 + strpbrk(str, " \t\r\n");
158 str[strcspn (str, " \t\r\n")] = 0; 154 str[strcspn(str, " \t\r\n")] = 0;
159 } 155 } else {
160 else {
161 cmd = NULL; 156 cmd = NULL;
162 } 157 }
163 } 158 }
164 159
165 if (cmd && strlen (cmd) == strspn (cmd, " \t\r\n")) 160 if (cmd && strlen(cmd) == strspn(cmd, " \t\r\n"))
166 cmd = NULL; 161 cmd = NULL;
167 162
168 argv[i++] = str; 163 argv[i++] = str;
@@ -173,33 +168,33 @@ np_runcmd_open(const char *cmdstring, int *pfd, int *pfderr)
173 168
174 /* child runs exceve() and _exit. */ 169 /* child runs exceve() and _exit. */
175 if (pid == 0) { 170 if (pid == 0) {
176#ifdef RLIMIT_CORE 171#ifdef RLIMIT_CORE
177 /* the program we execve shouldn't leave core files */ 172 /* the program we execve shouldn't leave core files */
178 getrlimit (RLIMIT_CORE, &limit); 173 getrlimit(RLIMIT_CORE, &limit);
179 limit.rlim_cur = 0; 174 limit.rlim_cur = 0;
180 setrlimit (RLIMIT_CORE, &limit); 175 setrlimit(RLIMIT_CORE, &limit);
181#endif 176#endif
182 close (pfd[0]); 177 close(pfd[0]);
183 if (pfd[1] != STDOUT_FILENO) { 178 if (pfd[1] != STDOUT_FILENO) {
184 dup2 (pfd[1], STDOUT_FILENO); 179 dup2(pfd[1], STDOUT_FILENO);
185 close (pfd[1]); 180 close(pfd[1]);
186 } 181 }
187 close (pfderr[0]); 182 close(pfderr[0]);
188 if (pfderr[1] != STDERR_FILENO) { 183 if (pfderr[1] != STDERR_FILENO) {
189 dup2 (pfderr[1], STDERR_FILENO); 184 dup2(pfderr[1], STDERR_FILENO);
190 close (pfderr[1]); 185 close(pfderr[1]);
191 } 186 }
192 187
193 /* close all descriptors in np_pids[] 188 /* close all descriptors in np_pids[]
194 * This is executed in a separate address space (pure child), 189 * This is executed in a separate address space (pure child),
195 * so we don't have to worry about async safety */ 190 * so we don't have to worry about async safety */
196 long maxfd = mp_open_max(); 191 long maxfd = mp_open_max();
197 for (i = 0; i < maxfd; i++) 192 for (i = 0; i < maxfd; i++)
198 if(np_pids[i] > 0) 193 if (np_pids[i] > 0)
199 close (i); 194 close(i);
200 195
201 execve (argv[0], argv, env); 196 execve(argv[0], argv, env);
202 _exit (STATE_UNKNOWN); 197 _exit(STATE_UNKNOWN);
203 } 198 }
204 199
205 /* parent picks up execution here */ 200 /* parent picks up execution here */
@@ -213,49 +208,44 @@ np_runcmd_open(const char *cmdstring, int *pfd, int *pfderr)
213 return pfd[0]; 208 return pfd[0];
214} 209}
215 210
216 211static int np_runcmd_close(int fd) {
217static int
218np_runcmd_close(int fd)
219{
220 int status; 212 int status;
221 pid_t pid; 213 pid_t pid;
222 214
223 /* make sure this fd was opened by popen() */ 215 /* make sure this fd was opened by popen() */
224 long maxfd = mp_open_max(); 216 long maxfd = mp_open_max();
225 if(fd < 0 || fd > maxfd || !np_pids || (pid = np_pids[fd]) == 0) 217 if (fd < 0 || fd > maxfd || !np_pids || (pid = np_pids[fd]) == 0)
226 return -1; 218 return -1;
227 219
228 np_pids[fd] = 0; 220 np_pids[fd] = 0;
229 if (close (fd) == -1) return -1; 221 if (close(fd) == -1)
222 return -1;
230 223
231 /* EINTR is ok (sort of), everything else is bad */ 224 /* EINTR is ok (sort of), everything else is bad */
232 while (waitpid (pid, &status, 0) < 0) 225 while (waitpid(pid, &status, 0) < 0)
233 if (errno != EINTR) return -1; 226 if (errno != EINTR)
227 return -1;
234 228
235 /* return child's termination status */ 229 /* return child's termination status */
236 return (WIFEXITED(status)) ? WEXITSTATUS(status) : -1; 230 return (WIFEXITED(status)) ? WEXITSTATUS(status) : -1;
237} 231}
238 232
239 233void runcmd_timeout_alarm_handler(int signo) {
240void
241runcmd_timeout_alarm_handler (int signo)
242{
243 234
244 if (signo == SIGALRM) 235 if (signo == SIGALRM)
245 puts(_("CRITICAL - Plugin timed out while executing system call")); 236 puts(_("CRITICAL - Plugin timed out while executing system call"));
246 237
247 long maxfd = mp_open_max(); 238 long maxfd = mp_open_max();
248 if(np_pids) for(long int i = 0; i < maxfd; i++) { 239 if (np_pids)
249 if(np_pids[i] != 0) kill(np_pids[i], SIGKILL); 240 for (long int i = 0; i < maxfd; i++) {
250 } 241 if (np_pids[i] != 0)
242 kill(np_pids[i], SIGKILL);
243 }
251 244
252 exit (STATE_CRITICAL); 245 exit(STATE_CRITICAL);
253} 246}
254 247
255 248static int np_fetch_output(int fd, output *op, int flags) {
256static int
257np_fetch_output(int fd, output *op, int flags)
258{
259 size_t len = 0, i = 0, lineno = 0; 249 size_t len = 0, i = 0, lineno = 0;
260 size_t rsf = 6, ary_size = 0; /* rsf = right shift factor, dec'ed uncond once */ 250 size_t rsf = 6, ary_size = 0; /* rsf = right shift factor, dec'ed uncond once */
261 char *buf = NULL; 251 char *buf = NULL;
@@ -264,7 +254,7 @@ np_fetch_output(int fd, output *op, int flags)
264 254
265 op->buf = NULL; 255 op->buf = NULL;
266 op->buflen = 0; 256 op->buflen = 0;
267 while((ret = read(fd, tmpbuf, sizeof(tmpbuf))) > 0) { 257 while ((ret = read(fd, tmpbuf, sizeof(tmpbuf))) > 0) {
268 len = (size_t)ret; 258 len = (size_t)ret;
269 op->buf = realloc(op->buf, op->buflen + len + 1); 259 op->buf = realloc(op->buf, op->buflen + len + 1);
270 memcpy(op->buf + op->buflen, tmpbuf, len); 260 memcpy(op->buf + op->buflen, tmpbuf, len);
@@ -272,33 +262,33 @@ np_fetch_output(int fd, output *op, int flags)
272 i++; 262 i++;
273 } 263 }
274 264
275 if(ret < 0) { 265 if (ret < 0) {
276 printf("read() returned %d: %s\n", ret, strerror(errno)); 266 printf("read() returned %d: %s\n", ret, strerror(errno));
277 return ret; 267 return ret;
278 } 268 }
279 269
280 /* some plugins may want to keep output unbroken, and some commands 270 /* some plugins may want to keep output unbroken, and some commands
281 * will yield no output, so return here for those */ 271 * will yield no output, so return here for those */
282 if(flags & RUNCMD_NO_ARRAYS || !op->buf || !op->buflen) 272 if (flags & RUNCMD_NO_ARRAYS || !op->buf || !op->buflen)
283 return op->buflen; 273 return op->buflen;
284 274
285 /* and some may want both */ 275 /* and some may want both */
286 if(flags & RUNCMD_NO_ASSOC) { 276 if (flags & RUNCMD_NO_ASSOC) {
287 buf = malloc(op->buflen); 277 buf = malloc(op->buflen);
288 memcpy(buf, op->buf, op->buflen); 278 memcpy(buf, op->buf, op->buflen);
289 } 279 } else
290 else buf = op->buf; 280 buf = op->buf;
291 281
292 op->line = NULL; 282 op->line = NULL;
293 op->lens = NULL; 283 op->lens = NULL;
294 i = 0; 284 i = 0;
295 while(i < op->buflen) { 285 while (i < op->buflen) {
296 /* make sure we have enough memory */ 286 /* make sure we have enough memory */
297 if(lineno >= ary_size) { 287 if (lineno >= ary_size) {
298 /* ary_size must never be zero */ 288 /* ary_size must never be zero */
299 do { 289 do {
300 ary_size = op->buflen >> --rsf; 290 ary_size = op->buflen >> --rsf;
301 } while(!ary_size); 291 } while (!ary_size);
302 292
303 op->line = realloc(op->line, ary_size * sizeof(char *)); 293 op->line = realloc(op->line, ary_size * sizeof(char *));
304 op->lens = realloc(op->lens, ary_size * sizeof(size_t)); 294 op->lens = realloc(op->lens, ary_size * sizeof(size_t));
@@ -308,7 +298,8 @@ np_fetch_output(int fd, output *op, int flags)
308 op->line[lineno] = &buf[i]; 298 op->line[lineno] = &buf[i];
309 299
310 /* hop to next newline or end of buffer */ 300 /* hop to next newline or end of buffer */
311 while(buf[i] != '\n' && i < op->buflen) i++; 301 while (buf[i] != '\n' && i < op->buflen)
302 i++;
312 buf[i] = '\0'; 303 buf[i] = '\0';
313 304
314 /* calculate the string length using pointer difference */ 305 /* calculate the string length using pointer difference */
@@ -321,21 +312,22 @@ np_fetch_output(int fd, output *op, int flags)
321 return lineno; 312 return lineno;
322} 313}
323 314
324 315int np_runcmd(const char *cmd, output *out, output *err, int flags) {
325int
326np_runcmd(const char *cmd, output *out, output *err, int flags)
327{
328 int fd, pfd_out[2], pfd_err[2]; 316 int fd, pfd_out[2], pfd_err[2];
329 317
330 /* initialize the structs */ 318 /* initialize the structs */
331 if(out) memset(out, 0, sizeof(output)); 319 if (out)
332 if(err) memset(err, 0, sizeof(output)); 320 memset(out, 0, sizeof(output));
333 321 if (err)
334 if((fd = np_runcmd_open(cmd, pfd_out, pfd_err)) == -1) 322 memset(err, 0, sizeof(output));
335 die (STATE_UNKNOWN, _("Could not open pipe: %s\n"), cmd); 323
336 324 if ((fd = np_runcmd_open(cmd, pfd_out, pfd_err)) == -1)
337 if(out) out->lines = np_fetch_output(pfd_out[0], out, flags); 325 die(STATE_UNKNOWN, _("Could not open pipe: %s\n"), cmd);
338 if(err) err->lines = np_fetch_output(pfd_err[0], err, flags); 326
327 if (out)
328 out->lines = np_fetch_output(pfd_out[0], out, flags);
329 if (err)
330 err->lines = np_fetch_output(pfd_err[0], err, flags);
339 331
340 return np_runcmd_close(fd); 332 return np_runcmd_close(fd);
341} 333}