#include <fcntl.h>
#include <net/if.h>
#include <stddef.h>
-#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>
#include "format-util.h"
#include "libudev-util.h"
#include "netlink-util.h"
+#include "parse-util.h"
#include "path-util.h"
#include "process-util.h"
#include "rlimit-util.h"
#include "user-util.h"
typedef struct Spawn {
+ sd_device *device;
const char *cmd;
pid_t pid;
usec_t timeout_warn_usec;
static const struct subst_map_entry map[] = {
{ .name = "devnode", .fmt = 'N', .type = FORMAT_SUBST_DEVNODE },
- { .name = "tempnode", .fmt = 'N', .type = FORMAT_SUBST_DEVNODE },
+ { .name = "tempnode", .fmt = 'N', .type = FORMAT_SUBST_DEVNODE }, /* deprecated */
{ .name = "attr", .fmt = 's', .type = FORMAT_SUBST_ATTR },
- { .name = "sysfs", .fmt = 's', .type = FORMAT_SUBST_ATTR },
+ { .name = "sysfs", .fmt = 's', .type = FORMAT_SUBST_ATTR }, /* deprecated */
{ .name = "env", .fmt = 'E', .type = FORMAT_SUBST_ENV },
{ .name = "kernel", .fmt = 'k', .type = FORMAT_SUBST_KERNEL },
{ .name = "number", .fmt = 'n', .type = FORMAT_SUBST_KERNEL_NUMBER },
{ .name = "sys", .fmt = 'S', .type = FORMAT_SUBST_SYS },
};
+static const char *format_type_to_string(FormatSubstitutionType t) {
+ for (size_t i = 0; i < ELEMENTSOF(map); i++)
+ if (map[i].type == t)
+ return map[i].name;
+ return NULL;
+}
+
+static char format_type_to_char(FormatSubstitutionType t) {
+ for (size_t i = 0; i < ELEMENTSOF(map); i++)
+ if (map[i].type == t)
+ return map[i].fmt;
+ return '\0';
+}
+
+static int get_subst_type(const char **str, bool strict, FormatSubstitutionType *ret_type, char ret_attr[static UTIL_PATH_SIZE]) {
+ const char *p = *str, *q = NULL;
+ size_t i;
+
+ assert(str);
+ assert(*str);
+ assert(ret_type);
+ assert(ret_attr);
+
+ if (*p == '$') {
+ p++;
+ if (*p == '$') {
+ *str = p;
+ return 0;
+ }
+ for (i = 0; i < ELEMENTSOF(map); i++)
+ if ((q = startswith(p, map[i].name)))
+ break;
+ } else if (*p == '%') {
+ p++;
+ if (*p == '%') {
+ *str = p;
+ return 0;
+ }
+
+ for (i = 0; i < ELEMENTSOF(map); i++)
+ if (*p == map[i].fmt) {
+ q = p + 1;
+ break;
+ }
+ } else
+ return 0;
+ if (!q)
+ /* When 'strict' flag is set, then '$' and '%' must be escaped. */
+ return strict ? -EINVAL : 0;
+
+ if (*q == '{') {
+ const char *start, *end;
+ size_t len;
+
+ start = q + 1;
+ end = strchr(start, '}');
+ if (!end)
+ return -EINVAL;
+
+ len = end - start;
+ if (len == 0 || len >= UTIL_PATH_SIZE)
+ return -EINVAL;
+
+ strnscpy(ret_attr, UTIL_PATH_SIZE, start, len);
+ q = end + 1;
+ } else
+ *ret_attr = '\0';
+
+ *str = q;
+ *ret_type = map[i].type;
+ return 1;
+}
+
+static int safe_atou_optional_plus(const char *s, unsigned *ret) {
+ const char *p;
+ int r;
+
+ assert(s);
+ assert(ret);
+
+ /* Returns 1 if plus, 0 if no plus, negative on error */
+
+ p = endswith(s, "+");
+ if (p)
+ s = strndupa(s, p - s);
+
+ r = safe_atou(s, ret);
+ if (r < 0)
+ return r;
+
+ return !!p;
+}
+
static ssize_t udev_event_subst_format(
UdevEvent *event,
FormatSubstitutionType type,
sd_device *parent, *dev = event->dev;
const char *val = NULL;
char *s = dest;
- dev_t devnum;
int r;
switch (type) {
break;
case FORMAT_SUBST_MAJOR:
case FORMAT_SUBST_MINOR: {
- char buf[DECIMAL_STR_MAX(unsigned)];
+ dev_t devnum;
r = sd_device_get_devnum(dev, &devnum);
if (r < 0 && r != -ENOENT)
return r;
- xsprintf(buf, "%u", r < 0 ? 0 : type == FORMAT_SUBST_MAJOR ? major(devnum) : minor(devnum));
- l = strpcpy(&s, l, buf);
+ l = strpcpyf(&s, l, "%u", r < 0 ? 0 : type == FORMAT_SUBST_MAJOR ? major(devnum) : minor(devnum));
break;
}
case FORMAT_SUBST_RESULT: {
- char *rest;
- int i;
+ unsigned index = 0; /* 0 means whole string */
+ bool has_plus;
if (!event->program_result)
goto null_terminate;
- /* get part of the result string */
- i = 0;
- if (attr)
- i = strtoul(attr, &rest, 10);
- if (i > 0) {
- char result[UTIL_PATH_SIZE], tmp[UTIL_PATH_SIZE], *cpos;
-
- strscpy(result, sizeof(result), event->program_result);
- cpos = result;
- while (--i) {
- while (cpos[0] != '\0' && !isspace(cpos[0]))
- cpos++;
- while (isspace(cpos[0]))
- cpos++;
- if (cpos[0] == '\0')
+ if (!isempty(attr)) {
+ r = safe_atou_optional_plus(attr, &index);
+ if (r < 0)
+ return r;
+
+ has_plus = r;
+ }
+
+ if (index == 0)
+ l = strpcpy(&s, l, event->program_result);
+ else {
+ const char *start, *p;
+ unsigned i;
+
+ p = skip_leading_chars(event->program_result, NULL);
+
+ for (i = 1; i < index; i++) {
+ while (*p && !strchr(WHITESPACE, *p))
+ p++;
+ p = skip_leading_chars(p, NULL);
+ if (*p == '\0')
break;
}
- if (i > 0) {
- log_error("requested part of result string not found");
- break;
+ if (i != index) {
+ log_device_debug(dev, "requested part of result string not found");
+ goto null_terminate;
}
- strscpy(tmp, sizeof(tmp), cpos);
- /* %{2+}c copies the whole string from the second part on */
- if (rest[0] != '+') {
- cpos = strchr(tmp, ' ');
- if (cpos)
- cpos[0] = '\0';
+
+ start = p;
+ /* %c{2+} copies the whole string from the second part on */
+ if (has_plus)
+ l = strpcpy(&s, l, start);
+ else {
+ while (*p && !strchr(WHITESPACE, *p))
+ p++;
+ l = strnpcpy(&s, l, start, p - start);
}
- l = strpcpy(&s, l, tmp);
- } else
- l = strpcpy(&s, l, event->program_result);
+ }
break;
}
case FORMAT_SUBST_ATTR: {
char vbuf[UTIL_NAME_SIZE];
- size_t len;
int count;
- if (!attr)
+ if (isempty(attr))
return -EINVAL;
/* try to read the value specified by "[dmi/id]product_name" */
/* strip trailing whitespace, and replace unwanted characters */
if (val != vbuf)
strscpy(vbuf, sizeof(vbuf), val);
- len = strlen(vbuf);
- while (len > 0 && isspace(vbuf[--len]))
- vbuf[len] = '\0';
+ delete_trailing_chars(vbuf, NULL);
count = util_replace_chars(vbuf, UDEV_ALLOWED_CHARS_INPUT);
if (count > 0)
log_device_debug(dev, "%i character(s) replaced", count);
l = strpcpy(&s, l, "/sys");
break;
case FORMAT_SUBST_ENV:
- if (!attr)
- goto null_terminate;
+ if (isempty(attr))
+ return -EINVAL;
r = sd_device_get_property_value(dev, attr, &val);
if (r == -ENOENT)
goto null_terminate;
ssize_t udev_event_apply_format(UdevEvent *event,
const char *src, char *dest, size_t size,
bool replace_whitespace) {
- const char *from;
- char *s;
- size_t l;
+ const char *s = src;
+ int r;
assert(event);
assert(event->dev);
assert(dest);
assert(size > 0);
- from = src;
- s = dest;
- l = size;
-
- for (;;) {
- const struct subst_map_entry *entry = NULL;
- char attrbuf[UTIL_PATH_SIZE], *attr;
- bool format_dollar = false;
+ while (*s) {
+ FormatSubstitutionType type;
+ char attr[UTIL_PATH_SIZE];
ssize_t subst_len;
- while (from[0] != '\0') {
- if (from[0] == '$') {
- /* substitute named variable */
- unsigned i;
-
- if (from[1] == '$') {
- from++;
- goto copy;
- }
-
- for (i = 0; i < ELEMENTSOF(map); i++) {
- if (startswith(&from[1], map[i].name)) {
- entry = &map[i];
- from += strlen(map[i].name)+1;
- format_dollar = true;
- goto subst;
- }
- }
- } else if (from[0] == '%') {
- /* substitute format char */
- unsigned i;
-
- if (from[1] == '%') {
- from++;
- goto copy;
- }
-
- for (i = 0; i < ELEMENTSOF(map); i++) {
- if (from[1] == map[i].fmt) {
- entry = &map[i];
- from += 2;
- goto subst;
- }
- }
- }
-copy:
- /* copy char */
- if (l < 2) /* need space for this char and the terminating NUL */
- goto out;
- s[0] = from[0];
- from++;
- s++;
- l--;
+ r = get_subst_type(&s, false, &type, attr);
+ if (r < 0)
+ return log_device_warning_errno(event->dev, r, "Invalid format string, ignoring: %s", src);
+ if (r == 0) {
+ if (size < 2) /* need space for this char and the terminating NUL */
+ break;
+ *dest++ = *s++;
+ size--;
+ continue;
}
- goto out;
-subst:
- /* extract possible $format{attr} */
- if (from[0] == '{') {
- unsigned i;
+ subst_len = udev_event_subst_format(event, type, attr, dest, size);
+ if (subst_len < 0)
+ return log_device_warning_errno(event->dev, subst_len,
+ "Failed to substitute variable '$%s' or apply format '%%%c', ignoring: %m",
+ format_type_to_string(type), format_type_to_char(type));
- from++;
- for (i = 0; from[i] != '}'; i++)
- if (from[i] == '\0') {
- log_error("missing closing brace for format '%s'", src);
- goto out;
- }
-
- if (i >= sizeof(attrbuf))
- goto out;
- memcpy(attrbuf, from, i);
- attrbuf[i] = '\0';
- from += i+1;
- attr = attrbuf;
- } else
- attr = NULL;
-
- subst_len = udev_event_subst_format(event, entry->type, attr, s, l);
- if (subst_len < 0) {
- if (format_dollar)
- log_device_warning_errno(event->dev, subst_len, "Failed to substitute variable '$%s', ignoring: %m", entry->name);
- else
- log_device_warning_errno(event->dev, subst_len, "Failed to apply format '%%%c', ignoring: %m", entry->fmt);
+ /* FORMAT_SUBST_RESULT handles spaces itself */
+ if (replace_whitespace && type != FORMAT_SUBST_RESULT)
+ /* util_replace_whitespace can replace in-place,
+ * and does nothing if subst_len == 0 */
+ subst_len = util_replace_whitespace(dest, dest, subst_len);
+ dest += subst_len;
+ size -= subst_len;
+ }
+
+ assert(size >= 1);
+ *dest = '\0';
+ return size;
+}
+
+int udev_check_format(const char *value, size_t *offset, const char **hint) {
+ FormatSubstitutionType type;
+ const char *s = value;
+ char attr[UTIL_PATH_SIZE];
+ int r;
+
+ while (*s) {
+ r = get_subst_type(&s, true, &type, attr);
+ if (r < 0) {
+ if (offset)
+ *offset = s - value;
+ if (hint)
+ *hint = "invalid substitution type";
+ return r;
+ } else if (r == 0) {
+ s++;
continue;
}
- /* FORMAT_SUBST_RESULT handles spaces itself */
- if (replace_whitespace && entry->type != FORMAT_SUBST_RESULT)
- /* util_replace_whitespace can replace in-place,
- * and does nothing if subst_len == 0
- */
- subst_len = util_replace_whitespace(s, s, subst_len);
+ if (IN_SET(type, FORMAT_SUBST_ATTR, FORMAT_SUBST_ENV) && isempty(attr)) {
+ if (offset)
+ *offset = s - value;
+ if (hint)
+ *hint = "attribute value missing";
+ return -EINVAL;
+ }
- s += subst_len;
- l -= subst_len;
+ if (type == FORMAT_SUBST_RESULT && !isempty(attr)) {
+ unsigned i;
+
+ r = safe_atou_optional_plus(attr, &i);
+ if (r < 0) {
+ if (offset)
+ *offset = s - value;
+ if (hint)
+ *hint = "attribute value not a valid number";
+ return r;
+ }
+ }
}
-out:
- assert(l >= 1);
- s[0] = '\0';
- return l;
+ return 0;
}
static int on_spawn_io(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
l = read(fd, p, size - 1);
if (l < 0) {
if (errno != EAGAIN)
- log_error_errno(errno, "Failed to read stdout of '%s': %m", spawn->cmd);
+ log_device_error_errno(spawn->device, errno,
+ "Failed to read stdout of '%s': %m", spawn->cmd);
return 0;
}
return 0;
STRV_FOREACH(q, v)
- log_debug("'%s'(%s) '%s'", spawn->cmd,
- fd == spawn->fd_stdout ? "out" : "err", *q);
+ log_device_debug(spawn->device, "'%s'(%s) '%s'", spawn->cmd,
+ fd == spawn->fd_stdout ? "out" : "err", *q);
}
return 0;
kill_and_sigcont(spawn->pid, SIGKILL);
- log_error("Spawned process '%s' ["PID_FMT"] timed out after %s, killing", spawn->cmd, spawn->pid,
- format_timespan(timeout, sizeof(timeout), spawn->timeout_usec, USEC_PER_SEC));
+ log_device_error(spawn->device, "Spawned process '%s' ["PID_FMT"] timed out after %s, killing",
+ spawn->cmd, spawn->pid,
+ format_timespan(timeout, sizeof(timeout), spawn->timeout_usec, USEC_PER_SEC));
return 1;
}
assert(spawn);
- log_warning("Spawned process '%s' ["PID_FMT"] is taking longer than %s to complete", spawn->cmd, spawn->pid,
- format_timespan(timeout, sizeof(timeout), spawn->timeout_warn_usec, USEC_PER_SEC));
+ log_device_warning(spawn->device, "Spawned process '%s' ["PID_FMT"] is taking longer than %s to complete",
+ spawn->cmd, spawn->pid,
+ format_timespan(timeout, sizeof(timeout), spawn->timeout_warn_usec, USEC_PER_SEC));
return 1;
}
switch (si->si_code) {
case CLD_EXITED:
if (si->si_status == 0)
- log_debug("Process '%s' succeeded.", spawn->cmd);
+ log_device_debug(spawn->device, "Process '%s' succeeded.", spawn->cmd);
else
- log_full(spawn->accept_failure ? LOG_DEBUG : LOG_WARNING,
- "Process '%s' failed with exit code %i.", spawn->cmd, si->si_status);
+ log_device_full(spawn->device, spawn->accept_failure ? LOG_DEBUG : LOG_WARNING, 0,
+ "Process '%s' failed with exit code %i.", spawn->cmd, si->si_status);
ret = si->si_status;
break;
case CLD_KILLED:
case CLD_DUMPED:
- log_error("Process '%s' terminated by signal %s.", spawn->cmd, signal_to_string(si->si_status));
+ log_device_error(spawn->device, "Process '%s' terminated by signal %s.", spawn->cmd, signal_to_string(si->si_status));
break;
default:
- log_error("Process '%s' failed due to unknown reason.", spawn->cmd);
+ log_device_error(spawn->device, "Process '%s' failed due to unknown reason.", spawn->cmd);
}
sd_event_exit(sd_event_source_get_event(s), ret);
/* pipes from child to parent */
if (result || log_get_max_level() >= LOG_INFO)
if (pipe2(outpipe, O_NONBLOCK|O_CLOEXEC) != 0)
- return log_error_errno(errno, "Failed to create pipe for command '%s': %m", cmd);
+ return log_device_error_errno(event->dev, errno,
+ "Failed to create pipe for command '%s': %m", cmd);
if (log_get_max_level() >= LOG_INFO)
if (pipe2(errpipe, O_NONBLOCK|O_CLOEXEC) != 0)
- return log_error_errno(errno, "Failed to create pipe for command '%s': %m", cmd);
+ return log_device_error_errno(event->dev, errno,
+ "Failed to create pipe for command '%s': %m", cmd);
argv = strv_split_full(cmd, NULL, SPLIT_QUOTES|SPLIT_RELAX);
if (!argv)
return log_oom();
if (isempty(argv[0]))
- return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
- "Invalid command '%s'", cmd);
+ return log_device_error_errno(event->dev, SYNTHETIC_ERRNO(EINVAL),
+ "Invalid command '%s'", cmd);
/* allow programs in /usr/lib/udev/ to be called without the path */
if (!path_is_absolute(argv[0])) {
if (r < 0)
return log_device_error_errno(event->dev, r, "Failed to get device properties");
- log_debug("Starting '%s'", cmd);
+ log_device_debug(event->dev, "Starting '%s'", cmd);
r = safe_fork("(spawn)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_LOG, &pid);
if (r < 0)
- return log_error_errno(r, "Failed to fork() to execute command '%s': %m", cmd);
+ return log_device_error_errno(event->dev, r,
+ "Failed to fork() to execute command '%s': %m", cmd);
if (r == 0) {
if (rearrange_stdio(-1, outpipe[WRITE_END], errpipe[WRITE_END]) < 0)
_exit(EXIT_FAILURE);
errpipe[WRITE_END] = safe_close(errpipe[WRITE_END]);
spawn = (Spawn) {
+ .device = event->dev,
.cmd = cmd,
.pid = pid,
.accept_failure = accept_failure,
};
r = spawn_wait(&spawn);
if (r < 0)
- return log_error_errno(r, "Failed to wait for spawned command '%s': %m", cmd);
+ return log_device_error_errno(event->dev, r,
+ "Failed to wait for spawned command '%s': %m", cmd);
if (result)
result[spawn.result_len] = '\0';
/* Set ID_RENAMING boolean property here, and drop it in the corresponding move uevent later. */
r = device_add_property(dev, "ID_RENAMING", "1");
if (r < 0)
- log_device_warning_errno(dev, r, "Failed to add 'ID_RENAMING' property: %m");
+ return log_device_warning_errno(dev, r, "Failed to add 'ID_RENAMING' property: %m");
r = device_rename(dev, event->name);
if (r < 0)
- log_device_warning_errno(dev, r, "Failed to update properties with new name '%s': %m", event->name);
+ return log_device_warning_errno(dev, r, "Failed to update properties with new name '%s': %m", event->name);
log_device_debug(dev, "Network interface %i is renamed from '%s' to '%s'", ifindex, oldname, event->name);
static int update_devnode(UdevEvent *event) {
sd_device *dev = event->dev;
- bool apply;
int r;
r = sd_device_get_devnum(dev, NULL);
if (!uid_is_valid(event->uid)) {
r = device_get_devnode_uid(dev, &event->uid);
- if (r == -ENOENT)
- event->uid = 0;
- else if (r < 0)
+ if (r < 0 && r != -ENOENT)
return log_device_error_errno(dev, r, "Failed to get devnode UID: %m");
}
if (!gid_is_valid(event->gid)) {
r = device_get_devnode_gid(dev, &event->gid);
- if (r == -ENOENT)
- event->gid = 0;
- else if (r < 0)
+ if (r < 0 && r != -ENOENT)
return log_device_error_errno(dev, r, "Failed to get devnode GID: %m");
}
r = device_get_devnode_mode(dev, &event->mode);
if (r < 0 && r != -ENOENT)
return log_device_error_errno(dev, r, "Failed to get devnode mode: %m");
- if (r == -ENOENT) {
- if (event->gid > 0)
- /* default 0660 if a group is assigned */
- event->mode = 0660;
- else
- /* default 0600 */
- event->mode = 0600;
- }
}
+ if (event->mode == MODE_INVALID && gid_is_valid(event->gid) && event->gid > 0)
+ /* If group is set, but mode is not set, "upgrade" mode for the group. */
+ event->mode = 0660;
+
+ bool apply_mac = device_for_action(dev, DEVICE_ACTION_ADD);
- apply = device_for_action(dev, DEVICE_ACTION_ADD) ||
- uid_is_valid(event->uid) ||
- gid_is_valid(event->gid) ||
- event->mode != MODE_INVALID;
- return udev_node_add(dev, apply, event->mode, event->uid, event->gid, event->seclabel_list);
+ return udev_node_add(dev, apply_mac, event->mode, event->uid, event->gid, event->seclabel_list);
}
static void event_execute_rules_on_remove(
/* Drop previously added property */
r = device_add_property(dev, "ID_RENAMING", NULL);
if (r < 0)
- return log_device_debug_errno(dev, r, "Failed to remove 'ID_RENAMING' property, ignoring: %m");
+ return log_device_debug_errno(dev, r, "Failed to remove 'ID_RENAMING' property: %m");
return 0;
}
r = device_clone_with_db(dev, &event->dev_db_clone);
if (r < 0)
- log_device_debug_errno(dev, r, "Failed to clone sd_device object, ignoring: %m");
+ return log_device_debug_errno(dev, r, "Failed to clone sd_device object: %m");
if (event->dev_db_clone && sd_device_get_devnum(dev, NULL) >= 0)
/* Disable watch during event processing. */
(void) udev_watch_end(event->dev_db_clone);
- if (action == DEVICE_ACTION_MOVE)
- (void) udev_event_on_move(event);
+ if (action == DEVICE_ACTION_MOVE) {
+ r = udev_event_on_move(event);
+ if (r < 0)
+ return r;
+ }
- (void) udev_rules_apply_to_event(rules, event, timeout_usec, properties_list);
+ r = udev_rules_apply_to_event(rules, event, timeout_usec, properties_list);
+ if (r < 0)
+ return log_device_debug_errno(dev, r, "Failed to apply udev rules: %m");
+
+ r = rename_netif(event);
+ if (r < 0)
+ return r;
- (void) rename_netif(event);
- (void) update_devnode(event);
+ r = update_devnode(event);
+ if (r < 0)
+ return r;
/* preserve old, or get new initialization timestamp */
r = device_ensure_usec_initialized(dev, event->dev_db_clone);
if (r < 0)
- log_device_debug_errno(dev, r, "Failed to set initialization timestamp, ignoring: %m");
+ return log_device_debug_errno(dev, r, "Failed to set initialization timestamp: %m");
/* (re)write database file */
r = device_tag_index(dev, event->dev_db_clone, true);
if (r < 0)
- log_device_debug_errno(dev, r, "Failed to update tags under /run/udev/tag/, ignoring: %m");
+ return log_device_debug_errno(dev, r, "Failed to update tags under /run/udev/tag/: %m");
r = device_update_db(dev);
if (r < 0)
- log_device_debug_errno(dev, r, "Failed to update database under /run/udev/data/, ignoring: %m");
+ return log_device_debug_errno(dev, r, "Failed to update database under /run/udev/data/: %m");
device_set_is_initialized(dev);
}
void udev_event_execute_run(UdevEvent *event, usec_t timeout_usec) {
- const char *cmd;
+ const char *command;
void *val;
Iterator i;
int r;
- ORDERED_HASHMAP_FOREACH_KEY(val, cmd, event->run_list, i) {
+ ORDERED_HASHMAP_FOREACH_KEY(val, command, event->run_list, i) {
UdevBuiltinCommand builtin_cmd = PTR_TO_UDEV_BUILTIN_CMD(val);
- char command[UTIL_PATH_SIZE];
-
- (void) udev_event_apply_format(event, cmd, command, sizeof(command), false);
if (builtin_cmd != _UDEV_BUILTIN_INVALID) {
log_device_debug(event->dev, "Running built-in command \"%s\"", command);