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.

229 lines
5.2 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 <sys/socket.h>
  25. #include <linux/netlink.h>
  26. #include <linux/connector.h>
  27. #include <linux/cn_proc.h>
  28. int sock = 0;
  29. int listener_set = 0;
  30. static int
  31. set_proc_listen(int sock, int enable)
  32. {
  33. struct nlmsghdr *hdr;
  34. struct __attribute__ ((__packed__)) payload_t {
  35. struct cn_msg msg;
  36. enum proc_cn_mcast_op op;
  37. } *payload;
  38. char buf[NLMSG_SPACE(sizeof(struct payload_t))];
  39. hdr = (struct nlmsghdr *)buf;
  40. hdr->nlmsg_type = NLMSG_DONE;
  41. hdr->nlmsg_flags = NLM_F_REQUEST;
  42. hdr->nlmsg_len = NLMSG_LENGTH(sizeof(*payload));
  43. hdr->nlmsg_pid = getpid();
  44. payload = (struct payload_t *) NLMSG_DATA(hdr);
  45. payload->msg.id.idx = CN_IDX_PROC;
  46. payload->msg.id.val = CN_VAL_PROC;
  47. payload->op = enable ? PROC_CN_MCAST_LISTEN : PROC_CN_MCAST_IGNORE;
  48. return send(sock, hdr, NLMSG_SPACE(sizeof(*payload)), 0);
  49. }
  50. static void
  51. wait4pid_close(void)
  52. {
  53. if ( listener_set ) {
  54. set_proc_listen(sock, 0);
  55. listener_set = 0;
  56. }
  57. if ( sock ) {
  58. close(sock);
  59. sock = 0;
  60. }
  61. }
  62. static int
  63. wait4pid_init(void)
  64. {
  65. struct sockaddr_nl sa;
  66. if ( (sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_CONNECTOR)) == -1 )
  67. return -1;
  68. atexit(wait4pid_close);
  69. memset(&sa, 0, sizeof(sa));
  70. sa.nl_family = AF_NETLINK;
  71. sa.nl_groups = CN_IDX_PROC;
  72. sa.nl_pid = getpid();
  73. if ( bind(sock, (struct sockaddr *)&sa, sizeof(sa)) == -1 )
  74. return -1;
  75. if ( setgid(getgid()) == -1 || setuid(getuid()) == -1 )
  76. return -1;
  77. if ( set_proc_listen(sock, 1) == -1 )
  78. return -1;
  79. return 0;
  80. }
  81. int
  82. wait4pid(pid_t pid)
  83. {
  84. int ec;
  85. struct __attribute__ ((__packed__)) payload_t {
  86. struct cn_msg msg;
  87. struct proc_event evt;
  88. } *payload;
  89. char buf[NLMSG_SPACE(sizeof(struct payload_t))];
  90. if ( ! listener_set && wait4pid_init() == -1 )
  91. return -1;
  92. while ( 1 ) {
  93. if ( (ec = recv(sock, buf, sizeof(buf), 0)) <= 0 ) {
  94. if ( ec == -1 && errno == EINTR )
  95. continue;
  96. else
  97. return -1;
  98. }
  99. payload = (struct payload_t *) NLMSG_DATA(buf);
  100. if ( payload->evt.what == PROC_EVENT_EXIT
  101. && payload->evt.event_data.exit.process_pid == pid ) {
  102. ec = payload->evt.event_data.exit.exit_code / 256;
  103. break;
  104. }
  105. }
  106. return ec;
  107. }
  108. int
  109. wait4all(pid_t *pids, size_t len)
  110. {
  111. int ec, nproc;
  112. struct __attribute__ ((__packed__)) payload_t {
  113. struct cn_msg msg;
  114. struct proc_event evt;
  115. } *payload;
  116. char buf[NLMSG_SPACE(sizeof(struct payload_t))];
  117. if ( ! listener_set && wait4pid_init() == -1 )
  118. return -1;
  119. nproc = len;
  120. while ( nproc > 0 ) {
  121. int ret, i;
  122. if ( (ret = recv(sock, buf, sizeof(buf), 0)) <= 0 ) {
  123. if ( ret == -1 && errno == EINTR )
  124. continue;
  125. else
  126. return -1;
  127. }
  128. payload = (struct payload_t *) NLMSG_DATA(buf);
  129. if ( payload->evt.what != PROC_EVENT_EXIT )
  130. continue;
  131. for ( i = 0; i < len; i++ ) {
  132. if ( pids[i] != -1
  133. && pids[i] == payload->evt.event_data.exit.process_pid ) {
  134. pids[i] = -1;
  135. nproc -= 1;
  136. if ( i == len - 1 )
  137. ec = payload->evt.event_data.exit.exit_code / 256;
  138. }
  139. }
  140. }
  141. return ec;
  142. }
  143. #else /* HAVE_LINUX_NETLINK_H */
  144. #include <string.h>
  145. #include <sys/stat.h>
  146. #include <unistd.h>
  147. int
  148. wait4pid(pid_t pid)
  149. {
  150. struct stat st_buf;
  151. char path[20];
  152. snprintf(path, sizeof(path), "/proc/%d", pid);
  153. while ( stat(path, &st_buf) != -1 )
  154. sleep(1);
  155. /* Anything else than ENOENT is an error. */
  156. return errno == ENOENT ? 0 : -1;
  157. }
  158. int
  159. wait4all(pid_t *pids, size_t len)
  160. {
  161. int nproc, i;
  162. char *paths;
  163. struct stat st_buf;
  164. paths = malloc(20 * len);
  165. for ( i = 0; i < len; i++ )
  166. snprintf(paths + 20 * i, 20, "/proc/%d", pids[i]);
  167. nproc = len;
  168. while ( nproc > 0 ) {
  169. for ( i = 0; i < len; i++ ) {
  170. if ( pids[i] == -1 )
  171. continue;
  172. if ( stat(paths + 20 * i, &st_buf) == -1 ) {
  173. if ( errno != ENOENT )
  174. return -1;
  175. pids[i] = -1;
  176. nproc -= 1;
  177. }
  178. }
  179. sleep(1);
  180. }
  181. free(paths);
  182. return 0;
  183. }
  184. #endif /* ! HAVE_LINUX_NETLINK_H */