author | Junio C Hamano <junkio@cox.net> | |
Fri, 21 Oct 2005 06:21:50 +0000 (23:21 -0700) | ||
committer | Junio C Hamano <junkio@cox.net> | |
Fri, 21 Oct 2005 06:21:50 +0000 (23:21 -0700) |
1 | 2 | |||
---|---|---|---|---|
daemon.c | patch | | diff1 | | diff2 | | blob | history |
diff --cc daemon.c
index 2b56d7d225a703b6a206ad949a87765dff147760,cec7e75d5ec6be6f14572592024f2461194b7369..0c6182fb93fce6937ecb2634462f7f6b0a835662
+++ b/daemon.c
return -1;
}
- if (chdir(dir) < 0) {
- logerror("Cannot chdir('%s'): %s", dir, strerror(errno));
+ if ( chdir(dir) )
return -1;
-
- }
-
- chdir(".git");
+
/*
* Security on the cheap.
*
freeaddrinfo(ai0);
- if (socknum == 0)
- die("unable to allocate any listen sockets on port %u", port);
+ *socklist_p = socklist;
+ return socknum;
+}
+
+#else /* NO_IPV6 */
+
+static int socksetup(int port, int **socklist_p)
+{
+ struct sockaddr_in sin;
+ int sockfd;
+
+ sockfd = socket(AF_INET, SOCK_STREAM, 0);
+ if (sockfd < 0)
+ return 0;
+
+ memset(&sin, 0, sizeof sin);
+ sin.sin_family = AF_INET;
+ sin.sin_addr.s_addr = htonl(INADDR_ANY);
+ sin.sin_port = htons(port);
+
+ if ( bind(sockfd, (struct sockaddr *)&sin, sizeof sin) < 0 ) {
+ close(sockfd);
+ return 0;
+ }
+
+ *socklist_p = xmalloc(sizeof(int));
+ **socklist_p = sockfd;
+}
+
+#endif
+
+static int service_loop(int socknum, int *socklist)
+{
+ struct pollfd *pfd;
+ int i;
+
+ pfd = xcalloc(socknum, sizeof(struct pollfd));
+
+ for (i = 0; i < socknum; i++) {
+ pfd[i].fd = socklist[i];
+ pfd[i].events = POLLIN;
+ }
+
+ signal(SIGCHLD, child_handler);
-
+
for (;;) {
int i;
- fds = fds_init;
- if (select(maxfd + 1, &fds, NULL, NULL, NULL) < 0) {
+ if (poll(pfd, socknum, -1) < 0) {
if (errno != EINTR) {
- error("select failed, resuming: %s",
+ error("poll failed, resuming: %s",
strerror(errno));
sleep(1);
}
}
}
-
+static int serve(int port)
+{
+ int socknum, *socklist;
-
++
+ socknum = socksetup(port, &socklist);
+ if (socknum == 0)
+ die("unable to allocate any listen sockets on port %u", port);
- }
++
+ return service_loop(socknum, socklist);
++}
+
int main(int argc, char **argv)
{
int port = DEFAULT_GIT_PORT;