Handle errors in fcntl() if O_NONBLOCK fails.

This commit is contained in:
Nicholas J. Kain 2011-06-25 10:46:24 -04:00
parent 71f59d0433
commit e50c429235
2 changed files with 22 additions and 13 deletions

View File

@ -81,7 +81,11 @@ static int arpping(struct client_state_t *cs, uint32_t test_ip,
close(arpfd); close(arpfd);
return -1; return -1;
} }
fcntl(arpfd, F_SETFL, fcntl(arpfd, F_GETFL) | O_NONBLOCK); if (fcntl(arpfd, F_SETFL, fcntl(arpfd, F_GETFL) | O_NONBLOCK) == -1) {
log_error("arp: failed to set non-blocking: %s", strerror(errno));
close(arpfd);
return -1;
}
cs->arpFd = arpfd; cs->arpFd = arpfd;
epoll_add(cs, arpfd); epoll_add(cs, arpfd);
} }

View File

@ -46,14 +46,6 @@
#include "options.h" #include "options.h"
#include "strl.h" #include "strl.h"
static int set_sock_nonblock(int fd)
{
int ret = 0, flags;
flags = fcntl(fd, F_GETFL);
ret = fcntl(fd, F_SETFL, flags | O_NONBLOCK);
return ret;
}
/* Returns fd of new listen socket bound to @ip:@port on interface @inf /* Returns fd of new listen socket bound to @ip:@port on interface @inf
* on success, or -1 on failure. */ * on success, or -1 on failure. */
static int create_udp_listen_socket(unsigned int ip, int port, char *inf) static int create_udp_listen_socket(unsigned int ip, int port, char *inf)
@ -65,7 +57,8 @@ static int create_udp_listen_socket(unsigned int ip, int port, char *inf)
log_line("Opening listen socket on 0x%08x:%d %s", ip, port, inf); log_line("Opening listen socket on 0x%08x:%d %s", ip, port, inf);
if ((fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) { if ((fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
log_error("create_udp_listen_socket: socket failed: %s", strerror(errno)); log_error("create_udp_listen_socket: socket failed: %s",
strerror(errno));
goto out; goto out;
} }
@ -80,7 +73,11 @@ static int create_udp_listen_socket(unsigned int ip, int port, char *inf)
&interface, sizeof interface) < 0) &interface, sizeof interface) < 0)
goto out_fd; goto out_fd;
set_sock_nonblock(fd); if (fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK) == -1) {
log_error("create_udp_listen_socket: set non-blocking failed: %s",
strerror(errno));
goto out_fd;
}
memset(&addr, 0, sizeof addr); memset(&addr, 0, sizeof addr);
addr.sin_family = AF_INET; addr.sin_family = AF_INET;
@ -147,7 +144,12 @@ static int create_raw_listen_socket(int ifindex)
sizeof filter_prog) >= 0) sizeof filter_prog) >= 0)
log_line("Attached filter to raw socket fd %d", fd); log_line("Attached filter to raw socket fd %d", fd);
set_sock_nonblock(fd); if (fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK) == -1) {
log_error("create_raw_listen_socket: set non-blocking failed: %s",
strerror(errno));
close(fd);
return -1;
}
sock.sll_family = AF_PACKET; sock.sll_family = AF_PACKET;
sock.sll_protocol = htons(ETH_P_IP); sock.sll_protocol = htons(ETH_P_IP);
@ -295,7 +297,10 @@ int raw_packet(struct dhcpMessage *payload, uint32_t source_ip,
memset(&packet, 0, offsetof(struct ip_udp_dhcp_packet, data)); memset(&packet, 0, offsetof(struct ip_udp_dhcp_packet, data));
packet.data = *payload; /* struct copy */ packet.data = *payload; /* struct copy */
set_sock_nonblock(fd); if (fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK) == -1) {
log_error("raw_packet: set non-blocking failed: %s", strerror(errno));
goto out_fd;
}
dest.sll_family = AF_PACKET; dest.sll_family = AF_PACKET;
dest.sll_protocol = htons(ETH_P_IP); dest.sll_protocol = htons(ETH_P_IP);