mirror of
https://github.com/systemd/systemd.git
synced 2024-11-02 19:21:53 +03:00
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.
This commit is contained in:
parent
1c6eb4e36b
commit
ad450c3e04
@ -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;
|
||||
|
@ -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_;
|
||||
|
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user