]>
Commit | Line | Data |
---|---|---|
eaa94919 LT |
1 | #include <signal.h> |
2 | #include <sys/wait.h> | |
a87e8be2 | 3 | #include <sys/socket.h> |
3cd6ecda | 4 | #include <sys/time.h> |
6573faff | 5 | #include <sys/poll.h> |
df076bdb | 6 | #include <netdb.h> |
a87e8be2 | 7 | #include <netinet/in.h> |
f8ff0c06 | 8 | #include <arpa/inet.h> |
9048fe1c | 9 | #include <syslog.h> |
979e32fa RS |
10 | #include "pkt-line.h" |
11 | #include "cache.h" | |
77cb17e9 | 12 | #include "exec_cmd.h" |
f8ff0c06 | 13 | |
9048fe1c | 14 | static int log_syslog; |
f8ff0c06 PB |
15 | static int verbose; |
16 | ||
960deccb PA |
17 | static const char daemon_usage[] = |
18 | "git-daemon [--verbose] [--syslog] [--inetd | --port=n] [--export-all]\n" | |
b21c31c9 PB |
19 | " [--timeout=n] [--init-timeout=n] [--strict-paths]\n" |
20 | " [--base-path=path] [directory...]"; | |
4ae95682 PA |
21 | |
22 | /* List of acceptable pathname prefixes */ | |
23 | static char **ok_paths = NULL; | |
4dbd1352 | 24 | static int strict_paths = 0; |
4ae95682 PA |
25 | |
26 | /* If this is set, git-daemon-export-ok is not required */ | |
27 | static int export_all_trees = 0; | |
f8ff0c06 | 28 | |
b21c31c9 PB |
29 | /* Take all paths relative to this one if non-NULL */ |
30 | static char *base_path = NULL; | |
31 | ||
960deccb PA |
32 | /* Timeout, and initial timeout */ |
33 | static unsigned int timeout = 0; | |
34 | static unsigned int init_timeout = 0; | |
f8ff0c06 | 35 | |
9048fe1c | 36 | static void logreport(int priority, const char *err, va_list params) |
f8ff0c06 PB |
37 | { |
38 | /* We should do a single write so that it is atomic and output | |
39 | * of several processes do not get intermingled. */ | |
40 | char buf[1024]; | |
41 | int buflen; | |
42 | int maxlen, msglen; | |
43 | ||
44 | /* sizeof(buf) should be big enough for "[pid] \n" */ | |
1bedd4ca | 45 | buflen = snprintf(buf, sizeof(buf), "[%ld] ", (long) getpid()); |
f8ff0c06 PB |
46 | |
47 | maxlen = sizeof(buf) - buflen - 1; /* -1 for our own LF */ | |
48 | msglen = vsnprintf(buf + buflen, maxlen, err, params); | |
49 | ||
9048fe1c PB |
50 | if (log_syslog) { |
51 | syslog(priority, "%s", buf); | |
52 | return; | |
53 | } | |
54 | ||
f8ff0c06 PB |
55 | /* maxlen counted our own LF but also counts space given to |
56 | * vsnprintf for the terminating NUL. We want to make sure that | |
57 | * we have space for our own LF and NUL after the "meat" of the | |
58 | * message, so truncate it at maxlen - 1. | |
59 | */ | |
60 | if (msglen > maxlen - 1) | |
61 | msglen = maxlen - 1; | |
62 | else if (msglen < 0) | |
63 | msglen = 0; /* Protect against weird return values. */ | |
64 | buflen += msglen; | |
65 | ||
66 | buf[buflen++] = '\n'; | |
67 | buf[buflen] = '\0'; | |
68 | ||
69 | write(2, buf, buflen); | |
70 | } | |
71 | ||
cdda4745 | 72 | static void logerror(const char *err, ...) |
f8ff0c06 PB |
73 | { |
74 | va_list params; | |
75 | va_start(params, err); | |
9048fe1c | 76 | logreport(LOG_ERR, err, params); |
f8ff0c06 PB |
77 | va_end(params); |
78 | } | |
79 | ||
cdda4745 | 80 | static void loginfo(const char *err, ...) |
f8ff0c06 PB |
81 | { |
82 | va_list params; | |
83 | if (!verbose) | |
84 | return; | |
85 | va_start(params, err); | |
9048fe1c | 86 | logreport(LOG_INFO, err, params); |
f8ff0c06 PB |
87 | va_end(params); |
88 | } | |
a87e8be2 | 89 | |
d79374c7 JH |
90 | static int avoid_alias(char *p) |
91 | { | |
92 | int sl, ndot; | |
93 | ||
94 | /* | |
95 | * This resurrects the belts and suspenders paranoia check by HPA | |
96 | * done in <435560F7.4080006@zytor.com> thread, now enter_repo() | |
97 | * does not do getcwd() based path canonicalizations. | |
98 | * | |
99 | * sl becomes true immediately after seeing '/' and continues to | |
100 | * be true as long as dots continue after that without intervening | |
101 | * non-dot character. | |
102 | */ | |
103 | if (!p || (*p != '/' && *p != '~')) | |
104 | return -1; | |
105 | sl = 1; ndot = 0; | |
106 | p++; | |
107 | ||
108 | while (1) { | |
109 | char ch = *p++; | |
110 | if (sl) { | |
111 | if (ch == '.') | |
112 | ndot++; | |
113 | else if (ch == '/') { | |
114 | if (ndot < 3) | |
115 | /* reject //, /./ and /../ */ | |
116 | return -1; | |
117 | ndot = 0; | |
118 | } | |
119 | else if (ch == 0) { | |
120 | if (0 < ndot && ndot < 3) | |
121 | /* reject /.$ and /..$ */ | |
122 | return -1; | |
123 | return 0; | |
124 | } | |
125 | else | |
126 | sl = ndot = 0; | |
127 | } | |
128 | else if (ch == 0) | |
129 | return 0; | |
130 | else if (ch == '/') { | |
131 | sl = 1; | |
132 | ndot = 0; | |
133 | } | |
134 | } | |
135 | } | |
136 | ||
4dbd1352 | 137 | static char *path_ok(char *dir) |
4ae95682 | 138 | { |
d79374c7 JH |
139 | char *path; |
140 | ||
141 | if (avoid_alias(dir)) { | |
142 | logerror("'%s': aliased", dir); | |
143 | return NULL; | |
144 | } | |
145 | ||
b21c31c9 PB |
146 | if (base_path) { |
147 | static char rpath[PATH_MAX]; | |
148 | if (*dir != '/') { | |
149 | /* Forbid possible base-path evasion using ~paths. */ | |
150 | logerror("'%s': Non-absolute path denied (base-path active)"); | |
151 | return NULL; | |
152 | } | |
153 | snprintf(rpath, PATH_MAX, "%s%s", base_path, dir); | |
154 | dir = rpath; | |
155 | } | |
156 | ||
d79374c7 | 157 | path = enter_repo(dir, strict_paths); |
3e04c62d | 158 | |
4dbd1352 AE |
159 | if (!path) { |
160 | logerror("'%s': unable to chdir or not a git archive", dir); | |
161 | return NULL; | |
4ae95682 PA |
162 | } |
163 | ||
164 | if ( ok_paths && *ok_paths ) { | |
ce335fe0 | 165 | char **pp; |
4dbd1352 | 166 | int pathlen = strlen(path); |
4ae95682 | 167 | |
ce335fe0 | 168 | /* The validation is done on the paths after enter_repo |
d79374c7 JH |
169 | * appends optional {.git,.git/.git} and friends, but |
170 | * it does not use getcwd(). So if your /pub is | |
171 | * a symlink to /mnt/pub, you can whitelist /pub and | |
172 | * do not have to say /mnt/pub. | |
173 | * Do not say /pub/. | |
ce335fe0 | 174 | */ |
4ae95682 PA |
175 | for ( pp = ok_paths ; *pp ; pp++ ) { |
176 | int len = strlen(*pp); | |
ce335fe0 JH |
177 | if (len <= pathlen && |
178 | !memcmp(*pp, path, len) && | |
179 | (path[len] == '\0' || | |
180 | (!strict_paths && path[len] == '/'))) | |
181 | return path; | |
4ae95682 | 182 | } |
4dbd1352 AE |
183 | } |
184 | else { | |
185 | /* be backwards compatible */ | |
186 | if (!strict_paths) | |
187 | return path; | |
4ae95682 PA |
188 | } |
189 | ||
4dbd1352 AE |
190 | logerror("'%s': not in whitelist", path); |
191 | return NULL; /* Fallthrough. Deny by default */ | |
4ae95682 | 192 | } |
a87e8be2 | 193 | |
4dbd1352 | 194 | static int upload(char *dir) |
a87e8be2 | 195 | { |
4dbd1352 AE |
196 | /* Timeout as string */ |
197 | char timeout_buf[64]; | |
198 | const char *path; | |
199 | ||
200 | loginfo("Request for '%s'", dir); | |
4ae95682 | 201 | |
4dbd1352 | 202 | if (!(path = path_ok(dir))) |
a87e8be2 | 203 | return -1; |
47888f0f | 204 | |
a87e8be2 LT |
205 | /* |
206 | * Security on the cheap. | |
207 | * | |
a935c397 | 208 | * We want a readable HEAD, usable "objects" directory, and |
a87e8be2 LT |
209 | * a "git-daemon-export-ok" flag that says that the other side |
210 | * is ok with us doing this. | |
4dbd1352 AE |
211 | * |
212 | * path_ok() uses enter_repo() and does whitelist checking. | |
213 | * We only need to make sure the repository is exported. | |
a87e8be2 | 214 | */ |
4dbd1352 | 215 | |
3e04c62d | 216 | if (!export_all_trees && access("git-daemon-export-ok", F_OK)) { |
4dbd1352 | 217 | logerror("'%s': repository not exported.", path); |
3e04c62d PA |
218 | errno = EACCES; |
219 | return -1; | |
220 | } | |
221 | ||
02d57da4 LT |
222 | /* |
223 | * We'll ignore SIGTERM from now on, we have a | |
224 | * good client. | |
225 | */ | |
226 | signal(SIGTERM, SIG_IGN); | |
227 | ||
960deccb PA |
228 | snprintf(timeout_buf, sizeof timeout_buf, "--timeout=%u", timeout); |
229 | ||
a87e8be2 | 230 | /* git-upload-pack only ever reads stuff, so this is safe */ |
77cb17e9 | 231 | execl_git_cmd("upload-pack", "--strict", timeout_buf, ".", NULL); |
a87e8be2 LT |
232 | return -1; |
233 | } | |
234 | ||
7d80694a | 235 | static int execute(void) |
a87e8be2 | 236 | { |
7d80694a LT |
237 | static char line[1000]; |
238 | int len; | |
239 | ||
960deccb | 240 | alarm(init_timeout ? init_timeout : timeout); |
7d80694a | 241 | len = packet_read_line(0, line, sizeof(line)); |
960deccb | 242 | alarm(0); |
7d80694a LT |
243 | |
244 | if (len && line[len-1] == '\n') | |
245 | line[--len] = 0; | |
246 | ||
4dbd1352 | 247 | if (!strncmp("git-upload-pack ", line, 16)) |
3e04c62d | 248 | return upload(line+16); |
a87e8be2 | 249 | |
f8ff0c06 | 250 | logerror("Protocol error: '%s'", line); |
a87e8be2 LT |
251 | return -1; |
252 | } | |
253 | ||
66e631de LT |
254 | |
255 | /* | |
256 | * We count spawned/reaped separately, just to avoid any | |
257 | * races when updating them from signals. The SIGCHLD handler | |
258 | * will only update children_reaped, and the fork logic will | |
259 | * only update children_spawned. | |
260 | * | |
261 | * MAX_CHILDREN should be a power-of-two to make the modulus | |
262 | * operation cheap. It should also be at least twice | |
263 | * the maximum number of connections we will ever allow. | |
264 | */ | |
265 | #define MAX_CHILDREN 128 | |
266 | ||
267 | static int max_connections = 25; | |
268 | ||
269 | /* These are updated by the signal handler */ | |
270 | static volatile unsigned int children_reaped = 0; | |
4d8fa916 | 271 | static pid_t dead_child[MAX_CHILDREN]; |
66e631de LT |
272 | |
273 | /* These are updated by the main loop */ | |
274 | static unsigned int children_spawned = 0; | |
275 | static unsigned int children_deleted = 0; | |
276 | ||
4d8fa916 | 277 | static struct child { |
66e631de | 278 | pid_t pid; |
7fa09084 | 279 | int addrlen; |
df076bdb | 280 | struct sockaddr_storage address; |
66e631de LT |
281 | } live_child[MAX_CHILDREN]; |
282 | ||
7fa09084 | 283 | static void add_child(int idx, pid_t pid, struct sockaddr *addr, int addrlen) |
66e631de LT |
284 | { |
285 | live_child[idx].pid = pid; | |
286 | live_child[idx].addrlen = addrlen; | |
df076bdb | 287 | memcpy(&live_child[idx].address, addr, addrlen); |
66e631de LT |
288 | } |
289 | ||
290 | /* | |
291 | * Walk from "deleted" to "spawned", and remove child "pid". | |
292 | * | |
293 | * We move everything up by one, since the new "deleted" will | |
294 | * be one higher. | |
295 | */ | |
296 | static void remove_child(pid_t pid, unsigned deleted, unsigned spawned) | |
297 | { | |
298 | struct child n; | |
299 | ||
300 | deleted %= MAX_CHILDREN; | |
301 | spawned %= MAX_CHILDREN; | |
302 | if (live_child[deleted].pid == pid) { | |
303 | live_child[deleted].pid = -1; | |
304 | return; | |
305 | } | |
306 | n = live_child[deleted]; | |
307 | for (;;) { | |
308 | struct child m; | |
309 | deleted = (deleted + 1) % MAX_CHILDREN; | |
310 | if (deleted == spawned) | |
311 | die("could not find dead child %d\n", pid); | |
312 | m = live_child[deleted]; | |
313 | live_child[deleted] = n; | |
314 | if (m.pid == pid) | |
315 | return; | |
316 | n = m; | |
317 | } | |
318 | } | |
319 | ||
320 | /* | |
321 | * This gets called if the number of connections grows | |
322 | * past "max_connections". | |
323 | * | |
324 | * We _should_ start off by searching for connections | |
325 | * from the same IP, and if there is some address wth | |
326 | * multiple connections, we should kill that first. | |
327 | * | |
328 | * As it is, we just "randomly" kill 25% of the connections, | |
329 | * and our pseudo-random generator sucks too. I have no | |
330 | * shame. | |
331 | * | |
332 | * Really, this is just a place-holder for a _real_ algorithm. | |
333 | */ | |
02d57da4 | 334 | static void kill_some_children(int signo, unsigned start, unsigned stop) |
66e631de LT |
335 | { |
336 | start %= MAX_CHILDREN; | |
337 | stop %= MAX_CHILDREN; | |
338 | while (start != stop) { | |
339 | if (!(start & 3)) | |
02d57da4 | 340 | kill(live_child[start].pid, signo); |
66e631de LT |
341 | start = (start + 1) % MAX_CHILDREN; |
342 | } | |
343 | } | |
344 | ||
02d57da4 | 345 | static void check_max_connections(void) |
a87e8be2 | 346 | { |
02d57da4 | 347 | for (;;) { |
eaa94919 | 348 | int active; |
66e631de | 349 | unsigned spawned, reaped, deleted; |
eaa94919 | 350 | |
66e631de | 351 | spawned = children_spawned; |
66e631de LT |
352 | reaped = children_reaped; |
353 | deleted = children_deleted; | |
354 | ||
355 | while (deleted < reaped) { | |
356 | pid_t pid = dead_child[deleted % MAX_CHILDREN]; | |
357 | remove_child(pid, deleted, spawned); | |
358 | deleted++; | |
359 | } | |
360 | children_deleted = deleted; | |
361 | ||
362 | active = spawned - deleted; | |
02d57da4 LT |
363 | if (active <= max_connections) |
364 | break; | |
66e631de | 365 | |
02d57da4 LT |
366 | /* Kill some unstarted connections with SIGTERM */ |
367 | kill_some_children(SIGTERM, deleted, spawned); | |
368 | if (active <= max_connections << 1) | |
369 | break; | |
370 | ||
371 | /* If the SIGTERM thing isn't helping use SIGKILL */ | |
372 | kill_some_children(SIGKILL, deleted, spawned); | |
373 | sleep(1); | |
374 | } | |
375 | } | |
376 | ||
7fa09084 | 377 | static void handle(int incoming, struct sockaddr *addr, int addrlen) |
02d57da4 LT |
378 | { |
379 | pid_t pid = fork(); | |
f8ff0c06 PB |
380 | char addrbuf[256] = ""; |
381 | int port = -1; | |
02d57da4 LT |
382 | |
383 | if (pid) { | |
384 | unsigned idx; | |
385 | ||
386 | close(incoming); | |
387 | if (pid < 0) | |
388 | return; | |
389 | ||
390 | idx = children_spawned % MAX_CHILDREN; | |
391 | children_spawned++; | |
392 | add_child(idx, pid, addr, addrlen); | |
66e631de | 393 | |
02d57da4 | 394 | check_max_connections(); |
a87e8be2 LT |
395 | return; |
396 | } | |
397 | ||
398 | dup2(incoming, 0); | |
399 | dup2(incoming, 1); | |
400 | close(incoming); | |
f8ff0c06 PB |
401 | |
402 | if (addr->sa_family == AF_INET) { | |
403 | struct sockaddr_in *sin_addr = (void *) addr; | |
404 | inet_ntop(AF_INET, &sin_addr->sin_addr, addrbuf, sizeof(addrbuf)); | |
405 | port = sin_addr->sin_port; | |
406 | ||
6573faff | 407 | #ifndef NO_IPV6 |
f8ff0c06 PB |
408 | } else if (addr->sa_family == AF_INET6) { |
409 | struct sockaddr_in6 *sin6_addr = (void *) addr; | |
410 | ||
411 | char *buf = addrbuf; | |
412 | *buf++ = '['; *buf = '\0'; /* stpcpy() is cool */ | |
413 | inet_ntop(AF_INET6, &sin6_addr->sin6_addr, buf, sizeof(addrbuf) - 1); | |
414 | strcat(buf, "]"); | |
415 | ||
416 | port = sin6_addr->sin6_port; | |
6573faff | 417 | #endif |
f8ff0c06 | 418 | } |
da38641d | 419 | loginfo("Connection from %s:%d", addrbuf, port); |
f8ff0c06 | 420 | |
7d80694a | 421 | exit(execute()); |
a87e8be2 LT |
422 | } |
423 | ||
eaa94919 LT |
424 | static void child_handler(int signo) |
425 | { | |
426 | for (;;) { | |
9048fe1c PB |
427 | int status; |
428 | pid_t pid = waitpid(-1, &status, WNOHANG); | |
66e631de LT |
429 | |
430 | if (pid > 0) { | |
431 | unsigned reaped = children_reaped; | |
432 | dead_child[reaped % MAX_CHILDREN] = pid; | |
433 | children_reaped = reaped + 1; | |
f8ff0c06 | 434 | /* XXX: Custom logging, since we don't wanna getpid() */ |
9048fe1c PB |
435 | if (verbose) { |
436 | char *dead = ""; | |
437 | if (!WIFEXITED(status) || WEXITSTATUS(status) > 0) | |
438 | dead = " (with error)"; | |
439 | if (log_syslog) | |
440 | syslog(LOG_INFO, "[%d] Disconnected%s", pid, dead); | |
441 | else | |
442 | fprintf(stderr, "[%d] Disconnected%s\n", pid, dead); | |
443 | } | |
eaa94919 LT |
444 | continue; |
445 | } | |
446 | break; | |
447 | } | |
448 | } | |
449 | ||
6573faff PA |
450 | #ifndef NO_IPV6 |
451 | ||
452 | static int socksetup(int port, int **socklist_p) | |
a87e8be2 | 453 | { |
df076bdb YH |
454 | int socknum = 0, *socklist = NULL; |
455 | int maxfd = -1; | |
df076bdb | 456 | char pbuf[NI_MAXSERV]; |
a87e8be2 | 457 | |
6573faff PA |
458 | struct addrinfo hints, *ai0, *ai; |
459 | int gai; | |
df076bdb YH |
460 | |
461 | sprintf(pbuf, "%d", port); | |
462 | memset(&hints, 0, sizeof(hints)); | |
463 | hints.ai_family = AF_UNSPEC; | |
464 | hints.ai_socktype = SOCK_STREAM; | |
465 | hints.ai_protocol = IPPROTO_TCP; | |
466 | hints.ai_flags = AI_PASSIVE; | |
467 | ||
468 | gai = getaddrinfo(NULL, pbuf, &hints, &ai0); | |
469 | if (gai) | |
470 | die("getaddrinfo() failed: %s\n", gai_strerror(gai)); | |
471 | ||
df076bdb YH |
472 | for (ai = ai0; ai; ai = ai->ai_next) { |
473 | int sockfd; | |
474 | int *newlist; | |
475 | ||
476 | sockfd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); | |
477 | if (sockfd < 0) | |
478 | continue; | |
479 | if (sockfd >= FD_SETSIZE) { | |
480 | error("too large socket descriptor."); | |
481 | close(sockfd); | |
482 | continue; | |
483 | } | |
484 | ||
485 | #ifdef IPV6_V6ONLY | |
486 | if (ai->ai_family == AF_INET6) { | |
487 | int on = 1; | |
488 | setsockopt(sockfd, IPPROTO_IPV6, IPV6_V6ONLY, | |
489 | &on, sizeof(on)); | |
490 | /* Note: error is not fatal */ | |
491 | } | |
492 | #endif | |
493 | ||
494 | if (bind(sockfd, ai->ai_addr, ai->ai_addrlen) < 0) { | |
495 | close(sockfd); | |
496 | continue; /* not fatal */ | |
497 | } | |
498 | if (listen(sockfd, 5) < 0) { | |
499 | close(sockfd); | |
500 | continue; /* not fatal */ | |
501 | } | |
502 | ||
503 | newlist = realloc(socklist, sizeof(int) * (socknum + 1)); | |
504 | if (!newlist) | |
505 | die("memory allocation failed: %s", strerror(errno)); | |
506 | ||
507 | socklist = newlist; | |
508 | socklist[socknum++] = sockfd; | |
509 | ||
df076bdb YH |
510 | if (maxfd < sockfd) |
511 | maxfd = sockfd; | |
512 | } | |
513 | ||
514 | freeaddrinfo(ai0); | |
515 | ||
6573faff PA |
516 | *socklist_p = socklist; |
517 | return socknum; | |
518 | } | |
519 | ||
520 | #else /* NO_IPV6 */ | |
521 | ||
522 | static int socksetup(int port, int **socklist_p) | |
523 | { | |
524 | struct sockaddr_in sin; | |
525 | int sockfd; | |
526 | ||
527 | sockfd = socket(AF_INET, SOCK_STREAM, 0); | |
528 | if (sockfd < 0) | |
529 | return 0; | |
530 | ||
531 | memset(&sin, 0, sizeof sin); | |
532 | sin.sin_family = AF_INET; | |
533 | sin.sin_addr.s_addr = htonl(INADDR_ANY); | |
534 | sin.sin_port = htons(port); | |
535 | ||
536 | if ( bind(sockfd, (struct sockaddr *)&sin, sizeof sin) < 0 ) { | |
537 | close(sockfd); | |
538 | return 0; | |
539 | } | |
a87e8be2 | 540 | |
f35230fb PS |
541 | if (listen(sockfd, 5) < 0) { |
542 | close(sockfd); | |
543 | return 0; | |
544 | } | |
545 | ||
1b4713fb | 546 | *socklist_p = xmalloc(sizeof(int)); |
6573faff | 547 | **socklist_p = sockfd; |
f35230fb | 548 | return 1; |
6573faff PA |
549 | } |
550 | ||
551 | #endif | |
552 | ||
553 | static int service_loop(int socknum, int *socklist) | |
554 | { | |
555 | struct pollfd *pfd; | |
556 | int i; | |
557 | ||
1b4713fb | 558 | pfd = xcalloc(socknum, sizeof(struct pollfd)); |
6573faff PA |
559 | |
560 | for (i = 0; i < socknum; i++) { | |
561 | pfd[i].fd = socklist[i]; | |
562 | pfd[i].events = POLLIN; | |
563 | } | |
9220282a PA |
564 | |
565 | signal(SIGCHLD, child_handler); | |
a87e8be2 LT |
566 | |
567 | for (;;) { | |
df076bdb | 568 | int i; |
6573faff | 569 | |
7872e055 | 570 | if (poll(pfd, socknum, -1) < 0) { |
1eef0b33 | 571 | if (errno != EINTR) { |
6573faff | 572 | error("poll failed, resuming: %s", |
1eef0b33 JH |
573 | strerror(errno)); |
574 | sleep(1); | |
575 | } | |
df076bdb YH |
576 | continue; |
577 | } | |
578 | ||
579 | for (i = 0; i < socknum; i++) { | |
6573faff | 580 | if (pfd[i].revents & POLLIN) { |
df076bdb | 581 | struct sockaddr_storage ss; |
7626e49e | 582 | unsigned int sslen = sizeof(ss); |
6573faff | 583 | int incoming = accept(pfd[i].fd, (struct sockaddr *)&ss, &sslen); |
df076bdb YH |
584 | if (incoming < 0) { |
585 | switch (errno) { | |
586 | case EAGAIN: | |
587 | case EINTR: | |
588 | case ECONNABORTED: | |
589 | continue; | |
590 | default: | |
591 | die("accept returned %s", strerror(errno)); | |
592 | } | |
593 | } | |
594 | handle(incoming, (struct sockaddr *)&ss, sslen); | |
a87e8be2 LT |
595 | } |
596 | } | |
a87e8be2 LT |
597 | } |
598 | } | |
599 | ||
6573faff PA |
600 | static int serve(int port) |
601 | { | |
602 | int socknum, *socklist; | |
4ae22d96 | 603 | |
6573faff PA |
604 | socknum = socksetup(port, &socklist); |
605 | if (socknum == 0) | |
606 | die("unable to allocate any listen sockets on port %u", port); | |
4ae22d96 | 607 | |
6573faff | 608 | return service_loop(socknum, socklist); |
4ae22d96 | 609 | } |
6573faff | 610 | |
a87e8be2 LT |
611 | int main(int argc, char **argv) |
612 | { | |
613 | int port = DEFAULT_GIT_PORT; | |
e64e1b79 | 614 | int inetd_mode = 0; |
a87e8be2 LT |
615 | int i; |
616 | ||
617 | for (i = 1; i < argc; i++) { | |
618 | char *arg = argv[i]; | |
619 | ||
620 | if (!strncmp(arg, "--port=", 7)) { | |
621 | char *end; | |
622 | unsigned long n; | |
623 | n = strtoul(arg+7, &end, 0); | |
624 | if (arg[7] && !*end) { | |
625 | port = n; | |
626 | continue; | |
627 | } | |
628 | } | |
e64e1b79 LT |
629 | if (!strcmp(arg, "--inetd")) { |
630 | inetd_mode = 1; | |
a8883288 | 631 | log_syslog = 1; |
e64e1b79 LT |
632 | continue; |
633 | } | |
f8ff0c06 PB |
634 | if (!strcmp(arg, "--verbose")) { |
635 | verbose = 1; | |
636 | continue; | |
637 | } | |
9048fe1c PB |
638 | if (!strcmp(arg, "--syslog")) { |
639 | log_syslog = 1; | |
9048fe1c PB |
640 | continue; |
641 | } | |
4ae95682 PA |
642 | if (!strcmp(arg, "--export-all")) { |
643 | export_all_trees = 1; | |
644 | continue; | |
645 | } | |
960deccb PA |
646 | if (!strncmp(arg, "--timeout=", 10)) { |
647 | timeout = atoi(arg+10); | |
a8883288 | 648 | continue; |
960deccb | 649 | } |
54e31a20 | 650 | if (!strncmp(arg, "--init-timeout=", 15)) { |
960deccb | 651 | init_timeout = atoi(arg+15); |
a8883288 | 652 | continue; |
960deccb | 653 | } |
4dbd1352 AE |
654 | if (!strcmp(arg, "--strict-paths")) { |
655 | strict_paths = 1; | |
656 | continue; | |
657 | } | |
b21c31c9 PB |
658 | if (!strncmp(arg, "--base-path=", 12)) { |
659 | base_path = arg+12; | |
660 | continue; | |
661 | } | |
4ae95682 PA |
662 | if (!strcmp(arg, "--")) { |
663 | ok_paths = &argv[i+1]; | |
664 | break; | |
665 | } else if (arg[0] != '-') { | |
666 | ok_paths = &argv[i]; | |
667 | break; | |
668 | } | |
e64e1b79 | 669 | |
a87e8be2 LT |
670 | usage(daemon_usage); |
671 | } | |
672 | ||
a8883288 AE |
673 | if (log_syslog) |
674 | openlog("git-daemon", 0, LOG_DAEMON); | |
675 | ||
4dbd1352 AE |
676 | if (strict_paths && (!ok_paths || !*ok_paths)) { |
677 | if (!inetd_mode) | |
678 | die("git-daemon: option --strict-paths requires a whitelist"); | |
679 | ||
680 | logerror("option --strict-paths requires a whitelist"); | |
681 | exit (1); | |
682 | } | |
683 | ||
7c3693f1 LD |
684 | if (inetd_mode) { |
685 | fclose(stderr); //FIXME: workaround | |
e64e1b79 | 686 | return execute(); |
7c3693f1 | 687 | } |
bce8230d AE |
688 | |
689 | return serve(port); | |
a87e8be2 | 690 | } |