daemon: add helper function named_sock_setup
Add named_sock_setup as helper function for socksetup to make it easier to create more than one listen sockets. named_sock_setup could be called more than one time and add the new sockets to the supplied socklist_p. Signed-off-by: Alexander Sulfrian <alexander@sulfrian.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Alexander Sulfrian committed
Aug 30, 2010 at 13:30 UTC
2caa321503cbb6dfd3867d5befe615ae541a77b3
1 file changed
+33
-20
daemon.c
+33
-20
@@ -734,11 +734,17 @@ static int set_reuse_addr(int sockfd)
734
&on, sizeof(on));
735
}
736
737
+struct socketlist {
738
+ int *list;
739
+ size_t nr;
740
+ size_t alloc;
741
+};
742
+
743
#ifndef NO_IPV6
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)
746
{
741
- int socknum = 0, *socklist = NULL;
747
+ int socknum = 0;
748
int maxfd = -1;
749
char pbuf[NI_MAXSERV];
750
struct addrinfo hints, *ai0, *ai;
@@ -753,8 +759,10 @@ static int socksetup(char *listen_addr, int listen_port, int **socklist_p)
759
hints.ai_flags = AI_PASSIVE;
760
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
+ }
766
767
for (ai = ai0; ai; ai = ai->ai_next) {
768
int sockfd;
@@ -795,8 +803,9 @@ static int socksetup(char *listen_addr, int listen_port, int **socklist_p)
803
if (flags >= 0)
804
fcntl(sockfd, F_SETFD, flags | FD_CLOEXEC);
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++;
809
810
if (maxfd < sockfd)
811
maxfd = sockfd;
@@ -804,13 +813,12 @@ static int socksetup(char *listen_addr, int listen_port, int **socklist_p)
813
814
freeaddrinfo(ai0);
815
807
- *socklist_p = socklist;
816
return socknum;
817
}
818
819
#else /* NO_IPV6 */
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)
822
{
823
struct sockaddr_in sin;
824
int sockfd;
@@ -851,22 +859,27 @@ static int socksetup(char *listen_addr, int listen_port, int **socklist_p)
859
if (flags >= 0)
860
fcntl(sockfd, F_SETFD, flags | FD_CLOEXEC);
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;
864
return 1;
865
}
866
867
#endif
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)
875
{
876
struct pollfd *pfd;
877
int i;
878
866
- pfd = xcalloc(socknum, sizeof(struct pollfd));
879
+ pfd = xcalloc(socklist->nr, sizeof(struct pollfd));
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];
883
pfd[i].events = POLLIN;
884
}
885
@@ -877,7 +890,7 @@ static int service_loop(int socknum, int *socklist)
890
891
check_dead_children();
892
880
- if (poll(pfd, socknum, -1) < 0) {
893
+ if (poll(pfd, socklist->nr, -1) < 0) {
894
if (errno != EINTR) {
895
logerror("Poll failed, resuming: %s",
896
strerror(errno));
@@ -886,7 +899,7 @@ static int service_loop(int socknum, int *socklist)
899
continue;
900
}
901
889
- for (i = 0; i < socknum; i++) {
902
+ for (i = 0; i < socklist->nr; i++) {
903
if (pfd[i].revents & POLLIN) {
904
struct sockaddr_storage ss;
905
unsigned int sslen = sizeof(ss);
@@ -948,10 +961,10 @@ static void store_pid(const char *path)
961
962
static int serve(char *listen_addr, int listen_port, struct passwd *pass, gid_t gid)
963
{
951
- int socknum, *socklist;
964
+ struct socketlist socklist = { NULL, 0, 0 };
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)
968
die("unable to allocate any listen sockets on host %s port %u",
969
listen_addr, listen_port);
970
@@ -960,7 +973,7 @@ static int serve(char *listen_addr, int listen_port, struct passwd *pass, gid_t
973
setuid(pass->pw_uid)))
974
die("cannot drop privileges");
975
963
- return service_loop(socknum, socklist);
976
+ return service_loop(&socklist);
977
}
978
979
int main(int argc, char **argv)