summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2019-11-25 14:58:24 +0100
committerLennart Poettering <lennart@poettering.net>2019-11-25 19:23:31 +0100
commitcde93ba2a5e2b33ebaaaa0358ddfcc35ef21d70d (patch)
tree382d430be16fdec5bab1f93ccf700b5b49f4964c
parentprocess-util: tweak get_process_cwd() when calling for own process (diff)
downloadsystemd-cde93ba2a5e2b33ebaaaa0358ddfcc35ef21d70d.tar.xz
systemd-cde93ba2a5e2b33ebaaaa0358ddfcc35ef21d70d.zip
process-util: shortcut get_process_comm() for our own process
Let's bypass /proc if we can.
-rw-r--r--src/basic/process-util.c29
1 files changed, 20 insertions, 9 deletions
diff --git a/src/basic/process-util.c b/src/basic/process-util.c
index 842c879c4e..003aecbc49 100644
--- a/src/basic/process-util.c
+++ b/src/basic/process-util.c
@@ -81,24 +81,35 @@ static int get_process_state(pid_t pid) {
int get_process_comm(pid_t pid, char **ret) {
_cleanup_free_ char *escaped = NULL, *comm = NULL;
- const char *p;
int r;
assert(ret);
assert(pid >= 0);
+ if (pid == 0 || pid == getpid_cached()) {
+ comm = new0(char, TASK_COMM_LEN + 1); /* Must fit in 16 byte according to prctl(2) */
+ if (!comm)
+ return -ENOMEM;
+
+ if (prctl(PR_GET_NAME, comm) < 0)
+ return -errno;
+ } else {
+ const char *p;
+
+ p = procfs_file_alloca(pid, "comm");
+
+ /* Note that process names of kernel threads can be much longer than TASK_COMM_LEN */
+ r = read_one_line_file(p, &comm);
+ if (r == -ENOENT)
+ return -ESRCH;
+ if (r < 0)
+ return r;
+ }
+
escaped = new(char, COMM_MAX_LEN);
if (!escaped)
return -ENOMEM;
- p = procfs_file_alloca(pid, "comm");
-
- r = read_one_line_file(p, &comm);
- if (r == -ENOENT)
- return -ESRCH;
- if (r < 0)
- return r;
-
/* Escape unprintable characters, just in case, but don't grow the string beyond the underlying size */
cellescape(escaped, COMM_MAX_LEN, comm);