@@ -734,11 +734,17 @@ static int set_reuse_addr(int sockfd)
734
734
& on , sizeof (on ));
735
735
}
736
736
737
+ struct socketlist {
738
+ int * list ;
739
+ size_t nr ;
740
+ size_t alloc ;
741
+ };
742
+
737
743
#ifndef NO_IPV6
738
744
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 )
740
746
{
741
- int socknum = 0 , * socklist = NULL ;
747
+ int socknum = 0 ;
742
748
int maxfd = -1 ;
743
749
char pbuf [NI_MAXSERV ];
744
750
struct addrinfo hints , * ai0 , * ai ;
@@ -753,8 +759,10 @@ static int socksetup(char *listen_addr, int listen_port, int **socklist_p)
753
759
hints .ai_flags = AI_PASSIVE ;
754
760
755
761
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
+ }
758
766
759
767
for (ai = ai0 ; ai ; ai = ai -> ai_next ) {
760
768
int sockfd ;
@@ -795,22 +803,22 @@ static int socksetup(char *listen_addr, int listen_port, int **socklist_p)
795
803
if (flags >= 0 )
796
804
fcntl (sockfd , F_SETFD , flags | FD_CLOEXEC );
797
805
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 ++ ;
800
809
801
810
if (maxfd < sockfd )
802
811
maxfd = sockfd ;
803
812
}
804
813
805
814
freeaddrinfo (ai0 );
806
815
807
- * socklist_p = socklist ;
808
816
return socknum ;
809
817
}
810
818
811
819
#else /* NO_IPV6 */
812
820
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 )
814
822
{
815
823
struct sockaddr_in sin ;
816
824
int sockfd ;
@@ -851,22 +859,27 @@ static int socksetup(char *listen_addr, int listen_port, int **socklist_p)
851
859
if (flags >= 0 )
852
860
fcntl (sockfd , F_SETFD , flags | FD_CLOEXEC );
853
861
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 ;
856
864
return 1 ;
857
865
}
858
866
859
867
#endif
860
868
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 )
862
875
{
863
876
struct pollfd * pfd ;
864
877
int i ;
865
878
866
- pfd = xcalloc (socknum , sizeof (struct pollfd ));
879
+ pfd = xcalloc (socklist -> nr , sizeof (struct pollfd ));
867
880
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 ];
870
883
pfd [i ].events = POLLIN ;
871
884
}
872
885
@@ -877,7 +890,7 @@ static int service_loop(int socknum, int *socklist)
877
890
878
891
check_dead_children ();
879
892
880
- if (poll (pfd , socknum , -1 ) < 0 ) {
893
+ if (poll (pfd , socklist -> nr , -1 ) < 0 ) {
881
894
if (errno != EINTR ) {
882
895
logerror ("Poll failed, resuming: %s" ,
883
896
strerror (errno ));
@@ -886,7 +899,7 @@ static int service_loop(int socknum, int *socklist)
886
899
continue ;
887
900
}
888
901
889
- for (i = 0 ; i < socknum ; i ++ ) {
902
+ for (i = 0 ; i < socklist -> nr ; i ++ ) {
890
903
if (pfd [i ].revents & POLLIN ) {
891
904
struct sockaddr_storage ss ;
892
905
unsigned int sslen = sizeof (ss );
@@ -948,10 +961,10 @@ static void store_pid(const char *path)
948
961
949
962
static int serve (char * listen_addr , int listen_port , struct passwd * pass , gid_t gid )
950
963
{
951
- int socknum , * socklist ;
964
+ struct socketlist socklist = { NULL , 0 , 0 } ;
952
965
953
- socknum = socksetup (listen_addr , listen_port , & socklist );
954
- if (socknum == 0 )
966
+ socksetup (listen_addr , listen_port , & socklist );
967
+ if (socklist . nr == 0 )
955
968
die ("unable to allocate any listen sockets on host %s port %u" ,
956
969
listen_addr , listen_port );
957
970
@@ -960,7 +973,7 @@ static int serve(char *listen_addr, int listen_port, struct passwd *pass, gid_t
960
973
setuid (pass -> pw_uid )))
961
974
die ("cannot drop privileges" );
962
975
963
- return service_loop (socknum , socklist );
976
+ return service_loop (& socklist );
964
977
}
965
978
966
979
int main (int argc , char * * argv )
0 commit comments