]> git.ipfire.org Git - thirdparty/util-linux.git/blob - misc-utils/waitpid.c
Merge branch 'PR/libmount-stat.h'
[thirdparty/util-linux.git] / misc-utils / waitpid.c
1 /*
2 * waitpid(1) - wait for process termination
3 *
4 * Copyright (C) 2022 Thomas Weißschuh <thomas@t-8ch.de>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it would be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #include <sys/epoll.h>
22 #include <sys/timerfd.h>
23 #include <assert.h>
24 #include <stdlib.h>
25 #include <errno.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <stdbool.h>
29 #include <getopt.h>
30
31 #include "pidfd-utils.h"
32 #include "c.h"
33 #include "nls.h"
34 #include "xalloc.h"
35 #include "strutils.h"
36 #include "exitcodes.h"
37 #include "timeutils.h"
38 #include "optutils.h"
39
40 #define EXIT_TIMEOUT_EXPIRED 3
41
42 #define TIMEOUT_SOCKET_IDX UINT64_MAX
43
44 static bool verbose = false;
45 static struct timespec timeout;
46 static bool allow_exited = false;
47 static size_t count;
48
49 static pid_t *parse_pids(size_t n_strings, char * const *strings)
50 {
51 pid_t *pids = xcalloc(n_strings, sizeof(*pids));
52
53 for (size_t i = 0; i < n_strings; i++)
54 pids[i] = strtopid_or_err(strings[i], _("failed to parse pid"));
55
56 return pids;
57 }
58
59 static int *open_pidfds(size_t n_pids, pid_t *pids)
60 {
61 int *pidfds = xcalloc(n_pids, sizeof(*pidfds));
62
63 for (size_t i = 0; i < n_pids; i++) {
64 pidfds[i] = pidfd_open(pids[i], 0);
65 if (pidfds[i] == -1) {
66 if (allow_exited && errno == ESRCH) {
67 warnx(_("PID %d has exited, skipping"), pids[i]);
68 continue;
69 }
70 err_nosys(EXIT_FAILURE, _("could not open pid %u"), pids[i]);
71 }
72 }
73
74 return pidfds;
75 }
76
77 static int open_timeoutfd(void)
78 {
79 int fd;
80 struct itimerspec timer = {};
81
82 if (!timeout.tv_sec && !timeout.tv_nsec)
83 return -1;
84
85 timer.it_value = timeout;
86
87 fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
88 if (fd == -1)
89 err_nosys(EXIT_FAILURE, _("could not create timerfd"));
90
91 if (timerfd_settime(fd, 0, &timer, NULL))
92 err_nosys(EXIT_FAILURE, _("could not set timer"));
93
94 return fd;
95
96 }
97
98 static size_t add_listeners(int epll, size_t n_pids, int * const pidfds, int timeoutfd)
99 {
100 size_t skipped = 0;
101 struct epoll_event evt = {
102 .events = EPOLLIN,
103 };
104
105 if (timeoutfd != -1) {
106 evt.data.u64 = TIMEOUT_SOCKET_IDX;
107 if (epoll_ctl(epll, EPOLL_CTL_ADD, timeoutfd, &evt))
108 err_nosys(EXIT_FAILURE, _("could not add timerfd"));
109 }
110
111 for (size_t i = 0; i < n_pids; i++) {
112 if (pidfds[i] == -1) {
113 skipped++;
114 continue;
115 }
116 evt.data.u64 = i;
117 if (epoll_ctl(epll, EPOLL_CTL_ADD, pidfds[i], &evt))
118 err_nosys(EXIT_FAILURE, _("could not add listener"));
119 }
120
121 return n_pids - skipped;
122 }
123
124 static void wait_for_exits(int epll, size_t active_pids, pid_t * const pids,
125 int * const pidfds)
126 {
127 while (active_pids) {
128 struct epoll_event evt;
129 int ret, fd;
130
131 ret = epoll_wait(epll, &evt, 1, -1);
132 if (ret == -1) {
133 if (errno == EINTR)
134 continue;
135 else
136 err_nosys(EXIT_FAILURE, _("failure during wait"));
137 }
138 if (evt.data.u64 == TIMEOUT_SOCKET_IDX) {
139 if (verbose)
140 printf(_("Timeout expired\n"));
141 exit(EXIT_TIMEOUT_EXPIRED);
142 }
143 if (verbose)
144 printf(_("PID %d finished\n"), pids[evt.data.u64]);
145 assert((size_t) ret <= active_pids);
146 fd = pidfds[evt.data.u64];
147 epoll_ctl(epll, EPOLL_CTL_DEL, fd, NULL);
148 active_pids -= ret;
149 }
150 }
151
152 static void __attribute__((__noreturn__)) usage(void)
153 {
154 FILE *out = stdout;
155
156 fputs(USAGE_HEADER, out);
157 fprintf(out, _(" %s [options] pid...\n"), program_invocation_short_name);
158
159 fputs(USAGE_OPTIONS, out);
160 fputs(_(" -v, --verbose be more verbose\n"), out);
161 fputs(_(" -t, --timeout=<timeout> wait at most timeout seconds\n"), out);
162 fputs(_(" -e, --exited allow exited PIDs\n"), out);
163 fputs(_(" -c, --count=<count> number of process exits to wait for\n"), out);
164
165 fputs(USAGE_SEPARATOR, out);
166 fprintf(out, USAGE_HELP_OPTIONS(25));
167
168 fprintf(out, USAGE_MAN_TAIL("waitpid(1)"));
169
170 exit(EXIT_SUCCESS);
171 }
172
173 static int parse_options(int argc, char **argv)
174 {
175 int c;
176 static const struct option longopts[] = {
177 { "verbose", no_argument, NULL, 'v' },
178 { "timeout", required_argument, NULL, 't' },
179 { "exited", no_argument, NULL, 'e' },
180 { "count", required_argument, NULL, 'c' },
181 { "version", no_argument, NULL, 'V' },
182 { "help", no_argument, NULL, 'h' },
183 { 0 }
184 };
185 static const ul_excl_t excl[] = { /* rows and cols in ASCII order */
186 { 'c', 'e' },
187 { 0 }
188 };
189 int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;
190
191 while ((c = getopt_long (argc, argv, "vVht:c:e", longopts, NULL)) != -1) {
192
193 err_exclusive_options(c, longopts, excl, excl_st);
194
195 switch (c) {
196 case 'v':
197 verbose = true;
198 break;
199 case 't':
200 strtotimespec_or_err(optarg, &timeout,
201 _("Could not parse timeout"));
202 break;
203 case 'e':
204 allow_exited = true;
205 break;
206 case 'c':
207 count = str2num_or_err(optarg, 10, _("Invalid count"),
208 1, INT64_MAX);
209 break;
210 case 'V':
211 print_version(EXIT_SUCCESS);
212 case 'h':
213 usage();
214 default:
215 errtryhelp(EXIT_FAILURE);
216 }
217 }
218
219 return optind;
220 }
221
222 int main(int argc, char **argv)
223 {
224 int pid_idx, epoll, timeoutfd, *pidfds;
225 size_t n_pids, active_pids;
226
227 setlocale(LC_ALL, "");
228 bindtextdomain(PACKAGE, LOCALEDIR);
229 textdomain(PACKAGE);
230
231 pid_idx = parse_options(argc, argv);
232 n_pids = argc - pid_idx;
233 if (!n_pids)
234 errx(EXIT_FAILURE, _("no PIDs specified"));
235
236 if (count && count > n_pids)
237 errx(EXIT_FAILURE,
238 _("can't want for %zu of %zu PIDs"), count, n_pids);
239
240 pid_t *pids = parse_pids(argc - pid_idx, argv + pid_idx);
241
242 pidfds = open_pidfds(n_pids, pids);
243 timeoutfd = open_timeoutfd();
244 epoll = epoll_create(n_pids);
245 if (epoll == -1)
246 err_nosys(EXIT_FAILURE, _("could not create epoll"));
247
248 active_pids = add_listeners(epoll, n_pids, pidfds, timeoutfd);
249 if (count)
250 active_pids = min(active_pids, count);
251 wait_for_exits(epoll, active_pids, pids, pidfds);
252 }