X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Futil.c;h=75ad56fecf2f3e45d0bd193b59bde6746b74e365;hp=38d630e6a0f7061c76ca277ab20d6cc3ba331bc7;hb=ac1234459056864aeb04053fdfe9b0001fc32590;hpb=f9b9232be9db82cc729a56a2e99ecb27be546aac diff --git a/src/util.c b/src/util.c index 38d630e6a..75ad56fec 100644 --- a/src/util.c +++ b/src/util.c @@ -50,6 +50,7 @@ #include #include #include +#include #include "macro.h" #include "util.h" @@ -61,6 +62,20 @@ #include "exit-status.h" #include "hashmap.h" +size_t page_size(void) { + static __thread size_t pgsz = 0; + long r; + + if (pgsz) + return pgsz; + + assert_se((r = sysconf(_SC_PAGESIZE)) > 0); + + pgsz = (size_t) r; + + return pgsz; +} + bool streq_ptr(const char *a, const char *b) { /* Like streq(), but tries to make sense of NULL pointers */ @@ -438,7 +453,7 @@ char **split_path_and_make_absolute(const char *p) { int get_parent_of_pid(pid_t pid, pid_t *_ppid) { int r; FILE *f; - char fn[132], line[256], *p; + char fn[PATH_MAX], line[LINE_MAX], *p; long unsigned ppid; assert(pid >= 0); @@ -499,7 +514,16 @@ int write_one_line_file(const char *fn, const char *line) { if (!endswith(line, "\n")) fputc('\n', f); - r = 0; + fflush(f); + + if (ferror(f)) { + if (errno != 0) + r = -errno; + else + r = -EIO; + } else + r = 0; + finish: fclose(f); return r; @@ -2040,7 +2064,7 @@ int chvt(int vt) { int read_one_char(FILE *f, char *ret, bool *need_nl) { struct termios old_termios, new_termios; char c; - char line[1024]; + char line[LINE_MAX]; assert(f); assert(ret); @@ -2248,7 +2272,7 @@ int flush_fd(int fd) { pollfd.events = POLLIN; for (;;) { - char buf[1024]; + char buf[LINE_MAX]; ssize_t l; int r; @@ -2874,7 +2898,7 @@ int getttyname_harder(int fd, char **r) { int get_ctty_devnr(dev_t *d) { int k; - char line[256], *p; + char line[LINE_MAX], *p; unsigned long ttynr; FILE *f; @@ -2909,7 +2933,7 @@ int get_ctty_devnr(dev_t *d) { int get_ctty(char **r, dev_t *_devnr) { int k; - char fn[128], *s, *b, *p; + char fn[PATH_MAX], *s, *b, *p; dev_t devnr; assert(r); @@ -3524,7 +3548,7 @@ int touch(const char *path) { assert(path); - if ((fd = open(path, O_WRONLY|O_CREAT|O_CLOEXEC|O_NOCTTY, 0666)) < 0) + if ((fd = open(path, O_WRONLY|O_CREAT|O_CLOEXEC|O_NOCTTY, 0644)) < 0) return -errno; close_nointr_nofail(fd); @@ -3568,7 +3592,6 @@ char *normalize_env_assignment(const char *s) { free(p); if (!value) { - free(p); free(name); return NULL; } @@ -3945,33 +3968,32 @@ int detect_vm(const char **id) { return 0; } -/* Returns a short identifier for the various VM/container implementations */ -int detect_virtualization(const char **id) { - int r; - static __thread const char *cached_id = NULL; - const char *_id; +int detect_container(const char **id) { FILE *f; - if (cached_id) { + /* Unfortunately many of these operations require root access + * in one way or another */ - if (cached_id == (const char*) -1) - return 0; + if (geteuid() != 0) + return -EPERM; + + if (running_in_chroot() > 0) { if (id) - *id = cached_id; + *id = "chroot"; return 1; } - /* Unfortunately most of these operations require root access - * in one way or another */ - if (geteuid() != 0) - return -EPERM; + /* /proc/vz exists in container and outside of the container, + * /proc/bc only outside of the container. */ + if (access("/proc/vz", F_OK) >= 0 && + access("/proc/bc", F_OK) < 0) { - if ((r = running_in_chroot()) > 0) { - _id = "chroot"; - r = 1; - goto finish; + if (id) + *id = "openvz"; + + return 1; } if ((f = fopen("/proc/self/cgroup", "r"))) { @@ -3991,36 +4013,49 @@ int detect_virtualization(const char **id) { if (!streq(p, ":ns:/")) { fclose(f); - r = 1; - _id = "ns"; - goto finish; + if (id) + *id = "pidns"; + + return 1; } } fclose(f); } - /* /proc/vz exists in container and outside of the container, - * /proc/bc only outside of the container. */ - if (access("/proc/vz", F_OK) >= 0 && - access("/proc/bc", F_OK) < 0) { - _id = "openvz"; - r = 1; - goto finish; + return 0; +} + +/* Returns a short identifier for the various VM/container implementations */ +int detect_virtualization(const char **id) { + static __thread const char *cached_id = NULL; + const char *_id; + int r; + + if (cached_id) { + + if (cached_id == (const char*) -1) + return 0; + + if (id) + *id = cached_id; + + return 1; } + if ((r = detect_container(&_id)) != 0) + goto finish; + r = detect_vm(&_id); finish: - if (r < 0) - return r; - else if (r > 0) + if (r > 0) { cached_id = _id; - else - cached_id = (const char*) -1; - if (id) - *id = _id; + if (id) + *id = _id; + } else if (r == 0) + cached_id = (const char*) -1; return r; } @@ -4162,6 +4197,63 @@ bool nulstr_contains(const char*nulstr, const char *needle) { return false; } +bool plymouth_running(void) { + return access("/run/plymouth/pid", F_OK) >= 0; +} + +void parse_syslog_priority(char **p, int *priority) { + int a = 0, b = 0, c = 0; + int k; + + assert(p); + assert(*p); + assert(priority); + + if ((*p)[0] != '<') + return; + + if (!strchr(*p, '>')) + return; + + if ((*p)[2] == '>') { + c = undecchar((*p)[1]); + k = 3; + } else if ((*p)[3] == '>') { + b = undecchar((*p)[1]); + c = undecchar((*p)[2]); + k = 4; + } else if ((*p)[4] == '>') { + a = undecchar((*p)[1]); + b = undecchar((*p)[2]); + c = undecchar((*p)[3]); + k = 5; + } else + return; + + if (a < 0 || b < 0 || c < 0) + return; + + *priority = a*100+b*10+c; + *p += k; +} + +int have_effective_cap(int value) { + cap_t cap; + cap_flag_value_t fv; + int r; + + if (!(cap = cap_get_proc())) + return -errno; + + if (cap_get_flag(cap, value, CAP_EFFECTIVE, &fv) < 0) + r = -errno; + else + r = fv == CAP_SET; + + cap_free(cap); + return r; +} + static const char *const ioprio_class_table[] = { [IOPRIO_CLASS_NONE] = "none", [IOPRIO_CLASS_RT] = "realtime", @@ -4182,7 +4274,7 @@ static const char *const sigchld_code_table[] = { DEFINE_STRING_TABLE_LOOKUP(sigchld_code, int); -static const char *const log_facility_table[LOG_NFACILITIES] = { +static const char *const log_facility_unshifted_table[LOG_NFACILITIES] = { [LOG_FAC(LOG_KERN)] = "kern", [LOG_FAC(LOG_USER)] = "user", [LOG_FAC(LOG_MAIL)] = "mail", @@ -4205,7 +4297,7 @@ static const char *const log_facility_table[LOG_NFACILITIES] = { [LOG_FAC(LOG_LOCAL7)] = "local7" }; -DEFINE_STRING_TABLE_LOOKUP(log_facility, int); +DEFINE_STRING_TABLE_LOOKUP(log_facility_unshifted, int); static const char *const log_level_table[] = { [LOG_EMERG] = "emerg",