chiark / gitweb /
util: various additional modernizations
authorLennart Poettering <lennart@poettering.net>
Fri, 14 Sep 2012 08:24:27 +0000 (10:24 +0200)
committerLennart Poettering <lennart@poettering.net>
Fri, 14 Sep 2012 08:24:27 +0000 (10:24 +0200)
src/shared/macro.h
src/shared/unit-name.c
src/shared/util.c
src/shared/util.h

index eb9a8c0a72c1233247b875cd8e8617b093864bfd..f8c5656538ac9c41f341f4dabaffd73cca74feec 100644 (file)
@@ -190,5 +190,6 @@ static inline size_t IOVEC_INCREMENT(struct iovec *i, unsigned n, size_t k) {
 #define _cleanup_free_ __attribute__((cleanup(freep)))
 #define _cleanup_fclose_ __attribute__((cleanup(fclosep)))
 #define _cleanup_close_ __attribute__((cleanup(closep)))
+#define _cleanup_closedir_ __attribute__((cleanup(closedirp)))
 
 #include "log.h"
index d6391228f2118686d4cae9cd79ca230a3d8ffc20..cfe3133b5ae58fa4538af335f965fbf79d04f53b 100644 (file)
@@ -477,8 +477,7 @@ char *unit_name_mangle(const char *name) {
         /* Try to turn a string that might not be a unit name into a
          * sensible unit name. */
 
-        if (path_startswith(name, "/dev/") ||
-            path_startswith(name, "/sys/"))
+        if (is_device_path(name))
                 return unit_name_from_path(name, ".device");
 
         if (path_is_absolute(name))
index 7be0df2a2b819aaef2235b8dd4992c3f60660748..007f15d68092da375a7a79b42f9cda271249e33f 100644 (file)
@@ -2043,29 +2043,23 @@ char *format_timespan(char *buf, size_t l, usec_t t) {
 }
 
 bool fstype_is_network(const char *fstype) {
-        static const char * const table[] = {
-                "cifs",
-                "smbfs",
-                "ncpfs",
-                "nfs",
-                "nfs4",
-                "gfs",
-                "gfs2"
-        };
-
-        unsigned i;
+        static const char table[] =
+                "cifs\0"
+                "smbfs\0"
+                "ncpfs\0"
+                "nfs\0"
+                "nfs4\0"
+                "gfs\0"
+                "gfs2";
 
-        for (i = 0; i < ELEMENTSOF(table); i++)
-                if (streq(table[i], fstype))
-                        return true;
-
-        return false;
+        return nulstr_contains(table, fstype);
 }
 
 int chvt(int vt) {
-        int fd, r = 0;
+        _cleanup_close_ int fd;
 
-        if ((fd = open_terminal("/dev/tty0", O_RDWR|O_NOCTTY|O_CLOEXEC)) < 0)
+        fd = open_terminal("/dev/tty0", O_RDWR|O_NOCTTY|O_CLOEXEC);
+        if (fd < 0)
                 return -errno;
 
         if (vt < 0) {
@@ -2074,20 +2068,16 @@ int chvt(int vt) {
                         0
                 };
 
-                if (ioctl(fd, TIOCLINUX, tiocl) < 0) {
-                        r = -errno;
-                        goto fail;
-                }
+                if (ioctl(fd, TIOCLINUX, tiocl) < 0)
+                        return -errno;
 
                 vt = tiocl[0] <= 0 ? 1 : tiocl[0];
         }
 
         if (ioctl(fd, VT_ACTIVATE, vt) < 0)
-                r = -errno;
+                return -errno;
 
-fail:
-        close_nointr_nofail(fd);
-        return r;
+        return 0;
 }
 
 int read_one_char(FILE *f, char *ret, usec_t t, bool *need_nl) {
@@ -2952,36 +2942,30 @@ bool is_device_path(const char *path) {
 }
 
 int dir_is_empty(const char *path) {
-        DIR *d;
+        _cleanup_closedir_ DIR *d;
         int r;
-        struct dirent buf, *de;
 
-        if (!(d = opendir(path)))
+        d = opendir(path);
+        if (!d)
                 return -errno;
 
         for (;;) {
-                if ((r = readdir_r(d, &buf, &de)) > 0) {
-                        r = -r;
-                        break;
-                }
+                struct dirent buf, *de;
 
-                if (!de) {
-                        r = 1;
-                        break;
-                }
+                r = readdir_r(d, &buf, &de);
+                if (r > 0)
+                        return -r;
 
-                if (!ignore_file(de->d_name)) {
-                        r = 0;
-                        break;
-                }
-        }
+                if (!de)
+                        return 1;
 
-        closedir(d);
-        return r;
+                if (!ignore_file(de->d_name))
+                        return 0;
+        }
 }
 
 unsigned long long random_ull(void) {
-        int fd;
+        _cleanup_close_ int fd;
         uint64_t ull;
         ssize_t r;
 
@@ -2990,8 +2974,6 @@ unsigned long long random_ull(void) {
                 goto fallback;
 
         r = loop_read(fd, &ull, sizeof(ull), true);
-        close_nointr_nofail(fd);
-
         if (r != sizeof(ull))
                 goto fallback;
 
@@ -3065,7 +3047,8 @@ bool hostname_is_set(void) {
 
 static char *lookup_uid(uid_t uid) {
         long bufsize;
-        char *buf, *name;
+        char *name;
+        _cleanup_free_ char *buf = NULL;
         struct passwd pwbuf, *pw = NULL;
 
         /* Shortcut things to avoid NSS lookups */
@@ -3080,13 +3063,8 @@ static char *lookup_uid(uid_t uid) {
         if (!buf)
                 return NULL;
 
-        if (getpwuid_r(uid, &pwbuf, buf, bufsize, &pw) == 0 && pw) {
-                name = strdup(pw->pw_name);
-                free(buf);
-                return name;
-        }
-
-        free(buf);
+        if (getpwuid_r(uid, &pwbuf, buf, bufsize, &pw) == 0 && pw)
+                return strdup(pw->pw_name);
 
         if (asprintf(&name, "%lu", (unsigned long) uid) < 0)
                 return NULL;
@@ -3122,12 +3100,14 @@ int getttyname_malloc(int fd, char **r) {
 
         assert(r);
 
-        if ((k = ttyname_r(fd, path, sizeof(path))) != 0)
+        k = ttyname_r(fd, path, sizeof(path));
+        if (k != 0)
                 return -k;
 
         char_array_0(path);
 
-        if (!(c = strdup(startswith(path, "/dev/") ? path + 5 : path)))
+        c = strdup(startswith(path, "/dev/") ? path + 5 : path);
+        if (!c)
                 return -ENOMEM;
 
         *r = c;
@@ -3138,7 +3118,8 @@ int getttyname_harder(int fd, char **r) {
         int k;
         char *s;
 
-        if ((k = getttyname_malloc(fd, &s)) < 0)
+        k = getttyname_malloc(fd, &s);
+        if (k < 0)
                 return k;
 
         if (streq(s, "tty")) {
@@ -5706,7 +5687,7 @@ bool is_valid_documentation_url(const char *url) {
 }
 
 bool in_initrd(void) {
-        static int saved = -1;
+        static __thread int saved = -1;
         struct statfs s;
 
         if (saved >= 0)
@@ -5877,3 +5858,8 @@ void closep(int *fd) {
         if (*fd >= 0)
                 close_nointr_nofail(*fd);
 }
+
+void closedirp(DIR **d) {
+        if (*d)
+                closedir(*d);
+}
index 90234eb27b8577f5c75c349f711e4d340c4c71f2..a3f825b5bb9f3f6cd78734e3dec34d997d16615d 100644 (file)
@@ -532,3 +532,4 @@ int get_home_dir(char **ret);
 void freep(void *p);
 void fclosep(FILE **f);
 void closep(int *fd);
+void closedirp(DIR **d);