Wait for an arbitrary process.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

239 lines
5.4 KiB

  1. /*
  2. * wait4pid - Wait for an arbitrary process
  3. * Copyright (C) 2013 Damien Goutte-Gattat
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #ifdef HAVE_CONFIG_H
  19. #include <config.h>
  20. #endif
  21. #ifdef HAVE_LINUX_NETLINK_H
  22. #include <string.h>
  23. #include <unistd.h>
  24. #include <signal.h>
  25. #include <sys/socket.h>
  26. #include <linux/netlink.h>
  27. #include <linux/connector.h>
  28. #include <linux/cn_proc.h>
  29. int sock = 0;
  30. int listener_set = 0;
  31. static int
  32. set_proc_listen(int sock, int enable)
  33. {
  34. struct nlmsghdr *hdr;
  35. struct __attribute__ ((__packed__)) payload_t {
  36. struct cn_msg msg;
  37. enum proc_cn_mcast_op op;
  38. } *payload;
  39. char buf[NLMSG_SPACE(sizeof(struct payload_t))];
  40. hdr = (struct nlmsghdr *)buf;
  41. hdr->nlmsg_type = NLMSG_DONE;
  42. hdr->nlmsg_flags = NLM_F_REQUEST;
  43. hdr->nlmsg_len = NLMSG_LENGTH(sizeof(*payload));
  44. hdr->nlmsg_pid = getpid();
  45. payload = (struct payload_t *) NLMSG_DATA(hdr);
  46. payload->msg.id.idx = CN_IDX_PROC;
  47. payload->msg.id.val = CN_VAL_PROC;
  48. payload->op = enable ? PROC_CN_MCAST_LISTEN : PROC_CN_MCAST_IGNORE;
  49. return send(sock, hdr, NLMSG_SPACE(sizeof(*payload)), 0);
  50. }
  51. static void
  52. wait4pid_close(void)
  53. {
  54. if ( listener_set ) {
  55. set_proc_listen(sock, 0);
  56. listener_set = 0;
  57. }
  58. if ( sock ) {
  59. close(sock);
  60. sock = 0;
  61. }
  62. }
  63. static int
  64. wait4pid_init(void)
  65. {
  66. struct sockaddr_nl sa;
  67. if ( (sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_CONNECTOR)) == -1 )
  68. return -1;
  69. atexit(wait4pid_close);
  70. memset(&sa, 0, sizeof(sa));
  71. sa.nl_family = AF_NETLINK;
  72. sa.nl_groups = CN_IDX_PROC;
  73. sa.nl_pid = getpid();
  74. if ( bind(sock, (struct sockaddr *)&sa, sizeof(sa)) == -1 )
  75. return -1;
  76. if ( setgid(getgid()) == -1 || setuid(getuid()) == -1 )
  77. return -1;
  78. if ( set_proc_listen(sock, 1) == -1 )
  79. return -1;
  80. return 0;
  81. }
  82. struct __attribute__ ((__packed__)) cn_proc_msg {
  83. struct cn_msg msg;
  84. struct proc_event evt;
  85. };
  86. static int
  87. do_recv(int sock, void *buf, size_t len, int flags)
  88. {
  89. int ret;
  90. do {
  91. ret = recv(sock, buf, len, flags);
  92. } while ( ret == -1 && errno == EINTR );
  93. return ret;
  94. }
  95. int
  96. wait4pid(pid_t pid)
  97. {
  98. int ec;
  99. struct cn_proc_msg *payload;
  100. char buf[NLMSG_SPACE(sizeof(struct cn_proc_msg))];
  101. if ( ! listener_set && wait4pid_init() == -1 )
  102. return -1;
  103. if ( kill(pid, 0) == -1 && errno == ESRCH )
  104. return -1;
  105. while ( do_recv(sock, buf, sizeof(buf), 0) > 0 ) {
  106. payload = (struct cn_proc_msg *) NLMSG_DATA(buf);
  107. if ( payload->evt.what == PROC_EVENT_EXIT
  108. && payload->evt.event_data.exit.process_pid == pid ) {
  109. ec = payload->evt.event_data.exit.exit_code / 256;
  110. break;
  111. }
  112. }
  113. return ec;
  114. }
  115. int
  116. wait4all(pid_t *pids, size_t len)
  117. {
  118. int ec, nproc, i;
  119. struct cn_proc_msg *payload;
  120. char buf[NLMSG_SPACE(sizeof(struct cn_proc_msg))];
  121. if ( ! listener_set && wait4pid_init() == -1 )
  122. return -1;
  123. for ( i = 0; i < len; i++ ) {
  124. if ( kill(pids[i], 0) == -1 && errno == ESRCH )
  125. return -1;
  126. }
  127. nproc = len;
  128. while ( nproc > 0 ) {
  129. if ( do_recv(sock, buf, sizeof(buf), 0) <= 0 )
  130. return -1;
  131. payload = (struct cn_proc_msg *) NLMSG_DATA(buf);
  132. if ( payload->evt.what != PROC_EVENT_EXIT )
  133. continue;
  134. for ( i = 0; i < len; i++ ) {
  135. if ( pids[i] != -1
  136. && pids[i] == payload->evt.event_data.exit.process_pid ) {
  137. pids[i] = -1;
  138. nproc -= 1;
  139. if ( i == len - 1 )
  140. ec = payload->evt.event_data.exit.exit_code / 256;
  141. }
  142. }
  143. }
  144. return ec;
  145. }
  146. #else /* HAVE_LINUX_NETLINK_H */
  147. #include <string.h>
  148. #include <sys/stat.h>
  149. #include <unistd.h>
  150. #include <xmalloc.h>
  151. int
  152. wait4pid(pid_t pid)
  153. {
  154. struct stat st_buf;
  155. char path[20];
  156. snprintf(path, sizeof(path), "/proc/%d", pid);
  157. while ( stat(path, &st_buf) != -1 )
  158. sleep(1);
  159. /* Anything else than ENOENT is an error. */
  160. return errno == ENOENT ? 0 : -1;
  161. }
  162. int
  163. wait4all(pid_t *pids, size_t len)
  164. {
  165. int nproc, i;
  166. char *paths;
  167. struct stat st_buf;
  168. paths = xmalloc(20 * len);
  169. for ( i = 0; i < len; i++ )
  170. snprintf(paths + 20 * i, 20, "/proc/%d", pids[i]);
  171. nproc = len;
  172. while ( nproc > 0 ) {
  173. for ( i = 0; i < len; i++ ) {
  174. if ( pids[i] == -1 )
  175. continue;
  176. if ( stat(paths + 20 * i, &st_buf) == -1 ) {
  177. if ( errno != ENOENT ) {
  178. nproc = -1;
  179. break;
  180. }
  181. pids[i] = -1;
  182. nproc -= 1;
  183. }
  184. }
  185. sleep(1);
  186. }
  187. free(paths);
  188. return nproc;
  189. }
  190. #endif /* ! HAVE_LINUX_NETLINK_H */