[systemd-commits] 3 commits - src/core src/journal src/nspawn src/shared src/test

Lennart Poettering lennart at kemper.freedesktop.org
Tue Apr 16 05:50:30 PDT 2013


 src/core/dbus-manager.c         |   13 +-
 src/journal/coredumpctl.c       |    1 
 src/journal/journal-internal.h  |    6 +
 src/journal/test-journal-enum.c |    1 
 src/nspawn/nspawn.c             |   16 ---
 src/shared/logs-show.c          |    1 
 src/shared/macro.h              |   16 +--
 src/shared/path-util.c          |   12 ++
 src/shared/path-util.h          |    1 
 src/shared/set.h                |    3 
 src/shared/strv.h               |    2 
 src/shared/util.c               |  189 ++++++++++++++++++----------------------
 src/shared/util.h               |   53 ++++++++---
 src/test/test-util.c            |   46 +++++++++
 14 files changed, 213 insertions(+), 147 deletions(-)

New commits:
commit 49aa47c7fb6c6cf85f2780080e89181974efdc3b
Author: Lennart Poettering <lennart at poettering.net>
Date:   Tue Apr 16 14:50:05 2013 +0200

    util: make generation of profcs PID paths nicer

diff --git a/src/shared/macro.h b/src/shared/macro.h
index cca41a3..9bf81dc 100644
--- a/src/shared/macro.h
+++ b/src/shared/macro.h
@@ -66,12 +66,16 @@
 #error "Wut? Pointers are neither 4 nor 8 bytes long?"
 #endif
 
+#define ALIGN_PTR(p) ((void*) ALIGN((unsigned long) p))
+#define ALIGN4_PTR(p) ((void*) ALIGN4((unsigned long) p))
 #define ALIGN8_PTR(p) ((void*) ALIGN8((unsigned long) p))
 
 static inline size_t ALIGN_TO(size_t l, size_t ali) {
         return ((l + ali - 1) & ~(ali - 1));
 }
 
+#define ALIGN_TO_PTR(p, ali) ((void*) ALIGN_TO((unsigned long) p))
+
 #define ELEMENTSOF(x) (sizeof(x)/sizeof((x)[0]))
 
 /*
diff --git a/src/shared/util.c b/src/shared/util.c
index 53caa7f..a55b27f 100644
--- a/src/shared/util.c
+++ b/src/shared/util.c
@@ -80,15 +80,14 @@ char **saved_argv = NULL;
 static volatile unsigned cached_columns = 0;
 static volatile unsigned cached_lines = 0;
 
-#define PROCFS_PATH_LEN (sizeof("/proc/")-1 + DECIMAL_STR_MAX(pid_t))
-
-#define FORMAT_PROCFS_PATH(buffer, path, pid)                                                           \
-        do {                                                                                            \
-                assert_cc(sizeof(buffer) == (PROCFS_PATH_LEN + 1 + sizeof(path)));                      \
-                snprintf(buffer, sizeof(buffer) - 1, "/proc/%lu/%s", (unsigned long) pid, path);        \
-                char_array_0(buffer);                                                                   \
-        } while(0)
-
+#define procfs_file_alloca(pid, field)                                  \
+        ({                                                              \
+                pid_t _pid_ = (pid);                                    \
+                char *_r_;                                              \
+                _r_ = alloca(sizeof("/proc/") -1 + DECIMAL_STR_MAX(pid_t) + 1 + sizeof(field)); \
+                sprintf(_r_, "/proc/%lu/" field, (unsigned long) _pid_); \
+                _r_;                                                    \
+        })
 
 size_t page_size(void) {
         static __thread size_t pgsz = 0;
@@ -468,15 +467,20 @@ char *split_quoted(const char *c, size_t *l, char **state) {
 int get_parent_of_pid(pid_t pid, pid_t *_ppid) {
         int r;
         _cleanup_fclose_ FILE *f = NULL;
-        char fn[sizeof("/proc/")-1 + DECIMAL_STR_MAX(pid_t) + sizeof("/stat")], line[LINE_MAX], *p;
+        char line[LINE_MAX];
         long unsigned ppid;
+        const char *p;
 
-        assert(pid > 0);
+        assert(pid >= 0);
         assert(_ppid);
 
-        assert_se(snprintf(fn, sizeof(fn)-1, "/proc/%lu/stat", (unsigned long) pid) < (int) (sizeof(fn)-1));
+        if (pid == 0) {
+                *_ppid = getppid();
+                return 0;
+        }
 
-        f = fopen(fn, "re");
+        p = procfs_file_alloca(pid, "stat");
+        f = fopen(p, "re");
         if (!f)
                 return -errno;
 
@@ -511,14 +515,18 @@ int get_parent_of_pid(pid_t pid, pid_t *_ppid) {
 
 int get_starttime_of_pid(pid_t pid, unsigned long long *st) {
         _cleanup_fclose_ FILE *f = NULL;
-        char fn[sizeof("/proc/")-1 + DECIMAL_STR_MAX(pid_t) + sizeof("/stat")], line[LINE_MAX], *p;
+        char line[LINE_MAX];
+        const char *p;
 
-        assert(pid > 0);
+        assert(pid >= 0);
         assert(st);
 
-        assert_se(snprintf(fn, sizeof(fn)-1, "/proc/%lu/stat", (unsigned long) pid) < (int) (sizeof(fn)-1));
+        if (pid == 0)
+                p = "/proc/self/stat";
+        else
+                p = procfs_file_alloca(pid, "stat");
 
-        f = fopen(fn, "re");
+        f = fopen(p, "re");
         if (!f)
                 return -errno;
 
@@ -585,60 +593,60 @@ char *truncate_nl(char *s) {
 }
 
 int get_process_comm(pid_t pid, char **name) {
-        int r;
+        const char *p;
 
         assert(name);
+        assert(pid >= 0);
 
         if (pid == 0)
-                r = read_one_line_file("/proc/self/comm", name);
-        else {
-                char path[PROCFS_PATH_LEN + sizeof("/comm")];
-                FORMAT_PROCFS_PATH(path, "comm", pid);
-                r = read_one_line_file(path, name);
-        }
+                p = "/proc/self/comm";
+        else
+                p = procfs_file_alloca(pid, "comm");
 
-        return r;
+        return read_one_line_file(p, name);
 }
 
 int get_process_cmdline(pid_t pid, size_t max_length, bool comm_fallback, char **line) {
+        _cleanup_fclose_ FILE *f = NULL;
         char *r = NULL, *k;
+        const char *p;
         int c;
-        FILE *f;
 
         assert(line);
+        assert(pid >= 0);
 
         if (pid == 0)
-                f = fopen("/proc/self/cmdline", "re");
-        else {
-                char path[PROCFS_PATH_LEN + sizeof("/cmdline")];
-                FORMAT_PROCFS_PATH(path, "cmdline", pid);
-                f = fopen(path, "re");
-        }
+                p = "/proc/self/cmdline";
+        else
+                p = procfs_file_alloca(pid, "cmdline");
 
+        f = fopen(p, "re");
         if (!f)
                 return -errno;
+
         if (max_length == 0) {
-                size_t len = 1;
+                size_t len = 0, allocated = 0;
+
                 while ((c = getc(f)) != EOF) {
-                        k = realloc(r, len+1);
-                        if (k == NULL) {
+
+                        if (!GREEDY_REALLOC(r, allocated, len+2)) {
                                 free(r);
-                                fclose(f);
                                 return -ENOMEM;
                         }
-                        r = k;
-                        r[len-1] = isprint(c) ? c : ' ';
-                        r[len] = 0;
-                        len++;
+
+                        r[len++] = isprint(c) ? c : ' ';
                 }
+
+                if (len > 0)
+                        r[len-1] = 0;
+
         } else {
                 bool space = false;
                 size_t left;
+
                 r = new(char, max_length);
-                if (!r) {
-                        fclose(f);
+                if (!r)
                         return -ENOMEM;
-                }
 
                 k = r;
                 left = max_length;
@@ -671,8 +679,6 @@ int get_process_cmdline(pid_t pid, size_t max_length, bool comm_fallback, char *
                         *k = 0;
         }
 
-        fclose(f);
-
         /* Kernel threads have no argv[] */
         if (r == NULL || r[0] == 0) {
                 char *t;
@@ -699,7 +705,7 @@ int get_process_cmdline(pid_t pid, size_t max_length, bool comm_fallback, char *
 }
 
 int is_kernel_thread(pid_t pid) {
-        char path[PROCFS_PATH_LEN + sizeof("/cmdline")];
+        const char *p;
         size_t count;
         char c;
         bool eof;
@@ -708,9 +714,10 @@ int is_kernel_thread(pid_t pid) {
         if (pid == 0)
                 return 0;
 
-        FORMAT_PROCFS_PATH(path, "cmdline", pid);
-        f = fopen(path, "re");
+        assert(pid > 0);
 
+        p = procfs_file_alloca(pid, "cmdline");
+        f = fopen(p, "re");
         if (!f)
                 return -errno;
 
@@ -726,26 +733,25 @@ int is_kernel_thread(pid_t pid) {
         return 0;
 }
 
+
 int get_process_exe(pid_t pid, char **name) {
-        int r;
+        const char *p;
 
+        assert(pid >= 0);
         assert(name);
 
         if (pid == 0)
-                r = readlink_malloc("/proc/self/exe", name);
-        else {
-                char path[PROCFS_PATH_LEN + sizeof("/exe")];
-                FORMAT_PROCFS_PATH(path, "exe", pid);
-                r = readlink_malloc(path, name);
-        }
+                p = "/proc/self/exe";
+        else
+                p = procfs_file_alloca(pid, "exe");
 
-        return r;
+        return readlink_malloc(p, name);
 }
 
 static int get_process_id(pid_t pid, const char *field, uid_t *uid) {
         _cleanup_fclose_ FILE *f = NULL;
-        char path[PROCFS_PATH_LEN + sizeof("/status")];
         char line[LINE_MAX];
+        const char *p;
 
         assert(field);
         assert(uid);
@@ -753,8 +759,8 @@ static int get_process_id(pid_t pid, const char *field, uid_t *uid) {
         if (pid == 0)
                 return getuid();
 
-        FORMAT_PROCFS_PATH(path, "status", pid);
-        f = fopen(path, "re");
+        p = procfs_file_alloca(pid, "status");
+        f = fopen(p, "re");
         if (!f)
                 return -errno;
 
@@ -781,6 +787,7 @@ int get_process_uid(pid_t pid, uid_t *uid) {
 }
 
 int get_process_gid(pid_t pid, gid_t *gid) {
+        assert_cc(sizeof(uid_t) == sizeof(gid_t));
         return get_process_id(pid, "Gid:", gid);
 }
 
@@ -2566,27 +2573,29 @@ int getttyname_harder(int fd, char **r) {
 }
 
 int get_ctty_devnr(pid_t pid, dev_t *d) {
-        int k;
-        char line[LINE_MAX], *p, *fn;
+        _cleanup_fclose_ FILE *f = NULL;
+        char line[LINE_MAX], *p;
         unsigned long ttynr;
-        FILE *f;
+        const char *fn;
+        int k;
 
-        if (asprintf(&fn, "/proc/%lu/stat", (unsigned long) (pid <= 0 ? getpid() : pid)) < 0)
-                return -ENOMEM;
+        assert(pid >= 0);
+        assert(d);
+
+        if (pid == 0)
+                fn = "/proc/self/stat";
+        else
+                fn = procfs_file_alloca(pid, "stat");
 
         f = fopen(fn, "re");
-        free(fn);
         if (!f)
                 return -errno;
 
         if (!fgets(line, sizeof(line), f)) {
                 k = feof(f) ? -EIO : -errno;
-                fclose(f);
                 return k;
         }
 
-        fclose(f);
-
         p = strrchr(line, ')');
         if (!p)
                 return -EIO;
@@ -5031,19 +5040,21 @@ int setrlimit_closest(int resource, const struct rlimit *rlim) {
 }
 
 int getenv_for_pid(pid_t pid, const char *field, char **_value) {
-        char path[sizeof("/proc/")-1 + DECIMAL_STR_MAX(pid_t) + sizeof("/environ")], *value = NULL;
+        _cleanup_fclose_ FILE *f = NULL;
+        char *value = NULL;
         int r;
-        FILE *f;
         bool done = false;
         size_t l;
+        const char *path;
 
+        assert(pid >= 0);
         assert(field);
         assert(_value);
 
         if (pid == 0)
-                pid = getpid();
-
-        snprintf(path, sizeof(path), "/proc/%lu/environ", (unsigned long) pid);
+                path = "/proc/self/environ";
+        else
+                path = procfs_file_alloca(pid, "environ");
 
         f = fopen(path, "re");
         if (!f)
@@ -5072,10 +5083,8 @@ int getenv_for_pid(pid_t pid, const char *field, char **_value) {
 
                 if (memcmp(line, field, l) == 0 && line[l] == '=') {
                         value = strdup(line + l + 1);
-                        if (!value) {
-                                r = -ENOMEM;
-                                break;
-                        }
+                        if (!value)
+                                return -ENOMEM;
 
                         r = 1;
                         break;
@@ -5083,11 +5092,7 @@ int getenv_for_pid(pid_t pid, const char *field, char **_value) {
 
         } while (!done);
 
-        fclose(f);
-
-        if (r >= 0)
-                *_value = value;
-
+        *_value = value;
         return r;
 }
 
diff --git a/src/test/test-util.c b/src/test/test-util.c
index eaf7e22..83959c0 100644
--- a/src/test/test-util.c
+++ b/src/test/test-util.c
@@ -348,6 +348,51 @@ static void test_u64log2(void) {
         assert(u64log2(1024*1024+5) == 20);
 }
 
+static void test_get_process_comm(void) {
+        _cleanup_free_ char *a = NULL, *c = NULL, *d = NULL, *f = NULL, *i = NULL;
+        unsigned long long b;
+        pid_t e;
+        uid_t u;
+        gid_t g;
+        dev_t h;
+        int r;
+
+        assert_se(get_process_comm(1, &a) >= 0);
+        log_info("pid1 comm: '%s'", a);
+
+        assert_se(get_starttime_of_pid(1, &b) >= 0);
+        log_info("pid1 starttime: '%llu'", b);
+
+        assert_se(get_process_cmdline(1, 0, true, &c) >= 0);
+        log_info("pid1 cmdline: '%s'", c);
+
+        assert_se(get_process_cmdline(1, 8, false, &d) >= 0);
+        log_info("pid1 cmdline truncated: '%s'", d);
+
+        assert_se(get_parent_of_pid(1, &e) >= 0);
+        log_info("pid1 ppid: '%llu'", (unsigned long long) e);
+        assert_se(e == 0);
+
+        assert_se(is_kernel_thread(1) == 0);
+
+        r = get_process_exe(1, &f);
+        assert_se(r >= 0 || r == -EACCES);
+        log_info("pid1 exe: '%s'", strna(f));
+
+        assert_se(get_process_uid(1, &u) == 0);
+        log_info("pid1 uid: '%llu'", (unsigned long long) u);
+        assert_se(u == 0);
+
+        assert_se(get_process_gid(1, &g) == 0);
+        log_info("pid1 gid: '%llu'", (unsigned long long) g);
+        assert_se(g == 0);
+
+        assert(get_ctty_devnr(1, &h) == -ENOENT);
+
+        getenv_for_pid(1, "PATH", &i);
+        log_info("pid1 $PATH: '%s'", strna(i));
+}
+
 int main(int argc, char *argv[]) {
         test_streq_ptr();
         test_first_word();
@@ -374,6 +419,7 @@ int main(int argc, char *argv[]) {
         test_bus_path_escape();
         test_hostname_is_valid();
         test_u64log2();
+        test_get_process_comm();
 
         return 0;
 }

commit 6606089752df90f3eeb4924af109046f1c73554c
Author: Lennart Poettering <lennart at poettering.net>
Date:   Tue Apr 16 05:47:04 2013 +0200

    path-util: unify code for detecting OS trees
    
    This also makes sure we always detect an OS tree the same way, by
    checking for /etc/os-release.

diff --git a/src/core/dbus-manager.c b/src/core/dbus-manager.c
index c23709c..d767dd5 100644
--- a/src/core/dbus-manager.c
+++ b/src/core/dbus-manager.c
@@ -1481,7 +1481,7 @@ static DBusHandlerResult bus_manager_message_handler(DBusConnection *connection,
         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "SwitchRoot")) {
                 const char *switch_root, *switch_root_init;
                 char *u, *v;
-                int k;
+                bool good;
 
                 SELINUX_ACCESS_CHECK(connection, message, "reboot");
 
@@ -1506,19 +1506,18 @@ static DBusHandlerResult bus_manager_message_handler(DBusConnection *connection,
 
                 /* Safety check */
                 if (isempty(switch_root_init))
-                        k = access(switch_root, F_OK);
+                        good = path_is_os_tree(switch_root);
                 else {
-                        char *p;
+                        _cleanup_free_ char *p = NULL;
 
                         p = strjoin(switch_root, "/", switch_root_init, NULL);
                         if (!p)
                                 goto oom;
 
-                        k = access(p, X_OK);
-                        free(p);
+                        good = access(p, X_OK) >= 0;
                 }
-                if (k < 0)
-                        return bus_send_error_reply(connection, message, NULL, -errno);
+                if (!good)
+                        return bus_send_error_reply(connection, message, NULL, -EINVAL);
 
                 u = strdup(switch_root);
                 if (!u)
diff --git a/src/nspawn/nspawn.c b/src/nspawn/nspawn.c
index c257682..416d4a6 100644
--- a/src/nspawn/nspawn.c
+++ b/src/nspawn/nspawn.c
@@ -918,20 +918,6 @@ static int drop_capabilities(void) {
         return capability_bounding_set_drop(~arg_retain, false);
 }
 
-static int is_os_tree(const char *path) {
-        int r;
-        char *p;
-        /* We use /bin/sh as flag file if something is an OS */
-
-        if (asprintf(&p, "%s/bin/sh", path) < 0)
-                return -ENOMEM;
-
-        r = access(p, F_OK);
-        free(p);
-
-        return r < 0 ? 0 : 1;
-}
-
 static int process_pty(int master, pid_t pid, sigset_t *mask) {
 
         char in_buffer[LINE_MAX], out_buffer[LINE_MAX];
@@ -1240,7 +1226,7 @@ int main(int argc, char *argv[]) {
                 goto finish;
         }
 
-        if (is_os_tree(arg_directory) <= 0) {
+        if (path_is_os_tree(arg_directory) <= 0) {
                 log_error("Directory %s doesn't look like an OS root directory. Refusing.", arg_directory);
                 goto finish;
         }
diff --git a/src/shared/path-util.c b/src/shared/path-util.c
index 0b50ea6..b623fc3 100644
--- a/src/shared/path-util.c
+++ b/src/shared/path-util.c
@@ -413,3 +413,15 @@ int path_is_read_only_fs(const char *path) {
 
         return !!(st.f_flag & ST_RDONLY);
 }
+
+int path_is_os_tree(const char *path) {
+        char *p;
+        int r;
+
+        /* We use /etc/os-release as flag file if something is an OS */
+
+        p = strappenda(path, "/etc/os-release");
+        r = access(p, F_OK);
+
+        return r < 0 ? 0 : 1;
+}
diff --git a/src/shared/path-util.h b/src/shared/path-util.h
index 9347bc3..ea0f173 100644
--- a/src/shared/path-util.h
+++ b/src/shared/path-util.h
@@ -40,3 +40,4 @@ char **path_strv_canonicalize_uniq(char **l);
 
 int path_is_mount_point(const char *path, bool allow_symlink);
 int path_is_read_only_fs(const char *path);
+int path_is_os_tree(const char *path);
diff --git a/src/shared/util.h b/src/shared/util.h
index 3aac165..cfb5493 100644
--- a/src/shared/util.h
+++ b/src/shared/util.h
@@ -684,3 +684,15 @@ int unlink_noerrno(const char *path);
                 _new_ = alloca(_len_);                  \
                 (void *) memset(_new_, 0, _len_);       \
         })
+
+#define strappenda(a, b)                                \
+        ({                                              \
+                const char *_a_ = (a), *_b_ = (b);      \
+                char *_c_;                              \
+                size_t _x_, _y_;                        \
+                _x_ = strlen(_a_);                      \
+                _y_ = strlen(_b_);                      \
+                _c_ = alloca(_x_ + _y_ + 1);            \
+                strcpy(stpcpy(_c_, _a_), _b_);          \
+                _c_;                                    \
+        })

commit dfb33a9737e62ab872d3937b7690b252d2892fe8
Author: Lennart Poettering <lennart at poettering.net>
Date:   Tue Apr 16 05:25:57 2013 +0200

    macro: rework how we define cleanup macros
    
    There's now a generic _cleanup_ macro with an argument. The macros for
    specific types are now defined using this macro, and in the header files
    where they belong.
    
    All cleanup handlers are now inline functions.

diff --git a/src/journal/coredumpctl.c b/src/journal/coredumpctl.c
index 99ca269..97d967d 100644
--- a/src/journal/coredumpctl.c
+++ b/src/journal/coredumpctl.c
@@ -35,6 +35,7 @@
 #include "path-util.h"
 #include "pager.h"
 #include "macro.h"
+#include "journal-internal.h"
 
 static enum {
         ACTION_NONE,
diff --git a/src/journal/journal-internal.h b/src/journal/journal-internal.h
index 3accf14..ff8b34a 100644
--- a/src/journal/journal-internal.h
+++ b/src/journal/journal-internal.h
@@ -132,3 +132,9 @@ struct sd_journal {
 
 char *journal_make_match_string(sd_journal *j);
 void journal_print_header(sd_journal *j);
+
+static inline void journal_closep(sd_journal **j) {
+        sd_journal_close(*j);
+}
+
+#define _cleanup_journal_close_ _cleanup_(journal_closep)
diff --git a/src/journal/test-journal-enum.c b/src/journal/test-journal-enum.c
index 88f583e..bd1f519 100644
--- a/src/journal/test-journal-enum.c
+++ b/src/journal/test-journal-enum.c
@@ -25,6 +25,7 @@
 #include "sd-journal.h"
 #include "macro.h"
 #include "util.h"
+#include "journal-internal.h"
 
 int main(int argc, char *argv[]) {
         unsigned n = 0;
diff --git a/src/shared/logs-show.c b/src/shared/logs-show.c
index 8897a10..5700321 100644
--- a/src/shared/logs-show.c
+++ b/src/shared/logs-show.c
@@ -30,6 +30,7 @@
 #include "util.h"
 #include "utf8.h"
 #include "hashmap.h"
+#include "journal-internal.h"
 
 #define PRINT_THRESHOLD 128
 #define JSON_THRESHOLD 4096
diff --git a/src/shared/macro.h b/src/shared/macro.h
index 99dc733..cca41a3 100644
--- a/src/shared/macro.h
+++ b/src/shared/macro.h
@@ -45,6 +45,7 @@
 #define _weakref_(x) __attribute__((weakref(#x)))
 #define _introspect_(x) __attribute__((section("introspect." x)))
 #define _alignas_(x) __attribute__((aligned(__alignof(x))))
+#define _cleanup_(x) __attribute__((cleanup(x)))
 
 /* automake test harness */
 #define EXIT_TEST_SKIP 77
@@ -214,17 +215,6 @@ static inline size_t IOVEC_INCREMENT(struct iovec *i, unsigned n, size_t k) {
         return k;
 }
 
-#define _cleanup_free_ __attribute__((cleanup(freep)))
-#define _cleanup_fclose_ __attribute__((cleanup(fclosep)))
-#define _cleanup_pclose_ __attribute__((cleanup(pclosep)))
-#define _cleanup_close_ __attribute__((cleanup(closep)))
-#define _cleanup_closedir_ __attribute__((cleanup(closedirp)))
-#define _cleanup_umask_ __attribute__((cleanup(umaskp)))
-#define _cleanup_set_free_ __attribute__((cleanup(set_freep)))
-#define _cleanup_set_free_free_ __attribute__((cleanup(set_free_freep)))
-#define _cleanup_strv_free_ __attribute__((cleanup(strv_freep)))
-#define _cleanup_journal_close_ __attribute__((cleanup(journal_closep)))
-
 #define VA_FORMAT_ADVANCE(format, ap)                                   \
 do {                                                                    \
         int _argtypes[128];                                             \
diff --git a/src/shared/set.h b/src/shared/set.h
index 38c4b58..8864f7b 100644
--- a/src/shared/set.h
+++ b/src/shared/set.h
@@ -77,3 +77,6 @@ char **set_get_strv(Set *s);
 
 #define SET_FOREACH_BACKWARDS(e, s, i) \
         for ((i) = ITERATOR_LAST, (e) = set_iterate_backwards((s), &(i)); (e); (e) = set_iterate_backwards((s), &(i)))
+
+#define _cleanup_set_free_ _cleanup_(set_freep)
+#define _cleanup_set_free_free_ _cleanup_(set_free_freep)
diff --git a/src/shared/strv.h b/src/shared/strv.h
index 4cd3865..92696b0 100644
--- a/src/shared/strv.h
+++ b/src/shared/strv.h
@@ -34,6 +34,8 @@ static inline void strv_freep(char ***l) {
         strv_free(*l);
 }
 
+#define _cleanup_strv_free_ _cleanup_(strv_freep)
+
 char **strv_copy(char * const *l) _malloc_;
 unsigned strv_length(char * const *l);
 
diff --git a/src/shared/util.c b/src/shared/util.c
index 4eb6493..53caa7f 100644
--- a/src/shared/util.c
+++ b/src/shared/util.c
@@ -5277,26 +5277,6 @@ int get_home_dir(char **_h) {
         return 0;
 }
 
-void fclosep(FILE **f) {
-        if (*f)
-                fclose(*f);
-}
-
-void pclosep(FILE **f) {
-        if (*f)
-                pclose(*f);
-}
-
-void closep(int *fd) {
-        if (*fd >= 0)
-                close_nointr_nofail(*fd);
-}
-
-void closedirp(DIR **d) {
-        if (*d)
-                closedir(*d);
-}
-
 bool filename_is_safe(const char *p) {
 
         if (isempty(p))
diff --git a/src/shared/util.h b/src/shared/util.h
index 683ff5a..3aac165 100644
--- a/src/shared/util.h
+++ b/src/shared/util.h
@@ -38,7 +38,6 @@
 #include <stddef.h>
 #include <unistd.h>
 
-#include <systemd/sd-journal.h>
 #include "macro.h"
 #include "time-util.h"
 
@@ -527,19 +526,37 @@ static inline void freep(void *p) {
         free(*(void**) p);
 }
 
-void fclosep(FILE **f);
-void pclosep(FILE **f);
-void closep(int *fd);
-void closedirp(DIR **d);
-static inline void umaskp(mode_t *u) {
-        umask(*u);
+static inline void fclosep(FILE **f) {
+        if (*f)
+                fclose(*f);
+}
+
+static inline void pclosep(FILE **f) {
+        if (*f)
+                pclose(*f);
+}
+
+static inline void closep(int *fd) {
+        if (*fd >= 0)
+                close_nointr_nofail(*fd);
+}
+
+static inline void closedirp(DIR **d) {
+        if (*d)
+                closedir(*d);
 }
 
-static inline void journal_closep(sd_journal **j) {
-        sd_journal_close(*j);
+static inline void umaskp(mode_t *u) {
+        umask(*u);
 }
 
-#define _cleanup_globfree_ __attribute__((cleanup(globfree)))
+#define _cleanup_free_ _cleanup_(freep)
+#define _cleanup_fclose_ _cleanup_(fclosep)
+#define _cleanup_pclose_ _cleanup_(pclosep)
+#define _cleanup_close_ _cleanup_(closep)
+#define _cleanup_closedir_ _cleanup_(closedirp)
+#define _cleanup_umask_ _cleanup_(umaskp)
+#define _cleanup_globfree_ _cleanup_(globfree)
 
 _malloc_  static inline void *malloc_multiply(size_t a, size_t b) {
         if (_unlikely_(b == 0 || a > ((size_t) -1) / b))
@@ -608,7 +625,7 @@ int create_tmp_dir(char template[], char** dir_name);
 
 static inline void *mempset(void *s, int c, size_t n) {
         memset(s, c, n);
-        return (char*)s + n;
+        return (uint8_t*)s + n;
 }
 
 char *hexmem(const void *p, size_t l);
@@ -619,7 +636,7 @@ char *strrep(const char *s, unsigned n);
 
 void* greedy_realloc(void **p, size_t *allocated, size_t need);
 #define GREEDY_REALLOC(array, allocated, need) \
-        greedy_realloc((void**) &(array), &(allocated), (sizeof *array) * (need))
+        greedy_realloc((void**) &(array), &(allocated), sizeof((array)[0]) * (need))
 
 static inline void _reset_errno_(int *saved_errno) {
         errno = *saved_errno;



More information about the systemd-commits mailing list