[systemd-commits] 2 commits - src/core src/shared

Zbigniew Jędrzejewski-Szmek zbyszek at kemper.freedesktop.org
Sat Jan 4 22:00:48 PST 2014


 src/core/killall.c       |    3 +
 src/shared/audit.c       |   10 +----
 src/shared/cgroup-util.c |    5 --
 src/shared/util.c        |   81 ++++++++++++-----------------------------------
 src/shared/util.h        |   10 ++++-
 5 files changed, 33 insertions(+), 76 deletions(-)

New commits:
commit b4696bce07e0a596accf097b7bff84bf30198f25
Author: Simon Peeters <peeters.simon at gmail.com>
Date:   Sat Jan 4 02:35:26 2014 +0100

    util.c: use read_one_line_file where possible

diff --git a/src/shared/util.c b/src/shared/util.c
index 50dac70..c824c39 100644
--- a/src/shared/util.c
+++ b/src/shared/util.c
@@ -440,8 +440,7 @@ 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 line[LINE_MAX];
+        _cleanup_free_ char *line = NULL;
         long unsigned ppid;
         const char *p;
 
@@ -454,14 +453,9 @@ int get_parent_of_pid(pid_t pid, pid_t *_ppid) {
         }
 
         p = procfs_file_alloca(pid, "stat");
-        f = fopen(p, "re");
-        if (!f)
-                return -errno;
-
-        if (!fgets(line, sizeof(line), f)) {
-                r = feof(f) ? -EIO : -errno;
+        r = read_one_line_file(p, &line);
+        if (r < 0)
                 return r;
-        }
 
         /* Let's skip the pid and comm fields. The latter is enclosed
          * in () but does not escape any () in its value, so let's
@@ -488,25 +482,17 @@ 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 line[LINE_MAX];
+        int r;
+        _cleanup_free_ char *line = NULL;
         const char *p;
 
         assert(pid >= 0);
         assert(st);
 
         p = procfs_file_alloca(pid, "stat");
-
-        f = fopen(p, "re");
-        if (!f)
-                return errno == ENOENT ? -ESRCH : -errno;
-
-        if (!fgets(line, sizeof(line), f)) {
-                if (ferror(f))
-                        return -errno;
-
-                return -EIO;
-        }
+        r = read_one_line_file(p, &line);
+        if (r < 0)
+                return r;
 
         /* Let's skip the pid and comm fields. The latter is enclosed
          * in () but does not escape any () in its value, so let's
@@ -2527,21 +2513,17 @@ int getttyname_harder(int fd, char **r) {
 }
 
 int get_ctty_devnr(pid_t pid, dev_t *d) {
-        _cleanup_fclose_ FILE *f = NULL;
-        char line[LINE_MAX], *p;
+        int r;
+        _cleanup_free_ char *line = NULL;
+        const char *p;
         unsigned long ttynr;
-        const char *fn;
 
         assert(pid >= 0);
 
-        fn = procfs_file_alloca(pid, "stat");
-
-        f = fopen(fn, "re");
-        if (!f)
-                return -errno;
-
-        if (!fgets(line, sizeof(line), f))
-                return feof(f) ? -EIO : -errno;
+        p = procfs_file_alloca(pid, "stat");
+        r = read_one_line_file(p, &line);
+        if (r < 0)
+                return r;
 
         p = strrchr(line, ')');
         if (!p)

commit b68fa010f72599e6da5822feda5ae3a47a4e63d8
Author: Simon Peeters <peeters.simon at gmail.com>
Date:   Sat Jan 4 02:35:23 2014 +0100

    shared: procfs_file_alloca: handle pid==0
    
    when pid is set to 0 use /proc/self

diff --git a/src/core/killall.c b/src/core/killall.c
index 7664775..57ed41c 100644
--- a/src/core/killall.c
+++ b/src/core/killall.c
@@ -33,7 +33,8 @@
 
 static bool ignore_proc(pid_t pid) {
         _cleanup_fclose_ FILE *f = NULL;
-        char c, *p;
+        char c;
+        const char *p;
         size_t count;
         uid_t uid;
         int r;
diff --git a/src/shared/audit.c b/src/shared/audit.c
index 9ab4640..8038ac3 100644
--- a/src/shared/audit.c
+++ b/src/shared/audit.c
@@ -46,10 +46,7 @@ int audit_session_from_pid(pid_t pid, uint32_t *id) {
         if (detect_container(NULL) > 0)
                 return -ENOTSUP;
 
-        if (pid == 0)
-                p = "/proc/self/sessionid";
-        else
-                p = procfs_file_alloca(pid, "sessionid");
+        p = procfs_file_alloca(pid, "sessionid");
 
         r = read_one_line_file(p, &s);
         if (r < 0)
@@ -78,10 +75,7 @@ int audit_loginuid_from_pid(pid_t pid, uid_t *uid) {
         if (detect_container(NULL) > 0)
                 return -ENOTSUP;
 
-        if (pid == 0)
-                p = "/proc/self/loginuid";
-        else
-                p = procfs_file_alloca(pid, "loginuid");
+        p = procfs_file_alloca(pid, "loginuid");
 
         r = read_one_line_file(p, &s);
         if (r < 0)
diff --git a/src/shared/cgroup-util.c b/src/shared/cgroup-util.c
index f2af8dc..855c9cd 100644
--- a/src/shared/cgroup-util.c
+++ b/src/shared/cgroup-util.c
@@ -746,10 +746,7 @@ int cg_pid_get_path(const char *controller, pid_t pid, char **path) {
         } else
                 controller = SYSTEMD_CGROUP_CONTROLLER;
 
-        if (pid == 0)
-                fs = "/proc/self/cgroup";
-        else
-                fs = procfs_file_alloca(pid, "cgroup");
+        fs = procfs_file_alloca(pid, "cgroup");
 
         f = fopen(fs, "re");
         if (!f)
diff --git a/src/shared/util.c b/src/shared/util.c
index f491708..50dac70 100644
--- a/src/shared/util.c
+++ b/src/shared/util.c
@@ -495,10 +495,7 @@ int get_starttime_of_pid(pid_t pid, unsigned long long *st) {
         assert(pid >= 0);
         assert(st);
 
-        if (pid == 0)
-                p = "/proc/self/stat";
-        else
-                p = procfs_file_alloca(pid, "stat");
+        p = procfs_file_alloca(pid, "stat");
 
         f = fopen(p, "re");
         if (!f)
@@ -573,10 +570,7 @@ int get_process_comm(pid_t pid, char **name) {
         assert(name);
         assert(pid >= 0);
 
-        if (pid == 0)
-                p = "/proc/self/comm";
-        else
-                p = procfs_file_alloca(pid, "comm");
+        p = procfs_file_alloca(pid, "comm");
 
         r = read_one_line_file(p, name);
         if (r == -ENOENT)
@@ -594,10 +588,7 @@ int get_process_cmdline(pid_t pid, size_t max_length, bool comm_fallback, char *
         assert(line);
         assert(pid >= 0);
 
-        if (pid == 0)
-                p = "/proc/self/cmdline";
-        else
-                p = procfs_file_alloca(pid, "cmdline");
+        p = procfs_file_alloca(pid, "cmdline");
 
         f = fopen(p, "re");
         if (!f)
@@ -716,10 +707,7 @@ int get_process_capeff(pid_t pid, char **capeff) {
         assert(capeff);
         assert(pid >= 0);
 
-        if (pid == 0)
-                p = "/proc/self/status";
-        else
-                p = procfs_file_alloca(pid, "status");
+        p = procfs_file_alloca(pid, "status");
 
         return get_status_field(p, "\nCapEff:", capeff);
 }
@@ -732,10 +720,7 @@ int get_process_exe(pid_t pid, char **name) {
         assert(pid >= 0);
         assert(name);
 
-        if (pid == 0)
-                p = "/proc/self/exe";
-        else
-                p = procfs_file_alloca(pid, "exe");
+        p = procfs_file_alloca(pid, "exe");
 
         r = readlink_malloc(p, name);
         if (r < 0)
@@ -2549,10 +2534,7 @@ int get_ctty_devnr(pid_t pid, dev_t *d) {
 
         assert(pid >= 0);
 
-        if (pid == 0)
-                fn = "/proc/self/stat";
-        else
-                fn = procfs_file_alloca(pid, "stat");
+        fn = procfs_file_alloca(pid, "stat");
 
         f = fopen(fn, "re");
         if (!f)
@@ -5095,10 +5077,7 @@ int getenv_for_pid(pid_t pid, const char *field, char **_value) {
         assert(field);
         assert(_value);
 
-        if (pid == 0)
-                path = "/proc/self/environ";
-        else
-                path = procfs_file_alloca(pid, "environ");
+        path = procfs_file_alloca(pid, "environ");
 
         f = fopen(path, "re");
         if (!f)
diff --git a/src/shared/util.h b/src/shared/util.h
index d9720d0..8645654 100644
--- a/src/shared/util.h
+++ b/src/shared/util.h
@@ -777,9 +777,13 @@ int unlink_noerrno(const char *path);
 #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/"PID_FMT"/" field, _pid_); \
+                const char *_r_;                                        \
+                if (_pid_ == 0) {                                       \
+                        _r_ = ("/proc/self/" field);                    \
+                } else {                                                \
+                        _r_ = alloca(strlen("/proc/") + DECIMAL_STR_MAX(pid_t) + 1 + sizeof(field)); \
+                        sprintf((char*) _r_, "/proc/"PID_FMT"/" field, _pid_);                       \
+                }                                                       \
                 _r_;                                                    \
         })
 



More information about the systemd-commits mailing list