X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Fshared%2Futil.c;h=af6bde2c3dd280e8c3f72f090d035d8d88b32e17;hp=fe05820395e58d2fcd051ea2ac0beeab177f92ef;hb=6afeb1cfe404e8615441c8727b48343253fc731a;hpb=8f294b45cbb627d31342f6a79444be59ce7e2274 diff --git a/src/shared/util.c b/src/shared/util.c index fe0582039..af6bde2c3 100644 --- a/src/shared/util.c +++ b/src/shared/util.c @@ -280,6 +280,14 @@ 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 */ + if (uid == (uid_t) 0xFFFFFFFF) + return -EINVAL; + + /* A long time ago UIDs where 16bit, hence explicitly avoid the 16bit -1 too */ + if (uid == (uid_t) 0xFFFF) + return -EINVAL; + *ret_uid = uid; return 0; } @@ -1440,7 +1448,7 @@ _pure_ static bool fd_in_set(int fd, const int fdset[], unsigned n_fdset) { } int close_all_fds(const int except[], unsigned n_except) { - DIR *d; + _cleanup_closedir_ DIR *d = NULL; struct dirent *de; int r = 0; @@ -1495,7 +1503,6 @@ int close_all_fds(const int except[], unsigned n_except) { } } - closedir(d); return r; } @@ -1514,6 +1521,7 @@ bool fstype_is_network(const char *fstype) { static const char table[] = "cifs\0" "smbfs\0" + "sshfs\0" "ncpfs\0" "ncp\0" "nfs\0" @@ -2671,7 +2679,7 @@ finish: } int rm_rf_children_dangerous(int fd, bool only_dirs, bool honour_sticky, struct stat *root_dev) { - DIR *d; + _cleanup_closedir_ DIR *d = NULL; int ret = 0; assert(fd >= 0); @@ -2694,15 +2702,12 @@ int rm_rf_children_dangerous(int fd, bool only_dirs, bool honour_sticky, struct errno = 0; de = readdir(d); - if (!de && errno != 0) { - if (ret == 0) + if (!de) { + if (errno != 0 && ret == 0) ret = -errno; - break; + return ret; } - if (!de) - break; - if (streq(de->d_name, ".") || streq(de->d_name, "..")) continue; @@ -2758,10 +2763,6 @@ int rm_rf_children_dangerous(int fd, bool only_dirs, bool honour_sticky, struct } } } - - closedir(d); - - return ret; } _pure_ static int is_temporary_fs(struct statfs *s) { @@ -3619,9 +3620,6 @@ char *fstab_node_to_udev_node(const char *p) { bool tty_is_vc(const char *tty) { assert(tty); - if (startswith(tty, "/dev/")) - tty += 5; - return vtnr_from_tty(tty) >= 0; } @@ -4145,6 +4143,46 @@ int symlink_atomic(const char *from, const char *to) { return 0; } +int mknod_atomic(const char *path, mode_t mode, dev_t dev) { + _cleanup_free_ char *t = NULL; + + assert(path); + + t = tempfn_random(path); + if (!t) + return -ENOMEM; + + if (mknod(t, mode, dev) < 0) + return -errno; + + if (rename(t, path) < 0) { + unlink_noerrno(t); + return -errno; + } + + return 0; +} + +int mkfifo_atomic(const char *path, mode_t mode) { + _cleanup_free_ char *t = NULL; + + assert(path); + + t = tempfn_random(path); + if (!t) + return -ENOMEM; + + if (mkfifo(t, mode) < 0) + return -errno; + + if (rename(t, path) < 0) { + unlink_noerrno(t); + return -errno; + } + + return 0; +} + bool display_is_local(const char *display) { assert(display); @@ -5646,14 +5684,17 @@ static int search_and_fopen_internal(const char *path, const char *mode, const c assert(mode); assert(_f); - if (!path_strv_canonicalize_absolute_uniq(search, root)) + if (!path_strv_resolve_uniq(search, root)) return -ENOMEM; STRV_FOREACH(i, search) { _cleanup_free_ char *p = NULL; FILE *f; - p = strjoin(*i, "/", path, NULL); + if (root) + p = strjoin(root, *i, "/", path, NULL); + else + p = strjoin(*i, "/", path, NULL); if (!p) return -ENOMEM;