popen.c 7.2 KB

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