From: Sven Eden Date: Wed, 30 Aug 2017 05:49:50 +0000 (+0200) Subject: Prep v235: Apply pending upstream updates in src/basic [1/4] X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=311b1ceb2cc3e69130c46b0485b2c55a1b166d62;p=elogind.git Prep v235: Apply pending upstream updates in src/basic [1/4] --- diff --git a/src/basic/capability-util.c b/src/basic/capability-util.c index 952bcc2d7..294dcc8d4 100644 --- a/src/basic/capability-util.c +++ b/src/basic/capability-util.c @@ -372,4 +372,19 @@ int drop_capability(cap_value_t cv) { return 0; } + +bool ambient_capabilities_supported(void) { + static int cache = -1; + + if (cache >= 0) + return cache; + + /* If PR_CAP_AMBIENT returns something valid, or an unexpected error code we assume that ambient caps are + * available. */ + + cache = prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_IS_SET, CAP_KILL, 0, 0) >= 0 || + !IN_SET(errno, EINVAL, EOPNOTSUPP, ENOSYS); + + return cache; +} #endif // 0 diff --git a/src/basic/capability-util.h b/src/basic/capability-util.h index 913048e4c..feaa3735e 100644 --- a/src/basic/capability-util.h +++ b/src/basic/capability-util.h @@ -57,3 +57,7 @@ static inline bool cap_test_all(uint64_t caps) { m = (UINT64_C(1) << (cap_last_cap() + 1)) - 1; return (caps & m) == m; } + +#if 0 /// UNNEEDED by elogind +bool ambient_capabilities_supported(void); +#endif // 0 diff --git a/src/basic/fileio.c b/src/basic/fileio.c index 13b2e312f..7edc31d0a 100644 --- a/src/basic/fileio.c +++ b/src/basic/fileio.c @@ -70,7 +70,7 @@ int write_string_stream_ts(FILE *f, const char *line, bool enforce_newline, stru return fflush_and_check(f); } -static int write_string_file_atomic(const char *fn, const char *line, bool enforce_newline, bool do_fsync) { +static int write_string_file_atomic(const char *fn, const char *line, bool enforce_newline) { _cleanup_fclose_ FILE *f = NULL; _cleanup_free_ char *p = NULL; int r; @@ -85,9 +85,6 @@ static int write_string_file_atomic(const char *fn, const char *line, bool enfor (void) fchmod_umask(fileno(f), 0644); r = write_string_stream(f, line, enforce_newline); - if (r >= 0 && do_fsync) - r = fflush_sync_and_check(f); - if (r >= 0) { if (rename(p, fn) < 0) r = -errno; @@ -106,14 +103,10 @@ int write_string_file_ts(const char *fn, const char *line, WriteStringFileFlags assert(fn); assert(line); - /* We don't know how to verify whether the file contents was already on-disk. */ - assert(!((flags & WRITE_STRING_FILE_VERIFY_ON_FAILURE) && (flags & WRITE_STRING_FILE_SYNC))); - if (flags & WRITE_STRING_FILE_ATOMIC) { assert(flags & WRITE_STRING_FILE_CREATE); - r = write_string_file_atomic(fn, line, !(flags & WRITE_STRING_FILE_AVOID_NEWLINE), - flags & WRITE_STRING_FILE_SYNC); + r = write_string_file_atomic(fn, line, !(flags & WRITE_STRING_FILE_AVOID_NEWLINE)); if (r < 0) goto fail; @@ -150,12 +143,6 @@ int write_string_file_ts(const char *fn, const char *line, WriteStringFileFlags if (r < 0) goto fail; - if (flags & WRITE_STRING_FILE_SYNC) { - r = fflush_sync_and_check(f); - if (r < 0) - return r; - } - return 0; fail: @@ -1144,21 +1131,6 @@ int fflush_and_check(FILE *f) { return 0; } -int fflush_sync_and_check(FILE *f) { - int r; - - assert(f); - - r = fflush_and_check(f); - if (r < 0) - return r; - - if (fsync(fileno(f)) < 0) - return -errno; - - return 0; -} - /* This is much like mkostemp() but is subject to umask(). */ int mkostemp_safe(char *pattern) { _cleanup_umask_ mode_t u = 0; diff --git a/src/basic/fileio.h b/src/basic/fileio.h index f76c3243e..fa223fdf5 100644 --- a/src/basic/fileio.h +++ b/src/basic/fileio.h @@ -29,11 +29,10 @@ #include "time-util.h" typedef enum { - WRITE_STRING_FILE_CREATE = 1<<0, - WRITE_STRING_FILE_ATOMIC = 1<<1, - WRITE_STRING_FILE_AVOID_NEWLINE = 1<<2, - WRITE_STRING_FILE_VERIFY_ON_FAILURE = 1<<3, - WRITE_STRING_FILE_SYNC = 1<<4, + WRITE_STRING_FILE_CREATE = 1, + WRITE_STRING_FILE_ATOMIC = 2, + WRITE_STRING_FILE_AVOID_NEWLINE = 4, + WRITE_STRING_FILE_VERIFY_ON_FAILURE = 8, } WriteStringFileFlags; int write_string_stream_ts(FILE *f, const char *line, bool enforce_newline, struct timespec *ts); @@ -84,7 +83,6 @@ int search_and_fopen_nulstr(const char *path, const char *mode, const char *root } else int fflush_and_check(FILE *f); -int fflush_sync_and_check(FILE *f); int fopen_temporary(const char *path, FILE **_f, char **_temp_path); int mkostemp_safe(char *pattern); diff --git a/src/basic/path-util.h b/src/basic/path-util.h index 3c0dbb321..0d162bac2 100644 --- a/src/basic/path-util.h +++ b/src/basic/path-util.h @@ -157,3 +157,13 @@ int systemd_installation_has_version(const char *root, unsigned minimal_version) #endif // 0 bool dot_or_dot_dot(const char *path); + +static inline const char *skip_dev_prefix(const char *p) { + const char *e; + + /* Drop any /dev prefix if there is any */ + + e = path_startswith(p, "/dev/"); + + return e ?: p; +} diff --git a/src/basic/terminal-util.c b/src/basic/terminal-util.c index 35c1a68df..f9d62978b 100644 --- a/src/basic/terminal-util.c +++ b/src/basic/terminal-util.c @@ -45,6 +45,7 @@ #include "io-util.h" #include "log.h" #include "macro.h" +#include "path-util.h" #include "parse-util.h" #include "process-util.h" #include "socket-util.h" @@ -560,6 +561,7 @@ int terminal_vhangup(const char *name) { int vt_disallocate(const char *name) { _cleanup_close_ int fd = -1; + const char *e, *n; unsigned u; int r; @@ -567,7 +569,8 @@ int vt_disallocate(const char *name) { * (i.e. because it is the active one), at least clear it * entirely (including the scrollback buffer) */ - if (!startswith(name, "/dev/")) + e = path_startswith(name, "/dev/"); + if (!e) return -EINVAL; if (!tty_is_vc(name)) { @@ -586,10 +589,11 @@ int vt_disallocate(const char *name) { return 0; } - if (!startswith(name, "/dev/tty")) + n = startswith(e, "tty"); + if (!n) return -EINVAL; - r = safe_atou(name+8, &u); + r = safe_atou(n, &u); if (r < 0) return r; @@ -654,10 +658,7 @@ bool tty_is_vc(const char *tty) { bool tty_is_console(const char *tty) { assert(tty); - if (startswith(tty, "/dev/")) - tty += 5; - - return streq(tty, "console"); + return streq(skip_dev_prefix(tty), "console"); } int vtnr_from_tty(const char *tty) { @@ -665,8 +666,7 @@ int vtnr_from_tty(const char *tty) { assert(tty); - if (startswith(tty, "/dev/")) - tty += 5; + tty = skip_dev_prefix(tty); if (!startswith(tty, "tty") ) return -EINVAL; @@ -781,8 +781,7 @@ bool tty_is_vc_resolve(const char *tty) { assert(tty); - if (startswith(tty, "/dev/")) - tty += 5; + tty = skip_dev_prefix(tty); if (streq(tty, "console")) { tty = resolve_dev_console(&active); @@ -927,11 +926,9 @@ int getttyname_malloc(int fd, char **ret) { r = ttyname_r(fd, path, sizeof(path)); if (r == 0) { - const char *p; char *c; - p = startswith(path, "/dev/"); - c = strdup(p ?: path); + c = strdup(skip_dev_prefix(path)); if (!c) return -ENOMEM;