diff options
Diffstat (limited to 'src/shared')
-rw-r--r-- | src/shared/dev-setup.c | 29 | ||||
-rw-r--r-- | src/shared/dev-setup.h | 4 | ||||
-rw-r--r-- | src/shared/missing.h | 8 | ||||
-rw-r--r-- | src/shared/path-util.c | 34 | ||||
-rw-r--r-- | src/shared/path-util.h | 1 | ||||
-rw-r--r-- | src/shared/random-util.c | 1 | ||||
-rw-r--r-- | src/shared/udev-util.h | 4 | ||||
-rw-r--r-- | src/shared/util.c | 509 | ||||
-rw-r--r-- | src/shared/util.h | 20 |
9 files changed, 594 insertions, 16 deletions
diff --git a/src/shared/dev-setup.c b/src/shared/dev-setup.c index c9eb0a5efc..60687a62dd 100644 --- a/src/shared/dev-setup.c +++ b/src/shared/dev-setup.c @@ -24,15 +24,14 @@ #include <assert.h> #include <unistd.h> -#include "dev-setup.h" #include "log.h" #include "macro.h" #include "util.h" #include "label.h" +#include "path-util.h" +#include "dev-setup.h" -int dev_setup(const char *prefix) { - const char *j, *k; - +int dev_setup(const char *prefix, uid_t uid, gid_t gid) { static const char symlinks[] = "-/proc/kcore\0" "/dev/core\0" "/proc/self/fd\0" "/dev/fd\0" @@ -40,7 +39,13 @@ int dev_setup(const char *prefix) { "/proc/self/fd/1\0" "/dev/stdout\0" "/proc/self/fd/2\0" "/dev/stderr\0"; + const char *j, *k; + int r; + NULSTR_FOREACH_PAIR(j, k, symlinks) { + _cleanup_free_ char *link_name = NULL; + const char *n; + if (j[0] == '-') { j++; @@ -49,15 +54,21 @@ int dev_setup(const char *prefix) { } if (prefix) { - _cleanup_free_ char *link_name = NULL; - - link_name = strjoin(prefix, "/", k, NULL); + link_name = prefix_root(prefix, k); if (!link_name) return -ENOMEM; - symlink_label(j, link_name); + n = link_name; } else - symlink_label(j, k); + n = k; + + r = symlink_label(j, n); + if (r < 0) + log_debug_errno(r, "Failed to symlink %s to %s: %m", j, n); + + if (uid != UID_INVALID || gid != GID_INVALID) + if (lchown(n, uid, gid) < 0) + log_debug_errno(errno, "Failed to chown %s: %m", n); } return 0; diff --git a/src/shared/dev-setup.h b/src/shared/dev-setup.h index 0adea22a99..e85d89e5bc 100644 --- a/src/shared/dev-setup.h +++ b/src/shared/dev-setup.h @@ -19,4 +19,6 @@ #pragma once -int dev_setup(const char *pathprefix); +#include <sys/types.h> + +int dev_setup(const char *prefix, uid_t uid, gid_t gid); diff --git a/src/shared/missing.h b/src/shared/missing.h index a5cf627373..2dc9d842d3 100644 --- a/src/shared/missing.h +++ b/src/shared/missing.h @@ -150,3 +150,11 @@ static inline int name_to_handle_at(int fd, const char *name, struct file_handle #ifndef INPUT_PROP_ACCELEROMETER #define INPUT_PROP_ACCELEROMETER 0x06 #endif + +#ifndef O_PATH +#define O_PATH 010000000 +#endif + +#ifndef AT_EMPTY_PATH +#define AT_EMPTY_PATH 0x1000 +#endif diff --git a/src/shared/path-util.c b/src/shared/path-util.c index 665b171263..0744563976 100644 --- a/src/shared/path-util.c +++ b/src/shared/path-util.c @@ -539,3 +539,37 @@ bool paths_check_timestamp(const char* const* paths, usec_t *timestamp, bool upd return changed; } + +char *prefix_root(const char *root, const char *path) { + char *n, *p; + size_t l; + + /* If root is passed, prefixes path with it. Otherwise returns + * it as is. */ + + assert(path); + + /* First, drop duplicate prefixing slashes from the path */ + while (path[0] == '/' && path[1] == '/') + path++; + + if (isempty(root) || path_equal(root, "/")) + return strdup(path); + + l = strlen(root) + 1 + strlen(path) + 1; + + n = new(char, l); + if (!n) + return NULL; + + p = stpcpy(n, root); + + while (p > n && p[-1] == '/') + p--; + + if (path[0] != '/') + *(p++) = '/'; + + strcpy(p, path); + return n; +} diff --git a/src/shared/path-util.h b/src/shared/path-util.h index 56d1c52ae2..0123c2609c 100644 --- a/src/shared/path-util.h +++ b/src/shared/path-util.h @@ -38,3 +38,4 @@ int fd_is_mount_point(int fd); int path_is_mount_point(const char *path, bool allow_symlink); bool paths_check_timestamp(const char* const* paths, usec_t *paths_ts_usec, bool update); +char *prefix_root(const char *root, const char *path); diff --git a/src/shared/random-util.c b/src/shared/random-util.c index 88f5182508..01a28c8ef4 100644 --- a/src/shared/random-util.c +++ b/src/shared/random-util.c @@ -23,7 +23,6 @@ #include <sys/stat.h> #include <fcntl.h> #include <time.h> -#include <sys/auxv.h> #include <linux/random.h> #include "random-util.h" diff --git a/src/shared/udev-util.h b/src/shared/udev-util.h index e1dec0b1b4..b258b4d428 100644 --- a/src/shared/udev-util.h +++ b/src/shared/udev-util.h @@ -28,6 +28,8 @@ DEFINE_TRIVIAL_CLEANUP_FUNC(struct udev_enumerate*, udev_enumerate_unref); DEFINE_TRIVIAL_CLEANUP_FUNC(struct udev_event*, udev_event_unref); DEFINE_TRIVIAL_CLEANUP_FUNC(struct udev_rules*, udev_rules_unref); DEFINE_TRIVIAL_CLEANUP_FUNC(struct udev_ctrl*, udev_ctrl_unref); +DEFINE_TRIVIAL_CLEANUP_FUNC(struct udev_ctrl_connection*, udev_ctrl_connection_unref); +DEFINE_TRIVIAL_CLEANUP_FUNC(struct udev_ctrl_msg*, udev_ctrl_msg_unref); DEFINE_TRIVIAL_CLEANUP_FUNC(struct udev_monitor*, udev_monitor_unref); #define _cleanup_udev_unref_ _cleanup_(udev_unrefp) @@ -36,5 +38,7 @@ DEFINE_TRIVIAL_CLEANUP_FUNC(struct udev_monitor*, udev_monitor_unref); #define _cleanup_udev_event_unref_ _cleanup_(udev_event_unrefp) #define _cleanup_udev_rules_unref_ _cleanup_(udev_rules_unrefp) #define _cleanup_udev_ctrl_unref_ _cleanup_(udev_ctrl_unrefp) +#define _cleanup_udev_ctrl_connection_unref_ _cleanup_(udev_ctrl_connection_unrefp) +#define _cleanup_udev_ctrl_msg_unref_ _cleanup_(udev_ctrl_msg_unrefp) #define _cleanup_udev_monitor_unref_ _cleanup_(udev_monitor_unrefp) #define _cleanup_udev_list_cleanup_ _cleanup_(udev_list_cleanup) diff --git a/src/shared/util.c b/src/shared/util.c index c30b4eaf57..88defdc4f2 100644 --- a/src/shared/util.c +++ b/src/shared/util.c @@ -60,11 +60,12 @@ #include "missing.h" #include "log.h" #include "strv.h" -#include "label.h" +#include "mkdir.h" #include "path-util.h" #include "exit-status.h" #include "hashmap.h" #include "fileio.h" +#include "utf8.h" #include "virt.h" #include "process-util.h" #include "random-util.h" @@ -511,10 +512,32 @@ char hexchar(int x) { return table[x & 15]; } +int unhexchar(char c) { + + if (c >= '0' && c <= '9') + return c - '0'; + + if (c >= 'a' && c <= 'f') + return c - 'a' + 10; + + if (c >= 'A' && c <= 'F') + return c - 'A' + 10; + + return -EINVAL; +} + char octchar(int x) { return '0' + (x & 7); } +int unoctchar(char c) { + + if (c >= '0' && c <= '7') + return c - '0'; + + return -EINVAL; +} + char *cescape(const char *s) { char *r, *t; const char *f; @@ -536,6 +559,208 @@ char *cescape(const char *s) { return r; } + +static int cunescape_one(const char *p, size_t length, char *ret, uint32_t *ret_unicode) { + int r = 1; + + assert(p); + assert(*p); + assert(ret); + + /* Unescapes C style. Returns the unescaped character in ret, + * unless we encountered a \u sequence in which case the full + * unicode character is returned in ret_unicode, instead. */ + + if (length != (size_t) -1 && length < 1) + return -EINVAL; + + switch (p[0]) { + + case 'a': + *ret = '\a'; + break; + case 'b': + *ret = '\b'; + break; + case 'f': + *ret = '\f'; + break; + case 'n': + *ret = '\n'; + break; + case 'r': + *ret = '\r'; + break; + case 't': + *ret = '\t'; + break; + case 'v': + *ret = '\v'; + break; + case '\\': + *ret = '\\'; + break; + case '"': + *ret = '"'; + break; + case '\'': + *ret = '\''; + break; + + case 's': + /* This is an extension of the XDG syntax files */ + *ret = ' '; + break; + + case 'x': { + /* hexadecimal encoding */ + int a, b; + + if (length != (size_t) -1 && length < 3) + return -EINVAL; + + a = unhexchar(p[1]); + if (a < 0) + return -EINVAL; + + b = unhexchar(p[2]); + if (b < 0) + return -EINVAL; + + /* Don't allow NUL bytes */ + if (a == 0 && b == 0) + return -EINVAL; + + *ret = (char) ((a << 4U) | b); + r = 3; + break; + } + + case 'u': { + /* C++11 style 16bit unicode */ + + int a[4]; + unsigned i; + uint32_t c; + + if (length != (size_t) -1 && length < 5) + return -EINVAL; + + for (i = 0; i < 4; i++) { + a[i] = unhexchar(p[1 + i]); + if (a[i] < 0) + return a[i]; + } + + c = ((uint32_t) a[0] << 12U) | ((uint32_t) a[1] << 8U) | ((uint32_t) a[2] << 4U) | (uint32_t) a[3]; + + /* Don't allow 0 chars */ + if (c == 0) + return -EINVAL; + + if (c < 128) + *ret = c; + else { + if (!ret_unicode) + return -EINVAL; + + *ret = 0; + *ret_unicode = c; + } + + r = 5; + break; + } + + case 'U': { + /* C++11 style 32bit unicode */ + + int a[8]; + unsigned i; + uint32_t c; + + if (length != (size_t) -1 && length < 9) + return -EINVAL; + + for (i = 0; i < 8; i++) { + a[i] = unhexchar(p[1 + i]); + if (a[i] < 0) + return a[i]; + } + + c = ((uint32_t) a[0] << 28U) | ((uint32_t) a[1] << 24U) | ((uint32_t) a[2] << 20U) | ((uint32_t) a[3] << 16U) | + ((uint32_t) a[4] << 12U) | ((uint32_t) a[5] << 8U) | ((uint32_t) a[6] << 4U) | (uint32_t) a[7]; + + /* Don't allow 0 chars */ + if (c == 0) + return -EINVAL; + + /* Don't allow invalid code points */ + if (!unichar_is_valid(c)) + return -EINVAL; + + if (c < 128) + *ret = c; + else { + if (!ret_unicode) + return -EINVAL; + + *ret = 0; + *ret_unicode = c; + } + + r = 9; + break; + } + + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': { + /* octal encoding */ + int a, b, c; + uint32_t m; + + if (length != (size_t) -1 && length < 4) + return -EINVAL; + + a = unoctchar(p[0]); + if (a < 0) + return -EINVAL; + + b = unoctchar(p[1]); + if (b < 0) + return -EINVAL; + + c = unoctchar(p[2]); + if (c < 0) + return -EINVAL; + + /* don't allow NUL bytes */ + if (a == 0 && b == 0 && c == 0) + return -EINVAL; + + /* Don't allow bytes above 255 */ + m = ((uint32_t) a << 6U) | ((uint32_t) b << 3U) | (uint32_t) c; + if (m > 255) + return -EINVAL; + + *ret = m; + r = 3; + break; + } + + default: + return -EINVAL; + } + + return r; +} + char *xescape(const char *s, const char *bad) { char *r, *t; const char *f; @@ -681,6 +906,44 @@ int loop_read_exact(int fd, void *buf, size_t nbytes, bool do_poll) { return 0; } +int loop_write(int fd, const void *buf, size_t nbytes, bool do_poll) { + const uint8_t *p = buf; + + assert(fd >= 0); + assert(buf); + + errno = 0; + + do { + ssize_t k; + + k = write(fd, p, nbytes); + if (k < 0) { + if (errno == EINTR) + continue; + + if (errno == EAGAIN && do_poll) { + /* We knowingly ignore any return value here, + * and expect that any error/EOF is reported + * via write() */ + + fd_wait_for_event(fd, POLLOUT, USEC_INFINITY); + continue; + } + + return -errno; + } + + if (nbytes > 0 && k == 0) /* Can't really happen */ + return -EIO; + + p += k; + nbytes -= k; + } while (nbytes > 0); + + return 0; +} + char* dirname_malloc(const char *path) { char *d, *dir, *dir2; @@ -1328,6 +1591,45 @@ int proc_cmdline(char **ret) { return read_one_line_file("/proc/cmdline", ret); } +int parse_proc_cmdline(int (*parse_item)(const char *key, const char *value)) { + _cleanup_free_ char *line = NULL; + const char *p; + int r; + + assert(parse_item); + + r = proc_cmdline(&line); + if (r < 0) + return r; + + p = line; + for (;;) { + _cleanup_free_ char *word = NULL; + char *value = NULL; + + r = unquote_first_word(&p, &word, UNQUOTE_RELAX); + if (r < 0) + return r; + if (r == 0) + break; + + /* Filter out arguments that are intended only for the + * initrd */ + if (!in_initrd() && startswith(word, "rd.")) + continue; + + value = strchr(word, '='); + if (value) + *(value++) = 0; + + r = parse_item(word, value); + if (r < 0) + return r; + } + + return 0; +} + int getpeercred(int fd, struct ucred *ucred) { socklen_t n = sizeof(struct ucred); struct ucred u; @@ -1433,6 +1735,211 @@ int is_dir(const char* path, bool follow) { return !!S_ISDIR(st.st_mode); } +int unquote_first_word(const char **p, char **ret, UnquoteFlags flags) { + _cleanup_free_ char *s = NULL; + size_t allocated = 0, sz = 0; + int r; + + enum { + START, + VALUE, + VALUE_ESCAPE, + SINGLE_QUOTE, + SINGLE_QUOTE_ESCAPE, + DOUBLE_QUOTE, + DOUBLE_QUOTE_ESCAPE, + SPACE, + } state = START; + + assert(p); + assert(*p); + assert(ret); + + /* Parses the first word of a string, and returns it in + * *ret. Removes all quotes in the process. When parsing fails + * (because of an uneven number of quotes or similar), leaves + * the pointer *p at the first invalid character. */ + + for (;;) { + char c = **p; + + switch (state) { + + case START: + if (c == 0) + goto finish; + else if (strchr(WHITESPACE, c)) + break; + + state = VALUE; + /* fallthrough */ + + case VALUE: + if (c == 0) + goto finish; + else if (c == '\'') + state = SINGLE_QUOTE; + else if (c == '\\') + state = VALUE_ESCAPE; + else if (c == '\"') + state = DOUBLE_QUOTE; + else if (strchr(WHITESPACE, c)) + state = SPACE; + else { + if (!GREEDY_REALLOC(s, allocated, sz+2)) + return -ENOMEM; + + s[sz++] = c; + } + + break; + + case VALUE_ESCAPE: + if (c == 0) { + if (flags & UNQUOTE_RELAX) + goto finish; + return -EINVAL; + } + + if (!GREEDY_REALLOC(s, allocated, sz+7)) + return -ENOMEM; + + if (flags & UNQUOTE_CUNESCAPE) { + uint32_t u; + + r = cunescape_one(*p, (size_t) -1, &c, &u); + if (r < 0) + return -EINVAL; + + (*p) += r - 1; + + if (c != 0) + s[sz++] = c; /* normal explicit char */ + else + sz += utf8_encode_unichar(s + sz, u); /* unicode chars we'll encode as utf8 */ + } else + s[sz++] = c; + + state = VALUE; + break; + + case SINGLE_QUOTE: + if (c == 0) { + if (flags & UNQUOTE_RELAX) + goto finish; + return -EINVAL; + } else if (c == '\'') + state = VALUE; + else if (c == '\\') + state = SINGLE_QUOTE_ESCAPE; + else { + if (!GREEDY_REALLOC(s, allocated, sz+2)) + return -ENOMEM; + + s[sz++] = c; + } + + break; + + case SINGLE_QUOTE_ESCAPE: + if (c == 0) { + if (flags & UNQUOTE_RELAX) + goto finish; + return -EINVAL; + } + + if (!GREEDY_REALLOC(s, allocated, sz+7)) + return -ENOMEM; + + if (flags & UNQUOTE_CUNESCAPE) { + uint32_t u; + + r = cunescape_one(*p, (size_t) -1, &c, &u); + if (r < 0) + return -EINVAL; + + (*p) += r - 1; + + if (c != 0) + s[sz++] = c; + else + sz += utf8_encode_unichar(s + sz, u); + } else + s[sz++] = c; + + state = SINGLE_QUOTE; + break; + + case DOUBLE_QUOTE: + if (c == 0) + return -EINVAL; + else if (c == '\"') + state = VALUE; + else if (c == '\\') + state = DOUBLE_QUOTE_ESCAPE; + else { + if (!GREEDY_REALLOC(s, allocated, sz+2)) + return -ENOMEM; + + s[sz++] = c; + } + + break; + + case DOUBLE_QUOTE_ESCAPE: + if (c == 0) { + if (flags & UNQUOTE_RELAX) + goto finish; + return -EINVAL; + } + + if (!GREEDY_REALLOC(s, allocated, sz+7)) + return -ENOMEM; + + if (flags & UNQUOTE_CUNESCAPE) { + uint32_t u; + + r = cunescape_one(*p, (size_t) -1, &c, &u); + if (r < 0) + return -EINVAL; + + (*p) += r - 1; + + if (c != 0) + s[sz++] = c; + else + sz += utf8_encode_unichar(s + sz, u); + } else + s[sz++] = c; + + state = DOUBLE_QUOTE; + break; + + case SPACE: + if (c == 0) + goto finish; + if (!strchr(WHITESPACE, c)) + goto finish; + + break; + } + + (*p) ++; + } + +finish: + if (!s) { + *ret = NULL; + return 0; + } + + s[sz] = 0; + *ret = s; + s = NULL; + + return 1; +} + int execute_command(const char *command, char *const argv[]) { pid_t pid; diff --git a/src/shared/util.h b/src/shared/util.h index 2aa8da0989..e9178b9bd3 100644 --- a/src/shared/util.h +++ b/src/shared/util.h @@ -140,7 +140,9 @@ char *truncate_nl(char *s); int rmdir_parents(const char *path, const char *stop); char hexchar(int x) _const_; +int unhexchar(char c) _const_; char octchar(int x) _const_; +int unoctchar(char c) _const_; char *cescape(const char *s); size_t cescape_char(char c, char *buf); @@ -219,6 +221,7 @@ int fopen_temporary(const char *path, FILE **_f, char **_temp_path); ssize_t loop_read(int fd, void *buf, size_t nbytes, bool do_poll); int loop_read_exact(int fd, void *buf, size_t nbytes, bool do_poll); +int loop_write(int fd, const void *buf, size_t nbytes, bool do_poll); char* dirname_malloc(const char *path); @@ -383,13 +386,15 @@ int unlink_noerrno(const char *path); static inline void qsort_safe(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *)) { - if (nmemb) { - assert(base); - qsort(base, nmemb, size, compar); - } + if (nmemb <= 1) + return; + + assert(base); + qsort(base, nmemb, size, compar); } int proc_cmdline(char **ret); +int parse_proc_cmdline(int (*parse_word)(const char *key, const char *value)); int getpeercred(int fd, struct ucred *ucred); #if HAVE_DECL_MKOSTEMP @@ -408,6 +413,13 @@ int tempfn_xxxxxx(const char *p, char **ret); int is_dir(const char *path, bool follow); +typedef enum UnquoteFlags { + UNQUOTE_RELAX = 1, + UNQUOTE_CUNESCAPE = 2, +} UnquoteFlags; + +int unquote_first_word(const char **p, char **ret, UnquoteFlags flags); + int execute_command(const char *command, char *const argv[]); #define INOTIFY_EVENT_MAX (sizeof(struct inotify_event) + NAME_MAX + 1) |