mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2025-01-09 06:25:19 +00:00
virNetDevMacVLanTapOpen: Slightly rework
There are few outdated things. Firstly, we don't need to undergo the torture of fopen, fscanf and fclose just to get the interface index when we have nice wrapper over that: virNetDevGetIndex. Secondly, we don't need to have statically allocated buffer for the path we are opening. Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
This commit is contained in:
parent
56e2171c6f
commit
025a87065f
@ -237,40 +237,15 @@ int virNetDevMacVLanTapOpen(const char *ifname,
|
|||||||
int retries)
|
int retries)
|
||||||
{
|
{
|
||||||
int ret = -1;
|
int ret = -1;
|
||||||
FILE *file = NULL;
|
|
||||||
char *path;
|
|
||||||
int ifindex;
|
int ifindex;
|
||||||
char tapname[50];
|
char *tapname = NULL;
|
||||||
int tapfd;
|
int tapfd;
|
||||||
|
|
||||||
if (virNetDevSysfsFile(&path, ifname, "ifindex") < 0)
|
if (virNetDevGetIndex(ifname, &ifindex) < 0)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
file = fopen(path, "r");
|
if (virAsprintf(&tapname, "/dev/tap%d", ifindex) < 0)
|
||||||
|
|
||||||
if (!file) {
|
|
||||||
virReportSystemError(errno,
|
|
||||||
_("cannot open macvtap file %s to determine "
|
|
||||||
"interface index"), path);
|
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
}
|
|
||||||
|
|
||||||
if (fscanf(file, "%d", &ifindex) != 1) {
|
|
||||||
virReportSystemError(errno,
|
|
||||||
"%s", _("cannot determine macvtap's tap device "
|
|
||||||
"interface index"));
|
|
||||||
goto cleanup;
|
|
||||||
}
|
|
||||||
|
|
||||||
VIR_FORCE_FCLOSE(file);
|
|
||||||
|
|
||||||
if (snprintf(tapname, sizeof(tapname),
|
|
||||||
"/dev/tap%d", ifindex) >= sizeof(tapname)) {
|
|
||||||
virReportSystemError(errno,
|
|
||||||
"%s",
|
|
||||||
_("internal buffer for tap device is too small"));
|
|
||||||
goto cleanup;
|
|
||||||
}
|
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
/* may need to wait for udev to be done */
|
/* may need to wait for udev to be done */
|
||||||
@ -291,8 +266,7 @@ int virNetDevMacVLanTapOpen(const char *ifname,
|
|||||||
}
|
}
|
||||||
ret = tapfd;
|
ret = tapfd;
|
||||||
cleanup:
|
cleanup:
|
||||||
VIR_FREE(path);
|
VIR_FREE(tapname);
|
||||||
VIR_FORCE_FCLOSE(file);
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user