chiark / gitweb /
systemd: reconnect to syslog as soon as the journal is fully up
[elogind.git] / src / util.c
index 72eb05954b3d7952e3e054aec5610240c8f8ba84..3179502f6c01466b3614fba82d1699ffc1337986 100644 (file)
@@ -1119,6 +1119,57 @@ int get_process_exe(pid_t pid, char **name) {
         return r;
 }
 
+int get_process_uid(pid_t pid, uid_t *uid) {
+        char *p;
+        FILE *f;
+        int r;
+
+        assert(uid);
+
+        if (pid == 0)
+                return getuid();
+
+        if (asprintf(&p, "/proc/%lu/status", (unsigned long) pid) < 0)
+                return -ENOMEM;
+
+        f = fopen(p, "re");
+        free(p);
+
+        if (!f)
+                return -errno;
+
+        while (!feof(f)) {
+                char line[LINE_MAX], *l;
+
+                if (!fgets(line, sizeof(line), f)) {
+                        if (feof(f))
+                                break;
+
+                        r = -errno;
+                        goto finish;
+                }
+
+                l = strstrip(line);
+
+                if (startswith(l, "Uid:")) {
+                        l += 4;
+                        l += strspn(l, WHITESPACE);
+
+                        l[strcspn(l, WHITESPACE)] = 0;
+
+                        r = parse_uid(l, uid);
+                        goto finish;
+                }
+        }
+
+        r = -EIO;
+
+finish:
+        fclose(f);
+
+        return r;
+}
+
 char *strnappend(const char *s, const char *suffix, size_t b) {
         size_t a;
         char *r;
@@ -2453,7 +2504,6 @@ int ask(char *ret, const char *replies, const char *text, ...) {
 int reset_terminal_fd(int fd) {
         struct termios termios;
         int r = 0;
-        long arg;
 
         /* Set terminal to some sane defaults */
 
@@ -2466,9 +2516,11 @@ int reset_terminal_fd(int fd) {
         /* Disable exclusive mode, just in case */
         ioctl(fd, TIOCNXCL);
 
+        /* Switch to text mode */
+        ioctl(fd, KDSETMODE, KD_TEXT);
+
         /* Enable console unicode mode */
-        arg = K_UNICODE;
-        ioctl(fd, KDSKBMODE, &arg);
+        ioctl(fd, KDSKBMODE, K_UNICODE);
 
         if (tcgetattr(fd, &termios) < 0) {
                 r = -errno;
@@ -3577,7 +3629,7 @@ cpu_set_t* cpu_set_malloc(unsigned *ncpus) {
         }
 }
 
-void status_vprintf(const char *status, const char *format, va_list ap) {
+void status_vprintf(const char *status, bool ellipse, const char *format, va_list ap) {
         char *s = NULL, *spaces = NULL, *e;
         int fd = -1, c;
         size_t emax, sl, left;
@@ -3592,38 +3644,42 @@ void status_vprintf(const char *status, const char *format, va_list ap) {
         if (vasprintf(&s, format, ap) < 0)
                 goto finish;
 
-        fd = open_terminal("/dev/tty", O_WRONLY|O_NOCTTY|O_CLOEXEC);
+        fd = open_terminal("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC);
         if (fd < 0)
                 goto finish;
 
-        c = fd_columns(fd);
-        if (c <= 0)
-                c = 80;
+        if (ellipse) {
+                c = fd_columns(fd);
+                if (c <= 0)
+                        c = 80;
 
-        if (status) {
-                sl = 2 + 6 + 1; /* " [" status "]" */
-                emax = (size_t) c > sl ? c - sl - 1 : 0;
-        } else
-                emax = c - 1;
+                if (status) {
+                        sl = 2 + 6 + 1; /* " [" status "]" */
+                        emax = (size_t) c > sl ? c - sl - 1 : 0;
+                } else
+                        emax = c - 1;
 
-        e = ellipsize(s, emax, 75);
-        if (e) {
-                free(s);
-                s = e;
+                e = ellipsize(s, emax, 75);
+                if (e) {
+                        free(s);
+                        s = e;
+                }
         }
 
         zero(iovec);
         IOVEC_SET_STRING(iovec[n++], s);
 
-        sl = strlen(s);
-        left = emax > sl ? emax - sl : 0;
-        if (left > 0) {
-                spaces = malloc(left);
-                if (spaces) {
-                        memset(spaces, ' ', left);
-                        iovec[n].iov_base = spaces;
-                        iovec[n].iov_len = left;
-                        n++;
+        if (ellipse) {
+                sl = strlen(s);
+                left = emax > sl ? emax - sl : 0;
+                if (left > 0) {
+                        spaces = malloc(left);
+                        if (spaces) {
+                                memset(spaces, ' ', left);
+                                iovec[n].iov_base = spaces;
+                                iovec[n].iov_len = left;
+                                n++;
+                        }
                 }
         }
 
@@ -3644,13 +3700,13 @@ finish:
                 close_nointr_nofail(fd);
 }
 
-void status_printf(const char *status, const char *format, ...) {
+void status_printf(const char *status, bool ellipse, const char *format, ...) {
         va_list ap;
 
         assert(format);
 
         va_start(ap, format);
-        status_vprintf(status, format, ap);
+        status_vprintf(status, ellipse, format, ap);
         va_end(ap);
 }
 
@@ -3808,6 +3864,7 @@ void status_welcome(void) {
                 const_color = "1";
 
         status_printf(NULL,
+                      false,
                       "\nWelcome to \x1B[%sm%s\x1B[0m!\n",
                       const_color ? const_color : ansi_color,
                       const_pretty ? const_pretty : pretty_name);
@@ -6045,3 +6102,16 @@ finish:
         return buf;
 
 }
+
+void* memdup(const void *p, size_t l) {
+        void *r;
+
+        assert(p);
+
+        r = malloc(l);
+        if (!r)
+                return NULL;
+
+        memcpy(r, p, l);
+        return r;
+}