chiark / gitweb /
util: properly detect ttyname_r() failing
[elogind.git] / src / util.c
index c0b63dd574ba911a4d9f282d5184a21785a8fbb6..3bcce2f019bcf6af535d5f4942d135185da4a870 100644 (file)
@@ -2531,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);
 
@@ -2997,6 +2998,17 @@ void nss_disable_nscd(void) {
                 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",