X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Futil.c;h=8d54049754c0495b2d4394483c9d2825fcc84f73;hb=3bfc71846c3eac9011f0182b0c8d5639a9549228;hp=2d4f2291932963fdfade7286741efe0d6d49deee;hpb=034a2a52ac0ec83e0229941d635d310b23eb04df;p=elogind.git diff --git a/src/util.c b/src/util.c index 2d4f22919..8d5404975 100644 --- a/src/util.c +++ b/src/util.c @@ -54,6 +54,7 @@ #include #include #include +#include #include "macro.h" #include "util.h" @@ -72,7 +73,7 @@ size_t page_size(void) { static __thread size_t pgsz = 0; long r; - if (pgsz) + if (_likely_(pgsz)) return pgsz; assert_se((r = sysconf(_SC_PAGESIZE)) > 0); @@ -808,7 +809,7 @@ int parse_env_file( const char *separator, ...) { int r = 0; - char *contents, *p; + char *contents = NULL, *p; assert(fname); assert(separator); @@ -1265,8 +1266,6 @@ bool is_path(const char *p) { } char *path_make_absolute(const char *p, const char *prefix) { - char *r; - assert(p); /* Makes every item in the list an absolute path by prepending @@ -1275,10 +1274,7 @@ char *path_make_absolute(const char *p, const char *prefix) { if (path_is_absolute(p) || !prefix) return strdup(p); - if (asprintf(&r, "%s/%s", prefix, p) < 0) - return NULL; - - return r; + return join(prefix, "/", p, NULL); } char *path_make_absolute_cwd(const char *p) { @@ -1410,21 +1406,18 @@ int reset_all_signal_handlers(void) { } char *strstrip(char *s) { - char *e, *l = NULL; + char *e; /* Drops trailing whitespace. Modifies the string in * place. Returns pointer to first non-space character */ s += strspn(s, WHITESPACE); - for (e = s; *e; e++) - if (!strchr(WHITESPACE, *e)) - l = e; + for (e = strchr(s, 0); e > s; e --) + if (!strchr(WHITESPACE, e[-1])) + break; - if (l) - *(l+1) = 0; - else - *s = 0; + *e = 0; return s; } @@ -3775,7 +3768,7 @@ int columns(void) { static __thread int parsed_columns = 0; const char *e; - if (parsed_columns > 0) + if (_likely_(parsed_columns > 0)) return parsed_columns; if ((e = getenv("COLUMNS"))) @@ -4371,7 +4364,7 @@ int detect_virtualization(const char **id) { const char *_id; int r; - if (cached_id) { + if (_likely_(cached_id)) { if (cached_id == (const char*) -1) return 0; @@ -5262,28 +5255,31 @@ int socket_from_display(const char *display, char **path) { int get_user_creds(const char **username, uid_t *uid, gid_t *gid, const char **home) { struct passwd *p; - unsigned long lu; + uid_t u; assert(username); assert(*username); - assert(uid); - assert(gid); - assert(home); /* We enforce some special rules for uid=0: in order to avoid * NSS lookups for root we hardcode its data. */ if (streq(*username, "root") || streq(*username, "0")) { *username = "root"; - *uid = 0; - *gid = 0; - *home = "/root"; + + if (uid) + *uid = 0; + + if (gid) + *gid = 0; + + if (home) + *home = "/root"; return 0; } - if (safe_atolu(*username, &lu) >= 0) { + if (parse_uid(*username, &u) >= 0) { errno = 0; - p = getpwuid((uid_t) lu); + p = getpwuid(u); /* If there are multiple users with the same id, make * sure to leave $USER to the configured value instead @@ -5300,9 +5296,53 @@ int get_user_creds(const char **username, uid_t *uid, gid_t *gid, const char **h if (!p) return errno != 0 ? -errno : -ESRCH; - *uid = p->pw_uid; - *gid = p->pw_gid; - *home = p->pw_dir; + if (uid) + *uid = p->pw_uid; + + if (gid) + *gid = p->pw_gid; + + if (home) + *home = p->pw_dir; + + return 0; +} + +int get_group_creds(const char **groupname, gid_t *gid) { + struct group *g; + gid_t id; + + assert(groupname); + + /* We enforce some special rules for gid=0: in order to avoid + * NSS lookups for root we hardcode its data. */ + + if (streq(*groupname, "root") || streq(*groupname, "0")) { + *groupname = "root"; + + if (gid) + *gid = 0; + + return 0; + } + + if (parse_gid(*groupname, &id) >= 0) { + errno = 0; + g = getgrgid(id); + + if (g) + *groupname = g->gr_name; + } else { + errno = 0; + g = getgrnam(*groupname); + } + + if (!g) + return errno != 0 ? -errno : -ESRCH; + + if (gid) + *gid = g->gr_gid; + return 0; } @@ -5384,7 +5424,10 @@ int get_files_in_directory(const char *path, char ***list) { char **l = NULL; assert(path); - assert(list); + + /* Returns all files in a directory in *list, and the number + * of files as return value. If list is NULL returns only the + * number */ d = opendir(path); for (;;) { @@ -5405,42 +5448,103 @@ int get_files_in_directory(const char *path, char ***list) { if (!dirent_is_file(de)) continue; - if ((unsigned) r >= n) { - char **t; + if (list) { + if ((unsigned) r >= n) { + char **t; - n = MAX(16, 2*r); - t = realloc(l, sizeof(char*) * n); - if (!t) { - r = -ENOMEM; - goto finish; - } + n = MAX(16, 2*r); + t = realloc(l, sizeof(char*) * n); + if (!t) { + r = -ENOMEM; + goto finish; + } - l = t; - } + l = t; + } - assert((unsigned) r < n); + assert((unsigned) r < n); - l[r] = strdup(de->d_name); - if (!l[r]) { - r = -ENOMEM; - goto finish; - } + l[r] = strdup(de->d_name); + if (!l[r]) { + r = -ENOMEM; + goto finish; + } - l[++r] = NULL; + l[++r] = NULL; + } else + r++; } finish: if (d) closedir(d); - if (r >= 0) - *list = l; - else + if (r >= 0) { + if (list) + *list = l; + } else strv_free(l); return r; } +char *join(const char *x, ...) { + va_list ap; + size_t l; + char *r, *p; + + va_start(ap, x); + + if (x) { + l = strlen(x); + + for (;;) { + const char *t; + + t = va_arg(ap, const char *); + if (!t) + break; + + l += strlen(t); + } + } else + l = 0; + + va_end(ap); + + r = new(char, l+1); + if (!r) + return NULL; + + if (x) { + p = stpcpy(r, x); + + va_start(ap, x); + + for (;;) { + const char *t; + + t = va_arg(ap, const char *); + if (!t) + break; + + p = stpcpy(p, t); + } + } else + r[0] = 0; + + return r; +} + +bool is_main_thread(void) { + static __thread int cached = 0; + + if (_unlikely_(cached == 0)) + cached = getpid() == gettid() ? 1 : -1; + + return cached > 0; +} + static const char *const ioprio_class_table[] = { [IOPRIO_CLASS_NONE] = "none", [IOPRIO_CLASS_RT] = "realtime",