]> git.ipfire.org Git - thirdparty/git.git/blame - daemon.c
git-daemon: keep track of children
[thirdparty/git.git] / daemon.c
CommitLineData
a87e8be2
LT
1#include "cache.h"
2#include "pkt-line.h"
eaa94919
LT
3#include <signal.h>
4#include <sys/wait.h>
a87e8be2
LT
5#include <sys/socket.h>
6#include <netinet/in.h>
7
e64e1b79 8static const char daemon_usage[] = "git-daemon [--inetd | --port=n]";
a87e8be2 9
eaa94919
LT
10/* We don't actually do anything about this yet */
11static int max_connections = 10;
12
13/*
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.
18 */
19static unsigned int children_spawned = 0;
20static unsigned int children_reaped = 0;
21
a87e8be2
LT
22static int upload(char *dir, int dirlen)
23{
24 if (chdir(dir) < 0)
25 return -1;
26 chdir(".git");
27
28 /*
29 * Security on the cheap.
30 *
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.
34 */
35 if (access("git-daemon-export-ok", F_OK) ||
36 access("objects/00", X_OK) ||
37 access("HEAD", R_OK))
38 return -1;
39
40 /* git-upload-pack only ever reads stuff, so this is safe */
41 execlp("git-upload-pack", "git-upload-pack", ".", NULL);
42 return -1;
43}
44
7d80694a 45static int execute(void)
a87e8be2 46{
7d80694a
LT
47 static char line[1000];
48 int len;
49
50 len = packet_read_line(0, line, sizeof(line));
51
52 if (len && line[len-1] == '\n')
53 line[--len] = 0;
54
a87e8be2
LT
55 if (!strncmp("git-upload-pack /", line, 17))
56 return upload(line + 16, len - 16);
57
58 fprintf(stderr, "got bad connection '%s'\n", line);
59 return -1;
60}
61
62static void handle(int incoming, struct sockaddr_in *addr, int addrlen)
63{
eaa94919
LT
64 pid_t pid = fork();
65
66 if (pid) {
67 int active;
68
a87e8be2 69 close(incoming);
eaa94919
LT
70 if (pid < 0)
71 return;
72
73 active = ++children_spawned - children_reaped;
74 if (active > max_connections) {
75 /*
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
79 * started yet.
80 */
81 }
a87e8be2
LT
82 return;
83 }
84
85 dup2(incoming, 0);
86 dup2(incoming, 1);
87 close(incoming);
7d80694a 88 exit(execute());
a87e8be2
LT
89}
90
eaa94919
LT
91static void child_handler(int signo)
92{
93 for (;;) {
94 if (waitpid(-1, NULL, WNOHANG) > 0) {
95 children_reaped++;
96 continue;
97 }
98 break;
99 }
100}
101
a87e8be2
LT
102static int serve(int port)
103{
104 int sockfd;
105 struct sockaddr_in addr;
106
eaa94919 107 signal(SIGCHLD, child_handler);
a87e8be2
LT
108 sockfd = socket(PF_INET, SOCK_STREAM, IPPROTO_IP);
109 if (sockfd < 0)
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));
118
119 for (;;) {
120 struct sockaddr_in in;
121 socklen_t addrlen = sizeof(in);
122 int incoming = accept(sockfd, (void *)&in, &addrlen);
123
124 if (incoming < 0) {
125 switch (errno) {
126 case EAGAIN:
127 case EINTR:
128 case ECONNABORTED:
129 continue;
130 default:
131 die("accept returned %s", strerror(errno));
132 }
133 }
134 handle(incoming, &in, addrlen);
135 }
136}
137
138int main(int argc, char **argv)
139{
140 int port = DEFAULT_GIT_PORT;
e64e1b79 141 int inetd_mode = 0;
a87e8be2
LT
142 int i;
143
144 for (i = 1; i < argc; i++) {
145 char *arg = argv[i];
146
147 if (!strncmp(arg, "--port=", 7)) {
148 char *end;
149 unsigned long n;
150 n = strtoul(arg+7, &end, 0);
151 if (arg[7] && !*end) {
152 port = n;
153 continue;
154 }
155 }
e64e1b79
LT
156
157 if (!strcmp(arg, "--inetd")) {
158 inetd_mode = 1;
159 continue;
160 }
161
a87e8be2
LT
162 usage(daemon_usage);
163 }
164
e64e1b79
LT
165 if (inetd_mode)
166 return execute();
167
a87e8be2
LT
168 return serve(port);
169}