From: Roy Marples Date: Thu, 15 May 2008 14:16:47 +0000 (+0000) Subject: Use non blocking sockets so read errors with EAGAIN. X-Git-Tag: v4.0.2~394 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3528961fc661b258de615673b26ebe47198f8b31;p=thirdparty%2Fdhcpcd.git Use non blocking sockets so read errors with EAGAIN. --- diff --git a/client.c b/client.c index af58bd96..7208675c 100644 --- a/client.c +++ b/client.c @@ -1191,7 +1191,6 @@ handle_packet(struct if_state *state, const struct options *options) for(;;) { memset(dhcp, 0, sizeof(*dhcp)); bytes = get_packet(iface, dhcp, sizeof(*dhcp)); - printf ("bb %d\n", bytes); if (bytes == -1 || bytes == 0) break; if (dhcp->cookie != htonl(MAGIC_COOKIE)) { @@ -1216,6 +1215,8 @@ handle_packet(struct if_state *state, const struct options *options) } if (handle_dhcp(state, &dhcp, options) == 0) return 0; + if (state->options & DHCPCD_FORKED) + return -1; } free(dhcp); diff --git a/socket.c b/socket.c index a41d46ab..0e0b2cf7 100644 --- a/socket.c +++ b/socket.c @@ -96,6 +96,7 @@ open_socket(struct interface *iface, int protocol) struct sockaddr_storage ss; } su; struct sock_fprog pf; + int flags; if ((s = socket(PF_PACKET, SOCK_DGRAM, htons(protocol))) == -1) return -1; @@ -119,7 +120,9 @@ open_socket(struct interface *iface, int protocol) } if (setsockopt(s, SOL_SOCKET, SO_ATTACH_FILTER, &pf, sizeof(pf)) != 0) goto eexit; - + if ((flags = fcntl(s, F_GETFL, 0)) == -1 + || fcntl(s, F_SETFL, flags | O_NONBLOCK) == -1) + goto eexit; if (bind(s, &su.sa, sizeof(su)) == -1) goto eexit; if (close_on_exec(s) == -1) @@ -174,20 +177,10 @@ get_packet(struct interface *iface, void *data, ssize_t len) ssize_t bytes; const uint8_t *p; - if (iface->buffer_pos > iface->buffer_len) { - iface->buffer_len = iface->buffer_pos = 0; - return 0; - } - bytes = read(iface->fd, iface->buffer, iface->buffer_size); - if (bytes == -1) return errno == EAGAIN ? 0 : -1; - /* So our loops to us work correctly */ - iface->buffer_len = bytes; - iface->buffer_pos = iface->buffer_len + 1; - /* If it's an ARP reply, then just send it back */ if (iface->socket_protocol == ETHERTYPE_ARP) return bytes;