~ubuntu-branches/ubuntu/trusty/systemd/trusty

« back to all changes in this revision

Viewing changes to .pc/debian-changes/src/udev/udev-event.c

  • Committer: Package Import Robot
  • Author(s): Michael Biebl, Michael Biebl, Michael Stapelberg, Daniel Schaal, Ondrej Balaz
  • Date: 2013-09-12 00:13:11 UTC
  • mfrom: (1.1.11) (9.1.2 experimental)
  • mto: This revision was merged to the branch mainline in revision 53.
  • Revision ID: package-import@ubuntu.com-20130912001311-dz35it34wr2lbday
Tags: 204-3
[ Michael Biebl ]
* Upload to unstable.
* Use /bin/bash in debug-shell.service as Debian doesn't have /sbin/sushell.
* Only import net.ifaces cmdline property for network devices.
* Generate strict dependencies between the binary packages using a
  shlibs.local file and add an explicit versioned dependency on
  libsystemd-login0 to systemd to ensure packages are upgraded in sync.
  Closes: #719444
* Drop obsolete Replaces: libudev0 from udev package.
* Use correct paths for various binaries, like /sbin/quotaon, which are
  installed in / and not /usr in Debian.  Closes: #721347
* Don't install kernel-install(8) man page since we don't install the
  corresponding binary either.  Closes: #722180
* Cherry-pick upstream fixes to make switching runlevels and starting
  reboot via ctrl-alt-del more robust.
* Cherry-pick upstream fix to properly apply ACLs to Journal files.

[ Michael Stapelberg ]
* Make systemctl enable|disable call update-rc.d for SysV init scripts.
  Closes: #709780
* Don't mount /tmp as tmpfs by default and make it possible to enable this
  feature via "systemctl enable tmp.mount".

[ Daniel Schaal ]
* Add bug-script to systemd and udev.  Closes: #711245

[ Ondrej Balaz ]
* Recognize discard option in /etc/crypttab.

Show diffs side-by-side

added added

removed removed

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