X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Futil.c;h=ed0991a68b45d1a4b699f6de08ca54dcf81797e5;hp=7664df59d06359150f0a5c1a82d13b16800038ed;hb=5b6319dceedd81f3f1ce7eb70ea5defaef43bcec;hpb=01f78473b104d28db0fa813414092bc6358ae521 diff --git a/src/util.c b/src/util.c index 7664df59d..ed0991a68 100644 --- a/src/util.c +++ b/src/util.c @@ -43,6 +43,7 @@ #include #include #include +#include #include "macro.h" #include "util.h" @@ -222,6 +223,13 @@ void close_nointr_nofail(int fd) { errno = saved_errno; } +void close_many(const int fds[], unsigned n_fd) { + unsigned i; + + for (i = 0; i < n_fd; i++) + close_nointr_nofail(fds[i]); +} + int parse_boolean(const char *v) { assert(v); @@ -582,6 +590,26 @@ int readlink_malloc(const char *p, char **r) { } } +int readlink_and_make_absolute(const char *p, char **r) { + char *target, *k; + int j; + + assert(p); + assert(r); + + if ((j = readlink_malloc(p, &target)) < 0) + return j; + + k = file_in_same_dir(p, target); + free(target); + + if (!k) + return -ENOMEM; + + *r = k; + return 0; +} + char *file_name_from_path(const char *p) { char *r; @@ -2121,6 +2149,39 @@ int dir_is_empty(const char *path) { return r; } +unsigned long long random_ull(void) { + int fd; + uint64_t ull; + ssize_t r; + + if ((fd = open("/dev/urandom", O_RDONLY|O_CLOEXEC|O_NOCTTY)) < 0) + goto fallback; + + r = loop_read(fd, &ull, sizeof(ull)); + close_nointr_nofail(fd); + + if (r != sizeof(ull)) + goto fallback; + + return ull; + +fallback: + return random() * RAND_MAX + random(); +} + +void rename_process(const char name[8]) { + assert(name); + + prctl(PR_SET_NAME, name); + + /* This is a like a poor man's setproctitle(). The string + * passed should fit in 7 chars (i.e. the length of + * "systemd") */ + + if (program_invocation_name) + strncpy(program_invocation_name, name, strlen(program_invocation_name)); +} + static const char *const ioprio_class_table[] = { [IOPRIO_CLASS_NONE] = "none", [IOPRIO_CLASS_RT] = "realtime",