]>
Commit | Line | Data |
---|---|---|
d46f37fd | 1 | /* |
22582bb2 | 2 | * Copyright (C) 2003-2013 Kay Sievers <kay@vrfy.org> |
d46f37fd | 3 | * |
55e9959b KS |
4 | * This program is free software: you can redistribute it and/or modify |
5 | * it under the terms of the GNU General Public License as published by | |
6 | * the Free Software Foundation, either version 2 of the License, or | |
7 | * (at your option) any later version. | |
d46f37fd | 8 | * |
55e9959b KS |
9 | * This program is distributed in the hope that it will be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License | |
15 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
d46f37fd KS |
16 | */ |
17 | ||
d46f37fd KS |
18 | #include <stdlib.h> |
19 | #include <stdio.h> | |
20 | #include <stddef.h> | |
21 | #include <unistd.h> | |
22 | #include <fcntl.h> | |
23 | #include <errno.h> | |
24 | #include <ctype.h> | |
25 | #include <string.h> | |
959e8b5d | 26 | #include <net/if.h> |
2181d30a | 27 | #include <sys/prctl.h> |
0a6f50c0 | 28 | #include <poll.h> |
2181d30a KS |
29 | #include <sys/epoll.h> |
30 | #include <sys/wait.h> | |
2181d30a | 31 | #include <sys/signalfd.h> |
d46f37fd KS |
32 | |
33 | #include "udev.h" | |
16d26d55 | 34 | #include "rtnl-util.h" |
d46f37fd | 35 | |
9ec6e95b | 36 | struct udev_event *udev_event_new(struct udev_device *dev) { |
912541b0 KS |
37 | struct udev *udev = udev_device_get_udev(dev); |
38 | struct udev_event *event; | |
39 | ||
955d98c9 | 40 | event = new0(struct udev_event, 1); |
912541b0 KS |
41 | if (event == NULL) |
42 | return NULL; | |
43 | event->dev = dev; | |
44 | event->udev = udev; | |
45 | udev_list_init(udev, &event->run_list, false); | |
c26547d6 | 46 | udev_list_init(udev, &event->seclabel_list, false); |
912541b0 | 47 | event->fd_signal = -1; |
40fe8b11 | 48 | event->birth_usec = now(CLOCK_MONOTONIC); |
912541b0 | 49 | return event; |
aa8734ff KS |
50 | } |
51 | ||
9ec6e95b | 52 | void udev_event_unref(struct udev_event *event) { |
912541b0 KS |
53 | if (event == NULL) |
54 | return; | |
4c83d994 | 55 | sd_rtnl_unref(event->rtnl); |
912541b0 | 56 | udev_list_cleanup(&event->run_list); |
c26547d6 | 57 | udev_list_cleanup(&event->seclabel_list); |
912541b0 KS |
58 | free(event->program_result); |
59 | free(event->name); | |
912541b0 | 60 | free(event); |
aa8734ff KS |
61 | } |
62 | ||
9ec6e95b | 63 | size_t udev_event_apply_format(struct udev_event *event, const char *src, char *dest, size_t size) { |
912541b0 KS |
64 | struct udev_device *dev = event->dev; |
65 | enum subst_type { | |
66 | SUBST_UNKNOWN, | |
67 | SUBST_DEVNODE, | |
68 | SUBST_ATTR, | |
69 | SUBST_ENV, | |
70 | SUBST_KERNEL, | |
71 | SUBST_KERNEL_NUMBER, | |
72 | SUBST_DRIVER, | |
73 | SUBST_DEVPATH, | |
74 | SUBST_ID, | |
75 | SUBST_MAJOR, | |
76 | SUBST_MINOR, | |
77 | SUBST_RESULT, | |
78 | SUBST_PARENT, | |
79 | SUBST_NAME, | |
80 | SUBST_LINKS, | |
81 | SUBST_ROOT, | |
82 | SUBST_SYS, | |
83 | }; | |
84 | static const struct subst_map { | |
04a9d3a0 KS |
85 | const char *name; |
86 | const char fmt; | |
912541b0 KS |
87 | enum subst_type type; |
88 | } map[] = { | |
3fd0c4c6 KS |
89 | { .name = "devnode", .fmt = 'N', .type = SUBST_DEVNODE }, |
90 | { .name = "tempnode", .fmt = 'N', .type = SUBST_DEVNODE }, | |
91 | { .name = "attr", .fmt = 's', .type = SUBST_ATTR }, | |
92 | { .name = "sysfs", .fmt = 's', .type = SUBST_ATTR }, | |
93 | { .name = "env", .fmt = 'E', .type = SUBST_ENV }, | |
94 | { .name = "kernel", .fmt = 'k', .type = SUBST_KERNEL }, | |
95 | { .name = "number", .fmt = 'n', .type = SUBST_KERNEL_NUMBER }, | |
96 | { .name = "driver", .fmt = 'd', .type = SUBST_DRIVER }, | |
97 | { .name = "devpath", .fmt = 'p', .type = SUBST_DEVPATH }, | |
98 | { .name = "id", .fmt = 'b', .type = SUBST_ID }, | |
99 | { .name = "major", .fmt = 'M', .type = SUBST_MAJOR }, | |
100 | { .name = "minor", .fmt = 'm', .type = SUBST_MINOR }, | |
101 | { .name = "result", .fmt = 'c', .type = SUBST_RESULT }, | |
102 | { .name = "parent", .fmt = 'P', .type = SUBST_PARENT }, | |
103 | { .name = "name", .fmt = 'D', .type = SUBST_NAME }, | |
104 | { .name = "links", .fmt = 'L', .type = SUBST_LINKS }, | |
105 | { .name = "root", .fmt = 'r', .type = SUBST_ROOT }, | |
106 | { .name = "sys", .fmt = 'S', .type = SUBST_SYS }, | |
912541b0 KS |
107 | }; |
108 | const char *from; | |
109 | char *s; | |
110 | size_t l; | |
111 | ||
112 | from = src; | |
113 | s = dest; | |
114 | l = size; | |
115 | ||
116 | for (;;) { | |
117 | enum subst_type type = SUBST_UNKNOWN; | |
118 | char attrbuf[UTIL_PATH_SIZE]; | |
119 | char *attr = NULL; | |
120 | ||
121 | while (from[0] != '\0') { | |
122 | if (from[0] == '$') { | |
123 | /* substitute named variable */ | |
124 | unsigned int i; | |
125 | ||
126 | if (from[1] == '$') { | |
127 | from++; | |
128 | goto copy; | |
129 | } | |
130 | ||
8fef0ff2 | 131 | for (i = 0; i < ELEMENTSOF(map); i++) { |
33502ffe | 132 | if (startswith(&from[1], map[i].name)) { |
912541b0 KS |
133 | type = map[i].type; |
134 | from += strlen(map[i].name)+1; | |
912541b0 KS |
135 | goto subst; |
136 | } | |
137 | } | |
138 | } else if (from[0] == '%') { | |
139 | /* substitute format char */ | |
140 | unsigned int i; | |
141 | ||
142 | if (from[1] == '%') { | |
143 | from++; | |
144 | goto copy; | |
145 | } | |
146 | ||
8fef0ff2 | 147 | for (i = 0; i < ELEMENTSOF(map); i++) { |
912541b0 KS |
148 | if (from[1] == map[i].fmt) { |
149 | type = map[i].type; | |
150 | from += 2; | |
912541b0 KS |
151 | goto subst; |
152 | } | |
153 | } | |
154 | } | |
065db052 | 155 | copy: |
912541b0 KS |
156 | /* copy char */ |
157 | if (l == 0) | |
158 | goto out; | |
159 | s[0] = from[0]; | |
160 | from++; | |
161 | s++; | |
162 | l--; | |
163 | } | |
164 | ||
165 | goto out; | |
065db052 | 166 | subst: |
912541b0 KS |
167 | /* extract possible $format{attr} */ |
168 | if (from[0] == '{') { | |
169 | unsigned int i; | |
170 | ||
171 | from++; | |
172 | for (i = 0; from[i] != '}'; i++) { | |
173 | if (from[i] == '\0') { | |
9f6445e3 | 174 | log_error("missing closing brace for format '%s'", src); |
912541b0 KS |
175 | goto out; |
176 | } | |
177 | } | |
178 | if (i >= sizeof(attrbuf)) | |
179 | goto out; | |
180 | memcpy(attrbuf, from, i); | |
181 | attrbuf[i] = '\0'; | |
182 | from += i+1; | |
183 | attr = attrbuf; | |
184 | } else { | |
185 | attr = NULL; | |
186 | } | |
187 | ||
188 | switch (type) { | |
189 | case SUBST_DEVPATH: | |
d5a89d7d | 190 | l = strpcpy(&s, l, udev_device_get_devpath(dev)); |
912541b0 KS |
191 | break; |
192 | case SUBST_KERNEL: | |
d5a89d7d | 193 | l = strpcpy(&s, l, udev_device_get_sysname(dev)); |
912541b0 KS |
194 | break; |
195 | case SUBST_KERNEL_NUMBER: | |
196 | if (udev_device_get_sysnum(dev) == NULL) | |
197 | break; | |
d5a89d7d | 198 | l = strpcpy(&s, l, udev_device_get_sysnum(dev)); |
912541b0 KS |
199 | break; |
200 | case SUBST_ID: | |
201 | if (event->dev_parent == NULL) | |
202 | break; | |
d5a89d7d | 203 | l = strpcpy(&s, l, udev_device_get_sysname(event->dev_parent)); |
912541b0 KS |
204 | break; |
205 | case SUBST_DRIVER: { | |
206 | const char *driver; | |
207 | ||
208 | if (event->dev_parent == NULL) | |
209 | break; | |
210 | ||
211 | driver = udev_device_get_driver(event->dev_parent); | |
212 | if (driver == NULL) | |
213 | break; | |
d5a89d7d | 214 | l = strpcpy(&s, l, driver); |
912541b0 KS |
215 | break; |
216 | } | |
217 | case SUBST_MAJOR: { | |
218 | char num[UTIL_PATH_SIZE]; | |
219 | ||
1fa2f38f | 220 | sprintf(num, "%u", major(udev_device_get_devnum(dev))); |
d5a89d7d | 221 | l = strpcpy(&s, l, num); |
912541b0 KS |
222 | break; |
223 | } | |
224 | case SUBST_MINOR: { | |
225 | char num[UTIL_PATH_SIZE]; | |
226 | ||
1fa2f38f | 227 | sprintf(num, "%u", minor(udev_device_get_devnum(dev))); |
d5a89d7d | 228 | l = strpcpy(&s, l, num); |
912541b0 KS |
229 | break; |
230 | } | |
231 | case SUBST_RESULT: { | |
232 | char *rest; | |
233 | int i; | |
234 | ||
235 | if (event->program_result == NULL) | |
236 | break; | |
237 | /* get part part of the result string */ | |
238 | i = 0; | |
239 | if (attr != NULL) | |
240 | i = strtoul(attr, &rest, 10); | |
241 | if (i > 0) { | |
242 | char result[UTIL_PATH_SIZE]; | |
243 | char tmp[UTIL_PATH_SIZE]; | |
244 | char *cpos; | |
245 | ||
d5a89d7d | 246 | strscpy(result, sizeof(result), event->program_result); |
912541b0 KS |
247 | cpos = result; |
248 | while (--i) { | |
249 | while (cpos[0] != '\0' && !isspace(cpos[0])) | |
250 | cpos++; | |
251 | while (isspace(cpos[0])) | |
252 | cpos++; | |
66390abe LN |
253 | if (cpos[0] == '\0') |
254 | break; | |
912541b0 KS |
255 | } |
256 | if (i > 0) { | |
9f6445e3 | 257 | log_error("requested part of result string not found"); |
912541b0 KS |
258 | break; |
259 | } | |
d5a89d7d | 260 | strscpy(tmp, sizeof(tmp), cpos); |
912541b0 KS |
261 | /* %{2+}c copies the whole string from the second part on */ |
262 | if (rest[0] != '+') { | |
263 | cpos = strchr(tmp, ' '); | |
264 | if (cpos) | |
265 | cpos[0] = '\0'; | |
266 | } | |
d5a89d7d | 267 | l = strpcpy(&s, l, tmp); |
912541b0 | 268 | } else { |
d5a89d7d | 269 | l = strpcpy(&s, l, event->program_result); |
912541b0 KS |
270 | } |
271 | break; | |
272 | } | |
273 | case SUBST_ATTR: { | |
274 | const char *value = NULL; | |
275 | char vbuf[UTIL_NAME_SIZE]; | |
276 | size_t len; | |
277 | int count; | |
278 | ||
279 | if (attr == NULL) { | |
9f6445e3 | 280 | log_error("missing file parameter for attr"); |
912541b0 KS |
281 | break; |
282 | } | |
283 | ||
284 | /* try to read the value specified by "[dmi/id]product_name" */ | |
285 | if (util_resolve_subsys_kernel(event->udev, attr, vbuf, sizeof(vbuf), 1) == 0) | |
286 | value = vbuf; | |
287 | ||
288 | /* try to read the attribute the device */ | |
289 | if (value == NULL) | |
290 | value = udev_device_get_sysattr_value(event->dev, attr); | |
291 | ||
292 | /* try to read the attribute of the parent device, other matches have selected */ | |
293 | if (value == NULL && event->dev_parent != NULL && event->dev_parent != event->dev) | |
294 | value = udev_device_get_sysattr_value(event->dev_parent, attr); | |
295 | ||
296 | if (value == NULL) | |
297 | break; | |
298 | ||
299 | /* strip trailing whitespace, and replace unwanted characters */ | |
300 | if (value != vbuf) | |
d5a89d7d | 301 | strscpy(vbuf, sizeof(vbuf), value); |
912541b0 KS |
302 | len = strlen(vbuf); |
303 | while (len > 0 && isspace(vbuf[--len])) | |
304 | vbuf[len] = '\0'; | |
305 | count = util_replace_chars(vbuf, UDEV_ALLOWED_CHARS_INPUT); | |
306 | if (count > 0) | |
9f6445e3 | 307 | log_debug("%i character(s) replaced" , count); |
d5a89d7d | 308 | l = strpcpy(&s, l, vbuf); |
912541b0 KS |
309 | break; |
310 | } | |
311 | case SUBST_PARENT: { | |
312 | struct udev_device *dev_parent; | |
313 | const char *devnode; | |
314 | ||
315 | dev_parent = udev_device_get_parent(event->dev); | |
316 | if (dev_parent == NULL) | |
317 | break; | |
318 | devnode = udev_device_get_devnode(dev_parent); | |
6ada823a | 319 | if (devnode != NULL) |
d5a89d7d | 320 | l = strpcpy(&s, l, devnode + strlen("/dev/")); |
912541b0 KS |
321 | break; |
322 | } | |
323 | case SUBST_DEVNODE: | |
324 | if (udev_device_get_devnode(dev) != NULL) | |
d5a89d7d | 325 | l = strpcpy(&s, l, udev_device_get_devnode(dev)); |
912541b0 | 326 | break; |
6ada823a KS |
327 | case SUBST_NAME: |
328 | if (event->name != NULL) | |
d5a89d7d | 329 | l = strpcpy(&s, l, event->name); |
6ada823a | 330 | else if (udev_device_get_devnode(dev) != NULL) |
d5a89d7d | 331 | l = strpcpy(&s, l, udev_device_get_devnode(dev) + strlen("/dev/")); |
6ada823a | 332 | else |
d5a89d7d | 333 | l = strpcpy(&s, l, udev_device_get_sysname(dev)); |
912541b0 KS |
334 | break; |
335 | case SUBST_LINKS: { | |
912541b0 KS |
336 | struct udev_list_entry *list_entry; |
337 | ||
338 | list_entry = udev_device_get_devlinks_list_entry(dev); | |
339 | if (list_entry == NULL) | |
340 | break; | |
d5a89d7d | 341 | l = strpcpy(&s, l, udev_list_entry_get_name(list_entry) + strlen("/dev/")); |
912541b0 | 342 | udev_list_entry_foreach(list_entry, udev_list_entry_get_next(list_entry)) |
d5a89d7d | 343 | l = strpcpyl(&s, l, " ", udev_list_entry_get_name(list_entry) + strlen("/dev/"), NULL); |
912541b0 KS |
344 | break; |
345 | } | |
346 | case SUBST_ROOT: | |
d5a89d7d | 347 | l = strpcpy(&s, l, "/dev"); |
912541b0 KS |
348 | break; |
349 | case SUBST_SYS: | |
d5a89d7d | 350 | l = strpcpy(&s, l, "/sys"); |
912541b0 KS |
351 | break; |
352 | case SUBST_ENV: | |
353 | if (attr == NULL) { | |
912541b0 KS |
354 | break; |
355 | } else { | |
356 | const char *value; | |
357 | ||
358 | value = udev_device_get_property_value(event->dev, attr); | |
359 | if (value == NULL) | |
360 | break; | |
d5a89d7d | 361 | l = strpcpy(&s, l, value); |
912541b0 KS |
362 | break; |
363 | } | |
364 | default: | |
9f6445e3 | 365 | log_error("unknown substitution type=%i", type); |
912541b0 KS |
366 | break; |
367 | } | |
368 | } | |
065db052 KS |
369 | |
370 | out: | |
912541b0 | 371 | s[0] = '\0'; |
912541b0 | 372 | return l; |
f1128767 KS |
373 | } |
374 | ||
2181d30a | 375 | static int spawn_exec(struct udev_event *event, |
912541b0 | 376 | const char *cmd, char *const argv[], char **envp, const sigset_t *sigmask, |
9ec6e95b | 377 | int fd_stdout, int fd_stderr) { |
19c784c4 | 378 | _cleanup_close_ int fd = -1; |
912541b0 KS |
379 | |
380 | /* discard child output or connect to pipe */ | |
381 | fd = open("/dev/null", O_RDWR); | |
382 | if (fd >= 0) { | |
383 | dup2(fd, STDIN_FILENO); | |
384 | if (fd_stdout < 0) | |
385 | dup2(fd, STDOUT_FILENO); | |
386 | if (fd_stderr < 0) | |
387 | dup2(fd, STDERR_FILENO); | |
19c784c4 | 388 | } else |
56f64d95 | 389 | log_error_errno(errno, "open /dev/null failed: %m"); |
912541b0 KS |
390 | |
391 | /* connect pipes to std{out,err} */ | |
392 | if (fd_stdout >= 0) { | |
393 | dup2(fd_stdout, STDOUT_FILENO); | |
19c784c4 | 394 | safe_close(fd_stdout); |
912541b0 KS |
395 | } |
396 | if (fd_stderr >= 0) { | |
397 | dup2(fd_stderr, STDERR_FILENO); | |
19c784c4 | 398 | safe_close(fd_stderr); |
912541b0 KS |
399 | } |
400 | ||
401 | /* terminate child in case parent goes away */ | |
402 | prctl(PR_SET_PDEATHSIG, SIGTERM); | |
403 | ||
404 | /* restore original udev sigmask before exec */ | |
405 | if (sigmask) | |
406 | sigprocmask(SIG_SETMASK, sigmask, NULL); | |
407 | ||
408 | execve(argv[0], argv, envp); | |
409 | ||
410 | /* exec failed */ | |
56f64d95 | 411 | log_error_errno(errno, "failed to execute '%s' '%s': %m", argv[0], cmd); |
19c784c4 TG |
412 | |
413 | return -errno; | |
2181d30a KS |
414 | } |
415 | ||
f18f225c | 416 | static void spawn_read(struct udev_event *event, |
dd5eddd2 KS |
417 | usec_t timeout_usec, |
418 | const char *cmd, | |
419 | int fd_stdout, int fd_stderr, | |
9ec6e95b | 420 | char *result, size_t ressize) { |
4cd5d5ad TG |
421 | _cleanup_close_ int fd_ep = -1; |
422 | struct epoll_event ep_outpipe = { | |
423 | .events = EPOLLIN, | |
424 | .data.ptr = &fd_stdout, | |
425 | }; | |
426 | struct epoll_event ep_errpipe = { | |
427 | .events = EPOLLIN, | |
428 | .data.ptr = &fd_stderr, | |
429 | }; | |
912541b0 | 430 | size_t respos = 0; |
4cd5d5ad | 431 | int r; |
912541b0 KS |
432 | |
433 | /* read from child if requested */ | |
434 | if (fd_stdout < 0 && fd_stderr < 0) | |
435 | return; | |
436 | ||
437 | fd_ep = epoll_create1(EPOLL_CLOEXEC); | |
438 | if (fd_ep < 0) { | |
56f64d95 | 439 | log_error_errno(errno, "error creating epoll fd: %m"); |
4cd5d5ad | 440 | return; |
912541b0 KS |
441 | } |
442 | ||
443 | if (fd_stdout >= 0) { | |
4cd5d5ad TG |
444 | r = epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_stdout, &ep_outpipe); |
445 | if (r < 0) { | |
56f64d95 | 446 | log_error_errno(errno, "fail to add stdout fd to epoll: %m"); |
4cd5d5ad | 447 | return; |
912541b0 KS |
448 | } |
449 | } | |
450 | ||
451 | if (fd_stderr >= 0) { | |
4cd5d5ad TG |
452 | r = epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_stderr, &ep_errpipe); |
453 | if (r < 0) { | |
56f64d95 | 454 | log_error_errno(errno, "fail to add stderr fd to epoll: %m"); |
4cd5d5ad | 455 | return; |
912541b0 KS |
456 | } |
457 | } | |
458 | ||
459 | /* read child output */ | |
460 | while (fd_stdout >= 0 || fd_stderr >= 0) { | |
461 | int timeout; | |
462 | int fdcount; | |
463 | struct epoll_event ev[4]; | |
464 | int i; | |
465 | ||
dd5eddd2 | 466 | if (timeout_usec > 0) { |
40fe8b11 | 467 | usec_t age_usec; |
912541b0 | 468 | |
40fe8b11 | 469 | age_usec = now(CLOCK_MONOTONIC) - event->birth_usec; |
dd5eddd2 | 470 | if (age_usec >= timeout_usec) { |
9f6445e3 | 471 | log_error("timeout '%s'", cmd); |
4cd5d5ad | 472 | return; |
912541b0 | 473 | } |
dd5eddd2 | 474 | timeout = ((timeout_usec - age_usec) / USEC_PER_MSEC) + MSEC_PER_SEC; |
912541b0 KS |
475 | } else { |
476 | timeout = -1; | |
477 | } | |
478 | ||
8fef0ff2 | 479 | fdcount = epoll_wait(fd_ep, ev, ELEMENTSOF(ev), timeout); |
912541b0 KS |
480 | if (fdcount < 0) { |
481 | if (errno == EINTR) | |
482 | continue; | |
56f64d95 | 483 | log_error_errno(errno, "failed to poll: %m"); |
4cd5d5ad TG |
484 | return; |
485 | } else if (fdcount == 0) { | |
9f6445e3 | 486 | log_error("timeout '%s'", cmd); |
4cd5d5ad | 487 | return; |
912541b0 KS |
488 | } |
489 | ||
490 | for (i = 0; i < fdcount; i++) { | |
491 | int *fd = (int *)ev[i].data.ptr; | |
492 | ||
3f796750 TG |
493 | if (*fd < 0) |
494 | continue; | |
495 | ||
912541b0 KS |
496 | if (ev[i].events & EPOLLIN) { |
497 | ssize_t count; | |
498 | char buf[4096]; | |
499 | ||
500 | count = read(*fd, buf, sizeof(buf)-1); | |
501 | if (count <= 0) | |
502 | continue; | |
503 | buf[count] = '\0'; | |
504 | ||
505 | /* store stdout result */ | |
506 | if (result != NULL && *fd == fd_stdout) { | |
507 | if (respos + count < ressize) { | |
508 | memcpy(&result[respos], buf, count); | |
509 | respos += count; | |
510 | } else { | |
1fa2f38f | 511 | log_error("'%s' ressize %zu too short", cmd, ressize); |
912541b0 KS |
512 | } |
513 | } | |
514 | ||
515 | /* log debug output only if we watch stderr */ | |
516 | if (fd_stderr >= 0) { | |
517 | char *pos; | |
518 | char *line; | |
519 | ||
520 | pos = buf; | |
521 | while ((line = strsep(&pos, "\n"))) { | |
522 | if (pos != NULL || line[0] != '\0') | |
9f6445e3 | 523 | log_debug("'%s'(%s) '%s'", cmd, *fd == fd_stdout ? "out" : "err" , line); |
912541b0 KS |
524 | } |
525 | } | |
526 | } else if (ev[i].events & EPOLLHUP) { | |
4cd5d5ad TG |
527 | r = epoll_ctl(fd_ep, EPOLL_CTL_DEL, *fd, NULL); |
528 | if (r < 0) { | |
56f64d95 | 529 | log_error_errno(errno, "failed to remove fd from epoll: %m"); |
4cd5d5ad | 530 | return; |
912541b0 KS |
531 | } |
532 | *fd = -1; | |
533 | } | |
534 | } | |
535 | } | |
536 | ||
537 | /* return the child's stdout string */ | |
baa30fbc | 538 | if (result != NULL) |
912541b0 | 539 | result[respos] = '\0'; |
2181d30a KS |
540 | } |
541 | ||
dd5eddd2 KS |
542 | static int spawn_wait(struct udev_event *event, |
543 | usec_t timeout_usec, | |
67117413 | 544 | usec_t timeout_warn_usec, |
dd5eddd2 | 545 | const char *cmd, pid_t pid) { |
912541b0 KS |
546 | struct pollfd pfd[1]; |
547 | int err = 0; | |
548 | ||
549 | pfd[0].events = POLLIN; | |
550 | pfd[0].fd = event->fd_signal; | |
551 | ||
552 | while (pid > 0) { | |
553 | int timeout; | |
67117413 | 554 | int timeout_warn = 0; |
912541b0 KS |
555 | int fdcount; |
556 | ||
dd5eddd2 | 557 | if (timeout_usec > 0) { |
40fe8b11 | 558 | usec_t age_usec; |
912541b0 | 559 | |
40fe8b11 | 560 | age_usec = now(CLOCK_MONOTONIC) - event->birth_usec; |
dd5eddd2 | 561 | if (age_usec >= timeout_usec) |
912541b0 | 562 | timeout = 1000; |
67117413 TG |
563 | else { |
564 | if (timeout_warn_usec > 0) | |
565 | timeout_warn = ((timeout_warn_usec - age_usec) / USEC_PER_MSEC) + MSEC_PER_SEC; | |
566 | ||
567 | timeout = ((timeout_usec - timeout_warn_usec - age_usec) / USEC_PER_MSEC) + MSEC_PER_SEC; | |
568 | } | |
912541b0 KS |
569 | } else { |
570 | timeout = -1; | |
571 | } | |
572 | ||
67117413 | 573 | fdcount = poll(pfd, 1, timeout_warn); |
912541b0 KS |
574 | if (fdcount < 0) { |
575 | if (errno == EINTR) | |
576 | continue; | |
577 | err = -errno; | |
56f64d95 | 578 | log_error_errno(errno, "failed to poll: %m"); |
912541b0 KS |
579 | goto out; |
580 | } | |
581 | if (fdcount == 0) { | |
1fa2f38f | 582 | log_warning("slow: '%s' ["PID_FMT"]", cmd, pid); |
67117413 TG |
583 | |
584 | fdcount = poll(pfd, 1, timeout); | |
585 | if (fdcount < 0) { | |
586 | if (errno == EINTR) | |
587 | continue; | |
588 | err = -errno; | |
56f64d95 | 589 | log_error_errno(errno, "failed to poll: %m"); |
67117413 TG |
590 | goto out; |
591 | } | |
592 | if (fdcount == 0) { | |
1fa2f38f | 593 | log_error("timeout: killing '%s' ["PID_FMT"]", cmd, pid); |
67117413 TG |
594 | kill(pid, SIGKILL); |
595 | } | |
912541b0 KS |
596 | } |
597 | ||
598 | if (pfd[0].revents & POLLIN) { | |
599 | struct signalfd_siginfo fdsi; | |
600 | int status; | |
601 | ssize_t size; | |
602 | ||
603 | size = read(event->fd_signal, &fdsi, sizeof(struct signalfd_siginfo)); | |
604 | if (size != sizeof(struct signalfd_siginfo)) | |
605 | continue; | |
606 | ||
607 | switch (fdsi.ssi_signo) { | |
608 | case SIGTERM: | |
609 | event->sigterm = true; | |
610 | break; | |
611 | case SIGCHLD: | |
612 | if (waitpid(pid, &status, WNOHANG) < 0) | |
613 | break; | |
614 | if (WIFEXITED(status)) { | |
1fa2f38f | 615 | log_debug("'%s' ["PID_FMT"] exit with return code %i", cmd, pid, WEXITSTATUS(status)); |
912541b0 KS |
616 | if (WEXITSTATUS(status) != 0) |
617 | err = -1; | |
618 | } else if (WIFSIGNALED(status)) { | |
1fa2f38f | 619 | log_error("'%s' ["PID_FMT"] terminated by signal %i (%s)", cmd, pid, WTERMSIG(status), strsignal(WTERMSIG(status))); |
912541b0 KS |
620 | err = -1; |
621 | } else if (WIFSTOPPED(status)) { | |
1fa2f38f | 622 | log_error("'%s' ["PID_FMT"] stopped", cmd, pid); |
912541b0 KS |
623 | err = -1; |
624 | } else if (WIFCONTINUED(status)) { | |
1fa2f38f | 625 | log_error("'%s' ["PID_FMT"] continued", cmd, pid); |
912541b0 KS |
626 | err = -1; |
627 | } else { | |
1fa2f38f | 628 | log_error("'%s' ["PID_FMT"] exit with status 0x%04x", cmd, pid, status); |
912541b0 KS |
629 | err = -1; |
630 | } | |
631 | pid = 0; | |
632 | break; | |
633 | } | |
634 | } | |
635 | } | |
2181d30a | 636 | out: |
912541b0 | 637 | return err; |
2181d30a KS |
638 | } |
639 | ||
9ec6e95b | 640 | int udev_build_argv(struct udev *udev, char *cmd, int *argc, char *argv[]) { |
912541b0 KS |
641 | int i = 0; |
642 | char *pos; | |
643 | ||
644 | if (strchr(cmd, ' ') == NULL) { | |
645 | argv[i++] = cmd; | |
646 | goto out; | |
647 | } | |
648 | ||
649 | pos = cmd; | |
650 | while (pos != NULL && pos[0] != '\0') { | |
651 | if (pos[0] == '\'') { | |
652 | /* do not separate quotes */ | |
653 | pos++; | |
654 | argv[i] = strsep(&pos, "\'"); | |
655 | if (pos != NULL) | |
656 | while (pos[0] == ' ') | |
657 | pos++; | |
658 | } else { | |
659 | argv[i] = strsep(&pos, " "); | |
660 | if (pos != NULL) | |
661 | while (pos[0] == ' ') | |
662 | pos++; | |
663 | } | |
912541b0 KS |
664 | i++; |
665 | } | |
e216e514 | 666 | out: |
912541b0 KS |
667 | argv[i] = NULL; |
668 | if (argc) | |
669 | *argc = i; | |
670 | return 0; | |
e216e514 KS |
671 | } |
672 | ||
2181d30a | 673 | int udev_event_spawn(struct udev_event *event, |
dd5eddd2 | 674 | usec_t timeout_usec, |
67117413 | 675 | usec_t timeout_warn_usec, |
912541b0 | 676 | const char *cmd, char **envp, const sigset_t *sigmask, |
dd5eddd2 | 677 | char *result, size_t ressize) { |
912541b0 KS |
678 | int outpipe[2] = {-1, -1}; |
679 | int errpipe[2] = {-1, -1}; | |
680 | pid_t pid; | |
681 | char arg[UTIL_PATH_SIZE]; | |
682 | char *argv[128]; | |
683 | char program[UTIL_PATH_SIZE]; | |
684 | int err = 0; | |
685 | ||
d5a89d7d | 686 | strscpy(arg, sizeof(arg), cmd); |
912541b0 KS |
687 | udev_build_argv(event->udev, arg, NULL, argv); |
688 | ||
689 | /* pipes from child to parent */ | |
25e773ee | 690 | if (result != NULL || log_get_max_level() >= LOG_INFO) { |
912541b0 KS |
691 | if (pipe2(outpipe, O_NONBLOCK) != 0) { |
692 | err = -errno; | |
56f64d95 | 693 | log_error_errno(errno, "pipe failed: %m"); |
912541b0 KS |
694 | goto out; |
695 | } | |
696 | } | |
25e773ee | 697 | if (log_get_max_level() >= LOG_INFO) { |
912541b0 KS |
698 | if (pipe2(errpipe, O_NONBLOCK) != 0) { |
699 | err = -errno; | |
56f64d95 | 700 | log_error_errno(errno, "pipe failed: %m"); |
912541b0 KS |
701 | goto out; |
702 | } | |
703 | } | |
704 | ||
705 | /* allow programs in /usr/lib/udev/ to be called without the path */ | |
706 | if (argv[0][0] != '/') { | |
d5a89d7d | 707 | strscpyl(program, sizeof(program), UDEVLIBEXECDIR "/", argv[0], NULL); |
912541b0 KS |
708 | argv[0] = program; |
709 | } | |
710 | ||
711 | pid = fork(); | |
712 | switch(pid) { | |
713 | case 0: | |
714 | /* child closes parent's ends of pipes */ | |
715 | if (outpipe[READ_END] >= 0) { | |
716 | close(outpipe[READ_END]); | |
717 | outpipe[READ_END] = -1; | |
718 | } | |
719 | if (errpipe[READ_END] >= 0) { | |
720 | close(errpipe[READ_END]); | |
721 | errpipe[READ_END] = -1; | |
722 | } | |
723 | ||
9f6445e3 | 724 | log_debug("starting '%s'", cmd); |
912541b0 | 725 | |
b49d9b50 KS |
726 | spawn_exec(event, cmd, argv, envp, sigmask, |
727 | outpipe[WRITE_END], errpipe[WRITE_END]); | |
912541b0 KS |
728 | |
729 | _exit(2 ); | |
730 | case -1: | |
56f64d95 | 731 | log_error_errno(errno, "fork of '%s' failed: %m", cmd); |
912541b0 KS |
732 | err = -1; |
733 | goto out; | |
734 | default: | |
735 | /* parent closed child's ends of pipes */ | |
736 | if (outpipe[WRITE_END] >= 0) { | |
737 | close(outpipe[WRITE_END]); | |
738 | outpipe[WRITE_END] = -1; | |
739 | } | |
740 | if (errpipe[WRITE_END] >= 0) { | |
741 | close(errpipe[WRITE_END]); | |
742 | errpipe[WRITE_END] = -1; | |
743 | } | |
744 | ||
dd5eddd2 KS |
745 | spawn_read(event, |
746 | timeout_usec, | |
747 | cmd, | |
748 | outpipe[READ_END], errpipe[READ_END], | |
749 | result, ressize); | |
912541b0 | 750 | |
67117413 | 751 | err = spawn_wait(event, timeout_usec, timeout_warn_usec, cmd, pid); |
912541b0 | 752 | } |
2181d30a KS |
753 | |
754 | out: | |
912541b0 KS |
755 | if (outpipe[READ_END] >= 0) |
756 | close(outpipe[READ_END]); | |
757 | if (outpipe[WRITE_END] >= 0) | |
758 | close(outpipe[WRITE_END]); | |
759 | if (errpipe[READ_END] >= 0) | |
760 | close(errpipe[READ_END]); | |
761 | if (errpipe[WRITE_END] >= 0) | |
762 | close(errpipe[WRITE_END]); | |
763 | return err; | |
2181d30a KS |
764 | } |
765 | ||
9ec6e95b | 766 | static int rename_netif(struct udev_event *event) { |
912541b0 | 767 | struct udev_device *dev = event->dev; |
16d26d55 TG |
768 | char name[IFNAMSIZ]; |
769 | const char *oldname; | |
770 | int r; | |
771 | ||
772 | oldname = udev_device_get_sysname(dev); | |
912541b0 | 773 | |
16d26d55 | 774 | strscpy(name, IFNAMSIZ, event->name); |
912541b0 | 775 | |
4c83d994 | 776 | r = rtnl_set_link_name(&event->rtnl, udev_device_get_ifindex(dev), name); |
f647962d MS |
777 | if (r < 0) |
778 | return log_error_errno(r, "Error changing net interface name '%s' to '%s': %m", oldname, name); | |
16d26d55 | 779 | |
ff49bc32 | 780 | log_debug("renamed network interface '%s' to '%s'", oldname, name); |
16d26d55 | 781 | |
4c83d994 | 782 | return 0; |
d46f37fd KS |
783 | } |
784 | ||
dd5eddd2 | 785 | void udev_event_execute_rules(struct udev_event *event, |
adeba500 KS |
786 | usec_t timeout_usec, usec_t timeout_warn_usec, |
787 | struct udev_list *properties_list, | |
788 | struct udev_rules *rules, | |
789 | const sigset_t *sigmask) { | |
912541b0 | 790 | struct udev_device *dev = event->dev; |
912541b0 KS |
791 | |
792 | if (udev_device_get_subsystem(dev) == NULL) | |
1ea97217 | 793 | return; |
912541b0 | 794 | |
090be865 | 795 | if (streq(udev_device_get_action(dev), "remove")) { |
912541b0 KS |
796 | udev_device_read_db(dev, NULL); |
797 | udev_device_delete_db(dev); | |
798 | udev_device_tag_index(dev, NULL, false); | |
799 | ||
800 | if (major(udev_device_get_devnum(dev)) != 0) | |
801 | udev_watch_end(event->udev, dev); | |
802 | ||
adeba500 KS |
803 | udev_rules_apply_to_event(rules, event, |
804 | timeout_usec, timeout_warn_usec, | |
805 | properties_list, | |
806 | sigmask); | |
912541b0 KS |
807 | |
808 | if (major(udev_device_get_devnum(dev)) != 0) | |
e7f32890 | 809 | udev_node_remove(dev); |
912541b0 | 810 | } else { |
fa639f3a | 811 | event->dev_db = udev_device_shallow_clone(dev); |
912541b0 KS |
812 | if (event->dev_db != NULL) { |
813 | udev_device_read_db(event->dev_db, NULL); | |
814 | udev_device_set_info_loaded(event->dev_db); | |
815 | ||
816 | /* disable watch during event processing */ | |
817 | if (major(udev_device_get_devnum(dev)) != 0) | |
818 | udev_watch_end(event->udev, event->dev_db); | |
819 | } | |
820 | ||
b081b27e TG |
821 | if (major(udev_device_get_devnum(dev)) == 0 && |
822 | streq(udev_device_get_action(dev), "move")) { | |
823 | struct udev_list_entry *entry; | |
824 | ||
825 | for ((entry = udev_device_get_properties_list_entry(event->dev_db)); entry; entry = udev_list_entry_get_next(entry)) { | |
826 | const char *key, *value; | |
827 | struct udev_list_entry *property; | |
828 | ||
829 | key = udev_list_entry_get_name(entry); | |
830 | value = udev_list_entry_get_value(entry); | |
831 | ||
04ef387e | 832 | property = udev_device_add_property(dev, key, value); |
b081b27e TG |
833 | udev_list_entry_set_num(property, true); |
834 | } | |
835 | } | |
836 | ||
adeba500 KS |
837 | udev_rules_apply_to_event(rules, event, |
838 | timeout_usec, timeout_warn_usec, | |
839 | properties_list, | |
840 | sigmask); | |
912541b0 KS |
841 | |
842 | /* rename a new network interface, if needed */ | |
090be865 TA |
843 | if (udev_device_get_ifindex(dev) > 0 && streq(udev_device_get_action(dev), "add") && |
844 | event->name != NULL && !streq(event->name, udev_device_get_sysname(dev))) { | |
1ea97217 | 845 | int r; |
912541b0 | 846 | |
1ea97217 | 847 | r = rename_netif(event); |
243d1825 TG |
848 | if (r < 0) |
849 | log_warning_errno(r, "could not rename interface '%d' from '%s' to '%s': %m", udev_device_get_ifindex(dev), | |
850 | udev_device_get_sysname(dev), event->name); | |
851 | else { | |
852 | const char *interface_old; | |
912541b0 | 853 | |
243d1825 TG |
854 | /* remember old name */ |
855 | interface_old = udev_device_get_sysname(dev); | |
856 | ||
857 | r = udev_device_rename(dev, event->name); | |
858 | if (r < 0) | |
859 | log_warning_errno(r, "renamed interface '%d' from '%s' to '%s', but could not update udev_device: %m", | |
860 | udev_device_get_ifindex(dev), udev_device_get_sysname(dev), event->name); | |
861 | else { | |
862 | udev_device_add_property(dev, "INTERFACE_OLD", interface_old); | |
863 | udev_device_add_property(dev, "INTERFACE", event->name); | |
9f6445e3 | 864 | log_debug("changed devpath to '%s'", udev_device_get_devpath(dev)); |
912541b0 KS |
865 | } |
866 | } | |
867 | } | |
868 | ||
b0a00806 | 869 | if (major(udev_device_get_devnum(dev)) > 0) { |
9a8ae49d KS |
870 | bool apply; |
871 | ||
912541b0 KS |
872 | /* remove/update possible left-over symlinks from old database entry */ |
873 | if (event->dev_db != NULL) | |
874 | udev_node_update_old_links(dev, event->dev_db); | |
875 | ||
1edefa4f KS |
876 | if (!event->owner_set) |
877 | event->uid = udev_device_get_devnode_uid(dev); | |
878 | ||
879 | if (!event->group_set) | |
880 | event->gid = udev_device_get_devnode_gid(dev); | |
881 | ||
912541b0 KS |
882 | if (!event->mode_set) { |
883 | if (udev_device_get_devnode_mode(dev) > 0) { | |
884 | /* kernel supplied value */ | |
885 | event->mode = udev_device_get_devnode_mode(dev); | |
886 | } else if (event->gid > 0) { | |
887 | /* default 0660 if a group is assigned */ | |
888 | event->mode = 0660; | |
889 | } else { | |
890 | /* default 0600 */ | |
891 | event->mode = 0600; | |
892 | } | |
893 | } | |
894 | ||
9a8ae49d | 895 | apply = streq(udev_device_get_action(dev), "add") || event->owner_set || event->group_set || event->mode_set; |
c26547d6 | 896 | udev_node_add(dev, apply, event->mode, event->uid, event->gid, &event->seclabel_list); |
912541b0 KS |
897 | } |
898 | ||
899 | /* preserve old, or get new initialization timestamp */ | |
900 | if (event->dev_db != NULL && udev_device_get_usec_initialized(event->dev_db) > 0) | |
901 | udev_device_set_usec_initialized(event->dev, udev_device_get_usec_initialized(event->dev_db)); | |
902 | else if (udev_device_get_usec_initialized(event->dev) == 0) | |
40fe8b11 | 903 | udev_device_set_usec_initialized(event->dev, now(CLOCK_MONOTONIC)); |
912541b0 KS |
904 | |
905 | /* (re)write database file */ | |
906 | udev_device_update_db(dev); | |
907 | udev_device_tag_index(dev, event->dev_db, true); | |
908 | udev_device_set_is_initialized(dev); | |
909 | ||
910 | udev_device_unref(event->dev_db); | |
911 | event->dev_db = NULL; | |
912 | } | |
d46f37fd | 913 | } |
2d73813e | 914 | |
67117413 | 915 | void udev_event_execute_run(struct udev_event *event, usec_t timeout_usec, usec_t timeout_warn_usec, const sigset_t *sigmask) { |
912541b0 | 916 | struct udev_list_entry *list_entry; |
912541b0 | 917 | |
912541b0 KS |
918 | udev_list_entry_foreach(list_entry, udev_list_get_entry(&event->run_list)) { |
919 | const char *cmd = udev_list_entry_get_name(list_entry); | |
83cd6b75 KS |
920 | enum udev_builtin_cmd builtin_cmd = udev_list_entry_get_num(list_entry); |
921 | ||
922 | if (builtin_cmd < UDEV_BUILTIN_MAX) { | |
923 | char command[UTIL_PATH_SIZE]; | |
912541b0 | 924 | |
83cd6b75 KS |
925 | udev_event_apply_format(event, cmd, command, sizeof(command)); |
926 | udev_builtin_run(event->dev, builtin_cmd, command, false); | |
912541b0 KS |
927 | } else { |
928 | char program[UTIL_PATH_SIZE]; | |
929 | char **envp; | |
930 | ||
931 | if (event->exec_delay > 0) { | |
9f6445e3 | 932 | log_debug("delay execution of '%s'", program); |
912541b0 KS |
933 | sleep(event->exec_delay); |
934 | } | |
935 | ||
936 | udev_event_apply_format(event, cmd, program, sizeof(program)); | |
937 | envp = udev_device_get_properties_envp(event->dev); | |
67117413 | 938 | udev_event_spawn(event, timeout_usec, timeout_warn_usec, program, envp, sigmask, NULL, 0); |
912541b0 KS |
939 | } |
940 | } | |
2d73813e | 941 | } |