[systemd-commits] 2 commits - src/shared

Lennart Poettering lennart at kemper.freedesktop.org
Fri Sep 14 01:36:58 PDT 2012


 src/shared/macro.h     |    1 
 src/shared/unit-name.c |    3 -
 src/shared/util.c      |  134 +++++++++++++++++++++++--------------------------
 src/shared/util.h      |    1 
 4 files changed, 68 insertions(+), 71 deletions(-)

New commits:
commit 73836c5c430f48838fec2606b1729dda371edd2d
Author: Lennart Poettering <lennart at poettering.net>
Date:   Fri Sep 14 10:36:50 2012 +0200

    util: more modernizations

diff --git a/src/shared/util.c b/src/shared/util.c
index 007f15d..6a40cf1 100644
--- a/src/shared/util.c
+++ b/src/shared/util.c
@@ -2050,7 +2050,7 @@ bool fstype_is_network(const char *fstype) {
                 "nfs\0"
                 "nfs4\0"
                 "gfs\0"
-                "gfs2";
+                "gfs2\0";
 
         return nulstr_contains(table, fstype);
 }
@@ -2904,9 +2904,9 @@ int make_stdio(int fd) {
 
         assert(fd >= 0);
 
-        r = dup2(fd, STDIN_FILENO);
-        s = dup2(fd, STDOUT_FILENO);
-        t = dup2(fd, STDERR_FILENO);
+        r = dup3(fd, STDIN_FILENO, 0);
+        s = dup3(fd, STDOUT_FILENO, 0);
+        t = dup3(fd, STDERR_FILENO, 0);
 
         if (fd >= 3)
                 close_nointr_nofail(fd);
@@ -2914,9 +2914,7 @@ int make_stdio(int fd) {
         if (r < 0 || s < 0 || t < 0)
                 return -errno;
 
-        fd_cloexec(STDIN_FILENO, false);
-        fd_cloexec(STDOUT_FILENO, false);
-        fd_cloexec(STDERR_FILENO, false);
+        /* We rely here that the new fd has O_CLOEXEC not set */
 
         return 0;
 }
@@ -3870,7 +3868,12 @@ int touch(const char *path) {
 
         assert(path);
 
-        if ((fd = open(path, O_WRONLY|O_CREAT|O_CLOEXEC|O_NOCTTY, 0644)) < 0)
+        /* This just opens the file for writing, ensuring it
+         * exists. It doesn't call utimensat() the way /usr/bin/touch
+         * does it. */
+
+        fd = open(path, O_WRONLY|O_CREAT|O_CLOEXEC|O_NOCTTY, 0644);
+        if (fd < 0)
                 return -errno;
 
         close_nointr_nofail(fd);
@@ -3881,6 +3884,10 @@ char *unquote(const char *s, const char* quotes) {
         size_t l;
         assert(s);
 
+        /* This is rather stupid, simply removes the heading and
+         * trailing quotes if there is one. Doesn't care about
+         * escaping or anything. */
+
         l = strlen(s);
         if (l < 2)
                 return strdup(s);
@@ -4022,10 +4029,12 @@ DIR *xopendirat(int fd, const char *name, int flags) {
         int nfd;
         DIR *d;
 
-        if ((nfd = openat(fd, name, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|flags)) < 0)
+        nfd = openat(fd, name, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|flags);
+        if (nfd < 0)
                 return NULL;
 
-        if (!(d = fdopendir(nfd))) {
+        d = fdopendir(nfd);
+        if (!d) {
                 close_nointr_nofail(nfd);
                 return NULL;
         }
@@ -4037,7 +4046,8 @@ int signal_from_string_try_harder(const char *s) {
         int signo;
         assert(s);
 
-        if ((signo = signal_from_string(s)) <= 0)
+        signo = signal_from_string(s);
+        if (signo <= 0)
                 if (startswith(s, "SIG"))
                         return signal_from_string(s+3);
 

commit a05f97b3ac590bc313e8c9d1613d06de0bf28ae2
Author: Lennart Poettering <lennart at poettering.net>
Date:   Fri Sep 14 10:24:27 2012 +0200

    util: various additional modernizations

diff --git a/src/shared/macro.h b/src/shared/macro.h
index eb9a8c0..f8c5656 100644
--- a/src/shared/macro.h
+++ b/src/shared/macro.h
@@ -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"
diff --git a/src/shared/unit-name.c b/src/shared/unit-name.c
index d639122..cfe3133 100644
--- a/src/shared/unit-name.c
+++ b/src/shared/unit-name.c
@@ -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))
diff --git a/src/shared/util.c b/src/shared/util.c
index 7be0df2..007f15d 100644
--- a/src/shared/util.c
+++ b/src/shared/util.c
@@ -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);
+}
diff --git a/src/shared/util.h b/src/shared/util.h
index 90234eb..a3f825b 100644
--- a/src/shared/util.h
+++ b/src/shared/util.h
@@ -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);



More information about the systemd-commits mailing list