diff options
author | Philip Guenther <guenther@cvs.openbsd.org> | 2016-03-21 00:49:37 +0000 |
---|---|---|
committer | Philip Guenther <guenther@cvs.openbsd.org> | 2016-03-21 00:49:37 +0000 |
commit | 480dc4adde06fe54aa2665b9b4089af30a42820d (patch) | |
tree | c2567b64123939d7200344008a7006c9dabfd254 | |
parent | d4c8db29044b848271e1a2a3de2d895b0df21c40 (diff) |
Instead of creating a socket with socket() or accept() and then
setting the O_NONBLOCK flag on it with fcntl(F_SETFL) afterwards,
just pass SOCK_NONBLOCK to socket() or accept4() and get it right
to begin with.
ok millert@ krw@ beck@ deraadt@ jca@
-rw-r--r-- | games/hunt/huntd/answer.c | 19 | ||||
-rw-r--r-- | games/hunt/huntd/driver.c | 10 | ||||
-rw-r--r-- | usr.bin/tcpbench/tcpbench.c | 8 | ||||
-rw-r--r-- | usr.sbin/npppd/l2tp/l2tpd.c | 15 | ||||
-rw-r--r-- | usr.sbin/npppd/pptp/pptpd.c | 18 | ||||
-rw-r--r-- | usr.sbin/radiusd/radiusd.c | 18 | ||||
-rw-r--r-- | usr.sbin/radiusd/radiusd_radius.c | 16 | ||||
-rw-r--r-- | usr.sbin/ypbind/ypbind.c | 8 |
8 files changed, 31 insertions, 81 deletions
diff --git a/games/hunt/huntd/answer.c b/games/hunt/huntd/answer.c index 58d77408e32..16c6b7600f5 100644 --- a/games/hunt/huntd/answer.c +++ b/games/hunt/huntd/answer.c @@ -1,4 +1,4 @@ -/* $OpenBSD: answer.c,v 1.18 2016/01/10 13:35:09 mestre Exp $ */ +/* $OpenBSD: answer.c,v 1.19 2016/03/21 00:49:36 guenther Exp $ */ /* $NetBSD: answer.c,v 1.3 1997/10/10 16:32:50 lukem Exp $ */ /* * Copyright (c) 1983-2003, Regents of the University of California. @@ -66,9 +66,14 @@ answer_first(void) int flags; struct spawn *sp; - /* Answer the call to hunt: */ + /* + * Answer the call to hunt, turning off blocking I/O, so a slow + * or dead terminal won't stop the game. All subsequent reads + * check how many bytes they read. + */ socklen = sizeof sockstruct; - newsock = accept(Socket, (struct sockaddr *) &sockstruct, &socklen); + newsock = accept4(Socket, (struct sockaddr *) &sockstruct, &socklen, + SOCK_NONBLOCK); if (newsock < 0) { logit(LOG_ERR, "accept"); return; @@ -92,14 +97,6 @@ answer_first(void) "struct sockaddr is not big enough! (%d > %zu)", socklen, sizeof Spawn->source); - /* - * Turn off blocking I/O, so a slow or dead terminal won't stop - * the game. All subsequent reads check how many bytes they read. - */ - flags = fcntl(newsock, F_GETFL, 0); - flags |= O_NDELAY; - (void) fcntl(newsock, F_SETFL, flags); - /* Start listening to the spawning connection */ sp->fd = newsock; FD_SET(sp->fd, &Fds_mask); diff --git a/games/hunt/huntd/driver.c b/games/hunt/huntd/driver.c index 159a163594f..e3c867fee1a 100644 --- a/games/hunt/huntd/driver.c +++ b/games/hunt/huntd/driver.c @@ -1,4 +1,4 @@ -/* $OpenBSD: driver.c,v 1.26 2016/01/07 21:37:53 mestre Exp $ */ +/* $OpenBSD: driver.c,v 1.27 2016/03/21 00:49:36 guenther Exp $ */ /* $NetBSD: driver.c,v 1.5 1997/10/20 00:37:16 lukem Exp $ */ /* * Copyright (c) 1983-2003, Regents of the University of California. @@ -995,7 +995,8 @@ send_stats(void) /* Accept a connection to the statistics socket: */ socklen = sizeof sockstruct; - s = accept(Status, (struct sockaddr *) &sockstruct, &socklen); + s = accept4(Status, (struct sockaddr *) &sockstruct, &socklen, + SOCK_NONBLOCK); if (s < 0) { if (errno == EINTR) return; @@ -1003,11 +1004,6 @@ send_stats(void) return; } - /* Don't allow the writes to block: */ - flags = fcntl(s, F_GETFL, 0); - flags |= O_NDELAY; - (void) fcntl(s, F_SETFL, flags); - fp = fdopen(s, "w"); if (fp == NULL) { logit(LOG_ERR, "fdopen"); diff --git a/usr.bin/tcpbench/tcpbench.c b/usr.bin/tcpbench/tcpbench.c index 1e1d5ece203..4d01bf0fec4 100644 --- a/usr.bin/tcpbench/tcpbench.c +++ b/usr.bin/tcpbench/tcpbench.c @@ -680,7 +680,8 @@ tcp_server_accept(int fd, short event, void *arg) event_add(&ts->ev, NULL); if (event & EV_TIMEOUT) return; - if ((sock = accept(fd, (struct sockaddr *)&ss, &sslen)) == -1) { + if ((sock = accept4(fd, (struct sockaddr *)&ss, &sslen, SOCK_NONBLOCK)) + == -1) { /* * Pause accept if we are out of file descriptors, or * libevent will haunt us here too. @@ -697,11 +698,6 @@ tcp_server_accept(int fd, short event, void *arg) } saddr_ntop((struct sockaddr *)&ss, sslen, tmp, sizeof(tmp)); - if ((r = fcntl(sock, F_GETFL, 0)) == -1) - err(1, "fcntl(F_GETFL)"); - r |= O_NONBLOCK; - if (fcntl(sock, F_SETFL, r) == -1) - err(1, "fcntl(F_SETFL, O_NONBLOCK)"); if (ptb->Tflag != -1 && ss.ss_family == AF_INET) { if (setsockopt(sock, IPPROTO_IP, IP_TOS, &ptb->Tflag, sizeof(ptb->Tflag))) diff --git a/usr.sbin/npppd/l2tp/l2tpd.c b/usr.sbin/npppd/l2tp/l2tpd.c index 48c0d236fde..e2b2c9d04be 100644 --- a/usr.sbin/npppd/l2tp/l2tpd.c +++ b/usr.sbin/npppd/l2tp/l2tpd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: l2tpd.c,v 1.18 2015/12/17 08:09:20 tb Exp $ */ +/* $OpenBSD: l2tpd.c,v 1.19 2016/03/21 00:49:36 guenther Exp $ */ /*- * Copyright (c) 2009 Internet Initiative Japan Inc. @@ -26,7 +26,7 @@ * SUCH DAMAGE. */ /**@file L2TP(Layer Two Tunneling Protocol "L2TP") / RFC2661 */ -/* $Id: l2tpd.c,v 1.18 2015/12/17 08:09:20 tb Exp $ */ +/* $Id: l2tpd.c,v 1.19 2016/03/21 00:49:36 guenther Exp $ */ #include <sys/types.h> #include <sys/socket.h> #include <sys/time.h> @@ -272,7 +272,7 @@ l2tpd_listener_start(l2tpd_listener *_this) strlcpy(_this->tun_name, L2TPD_DEFAULT_LAYER2_LABEL, sizeof(_this->tun_name)); if ((sock = socket(_this->bind.sin6.sin6_family, - SOCK_DGRAM, IPPROTO_UDP)) < 0) { + SOCK_DGRAM | SOCK_NONBLOCK, IPPROTO_UDP)) < 0) { l2tpd_log(_l2tpd, LOG_ERR, "socket() failed in %s(): %m", __func__); goto fail; @@ -284,15 +284,6 @@ l2tpd_listener_start(l2tpd_listener *_this) l2tpd_log(_l2tpd, LOG_WARNING, "%s(): setsockopt(IP_STRICT_RCVIF) failed: %m", __func__); #endif - if ((ival = fcntl(sock, F_GETFL, 0)) < 0) { - l2tpd_log(_l2tpd, LOG_ERR, - "fcntl(,F_GETFL) failed in %s(): %m", __func__); - goto fail; - } else if (fcntl(sock, F_SETFL, ival | O_NONBLOCK) < 0) { - l2tpd_log(_l2tpd, LOG_ERR, "fcntl(,F_SETFL,O_NONBLOCK) failed " - "in %s(): %m", __func__); - goto fail; - } ival = 1; if (setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, &ival, sizeof(ival)) != 0) { diff --git a/usr.sbin/npppd/pptp/pptpd.c b/usr.sbin/npppd/pptp/pptpd.c index be00b47c9bd..0c6475cb7ee 100644 --- a/usr.sbin/npppd/pptp/pptpd.c +++ b/usr.sbin/npppd/pptp/pptpd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: pptpd.c,v 1.29 2015/12/17 07:56:01 tb Exp $ */ +/* $OpenBSD: pptpd.c,v 1.30 2016/03/21 00:49:36 guenther Exp $ */ /*- * Copyright (c) 2009 Internet Initiative Japan Inc. @@ -25,12 +25,12 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ -/* $Id: pptpd.c,v 1.29 2015/12/17 07:56:01 tb Exp $ */ +/* $Id: pptpd.c,v 1.30 2016/03/21 00:49:36 guenther Exp $ */ /**@file * This file provides a implementation of PPTP daemon. Currently it * provides functions for PAC (PPTP Access Concentrator) only. - * $Id: pptpd.c,v 1.29 2015/12/17 07:56:01 tb Exp $ + * $Id: pptpd.c,v 1.30 2016/03/21 00:49:36 guenther Exp $ */ #include <sys/types.h> #include <sys/socket.h> @@ -300,7 +300,8 @@ pptpd_listener_start(pptpd_listener *_this) memcpy(&bind_sin, &_this->bind_sin, sizeof(bind_sin)); memcpy(&bind_sin_gre, &_this->bind_sin_gre, sizeof(bind_sin_gre)); - if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) { + if ((sock = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, IPPROTO_TCP)) + < 0) { pptpd_log(_this->self, LOG_ERR, "socket() failed at %s(): %m", __func__); goto fail; @@ -317,15 +318,6 @@ pptpd_listener_start(pptpd_listener *_this) pptpd_log(_this->self, LOG_WARNING, "%s(): setsockopt(IP_STRICT_RCVIF) failed: %m", __func__); #endif - if ((ival = fcntl(sock, F_GETFL, 0)) < 0) { - pptpd_log(_this->self, LOG_ERR, - "fcntl(F_GET_FL) failed at %s(): %m", __func__); - goto fail; - } else if (fcntl(sock, F_SETFL, ival | O_NONBLOCK) < 0) { - pptpd_log(_this->self, LOG_ERR, - "fcntl(F_SET_FL) failed at %s(): %m", __func__); - goto fail; - } if (bind(sock, (struct sockaddr *)&_this->bind_sin, _this->bind_sin.sin_len) != 0) { pptpd_log(_this->self, LOG_ERR, diff --git a/usr.sbin/radiusd/radiusd.c b/usr.sbin/radiusd/radiusd.c index 364ca5ef29b..663a50c545f 100644 --- a/usr.sbin/radiusd/radiusd.c +++ b/usr.sbin/radiusd/radiusd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: radiusd.c,v 1.15 2016/02/09 05:14:08 jsg Exp $ */ +/* $OpenBSD: radiusd.c,v 1.16 2016/03/21 00:49:36 guenther Exp $ */ /* * Copyright (c) 2013 Internet Initiative Japan Inc. @@ -196,7 +196,7 @@ radiusd_start(struct radiusd *radiusd) { struct radiusd_listen *l; struct radiusd_module *module; - int s, ival; + int s; char hbuf[NI_MAXHOST]; TAILQ_FOREACH(l, &radiusd->listen, next) { @@ -206,8 +206,8 @@ radiusd_start(struct radiusd *radiusd) log_warn("%s: getnameinfo()", __func__); goto on_error; } - if ((s = socket(l->addr.ipv4.sin_family, l->stype, l->sproto)) - < 0) { + if ((s = socket(l->addr.ipv4.sin_family, + l->stype | SOCK_NONBLOCK, l->sproto)) < 0) { log_warn("Listen %s port %d is failed: socket()", hbuf, (int)htons(l->addr.ipv4.sin_port)); goto on_error; @@ -219,16 +219,6 @@ radiusd_start(struct radiusd *radiusd) close(s); goto on_error; } - if ((ival = fcntl(s, F_GETFL, 0)) < 0) { - log_warn("fcntl(F_GETFL) failed at %s()", __func__); - close(s); - goto on_error; - } - if (fcntl(s, F_SETFL, ival | O_NONBLOCK) < 0) { - log_warn("fcntl(F_SETFL,O_NONBLOCK) failed at %s()", __func__); - close(s); - goto on_error; - } if (l->addr.ipv4.sin_family == AF_INET) log_info("Start listening on %s:%d/udp", hbuf, (int)ntohs(l->addr.ipv4.sin_port)); diff --git a/usr.sbin/radiusd/radiusd_radius.c b/usr.sbin/radiusd/radiusd_radius.c index c9d7f5757be..7dafb2d2c24 100644 --- a/usr.sbin/radiusd/radiusd_radius.c +++ b/usr.sbin/radiusd/radiusd_radius.c @@ -1,4 +1,4 @@ -/* $OpenBSD: radiusd_radius.c,v 1.11 2015/11/03 05:09:22 mmcc Exp $ */ +/* $OpenBSD: radiusd_radius.c,v 1.12 2016/03/21 00:49:36 guenther Exp $ */ /* * Copyright (c) 2013 Internet Initiative Japan Inc. @@ -330,12 +330,12 @@ on_fail: static int radius_server_start(struct radius_server *server) { - int ival; socklen_t locallen; char buf0[NI_MAXHOST + NI_MAXSERV + 32]; char buf1[NI_MAXHOST + NI_MAXSERV + 32]; - if ((server->sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) { + if ((server->sock = socket(AF_INET, SOCK_DGRAM | SOCK_NONBLOCK, 0)) + < 0) { module_radius_log(server->module, LOG_WARNING, "%s: socket() failed", __func__); goto on_error; @@ -348,16 +348,6 @@ radius_server_start(struct radius_server *server) server->addr.sin4.sin_len, buf1, sizeof(buf1))); goto on_error; } - if ((ival = fcntl(server->sock, F_GETFL, 0)) < 0) { - module_radius_log(server->module, LOG_WARNING, - "%s: fcntl(F_GETFL) failed", __func__); - goto on_error; - } - if (fcntl(server->sock, F_SETFL, ival | O_NONBLOCK) < 0) { - module_radius_log(server->module, LOG_WARNING, - "%s: fcntl(F_SETFL) failed", __func__); - goto on_error; - } locallen = sizeof(server->local); if (getsockname(server->sock, (struct sockaddr *)&server->local, &locallen) != 0) { diff --git a/usr.sbin/ypbind/ypbind.c b/usr.sbin/ypbind/ypbind.c index e4b78df3d00..9c4aef0e470 100644 --- a/usr.sbin/ypbind/ypbind.c +++ b/usr.sbin/ypbind/ypbind.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ypbind.c,v 1.65 2015/12/12 20:04:23 mmcc Exp $ */ +/* $OpenBSD: ypbind.c,v 1.66 2016/03/21 00:49:36 guenther Exp $ */ /* * Copyright (c) 1992, 1993, 1996, 1997, 1998 Theo de Raadt <deraadt@openbsd.org> @@ -475,7 +475,7 @@ main(int argc, char *argv[]) } } - if ((rpcsock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) { + if ((rpcsock = socket(AF_INET, SOCK_DGRAM | SOCK_NONBLOCK, 0)) < 0) { perror("socket"); return -1; } @@ -485,7 +485,7 @@ main(int argc, char *argv[]) sin.sin_port = 0; bindresvport(rpcsock, &sin); - if ((pingsock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) { + if ((pingsock = socket(AF_INET, SOCK_DGRAM | SOCK_NONBLOCK, 0)) < 0) { perror("socket"); return -1; } @@ -495,8 +495,6 @@ main(int argc, char *argv[]) sin.sin_port = 0; bindresvport(pingsock, &sin); - fcntl(rpcsock, F_SETFL, fcntl(rpcsock, F_GETFL, 0) | FNDELAY); - fcntl(pingsock, F_SETFL, fcntl(pingsock, F_GETFL, 0) | FNDELAY); setsockopt(rpcsock, SOL_SOCKET, SO_BROADCAST, &one, (socklen_t)sizeof(one)); rmtca.prog = YPPROG; |