]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lslocks: use stuff from lib/procutils
authorKarel Zak <kzak@redhat.com>
Wed, 25 Nov 2015 13:17:22 +0000 (14:17 +0100)
committerKarel Zak <kzak@redhat.com>
Wed, 25 Nov 2015 13:17:22 +0000 (14:17 +0100)
Signed-off-by: Karel Zak <kzak@redhat.com>
include/procutils.h
lib/procutils.c
misc-utils/lslocks.c

index 3040d197b209e82dae6334e21029046df0fc2613..9f8dd76ec1d05bba403e2bbef7020dffe5afeeb3 100644 (file)
@@ -29,5 +29,6 @@ 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);
+extern char *proc_get_command_name(pid_t pid);
 
 #endif /* UTIL_LINUX_PROCUTILS */
index 48ee7cf06eee306d5f24d6d0abb25c08e2bfb209..2e9a0537aa157703d2b42b36f4a7a4c942a373a6 100644 (file)
@@ -97,15 +97,15 @@ 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)
+/* returns process command path, use free() for result */
+static char *proc_file_strdup(pid_t pid, const char *name)
 {
        char buf[BUFSIZ], *res = NULL;
        ssize_t sz = 0;
        size_t i;
        int fd;
 
-       snprintf(buf, sizeof(buf), "/proc/%d/cmdline", (int) pid);
+       snprintf(buf, sizeof(buf), "/proc/%d/%s", (int) pid, name);
        fd = open(buf, O_RDONLY);
        if (fd < 0)
                goto done;
@@ -127,6 +127,18 @@ done:
        return res;
 }
 
+/* returns process command path, use free() for result */
+char *proc_get_command(pid_t pid)
+{
+       return proc_file_strdup(pid, "cmdline");
+}
+
+/* returns process command name, use free() for result */
+char *proc_get_command_name(pid_t pid)
+{
+       return proc_file_strdup(pid, "comm");
+}
+
 struct proc_processes *proc_open_processes(void)
 {
        struct proc_processes *ps;
index 35ca5c75a2b3bac04c51eb9e4d5d26f9589106d7..389a3e42f7b0f98888ba13c1e463edcd79be1ce5 100644 (file)
@@ -45,6 +45,7 @@
 #include "list.h"
 #include "closestream.h"
 #include "optutils.h"
+#include "procutils.h"
 
 /* column IDs */
 enum {
@@ -118,28 +119,6 @@ static void disable_columns_truncate(void)
                infos[i].flags &= ~SCOLS_FL_TRUNC;
 }
 
-/*
- * Return a PID's command name
- */
-static char *get_cmdname(pid_t id)
-{
-       FILE *fp;
-       char path[PATH_MAX], *ret = NULL;
-
-       sprintf(path, "/proc/%d/comm", id);
-       if (!(fp = fopen(path, "r")))
-               return NULL;
-
-       if (!fgets(path, sizeof(path), fp))
-               goto out;
-
-       path[strlen(path) - 1] = '\0';
-       ret = xstrdup(path);
-out:
-       fclose(fp);
-       return ret;
-}
-
 /*
  * Associate the device's mountpoint for a filename
  */
@@ -285,7 +264,7 @@ static int get_local_locks(struct list_head *locks)
                                 * to the list, no need to worry now.
                                 */
                                l->pid = strtos32_or_err(tok, _("failed to parse pid"));
-                               l->cmdname = get_cmdname(l->pid);
+                               l->cmdname = proc_get_command_name(l->pid);
                                if (!l->cmdname)
                                        l->cmdname = xstrdup(_("(unknown)"));
                                break;