X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Fshared%2Futil.c;h=1210900bcaee61e72952e58dfe39b8b7f5e0e25a;hb=7f76961982e03d4d5f781e7e7113fc7eff970f82;hp=06bd1b9f04b4fdc4714d6c5d0ae7a872b5ecf93b;hpb=4968105790c65af58d4ab42bffa2a4bedc0be8ee;p=elogind.git diff --git a/src/shared/util.c b/src/shared/util.c index 06bd1b9f0..1210900bc 100644 --- a/src/shared/util.c +++ b/src/shared/util.c @@ -1352,12 +1352,19 @@ char *cunescape_length_with_prefix(const char *s, size_t length, const char *pre memcpy(r, prefix, pl); for (f = s, t = r + pl; f < s + length; f++) { + size_t remaining = s + length - f; + assert(remaining > 0); - if (*f != '\\') { + if (*f != '\\') { /* a literal literal */ *(t++) = *f; continue; } + if (--remaining == 0) { /* copy trailing backslash verbatim */ + *(t++) = *f; + break; + } + f++; switch (*f) { @@ -1400,10 +1407,12 @@ char *cunescape_length_with_prefix(const char *s, size_t length, const char *pre case 'x': { /* hexadecimal encoding */ - int a, b; + int a = -1, b = -1; - a = unhexchar(f[1]); - b = unhexchar(f[2]); + if (remaining >= 2) { + a = unhexchar(f[1]); + b = unhexchar(f[2]); + } if (a < 0 || b < 0 || (a == 0 && b == 0)) { /* Invalid escape code, let's take it literal then */ @@ -1426,11 +1435,13 @@ char *cunescape_length_with_prefix(const char *s, size_t length, const char *pre case '6': case '7': { /* octal encoding */ - int a, b, c; + int a = -1, b = -1, c = -1; - a = unoctchar(f[0]); - b = unoctchar(f[1]); - c = unoctchar(f[2]); + if (remaining >= 3) { + a = unoctchar(f[0]); + b = unoctchar(f[1]); + c = unoctchar(f[2]); + } if (a < 0 || b < 0 || c < 0 || (a == 0 && b == 0 && c == 0)) { /* Invalid escape code, let's take it literal then */ @@ -1444,11 +1455,6 @@ char *cunescape_length_with_prefix(const char *s, size_t length, const char *pre break; } - case 0: - /* premature end of string. */ - *(t++) = '\\'; - goto finish; - default: /* Invalid escape code, let's take it literal then */ *(t++) = '\\'; @@ -1457,7 +1463,6 @@ char *cunescape_length_with_prefix(const char *s, size_t length, const char *pre } } -finish: *t = 0; return r; } @@ -4037,10 +4042,10 @@ bool dirent_is_file_with_suffix(const struct dirent *de, const char *suffix) { return endswith(de->d_name, suffix); } -static int do_execute(const char *directory, usec_t timeout, char *argv[]) { +static int do_execute(char **directories, usec_t timeout, char *argv[]) { _cleanup_hashmap_free_free_ Hashmap *pids = NULL; - _cleanup_closedir_ DIR *d; - struct dirent *de; + _cleanup_set_free_free_ Set *seen = NULL; + char **directory; /* We fork this all off from a child process so that we can * somewhat cleanly make use of SIGALRM to set a time limit */ @@ -4050,57 +4055,80 @@ static int do_execute(const char *directory, usec_t timeout, char *argv[]) { assert_se(prctl(PR_SET_PDEATHSIG, SIGTERM) == 0); - d = opendir(directory); - if (!d) { - if (errno == ENOENT) - return 0; - - return log_error_errno(errno, "Failed to open directory %s: %m", directory); - } - pids = hashmap_new(NULL); if (!pids) return log_oom(); - FOREACH_DIRENT(de, d, break) { - _cleanup_free_ char *path = NULL; - pid_t pid; - int r; + seen = set_new(&string_hash_ops); + if (!seen) + return log_oom(); - if (!dirent_is_file(de)) - continue; + STRV_FOREACH(directory, directories) { + _cleanup_closedir_ DIR *d; + struct dirent *de; - path = strjoin(directory, "/", de->d_name, NULL); - if (!path) - return log_oom(); + d = opendir(*directory); + if (!d) { + if (errno == ENOENT) + continue; - pid = fork(); - if (pid < 0) { - log_error_errno(errno, "Failed to fork: %m"); - continue; - } else if (pid == 0) { - char *_argv[2]; + return log_error_errno(errno, "Failed to open directory %s: %m", *directory); + } - assert_se(prctl(PR_SET_PDEATHSIG, SIGTERM) == 0); + FOREACH_DIRENT(de, d, break) { + _cleanup_free_ char *path = NULL; + pid_t pid; + int r; - if (!argv) { - _argv[0] = path; - _argv[1] = NULL; - argv = _argv; + if (!dirent_is_file(de)) + continue; + + if (set_contains(seen, de->d_name)) { + log_debug("%1$s/%2$s skipped (%2$s was already seen).", *directory, de->d_name); + continue; + } + + r = set_put_strdup(seen, de->d_name); + if (r < 0) + return log_oom(); + + path = strjoin(*directory, "/", de->d_name, NULL); + if (!path) + return log_oom(); + + if (null_or_empty_path(path)) { + log_debug("%s is empty (a mask).", path); + continue; } else - argv[0] = path; + log_debug("%s will be executed.", path); - execv(path, argv); - return log_error_errno(errno, "Failed to execute %s: %m", path); - } + pid = fork(); + if (pid < 0) { + log_error_errno(errno, "Failed to fork: %m"); + continue; + } else if (pid == 0) { + char *_argv[2]; - log_debug("Spawned %s as " PID_FMT ".", path, pid); + assert_se(prctl(PR_SET_PDEATHSIG, SIGTERM) == 0); - r = hashmap_put(pids, UINT_TO_PTR(pid), path); - if (r < 0) - return log_oom(); + if (!argv) { + _argv[0] = path; + _argv[1] = NULL; + argv = _argv; + } else + argv[0] = path; + + execv(path, argv); + return log_error_errno(errno, "Failed to execute %s: %m", path); + } - path = NULL; + log_debug("Spawned %s as " PID_FMT ".", path, pid); + + r = hashmap_put(pids, UINT_TO_PTR(pid), path); + if (r < 0) + return log_oom(); + path = NULL; + } } /* Abort execution of this process after the timout. We simply @@ -4126,14 +4154,21 @@ static int do_execute(const char *directory, usec_t timeout, char *argv[]) { return 0; } -void execute_directory(const char *directory, usec_t timeout, char *argv[]) { +void execute_directories(const char* const* directories, usec_t timeout, char *argv[]) { pid_t executor_pid; int r; + char *name; + char **dirs = (char**) directories; - assert(directory); + assert(!strv_isempty(dirs)); - /* Executes all binaries in the directory in parallel and waits - * for them to finish. Optionally a timeout is applied. */ + name = basename(dirs[0]); + assert(!isempty(name)); + + /* Executes all binaries in the directories in parallel and waits + * for them to finish. Optionally a timeout is applied. If a file + * with the same name exists in more than one directory, the + * earliest one wins. */ executor_pid = fork(); if (executor_pid < 0) { @@ -4141,11 +4176,11 @@ void execute_directory(const char *directory, usec_t timeout, char *argv[]) { return; } else if (executor_pid == 0) { - r = do_execute(directory, timeout, argv); + r = do_execute(dirs, timeout, argv); _exit(r < 0 ? EXIT_FAILURE : EXIT_SUCCESS); } - wait_for_terminate_and_warn(directory, executor_pid, true); + wait_for_terminate_and_warn(name, executor_pid, true); } int kill_and_sigcont(pid_t pid, int sig) { @@ -6714,23 +6749,6 @@ uint64_t physical_memory(void) { return (uint64_t) mem * (uint64_t) page_size(); } -char* mount_test_option(const char *haystack, const char *needle) { - - struct mntent me = { - .mnt_opts = (char*) haystack - }; - - assert(needle); - - /* Like glibc's hasmntopt(), but works on a string, not a - * struct mntent */ - - if (!haystack) - return NULL; - - return hasmntopt(&me, needle); -} - void hexdump(FILE *f, const void *p, size_t s) { const uint8_t *b = p; unsigned n = 0;