__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_task_name(pid_t pid, char *name, size_t len);
int get_rtnl_link_stats_rta(struct rtnl_link_stats64 *stats64,
struct rtattr *tb[]);
} else if (err == 2 &&
!strcmp("iff", key) &&
!strcmp(name, value)) {
- char *pname = get_task_name(pid);
+ SPRINT_BUF(pname);
- print_string(PRINT_ANY, "name",
- "%s", pname ? : "<NULL>");
+ if (get_task_name(pid, pname, sizeof(pname)))
+ print_string(PRINT_ANY, "name",
+ "%s", "<NULL>");
+ else
+ print_string(PRINT_ANY, "name",
+ "%s", pname);
- print_uint(PRINT_ANY, "pid",
- "(%d)", pid);
- free(pname);
+ print_uint(PRINT_ANY, "pid", "(%d)", pid);
}
free(key);
return 0;
}
-char *get_task_name(pid_t pid)
+int get_task_name(pid_t pid, char *name, size_t len)
{
- char *comm;
+ char path[PATH_MAX];
FILE *f;
if (!pid)
- return NULL;
+ return -1;
- if (asprintf(&comm, "/proc/%d/comm", pid) < 0)
- return NULL;
+ if (snprintf(path, sizeof(path), "/proc/%d/comm", pid) >= sizeof(path))
+ return -1;
- f = fopen(comm, "r");
+ f = fopen(path, "r");
if (!f)
- return NULL;
+ return -1;
- if (fscanf(f, "%ms\n", &comm) != 1)
- comm = NULL;
+ if (!fgets(name, len, f))
+ return -1;
+
+ /* comm ends in \n, get rid of it */
+ name[strcspn(name, "\n")] = '\0';
fclose(f);
- return comm;
+ return 0;
}
goto out;
if (nla_line[RDMA_NLDEV_ATTR_RES_PID]) {
+ SPRINT_BUF(b);
+
pid = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_RES_PID]);
- comm = get_task_name(pid);
+ if (!get_task_name(pid, b, sizeof(b)))
+ comm = b;
}
if (rd_is_filtered_attr(rd, "pid", pid,
print_driver_table(rd, nla_line[RDMA_NLDEV_ATTR_DRIVER]);
newline(rd);
-out: if (nla_line[RDMA_NLDEV_ATTR_RES_PID])
- free(comm);
+out:
return MNL_CB_OK;
}
goto out;
if (nla_line[RDMA_NLDEV_ATTR_RES_PID]) {
+ SPRINT_BUF(b);
+
pid = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_RES_PID]);
- comm = get_task_name(pid);
+ if (!get_task_name(pid, b, sizeof(b)))
+ comm = b;
}
if (rd_is_filtered_attr(rd, "pid", pid,
print_driver_table(rd, nla_line[RDMA_NLDEV_ATTR_DRIVER]);
newline(rd);
-out: if (nla_line[RDMA_NLDEV_ATTR_RES_PID])
- free(comm);
+out:
return MNL_CB_OK;
}
return MNL_CB_ERROR;
if (nla_line[RDMA_NLDEV_ATTR_RES_PID]) {
+ SPRINT_BUF(b);
+
pid = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_RES_PID]);
- comm = get_task_name(pid);
+ if (!get_task_name(pid, b, sizeof(b)))
+ comm = b;
}
if (rd_is_filtered_attr(rd, "pid", pid,
newline(rd);
out:
- if (nla_line[RDMA_NLDEV_ATTR_RES_PID])
- free(comm);
return MNL_CB_OK;
}
goto out;
if (nla_line[RDMA_NLDEV_ATTR_RES_PID]) {
+ SPRINT_BUF(b);
+
pid = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_RES_PID]);
- comm = get_task_name(pid);
+ if (!get_task_name(pid, b, sizeof(b)))
+ comm = b;
}
if (rd_is_filtered_attr(rd, "pid", pid,
newline(rd);
out:
- if (nla_line[RDMA_NLDEV_ATTR_RES_PID])
- free(comm);
return MNL_CB_OK;
}
nla_line[RDMA_NLDEV_ATTR_RES_UNSAFE_GLOBAL_RKEY]);
if (nla_line[RDMA_NLDEV_ATTR_RES_PID]) {
+ SPRINT_BUF(b);
+
pid = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_RES_PID]);
- comm = get_task_name(pid);
+ if (!get_task_name(pid, b, sizeof(b)))
+ comm = b;
}
if (rd_is_filtered_attr(rd, "pid", pid,
print_driver_table(rd, nla_line[RDMA_NLDEV_ATTR_DRIVER]);
newline(rd);
-out: if (nla_line[RDMA_NLDEV_ATTR_RES_PID])
- free(comm);
+out:
return MNL_CB_OK;
}
goto out;
if (nla_line[RDMA_NLDEV_ATTR_RES_PID]) {
+ SPRINT_BUF(b);
+
pid = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_RES_PID]);
- comm = get_task_name(pid);
+ if (!get_task_name(pid, b, sizeof(b)))
+ comm = b;
}
if (rd_is_filtered_attr(rd, "pid", pid,
print_driver_table(rd, nla_line[RDMA_NLDEV_ATTR_DRIVER]);
newline(rd);
out:
- if (nla_line[RDMA_NLDEV_ATTR_RES_PID])
- free(comm);
return MNL_CB_OK;
}
return MNL_CB_ERROR;
if (nla_line[RDMA_NLDEV_ATTR_RES_PID]) {
+ SPRINT_BUF(b);
+
pid = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_RES_PID]);
- comm = get_task_name(pid);
+ if (!get_task_name(pid, b, sizeof(b)))
+ comm = b;
}
if (rd_is_filtered_attr(rd, "pid", pid,
nla_line[RDMA_NLDEV_ATTR_RES_PID]))
newline(rd);
out:
- if (nla_line[RDMA_NLDEV_ATTR_RES_PID])
- free(comm);
return MNL_CB_OK;
}
return MNL_CB_OK;
if (nla_line[RDMA_NLDEV_ATTR_RES_PID]) {
+ SPRINT_BUF(b);
+
pid = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_RES_PID]);
- comm = get_task_name(pid);
+ if (!get_task_name(pid, b, sizeof(b)))
+ comm = b;
}
if (rd_is_filtered_attr(rd, "pid", pid,
nla_line[RDMA_NLDEV_ATTR_RES_PID]))