提交 4bce487e 编写于 作者: M Markus Armbruster 提交者: Stefan Hajnoczi

tap-bsd: Convert tap_open() to Error

Fixes inappropriate use of stderr in monitor command handler.

While there, improve some of the messages a bit.
Signed-off-by: NMarkus Armbruster <armbru@redhat.com>
Reviewed-by: NEric Blake <eblake@redhat.com>
Message-id: 1431691143-1015-13-git-send-email-armbru@redhat.com
Signed-off-by: NStefan Hajnoczi <stefanha@redhat.com>
上级 47896e2f
...@@ -37,7 +37,6 @@ ...@@ -37,7 +37,6 @@
int tap_open(char *ifname, int ifname_size, int *vnet_hdr, int tap_open(char *ifname, int ifname_size, int *vnet_hdr,
int vnet_hdr_required, int mq_required, Error **errp) int vnet_hdr_required, int mq_required, Error **errp)
{ {
/* FIXME error_setg(errp, ...) on failure */
int fd; int fd;
#ifdef TAPGIFNAME #ifdef TAPGIFNAME
struct ifreq ifr; struct ifreq ifr;
...@@ -72,23 +71,19 @@ int tap_open(char *ifname, int ifname_size, int *vnet_hdr, ...@@ -72,23 +71,19 @@ int tap_open(char *ifname, int ifname_size, int *vnet_hdr,
} }
} }
if (fd < 0) { if (fd < 0) {
error_report("warning: could not open %s (%s): no virtual network emulation", error_setg_errno(errp, errno, "could not open %s", dname);
dname, strerror(errno));
return -1; return -1;
} }
#ifdef TAPGIFNAME #ifdef TAPGIFNAME
if (ioctl(fd, TAPGIFNAME, (void *)&ifr) < 0) { if (ioctl(fd, TAPGIFNAME, (void *)&ifr) < 0) {
fprintf(stderr, "warning: could not get tap name: %s\n", error_setg_errno(errp, errno, "could not get tap name");
strerror(errno));
return -1; return -1;
} }
pstrcpy(ifname, ifname_size, ifr.ifr_name); pstrcpy(ifname, ifname_size, ifr.ifr_name);
#else #else
if (fstat(fd, &s) < 0) { if (fstat(fd, &s) < 0) {
fprintf(stderr, error_setg_errno(errp, errno, "could not stat %s", dname);
"warning: could not stat /dev/tap: no virtual network emulation: %s\n",
strerror(errno));
return -1; return -1;
} }
dev = devname(s.st_rdev, S_IFCHR); dev = devname(s.st_rdev, S_IFCHR);
...@@ -100,8 +95,8 @@ int tap_open(char *ifname, int ifname_size, int *vnet_hdr, ...@@ -100,8 +95,8 @@ int tap_open(char *ifname, int ifname_size, int *vnet_hdr,
*vnet_hdr = 0; *vnet_hdr = 0;
if (vnet_hdr_required && !*vnet_hdr) { if (vnet_hdr_required && !*vnet_hdr) {
error_report("vnet_hdr=1 requested, but no kernel " error_setg(errp, "vnet_hdr=1 requested, but no kernel "
"support for IFF_VNET_HDR available"); "support for IFF_VNET_HDR available");
close(fd); close(fd);
return -1; return -1;
} }
...@@ -117,13 +112,12 @@ int tap_open(char *ifname, int ifname_size, int *vnet_hdr, ...@@ -117,13 +112,12 @@ int tap_open(char *ifname, int ifname_size, int *vnet_hdr,
int tap_open(char *ifname, int ifname_size, int *vnet_hdr, int tap_open(char *ifname, int ifname_size, int *vnet_hdr,
int vnet_hdr_required, int mq_required, Error **errp) int vnet_hdr_required, int mq_required, Error **errp)
{ {
/* FIXME error_setg(errp, ...) on failure */
int fd, s, ret; int fd, s, ret;
struct ifreq ifr; struct ifreq ifr;
TFR(fd = open(PATH_NET_TAP, O_RDWR)); TFR(fd = open(PATH_NET_TAP, O_RDWR));
if (fd < 0) { if (fd < 0) {
error_report("could not open %s: %s", PATH_NET_TAP, strerror(errno)); error_setg_errno(errp, errno, "could not open %s", PATH_NET_TAP);
return -1; return -1;
} }
...@@ -131,7 +125,7 @@ int tap_open(char *ifname, int ifname_size, int *vnet_hdr, ...@@ -131,7 +125,7 @@ int tap_open(char *ifname, int ifname_size, int *vnet_hdr,
ret = ioctl(fd, TAPGIFNAME, (void *)&ifr); ret = ioctl(fd, TAPGIFNAME, (void *)&ifr);
if (ret < 0) { if (ret < 0) {
error_report("could not get tap interface name"); error_setg_errno(errp, errno, "could not get tap interface name");
goto error; goto error;
} }
...@@ -139,14 +133,15 @@ int tap_open(char *ifname, int ifname_size, int *vnet_hdr, ...@@ -139,14 +133,15 @@ int tap_open(char *ifname, int ifname_size, int *vnet_hdr,
/* User requested the interface to have a specific name */ /* User requested the interface to have a specific name */
s = socket(AF_LOCAL, SOCK_DGRAM, 0); s = socket(AF_LOCAL, SOCK_DGRAM, 0);
if (s < 0) { if (s < 0) {
error_report("could not open socket to set interface name"); error_setg_errno(errp, errno,
"could not open socket to set interface name");
goto error; goto error;
} }
ifr.ifr_data = ifname; ifr.ifr_data = ifname;
ret = ioctl(s, SIOCSIFNAME, (void *)&ifr); ret = ioctl(s, SIOCSIFNAME, (void *)&ifr);
close(s); close(s);
if (ret < 0) { if (ret < 0) {
error_report("could not set tap interface name"); error_setg(errp, "could not set tap interface name");
goto error; goto error;
} }
} else { } else {
...@@ -158,14 +153,14 @@ int tap_open(char *ifname, int ifname_size, int *vnet_hdr, ...@@ -158,14 +153,14 @@ int tap_open(char *ifname, int ifname_size, int *vnet_hdr,
*vnet_hdr = 0; *vnet_hdr = 0;
if (vnet_hdr_required && !*vnet_hdr) { if (vnet_hdr_required && !*vnet_hdr) {
error_report("vnet_hdr=1 requested, but no kernel " error_setg(errp, "vnet_hdr=1 requested, but no kernel "
"support for IFF_VNET_HDR available"); "support for IFF_VNET_HDR available");
goto error; goto error;
} }
} }
if (mq_required) { if (mq_required) {
error_report("mq_required requested, but not kernel support" error_setg(errp, "mq_required requested, but no kernel support"
"for IFF_MULTI_QUEUE available"); " for IFF_MULTI_QUEUE available");
goto error; goto error;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册