popen.c 7.2 KB

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