From: Andrea Claudi Date: Mon, 19 Apr 2021 13:34:58 +0000 (+0200) Subject: lib: move get_task_name() from rdma X-Git-Tag: v5.13.0~9^2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=81bfd01a4c9e6824c947b8f972faf9c136cd387b;p=thirdparty%2Fiproute2.git lib: move get_task_name() from rdma 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 Acked-by: Leon Romanovsky Signed-off-by: David Ahern --- diff --git a/include/utils.h b/include/utils.h index b29c3798f..187444d52 100644 --- a/include/utils.h +++ b/include/utils.h @@ -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[]); diff --git a/ip/iptuntap.c b/ip/iptuntap.c index e9cc7c0f5..9cdb4a806 100644 --- a/ip/iptuntap.c +++ b/ip/iptuntap.c @@ -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 ? : ""); diff --git a/lib/fs.c b/lib/fs.c index ee0b130b6..f161d888e 100644 --- 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; +} + diff --git a/rdma/res.c b/rdma/res.c index dc12bbe4b..f42ae938f 100644 --- a/rdma/res.c +++ b/rdma/res.c @@ -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) { diff --git a/rdma/res.h b/rdma/res.h index 707941dae..e8bd02e4b 100644 --- a/rdma/res.h +++ b/rdma/res.h @@ -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);