From owner-svn-src-all@freebsd.org Wed May 11 17:52:07 2016 Return-Path: Delivered-To: svn-src-all@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 9A902B372EB; Wed, 11 May 2016 17:52:07 +0000 (UTC) (envelope-from cem@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 544B216B8; Wed, 11 May 2016 17:52:07 +0000 (UTC) (envelope-from cem@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id u4BHq6JZ054785; Wed, 11 May 2016 17:52:06 GMT (envelope-from cem@FreeBSD.org) Received: (from cem@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id u4BHq6TI054784; Wed, 11 May 2016 17:52:06 GMT (envelope-from cem@FreeBSD.org) Message-Id: <201605111752.u4BHq6TI054784@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: cem set sender to cem@FreeBSD.org using -f From: "Conrad E. Meyer" Date: Wed, 11 May 2016 17:52:06 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r299473 - head/usr.bin/whois X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 11 May 2016 17:52:07 -0000 Author: cem Date: Wed May 11 17:52:06 2016 New Revision: 299473 URL: https://svnweb.freebsd.org/changeset/base/299473 Log: whois(1): Pull out async multiple host connection code into a routine This logic was added to the whois() function in r281959, but could easily be its own routine. In this case, I think the abstraction makes both functions easier to reason about. This precedes some Coverity-suggested cleanup. Sponsored by: EMC / Isilon Storage Division Modified: head/usr.bin/whois/whois.c Modified: head/usr.bin/whois/whois.c ============================================================================== --- head/usr.bin/whois/whois.c Wed May 11 17:40:51 2016 (r299472) +++ head/usr.bin/whois/whois.c Wed May 11 17:52:06 2016 (r299473) @@ -285,19 +285,15 @@ s_asprintf(char **ret, const char *forma va_end(ap); } -static void -whois(const char *query, const char *hostname, int flags) +static int +connect_to_any_host(struct addrinfo *hostres) { - FILE *fp; - struct addrinfo *hostres, *res; - char *buf, *host, *nhost, *p; - int s = -1, f; + struct addrinfo *res; nfds_t i, j; - size_t len, count; + size_t count; struct pollfd *fds; - int timeout = 180; + int timeout = 180, s = -1; - hostres = gethostinfo(hostname, 1); for (res = hostres, count = 0; res; res = res->ai_next) count++; fds = calloc(count, sizeof(*fds)); @@ -401,15 +397,29 @@ whois(const char *query, const char *hos s = -1; if (count == 0) errno = ETIMEDOUT; -done: - if (s == -1) - err(EX_OSERR, "connect()"); +done: /* Close all watched fds except the succeeded one */ for (j = 0; j < i; j++) if (fds[j].fd != s && fds[j].fd != -1) close(fds[j].fd); free(fds); + return (s); +} + +static void +whois(const char *query, const char *hostname, int flags) +{ + FILE *fp; + struct addrinfo *hostres; + char *buf, *host, *nhost, *p; + int s, f; + size_t len, i; + + hostres = gethostinfo(hostname, 1); + s = connect_to_any_host(hostres); + if (s == -1) + err(EX_OSERR, "connect()"); /* Restore default blocking behavior. */ if ((f = fcntl(s, F_GETFL)) == -1)