tap-solaris: Convert tap_open() to Error
Fixes inappropriate use of syslog(). Not fixed: leaks on error paths, suspicious non-fatal errors. FIXMEs added instead. Signed-off-by: Markus Armbruster <armbru@redhat.com> Reviewed-by: Eric Blake <eblake@redhat.com> Message-id: 1431691143-1015-14-git-send-email-armbru@redhat.com Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
This commit is contained in:
		
							parent
							
								
									4bce487e14
								
							
						
					
					
						commit
						576c6eb670
					
				@ -36,7 +36,6 @@
 | 
				
			|||||||
#include <netinet/udp.h>
 | 
					#include <netinet/udp.h>
 | 
				
			||||||
#include <netinet/tcp.h>
 | 
					#include <netinet/tcp.h>
 | 
				
			||||||
#include <net/if.h>
 | 
					#include <net/if.h>
 | 
				
			||||||
#include <syslog.h>
 | 
					 | 
				
			||||||
#include <stropts.h>
 | 
					#include <stropts.h>
 | 
				
			||||||
#include "qemu/error-report.h"
 | 
					#include "qemu/error-report.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -56,8 +55,10 @@ ssize_t tap_read_packet(int tapfd, uint8_t *buf, int maxlen)
 | 
				
			|||||||
 * Allocate TAP device, returns opened fd.
 | 
					 * Allocate TAP device, returns opened fd.
 | 
				
			||||||
 * Stores dev name in the first arg(must be large enough).
 | 
					 * Stores dev name in the first arg(must be large enough).
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
static int tap_alloc(char *dev, size_t dev_size)
 | 
					static int tap_alloc(char *dev, size_t dev_size, Error **errp)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
 | 
					    /* FIXME leaks like a sieve on error paths */
 | 
				
			||||||
 | 
					    /* FIXME suspicious: many errors are reported, then ignored */
 | 
				
			||||||
    int tap_fd, if_fd, ppa = -1;
 | 
					    int tap_fd, if_fd, ppa = -1;
 | 
				
			||||||
    static int ip_fd = 0;
 | 
					    static int ip_fd = 0;
 | 
				
			||||||
    char *ptr;
 | 
					    char *ptr;
 | 
				
			||||||
@ -83,13 +84,13 @@ static int tap_alloc(char *dev, size_t dev_size)
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    TFR(ip_fd = open("/dev/udp", O_RDWR, 0));
 | 
					    TFR(ip_fd = open("/dev/udp", O_RDWR, 0));
 | 
				
			||||||
    if (ip_fd < 0) {
 | 
					    if (ip_fd < 0) {
 | 
				
			||||||
       syslog(LOG_ERR, "Can't open /dev/ip (actually /dev/udp)");
 | 
					        error_setg(errp, "Can't open /dev/ip (actually /dev/udp)");
 | 
				
			||||||
        return -1;
 | 
					        return -1;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    TFR(tap_fd = open("/dev/tap", O_RDWR, 0));
 | 
					    TFR(tap_fd = open("/dev/tap", O_RDWR, 0));
 | 
				
			||||||
    if (tap_fd < 0) {
 | 
					    if (tap_fd < 0) {
 | 
				
			||||||
       syslog(LOG_ERR, "Can't open /dev/tap");
 | 
					        error_setg(errp, "Can't open /dev/tap");
 | 
				
			||||||
        return -1;
 | 
					        return -1;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -99,20 +100,20 @@ static int tap_alloc(char *dev, size_t dev_size)
 | 
				
			|||||||
    strioc_ppa.ic_len = sizeof(ppa);
 | 
					    strioc_ppa.ic_len = sizeof(ppa);
 | 
				
			||||||
    strioc_ppa.ic_dp = (char *)&ppa;
 | 
					    strioc_ppa.ic_dp = (char *)&ppa;
 | 
				
			||||||
    if ((ppa = ioctl (tap_fd, I_STR, &strioc_ppa)) < 0)
 | 
					    if ((ppa = ioctl (tap_fd, I_STR, &strioc_ppa)) < 0)
 | 
				
			||||||
       syslog (LOG_ERR, "Can't assign new interface");
 | 
					        error_report("Can't assign new interface");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    TFR(if_fd = open("/dev/tap", O_RDWR, 0));
 | 
					    TFR(if_fd = open("/dev/tap", O_RDWR, 0));
 | 
				
			||||||
    if (if_fd < 0) {
 | 
					    if (if_fd < 0) {
 | 
				
			||||||
       syslog(LOG_ERR, "Can't open /dev/tap (2)");
 | 
					        error_setg(errp, "Can't open /dev/tap (2)");
 | 
				
			||||||
        return -1;
 | 
					        return -1;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    if(ioctl(if_fd, I_PUSH, "ip") < 0){
 | 
					    if(ioctl(if_fd, I_PUSH, "ip") < 0){
 | 
				
			||||||
       syslog(LOG_ERR, "Can't push IP module");
 | 
					        error_setg(errp, "Can't push IP module");
 | 
				
			||||||
        return -1;
 | 
					        return -1;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) < 0)
 | 
					    if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) < 0)
 | 
				
			||||||
	syslog(LOG_ERR, "Can't get flags\n");
 | 
					        error_report("Can't get flags");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    snprintf (actual_name, 32, "tap%d", ppa);
 | 
					    snprintf (actual_name, 32, "tap%d", ppa);
 | 
				
			||||||
    pstrcpy(ifr.lifr_name, sizeof(ifr.lifr_name), actual_name);
 | 
					    pstrcpy(ifr.lifr_name, sizeof(ifr.lifr_name), actual_name);
 | 
				
			||||||
@ -121,22 +122,22 @@ static int tap_alloc(char *dev, size_t dev_size)
 | 
				
			|||||||
    /* Assign ppa according to the unit number returned by tun device */
 | 
					    /* Assign ppa according to the unit number returned by tun device */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (ioctl (if_fd, SIOCSLIFNAME, &ifr) < 0)
 | 
					    if (ioctl (if_fd, SIOCSLIFNAME, &ifr) < 0)
 | 
				
			||||||
        syslog (LOG_ERR, "Can't set PPA %d", ppa);
 | 
					        error_report("Can't set PPA %d", ppa);
 | 
				
			||||||
    if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) <0)
 | 
					    if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) <0)
 | 
				
			||||||
        syslog (LOG_ERR, "Can't get flags\n");
 | 
					        error_report("Can't get flags");
 | 
				
			||||||
    /* Push arp module to if_fd */
 | 
					    /* Push arp module to if_fd */
 | 
				
			||||||
    if (ioctl (if_fd, I_PUSH, "arp") < 0)
 | 
					    if (ioctl (if_fd, I_PUSH, "arp") < 0)
 | 
				
			||||||
        syslog (LOG_ERR, "Can't push ARP module (2)");
 | 
					        error_report("Can't push ARP module (2)");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /* Push arp module to ip_fd */
 | 
					    /* Push arp module to ip_fd */
 | 
				
			||||||
    if (ioctl (ip_fd, I_POP, NULL) < 0)
 | 
					    if (ioctl (ip_fd, I_POP, NULL) < 0)
 | 
				
			||||||
        syslog (LOG_ERR, "I_POP failed\n");
 | 
					        error_report("I_POP failed");
 | 
				
			||||||
    if (ioctl (ip_fd, I_PUSH, "arp") < 0)
 | 
					    if (ioctl (ip_fd, I_PUSH, "arp") < 0)
 | 
				
			||||||
        syslog (LOG_ERR, "Can't push ARP module (3)\n");
 | 
					        error_report("Can't push ARP module (3)");
 | 
				
			||||||
    /* Open arp_fd */
 | 
					    /* Open arp_fd */
 | 
				
			||||||
    TFR(arp_fd = open ("/dev/tap", O_RDWR, 0));
 | 
					    TFR(arp_fd = open ("/dev/tap", O_RDWR, 0));
 | 
				
			||||||
    if (arp_fd < 0)
 | 
					    if (arp_fd < 0)
 | 
				
			||||||
       syslog (LOG_ERR, "Can't open %s\n", "/dev/tap");
 | 
					        error_report("Can't open %s", "/dev/tap");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /* Set ifname to arp */
 | 
					    /* Set ifname to arp */
 | 
				
			||||||
    strioc_if.ic_cmd = SIOCSLIFNAME;
 | 
					    strioc_if.ic_cmd = SIOCSLIFNAME;
 | 
				
			||||||
@ -144,16 +145,16 @@ static int tap_alloc(char *dev, size_t dev_size)
 | 
				
			|||||||
    strioc_if.ic_len = sizeof(ifr);
 | 
					    strioc_if.ic_len = sizeof(ifr);
 | 
				
			||||||
    strioc_if.ic_dp = (char *)𝔦
 | 
					    strioc_if.ic_dp = (char *)𝔦
 | 
				
			||||||
    if (ioctl(arp_fd, I_STR, &strioc_if) < 0){
 | 
					    if (ioctl(arp_fd, I_STR, &strioc_if) < 0){
 | 
				
			||||||
        syslog (LOG_ERR, "Can't set ifname to arp\n");
 | 
					        error_report("Can't set ifname to arp");
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if((ip_muxid = ioctl(ip_fd, I_LINK, if_fd)) < 0){
 | 
					    if((ip_muxid = ioctl(ip_fd, I_LINK, if_fd)) < 0){
 | 
				
			||||||
       syslog(LOG_ERR, "Can't link TAP device to IP");
 | 
					        error_setg(errp, "Can't link TAP device to IP");
 | 
				
			||||||
        return -1;
 | 
					        return -1;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if ((arp_muxid = ioctl (ip_fd, link_type, arp_fd)) < 0)
 | 
					    if ((arp_muxid = ioctl (ip_fd, link_type, arp_fd)) < 0)
 | 
				
			||||||
        syslog (LOG_ERR, "Can't link TAP device to ARP");
 | 
					        error_report("Can't link TAP device to ARP");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    close (if_fd);
 | 
					    close (if_fd);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -166,7 +167,7 @@ static int tap_alloc(char *dev, size_t dev_size)
 | 
				
			|||||||
    {
 | 
					    {
 | 
				
			||||||
      ioctl (ip_fd, I_PUNLINK , arp_muxid);
 | 
					      ioctl (ip_fd, I_PUNLINK , arp_muxid);
 | 
				
			||||||
      ioctl (ip_fd, I_PUNLINK, ip_muxid);
 | 
					      ioctl (ip_fd, I_PUNLINK, ip_muxid);
 | 
				
			||||||
      syslog (LOG_ERR, "Can't set multiplexor id");
 | 
					      error_report("Can't set multiplexor id");
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    snprintf(dev, dev_size, "tap%d", ppa);
 | 
					    snprintf(dev, dev_size, "tap%d", ppa);
 | 
				
			||||||
@ -176,11 +177,11 @@ static int tap_alloc(char *dev, size_t dev_size)
 | 
				
			|||||||
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 */
 | 
					 | 
				
			||||||
    char  dev[10]="";
 | 
					    char  dev[10]="";
 | 
				
			||||||
    int fd;
 | 
					    int fd;
 | 
				
			||||||
    if( (fd = tap_alloc(dev, sizeof(dev))) < 0 ){
 | 
					
 | 
				
			||||||
       fprintf(stderr, "Cannot allocate TAP device\n");
 | 
					    fd = tap_alloc(dev, sizeof(dev), errp);
 | 
				
			||||||
 | 
					    if (fd < 0) {
 | 
				
			||||||
        return -1;
 | 
					        return -1;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    pstrcpy(ifname, ifname_size, dev);
 | 
					    pstrcpy(ifname, ifname_size, dev);
 | 
				
			||||||
@ -189,7 +190,7 @@ 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;
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user