diff options
author | Lennart Poettering <lennart@poettering.net> | 2013-04-04 03:39:39 +0200 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2013-04-04 03:39:39 +0200 |
commit | 5c0d398dfc4d79df2209515d28cafd9dc129838e (patch) | |
tree | 5ee8d20442689e6e50e3747e95ae939ce3815957 /src/core | |
parent | 2fa4092c2829dd14e50c430ae2f23551d23c6c1d (diff) |
util: add a bit of syntactic sugar to run short code fragments with a different umask
Diffstat (limited to 'src/core')
-rw-r--r-- | src/core/machine-id-setup.c | 43 | ||||
-rw-r--r-- | src/core/manager.c | 14 |
2 files changed, 25 insertions, 32 deletions
diff --git a/src/core/machine-id-setup.c b/src/core/machine-id-setup.c index 51074fea44..fbba3aab78 100644 --- a/src/core/machine-id-setup.c +++ b/src/core/machine-id-setup.c @@ -155,31 +155,27 @@ int machine_id_setup(void) { bool writable; struct stat st; char id[34]; /* 32 + \n + \0 */ - mode_t m; - m = umask(0000); - - /* We create this 0444, to indicate that this isn't really - * something you should ever modify. Of course, since the file - * will be owned by root it doesn't matter much, but maybe - * people look. */ + RUN_WITH_UMASK(0000) { + /* We create this 0444, to indicate that this isn't really + * something you should ever modify. Of course, since the file + * will be owned by root it doesn't matter much, but maybe + * people look. */ + + fd = open("/etc/machine-id", O_RDWR|O_CREAT|O_CLOEXEC|O_NOCTTY, 0444); + if (fd >= 0) + writable = true; + else { + fd = open("/etc/machine-id", O_RDONLY|O_CLOEXEC|O_NOCTTY); + if (fd < 0) { + log_error("Cannot open /etc/machine-id: %m"); + return -errno; + } - fd = open("/etc/machine-id", O_RDWR|O_CREAT|O_CLOEXEC|O_NOCTTY, 0444); - if (fd >= 0) - writable = true; - else { - fd = open("/etc/machine-id", O_RDONLY|O_CLOEXEC|O_NOCTTY); - if (fd < 0) { - umask(m); - log_error("Cannot open /etc/machine-id: %m"); - return -errno; + writable = false; } - - writable = false; } - umask(m); - if (fstat(fd, &st) < 0) { log_error("fstat() failed: %m"); r = -errno; @@ -215,10 +211,9 @@ int machine_id_setup(void) { /* Hmm, we couldn't write it? So let's write it to * /run/machine-id as a replacement */ - m = umask(0022); - r = write_string_file("/run/machine-id", id); - umask(m); - + RUN_WITH_UMASK(0022) { + r = write_string_file("/run/machine-id", id); + } if (r < 0) { log_error("Cannot write /run/machine-id: %s", strerror(-r)); diff --git a/src/core/manager.c b/src/core/manager.c index f90ccd5b5f..e26522a4dd 100644 --- a/src/core/manager.c +++ b/src/core/manager.c @@ -1984,7 +1984,6 @@ void manager_dispatch_bus_query_pid_done( int manager_open_serialization(Manager *m, FILE **_f) { char *path = NULL; - mode_t saved_umask; int fd; FILE *f; @@ -1998,9 +1997,9 @@ int manager_open_serialization(Manager *m, FILE **_f) { if (!path) return -ENOMEM; - saved_umask = umask(0077); - fd = mkostemp(path, O_RDWR|O_CLOEXEC); - umask(saved_umask); + RUN_WITH_UMASK(0077) { + fd = mkostemp(path, O_RDWR|O_CLOEXEC); + } if (fd < 0) { free(path); @@ -2515,7 +2514,6 @@ void manager_run_generators(Manager *m) { DIR *d = NULL; const char *generator_path; const char *argv[5]; - mode_t u; int r; assert(m); @@ -2549,9 +2547,9 @@ void manager_run_generators(Manager *m) { argv[3] = m->generator_unit_path_late; argv[4] = NULL; - u = umask(0022); - execute_directory(generator_path, d, (char**) argv); - umask(u); + RUN_WITH_UMASK(0022) { + execute_directory(generator_path, d, (char**) argv); + } trim_generator_dir(m, &m->generator_unit_path); trim_generator_dir(m, &m->generator_unit_path_early); |