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