From ef1673d16907726d83bdff2e57b5261997a85020 Mon Sep 17 00:00:00 2001 From: Mirco Tischler Date: Thu, 17 Jan 2013 18:55:05 +0100 Subject: journal: log _SYSTEMD_USER_UNIT for user session units --- src/journal/journald-server.c | 11 +++- src/shared/cgroup-util.c | 122 ++++++++++++++++++++++++++++++++---------- src/shared/cgroup-util.h | 1 + 3 files changed, 105 insertions(+), 29 deletions(-) (limited to 'src') diff --git a/src/journal/journald-server.c b/src/journal/journald-server.c index ec9be65575..12a46e6bd0 100644 --- a/src/journal/journald-server.c +++ b/src/journal/journald-server.c @@ -601,8 +601,15 @@ static void dispatch_message_real( if (cg_pid_get_unit(ucred->pid, &t) >= 0) { unit = strappend("_SYSTEMD_UNIT=", t); free(t); - } else if (unit_id) - unit = strappend("_SYSTEMD_UNIT=", unit_id); + } else if (cg_pid_get_user_unit(ucred->pid, &t) >= 0) { + unit = strappend("_SYSTEMD_USER_UNIT=", t); + free(t); + } else if (unit_id) { + if (session) + unit = strappend("_SYSTEMD_USER_UNIT=", unit_id); + else + unit = strappend("_SYSTEMD_UNIT=", unit_id); + } if (unit) IOVEC_SET_STRING(iovec[n++], unit); diff --git a/src/shared/cgroup-util.c b/src/shared/cgroup-util.c index 9dfab2eaac..3d12233a1b 100644 --- a/src/shared/cgroup-util.c +++ b/src/shared/cgroup-util.c @@ -36,6 +36,7 @@ #include "util.h" #include "path-util.h" #include "strv.h" +#include "unit-name.h" int cg_enumerate_processes(const char *controller, const char *path, FILE **_f) { char *fs; @@ -1210,11 +1211,76 @@ int cg_pid_get_cgroup(pid_t pid, char **root, char **cgroup) { return 0; } -int cg_pid_get_unit(pid_t pid, char **unit) { +static int instance_unit_from_cgroup(char **cgroup){ + char *at; + + assert(cgroup); + + at = memchr(*cgroup, '@', strlen(*cgroup)); + if (at && at[1] == '.') { + char *i, *s; + + /* This is a templated service */ + i = memchr(at, '/', strlen(at)); + if(!i) + return -EIO; + + s = strndup(at + 1, i - at); + if (!s) + return -ENOMEM; + + i = strdup(i + 1); + if (!i) { + free(s); + return -ENOMEM; + } + + strcpy(at + 1, i); + strcpy(at + strlen(i) + 1, s); + at[strlen(at) - 1] = '\0'; + + free(i); + free(s); + } + + return 0; +} + +static int cgroup_to_unit(char *cgroup, char **unit){ int r; - char *cgroup, *p, *at, *b; + char *b, *p; size_t k; + assert(cgroup); + assert(unit); + + r = instance_unit_from_cgroup(&cgroup); + if (r < 0) + return r; + + p = strrchr(cgroup, '/') + 1; + k = strlen(p); + + b = strndup(p, k); + + if (!b) + return -ENOMEM; + + r = unit_name_is_valid(b, true); + if (!r) { + free(b); + return -ENOENT; + } + + *unit = b; + + return 0; +} + +int cg_pid_get_unit(pid_t pid, char **unit) { + int r; + char *cgroup; + assert(pid >= 0); assert(unit); @@ -1227,38 +1293,40 @@ int cg_pid_get_unit(pid_t pid, char **unit) { return -ENOENT; } - p = cgroup + 8; - k = strcspn(p, "/"); + r = cgroup_to_unit(cgroup, unit); + if (r < 0){ + free(cgroup); + return r; + } - at = memchr(p, '@', k); - if (at && at[1] == '.') { - size_t j; + free(cgroup); - /* This is a templated service */ - if (p[k] != '/') { - free(cgroup); - return -EIO; - } + return 0; +} - j = strcspn(p+k+1, "/"); +int cg_pid_get_user_unit(pid_t pid, char **unit) { + int r; + char *cgroup; - b = malloc(k + j + 1); + assert(pid >= 0); + assert(unit); - if (b) { - memcpy(b, p, at - p + 1); - memcpy(b + (at - p) + 1, p + k + 1, j); - memcpy(b + (at - p) + 1 + j, at + 1, k - (at - p) - 1); - b[k+j] = 0; - } - } else - b = strndup(p, k); + r = cg_pid_get_cgroup(pid, NULL, &cgroup); + if (r < 0) + return r; - free(cgroup); + if (!startswith(cgroup, "/user/")) { + free(cgroup); + return -ENOENT; + } - if (!b) - return -ENOMEM; + r = cgroup_to_unit(cgroup, unit); + if (r < 0) { + free(cgroup); + return r; + } - *unit = b; - return 0; + free(cgroup); + return 0; } diff --git a/src/shared/cgroup-util.h b/src/shared/cgroup-util.h index f663fba687..2429ba2430 100644 --- a/src/shared/cgroup-util.h +++ b/src/shared/cgroup-util.h @@ -71,5 +71,6 @@ int cg_is_empty_recursive(const char *controller, const char *path, bool ignore_ int cg_get_user_path(char **path); int cg_pid_get_cgroup(pid_t pid, char **root, char **cgroup); int cg_pid_get_unit(pid_t pid, char **unit); +int cg_pid_get_user_unit(pid_t pid, char **unit); char **cg_shorten_controllers(char **controllers); -- cgit v1.2.3-54-g00ecf