From owner-svn-src-projects@FreeBSD.ORG Sun Aug 22 21:06:19 2010 Return-Path: Delivered-To: svn-src-projects@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 2BE7B1065698; Sun, 22 Aug 2010 21:06:19 +0000 (UTC) (envelope-from attilio@FreeBSD.org) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:4f8:fff6::2c]) by mx1.freebsd.org (Postfix) with ESMTP id 1B1BF8FC13; Sun, 22 Aug 2010 21:06:19 +0000 (UTC) Received: from svn.freebsd.org (localhost [127.0.0.1]) by svn.freebsd.org (8.14.3/8.14.3) with ESMTP id o7ML6J1a015011; Sun, 22 Aug 2010 21:06:19 GMT (envelope-from attilio@svn.freebsd.org) Received: (from attilio@localhost) by svn.freebsd.org (8.14.3/8.14.3/Submit) id o7ML6IOO015009; Sun, 22 Aug 2010 21:06:18 GMT (envelope-from attilio@svn.freebsd.org) Message-Id: <201008222106.o7ML6IOO015009@svn.freebsd.org> From: Attilio Rao Date: Sun, 22 Aug 2010 21:06:18 +0000 (UTC) To: src-committers@freebsd.org, svn-src-projects@freebsd.org X-SVN-Group: projects MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cc: Subject: svn commit: r211645 - projects/sv/usr.sbin/netdumpsrv X-BeenThere: svn-src-projects@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "SVN commit messages for the src " projects" tree" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 22 Aug 2010 21:06:19 -0000 Author: attilio Date: Sun Aug 22 21:06:18 2010 New Revision: 211645 URL: http://svn.freebsd.org/changeset/base/211645 Log: Style-cleanup for eventloop(). Modified: projects/sv/usr.sbin/netdumpsrv/netdump_server.c Modified: projects/sv/usr.sbin/netdumpsrv/netdump_server.c ============================================================================== --- projects/sv/usr.sbin/netdumpsrv/netdump_server.c Sun Aug 22 20:30:18 2010 (r211644) +++ projects/sv/usr.sbin/netdumpsrv/netdump_server.c Sun Aug 22 21:06:18 2010 (r211645) @@ -590,147 +590,134 @@ handle_packet(struct netdump_client *cli } } -static void eventloop() +static void +eventloop() { - struct netdump_msg msg; - - while (!do_shutdown) - { + struct netdump_msg msg; + char fromstr[INET_ADDRSTRLEN + 6]; fd_set readfds; - int maxfd=sock+1; - struct netdump_client *client, *tmp; - struct timeval tv; struct sockaddr_in from; - char fromstr[INET_ADDRSTRLEN+6]; /* Long enough for IP+':'+port+'\0' */ - - FD_ZERO(&readfds); - FD_SET(sock, &readfds); - SLIST_FOREACH(client, &clients, iter) { - FD_SET(client->sock, &readfds); - if (maxfd <= client->sock) - { - maxfd = client->sock+1; - } - } - - /* So that we time out clients regularly */ - tv.tv_sec = 10; - tv.tv_usec = 0; - - if (select(maxfd, &readfds, NULL, NULL, &tv) == -1) - { - if (errno == EINTR) - { - continue; - } - - LOGERR_PERROR("select()"); - /* Errors with select() probably won't go away if we just try to - * select() again */ - pidfile_remove(pfh); - exit(1); - } - - now = time(NULL); - - if (FD_ISSET(sock, &readfds)) { - int len; - - len = receive_message(sock, &from, fromstr, sizeof(fromstr), &msg); + struct timeval tv; + struct netdump_client *client, *tmp; + int len, maxfd; - if (len == -1) - { - if (errno == EINTR) - { - continue; + while (do_shutdown == 0) { + maxfd = sock + 1; + FD_ZERO(&readfds); + FD_SET(sock, &readfds); + SLIST_FOREACH(client, &clients, iter) { + FD_SET(client->sock, &readfds); + if (maxfd <= client->sock) + maxfd = client->sock+1; } - if (errno != EAGAIN) - { - pidfile_remove(pfh); - LOGERR_PERROR("recvfrom()"); - exit(1); + /* So that we time out clients regularly. */ + tv.tv_sec = CLIENT_TPASS; + tv.tv_usec = 0; + if (select(maxfd, &readfds, NULL, NULL, &tv) == -1) { + if (errno == EINTR) + continue; + LOGERR_PERROR("select()"); + + /* + * Errors with select() probably will not go away + * with simple retrying. + */ + pidfile_remove(pfh); + exit(1); } - } - else if (len == 0) - { - /* The packet was rejected (probably because it was too small). - * Just ignore it. */ - } - else - { - /* Check if they're on the clients list */ - SLIST_FOREACH(client, &clients, iter) { - if (client->ip.s_addr == from.sin_addr.s_addr) - { - break; - } + now = time(NULL); + if (FD_ISSET(sock, &readfds)) { + len = receive_message(sock, &from, fromstr, + sizeof(fromstr), &msg); + if (len == -1) { + if (errno == EINTR) + continue; + if (errno != EAGAIN) { + pidfile_remove(pfh); + LOGERR_PERROR("recvfrom()"); + exit(1); + } + } else if (len != 0) { + + /* + * With len == 0 the packet was rejected + * (probably because it was too small) so just + * ignore this case. + */ + + /* Check if they are on the clients list. */ + SLIST_FOREACH(client, &clients, iter) + if (client->ip.s_addr == + from.sin_addr.s_addr) + break; + + /* + * Technically, clients should not be + * responding on the server port, so client + * should be NULL, however, if they insist on + * doing so, it's not really going to hurt + * anything (except maybe fill up the server + * socket's receive buffer), so still + * accept it. The only possibly legitimate case + * is if there's a new dump starting and the + * previous one didn't finish cleanly. Handle + * this by suppressing the error on HERALD + * packets. + */ + if (client != NULL && + msg.hdr.type != NETDUMP_HERALD && + client->printed_port_warning == 0) { + LOGWARN("Client %s responding on server port\n", + client->hostname); + client->printed_port_warning = 1; + } + handle_packet(client, &from, fromstr, &msg); + } } - /* Technically, clients shouldn't be responding on the server - * port, so client should be NULL, however, if they insist on - * doing so, it's not really going to hurt anything (except - * maybe fill up the server socket's receive buffer), so still - * accept it. The only possibly legitimate case is if there's - * a new dump starting and the previous one didn't finish - * cleanly. Handle this by suppressing the error on HERALD - * packets. + /* + * handle_packet() and handle_timeout() may free the client, + * handle stale pointers. */ - if (client && msg.hdr.type != NETDUMP_HERALD && - !client->printed_port_warning) - { - LOGWARN("Client %s responding on server port\n", - client->hostname); - client->printed_port_warning = 1; + SLIST_FOREACH_SAFE(client, &clients, iter, tmp) { + if (FD_ISSET(client->sock, &readfds)) { + len = receive_message(client->sock, &from, + fromstr, sizeof(fromstr), &msg); + if (len == -1) { + if (errno == EINTR || errno == EAGAIN) + continue; + LOGERR_PERROR("recvfrom()"); + + /* + * Client socket is broken for + * some reason. + */ + handle_timeout(client); + } else if (len != 0) { + + /* + * With len == 0 the packet was + * rejected (probably because it was + * too small) so just ignore this case. + */ + + FD_CLR(client->sock, &readfds); + handle_packet(client, &from, fromstr, + &msg); + } + } } - - handle_packet(client, &from, fromstr, &msg); - } + timeout_clients(); } + LOGINFO("Shutting down..."); /* - * handle_packet() and handle_timeout() may free the client, - * handle stale pointers. + * Clients is the head of the list, so clients != NULL iff the list + * is not empty. Call it a timeout so that the scripts get run. */ - SLIST_FOREACH_SAFE(client, &clients, iter, tmp) { - if (FD_ISSET(client->sock, &readfds)) - { - int len = receive_message(client->sock, &from, fromstr, - sizeof(fromstr), &msg); - if (len == -1) - { - if (errno == EINTR || errno == EAGAIN) - { - continue; - } - - LOGERR_PERROR("recvfrom()"); - /* Client socket is broken for some reason */ - handle_timeout(client); - } - else if (len == 0) - { - /* The packet was rejected (probably because it was too - * small). Just ignore it. */ - } - else - { - FD_CLR(client->sock, &readfds); - handle_packet(client, &from, fromstr, &msg); - } - } - } - - timeout_clients(); - } - - LOGINFO("Shutting down..."); - /* Clients is the head of the list, so clients != NULL iff the list isn't - * empty. Call it a timeout so that the scripts get run. */ - while (!SLIST_EMPTY(&clients)) - { - handle_timeout(SLIST_FIRST(&clients)); - } + while (!SLIST_EMPTY(&clients)) + handle_timeout(SLIST_FIRST(&clients)); } static void