[systemd-devel] [PATCH 2/2] RFC: journald: Avoid reading /proc/PID/cgroup more than once per msg

Holger Hans Peter Freyther holger at freyther.de
Thu Jun 27 09:30:11 PDT 2013


From: Holger Hans Peter Freyther <holger at moiji-mobile.com>

---
 src/journal/journald-server.c |   45 ++++++++++++++++++++++-------------------
 1 file changed, 24 insertions(+), 21 deletions(-)

diff --git a/src/journal/journald-server.c b/src/journal/journald-server.c
index b08aa1d..e651a6d 100644
--- a/src/journal/journald-server.c
+++ b/src/journal/journald-server.c
@@ -523,7 +523,8 @@ static void dispatch_message_real(
                 struct timeval *tv,
                 const char *label, size_t label_len,
                 const char *unit_id,
-                pid_t object_pid) {
+                pid_t object_pid,
+                const char *cached_path) {
 
         char    pid[sizeof("_PID=") + DECIMAL_STR_MAX(pid_t)],
                 uid[sizeof("_UID=") + DECIMAL_STR_MAX(uid_t)],
@@ -541,7 +542,7 @@ static void dispatch_message_real(
         char *x;
         sd_id128_t id;
         int r;
-        char *t, *c;
+        char *t, *path;
         uid_t realuid = 0, owner = 0, journal_uid;
         bool owner_valid = false;
 #ifdef HAVE_AUDIT
@@ -606,31 +607,30 @@ static void dispatch_message_real(
                 }
 #endif
 
-                r = cg_pid_get_path_shifted_with_root(ucred->pid, NULL, &c, cached_cg_root());
-                if (r >= 0) {
+                if (cached_path) {
                         char *session = NULL;
 
-                        x = strappenda("_SYSTEMD_CGROUP=", c);
+                        x = strappenda("_SYSTEMD_CGROUP=", cached_path);
                         IOVEC_SET_STRING(iovec[n++], x);
 
-                        r = cg_path_get_session(c, &t);
+                        r = cg_path_get_session(cached_path, &t);
                         if (r >= 0) {
                                 session = strappenda("_SYSTEMD_SESSION=", t);
                                 free(t);
                                 IOVEC_SET_STRING(iovec[n++], session);
                         }
 
-                        if (cg_path_get_owner_uid(c, &owner) >= 0) {
+                        if (cg_path_get_owner_uid(cached_path, &owner) >= 0) {
                                 owner_valid = true;
 
                                 sprintf(owner_uid, "_SYSTEMD_OWNER_UID=%lu", (unsigned long) owner);
                                 IOVEC_SET_STRING(iovec[n++], owner_uid);
                         }
 
-                        if (cg_path_get_unit(c, &t) >= 0) {
+                        if (cg_path_get_unit(cached_path, &t) >= 0) {
                                 x = strappenda("_SYSTEMD_UNIT=", t);
                                 free(t);
-                        } else if (cg_path_get_user_unit(c, &t) >= 0) {
+                        } else if (cg_path_get_user_unit(cached_path, &t) >= 0) {
                                 x = strappenda("_SYSTEMD_USER_UNIT=", t);
                                 free(t);
                         } else if (unit_id) {
@@ -643,8 +643,6 @@ static void dispatch_message_real(
 
                         if (x)
                                 IOVEC_SET_STRING(iovec[n++], x);
-
-                        free(c);
                 }
 
 #ifdef HAVE_SELINUX
@@ -715,27 +713,27 @@ static void dispatch_message_real(
                 }
 #endif
 
-                r = cg_pid_get_path_shifted_with_root(object_pid, NULL, &c, cached_cg_root());
+                r = cg_pid_get_path_shifted_with_root(object_pid, NULL, &path, cached_cg_root());
                 if (r >= 0) {
-                        x = strappenda("OBJECT_SYSTEMD_CGROUP=", c);
+                        x = strappenda("OBJECT_SYSTEMD_CGROUP=", path);
                         IOVEC_SET_STRING(iovec[n++], x);
 
-                        r = cg_path_get_session(c, &t);
+                        r = cg_path_get_session(path, &t);
                         if (r >= 0) {
                                 x = strappenda("OBJECT_SYSTEMD_SESSION=", t);
                                 free(t);
                                 IOVEC_SET_STRING(iovec[n++], x);
                         }
 
-                        if (cg_path_get_owner_uid(c, &owner) >= 0) {
+                        if (cg_path_get_owner_uid(path, &owner) >= 0) {
                                 sprintf(o_owner_uid, "OBJECT_SYSTEMD_OWNER_UID=%lu", (unsigned long) owner);
                                 IOVEC_SET_STRING(iovec[n++], o_owner_uid);
                         }
 
-                        if (cg_path_get_unit(c, &t) >= 0) {
+                        if (cg_path_get_unit(path, &t) >= 0) {
                                 x = strappenda("OBJECT_SYSTEMD_UNIT=", t);
                                 free(t);
-                        } else if (cg_path_get_user_unit(c, &t) >= 0) {
+                        } else if (cg_path_get_user_unit(path, &t) >= 0) {
                                 x = strappenda("OBJECT_SYSTEMD_USER_UNIT=", t);
                                 free(t);
                         } else
@@ -744,7 +742,7 @@ static void dispatch_message_real(
                         if (x)
                                 IOVEC_SET_STRING(iovec[n++], x);
 
-                        free(c);
+                        free(path);
                 }
         }
         assert(n <= m);
@@ -799,9 +797,10 @@ void server_driver_message(Server *s, sd_id128_t message_id, const char *format,
         char mid[11 + 32 + 1];
         char buffer[16 + LINE_MAX + 1];
         struct iovec iovec[N_IOVEC_META_FIELDS + 4];
-        int n = 0;
+        int n = 0, r;
         va_list ap;
         struct ucred ucred = {};
+        _cleanup_free_ char *p = NULL;
 
         assert(s);
         assert(format);
@@ -826,7 +825,11 @@ void server_driver_message(Server *s, sd_id128_t message_id, const char *format,
         ucred.uid = getuid();
         ucred.gid = getgid();
 
-        dispatch_message_real(s, iovec, n, ELEMENTSOF(iovec), &ucred, NULL, NULL, 0, NULL, 0);
+        r = cg_pid_get_path_shifted_with_root(ucred.pid, NULL, &p, cached_cg_root());
+        if (r < 0)
+                p = NULL;
+
+        dispatch_message_real(s, iovec, n, ELEMENTSOF(iovec), &ucred, NULL, NULL, 0, NULL, 0, p);
 }
 
 void server_dispatch_message(
@@ -887,7 +890,7 @@ void server_dispatch_message(
                                       "Suppressed %u messages from %s", rl - 1, path);
 
 finish:
-        dispatch_message_real(s, iovec, n, m, ucred, tv, label, label_len, unit_id, object_pid);
+        dispatch_message_real(s, iovec, n, m, ucred, tv, label, label_len, unit_id, object_pid, path);
 }
 
 
-- 
1.7.10.4



More information about the systemd-devel mailing list