Make sure all sockets are set NONBLOCK so that writes do not block.

This commit is contained in:
Nicholas J. Kain 2014-08-19 11:09:59 -04:00
parent 12114c9bae
commit 94c107d465
3 changed files with 9 additions and 18 deletions

View File

@ -467,7 +467,7 @@ static ssize_t rtnl_if_mtu_set(int fd, unsigned int mtu)
int perform_ifup(void) int perform_ifup(void)
{ {
int fd = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE); int fd = socket(AF_NETLINK, SOCK_DGRAM | SOCK_NONBLOCK, NETLINK_ROUTE);
if (fd < 0) { if (fd < 0) {
log_line("%s: (%s) netlink socket open failed: %s", log_line("%s: (%s) netlink socket open failed: %s",
client_config.interface, __func__, strerror(errno)); client_config.interface, __func__, strerror(errno));
@ -525,7 +525,7 @@ void perform_ip_subnet_bcast(const char *str_ipaddr,
bcast.s_addr = ipaddr.s_addr | htonl(0xfffffffflu >> prefixlen); bcast.s_addr = ipaddr.s_addr | htonl(0xfffffffflu >> prefixlen);
} }
fd = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE); fd = socket(AF_NETLINK, SOCK_DGRAM | SOCK_NONBLOCK, NETLINK_ROUTE);
if (fd < 0) { if (fd < 0) {
log_error("%s: (%s) netlink socket open failed: %s", log_error("%s: (%s) netlink socket open failed: %s",
client_config.interface, __func__, strerror(errno)); client_config.interface, __func__, strerror(errno));
@ -584,7 +584,7 @@ void perform_router(const char *str_router, size_t len)
return; return;
} }
int fd = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE); int fd = socket(AF_NETLINK, SOCK_DGRAM | SOCK_NONBLOCK, NETLINK_ROUTE);
if (fd < 0) { if (fd < 0) {
log_error("%s: (%s) netlink socket open failed: %s", log_error("%s: (%s) netlink socket open failed: %s",
client_config.interface, __func__, strerror(errno)); client_config.interface, __func__, strerror(errno));
@ -633,7 +633,7 @@ void perform_mtu(const char *str, size_t len)
} }
unsigned int mtu = (unsigned int)tmtu; unsigned int mtu = (unsigned int)tmtu;
int fd = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE); int fd = socket(AF_NETLINK, SOCK_DGRAM | SOCK_NONBLOCK, NETLINK_ROUTE);
if (fd < 0) { if (fd < 0) {
log_error("%s: (%s) netlink socket open failed: %s", log_error("%s: (%s) netlink socket open failed: %s",
client_config.interface, __func__, strerror(errno)); client_config.interface, __func__, strerror(errno));

View File

@ -256,21 +256,11 @@ int nl_sendgetaddr6(int fd, int seq, int ifindex)
int nl_open(int nltype, int nlgroup, int *nlportid) int nl_open(int nltype, int nlgroup, int *nlportid)
{ {
int fd; int fd;
fd = socket(AF_NETLINK, SOCK_RAW, nltype); fd = socket(AF_NETLINK, SOCK_RAW | SOCK_NONBLOCK | SOCK_CLOEXEC, nltype);
if (fd < 0) { if (fd < 0) {
log_error("%s: socket failed: %s", __func__, strerror(errno)); log_error("%s: socket failed: %s", __func__, strerror(errno));
return -1; return -1;
} }
if (fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK) < 0) {
log_error("%s: Set non-blocking failed: %s",
__func__, strerror(errno));
goto err_close;
}
if (fcntl(fd, F_SETFD, FD_CLOEXEC)) {
log_error("%s: Set close-on-exec failed: %s",
__func__, strerror(errno));
goto err_close;
}
socklen_t al; socklen_t al;
struct sockaddr_nl nlsock = { struct sockaddr_nl nlsock = {
.nl_family = AF_NETLINK, .nl_family = AF_NETLINK,

View File

@ -115,7 +115,7 @@ int request_sockd_fd(char *buf, size_t buflen, char *response)
static int create_arp_socket(void) static int create_arp_socket(void)
{ {
int fd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ARP)); int fd = socket(AF_PACKET, SOCK_RAW | SOCK_NONBLOCK, htons(ETH_P_ARP));
if (fd < 0) { if (fd < 0) {
log_error("%s: (%s) socket failed: %s", client_config.interface, log_error("%s: (%s) socket failed: %s", client_config.interface,
__func__, strerror(errno)); __func__, strerror(errno));
@ -154,7 +154,7 @@ static int create_arp_socket(void)
static int create_udp_socket(uint32_t ip, uint16_t port, char *iface) static int create_udp_socket(uint32_t ip, uint16_t port, char *iface)
{ {
int fd; int fd;
if ((fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) { if ((fd = socket(AF_INET, SOCK_DGRAM | SOCK_NONBLOCK, IPPROTO_UDP)) < 0) {
log_error("%s: (%s) socket failed: %s", log_error("%s: (%s) socket failed: %s",
client_config.interface, __func__, strerror(errno)); client_config.interface, __func__, strerror(errno));
goto out; goto out;
@ -211,7 +211,8 @@ static int create_raw_socket(struct sockaddr_ll *sa, bool *using_bpf,
const struct sock_fprog *filter_prog) const struct sock_fprog *filter_prog)
{ {
int fd; int fd;
if ((fd = socket(AF_PACKET, SOCK_DGRAM, htons(ETH_P_IP))) < 0) { if ((fd = socket(AF_PACKET, SOCK_DGRAM | SOCK_NONBLOCK,
htons(ETH_P_IP))) < 0) {
log_error("create_raw_socket: socket failed: %s", strerror(errno)); log_error("create_raw_socket: socket failed: %s", strerror(errno));
goto out; goto out;
} }