Skip to contentSkip to conversation

ip vrf: Add command name next to pid

PATCH Superseded David Ahern 2 participants 3 messages Apr 13, 2017 – Apr 14, 2017

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ip vrf: Add command name next to pid

'ip vrf pids' is used to list processes bound to a vrf, but it only shows the pid leaving a lot of work for the user. Add the command name to the output. With this patch you get the more user friendly:

    $ ip vrf pids mgmt
     1121  ntpd
     1418  gdm-session-wor
     1488  gnome-session
     1491  dbus-launch
     1492  dbus-daemon
     1565  sshd
     ...
---
 include/utils.h |  1 +
 ip/ipvrf.c      | 24 ++++++++++++++----------
 lib/fs.c        | 42 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 57 insertions(+), 10 deletions(-)
include/utils.h +1 -0
index 22369e0b4e03..d66784674633 100644
@@ -260,5 +260,6 @@ int get_real_family(int rtm_type, int rtm_family);
 int cmd_exec(const char *cmd, char **argv, bool do_fork);
 int make_path(const char *path, mode_t mode);
 char *find_cgroup2_mount(void);
+int get_comm(char *pid, char *comm, int len);
 
 #endif /* __UTILS_H__ */
ip/ipvrf.c +14 -10
index 5e204a9ebbb1..6d4bd6206cc8 100644
@@ -111,27 +111,31 @@ static void read_cgroup_pids(const char *base_path, char *name)
 {
 	char path[PATH_MAX];
 	char buf[4096];
-	ssize_t n;
-	int fd;
+	FILE *fp;
 
 	if (snprintf(path, sizeof(path), "%s/vrf/%s%s",
 		     base_path, name, CGRP_PROC_FILE) >= sizeof(path))
 		return;
 
-	fd = open(path, O_RDONLY);
-	if (fd < 0)
+	fp = fopen(path, "r");
+	if (!fp)
 		return; /* no cgroup file, nothing to show */
 
 	/* dump contents (pids) of cgroup.procs */
-	while (1) {
-		n = read(fd, buf, sizeof(buf) - 1);
-		if (n <= 0)
-			break;
+	while (fgets(buf, sizeof(buf), fp)) {
+		char *nl, comm[32];
 
-		printf("%s", buf);
+		nl = strchr(buf, '\n');
+		if (nl)
+			*nl = '\0';
+
+		if (get_comm(buf, comm, sizeof(comm)))
+			strcpy(comm, "<terminated?>");
+
+		printf("%5s  %s\n", buf, comm);
 	}
 
-	close(fd);
+	fclose(fp);
 }
 
 /* recurse path looking for PATH[/NETNS]/vrf/NAME */
lib/fs.c +42 -0

51 lines not on this page · raw message

Signature · 1 line
2.1.4
#1 lore raw

Overall, this looks like a good idea.

lib/fs.c int make_path(const char *path, mode_t mode) · line 154

Quoted 2 lines
lib/fs.c +2 -0
+
+int get_comm(char *pid, char *comm, int len)

Please use more glibc style function signature, like:

int get_comm(const char *pid, char *comm, size_t len)

And maybe even
int get_command_name(pid_t pid, char *comm, size_t len)

#2 lore raw
Quoted 8 lines · Stephen Hemminger

Overall, this looks like a good idea.

lib/fs.c int make_path(const char *path, mode_t mode) · line 154

Quoted 2 lines
lib/fs.c +2 -0
+
+int get_comm(char *pid, char *comm, int len)

Please use more glibc style function signature, like:

Quoted 5 lines

int get_comm(const char *pid, char *comm, size_t len)

And maybe even
int get_command_name(pid_t pid, char *comm, size_t len)

I'd pref to keep as char *. No sense converting from string to pid_t to string. The vrf code is reading a cgroups file; the other potential user is netns which is also reading pid from a file.