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:
David Ahern 2016-12-15 12:07:00 -08:00 committed by Stephen Hemminger
parent c94112faf5
commit b5efa59763
1 changed files with 39 additions and 30 deletions

View File

@ -40,14 +40,43 @@ static void usage(void)
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 buf[4096];
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;
ssize_t n;
if (argc < 1)
pid = getpid();
@ -56,35 +85,15 @@ static int ipvrf_identify(int argc, char **argv)
else if (get_unsigned(&pid, argv[0], 10))
invarg("Invalid pid\n", argv[0]);
snprintf(path, sizeof(path), "/proc/%d/cgroup", pid);
fd = open(path, O_RDONLY);
if (fd < 0) {
fprintf(stderr,
"Failed to open cgroups file: %s\n", strerror(errno));
return -1;
rc = vrf_identify(pid, vrf, sizeof(vrf));
if (!rc) {
if (vrf[0] != '\0')
printf("%s\n", vrf);
} else {
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;
}