popen.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /******************************************************************************
  2. * popen.c
  3. *
  4. * A safe alternative to popen
  5. *
  6. * Provides spopen and spclose
  7. FILE * spopen(const char *);
  8. int spclose(FILE *);
  9. *
  10. * Code taken with liitle modification from "Advanced Programming for the Unix
  11. * Environment" by W. Richard Stevens
  12. *
  13. * This is considered safe in that no shell is spawned, and the environment and
  14. * path passed to the exec'd program are esstially empty. (popen create a shell
  15. * and passes the environment to it).
  16. *
  17. * $Id$
  18. *
  19. ******************************************************************************/
  20. #include "common.h"
  21. /* extern so plugin has pid to kill exec'd process on timeouts */
  22. extern int timeout_interval;
  23. extern pid_t *childpid;
  24. extern int *child_stderr_array;
  25. extern FILE *child_process;
  26. FILE *spopen (const char *);
  27. int spclose (FILE *);
  28. #ifdef REDHAT_SPOPEN_ERROR
  29. RETSIGTYPE popen_sigchld_handler (int);
  30. #endif
  31. RETSIGTYPE popen_timeout_alarm_handler (int);
  32. #include <stdarg.h> /* ANSI C header file */
  33. #include <fcntl.h>
  34. #include <limits.h>
  35. #include <sys/resource.h>
  36. #ifdef HAVE_SYS_WAIT_H
  37. #include <sys/wait.h>
  38. #endif
  39. #ifndef WEXITSTATUS
  40. # define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
  41. #endif
  42. #ifndef WIFEXITED
  43. # define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
  44. #endif
  45. /* 4.3BSD Reno <signal.h> doesn't define SIG_ERR */
  46. #if defined(SIG_IGN) && !defined(SIG_ERR)
  47. #define SIG_ERR ((Sigfunc *)-1)
  48. #endif
  49. #define min(a,b) ((a) < (b) ? (a) : (b))
  50. #define max(a,b) ((a) > (b) ? (a) : (b))
  51. int open_max (void); /* {Prog openmax} */
  52. static void err_sys (const char *, ...) __attribute__((noreturn,format(printf, 1, 2)));
  53. char *rtrim (char *, const char *);
  54. char *pname = NULL; /* caller can set this from argv[0] */
  55. /*int *childerr = NULL;*//* ptr to array allocated at run-time */
  56. /*extern pid_t *childpid = NULL; *//* ptr to array allocated at run-time */
  57. static int maxfd; /* from our open_max(), {Prog openmax} */
  58. #ifdef REDHAT_SPOPEN_ERROR
  59. static volatile int childtermd = 0;
  60. #endif
  61. FILE *
  62. spopen (const char *cmdstring)
  63. {
  64. char *env[2];
  65. char *cmd = NULL;
  66. char **argv = NULL;
  67. char *str;
  68. int argc;
  69. int i = 0, pfd[2], pfderr[2];
  70. pid_t pid;
  71. #ifdef RLIMIT_CORE
  72. /* do not leave core files */
  73. struct rlimit limit;
  74. getrlimit (RLIMIT_CORE, &limit);
  75. limit.rlim_cur = 0;
  76. setrlimit (RLIMIT_CORE, &limit);
  77. #endif
  78. env[0] = strdup("LC_ALL=C");
  79. env[1] = '\0';
  80. /* if no command was passed, return with no error */
  81. if (cmdstring == NULL)
  82. return (NULL);
  83. /* make copy of command string so strtok() doesn't silently modify it */
  84. /* (the calling program may want to access it later) */
  85. cmd = malloc (strlen (cmdstring) + 1);
  86. if (cmd == NULL)
  87. return NULL;
  88. strcpy (cmd, cmdstring);
  89. /* This is not a shell, so we don't handle "???" */
  90. if (strstr (cmdstring, "\""))
  91. return NULL;
  92. /* allow single quotes, but only if non-whitesapce doesn't occur on both sides */
  93. if (strstr (cmdstring, " ' ") || strstr (cmdstring, "'''"))
  94. return NULL;
  95. /* there cannot be more args than characters */
  96. argc = strlen (cmdstring) + 1; /* add 1 for NULL termination */
  97. argv = malloc (sizeof(char*)*argc);
  98. if (argv == NULL) {
  99. printf (_("Could not malloc argv array in popen()\n"));
  100. return NULL;
  101. }
  102. /* loop to get arguments to command */
  103. while (cmd) {
  104. str = cmd;
  105. str += strspn (str, " \t\r\n"); /* trim any leading whitespace */
  106. if (i >= argc - 2) {
  107. printf (_("CRITICAL - You need more args!!!\n"));
  108. return (NULL);
  109. }
  110. if (strstr (str, "'") == str) { /* handle SIMPLE quoted strings */
  111. str++;
  112. if (!strstr (str, "'"))
  113. return NULL; /* balanced? */
  114. cmd = 1 + strstr (str, "'");
  115. str[strcspn (str, "'")] = 0;
  116. }
  117. else {
  118. if (strpbrk (str, " \t\r\n")) {
  119. cmd = 1 + strpbrk (str, " \t\r\n");
  120. str[strcspn (str, " \t\r\n")] = 0;
  121. }
  122. else {
  123. cmd = NULL;
  124. }
  125. }
  126. if (cmd && strlen (cmd) == strspn (cmd, " \t\r\n"))
  127. cmd = NULL;
  128. argv[i++] = str;
  129. }
  130. argv[i] = NULL;
  131. if (childpid == NULL) { /* first time through */
  132. maxfd = open_max (); /* allocate zeroed out array for child pids */
  133. if ((childpid = calloc ((size_t)maxfd, sizeof (pid_t))) == NULL)
  134. return (NULL);
  135. }
  136. if (child_stderr_array == NULL) { /* first time through */
  137. maxfd = open_max (); /* allocate zeroed out array for child pids */
  138. if ((child_stderr_array = calloc ((size_t)maxfd, sizeof (int))) == NULL)
  139. return (NULL);
  140. }
  141. if (pipe (pfd) < 0)
  142. return (NULL); /* errno set by pipe() */
  143. if (pipe (pfderr) < 0)
  144. return (NULL); /* errno set by pipe() */
  145. #ifdef REDHAT_SPOPEN_ERROR
  146. if (signal (SIGCHLD, popen_sigchld_handler) == SIG_ERR) {
  147. usage4 (_("Cannot catch SIGCHLD"));
  148. }
  149. #endif
  150. if ((pid = fork ()) < 0)
  151. return (NULL); /* errno set by fork() */
  152. else if (pid == 0) { /* child */
  153. close (pfd[0]);
  154. if (pfd[1] != STDOUT_FILENO) {
  155. dup2 (pfd[1], STDOUT_FILENO);
  156. close (pfd[1]);
  157. }
  158. close (pfderr[0]);
  159. if (pfderr[1] != STDERR_FILENO) {
  160. dup2 (pfderr[1], STDERR_FILENO);
  161. close (pfderr[1]);
  162. }
  163. /* close all descriptors in childpid[] */
  164. for (i = 0; i < maxfd; i++)
  165. if (childpid[i] > 0)
  166. close (i);
  167. execve (argv[0], argv, env);
  168. _exit (0);
  169. }
  170. close (pfd[1]); /* parent */
  171. if ((child_process = fdopen (pfd[0], "r")) == NULL)
  172. return (NULL);
  173. close (pfderr[1]);
  174. childpid[fileno (child_process)] = pid; /* remember child pid for this fd */
  175. child_stderr_array[fileno (child_process)] = pfderr[0]; /* remember STDERR */
  176. return (child_process);
  177. }
  178. int
  179. spclose (FILE * fp)
  180. {
  181. int fd, status;
  182. pid_t pid;
  183. if (childpid == NULL)
  184. return (1); /* popen() has never been called */
  185. fd = fileno (fp);
  186. if ((pid = childpid[fd]) == 0)
  187. return (1); /* fp wasn't opened by popen() */
  188. childpid[fd] = 0;
  189. if (fclose (fp) == EOF)
  190. return (1);
  191. #ifdef REDHAT_SPOPEN_ERROR
  192. while (!childtermd); /* wait until SIGCHLD */
  193. #endif
  194. while (waitpid (pid, &status, 0) < 0)
  195. if (errno != EINTR)
  196. return (1); /* error other than EINTR from waitpid() */
  197. if (WIFEXITED (status))
  198. return (WEXITSTATUS (status)); /* return child's termination status */
  199. return (1);
  200. }
  201. #ifdef OPEN_MAX
  202. static int openmax = OPEN_MAX;
  203. #else
  204. static int openmax = 0;
  205. #endif
  206. #define OPEN_MAX_GUESS 256 /* if OPEN_MAX is indeterminate */
  207. /* no guarantee this is adequate */
  208. #ifdef REDHAT_SPOPEN_ERROR
  209. RETSIGTYPE
  210. popen_sigchld_handler (int signo)
  211. {
  212. if (signo == SIGCHLD)
  213. childtermd = 1;
  214. }
  215. #endif
  216. RETSIGTYPE
  217. popen_timeout_alarm_handler (int signo)
  218. {
  219. int fh;
  220. if (signo == SIGALRM) {
  221. if (child_process != NULL) {
  222. fh=fileno (child_process);
  223. if(fh >= 0){
  224. kill (childpid[fh], SIGKILL);
  225. }
  226. printf (_("CRITICAL - Plugin timed out after %d seconds\n"),
  227. timeout_interval);
  228. } else {
  229. printf (_("CRITICAL - popen timeout received, but no child process\n"));
  230. }
  231. exit (STATE_CRITICAL);
  232. }
  233. }
  234. int
  235. open_max (void)
  236. {
  237. if (openmax == 0) { /* first time through */
  238. errno = 0;
  239. if ((openmax = sysconf (_SC_OPEN_MAX)) < 0) {
  240. if (errno == 0)
  241. openmax = OPEN_MAX_GUESS; /* it's indeterminate */
  242. else
  243. err_sys (_("sysconf error for _SC_OPEN_MAX"));
  244. }
  245. }
  246. return (openmax);
  247. }
  248. /* Fatal error related to a system call.
  249. * Print a message and die. */
  250. #define MAXLINE 2048
  251. static void
  252. err_sys (const char *fmt, ...)
  253. {
  254. int errnoflag = 1;
  255. int errno_save;
  256. char buf[MAXLINE];
  257. va_list ap;
  258. va_start (ap, fmt);
  259. /* err_doit (1, fmt, ap); */
  260. errno_save = errno; /* value caller might want printed */
  261. vsprintf (buf, fmt, ap);
  262. if (errnoflag)
  263. sprintf (buf + strlen (buf), ": %s", strerror (errno_save));
  264. strcat (buf, "\n");
  265. fflush (stdout); /* in case stdout and stderr are the same */
  266. fputs (buf, stderr);
  267. fflush (NULL); /* flushes all stdio output streams */
  268. va_end (ap);
  269. exit (1);
  270. }
  271. char *
  272. rtrim (char *str, const char *tok)
  273. {
  274. int i = 0;
  275. int j = sizeof (str);
  276. while (str != NULL && i < j) {
  277. if (*(str + i) == *tok) {
  278. sprintf (str + i, "%s", "\0");
  279. return str;
  280. }
  281. i++;
  282. }
  283. return str;
  284. }