diff options
author | Tom Gundersen <teg@jklm.no> | 2015-06-28 23:42:52 +0200 |
---|---|---|
committer | Tom Gundersen <teg@jklm.no> | 2015-06-29 19:51:04 +0200 |
commit | bbf35206735f97cf3fcda8d26982b35b0cad20a9 (patch) | |
tree | 22c7f66a5ea8f058e56929085c63216399a2a8e5 | |
parent | 1cf34d7118e93f0734a52d0eb09f238cf9962b85 (diff) |
udev: event - simplify udev_event_spawn() logic
Push the extraction of the envp + argv as close as possible to their use, to avoid code
duplication. As a sideeffect fix logging when delaing execution.
-rw-r--r-- | src/udev/udev-event.c | 50 | ||||
-rw-r--r-- | src/udev/udev-rules.c | 11 | ||||
-rw-r--r-- | src/udev/udev.h | 3 |
3 files changed, 28 insertions, 36 deletions
diff --git a/src/udev/udev-event.c b/src/udev/udev-event.c index 1092071e26..4761222786 100644 --- a/src/udev/udev-event.c +++ b/src/udev/udev-event.c @@ -721,19 +721,13 @@ int udev_event_spawn(struct udev_event *event, usec_t timeout_usec, usec_t timeout_warn_usec, bool accept_failure, - const char *cmd, char **envp, + const char *cmd, char *result, size_t ressize) { 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 || log_get_max_level() >= LOG_INFO) { if (pipe2(outpipe, O_NONBLOCK) != 0) { @@ -750,15 +744,14 @@ int udev_event_spawn(struct udev_event *event, } } - /* 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: + { + char arg[UTIL_PATH_SIZE]; + char *argv[128]; + char program[UTIL_PATH_SIZE]; + /* child closes parent's ends of pipes */ if (outpipe[READ_END] >= 0) { close(outpipe[READ_END]); @@ -769,12 +762,22 @@ int udev_event_spawn(struct udev_event *event, errpipe[READ_END] = -1; } + strscpy(arg, sizeof(arg), cmd); + udev_build_argv(event->udev, arg, NULL, argv); + + /* 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; + } + log_debug("starting '%s'", cmd); - spawn_exec(event, cmd, argv, envp, + spawn_exec(event, cmd, argv, udev_device_get_properties_envp(event->dev), outpipe[WRITE_END], errpipe[WRITE_END]); - _exit(2 ); + _exit(2); + } case -1: log_error_errno(errno, "fork of '%s' failed: %m", cmd); err = -1; @@ -934,26 +937,21 @@ void udev_event_execute_run(struct udev_event *event, usec_t timeout_usec, usec_ struct udev_list_entry *list_entry; udev_list_entry_foreach(list_entry, udev_list_get_entry(&event->run_list)) { + char command[UTIL_PATH_SIZE]; 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_event_apply_format(event, cmd, command, sizeof(command)); + if (builtin_cmd < UDEV_BUILTIN_MAX) udev_builtin_run(event->dev, builtin_cmd, command, false); - } else { - char program[UTIL_PATH_SIZE]; - char **envp; - + else { if (event->exec_delay > 0) { - log_debug("delay execution of '%s'", program); + log_debug("delay execution of '%s'", command); 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, timeout_usec, timeout_warn_usec, false, program, envp, NULL, 0); + udev_event_spawn(event, timeout_usec, timeout_warn_usec, false, command, NULL, 0); } } } diff --git a/src/udev/udev-rules.c b/src/udev/udev-rules.c index 8ebc061eb1..c3e2e211e3 100644 --- a/src/udev/udev-rules.c +++ b/src/udev/udev-rules.c @@ -634,14 +634,11 @@ static int import_program_into_properties(struct udev_event *event, usec_t timeout_usec, usec_t timeout_warn_usec, const char *program) { - struct udev_device *dev = event->dev; - char **envp; char result[UTIL_LINE_SIZE]; char *line; int err; - envp = udev_device_get_properties_envp(dev); - err = udev_event_spawn(event, timeout_usec, timeout_warn_usec, true, program, envp, result, sizeof(result)); + err = udev_event_spawn(event, timeout_usec, timeout_warn_usec, true, program, result, sizeof(result)); if (err < 0) return err; @@ -654,7 +651,7 @@ static int import_program_into_properties(struct udev_event *event, pos[0] = '\0'; pos = &pos[1]; } - import_property_from_string(dev, line); + import_property_from_string(event->dev, line); line = pos; } return 0; @@ -2119,19 +2116,17 @@ int udev_rules_apply_to_event(struct udev_rules *rules, } case TK_M_PROGRAM: { char program[UTIL_PATH_SIZE]; - char **envp; char result[UTIL_LINE_SIZE]; free(event->program_result); event->program_result = NULL; udev_event_apply_format(event, rules_str(rules, cur->key.value_off), program, sizeof(program)); - envp = udev_device_get_properties_envp(event->dev); log_debug("PROGRAM '%s' %s:%u", program, rules_str(rules, rule->rule.filename_off), rule->rule.filename_line); - if (udev_event_spawn(event, timeout_usec, timeout_warn_usec, true, program, envp, result, sizeof(result)) < 0) { + if (udev_event_spawn(event, timeout_usec, timeout_warn_usec, true, program, result, sizeof(result)) < 0) { if (cur->key.op != OP_NOMATCH) goto nomatch; } else { diff --git a/src/udev/udev.h b/src/udev/udev.h index 3dca72e499..d17fc8c1ea 100644 --- a/src/udev/udev.h +++ b/src/udev/udev.h @@ -85,8 +85,7 @@ int udev_event_spawn(struct udev_event *event, usec_t timeout_usec, usec_t timeout_warn_usec, bool accept_failure, - const char *cmd, char **envp, - char *result, size_t ressize); + const char *cmd, char *result, size_t ressize); void udev_event_execute_rules(struct udev_event *event, usec_t timeout_usec, usec_t timeout_warn_usec, struct udev_list *properties_list, |