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 <aclaudi@redhat.com>
Acked-by: Leon Romanovsky <leonro@nvidia.com>
Signed-off-by: David Ahern <dsahern@kernel.org>
This commit is contained in:
Andrea Claudi 2021-04-19 15:34:58 +02:00 committed by David Ahern
parent 75a35d50a3
commit 81bfd01a4c
5 changed files with 26 additions and 55 deletions

View 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[]);

View 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>");

View File

@ -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;
}

View 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)
{

View 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);