X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Fshared%2Futil.c;h=d86df17591af379453b31af2b36890842b8c49e3;hb=cd9556cc84523e2d515a8be2648e2a50fb6b2144;hp=43948ccbd8970c5076d2f00659d26f2d449fa4ce;hpb=3d9a412243035beeaaf3465a62065444a5adf21c;p=elogind.git diff --git a/src/shared/util.c b/src/shared/util.c index 43948ccbd..d86df1759 100644 --- a/src/shared/util.c +++ b/src/shared/util.c @@ -4287,6 +4287,15 @@ bool tty_is_vc(const char *tty) { return vtnr_from_tty(tty) >= 0; } +bool tty_is_console(const char *tty) { + assert(tty); + + if (startswith(tty, "/dev/")) + tty += 5; + + return streq(tty, "console"); +} + int vtnr_from_tty(const char *tty) { int i, r; @@ -4343,7 +4352,7 @@ bool tty_is_vc_resolve(const char *tty) { const char *default_term_for_tty(const char *tty) { assert(tty); - return tty_is_vc_resolve(tty) ? "TERM=linux" : "TERM=vt100"; + return tty_is_vc_resolve(tty) ? "TERM=linux" : "TERM=vt102"; } bool dirent_is_file(const struct dirent *de) { @@ -4868,7 +4877,7 @@ static int files_add(Hashmap *h, const char *path, const char *suffix) { for (;;) { int k; - char *p, *f; + char *p; k = readdir_r(dir, &buffer, &de); if (k != 0) { @@ -4887,17 +4896,10 @@ static int files_add(Hashmap *h, const char *path, const char *suffix) { goto finish; } - f = canonicalize_file_name(p); - if (!f) { - log_error("Failed to canonicalize file name '%s': %m", p); + if (hashmap_put(h, file_name_from_path(p), p) <= 0) { + log_debug("Skip overridden file: %s.", p); free(p); - continue; } - free(p); - - log_debug("found: %s\n", f); - if (hashmap_put(h, file_name_from_path(f), f) <= 0) - free(f); } finish: @@ -4992,7 +4994,6 @@ int hwclock_is_localtime(void) { if (!b) return -EIO; - truncate_nl(line); local = streq(line, "LOCAL"); @@ -5127,7 +5128,7 @@ int hwclock_get_time(struct tm *tm) { if (ioctl(fd, RTC_RD_TIME, tm) < 0) err = -errno; - /* We don't now daylight saving, so we reset this in order not + /* We don't know daylight saving, so we reset this in order not * to confused mktime(). */ tm->tm_isdst = -1; @@ -6159,3 +6160,65 @@ int path_is_read_only_fs(const char *path) { return !!(st.f_flag & ST_RDONLY); } + +int getenv_for_pid(pid_t pid, const char *field, char **_value) { + char path[sizeof("/proc/")-1+10+sizeof("/environ")], *value = NULL; + int r; + FILE *f; + bool done = false; + size_t l; + + assert(field); + assert(_value); + + if (pid == 0) + pid = getpid(); + + snprintf(path, sizeof(path), "/proc/%lu/environ", (unsigned long) pid); + char_array_0(path); + + f = fopen(path, "re"); + if (!f) + return -errno; + + l = strlen(field); + r = 0; + + do { + char line[LINE_MAX]; + unsigned i; + + for (i = 0; i < sizeof(line)-1; i++) { + int c; + + c = getc(f); + if (_unlikely_(c == EOF)) { + done = true; + break; + } else if (c == 0) + break; + + line[i] = c; + } + line[i] = 0; + + if (memcmp(line, field, l) == 0 && line[l] == '=') { + value = strdup(line + l + 1); + if (!value) { + r = -ENOMEM; + break; + } + + r = 1; + break; + } + + } while (!done); + + fclose(f); + + if (r >= 0) + *_value = value; + + return r; +}