X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Fshared%2Futil.c;h=3a4d1965ae314788152999d7d518186744ad2601;hp=a6669c5a97b663c2b8f8edb518f0fcf3203652ee;hb=0bee65f0622c4faa8ac8ae771cc0c8a936dfa284;hpb=9588bc32096fc8342bfd8b989689717186d7d86e diff --git a/src/shared/util.c b/src/shared/util.c index a6669c5a9..3a4d1965a 100644 --- a/src/shared/util.c +++ b/src/shared/util.c @@ -41,7 +41,6 @@ #include #include #include -#include #include #include #include @@ -77,6 +76,7 @@ #include "utf8.h" #include "gunicode.h" #include "virt.h" +#include "def.h" int saved_argc = 0; char **saved_argv = NULL; @@ -1356,78 +1356,6 @@ char *xescape(const char *s, const char *bad) { return r; } -char *bus_path_escape(const char *s) { - char *r, *t; - const char *f; - - assert(s); - - /* Escapes all chars that D-Bus' object path cannot deal - * with. Can be reversed with bus_path_unescape(). We special - * case the empty string. */ - - if (*s == 0) - return strdup("_"); - - r = new(char, strlen(s)*3 + 1); - if (!r) - return NULL; - - for (f = s, t = r; *f; f++) { - - /* Escape everything that is not a-zA-Z0-9. We also - * escape 0-9 if it's the first character */ - - if (!(*f >= 'A' && *f <= 'Z') && - !(*f >= 'a' && *f <= 'z') && - !(f > s && *f >= '0' && *f <= '9')) { - *(t++) = '_'; - *(t++) = hexchar(*f >> 4); - *(t++) = hexchar(*f); - } else - *(t++) = *f; - } - - *t = 0; - - return r; -} - -char *bus_path_unescape(const char *f) { - char *r, *t; - - assert(f); - - /* Special case for the empty string */ - if (streq(f, "_")) - return strdup(""); - - r = new(char, strlen(f) + 1); - if (!r) - return NULL; - - for (t = r; *f; f++) { - - if (*f == '_') { - int a, b; - - if ((a = unhexchar(f[1])) < 0 || - (b = unhexchar(f[2])) < 0) { - /* Invalid escape code, let's take it literal then */ - *(t++) = '_'; - } else { - *(t++) = (char) ((a << 4) | b); - f += 2; - } - } else - *(t++) = *f; - } - - *t = 0; - - return r; -} - char *ascii_strlower(char *t) { char *p; @@ -2597,10 +2525,8 @@ int get_ctty_devnr(pid_t pid, dev_t *d) { char line[LINE_MAX], *p; unsigned long ttynr; const char *fn; - int k; assert(pid >= 0); - assert(d); if (pid == 0) fn = "/proc/self/stat"; @@ -2611,10 +2537,8 @@ int get_ctty_devnr(pid_t pid, dev_t *d) { if (!f) return -errno; - if (!fgets(line, sizeof(line), f)) { - k = feof(f) ? -EIO : -errno; - return k; - } + if (!fgets(line, sizeof(line), f)) + return feof(f) ? -EIO : -errno; p = strrchr(line, ')'); if (!p) @@ -2634,7 +2558,9 @@ int get_ctty_devnr(pid_t pid, dev_t *d) { if (major(ttynr) == 0 && minor(ttynr) == 0) return -ENOENT; - *d = (dev_t) ttynr; + if (d) + *d = (dev_t) ttynr; + return 0; } @@ -3699,6 +3625,16 @@ char *resolve_dev_console(char **active) { else tty = *active; + if (streq(tty, "tty0")) { + char *tmp; + + /* Get the active VC (e.g. tty1) */ + if (read_one_line_file("/sys/class/tty/tty0/active", &tmp) >= 0) { + free(*active); + tty = *active = tmp; + } + } + return tty; }