summaryrefslogtreecommitdiff
path: root/src/shared
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 /src/shared
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.
Diffstat (limited to 'src/shared')
-rw-r--r--src/shared/util.c39
-rw-r--r--src/shared/util.h2
2 files changed, 37 insertions, 4 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_;