]> git.ipfire.org Git - thirdparty/iproute2.git/commitdiff
lib: move get_task_name() from rdma
authorAndrea Claudi <aclaudi@redhat.com>
Mon, 19 Apr 2021 13:34:58 +0000 (15:34 +0200)
committerDavid Ahern <dsahern@kernel.org>
Thu, 22 Apr 2021 05:22:16 +0000 (05:22 +0000)
The function get_task_name() is used to get the name of a process from
its pid, and its implementation is similar to ip/iptuntap.c:pid_name().

Move it to lib/fs.c to use a single implementation and make it easily
reusable.

Signed-off-by: Andrea Claudi <aclaudi@redhat.com>
Acked-by: Leon Romanovsky <leonro@nvidia.com>
Signed-off-by: David Ahern <dsahern@kernel.org>
include/utils.h
ip/iptuntap.c
lib/fs.c
rdma/res.c
rdma/res.h

index b29c3798f898786f326cde87f55464fb251c7661..187444d52b4147156a722c318c7bb87ff16595c4 100644 (file)
@@ -308,6 +308,7 @@ char *find_cgroup2_mount(bool do_mount);
 __u64 get_cgroup2_id(const char *path);
 char *get_cgroup2_path(__u64 id, bool full);
 int get_command_name(const char *pid, char *comm, size_t len);
+char *get_task_name(pid_t pid);
 
 int get_rtnl_link_stats_rta(struct rtnl_link_stats64 *stats64,
                            struct rtattr *tb[]);
index e9cc7c0f5f702ebdc821ddd014aebc629aeeedc3..9cdb4a806c5a33f388f5182340c79c84862c0792 100644 (file)
@@ -260,35 +260,6 @@ static void print_flags(long flags)
        close_json_array(PRINT_JSON, NULL);
 }
 
-static char *pid_name(pid_t pid)
-{
-       char *comm;
-       FILE *f;
-       int err;
-
-       err = asprintf(&comm, "/proc/%d/comm", pid);
-       if (err < 0)
-               return NULL;
-
-       f = fopen(comm, "r");
-       free(comm);
-       if (!f) {
-               perror("fopen");
-               return NULL;
-       }
-
-       if (fscanf(f, "%ms\n", &comm) != 1) {
-               perror("fscanf");
-               comm = NULL;
-       }
-
-
-       if (fclose(f))
-               perror("fclose");
-
-       return comm;
-}
-
 static void show_processes(const char *name)
 {
        glob_t globbuf = { };
@@ -346,7 +317,7 @@ static void show_processes(const char *name)
                        } else if (err == 2 &&
                                   !strcmp("iff", key) &&
                                   !strcmp(name, value)) {
-                               char *pname = pid_name(pid);
+                               char *pname = get_task_name(pid);
 
                                print_string(PRINT_ANY, "name",
                                             "%s", pname ? : "<NULL>");
index ee0b130b6c33cbb488adb5e3b19be57f72737ab6..f161d888e3db472896efc7eb64100d092e1dd447 100644 (file)
--- a/lib/fs.c
+++ b/lib/fs.c
@@ -316,3 +316,27 @@ int get_command_name(const char *pid, char *comm, size_t len)
 
        return 0;
 }
+
+char *get_task_name(pid_t pid)
+{
+       char *comm;
+       FILE *f;
+
+       if (!pid)
+               return NULL;
+
+       if (asprintf(&comm, "/proc/%d/comm", pid) < 0)
+               return NULL;
+
+       f = fopen(comm, "r");
+       if (!f)
+               return NULL;
+
+       if (fscanf(f, "%ms\n", &comm) != 1)
+               comm = NULL;
+
+       fclose(f);
+
+       return comm;
+}
+
index dc12bbe4bffeb2e47362cf7550734fcfa1bad403..f42ae938f6f9a160a6fea1b806f39ddeb1df285a 100644 (file)
@@ -195,30 +195,6 @@ void print_qp_type(struct rd *rd, uint32_t val)
                           qp_types_to_str(val));
 }
 
-char *get_task_name(uint32_t pid)
-{
-       char *comm;
-       FILE *f;
-
-       if (!pid)
-               return NULL;
-
-       if (asprintf(&comm, "/proc/%d/comm", pid) < 0)
-               return NULL;
-
-       f = fopen(comm, "r");
-       free(comm);
-       if (!f)
-               return NULL;
-
-       if (fscanf(f, "%ms\n", &comm) != 1)
-               comm = NULL;
-
-       fclose(f);
-
-       return comm;
-}
-
 void print_key(struct rd *rd, const char *name, uint64_t val,
               struct nlattr *nlattr)
 {
index 707941daef8965bb2efa7c57f69e00c1410dcc31..e8bd02e4b2d93cc32a0bd2043dc63e714d5ff00a 100644 (file)
@@ -155,7 +155,6 @@ filters qp_valid_filters[MAX_NUMBER_OF_FILTERS] = {
 RES_FUNC(res_qp, RDMA_NLDEV_CMD_RES_QP_GET, qp_valid_filters, false,
         RDMA_NLDEV_ATTR_RES_LQPN);
 
-char *get_task_name(uint32_t pid);
 void print_dev(struct rd *rd, uint32_t idx, const char *name);
 void print_link(struct rd *rd, uint32_t idx, const char *name, uint32_t port,
                struct nlattr **nla_line);