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
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
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;
(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;
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;
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:
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;
#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);
} 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);
#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;
+}
#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"
int vt_disallocate(const char *name) {
_cleanup_close_ int fd = -1;
+ const char *e, *n;
unsigned u;
int r;
* (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)) {
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;
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) {
assert(tty);
- if (startswith(tty, "/dev/"))
- tty += 5;
+ tty = skip_dev_prefix(tty);
if (!startswith(tty, "tty") )
return -EINVAL;
assert(tty);
- if (startswith(tty, "/dev/"))
- tty += 5;
+ tty = skip_dev_prefix(tty);
if (streq(tty, "console")) {
tty = resolve_dev_console(&active);
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;