5 #include <sys/socket.h>
6 #include <netinet/in.h>
8 static const char daemon_usage[] = "git-daemon [--inetd | --port=n]";
10 /* We don't actually do anything about this yet */
11 static int max_connections = 10;
14 * We count spawned/reaped separately, just to avoid any
15 * races when updating them from signals. The SIGCHLD handler
16 * will only update children_reaped, and the fork logic will
17 * only update children_spawned.
19 static unsigned int children_spawned = 0;
20 static unsigned int children_reaped = 0;
22 static int upload(char *dir, int dirlen)
29 * Security on the cheap.
31 * We want a readable HEAD, usable "objects" directory, and
32 * a "git-daemon-export-ok" flag that says that the other side
33 * is ok with us doing this.
35 if (access("git-daemon-export-ok", F_OK) ||
36 access("objects/00", X_OK) ||
40 /* git-upload-pack only ever reads stuff, so this is safe */
41 execlp("git-upload-pack", "git-upload-pack", ".", NULL);
45 static int execute(void)
47 static char line[1000];
50 len = packet_read_line(0, line, sizeof(line));
52 if (len && line[len-1] == '\n')
55 if (!strncmp("git-upload-pack /", line, 17))
56 return upload(line + 16, len - 16);
58 fprintf(stderr, "got bad connection '%s'\n", line);
62 static void handle(int incoming, struct sockaddr_in *addr, int addrlen)
73 active = ++children_spawned - children_reaped;
74 if (active > max_connections) {
76 * Fixme! This is where you'd have to do something to
77 * limit the number of children. Like killing off random
78 * ones, or at least the ones that haven't even gotten
91 static void child_handler(int signo)
94 if (waitpid(-1, NULL, WNOHANG) > 0) {
102 static int serve(int port)
105 struct sockaddr_in addr;
107 signal(SIGCHLD, child_handler);
108 sockfd = socket(PF_INET, SOCK_STREAM, IPPROTO_IP);
110 die("unable to open socket (%s)", strerror(errno));
111 memset(&addr, 0, sizeof(addr));
112 addr.sin_port = htons(port);
113 addr.sin_family = AF_INET;
114 if (bind(sockfd, (void *)&addr, sizeof(addr)) < 0)
115 die("unable to bind to port %d (%s)", port, strerror(errno));
116 if (listen(sockfd, 5) < 0)
117 die("unable to listen to port %d (%s)", port, strerror(errno));
120 struct sockaddr_in in;
121 socklen_t addrlen = sizeof(in);
122 int incoming = accept(sockfd, (void *)&in, &addrlen);
131 die("accept returned %s", strerror(errno));
134 handle(incoming, &in, addrlen);
138 int main(int argc, char **argv)
140 int port = DEFAULT_GIT_PORT;
144 for (i = 1; i < argc; i++) {
147 if (!strncmp(arg, "--port=", 7)) {
150 n = strtoul(arg+7, &end, 0);
151 if (arg[7] && !*end) {
157 if (!strcmp(arg, "--inetd")) {