shared: procfs_file_alloca: handle pid==0

when pid is set to 0 use /proc/self
This commit is contained in:
Simon Peeters 2014-01-04 02:35:23 +01:00 committed by Zbigniew Jędrzejewski-Szmek
parent 9dddaedfcc
commit b68fa010f7
5 changed files with 19 additions and 44 deletions

View File

@ -33,7 +33,8 @@
static bool ignore_proc(pid_t pid) {
_cleanup_fclose_ FILE *f = NULL;
char c, *p;
char c;
const char *p;
size_t count;
uid_t uid;
int r;

View File

@ -46,9 +46,6 @@ int audit_session_from_pid(pid_t pid, uint32_t *id) {
if (detect_container(NULL) > 0)
return -ENOTSUP;
if (pid == 0)
p = "/proc/self/sessionid";
else
p = procfs_file_alloca(pid, "sessionid");
r = read_one_line_file(p, &s);
@ -78,9 +75,6 @@ int audit_loginuid_from_pid(pid_t pid, uid_t *uid) {
if (detect_container(NULL) > 0)
return -ENOTSUP;
if (pid == 0)
p = "/proc/self/loginuid";
else
p = procfs_file_alloca(pid, "loginuid");
r = read_one_line_file(p, &s);

View File

@ -746,9 +746,6 @@ int cg_pid_get_path(const char *controller, pid_t pid, char **path) {
} else
controller = SYSTEMD_CGROUP_CONTROLLER;
if (pid == 0)
fs = "/proc/self/cgroup";
else
fs = procfs_file_alloca(pid, "cgroup");
f = fopen(fs, "re");

View File

@ -495,9 +495,6 @@ int get_starttime_of_pid(pid_t pid, unsigned long long *st) {
assert(pid >= 0);
assert(st);
if (pid == 0)
p = "/proc/self/stat";
else
p = procfs_file_alloca(pid, "stat");
f = fopen(p, "re");
@ -573,9 +570,6 @@ int get_process_comm(pid_t pid, char **name) {
assert(name);
assert(pid >= 0);
if (pid == 0)
p = "/proc/self/comm";
else
p = procfs_file_alloca(pid, "comm");
r = read_one_line_file(p, name);
@ -594,9 +588,6 @@ int get_process_cmdline(pid_t pid, size_t max_length, bool comm_fallback, char *
assert(line);
assert(pid >= 0);
if (pid == 0)
p = "/proc/self/cmdline";
else
p = procfs_file_alloca(pid, "cmdline");
f = fopen(p, "re");
@ -716,9 +707,6 @@ int get_process_capeff(pid_t pid, char **capeff) {
assert(capeff);
assert(pid >= 0);
if (pid == 0)
p = "/proc/self/status";
else
p = procfs_file_alloca(pid, "status");
return get_status_field(p, "\nCapEff:", capeff);
@ -732,9 +720,6 @@ int get_process_exe(pid_t pid, char **name) {
assert(pid >= 0);
assert(name);
if (pid == 0)
p = "/proc/self/exe";
else
p = procfs_file_alloca(pid, "exe");
r = readlink_malloc(p, name);
@ -2549,9 +2534,6 @@ int get_ctty_devnr(pid_t pid, dev_t *d) {
assert(pid >= 0);
if (pid == 0)
fn = "/proc/self/stat";
else
fn = procfs_file_alloca(pid, "stat");
f = fopen(fn, "re");
@ -5095,9 +5077,6 @@ int getenv_for_pid(pid_t pid, const char *field, char **_value) {
assert(field);
assert(_value);
if (pid == 0)
path = "/proc/self/environ";
else
path = procfs_file_alloca(pid, "environ");
f = fopen(path, "re");

View File

@ -777,9 +777,13 @@ int unlink_noerrno(const char *path);
#define procfs_file_alloca(pid, field) \
({ \
pid_t _pid_ = (pid); \
char *_r_; \
_r_ = alloca(sizeof("/proc/") -1 + DECIMAL_STR_MAX(pid_t) + 1 + sizeof(field)); \
sprintf(_r_, "/proc/"PID_FMT"/" field, _pid_); \
const char *_r_; \
if (_pid_ == 0) { \
_r_ = ("/proc/self/" field); \
} else { \
_r_ = alloca(strlen("/proc/") + DECIMAL_STR_MAX(pid_t) + 1 + sizeof(field)); \
sprintf((char*) _r_, "/proc/"PID_FMT"/" field, _pid_); \
} \
_r_; \
})