udev-event.c revision 9f6445e34a57c270f013c9416c123e56261553dd
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering/*
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering * Copyright (C) 2003-2013 Kay Sievers <kay@vrfy.org>
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering *
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering * This program is free software: you can redistribute it and/or modify
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering * it under the terms of the GNU General Public License as published by
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering * the Free Software Foundation, either version 2 of the License, or
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering * (at your option) any later version.
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering *
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering * This program is distributed in the hope that it will be useful,
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering * but WITHOUT ANY WARRANTY; without even the implied warranty of
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering * GNU General Public License for more details.
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering *
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering * You should have received a copy of the GNU General Public License
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering * along with this program. If not, see <http://www.gnu.org/licenses/>.
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering */
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering#include <stdlib.h>
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering#include <stdio.h>
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering#include <stddef.h>
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering#include <unistd.h>
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering#include <fcntl.h>
4871690d9e32608bbd9b18505b5326c2079c9690Allin Cottrell#include <errno.h>
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering#include <ctype.h>
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering#include <string.h>
178cc7700c23ac088cd7190d7854282075028d91Lennart Poettering#include <time.h>
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering#include <net/if.h>
d682b3a7e7c7c2941a4d3e193f1e330dbc9fae89Lennart Poettering#include <sys/prctl.h>
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek#include <sys/poll.h>
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering#include <sys/epoll.h>
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering#include <sys/wait.h>
3b7124a8db56ed57525b9ecfd19cfdc8c9facba0Lennart Poettering#include <sys/signalfd.h>
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering#include "udev.h"
178cc7700c23ac088cd7190d7854282075028d91Lennart Poettering#include "rtnl-util.h"
178cc7700c23ac088cd7190d7854282075028d91Lennart Poettering
178cc7700c23ac088cd7190d7854282075028d91Lennart Poetteringstruct udev_event *udev_event_new(struct udev_device *dev)
3b3154df7e2773332bb814e167187367a0ccae4aLennart Poettering{
b92bea5d2a9481de69bb627a7b442a9f58fca43dZbigniew Jędrzejewski-Szmek struct udev *udev = udev_device_get_udev(dev);
46b131574fdd7d77c15a0919ca9010cad7aa6ac7Lennart Poettering struct udev_event *event;
b92bea5d2a9481de69bb627a7b442a9f58fca43dZbigniew Jędrzejewski-Szmek
b92bea5d2a9481de69bb627a7b442a9f58fca43dZbigniew Jędrzejewski-Szmek event = calloc(1, sizeof(struct udev_event));
b92bea5d2a9481de69bb627a7b442a9f58fca43dZbigniew Jędrzejewski-Szmek if (event == NULL)
b92bea5d2a9481de69bb627a7b442a9f58fca43dZbigniew Jędrzejewski-Szmek return NULL;
b92bea5d2a9481de69bb627a7b442a9f58fca43dZbigniew Jędrzejewski-Szmek event->dev = dev;
b92bea5d2a9481de69bb627a7b442a9f58fca43dZbigniew Jędrzejewski-Szmek event->udev = udev;
46b131574fdd7d77c15a0919ca9010cad7aa6ac7Lennart Poettering udev_list_init(udev, &event->run_list, false);
b92bea5d2a9481de69bb627a7b442a9f58fca43dZbigniew Jędrzejewski-Szmek udev_list_init(udev, &event->seclabel_list, false);
f8294e4175918117ca6c131720bcf287eadcd029Josh Triplett event->fd_signal = -1;
b92bea5d2a9481de69bb627a7b442a9f58fca43dZbigniew Jędrzejewski-Szmek event->birth_usec = now(CLOCK_MONOTONIC);
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering event->timeout_usec = 30 * 1000 * 1000;
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering return event;
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering}
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poetteringvoid udev_event_unref(struct udev_event *event)
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering{
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering if (event == NULL)
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering return;
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering udev_list_cleanup(&event->run_list);
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering udev_list_cleanup(&event->seclabel_list);
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering free(event->program_result);
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering free(event->name);
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering free(event);
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering}
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poetteringsize_t udev_event_apply_format(struct udev_event *event, const char *src, char *dest, size_t size)
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering{
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering struct udev_device *dev = event->dev;
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering enum subst_type {
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering SUBST_UNKNOWN,
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering SUBST_DEVNODE,
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering SUBST_ATTR,
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering SUBST_ENV,
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering SUBST_KERNEL,
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering SUBST_KERNEL_NUMBER,
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering SUBST_DRIVER,
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering SUBST_DEVPATH,
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering SUBST_ID,
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering SUBST_MAJOR,
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering SUBST_MINOR,
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering SUBST_RESULT,
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering SUBST_PARENT,
178cc7700c23ac088cd7190d7854282075028d91Lennart Poettering SUBST_NAME,
178cc7700c23ac088cd7190d7854282075028d91Lennart Poettering SUBST_LINKS,
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering SUBST_ROOT,
178cc7700c23ac088cd7190d7854282075028d91Lennart Poettering SUBST_SYS,
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering };
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering static const struct subst_map {
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering const char *name;
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering const char fmt;
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering enum subst_type type;
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering } map[] = {
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering { .name = "devnode", .fmt = 'N', .type = SUBST_DEVNODE },
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering { .name = "tempnode", .fmt = 'N', .type = SUBST_DEVNODE },
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering { .name = "attr", .fmt = 's', .type = SUBST_ATTR },
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering { .name = "sysfs", .fmt = 's', .type = SUBST_ATTR },
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering { .name = "env", .fmt = 'E', .type = SUBST_ENV },
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering { .name = "kernel", .fmt = 'k', .type = SUBST_KERNEL },
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering { .name = "number", .fmt = 'n', .type = SUBST_KERNEL_NUMBER },
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering { .name = "driver", .fmt = 'd', .type = SUBST_DRIVER },
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering { .name = "devpath", .fmt = 'p', .type = SUBST_DEVPATH },
178cc7700c23ac088cd7190d7854282075028d91Lennart Poettering { .name = "id", .fmt = 'b', .type = SUBST_ID },
178cc7700c23ac088cd7190d7854282075028d91Lennart Poettering { .name = "major", .fmt = 'M', .type = SUBST_MAJOR },
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering { .name = "minor", .fmt = 'm', .type = SUBST_MINOR },
178cc7700c23ac088cd7190d7854282075028d91Lennart Poettering { .name = "result", .fmt = 'c', .type = SUBST_RESULT },
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering { .name = "parent", .fmt = 'P', .type = SUBST_PARENT },
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering { .name = "name", .fmt = 'D', .type = SUBST_NAME },
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering { .name = "links", .fmt = 'L', .type = SUBST_LINKS },
56f64d95763a799ba4475daf44d8e9f72a1bd474Michal Schmidt { .name = "root", .fmt = 'r', .type = SUBST_ROOT },
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering { .name = "sys", .fmt = 'S', .type = SUBST_SYS },
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering };
3b3154df7e2773332bb814e167187367a0ccae4aLennart Poettering const char *from;
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering char *s;
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering size_t l;
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering from = src;
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering s = dest;
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering l = size;
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering for (;;) {
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering enum subst_type type = SUBST_UNKNOWN;
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering char attrbuf[UTIL_PATH_SIZE];
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering char *attr = NULL;
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering
3b3154df7e2773332bb814e167187367a0ccae4aLennart Poettering while (from[0] != '\0') {
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering if (from[0] == '$') {
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering /* substitute named variable */
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering unsigned int i;
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering if (from[1] == '$') {
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering from++;
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering goto copy;
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering }
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering for (i = 0; i < ELEMENTSOF(map); i++) {
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering if (startswith(&from[1], map[i].name)) {
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering type = map[i].type;
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering from += strlen(map[i].name)+1;
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering goto subst;
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering }
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering }
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering } else if (from[0] == '%') {
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering /* substitute format char */
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering unsigned int i;
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering if (from[1] == '%') {
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering from++;
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering goto copy;
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering }
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering for (i = 0; i < ELEMENTSOF(map); i++) {
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering if (from[1] == map[i].fmt) {
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering type = map[i].type;
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering from += 2;
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering goto subst;
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering }
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering }
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering }
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poetteringcopy:
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering /* copy char */
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering if (l == 0)
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek goto out;
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering s[0] = from[0];
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering from++;
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering s++;
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering l--;
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering }
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering goto out;
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poetteringsubst:
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering /* extract possible $format{attr} */
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering if (from[0] == '{') {
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering unsigned int i;
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering from++;
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering for (i = 0; from[i] != '}'; i++) {
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering if (from[i] == '\0') {
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering log_error("missing closing brace for format '%s'", src);
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering goto out;
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering }
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering }
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering if (i >= sizeof(attrbuf))
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering goto out;
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering memcpy(attrbuf, from, i);
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering attrbuf[i] = '\0';
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering from += i+1;
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering attr = attrbuf;
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering } else {
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering attr = NULL;
e88baee88fad8bc59d33b55a7a2d640ef9e16cd6Zbigniew Jędrzejewski-Szmek }
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering switch (type) {
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering case SUBST_DEVPATH:
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering l = strpcpy(&s, l, udev_device_get_devpath(dev));
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering break;
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering case SUBST_KERNEL:
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering l = strpcpy(&s, l, udev_device_get_sysname(dev));
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering break;
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering case SUBST_KERNEL_NUMBER:
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering if (udev_device_get_sysnum(dev) == NULL)
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering break;
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering l = strpcpy(&s, l, udev_device_get_sysnum(dev));
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering break;
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering case SUBST_ID:
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering if (event->dev_parent == NULL)
e88baee88fad8bc59d33b55a7a2d640ef9e16cd6Zbigniew Jędrzejewski-Szmek break;
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering l = strpcpy(&s, l, udev_device_get_sysname(event->dev_parent));
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering break;
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering case SUBST_DRIVER: {
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering const char *driver;
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering if (event->dev_parent == NULL)
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering break;
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering driver = udev_device_get_driver(event->dev_parent);
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering if (driver == NULL)
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering break;
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering l = strpcpy(&s, l, driver);
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering break;
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering }
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering case SUBST_MAJOR: {
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering char num[UTIL_PATH_SIZE];
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering sprintf(num, "%d", major(udev_device_get_devnum(dev)));
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering l = strpcpy(&s, l, num);
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering break;
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering }
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering case SUBST_MINOR: {
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering char num[UTIL_PATH_SIZE];
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering sprintf(num, "%d", minor(udev_device_get_devnum(dev)));
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering l = strpcpy(&s, l, num);
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering break;
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering }
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering case SUBST_RESULT: {
e88baee88fad8bc59d33b55a7a2d640ef9e16cd6Zbigniew Jędrzejewski-Szmek char *rest;
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering int i;
e88baee88fad8bc59d33b55a7a2d640ef9e16cd6Zbigniew Jędrzejewski-Szmek
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering if (event->program_result == NULL)
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering break;
e3bfb7be07d9b1f4ebb12eb22c4c8bcd2a988d51Zbigniew Jędrzejewski-Szmek /* get part part of the result string */
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering i = 0;
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering if (attr != NULL)
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering i = strtoul(attr, &rest, 10);
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering if (i > 0) {
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering char result[UTIL_PATH_SIZE];
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering char tmp[UTIL_PATH_SIZE];
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering char *cpos;
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering strscpy(result, sizeof(result), event->program_result);
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering cpos = result;
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering while (--i) {
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering while (cpos[0] != '\0' && !isspace(cpos[0]))
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering cpos++;
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering while (isspace(cpos[0]))
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering cpos++;
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering }
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering if (i > 0) {
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering log_error("requested part of result string not found");
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering break;
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering }
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering strscpy(tmp, sizeof(tmp), cpos);
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering /* %{2+}c copies the whole string from the second part on */
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering if (rest[0] != '+') {
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering cpos = strchr(tmp, ' ');
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering if (cpos)
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering cpos[0] = '\0';
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering }
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering l = strpcpy(&s, l, tmp);
ac50788b0f5aeee09e7d45db28ae8ab7f39cd52eZbigniew Jędrzejewski-Szmek } else {
ac50788b0f5aeee09e7d45db28ae8ab7f39cd52eZbigniew Jędrzejewski-Szmek l = strpcpy(&s, l, event->program_result);
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering }
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering break;
ac50788b0f5aeee09e7d45db28ae8ab7f39cd52eZbigniew Jędrzejewski-Szmek }
ac50788b0f5aeee09e7d45db28ae8ab7f39cd52eZbigniew Jędrzejewski-Szmek case SUBST_ATTR: {
ac50788b0f5aeee09e7d45db28ae8ab7f39cd52eZbigniew Jędrzejewski-Szmek const char *value = NULL;
ac50788b0f5aeee09e7d45db28ae8ab7f39cd52eZbigniew Jędrzejewski-Szmek char vbuf[UTIL_NAME_SIZE];
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering size_t len;
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering int count;
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering if (attr == NULL) {
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering log_error("missing file parameter for attr");
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering break;
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering }
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering /* try to read the value specified by "[dmi/id]product_name" */
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering if (util_resolve_subsys_kernel(event->udev, attr, vbuf, sizeof(vbuf), 1) == 0)
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering value = vbuf;
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering /* try to read the attribute the device */
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering if (value == NULL)
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering value = udev_device_get_sysattr_value(event->dev, attr);
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering /* try to read the attribute of the parent device, other matches have selected */
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering if (value == NULL && event->dev_parent != NULL && event->dev_parent != event->dev)
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering value = udev_device_get_sysattr_value(event->dev_parent, attr);
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering if (value == NULL)
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering break;
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering /* strip trailing whitespace, and replace unwanted characters */
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering if (value != vbuf)
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering strscpy(vbuf, sizeof(vbuf), value);
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering len = strlen(vbuf);
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering while (len > 0 && isspace(vbuf[--len]))
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering vbuf[len] = '\0';
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering count = util_replace_chars(vbuf, UDEV_ALLOWED_CHARS_INPUT);
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering if (count > 0)
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering log_debug("%i character(s) replaced" , count);
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering l = strpcpy(&s, l, vbuf);
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering break;
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering }
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering case SUBST_PARENT: {
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering struct udev_device *dev_parent;
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering const char *devnode;
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering dev_parent = udev_device_get_parent(event->dev);
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering if (dev_parent == NULL)
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering break;
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering devnode = udev_device_get_devnode(dev_parent);
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering if (devnode != NULL)
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering l = strpcpy(&s, l, devnode + strlen("/dev/"));
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering break;
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering }
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering case SUBST_DEVNODE:
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering if (udev_device_get_devnode(dev) != NULL)
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering l = strpcpy(&s, l, udev_device_get_devnode(dev));
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering break;
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering case SUBST_NAME:
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering if (event->name != NULL)
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering l = strpcpy(&s, l, event->name);
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering else if (udev_device_get_devnode(dev) != NULL)
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering l = strpcpy(&s, l, udev_device_get_devnode(dev) + strlen("/dev/"));
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering else
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering l = strpcpy(&s, l, udev_device_get_sysname(dev));
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering break;
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering case SUBST_LINKS: {
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering struct udev_list_entry *list_entry;
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering list_entry = udev_device_get_devlinks_list_entry(dev);
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering if (list_entry == NULL)
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering break;
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering l = strpcpy(&s, l, udev_list_entry_get_name(list_entry) + strlen("/dev/"));
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering udev_list_entry_foreach(list_entry, udev_list_entry_get_next(list_entry))
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering l = strpcpyl(&s, l, " ", udev_list_entry_get_name(list_entry) + strlen("/dev/"), NULL);
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering break;
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering }
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering case SUBST_ROOT:
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering l = strpcpy(&s, l, "/dev");
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering break;
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering case SUBST_SYS:
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering l = strpcpy(&s, l, "/sys");
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering break;
3b3154df7e2773332bb814e167187367a0ccae4aLennart Poettering case SUBST_ENV:
3b3154df7e2773332bb814e167187367a0ccae4aLennart Poettering if (attr == NULL) {
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering break;
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering } else {
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering const char *value;
8457f8d6ac7adc6c6ef31378e6e7761cce522141Lennart Poettering
8457f8d6ac7adc6c6ef31378e6e7761cce522141Lennart Poettering value = udev_device_get_property_value(event->dev, attr);
8457f8d6ac7adc6c6ef31378e6e7761cce522141Lennart Poettering if (value == NULL)
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering break;
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering l = strpcpy(&s, l, value);
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering break;
8457f8d6ac7adc6c6ef31378e6e7761cce522141Lennart Poettering }
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering default:
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering log_error("unknown substitution type=%i", type);
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering break;
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering }
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering }
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering
e3bfb7be07d9b1f4ebb12eb22c4c8bcd2a988d51Zbigniew Jędrzejewski-Szmekout:
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering s[0] = '\0';
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering return l;
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering}
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poetteringstatic int spawn_exec(struct udev_event *event,
5809560d858f45351856d6fe786a8117306dd0f2Lennart Poettering const char *cmd, char *const argv[], char **envp, const sigset_t *sigmask,
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering int fd_stdout, int fd_stderr)
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering{
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering int err;
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering int fd;
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering /* discard child output or connect to pipe */
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering fd = open("/dev/null", O_RDWR);
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen if (fd >= 0) {
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen dup2(fd, STDIN_FILENO);
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen if (fd_stdout < 0)
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering dup2(fd, STDOUT_FILENO);
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering if (fd_stderr < 0)
8457f8d6ac7adc6c6ef31378e6e7761cce522141Lennart Poettering dup2(fd, STDERR_FILENO);
8457f8d6ac7adc6c6ef31378e6e7761cce522141Lennart Poettering close(fd);
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering } else {
8457f8d6ac7adc6c6ef31378e6e7761cce522141Lennart Poettering log_error("open /dev/null failed: %m");
8457f8d6ac7adc6c6ef31378e6e7761cce522141Lennart Poettering }
8457f8d6ac7adc6c6ef31378e6e7761cce522141Lennart Poettering
8457f8d6ac7adc6c6ef31378e6e7761cce522141Lennart Poettering /* connect pipes to std{out,err} */
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering if (fd_stdout >= 0) {
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering dup2(fd_stdout, STDOUT_FILENO);
8457f8d6ac7adc6c6ef31378e6e7761cce522141Lennart Poettering close(fd_stdout);
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering }
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering if (fd_stderr >= 0) {
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering dup2(fd_stderr, STDERR_FILENO);
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering close(fd_stderr);
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering }
8457f8d6ac7adc6c6ef31378e6e7761cce522141Lennart Poettering
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering /* terminate child in case parent goes away */
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering prctl(PR_SET_PDEATHSIG, SIGTERM);
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering /* restore original udev sigmask before exec */
8457f8d6ac7adc6c6ef31378e6e7761cce522141Lennart Poettering if (sigmask)
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering sigprocmask(SIG_SETMASK, sigmask, NULL);
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering execve(argv[0], argv, envp);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering /* exec failed */
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering err = -errno;
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering log_error("failed to execute '%s' '%s': %m", argv[0], cmd);
3b3154df7e2773332bb814e167187367a0ccae4aLennart Poettering return err;
3b3154df7e2773332bb814e167187367a0ccae4aLennart Poettering}
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poetteringstatic void spawn_read(struct udev_event *event,
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering const char *cmd,
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering int fd_stdout, int fd_stderr,
46b131574fdd7d77c15a0919ca9010cad7aa6ac7Lennart Poettering char *result, size_t ressize)
b92bea5d2a9481de69bb627a7b442a9f58fca43dZbigniew Jędrzejewski-Szmek{
03ee5c38cb0da193dd08733fb4c0c2809cee6a99Lennart Poettering size_t respos = 0;
b92bea5d2a9481de69bb627a7b442a9f58fca43dZbigniew Jędrzejewski-Szmek int fd_ep = -1;
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering struct epoll_event ep_outpipe, ep_errpipe;
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering
4a62c710b62a5a3c7a8a278b810b9d5b5a0c8f4fMichal Schmidt /* read from child if requested */
4a62c710b62a5a3c7a8a278b810b9d5b5a0c8f4fMichal Schmidt if (fd_stdout < 0 && fd_stderr < 0)
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering return;
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering fd_ep = epoll_create1(EPOLL_CLOEXEC);
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering if (fd_ep < 0) {
4a62c710b62a5a3c7a8a278b810b9d5b5a0c8f4fMichal Schmidt log_error("error creating epoll fd: %m");
4a62c710b62a5a3c7a8a278b810b9d5b5a0c8f4fMichal Schmidt goto out;
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering }
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering if (fd_stdout >= 0) {
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering memset(&ep_outpipe, 0, sizeof(struct epoll_event));
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering ep_outpipe.events = EPOLLIN;
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering ep_outpipe.data.ptr = &fd_stdout;
4a62c710b62a5a3c7a8a278b810b9d5b5a0c8f4fMichal Schmidt if (epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_stdout, &ep_outpipe) < 0) {
4a62c710b62a5a3c7a8a278b810b9d5b5a0c8f4fMichal Schmidt log_error("fail to add fd to epoll: %m");
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering goto out;
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering }
6baa7db00812437bbc87e73faa1a11b6cf631958Lennart Poettering }
d682b3a7e7c7c2941a4d3e193f1e330dbc9fae89Lennart Poettering
d682b3a7e7c7c2941a4d3e193f1e330dbc9fae89Lennart Poettering if (fd_stderr >= 0) {
56f64d95763a799ba4475daf44d8e9f72a1bd474Michal Schmidt memset(&ep_errpipe, 0, sizeof(struct epoll_event));
d682b3a7e7c7c2941a4d3e193f1e330dbc9fae89Lennart Poettering ep_errpipe.events = EPOLLIN;
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering ep_errpipe.data.ptr = &fd_stderr;
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering if (epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_stderr, &ep_errpipe) < 0) {
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering log_error("fail to add fd to epoll: %m");
4a62c710b62a5a3c7a8a278b810b9d5b5a0c8f4fMichal Schmidt goto out;
4a62c710b62a5a3c7a8a278b810b9d5b5a0c8f4fMichal Schmidt }
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering }
151b9b9662a90455262ce575a8a8ae74bf4ff336Lennart Poettering
23bbb0de4e3f85d9704a5c12a5afa2dfa0159e41Michal Schmidt /* read child output */
23bbb0de4e3f85d9704a5c12a5afa2dfa0159e41Michal Schmidt while (fd_stdout >= 0 || fd_stderr >= 0) {
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering int timeout;
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering int fdcount;
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering struct epoll_event ev[4];
178cc7700c23ac088cd7190d7854282075028d91Lennart Poettering int i;
178cc7700c23ac088cd7190d7854282075028d91Lennart Poettering
178cc7700c23ac088cd7190d7854282075028d91Lennart Poettering if (event->timeout_usec > 0) {
178cc7700c23ac088cd7190d7854282075028d91Lennart Poettering usec_t age_usec;
178cc7700c23ac088cd7190d7854282075028d91Lennart Poettering
178cc7700c23ac088cd7190d7854282075028d91Lennart Poettering age_usec = now(CLOCK_MONOTONIC) - event->birth_usec;
178cc7700c23ac088cd7190d7854282075028d91Lennart Poettering if (age_usec >= event->timeout_usec) {
178cc7700c23ac088cd7190d7854282075028d91Lennart Poettering log_error("timeout '%s'", cmd);
178cc7700c23ac088cd7190d7854282075028d91Lennart Poettering goto out;
178cc7700c23ac088cd7190d7854282075028d91Lennart Poettering }
178cc7700c23ac088cd7190d7854282075028d91Lennart Poettering timeout = ((event->timeout_usec - age_usec) / 1000) + 1000;
178cc7700c23ac088cd7190d7854282075028d91Lennart Poettering } else {
178cc7700c23ac088cd7190d7854282075028d91Lennart Poettering timeout = -1;
178cc7700c23ac088cd7190d7854282075028d91Lennart Poettering }
178cc7700c23ac088cd7190d7854282075028d91Lennart Poettering
178cc7700c23ac088cd7190d7854282075028d91Lennart Poettering fdcount = epoll_wait(fd_ep, ev, ELEMENTSOF(ev), timeout);
178cc7700c23ac088cd7190d7854282075028d91Lennart Poettering if (fdcount < 0) {
if (errno == EINTR)
continue;
log_error("failed to poll: %m");
goto out;
}
if (fdcount == 0) {
log_error("timeout '%s'", cmd);
goto out;
}
for (i = 0; i < fdcount; i++) {
int *fd = (int *)ev[i].data.ptr;
if (ev[i].events & EPOLLIN) {
ssize_t count;
char buf[4096];
count = read(*fd, buf, sizeof(buf)-1);
if (count <= 0)
continue;
buf[count] = '\0';
/* store stdout result */
if (result != NULL && *fd == fd_stdout) {
if (respos + count < ressize) {
memcpy(&result[respos], buf, count);
respos += count;
} else {
log_error("'%s' ressize %zd too short", cmd, ressize);
}
}
/* log debug output only if we watch stderr */
if (fd_stderr >= 0) {
char *pos;
char *line;
pos = buf;
while ((line = strsep(&pos, "\n"))) {
if (pos != NULL || line[0] != '\0')
log_debug("'%s'(%s) '%s'", cmd, *fd == fd_stdout ? "out" : "err" , line);
}
}
} else if (ev[i].events & EPOLLHUP) {
if (epoll_ctl(fd_ep, EPOLL_CTL_DEL, *fd, NULL) < 0) {
log_error("failed to remove fd from epoll: %m");
goto out;
}
*fd = -1;
}
}
}
/* return the child's stdout string */
if (result != NULL)
result[respos] = '\0';
out:
if (fd_ep >= 0)
close(fd_ep);
}
static int spawn_wait(struct udev_event *event, const char *cmd, pid_t pid)
{
struct pollfd pfd[1];
int err = 0;
pfd[0].events = POLLIN;
pfd[0].fd = event->fd_signal;
while (pid > 0) {
int timeout;
int fdcount;
if (event->timeout_usec > 0) {
usec_t age_usec;
age_usec = now(CLOCK_MONOTONIC) - event->birth_usec;
if (age_usec >= event->timeout_usec)
timeout = 1000;
else
timeout = ((event->timeout_usec - age_usec) / 1000) + 1000;
} else {
timeout = -1;
}
fdcount = poll(pfd, 1, timeout);
if (fdcount < 0) {
if (errno == EINTR)
continue;
err = -errno;
log_error("failed to poll: %m");
goto out;
}
if (fdcount == 0) {
log_error("timeout: killing '%s' [%u]", cmd, pid);
kill(pid, SIGKILL);
}
if (pfd[0].revents & POLLIN) {
struct signalfd_siginfo fdsi;
int status;
ssize_t size;
size = read(event->fd_signal, &fdsi, sizeof(struct signalfd_siginfo));
if (size != sizeof(struct signalfd_siginfo))
continue;
switch (fdsi.ssi_signo) {
case SIGTERM:
event->sigterm = true;
break;
case SIGCHLD:
if (waitpid(pid, &status, WNOHANG) < 0)
break;
if (WIFEXITED(status)) {
log_debug("'%s' [%u] exit with return code %i", cmd, pid, WEXITSTATUS(status));
if (WEXITSTATUS(status) != 0)
err = -1;
} else if (WIFSIGNALED(status)) {
log_error("'%s' [%u] terminated by signal %i (%s)", cmd, pid, WTERMSIG(status), strsignal(WTERMSIG(status)));
err = -1;
} else if (WIFSTOPPED(status)) {
log_error("'%s' [%u] stopped", cmd, pid);
err = -1;
} else if (WIFCONTINUED(status)) {
log_error("'%s' [%u] continued", cmd, pid);
err = -1;
} else {
log_error("'%s' [%u] exit with status 0x%04x", cmd, pid, status);
err = -1;
}
pid = 0;
break;
}
}
}
out:
return err;
}
int udev_build_argv(struct udev *udev, char *cmd, int *argc, char *argv[])
{
int i = 0;
char *pos;
if (strchr(cmd, ' ') == NULL) {
argv[i++] = cmd;
goto out;
}
pos = cmd;
while (pos != NULL && pos[0] != '\0') {
if (pos[0] == '\'') {
/* do not separate quotes */
pos++;
argv[i] = strsep(&pos, "\'");
if (pos != NULL)
while (pos[0] == ' ')
pos++;
} else {
argv[i] = strsep(&pos, " ");
if (pos != NULL)
while (pos[0] == ' ')
pos++;
}
i++;
}
out:
argv[i] = NULL;
if (argc)
*argc = i;
return 0;
}
int udev_event_spawn(struct udev_event *event,
const char *cmd, char **envp, const sigset_t *sigmask,
char *result, size_t ressize)
{
struct udev *udev = event->udev;
int outpipe[2] = {-1, -1};
int errpipe[2] = {-1, -1};
pid_t pid;
char arg[UTIL_PATH_SIZE];
char *argv[128];
char program[UTIL_PATH_SIZE];
int err = 0;
strscpy(arg, sizeof(arg), cmd);
udev_build_argv(event->udev, arg, NULL, argv);
/* pipes from child to parent */
if (result != NULL || udev_get_log_priority(udev) >= LOG_INFO) {
if (pipe2(outpipe, O_NONBLOCK) != 0) {
err = -errno;
log_error("pipe failed: %m");
goto out;
}
}
if (udev_get_log_priority(udev) >= LOG_INFO) {
if (pipe2(errpipe, O_NONBLOCK) != 0) {
err = -errno;
log_error("pipe failed: %m");
goto out;
}
}
/* allow programs in /usr/lib/udev/ to be called without the path */
if (argv[0][0] != '/') {
strscpyl(program, sizeof(program), UDEVLIBEXECDIR "/", argv[0], NULL);
argv[0] = program;
}
pid = fork();
switch(pid) {
case 0:
/* child closes parent's ends of pipes */
if (outpipe[READ_END] >= 0) {
close(outpipe[READ_END]);
outpipe[READ_END] = -1;
}
if (errpipe[READ_END] >= 0) {
close(errpipe[READ_END]);
errpipe[READ_END] = -1;
}
log_debug("starting '%s'", cmd);
spawn_exec(event, cmd, argv, envp, sigmask,
outpipe[WRITE_END], errpipe[WRITE_END]);
_exit(2 );
case -1:
log_error("fork of '%s' failed: %m", cmd);
err = -1;
goto out;
default:
/* parent closed child's ends of pipes */
if (outpipe[WRITE_END] >= 0) {
close(outpipe[WRITE_END]);
outpipe[WRITE_END] = -1;
}
if (errpipe[WRITE_END] >= 0) {
close(errpipe[WRITE_END]);
errpipe[WRITE_END] = -1;
}
spawn_read(event, cmd,
outpipe[READ_END], errpipe[READ_END],
result, ressize);
err = spawn_wait(event, cmd, pid);
}
out:
if (outpipe[READ_END] >= 0)
close(outpipe[READ_END]);
if (outpipe[WRITE_END] >= 0)
close(outpipe[WRITE_END]);
if (errpipe[READ_END] >= 0)
close(errpipe[READ_END]);
if (errpipe[WRITE_END] >= 0)
close(errpipe[WRITE_END]);
return err;
}
static int rename_netif(struct udev_event *event)
{
struct udev_device *dev = event->dev;
_cleanup_sd_rtnl_unref_ sd_rtnl *rtnl = NULL;
char name[IFNAMSIZ];
const char *oldname;
int r;
oldname = udev_device_get_sysname(dev);
log_debug("changing net interface name from '%s' to '%s'",
oldname, event->name);
strscpy(name, IFNAMSIZ, event->name);
r = sd_rtnl_open(0, &rtnl);
if (r < 0)
return r;
r = rtnl_set_link_name(rtnl, udev_device_get_ifindex(dev), name);
if (r < 0)
log_error("error changing net interface name %s to %s: %s",
oldname, name, strerror(-r));
else
print_kmsg("renamed network interface %s to %s", oldname, name);
return r;
}
int udev_event_execute_rules(struct udev_event *event, struct udev_rules *rules, const sigset_t *sigmask)
{
struct udev_device *dev = event->dev;
int err = 0;
if (udev_device_get_subsystem(dev) == NULL)
return -1;
if (streq(udev_device_get_action(dev), "remove")) {
udev_device_read_db(dev, NULL);
udev_device_delete_db(dev);
udev_device_tag_index(dev, NULL, false);
if (major(udev_device_get_devnum(dev)) != 0)
udev_watch_end(event->udev, dev);
udev_rules_apply_to_event(rules, event, sigmask);
if (major(udev_device_get_devnum(dev)) != 0)
udev_node_remove(dev);
} else {
event->dev_db = udev_device_new(event->udev);
if (event->dev_db != NULL) {
udev_device_set_syspath(event->dev_db, udev_device_get_syspath(dev));
udev_device_set_subsystem(event->dev_db, udev_device_get_subsystem(dev));
udev_device_read_db(event->dev_db, NULL);
udev_device_set_info_loaded(event->dev_db);
/* disable watch during event processing */
if (major(udev_device_get_devnum(dev)) != 0)
udev_watch_end(event->udev, event->dev_db);
}
udev_rules_apply_to_event(rules, event, sigmask);
/* rename a new network interface, if needed */
if (udev_device_get_ifindex(dev) > 0 && streq(udev_device_get_action(dev), "add") &&
event->name != NULL && !streq(event->name, udev_device_get_sysname(dev))) {
char syspath[UTIL_PATH_SIZE];
char *pos;
err = rename_netif(event);
if (err == 0) {
log_debug("renamed netif to '%s'", event->name);
/* remember old name */
udev_device_add_property(dev, "INTERFACE_OLD", udev_device_get_sysname(dev));
/* now change the devpath, because the kernel device name has changed */
strscpy(syspath, sizeof(syspath), udev_device_get_syspath(dev));
pos = strrchr(syspath, '/');
if (pos != NULL) {
pos++;
strscpy(pos, sizeof(syspath) - (pos - syspath), event->name);
udev_device_set_syspath(event->dev, syspath);
udev_device_add_property(dev, "INTERFACE", udev_device_get_sysname(dev));
log_debug("changed devpath to '%s'", udev_device_get_devpath(dev));
}
}
}
if (major(udev_device_get_devnum(dev)) > 0) {
bool apply;
/* remove/update possible left-over symlinks from old database entry */
if (event->dev_db != NULL)
udev_node_update_old_links(dev, event->dev_db);
if (!event->owner_set)
event->uid = udev_device_get_devnode_uid(dev);
if (!event->group_set)
event->gid = udev_device_get_devnode_gid(dev);
if (!event->mode_set) {
if (udev_device_get_devnode_mode(dev) > 0) {
/* kernel supplied value */
event->mode = udev_device_get_devnode_mode(dev);
} else if (event->gid > 0) {
/* default 0660 if a group is assigned */
event->mode = 0660;
} else {
/* default 0600 */
event->mode = 0600;
}
}
apply = streq(udev_device_get_action(dev), "add") || event->owner_set || event->group_set || event->mode_set;
udev_node_add(dev, apply, event->mode, event->uid, event->gid, &event->seclabel_list);
}
/* preserve old, or get new initialization timestamp */
if (event->dev_db != NULL && udev_device_get_usec_initialized(event->dev_db) > 0)
udev_device_set_usec_initialized(event->dev, udev_device_get_usec_initialized(event->dev_db));
else if (udev_device_get_usec_initialized(event->dev) == 0)
udev_device_set_usec_initialized(event->dev, now(CLOCK_MONOTONIC));
/* (re)write database file */
udev_device_update_db(dev);
udev_device_tag_index(dev, event->dev_db, true);
udev_device_set_is_initialized(dev);
udev_device_unref(event->dev_db);
event->dev_db = NULL;
}
return err;
}
void udev_event_execute_run(struct udev_event *event, const sigset_t *sigmask)
{
struct udev_list_entry *list_entry;
udev_list_entry_foreach(list_entry, udev_list_get_entry(&event->run_list)) {
const char *cmd = udev_list_entry_get_name(list_entry);
enum udev_builtin_cmd builtin_cmd = udev_list_entry_get_num(list_entry);
if (builtin_cmd < UDEV_BUILTIN_MAX) {
char command[UTIL_PATH_SIZE];
udev_event_apply_format(event, cmd, command, sizeof(command));
udev_builtin_run(event->dev, builtin_cmd, command, false);
} else {
char program[UTIL_PATH_SIZE];
char **envp;
if (event->exec_delay > 0) {
log_debug("delay execution of '%s'", program);
sleep(event->exec_delay);
}
udev_event_apply_format(event, cmd, program, sizeof(program));
envp = udev_device_get_properties_envp(event->dev);
udev_event_spawn(event, program, envp, sigmask, NULL, 0);
}
}
}