popen.c 7.3 KB

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