X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Fshared%2Futil.c;h=f01cdd7451f9d70f8f13a4e6c4a7229a2bbd692b;hp=d3eec5f8785588ae786d79e484cd60f7c2978025;hb=e706d931e0f5741d57c6f3752b4e3520f90f47d5;hpb=1a29929959fd8f59e19ce60c25d1a1f7d910fac0 diff --git a/src/shared/util.c b/src/shared/util.c index d3eec5f87..f01cdd745 100644 --- a/src/shared/util.c +++ b/src/shared/util.c @@ -354,7 +354,7 @@ int parse_uid(const char *s, uid_t* ret_uid) { if ((unsigned long) uid != ul) return -ERANGE; - /* Some libc APIs use (uid_t) -1 as special placeholder */ + /* Some libc APIs use UID_INVALID as special placeholder */ if (uid == (uid_t) 0xFFFFFFFF) return -ENXIO; @@ -955,7 +955,7 @@ int get_process_root(pid_t pid, char **root) { return get_process_link_contents(p, root); } -int get_process_environ(pid_t pid, char **environ) { +int get_process_environ(pid_t pid, char **env) { _cleanup_fclose_ FILE *f = NULL; _cleanup_free_ char *outcome = NULL; int c; @@ -963,7 +963,7 @@ int get_process_environ(pid_t pid, char **environ) { size_t allocated = 0, sz = 0; assert(pid >= 0); - assert(environ); + assert(env); p = procfs_file_alloca(pid, "environ"); @@ -982,7 +982,7 @@ int get_process_environ(pid_t pid, char **environ) { } outcome[sz] = '\0'; - *environ = outcome; + *env = outcome; outcome = NULL; return 0; @@ -2214,7 +2214,7 @@ int acquire_terminal( r = reset_terminal_fd(fd, true); if (r < 0) - log_warning("Failed to reset terminal: %s", strerror(-r)); + log_warning_errno(r, "Failed to reset terminal: %m"); return fd; @@ -3052,6 +3052,15 @@ _pure_ static int is_temporary_fs(struct statfs *s) { F_TYPE_EQUAL(s->f_type, RAMFS_MAGIC); } +int is_fd_on_temporary_fs(int fd) { + struct statfs s; + + if (fstatfs(fd, &s) < 0) + return -errno; + + return is_temporary_fs(&s); +} + int rm_rf_children(int fd, bool only_dirs, bool honour_sticky, struct stat *root_dev) { struct statfs s; @@ -3167,11 +3176,11 @@ int chmod_and_chown(const char *path, mode_t mode, uid_t uid, gid_t gid) { * first change the access mode and only then hand out * ownership to avoid a window where access is too open. */ - if (mode != (mode_t) -1) + if (mode != MODE_INVALID) if (chmod(path, mode) < 0) return -errno; - if (uid != (uid_t) -1 || gid != (gid_t) -1) + if (uid != UID_INVALID || gid != GID_INVALID) if (chown(path, uid, gid) < 0) return -errno; @@ -3185,11 +3194,11 @@ int fchmod_and_fchown(int fd, mode_t mode, uid_t uid, gid_t gid) { * first change the access mode and only then hand out * ownership to avoid a window where access is too open. */ - if (mode != (mode_t) -1) + if (mode != MODE_INVALID) if (fchmod(fd, mode) < 0) return -errno; - if (uid != (uid_t) -1 || gid != (gid_t) -1) + if (uid != UID_INVALID || gid != GID_INVALID) if (fchown(fd, uid, gid) < 0) return -errno; @@ -3680,7 +3689,7 @@ int touch_file(const char *path, bool parents, usec_t stamp, uid_t uid, gid_t gi return -errno; } - if (uid != (uid_t) -1 || gid != (gid_t) -1) { + if (uid != UID_INVALID || gid != GID_INVALID) { r = fchown(fd, uid, gid); if (r < 0) return -errno; @@ -3701,7 +3710,7 @@ int touch_file(const char *path, bool parents, usec_t stamp, uid_t uid, gid_t gi } int touch(const char *path) { - return touch_file(path, false, USEC_INFINITY, (uid_t) -1, (gid_t) -1, 0); + return touch_file(path, false, USEC_INFINITY, UID_INVALID, GID_INVALID, 0); } char *unquote(const char *s, const char* quotes) { @@ -3786,8 +3795,11 @@ int wait_for_terminate(pid_t pid, siginfo_t *status) { * * That is, success is indicated by a return value of zero, and an * error is indicated by a non-zero value. + * + * A warning is emitted if the process terminates abnormally, + * and also if it returns non-zero unless check_exit_code is true. */ -int wait_for_terminate_and_warn(const char *name, pid_t pid) { +int wait_for_terminate_and_warn(const char *name, pid_t pid, bool check_exit_code) { int r; siginfo_t status; @@ -3795,20 +3807,17 @@ int wait_for_terminate_and_warn(const char *name, pid_t pid) { assert(pid > 1); r = wait_for_terminate(pid, &status); - if (r < 0) { - log_warning("Failed to wait for %s: %s", name, strerror(-r)); - return r; - } + if (r < 0) + return log_warning_errno(r, "Failed to wait for %s: %m", name); if (status.si_code == CLD_EXITED) { - if (status.si_status != 0) { - log_warning("%s failed with error code %i.", name, status.si_status); - return status.si_status; - } - - log_debug("%s succeeded.", name); - return 0; + if (status.si_status != 0) + log_full(check_exit_code ? LOG_WARNING : LOG_DEBUG, + "%s failed with error code %i.", name, status.si_status); + else + log_debug("%s succeeded.", name); + return status.si_status; } else if (status.si_code == CLD_KILLED || status.si_code == CLD_DUMPED) { @@ -4067,7 +4076,7 @@ void execute_directory(const char *directory, DIR *d, usec_t timeout, char *argv executor_pid = fork(); if (executor_pid < 0) { - log_error("Failed to fork: %m"); + log_error_errno(errno, "Failed to fork: %m"); return; } else if (executor_pid == 0) { @@ -4090,7 +4099,7 @@ void execute_directory(const char *directory, DIR *d, usec_t timeout, char *argv if (errno == ENOENT) _exit(EXIT_SUCCESS); - log_error("Failed to enumerate directory %s: %m", directory); + log_error_errno(errno, "Failed to enumerate directory %s: %m", directory); _exit(EXIT_FAILURE); } } @@ -4116,7 +4125,7 @@ void execute_directory(const char *directory, DIR *d, usec_t timeout, char *argv pid = fork(); if (pid < 0) { - log_error("Failed to fork: %m"); + log_error_errno(errno, "Failed to fork: %m"); continue; } else if (pid == 0) { char *_argv[2]; @@ -4131,7 +4140,7 @@ void execute_directory(const char *directory, DIR *d, usec_t timeout, char *argv argv[0] = path; execv(path, argv); - log_error("Failed to execute %s: %m", path); + log_error_errno(errno, "Failed to execute %s: %m", path); _exit(EXIT_FAILURE); } @@ -4163,13 +4172,13 @@ void execute_directory(const char *directory, DIR *d, usec_t timeout, char *argv path = hashmap_remove(pids, UINT_TO_PTR(pid)); assert(path); - wait_for_terminate_and_warn(path, pid); + wait_for_terminate_and_warn(path, pid, true); } _exit(EXIT_SUCCESS); } - wait_for_terminate_and_warn(directory, executor_pid); + wait_for_terminate_and_warn(directory, executor_pid, true); } int kill_and_sigcont(pid_t pid, int sig) { @@ -5323,7 +5332,7 @@ int fork_agent(pid_t *pid, const int except[], unsigned n_except, const char *pa * keep an unused copy of stdin around. */ fd = open("/dev/tty", O_WRONLY); if (fd < 0) { - log_error("Failed to open /dev/tty: %m"); + log_error_errno(errno, "Failed to open /dev/tty: %m"); _exit(EXIT_FAILURE); } @@ -5506,16 +5515,12 @@ int make_console_stdio(void) { /* Make /dev/console the controlling terminal and stdin/stdout/stderr */ fd = acquire_terminal("/dev/console", false, true, true, USEC_INFINITY); - if (fd < 0) { - log_error("Failed to acquire terminal: %s", strerror(-fd)); - return fd; - } + if (fd < 0) + return log_error_errno(fd, "Failed to acquire terminal: %m"); r = make_stdio(fd); - if (r < 0) { - log_error("Failed to duplicate terminal fd: %s", strerror(-r)); - return r; - } + if (r < 0) + return log_error_errno(r, "Failed to duplicate terminal fd: %m"); return 0; } @@ -6553,9 +6558,9 @@ int getpeercred(int fd, struct ucred *ucred) { * to namespacing issues */ if (u.pid <= 0) return -ENODATA; - if (u.uid == (uid_t) -1) + if (u.uid == UID_INVALID) return -ENODATA; - if (u.gid == (gid_t) -1) + if (u.gid == GID_INVALID) return -ENODATA; *ucred = u;