X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Futil.c;h=a3cfe864b6e44c5cf38e04c7c0a18386be0364e4;hb=f4b4781191e8edfb5690e4447166e3ba7bcb48f5;hp=99737e4e63012ba66f33b8b86d679f0b2a39d129;hpb=260a2be45522f03ce8d8aca38e471d7b0882ff05;p=elogind.git diff --git a/src/util.c b/src/util.c index 99737e4e6..a3cfe864b 100644 --- a/src/util.c +++ b/src/util.c @@ -3529,6 +3529,22 @@ int chmod_and_chown(const char *path, mode_t mode, uid_t uid, gid_t gid) { return 0; } +int fchmod_and_fchown(int fd, mode_t mode, uid_t uid, gid_t gid) { + assert(fd >= 0); + + /* 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 (fchmod(fd, mode) < 0) + return -errno; + + if (fchown(fd, uid, gid) < 0) + return -errno; + + return 0; +} + cpu_set_t* cpu_set_malloc(unsigned *ncpus) { cpu_set_t *r; unsigned n = 1024;