]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lib/procutils: add proc_get_cmdline()
authorKarel Zak <kzak@redhat.com>
Tue, 30 Jun 2015 14:08:52 +0000 (16:08 +0200)
committerKarel Zak <kzak@redhat.com>
Mon, 20 Jul 2015 09:48:09 +0000 (11:48 +0200)
Signed-off-by: Karel Zak <kzak@redhat.com>
include/procutils.h
lib/procutils.c

index 14b766cb7ee10ab05d300aef41d5f0701315f1e1..3040d197b209e82dae6334e21029046df0fc2613 100644 (file)
@@ -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 */
index 1823d16b1b1e9a73d2b6610df3c071f29f94252c..00b977c5aa76d7b7f9928688862fa499491c9397 100644 (file)
@@ -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;