X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Fbasic%2Ffs-util.c;h=5a9b1c950f26bc1533d4f343e4d0b22487967d35;hb=1a7523a8c73ff91109c459da6f0af2fd3585e1e7;hp=942f43ae938f4d56005ce86bff35dae2e8b70974;hpb=5df5d72136c7c0b4a45a76463cb3441404f29cb4;p=elogind.git diff --git a/src/basic/fs-util.c b/src/basic/fs-util.c index 942f43ae9..5a9b1c950 100644 --- a/src/basic/fs-util.c +++ b/src/basic/fs-util.c @@ -234,6 +234,22 @@ int chmod_and_chown(const char *path, mode_t mode, uid_t uid, gid_t gid) { return 0; } +int fchmod_and_chown(int fd, mode_t mode, uid_t uid, gid_t gid) { + /* Under the assumption that we are running privileged we + * first change the access mode and only then hand out + * ownership to avoid a window where access is too open. */ + + if (mode != MODE_INVALID) + if (fchmod(fd, mode) < 0) + return -errno; + + if (uid != UID_INVALID || gid != GID_INVALID) + if (fchown(fd, uid, gid) < 0) + return -errno; + + return 0; +} + int fchmod_umask(int fd, mode_t m) { mode_t u; int r;