summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakub Filak <jfilak@redhat.com>2014-11-19 11:01:19 +0100
committerLennart Poettering <lennart@poettering.net>2014-11-21 00:32:02 +0100
commitad450c3e0418fe23e2896e581575cb06564ca29a (patch)
tree3f85df33ef5f286f750f904c5b6dc05aa336c838
parent1c6eb4e36bf88015d0bbe8fae417622e6132b864 (diff)
util: add functions getting proc cwd and root
/proc/[pid]/cwd and /proc/[pid]/root are symliks to corresponding directories The added functions returns values of that symlinks.
-rw-r--r--src/shared/util.c39
-rw-r--r--src/shared/util.h2
-rw-r--r--src/test/test-util.c13
3 files changed, 49 insertions, 5 deletions
diff --git a/src/shared/util.c b/src/shared/util.c
index eeced4769a..01660526e4 100644
--- a/src/shared/util.c
+++ b/src/shared/util.c
@@ -797,19 +797,30 @@ int get_process_capeff(pid_t pid, char **capeff) {
return get_status_field(p, "\nCapEff:", capeff);
}
+static int get_process_link_contents(const char *proc_file, char **name) {
+ int r;
+
+ assert(proc_file);
+ assert(name);
+
+ r = readlink_malloc(proc_file, name);
+ if (r < 0)
+ return r == -ENOENT ? -ESRCH : r;
+
+ return 0;
+}
+
int get_process_exe(pid_t pid, char **name) {
const char *p;
char *d;
int r;
assert(pid >= 0);
- assert(name);
p = procfs_file_alloca(pid, "exe");
-
- r = readlink_malloc(p, name);
+ r = get_process_link_contents(p, name);
if (r < 0)
- return r == -ENOENT ? -ESRCH : r;
+ return r;
d = endswith(*name, " (deleted)");
if (d)
@@ -861,6 +872,26 @@ int get_process_gid(pid_t pid, gid_t *gid) {
return get_process_id(pid, "Gid:", gid);
}
+int get_process_cwd(pid_t pid, char **cwd) {
+ const char *p;
+
+ assert(pid >= 0);
+
+ p = procfs_file_alloca(pid, "cwd");
+
+ return get_process_link_contents(p, cwd);
+}
+
+int get_process_root(pid_t pid, char **root) {
+ const char *p;
+
+ assert(pid >= 0);
+
+ p = procfs_file_alloca(pid, "root");
+
+ return get_process_link_contents(p, root);
+}
+
char *strnappend(const char *s, const char *suffix, size_t b) {
size_t a;
char *r;
diff --git a/src/shared/util.h b/src/shared/util.h
index 835fee496d..fc59481b99 100644
--- a/src/shared/util.h
+++ b/src/shared/util.h
@@ -295,6 +295,8 @@ int get_process_exe(pid_t pid, char **name);
int get_process_uid(pid_t pid, uid_t *uid);
int get_process_gid(pid_t pid, gid_t *gid);
int get_process_capeff(pid_t pid, char **capeff);
+int get_process_cwd(pid_t pid, char **cwd);
+int get_process_root(pid_t pid, char **root);
char hexchar(int x) _const_;
int unhexchar(char c) _const_;
diff --git a/src/test/test-util.c b/src/test/test-util.c
index 01e10c9fc1..b33e15aef0 100644
--- a/src/test/test-util.c
+++ b/src/test/test-util.c
@@ -490,13 +490,14 @@ static void test_u64log2(void) {
static void test_get_process_comm(void) {
struct stat st;
- _cleanup_free_ char *a = NULL, *c = NULL, *d = NULL, *f = NULL, *i = NULL;
+ _cleanup_free_ char *a = NULL, *c = NULL, *d = NULL, *f = NULL, *i = NULL, *cwd = NULL, *root = NULL;
unsigned long long b;
pid_t e;
uid_t u;
gid_t g;
dev_t h;
int r;
+ pid_t me;
if (stat("/proc/1/comm", &st) == 0) {
assert_se(get_process_comm(1, &a) >= 0);
@@ -532,6 +533,16 @@ static void test_get_process_comm(void) {
log_info("pid1 gid: "GID_FMT, g);
assert_se(g == 0);
+ me = getpid();
+
+ r = get_process_cwd(me, &cwd);
+ assert_se(r >= 0 || r == -EACCES);
+ log_info("pid1 cwd: '%s'", cwd);
+
+ r = get_process_root(me, &root);
+ assert_se(r >= 0 || r == -EACCES);
+ log_info("pid1 root: '%s'", root);
+
assert_se(get_ctty_devnr(1, &h) == -ENOENT);
getenv_for_pid(1, "PATH", &i);