popen.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "../libnetdata.h"
  3. static pthread_mutex_t myp_lock;
  4. static int myp_tracking = 0;
  5. struct mypopen {
  6. pid_t pid;
  7. struct mypopen *next;
  8. struct mypopen *prev;
  9. };
  10. static struct mypopen *mypopen_root = NULL;
  11. // myp_add_lock takes the lock if we're tracking.
  12. static void myp_add_lock(void) {
  13. if (myp_tracking == 0)
  14. return;
  15. netdata_mutex_lock(&myp_lock);
  16. }
  17. // myp_add_unlock release the lock if we're tracking.
  18. static void myp_add_unlock(void) {
  19. if (myp_tracking == 0)
  20. return;
  21. netdata_mutex_unlock(&myp_lock);
  22. }
  23. // myp_add_locked adds pid if we're tracking.
  24. // myp_add_lock must have been called previously.
  25. static void myp_add_locked(pid_t pid) {
  26. struct mypopen *mp;
  27. if (myp_tracking == 0)
  28. return;
  29. mp = mallocz(sizeof(struct mypopen));
  30. mp->pid = pid;
  31. mp->next = mypopen_root;
  32. mp->prev = NULL;
  33. if (mypopen_root != NULL)
  34. mypopen_root->prev = mp;
  35. mypopen_root = mp;
  36. netdata_mutex_unlock(&myp_lock);
  37. }
  38. // myp_del deletes pid if we're tracking.
  39. static void myp_del(pid_t pid) {
  40. struct mypopen *mp;
  41. if (myp_tracking == 0)
  42. return;
  43. netdata_mutex_lock(&myp_lock);
  44. for (mp = mypopen_root; mp != NULL; mp = mp->next) {
  45. if (mp->pid == pid) {
  46. if (mp->next != NULL)
  47. mp->next->prev = mp->prev;
  48. if (mp->prev != NULL)
  49. mp->prev->next = mp->next;
  50. if (mypopen_root == mp)
  51. mypopen_root = mp->next;
  52. freez(mp);
  53. break;
  54. }
  55. }
  56. if (mp == NULL)
  57. error("Cannot find pid %d.", pid);
  58. netdata_mutex_unlock(&myp_lock);
  59. }
  60. #define PIPE_READ 0
  61. #define PIPE_WRITE 1
  62. /* custom_popene flag definitions */
  63. #define FLAG_CREATE_PIPE 1 // Create a pipe like popen() when set, otherwise set stdout to /dev/null
  64. #define FLAG_CLOSE_FD 2 // Close all file descriptors other than STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO
  65. /*
  66. * Returns -1 on failure, 0 on success. When FLAG_CREATE_PIPE is set, on success set the FILE *fp pointer.
  67. */
  68. static inline int custom_popene(const char *command, volatile pid_t *pidptr, char **env, uint8_t flags, FILE **fpp) {
  69. FILE *fp = NULL;
  70. int ret = 0; // success by default
  71. int pipefd[2], error;
  72. pid_t pid;
  73. char *const spawn_argv[] = {
  74. "sh",
  75. "-c",
  76. (char *)command,
  77. NULL
  78. };
  79. posix_spawnattr_t attr;
  80. posix_spawn_file_actions_t fa;
  81. if (flags & FLAG_CREATE_PIPE) {
  82. if (pipe(pipefd) == -1)
  83. return -1;
  84. if ((fp = fdopen(pipefd[PIPE_READ], "r")) == NULL) {
  85. goto error_after_pipe;
  86. }
  87. }
  88. if (flags & FLAG_CLOSE_FD) {
  89. // Mark all files to be closed by the exec() stage of posix_spawn()
  90. int i;
  91. for (i = (int) (sysconf(_SC_OPEN_MAX) - 1); i >= 0; i--) {
  92. if (i != STDIN_FILENO && i != STDERR_FILENO)
  93. (void) fcntl(i, F_SETFD, FD_CLOEXEC);
  94. }
  95. }
  96. if (!posix_spawn_file_actions_init(&fa)) {
  97. if (flags & FLAG_CREATE_PIPE) {
  98. // move the pipe to stdout in the child
  99. if (posix_spawn_file_actions_adddup2(&fa, pipefd[PIPE_WRITE], STDOUT_FILENO)) {
  100. error("posix_spawn_file_actions_adddup2() failed");
  101. goto error_after_posix_spawn_file_actions_init;
  102. }
  103. } else {
  104. // set stdout to /dev/null
  105. if (posix_spawn_file_actions_addopen(&fa, STDOUT_FILENO, "/dev/null", O_WRONLY, 0)) {
  106. error("posix_spawn_file_actions_addopen() failed");
  107. // this is not a fatal error
  108. }
  109. }
  110. } else {
  111. error("posix_spawn_file_actions_init() failed.");
  112. goto error_after_pipe;
  113. }
  114. if (!(error = posix_spawnattr_init(&attr))) {
  115. // reset all signals in the child
  116. sigset_t mask;
  117. if (posix_spawnattr_setflags(&attr, POSIX_SPAWN_SETSIGMASK | POSIX_SPAWN_SETSIGDEF))
  118. error("posix_spawnattr_setflags() failed.");
  119. sigemptyset(&mask);
  120. if (posix_spawnattr_setsigmask(&attr, &mask))
  121. error("posix_spawnattr_setsigmask() failed.");
  122. } else {
  123. error("posix_spawnattr_init() failed.");
  124. }
  125. // Take the lock while we fork to ensure we don't race with SIGCHLD
  126. // delivery on a process which exits quickly.
  127. myp_add_lock();
  128. if (!posix_spawn(&pid, "/bin/sh", &fa, &attr, spawn_argv, env)) {
  129. *pidptr = pid;
  130. myp_add_locked(pid);
  131. debug(D_CHILDS, "Spawned command: '%s' on pid %d from parent pid %d.", command, pid, getpid());
  132. } else {
  133. myp_add_unlock();
  134. error("Failed to spawn command: '%s' from parent pid %d.", command, getpid());
  135. if (flags & FLAG_CREATE_PIPE) {
  136. fclose(fp);
  137. }
  138. ret = -1;
  139. }
  140. if (flags & FLAG_CREATE_PIPE) {
  141. close(pipefd[PIPE_WRITE]);
  142. if (0 == ret) // on success set FILE * pointer
  143. *fpp = fp;
  144. }
  145. if (!error) {
  146. // posix_spawnattr_init() succeeded
  147. if (posix_spawnattr_destroy(&attr))
  148. error("posix_spawnattr_destroy");
  149. }
  150. if (posix_spawn_file_actions_destroy(&fa))
  151. error("posix_spawn_file_actions_destroy");
  152. return ret;
  153. error_after_posix_spawn_file_actions_init:
  154. if (posix_spawn_file_actions_destroy(&fa))
  155. error("posix_spawn_file_actions_destroy");
  156. error_after_pipe:
  157. if (flags & FLAG_CREATE_PIPE) {
  158. if (fp)
  159. fclose(fp);
  160. else
  161. close(pipefd[PIPE_READ]);
  162. close(pipefd[PIPE_WRITE]);
  163. }
  164. return -1;
  165. }
  166. // See man environ
  167. extern char **environ;
  168. // myp_init should be called by apps which act as init
  169. // (pid 1) so that processes created by mypopen and mypopene
  170. // are tracked. This enables the reaper to ignore processes
  171. // which will be handled internally, by calling myp_reap, to
  172. // avoid issues with already reaped processes during wait calls.
  173. //
  174. // Callers should call myp_free() to clean up resources.
  175. void myp_init(void) {
  176. info("process tracking enabled.");
  177. myp_tracking = 1;
  178. if (netdata_mutex_init(&myp_lock) != 0) {
  179. fatal("myp_init() mutex init failed.");
  180. }
  181. }
  182. // myp_free cleans up any resources allocated for process
  183. // tracking.
  184. void myp_free(void) {
  185. struct mypopen *mp, *next;
  186. if (myp_tracking == 0)
  187. return;
  188. netdata_mutex_lock(&myp_lock);
  189. for (mp = mypopen_root; mp != NULL; mp = next) {
  190. next = mp->next;
  191. freez(mp);
  192. }
  193. mypopen_root = NULL;
  194. myp_tracking = 0;
  195. netdata_mutex_unlock(&myp_lock);
  196. }
  197. // myp_reap returns 1 if pid should be reaped, 0 otherwise.
  198. int myp_reap(pid_t pid) {
  199. struct mypopen *mp;
  200. if (myp_tracking == 0)
  201. return 0;
  202. netdata_mutex_lock(&myp_lock);
  203. for (mp = mypopen_root; mp != NULL; mp = mp->next) {
  204. if (mp->pid == pid) {
  205. netdata_mutex_unlock(&myp_lock);
  206. return 0;
  207. }
  208. }
  209. netdata_mutex_unlock(&myp_lock);
  210. return 1;
  211. }
  212. FILE *mypopen(const char *command, volatile pid_t *pidptr) {
  213. FILE *fp = NULL;
  214. (void)custom_popene(command, pidptr, environ, FLAG_CREATE_PIPE | FLAG_CLOSE_FD, &fp);
  215. return fp;
  216. }
  217. FILE *mypopene(const char *command, volatile pid_t *pidptr, char **env) {
  218. FILE *fp = NULL;
  219. (void)custom_popene(command, pidptr, env, FLAG_CREATE_PIPE | FLAG_CLOSE_FD, &fp);
  220. return fp;
  221. }
  222. // returns 0 on success, -1 on failure
  223. int netdata_spawn(const char *command, volatile pid_t *pidptr) {
  224. return custom_popene(command, pidptr, environ, 0, NULL);
  225. }
  226. int custom_pclose(FILE *fp, pid_t pid) {
  227. int ret;
  228. siginfo_t info;
  229. debug(D_EXIT, "Request to mypclose() on pid %d", pid);
  230. if (fp) {
  231. // close the pipe fd
  232. // this is required in musl
  233. // without it the childs do not exit
  234. close(fileno(fp));
  235. // close the pipe file pointer
  236. fclose(fp);
  237. }
  238. errno = 0;
  239. ret = waitid(P_PID, (id_t) pid, &info, WEXITED);
  240. myp_del(pid);
  241. if (ret != -1) {
  242. switch (info.si_code) {
  243. case CLD_EXITED:
  244. if(info.si_status)
  245. error("child pid %d exited with code %d.", info.si_pid, info.si_status);
  246. return(info.si_status);
  247. case CLD_KILLED:
  248. if(info.si_status == 15) {
  249. info("child pid %d killed by signal %d.", info.si_pid, info.si_status);
  250. return(0);
  251. }
  252. else {
  253. error("child pid %d killed by signal %d.", info.si_pid, info.si_status);
  254. return(-1);
  255. }
  256. case CLD_DUMPED:
  257. error("child pid %d core dumped by signal %d.", info.si_pid, info.si_status);
  258. return(-2);
  259. case CLD_STOPPED:
  260. error("child pid %d stopped by signal %d.", info.si_pid, info.si_status);
  261. return(0);
  262. case CLD_TRAPPED:
  263. error("child pid %d trapped by signal %d.", info.si_pid, info.si_status);
  264. return(-4);
  265. case CLD_CONTINUED:
  266. error("child pid %d continued by signal %d.", info.si_pid, info.si_status);
  267. return(0);
  268. default:
  269. error("child pid %d gave us a SIGCHLD with code %d and status %d.", info.si_pid, info.si_code, info.si_status);
  270. return(-5);
  271. }
  272. }
  273. else
  274. error("Cannot waitid() for pid %d", pid);
  275. return 0;
  276. }
  277. int mypclose(FILE *fp, pid_t pid)
  278. {
  279. return custom_pclose(fp, pid);
  280. }
  281. int netdata_spawn_waitpid(pid_t pid)
  282. {
  283. return custom_pclose(NULL, pid);
  284. }