summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKarel Zak2015-06-30 16:08:52 +0200
committerKarel Zak2015-07-20 11:48:09 +0200
commitf6c01f4eed23ca05156810a6985e2a0799636b7a (patch)
treef156ced65a472b182e5f997750d00e20aeb384c7
parentinclude/all-io: read_all() don't retry on EOF (diff)
downloadkernel-qcow2-util-linux-f6c01f4eed23ca05156810a6985e2a0799636b7a.tar.gz
kernel-qcow2-util-linux-f6c01f4eed23ca05156810a6985e2a0799636b7a.tar.xz
kernel-qcow2-util-linux-f6c01f4eed23ca05156810a6985e2a0799636b7a.zip
lib/procutils: add proc_get_cmdline()
Signed-off-by: Karel Zak <kzak@redhat.com>
-rw-r--r--include/procutils.h1
-rw-r--r--lib/procutils.c31
2 files changed, 32 insertions, 0 deletions
diff --git a/include/procutils.h b/include/procutils.h
index 14b766cb7..3040d197b 100644
--- a/include/procutils.h
+++ b/include/procutils.h
@@ -28,5 +28,6 @@ extern void proc_processes_filter_by_name(struct proc_processes *ps, const char
extern void proc_processes_filter_by_uid(struct proc_processes *ps, uid_t uid);
extern int proc_next_pid(struct proc_processes *ps, pid_t *pid);
+extern char *proc_get_command(pid_t pid);
#endif /* UTIL_LINUX_PROCUTILS */
diff --git a/lib/procutils.c b/lib/procutils.c
index 1823d16b1..00b977c5a 100644
--- a/lib/procutils.c
+++ b/lib/procutils.c
@@ -24,6 +24,7 @@
#include "procutils.h"
#include "at.h"
+#include "all-io.h"
#include "c.h"
/*
@@ -96,6 +97,36 @@ int proc_next_tid(struct proc_tasks *tasks, pid_t *tid)
return 0;
}
+/* returns process command name, use free() for result */
+char *proc_get_command(pid_t pid)
+{
+ char buf[BUFSIZ], *res = NULL;
+ ssize_t sz = 0;
+ size_t i;
+ int fd = -1;
+
+ snprintf(buf, sizeof(buf), "/proc/%d/cmdline", (int) pid);
+ fd = open(buf, O_RDONLY);
+ if (fd < 0)
+ goto done;
+
+ sz = read_all(fd, buf, sizeof(buf));
+ if (sz <= 0)
+ goto done;
+
+ for (i = 0; i < (size_t) sz; i++) {
+
+ if (buf[i] == '\0')
+ buf[i] = ' ';
+ }
+ buf[sz - 1] = '\0';
+ res = strdup(buf);
+done:
+ if (fd >= 0)
+ close(fd);
+ return res;
+}
+
struct proc_processes *proc_open_processes(void)
{
struct proc_processes *ps;