ip vrf: Refactor ipvrf_identify
Split ipvrf_identify into arg processing and a function that does the actual cgroup file parsing. The latter function is used in a follow on patch. In the process, convert the reading of the cgroups file to use fopen and fgets just in case the file ever grows beyond 4k. Move printing of any error message and the vrf name to the caller of the new vrf_identify. Signed-off-by: David Ahern <dsa@cumulusnetworks.com>
This commit is contained in:
parent
c94112faf5
commit
b5efa59763
69
ip/ipvrf.c
69
ip/ipvrf.c
|
|
@ -40,14 +40,43 @@ static void usage(void)
|
||||||
exit(-1);
|
exit(-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int ipvrf_identify(int argc, char **argv)
|
static int vrf_identify(pid_t pid, char *name, size_t len)
|
||||||
{
|
{
|
||||||
char path[PATH_MAX];
|
char path[PATH_MAX];
|
||||||
char buf[4096];
|
char buf[4096];
|
||||||
char *vrf, *end;
|
char *vrf, *end;
|
||||||
int fd, rc = -1;
|
FILE *fp;
|
||||||
|
|
||||||
|
snprintf(path, sizeof(path), "/proc/%d/cgroup", pid);
|
||||||
|
fp = fopen(path, "r");
|
||||||
|
if (!fp)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
memset(name, 0, len);
|
||||||
|
|
||||||
|
while (fgets(buf, sizeof(buf), fp)) {
|
||||||
|
vrf = strstr(buf, "::/vrf/");
|
||||||
|
if (vrf) {
|
||||||
|
vrf += 7; /* skip past "::/vrf/" */
|
||||||
|
end = strchr(vrf, '\n');
|
||||||
|
if (end)
|
||||||
|
*end = '\0';
|
||||||
|
|
||||||
|
strncpy(name, vrf, len - 1);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fclose(fp);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int ipvrf_identify(int argc, char **argv)
|
||||||
|
{
|
||||||
|
char vrf[32];
|
||||||
|
int rc;
|
||||||
unsigned int pid;
|
unsigned int pid;
|
||||||
ssize_t n;
|
|
||||||
|
|
||||||
if (argc < 1)
|
if (argc < 1)
|
||||||
pid = getpid();
|
pid = getpid();
|
||||||
|
|
@ -56,35 +85,15 @@ static int ipvrf_identify(int argc, char **argv)
|
||||||
else if (get_unsigned(&pid, argv[0], 10))
|
else if (get_unsigned(&pid, argv[0], 10))
|
||||||
invarg("Invalid pid\n", argv[0]);
|
invarg("Invalid pid\n", argv[0]);
|
||||||
|
|
||||||
snprintf(path, sizeof(path), "/proc/%d/cgroup", pid);
|
rc = vrf_identify(pid, vrf, sizeof(vrf));
|
||||||
fd = open(path, O_RDONLY);
|
if (!rc) {
|
||||||
if (fd < 0) {
|
if (vrf[0] != '\0')
|
||||||
fprintf(stderr,
|
printf("%s\n", vrf);
|
||||||
"Failed to open cgroups file: %s\n", strerror(errno));
|
} else {
|
||||||
return -1;
|
fprintf(stderr, "Failed to lookup vrf association: %s\n",
|
||||||
|
strerror(errno));
|
||||||
}
|
}
|
||||||
|
|
||||||
n = read(fd, buf, sizeof(buf) - 1);
|
|
||||||
if (n < 0) {
|
|
||||||
fprintf(stderr,
|
|
||||||
"Failed to read cgroups file: %s\n", strerror(errno));
|
|
||||||
goto out;
|
|
||||||
}
|
|
||||||
buf[n] = '\0';
|
|
||||||
vrf = strstr(buf, "::/vrf/");
|
|
||||||
if (vrf) {
|
|
||||||
vrf += 7; /* skip past "::/vrf/" */
|
|
||||||
end = strchr(vrf, '\n');
|
|
||||||
if (end)
|
|
||||||
*end = '\0';
|
|
||||||
|
|
||||||
printf("%s\n", vrf);
|
|
||||||
}
|
|
||||||
|
|
||||||
rc = 0;
|
|
||||||
out:
|
|
||||||
close(fd);
|
|
||||||
|
|
||||||
return rc;
|
return rc;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue