From aad3a64d63db22db3e506de973f9577d8205554d Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Mon, 25 Nov 2019 14:55:50 +0100 Subject: [PATCH 1/3] process-util: tweak get_process_cwd() when calling for own process Let's bypass /proc if we can. --- src/basic/process-util.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/basic/process-util.c b/src/basic/process-util.c index aaec6a6a99..842c879c4e 100644 --- a/src/basic/process-util.c +++ b/src/basic/process-util.c @@ -21,8 +21,8 @@ #include "alloc-util.h" #include "architecture.h" -#include "escape.h" #include "env-util.h" +#include "escape.h" #include "fd-util.h" #include "fileio.h" #include "fs-util.h" @@ -34,6 +34,7 @@ #include "missing_sched.h" #include "missing_syscall.h" #include "namespace-util.h" +#include "path-util.h" #include "process-util.h" #include "raw-clone.h" #include "rlimit-util.h" @@ -500,6 +501,9 @@ int get_process_cwd(pid_t pid, char **cwd) { assert(pid >= 0); + if (pid == 0 || pid == getpid_cached()) + return safe_getcwd(cwd); + p = procfs_file_alloca(pid, "cwd"); return get_process_link_contents(p, cwd); From cde93ba2a5e2b33ebaaaa0358ddfcc35ef21d70d Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Mon, 25 Nov 2019 14:58:24 +0100 Subject: [PATCH 2/3] process-util: shortcut get_process_comm() for our own process Let's bypass /proc if we can. --- src/basic/process-util.c | 29 ++++++++++++++++++++--------- 1 file 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); From 5c7b99745a7375ebae125f27c2872919089c0d5c Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Mon, 25 Nov 2019 14:59:01 +0100 Subject: [PATCH 3/3] process-util: shortcut get_process_state() for our own process --- src/basic/process-util.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/basic/process-util.c b/src/basic/process-util.c index 003aecbc49..9b6c4c31f7 100644 --- a/src/basic/process-util.c +++ b/src/basic/process-util.c @@ -52,13 +52,17 @@ #define COMM_MAX_LEN 128 static int get_process_state(pid_t pid) { + _cleanup_free_ char *line = NULL; const char *p; char state; int r; - _cleanup_free_ char *line = NULL; assert(pid >= 0); + /* Shortcut: if we are enquired about our own state, we are obviously running */ + if (pid == 0 || pid == getpid_cached()) + return (unsigned char) 'R'; + p = procfs_file_alloca(pid, "stat"); r = read_one_line_file(p, &line);