@@ -734,11 +734,17 @@ static int set_reuse_addr(int sockfd)
734734 & on , sizeof (on ));
735735}
736736
737+ struct socketlist {
738+ int * list ;
739+ size_t nr ;
740+ size_t alloc ;
741+ };
742+
737743#ifndef NO_IPV6
738744
739- static int socksetup (char * listen_addr , int listen_port , int * * socklist_p )
745+ static int setup_named_sock (char * listen_addr , int listen_port , struct socketlist * socklist )
740746{
741- int socknum = 0 , * socklist = NULL ;
747+ int socknum = 0 ;
742748 int maxfd = -1 ;
743749 char pbuf [NI_MAXSERV ];
744750 struct addrinfo hints , * ai0 , * ai ;
@@ -753,8 +759,10 @@ static int socksetup(char *listen_addr, int listen_port, int **socklist_p)
753759 hints .ai_flags = AI_PASSIVE ;
754760
755761 gai = getaddrinfo (listen_addr , pbuf , & hints , & ai0 );
756- if (gai )
757- die ("getaddrinfo() failed: %s" , gai_strerror (gai ));
762+ if (gai ) {
763+ logerror ("getaddrinfo() for %s failed: %s" , listen_addr , gai_strerror (gai ));
764+ return 0 ;
765+ }
758766
759767 for (ai = ai0 ; ai ; ai = ai -> ai_next ) {
760768 int sockfd ;
@@ -795,22 +803,22 @@ static int socksetup(char *listen_addr, int listen_port, int **socklist_p)
795803 if (flags >= 0 )
796804 fcntl (sockfd , F_SETFD , flags | FD_CLOEXEC );
797805
798- socklist = xrealloc (socklist , sizeof (int ) * (socknum + 1 ));
799- socklist [socknum ++ ] = sockfd ;
806+ ALLOC_GROW (socklist -> list , socklist -> nr + 1 , socklist -> alloc );
807+ socklist -> list [socklist -> nr ++ ] = sockfd ;
808+ socknum ++ ;
800809
801810 if (maxfd < sockfd )
802811 maxfd = sockfd ;
803812 }
804813
805814 freeaddrinfo (ai0 );
806815
807- * socklist_p = socklist ;
808816 return socknum ;
809817}
810818
811819#else /* NO_IPV6 */
812820
813- static int socksetup (char * listen_addr , int listen_port , int * * socklist_p )
821+ static int setup_named_sock (char * listen_addr , int listen_port , struct socketlist * socklist )
814822{
815823 struct sockaddr_in sin ;
816824 int sockfd ;
@@ -851,22 +859,27 @@ static int socksetup(char *listen_addr, int listen_port, int **socklist_p)
851859 if (flags >= 0 )
852860 fcntl (sockfd , F_SETFD , flags | FD_CLOEXEC );
853861
854- * socklist_p = xmalloc ( sizeof ( int ) );
855- * * socklist_p = sockfd ;
862+ ALLOC_GROW ( socklist -> list , socklist -> nr + 1 , socklist -> alloc );
863+ socklist -> list [ socklist -> nr ++ ] = sockfd ;
856864 return 1 ;
857865}
858866
859867#endif
860868
861- static int service_loop (int socknum , int * socklist )
869+ static void socksetup (char * listen_addr , int listen_port , struct socketlist * socklist )
870+ {
871+ setup_named_sock (listen_addr , listen_port , socklist );
872+ }
873+
874+ static int service_loop (struct socketlist * socklist )
862875{
863876 struct pollfd * pfd ;
864877 int i ;
865878
866- pfd = xcalloc (socknum , sizeof (struct pollfd ));
879+ pfd = xcalloc (socklist -> nr , sizeof (struct pollfd ));
867880
868- for (i = 0 ; i < socknum ; i ++ ) {
869- pfd [i ].fd = socklist [i ];
881+ for (i = 0 ; i < socklist -> nr ; i ++ ) {
882+ pfd [i ].fd = socklist -> list [i ];
870883 pfd [i ].events = POLLIN ;
871884 }
872885
@@ -877,7 +890,7 @@ static int service_loop(int socknum, int *socklist)
877890
878891 check_dead_children ();
879892
880- if (poll (pfd , socknum , -1 ) < 0 ) {
893+ if (poll (pfd , socklist -> nr , -1 ) < 0 ) {
881894 if (errno != EINTR ) {
882895 logerror ("Poll failed, resuming: %s" ,
883896 strerror (errno ));
@@ -886,7 +899,7 @@ static int service_loop(int socknum, int *socklist)
886899 continue ;
887900 }
888901
889- for (i = 0 ; i < socknum ; i ++ ) {
902+ for (i = 0 ; i < socklist -> nr ; i ++ ) {
890903 if (pfd [i ].revents & POLLIN ) {
891904 struct sockaddr_storage ss ;
892905 unsigned int sslen = sizeof (ss );
@@ -948,10 +961,10 @@ static void store_pid(const char *path)
948961
949962static int serve (char * listen_addr , int listen_port , struct passwd * pass , gid_t gid )
950963{
951- int socknum , * socklist ;
964+ struct socketlist socklist = { NULL , 0 , 0 } ;
952965
953- socknum = socksetup (listen_addr , listen_port , & socklist );
954- if (socknum == 0 )
966+ socksetup (listen_addr , listen_port , & socklist );
967+ if (socklist . nr == 0 )
955968 die ("unable to allocate any listen sockets on host %s port %u" ,
956969 listen_addr , listen_port );
957970
@@ -960,7 +973,7 @@ static int serve(char *listen_addr, int listen_port, struct passwd *pass, gid_t
960973 setuid (pass -> pw_uid )))
961974 die ("cannot drop privileges" );
962975
963- return service_loop (socknum , socklist );
976+ return service_loop (& socklist );
964977}
965978
966979int main (int argc , char * * argv )
0 commit comments