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;