popen.c 7.2 KB

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