--- net-snmp-5.9.4/agent/mibgroup/kernel_sunos5.c.orig +++ net-snmp-5.9.4/agent/mibgroup/kernel_sunos5.c @@ -1087,18 +1087,21 @@ static int _dlpi_open(const char *devname) { - char *devstr; + char devstr[255], devstrcpy[255]; int fd = -1; int ppa = -1; + int ret = -1; DEBUGMSGTL(("kernel_sunos5", "_dlpi_open called\n")); if (devname == NULL) - return (-1); + return (-1); + + ret = snprintf(devstr, sizeof(devstr), "/dev/%s", devname); - if ((devstr = malloc(5 + strlen(devname) + 1)) == NULL) - return (-1); - (void) sprintf(devstr, "/dev/%s", devname); + if(ret < 0 || ret > sizeof(devstr) - 1) + return -1; + DEBUGMSGTL(("kernel_sunos5:dlpi", "devstr(%s)\n", devstr)); /* * First try opening the device using style 1, if the device does not @@ -1109,13 +1112,49 @@ DEBUGMSGTL(("kernel_sunos5:dlpi", "style1 open(%s)\n", devstr)); if ((fd = open(devstr, O_RDWR | O_NONBLOCK)) < 0) { DEBUGMSGTL(("kernel_sunos5:dlpi", "style1 open failed\n")); - if (_dlpi_parse_devname(devstr, &ppa) == 0) { - DEBUGMSGTL(("kernel_sunos5:dlpi", "style2 parse: %s, %d\n", - devstr, ppa)); - /* try style 2 */ - DEBUGMSGTL(("kernel_sunos5:dlpi", "style2 open(%s)\n", devstr)); + fd = -1; + } else { + DEBUGMSGTL(("kernel_sunos5:dlpi", "style1 open succeeded\n")); + } + + /* Creating a copy of the devname incase style 3 open + * is required + */ + ret = strlcpy(devstrcpy, devstr, sizeof(devstrcpy)); + + if(ret > sizeof(devstrcpy) - 1) + return -1; + + DEBUGMSGTL(("kernel_sunos5:dlpi", "devstrcpy(%s)\n", devstrcpy)); + + + if (fd == -1 ) { + ret = snprintf(devstr, sizeof(devstr), "/dev/net/%s", devname); + + if(ret < 0 || ret > sizeof(devstr) - 1) + return -1; + + DEBUGMSGTL(("kernel_sunos5:dlpi", "devstr(%s)\n", devstr)); + + /* try style 2 */ + + if ((fd = open(devstr, O_RDWR | O_NONBLOCK)) < 0) { + DEBUGMSGTL(("kernel_sunos5:dlpi", "style2 open failed\n")); + fd = -1; + } else { + DEBUGMSGTL(("kernel_sunos5:dlpi", "style2 open succeeded\n")); + } + } + + + if (fd == -1 ) { + if (_dlpi_parse_devname(devstrcpy, &ppa) == 0) { + DEBUGMSGTL(("kernel_sunos5:dlpi", "style3 parse: %s, %d\n", + devstrcpy, ppa)); + /* try style 3 */ + DEBUGMSGTL(("kernel_sunos5:dlpi", "style3 open(%s)\n", devstrcpy)); - if ((fd = open(devstr, O_RDWR | O_NONBLOCK)) != -1) { + if ((fd = open(devstrcpy, O_RDWR | O_NONBLOCK)) != -1) { if (_dlpi_attach(fd, ppa) == 0) { DEBUGMSGTL(("kernel_sunos5:dlpi", "attached\n")); } else { @@ -1124,16 +1163,11 @@ fd = -1; } } else { - DEBUGMSGTL(("kernel_sunos5:dlpi", "style2 open failed\n")); + DEBUGMSGTL(("kernel_sunos5:dlpi", "style3 open failed\n")); } } - } else { - DEBUGMSGTL(("kernel_sunos5:dlpi", "style1 open succeeded\n")); } - /* clean up */ - free(devstr); - return (fd); } @@ -1148,7 +1182,9 @@ struct strbuf ctlbuf; char buf[MAX(DL_PHYS_ADDR_ACK_SIZE+64, DL_ERROR_ACK_SIZE)]; int flag = 0; - + struct pollfd pfd; + int timeout = 100; + DEBUGMSGTL(("kernel_sunos5:dlpi", "_dlpi_get_phys_address\n")); paddr_req.dl_primitive = DL_PHYS_ADDR_REQ; @@ -1161,7 +1197,22 @@ ctlbuf.maxlen = sizeof(buf); ctlbuf.len = 0; ctlbuf.buf = buf; - if (getmsg(fd, &ctlbuf, NULL, &flag) < 0) + + pfd.fd = fd; + pfd.events = POLLIN | POLLPRI; + + switch (poll(&pfd, 1, timeout)) { + default: + if (pfd.revents & POLLHUP) + return (-1); + break; + case 0: + return (-1); + case -1: + return (-1); + } + + if (getmsg(fd, &ctlbuf, NULL, &flag) < 0) return (-1); if (ctlbuf.len < sizeof(uint32_t)) @@ -1203,6 +1254,8 @@ struct strbuf ctlbuf; char buf[MAX(DL_INFO_ACK_SIZE, DL_ERROR_ACK_SIZE)]; int flag = 0; + struct pollfd pfd; + int timeout = 100; DEBUGMSGTL(("kernel_sunos5:dlpi", "_dlpi_get_iftype\n")); @@ -1217,7 +1270,22 @@ ctlbuf.maxlen = sizeof(buf); ctlbuf.len = 0; ctlbuf.buf = buf; - if (getmsg(fd, &ctlbuf, NULL, &flag) < 0) { + + pfd.fd = fd; + pfd.events = POLLIN | POLLPRI; + + switch (poll(&pfd, 1, timeout)) { + default: + if (pfd.revents & POLLHUP) + return (-1); + break; + case 0: + return (-1); + case -1: + return (-1); + } + + if (getmsg(fd, &ctlbuf, NULL, &flag) < 0) { DEBUGMSGTL(("kernel_sunos5:dlpi", "getmsg failed: %d\n", errno)); return (-1); }