chiark / gitweb /
util: properly detect ttyname_r() failing
[elogind.git] / src / util.c
index 4e371d148e34759697fca5a3fbdaa45dcf5f9372..3bcce2f019bcf6af535d5f4942d135185da4a870 100644 (file)
@@ -1,4 +1,4 @@
-/*-*- Mode: C; c-basic-offset: 8 -*-*/
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
 
 /***
   This file is part of systemd.
@@ -48,6 +48,7 @@
 #include <pwd.h>
 #include <netinet/ip.h>
 #include <linux/kd.h>
+#include <dlfcn.h>
 
 #include "macro.h"
 #include "util.h"
@@ -2492,18 +2493,6 @@ char* gethostname_malloc(void) {
         return strdup(u.sysname);
 }
 
-int getmachineid_malloc(char **b) {
-        int r;
-
-        assert(b);
-
-        if ((r = read_one_line_file("/var/lib/dbus/machine-id", b)) < 0)
-                return r;
-
-        strstrip(*b);
-        return 0;
-}
-
 char* getlogname_malloc(void) {
         uid_t uid;
         long bufsize;
@@ -2542,11 +2531,12 @@ char* getlogname_malloc(void) {
 
 int getttyname_malloc(char **r) {
         char path[PATH_MAX], *p, *c;
+        int k;
 
         assert(r);
 
-        if (ttyname_r(STDIN_FILENO, path, sizeof(path)) < 0)
-                return -errno;
+        if ((k = ttyname_r(STDIN_FILENO, path, sizeof(path))) != 0)
+                return -k;
 
         char_array_0(path);
 
@@ -2991,6 +2981,35 @@ char *ellipsize(const char *s, unsigned length, unsigned percent) {
         return r;
 }
 
+void nss_disable_nscd(void) {
+
+        void (*func)(void);
+
+        /* This is an internal glibc function call. We are not
+         * supposed to call this, because we are not nscd. However
+         * sometimes we feel really dangerous and do it
+         * nonetheless. Muahahah! But at least we protect this with a
+         * dlsym() just in case glibc takes this away from us. */
+
+        if ((func = dlsym(RTLD_DEFAULT, "__nss_disable_nscd"))) {
+                log_debug("Disabling nscd.");
+                func();
+        } else
+                log_debug("Cannot disable nscd.");
+}
+
+int touch(const char *path) {
+        int fd;
+
+        assert(path);
+
+        if ((fd = open(path, O_WRONLY|O_CREAT|O_CLOEXEC|O_NOCTTY, 0666)) < 0)
+                return -errno;
+
+        close_nointr_nofail(fd);
+        return 0;
+}
+
 static const char *const ioprio_class_table[] = {
         [IOPRIO_CLASS_NONE] = "none",
         [IOPRIO_CLASS_RT] = "realtime",