git

Форк
0
/
daemon.c 
1465 строк · 34.6 Кб
1
#define USE_THE_REPOSITORY_VARIABLE
2

3
#include "git-compat-util.h"
4
#include "abspath.h"
5
#include "config.h"
6
#include "environment.h"
7
#include "path.h"
8
#include "pkt-line.h"
9
#include "protocol.h"
10
#include "run-command.h"
11
#include "setup.h"
12
#include "strbuf.h"
13
#include "string-list.h"
14

15
#ifdef NO_INITGROUPS
16
#define initgroups(x, y) (0) /* nothing */
17
#endif
18

19
static enum log_destination {
20
	LOG_DESTINATION_UNSET = -1,
21
	LOG_DESTINATION_NONE = 0,
22
	LOG_DESTINATION_STDERR = 1,
23
	LOG_DESTINATION_SYSLOG = 2,
24
} log_destination = LOG_DESTINATION_UNSET;
25
static int verbose;
26
static int reuseaddr;
27
static int informative_errors;
28

29
static const char daemon_usage[] =
30
"git daemon [--verbose] [--syslog] [--export-all]\n"
31
"           [--timeout=<n>] [--init-timeout=<n>] [--max-connections=<n>]\n"
32
"           [--strict-paths] [--base-path=<path>] [--base-path-relaxed]\n"
33
"           [--user-path | --user-path=<path>]\n"
34
"           [--interpolated-path=<path>]\n"
35
"           [--reuseaddr] [--pid-file=<file>]\n"
36
"           [--(enable|disable|allow-override|forbid-override)=<service>]\n"
37
"           [--access-hook=<path>]\n"
38
"           [--inetd | [--listen=<host_or_ipaddr>] [--port=<n>]\n"
39
"                      [--detach] [--user=<user> [--group=<group>]]\n"
40
"           [--log-destination=(stderr|syslog|none)]\n"
41
"           [<directory>...]";
42

43
/* List of acceptable pathname prefixes */
44
static const char **ok_paths;
45
static int strict_paths;
46

47
/* If this is set, git-daemon-export-ok is not required */
48
static int export_all_trees;
49

50
/* Take all paths relative to this one if non-NULL */
51
static const char *base_path;
52
static const char *interpolated_path;
53
static int base_path_relaxed;
54

55
/* If defined, ~user notation is allowed and the string is inserted
56
 * after ~user/.  E.g. a request to git://host/~alice/frotz would
57
 * go to /home/alice/pub_git/frotz with --user-path=pub_git.
58
 */
59
static const char *user_path;
60

61
/* Timeout, and initial timeout */
62
static unsigned int timeout;
63
static unsigned int init_timeout;
64

65
struct hostinfo {
66
	struct strbuf hostname;
67
	struct strbuf canon_hostname;
68
	struct strbuf ip_address;
69
	struct strbuf tcp_port;
70
	unsigned int hostname_lookup_done:1;
71
	unsigned int saw_extended_args:1;
72
};
73
#define HOSTINFO_INIT { \
74
	.hostname = STRBUF_INIT, \
75
	.canon_hostname = STRBUF_INIT, \
76
	.ip_address = STRBUF_INIT, \
77
	.tcp_port = STRBUF_INIT, \
78
}
79

80
static void lookup_hostname(struct hostinfo *hi);
81

82
static const char *get_canon_hostname(struct hostinfo *hi)
83
{
84
	lookup_hostname(hi);
85
	return hi->canon_hostname.buf;
86
}
87

88
static const char *get_ip_address(struct hostinfo *hi)
89
{
90
	lookup_hostname(hi);
91
	return hi->ip_address.buf;
92
}
93

94
static void logreport(int priority, const char *err, va_list params)
95
{
96
	switch (log_destination) {
97
	case LOG_DESTINATION_SYSLOG: {
98
		char buf[1024];
99
		vsnprintf(buf, sizeof(buf), err, params);
100
		syslog(priority, "%s", buf);
101
		break;
102
	}
103
	case LOG_DESTINATION_STDERR:
104
		/*
105
		 * Since stderr is set to buffered mode, the
106
		 * logging of different processes will not overlap
107
		 * unless they overflow the (rather big) buffers.
108
		 */
109
		fprintf(stderr, "[%"PRIuMAX"] ", (uintmax_t)getpid());
110
		vfprintf(stderr, err, params);
111
		fputc('\n', stderr);
112
		fflush(stderr);
113
		break;
114
	case LOG_DESTINATION_NONE:
115
		break;
116
	case LOG_DESTINATION_UNSET:
117
		BUG("log destination not initialized correctly");
118
	}
119
}
120

121
__attribute__((format (printf, 1, 2)))
122
static void logerror(const char *err, ...)
123
{
124
	va_list params;
125
	va_start(params, err);
126
	logreport(LOG_ERR, err, params);
127
	va_end(params);
128
}
129

130
__attribute__((format (printf, 1, 2)))
131
static void loginfo(const char *err, ...)
132
{
133
	va_list params;
134
	if (!verbose)
135
		return;
136
	va_start(params, err);
137
	logreport(LOG_INFO, err, params);
138
	va_end(params);
139
}
140

141
static void NORETURN daemon_die(const char *err, va_list params)
142
{
143
	logreport(LOG_ERR, err, params);
144
	exit(1);
145
}
146

147
static const char *path_ok(const char *directory, struct hostinfo *hi)
148
{
149
	static char rpath[PATH_MAX];
150
	static char interp_path[PATH_MAX];
151
	size_t rlen;
152
	const char *path;
153
	const char *dir;
154

155
	dir = directory;
156

157
	if (daemon_avoid_alias(dir)) {
158
		logerror("'%s': aliased", dir);
159
		return NULL;
160
	}
161

162
	if (*dir == '~') {
163
		if (!user_path) {
164
			logerror("'%s': User-path not allowed", dir);
165
			return NULL;
166
		}
167
		if (*user_path) {
168
			/* Got either "~alice" or "~alice/foo";
169
			 * rewrite them to "~alice/%s" or
170
			 * "~alice/%s/foo".
171
			 */
172
			int namlen, restlen = strlen(dir);
173
			const char *slash = strchr(dir, '/');
174
			if (!slash)
175
				slash = dir + restlen;
176
			namlen = slash - dir;
177
			restlen -= namlen;
178
			loginfo("userpath <%s>, request <%s>, namlen %d, restlen %d, slash <%s>", user_path, dir, namlen, restlen, slash);
179
			rlen = snprintf(rpath, sizeof(rpath), "%.*s/%s%.*s",
180
					namlen, dir, user_path, restlen, slash);
181
			if (rlen >= sizeof(rpath)) {
182
				logerror("user-path too large: %s", rpath);
183
				return NULL;
184
			}
185
			dir = rpath;
186
		}
187
	}
188
	else if (interpolated_path && hi->saw_extended_args) {
189
		struct strbuf expanded_path = STRBUF_INIT;
190
		const char *format = interpolated_path;
191

192
		if (*dir != '/') {
193
			/* Allow only absolute */
194
			logerror("'%s': Non-absolute path denied (interpolated-path active)", dir);
195
			return NULL;
196
		}
197

198
		while (strbuf_expand_step(&expanded_path, &format)) {
199
			if (skip_prefix(format, "%", &format))
200
				strbuf_addch(&expanded_path, '%');
201
			else if (skip_prefix(format, "H", &format))
202
				strbuf_addbuf(&expanded_path, &hi->hostname);
203
			else if (skip_prefix(format, "CH", &format))
204
				strbuf_addstr(&expanded_path,
205
					      get_canon_hostname(hi));
206
			else if (skip_prefix(format, "IP", &format))
207
				strbuf_addstr(&expanded_path,
208
					      get_ip_address(hi));
209
			else if (skip_prefix(format, "P", &format))
210
				strbuf_addbuf(&expanded_path, &hi->tcp_port);
211
			else if (skip_prefix(format, "D", &format))
212
				strbuf_addstr(&expanded_path, directory);
213
			else
214
				strbuf_addch(&expanded_path, '%');
215
		}
216

217
		rlen = strlcpy(interp_path, expanded_path.buf,
218
			       sizeof(interp_path));
219
		strbuf_release(&expanded_path);
220
		if (rlen >= sizeof(interp_path)) {
221
			logerror("interpolated path too large: %s",
222
				 interp_path);
223
			return NULL;
224
		}
225

226
		loginfo("Interpolated dir '%s'", interp_path);
227

228
		dir = interp_path;
229
	}
230
	else if (base_path) {
231
		if (*dir != '/') {
232
			/* Allow only absolute */
233
			logerror("'%s': Non-absolute path denied (base-path active)", dir);
234
			return NULL;
235
		}
236
		rlen = snprintf(rpath, sizeof(rpath), "%s%s", base_path, dir);
237
		if (rlen >= sizeof(rpath)) {
238
			logerror("base-path too large: %s", rpath);
239
			return NULL;
240
		}
241
		dir = rpath;
242
	}
243

244
	path = enter_repo(dir, strict_paths);
245
	if (!path && base_path && base_path_relaxed) {
246
		/*
247
		 * if we fail and base_path_relaxed is enabled, try without
248
		 * prefixing the base path
249
		 */
250
		dir = directory;
251
		path = enter_repo(dir, strict_paths);
252
	}
253

254
	if (!path) {
255
		logerror("'%s' does not appear to be a git repository", dir);
256
		return NULL;
257
	}
258

259
	if ( ok_paths && *ok_paths ) {
260
		const char **pp;
261
		int pathlen = strlen(path);
262

263
		/* The validation is done on the paths after enter_repo
264
		 * appends optional {.git,.git/.git} and friends, but
265
		 * it does not use getcwd().  So if your /pub is
266
		 * a symlink to /mnt/pub, you can include /pub and
267
		 * do not have to say /mnt/pub.
268
		 * Do not say /pub/.
269
		 */
270
		for ( pp = ok_paths ; *pp ; pp++ ) {
271
			int len = strlen(*pp);
272
			if (len <= pathlen &&
273
			    !memcmp(*pp, path, len) &&
274
			    (path[len] == '\0' ||
275
			     (!strict_paths && path[len] == '/')))
276
				return path;
277
		}
278
	}
279
	else {
280
		/* be backwards compatible */
281
		if (!strict_paths)
282
			return path;
283
	}
284

285
	logerror("'%s': not in directory list", path);
286
	return NULL;		/* Fallthrough. Deny by default */
287
}
288

289
typedef int (*daemon_service_fn)(const struct strvec *env);
290
struct daemon_service {
291
	const char *name;
292
	const char *config_name;
293
	daemon_service_fn fn;
294
	int enabled;
295
	int overridable;
296
};
297

298
static int daemon_error(const char *dir, const char *msg)
299
{
300
	if (!informative_errors)
301
		msg = "access denied or repository not exported";
302
	packet_write_fmt(1, "ERR %s: %s", msg, dir);
303
	return -1;
304
}
305

306
static const char *access_hook;
307

308
static int run_access_hook(struct daemon_service *service, const char *dir,
309
			   const char *path, struct hostinfo *hi)
310
{
311
	struct child_process child = CHILD_PROCESS_INIT;
312
	struct strbuf buf = STRBUF_INIT;
313
	char *eol;
314
	int seen_errors = 0;
315

316
	strvec_push(&child.args, access_hook);
317
	strvec_push(&child.args, service->name);
318
	strvec_push(&child.args, path);
319
	strvec_push(&child.args, hi->hostname.buf);
320
	strvec_push(&child.args, get_canon_hostname(hi));
321
	strvec_push(&child.args, get_ip_address(hi));
322
	strvec_push(&child.args, hi->tcp_port.buf);
323

324
	child.use_shell = 1;
325
	child.no_stdin = 1;
326
	child.no_stderr = 1;
327
	child.out = -1;
328
	if (start_command(&child)) {
329
		logerror("daemon access hook '%s' failed to start",
330
			 access_hook);
331
		goto error_return;
332
	}
333
	if (strbuf_read(&buf, child.out, 0) < 0) {
334
		logerror("failed to read from pipe to daemon access hook '%s'",
335
			 access_hook);
336
		strbuf_reset(&buf);
337
		seen_errors = 1;
338
	}
339
	if (close(child.out) < 0) {
340
		logerror("failed to close pipe to daemon access hook '%s'",
341
			 access_hook);
342
		seen_errors = 1;
343
	}
344
	if (finish_command(&child))
345
		seen_errors = 1;
346

347
	if (!seen_errors) {
348
		strbuf_release(&buf);
349
		return 0;
350
	}
351

352
error_return:
353
	strbuf_ltrim(&buf);
354
	if (!buf.len)
355
		strbuf_addstr(&buf, "service rejected");
356
	eol = strchr(buf.buf, '\n');
357
	if (eol)
358
		*eol = '\0';
359
	errno = EACCES;
360
	daemon_error(dir, buf.buf);
361
	strbuf_release(&buf);
362
	return -1;
363
}
364

365
static int run_service(const char *dir, struct daemon_service *service,
366
		       struct hostinfo *hi, const struct strvec *env)
367
{
368
	const char *path;
369
	int enabled = service->enabled;
370
	struct strbuf var = STRBUF_INIT;
371

372
	loginfo("Request %s for '%s'", service->name, dir);
373

374
	if (!enabled && !service->overridable) {
375
		logerror("'%s': service not enabled.", service->name);
376
		errno = EACCES;
377
		return daemon_error(dir, "service not enabled");
378
	}
379

380
	if (!(path = path_ok(dir, hi)))
381
		return daemon_error(dir, "no such repository");
382

383
	/*
384
	 * Security on the cheap.
385
	 *
386
	 * We want a readable HEAD, usable "objects" directory, and
387
	 * a "git-daemon-export-ok" flag that says that the other side
388
	 * is ok with us doing this.
389
	 *
390
	 * path_ok() uses enter_repo() and checks for included directories.
391
	 * We only need to make sure the repository is exported.
392
	 */
393

394
	if (!export_all_trees && access("git-daemon-export-ok", F_OK)) {
395
		logerror("'%s': repository not exported.", path);
396
		errno = EACCES;
397
		return daemon_error(dir, "repository not exported");
398
	}
399

400
	if (service->overridable) {
401
		strbuf_addf(&var, "daemon.%s", service->config_name);
402
		git_config_get_bool(var.buf, &enabled);
403
		strbuf_release(&var);
404
	}
405
	if (!enabled) {
406
		logerror("'%s': service not enabled for '%s'",
407
			 service->name, path);
408
		errno = EACCES;
409
		return daemon_error(dir, "service not enabled");
410
	}
411

412
	/*
413
	 * Optionally, a hook can choose to deny access to the
414
	 * repository depending on the phase of the moon.
415
	 */
416
	if (access_hook && run_access_hook(service, dir, path, hi))
417
		return -1;
418

419
	/*
420
	 * We'll ignore SIGTERM from now on, we have a
421
	 * good client.
422
	 */
423
	signal(SIGTERM, SIG_IGN);
424

425
	return service->fn(env);
426
}
427

428
static void copy_to_log(int fd)
429
{
430
	struct strbuf line = STRBUF_INIT;
431
	FILE *fp;
432

433
	fp = fdopen(fd, "r");
434
	if (!fp) {
435
		logerror("fdopen of error channel failed");
436
		close(fd);
437
		return;
438
	}
439

440
	while (strbuf_getline_lf(&line, fp) != EOF) {
441
		logerror("%s", line.buf);
442
		strbuf_setlen(&line, 0);
443
	}
444

445
	strbuf_release(&line);
446
	fclose(fp);
447
}
448

449
static int run_service_command(struct child_process *cld)
450
{
451
	strvec_push(&cld->args, ".");
452
	cld->git_cmd = 1;
453
	cld->err = -1;
454
	if (start_command(cld))
455
		return -1;
456

457
	close(0);
458
	close(1);
459

460
	copy_to_log(cld->err);
461

462
	return finish_command(cld);
463
}
464

465
static int upload_pack(const struct strvec *env)
466
{
467
	struct child_process cld = CHILD_PROCESS_INIT;
468
	strvec_pushl(&cld.args, "upload-pack", "--strict", NULL);
469
	strvec_pushf(&cld.args, "--timeout=%u", timeout);
470

471
	strvec_pushv(&cld.env, env->v);
472

473
	return run_service_command(&cld);
474
}
475

476
static int upload_archive(const struct strvec *env)
477
{
478
	struct child_process cld = CHILD_PROCESS_INIT;
479
	strvec_push(&cld.args, "upload-archive");
480

481
	strvec_pushv(&cld.env, env->v);
482

483
	return run_service_command(&cld);
484
}
485

486
static int receive_pack(const struct strvec *env)
487
{
488
	struct child_process cld = CHILD_PROCESS_INIT;
489
	strvec_push(&cld.args, "receive-pack");
490

491
	strvec_pushv(&cld.env, env->v);
492

493
	return run_service_command(&cld);
494
}
495

496
static struct daemon_service daemon_service[] = {
497
	{ "upload-archive", "uploadarch", upload_archive, 0, 1 },
498
	{ "upload-pack", "uploadpack", upload_pack, 1, 1 },
499
	{ "receive-pack", "receivepack", receive_pack, 0, 1 },
500
};
501

502
static void enable_service(const char *name, int ena)
503
{
504
	int i;
505
	for (i = 0; i < ARRAY_SIZE(daemon_service); i++) {
506
		if (!strcmp(daemon_service[i].name, name)) {
507
			daemon_service[i].enabled = ena;
508
			return;
509
		}
510
	}
511
	die("No such service %s", name);
512
}
513

514
static void make_service_overridable(const char *name, int ena)
515
{
516
	int i;
517
	for (i = 0; i < ARRAY_SIZE(daemon_service); i++) {
518
		if (!strcmp(daemon_service[i].name, name)) {
519
			daemon_service[i].overridable = ena;
520
			return;
521
		}
522
	}
523
	die("No such service %s", name);
524
}
525

526
static void parse_host_and_port(char *hostport, char **host,
527
	char **port)
528
{
529
	if (*hostport == '[') {
530
		char *end;
531

532
		end = strchr(hostport, ']');
533
		if (!end)
534
			die("Invalid request ('[' without ']')");
535
		*end = '\0';
536
		*host = hostport + 1;
537
		if (!end[1])
538
			*port = NULL;
539
		else if (end[1] == ':')
540
			*port = end + 2;
541
		else
542
			die("Garbage after end of host part");
543
	} else {
544
		*host = hostport;
545
		*port = strrchr(hostport, ':');
546
		if (*port) {
547
			**port = '\0';
548
			++*port;
549
		}
550
	}
551
}
552

553
/*
554
 * Sanitize a string from the client so that it's OK to be inserted into a
555
 * filesystem path. Specifically, we disallow directory separators, runs
556
 * of "..", and trailing and leading dots, which means that the client
557
 * cannot escape our base path via ".." traversal.
558
 */
559
static void sanitize_client(struct strbuf *out, const char *in)
560
{
561
	for (; *in; in++) {
562
		if (is_dir_sep(*in))
563
			continue;
564
		if (*in == '.' && (!out->len || out->buf[out->len - 1] == '.'))
565
			continue;
566
		strbuf_addch(out, *in);
567
	}
568

569
	while (out->len && out->buf[out->len - 1] == '.')
570
		strbuf_setlen(out, out->len - 1);
571
}
572

573
/*
574
 * Like sanitize_client, but we also perform any canonicalization
575
 * to make life easier on the admin.
576
 */
577
static void canonicalize_client(struct strbuf *out, const char *in)
578
{
579
	sanitize_client(out, in);
580
	strbuf_tolower(out);
581
}
582

583
/*
584
 * Read the host as supplied by the client connection.
585
 *
586
 * Returns a pointer to the character after the NUL byte terminating the host
587
 * argument, or 'extra_args' if there is no host argument.
588
 */
589
static char *parse_host_arg(struct hostinfo *hi, char *extra_args, int buflen)
590
{
591
	char *val;
592
	int vallen;
593
	char *end = extra_args + buflen;
594

595
	if (extra_args < end && *extra_args) {
596
		hi->saw_extended_args = 1;
597
		if (strncasecmp("host=", extra_args, 5) == 0) {
598
			val = extra_args + 5;
599
			vallen = strlen(val) + 1;
600
			loginfo("Extended attribute \"host\": %s", val);
601
			if (*val) {
602
				/* Split <host>:<port> at colon. */
603
				char *host;
604
				char *port;
605
				parse_host_and_port(val, &host, &port);
606
				if (port)
607
					sanitize_client(&hi->tcp_port, port);
608
				canonicalize_client(&hi->hostname, host);
609
				hi->hostname_lookup_done = 0;
610
			}
611

612
			/* On to the next one */
613
			extra_args = val + vallen;
614
		}
615
		if (extra_args < end && *extra_args)
616
			die("Invalid request");
617
	}
618

619
	return extra_args;
620
}
621

622
static void parse_extra_args(struct hostinfo *hi, struct strvec *env,
623
			     char *extra_args, int buflen)
624
{
625
	const char *end = extra_args + buflen;
626
	struct strbuf git_protocol = STRBUF_INIT;
627

628
	/* First look for the host argument */
629
	extra_args = parse_host_arg(hi, extra_args, buflen);
630

631
	/* Look for additional arguments places after a second NUL byte */
632
	for (; extra_args < end; extra_args += strlen(extra_args) + 1) {
633
		const char *arg = extra_args;
634

635
		/*
636
		 * Parse the extra arguments, adding most to 'git_protocol'
637
		 * which will be used to set the 'GIT_PROTOCOL' envvar in the
638
		 * service that will be run.
639
		 *
640
		 * If there ends up being a particular arg in the future that
641
		 * git-daemon needs to parse specifically (like the 'host' arg)
642
		 * then it can be parsed here and not added to 'git_protocol'.
643
		 */
644
		if (*arg) {
645
			if (git_protocol.len > 0)
646
				strbuf_addch(&git_protocol, ':');
647
			strbuf_addstr(&git_protocol, arg);
648
		}
649
	}
650

651
	if (git_protocol.len > 0) {
652
		loginfo("Extended attribute \"protocol\": %s", git_protocol.buf);
653
		strvec_pushf(env, GIT_PROTOCOL_ENVIRONMENT "=%s",
654
			     git_protocol.buf);
655
	}
656
	strbuf_release(&git_protocol);
657
}
658

659
/*
660
 * Locate canonical hostname and its IP address.
661
 */
662
static void lookup_hostname(struct hostinfo *hi)
663
{
664
	if (!hi->hostname_lookup_done && hi->hostname.len) {
665
#ifndef NO_IPV6
666
		struct addrinfo hints;
667
		struct addrinfo *ai;
668
		int gai;
669
		static char addrbuf[HOST_NAME_MAX + 1];
670

671
		memset(&hints, 0, sizeof(hints));
672
		hints.ai_flags = AI_CANONNAME;
673

674
		gai = getaddrinfo(hi->hostname.buf, NULL, &hints, &ai);
675
		if (!gai) {
676
			struct sockaddr_in *sin_addr = (void *)ai->ai_addr;
677

678
			inet_ntop(AF_INET, &sin_addr->sin_addr,
679
				  addrbuf, sizeof(addrbuf));
680
			strbuf_addstr(&hi->ip_address, addrbuf);
681

682
			if (ai->ai_canonname)
683
				sanitize_client(&hi->canon_hostname,
684
						ai->ai_canonname);
685
			else
686
				strbuf_addbuf(&hi->canon_hostname,
687
					      &hi->ip_address);
688

689
			freeaddrinfo(ai);
690
		}
691
#else
692
		struct hostent *hent;
693
		struct sockaddr_in sa;
694
		char **ap;
695
		static char addrbuf[HOST_NAME_MAX + 1];
696

697
		hent = gethostbyname(hi->hostname.buf);
698
		if (hent) {
699
			ap = hent->h_addr_list;
700
			memset(&sa, 0, sizeof sa);
701
			sa.sin_family = hent->h_addrtype;
702
			sa.sin_port = htons(0);
703
			memcpy(&sa.sin_addr, *ap, hent->h_length);
704

705
			inet_ntop(hent->h_addrtype, &sa.sin_addr,
706
				  addrbuf, sizeof(addrbuf));
707

708
			sanitize_client(&hi->canon_hostname, hent->h_name);
709
			strbuf_addstr(&hi->ip_address, addrbuf);
710
		}
711
#endif
712
		hi->hostname_lookup_done = 1;
713
	}
714
}
715

716
static void hostinfo_clear(struct hostinfo *hi)
717
{
718
	strbuf_release(&hi->hostname);
719
	strbuf_release(&hi->canon_hostname);
720
	strbuf_release(&hi->ip_address);
721
	strbuf_release(&hi->tcp_port);
722
}
723

724
static void set_keep_alive(int sockfd)
725
{
726
	int ka = 1;
727

728
	if (setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, &ka, sizeof(ka)) < 0) {
729
		if (errno != ENOTSOCK)
730
			logerror("unable to set SO_KEEPALIVE on socket: %s",
731
				strerror(errno));
732
	}
733
}
734

735
static int execute(void)
736
{
737
	char *line = packet_buffer;
738
	int pktlen, len, i;
739
	char *addr = getenv("REMOTE_ADDR"), *port = getenv("REMOTE_PORT");
740
	struct hostinfo hi = HOSTINFO_INIT;
741
	struct strvec env = STRVEC_INIT;
742

743
	if (addr)
744
		loginfo("Connection from %s:%s", addr, port);
745

746
	set_keep_alive(0);
747
	alarm(init_timeout ? init_timeout : timeout);
748
	pktlen = packet_read(0, packet_buffer, sizeof(packet_buffer), 0);
749
	alarm(0);
750

751
	len = strlen(line);
752
	if (len && line[len-1] == '\n')
753
		line[len-1] = 0;
754

755
	/* parse additional args hidden behind a NUL byte */
756
	if (len != pktlen)
757
		parse_extra_args(&hi, &env, line + len + 1, pktlen - len - 1);
758

759
	for (i = 0; i < ARRAY_SIZE(daemon_service); i++) {
760
		struct daemon_service *s = &(daemon_service[i]);
761
		const char *arg;
762

763
		if (skip_prefix(line, "git-", &arg) &&
764
		    skip_prefix(arg, s->name, &arg) &&
765
		    *arg++ == ' ') {
766
			/*
767
			 * Note: The directory here is probably context sensitive,
768
			 * and might depend on the actual service being performed.
769
			 */
770
			int rc = run_service(arg, s, &hi, &env);
771
			hostinfo_clear(&hi);
772
			strvec_clear(&env);
773
			return rc;
774
		}
775
	}
776

777
	hostinfo_clear(&hi);
778
	strvec_clear(&env);
779
	logerror("Protocol error: '%s'", line);
780
	return -1;
781
}
782

783
static int addrcmp(const struct sockaddr_storage *s1,
784
    const struct sockaddr_storage *s2)
785
{
786
	const struct sockaddr *sa1 = (const struct sockaddr*) s1;
787
	const struct sockaddr *sa2 = (const struct sockaddr*) s2;
788

789
	if (sa1->sa_family != sa2->sa_family)
790
		return sa1->sa_family - sa2->sa_family;
791
	if (sa1->sa_family == AF_INET)
792
		return memcmp(&((struct sockaddr_in *)s1)->sin_addr,
793
		    &((struct sockaddr_in *)s2)->sin_addr,
794
		    sizeof(struct in_addr));
795
#ifndef NO_IPV6
796
	if (sa1->sa_family == AF_INET6)
797
		return memcmp(&((struct sockaddr_in6 *)s1)->sin6_addr,
798
		    &((struct sockaddr_in6 *)s2)->sin6_addr,
799
		    sizeof(struct in6_addr));
800
#endif
801
	return 0;
802
}
803

804
static int max_connections = 32;
805

806
static unsigned int live_children;
807

808
static struct child {
809
	struct child *next;
810
	struct child_process cld;
811
	struct sockaddr_storage address;
812
} *firstborn;
813

814
static void add_child(struct child_process *cld, struct sockaddr *addr, socklen_t addrlen)
815
{
816
	struct child *newborn, **cradle;
817

818
	CALLOC_ARRAY(newborn, 1);
819
	live_children++;
820
	memcpy(&newborn->cld, cld, sizeof(*cld));
821
	memcpy(&newborn->address, addr, addrlen);
822
	for (cradle = &firstborn; *cradle; cradle = &(*cradle)->next)
823
		if (!addrcmp(&(*cradle)->address, &newborn->address))
824
			break;
825
	newborn->next = *cradle;
826
	*cradle = newborn;
827
}
828

829
/*
830
 * This gets called if the number of connections grows
831
 * past "max_connections".
832
 *
833
 * We kill the newest connection from a duplicate IP.
834
 */
835
static void kill_some_child(void)
836
{
837
	const struct child *blanket, *next;
838

839
	if (!(blanket = firstborn))
840
		return;
841

842
	for (; (next = blanket->next); blanket = next)
843
		if (!addrcmp(&blanket->address, &next->address)) {
844
			kill(blanket->cld.pid, SIGTERM);
845
			break;
846
		}
847
}
848

849
static void check_dead_children(void)
850
{
851
	int status;
852
	pid_t pid;
853

854
	struct child **cradle, *blanket;
855
	for (cradle = &firstborn; (blanket = *cradle);)
856
		if ((pid = waitpid(blanket->cld.pid, &status, WNOHANG)) > 1) {
857
			const char *dead = "";
858
			if (status)
859
				dead = " (with error)";
860
			loginfo("[%"PRIuMAX"] Disconnected%s", (uintmax_t)pid, dead);
861

862
			/* remove the child */
863
			*cradle = blanket->next;
864
			live_children--;
865
			child_process_clear(&blanket->cld);
866
			free(blanket);
867
		} else
868
			cradle = &blanket->next;
869
}
870

871
static struct strvec cld_argv = STRVEC_INIT;
872
static void handle(int incoming, struct sockaddr *addr, socklen_t addrlen)
873
{
874
	struct child_process cld = CHILD_PROCESS_INIT;
875

876
	if (max_connections && live_children >= max_connections) {
877
		kill_some_child();
878
		sleep(1);  /* give it some time to die */
879
		check_dead_children();
880
		if (live_children >= max_connections) {
881
			close(incoming);
882
			logerror("Too many children, dropping connection");
883
			return;
884
		}
885
	}
886

887
	if (addr->sa_family == AF_INET) {
888
		char buf[128] = "";
889
		struct sockaddr_in *sin_addr = (void *) addr;
890
		inet_ntop(addr->sa_family, &sin_addr->sin_addr, buf, sizeof(buf));
891
		strvec_pushf(&cld.env, "REMOTE_ADDR=%s", buf);
892
		strvec_pushf(&cld.env, "REMOTE_PORT=%d",
893
			     ntohs(sin_addr->sin_port));
894
#ifndef NO_IPV6
895
	} else if (addr->sa_family == AF_INET6) {
896
		char buf[128] = "";
897
		struct sockaddr_in6 *sin6_addr = (void *) addr;
898
		inet_ntop(AF_INET6, &sin6_addr->sin6_addr, buf, sizeof(buf));
899
		strvec_pushf(&cld.env, "REMOTE_ADDR=[%s]", buf);
900
		strvec_pushf(&cld.env, "REMOTE_PORT=%d",
901
			     ntohs(sin6_addr->sin6_port));
902
#endif
903
	}
904

905
	strvec_pushv(&cld.args, cld_argv.v);
906
	cld.in = incoming;
907
	cld.out = dup(incoming);
908

909
	if (start_command(&cld))
910
		logerror("unable to fork");
911
	else
912
		add_child(&cld, addr, addrlen);
913
}
914

915
static void child_handler(int signo UNUSED)
916
{
917
	/*
918
	 * Otherwise empty handler because systemcalls will get interrupted
919
	 * upon signal receipt
920
	 * SysV needs the handler to be rearmed
921
	 */
922
	signal(SIGCHLD, child_handler);
923
}
924

925
static int set_reuse_addr(int sockfd)
926
{
927
	int on = 1;
928

929
	if (!reuseaddr)
930
		return 0;
931
	return setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,
932
			  &on, sizeof(on));
933
}
934

935
struct socketlist {
936
	int *list;
937
	size_t nr;
938
	size_t alloc;
939
};
940

941
static const char *ip2str(int family, struct sockaddr *sin, socklen_t len)
942
{
943
#ifdef NO_IPV6
944
	static char ip[INET_ADDRSTRLEN];
945
#else
946
	static char ip[INET6_ADDRSTRLEN];
947
#endif
948

949
	switch (family) {
950
#ifndef NO_IPV6
951
	case AF_INET6:
952
		inet_ntop(family, &((struct sockaddr_in6*)sin)->sin6_addr, ip, len);
953
		break;
954
#endif
955
	case AF_INET:
956
		inet_ntop(family, &((struct sockaddr_in*)sin)->sin_addr, ip, len);
957
		break;
958
	default:
959
		xsnprintf(ip, sizeof(ip), "<unknown>");
960
	}
961
	return ip;
962
}
963

964
#ifndef NO_IPV6
965

966
static int setup_named_sock(char *listen_addr, int listen_port, struct socketlist *socklist)
967
{
968
	int socknum = 0;
969
	char pbuf[NI_MAXSERV];
970
	struct addrinfo hints, *ai0, *ai;
971
	int gai;
972
	long flags;
973

974
	xsnprintf(pbuf, sizeof(pbuf), "%d", listen_port);
975
	memset(&hints, 0, sizeof(hints));
976
	hints.ai_family = AF_UNSPEC;
977
	hints.ai_socktype = SOCK_STREAM;
978
	hints.ai_protocol = IPPROTO_TCP;
979
	hints.ai_flags = AI_PASSIVE;
980

981
	gai = getaddrinfo(listen_addr, pbuf, &hints, &ai0);
982
	if (gai) {
983
		logerror("getaddrinfo() for %s failed: %s", listen_addr, gai_strerror(gai));
984
		return 0;
985
	}
986

987
	for (ai = ai0; ai; ai = ai->ai_next) {
988
		int sockfd;
989

990
		sockfd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
991
		if (sockfd < 0)
992
			continue;
993
		if (sockfd >= FD_SETSIZE) {
994
			logerror("Socket descriptor too large");
995
			close(sockfd);
996
			continue;
997
		}
998

999
#ifdef IPV6_V6ONLY
1000
		if (ai->ai_family == AF_INET6) {
1001
			int on = 1;
1002
			setsockopt(sockfd, IPPROTO_IPV6, IPV6_V6ONLY,
1003
				   &on, sizeof(on));
1004
			/* Note: error is not fatal */
1005
		}
1006
#endif
1007

1008
		if (set_reuse_addr(sockfd)) {
1009
			logerror("Could not set SO_REUSEADDR: %s", strerror(errno));
1010
			close(sockfd);
1011
			continue;
1012
		}
1013

1014
		set_keep_alive(sockfd);
1015

1016
		if (bind(sockfd, ai->ai_addr, ai->ai_addrlen) < 0) {
1017
			logerror("Could not bind to %s: %s",
1018
				 ip2str(ai->ai_family, ai->ai_addr, ai->ai_addrlen),
1019
				 strerror(errno));
1020
			close(sockfd);
1021
			continue;	/* not fatal */
1022
		}
1023
		if (listen(sockfd, 5) < 0) {
1024
			logerror("Could not listen to %s: %s",
1025
				 ip2str(ai->ai_family, ai->ai_addr, ai->ai_addrlen),
1026
				 strerror(errno));
1027
			close(sockfd);
1028
			continue;	/* not fatal */
1029
		}
1030

1031
		flags = fcntl(sockfd, F_GETFD, 0);
1032
		if (flags >= 0)
1033
			fcntl(sockfd, F_SETFD, flags | FD_CLOEXEC);
1034

1035
		ALLOC_GROW(socklist->list, socklist->nr + 1, socklist->alloc);
1036
		socklist->list[socklist->nr++] = sockfd;
1037
		socknum++;
1038
	}
1039

1040
	freeaddrinfo(ai0);
1041

1042
	return socknum;
1043
}
1044

1045
#else /* NO_IPV6 */
1046

1047
static int setup_named_sock(char *listen_addr, int listen_port, struct socketlist *socklist)
1048
{
1049
	struct sockaddr_in sin;
1050
	int sockfd;
1051
	long flags;
1052

1053
	memset(&sin, 0, sizeof sin);
1054
	sin.sin_family = AF_INET;
1055
	sin.sin_port = htons(listen_port);
1056

1057
	if (listen_addr) {
1058
		/* Well, host better be an IP address here. */
1059
		if (inet_pton(AF_INET, listen_addr, &sin.sin_addr.s_addr) <= 0)
1060
			return 0;
1061
	} else {
1062
		sin.sin_addr.s_addr = htonl(INADDR_ANY);
1063
	}
1064

1065
	sockfd = socket(AF_INET, SOCK_STREAM, 0);
1066
	if (sockfd < 0)
1067
		return 0;
1068

1069
	if (set_reuse_addr(sockfd)) {
1070
		logerror("Could not set SO_REUSEADDR: %s", strerror(errno));
1071
		close(sockfd);
1072
		return 0;
1073
	}
1074

1075
	set_keep_alive(sockfd);
1076

1077
	if ( bind(sockfd, (struct sockaddr *)&sin, sizeof sin) < 0 ) {
1078
		logerror("Could not bind to %s: %s",
1079
			 ip2str(AF_INET, (struct sockaddr *)&sin, sizeof(sin)),
1080
			 strerror(errno));
1081
		close(sockfd);
1082
		return 0;
1083
	}
1084

1085
	if (listen(sockfd, 5) < 0) {
1086
		logerror("Could not listen to %s: %s",
1087
			 ip2str(AF_INET, (struct sockaddr *)&sin, sizeof(sin)),
1088
			 strerror(errno));
1089
		close(sockfd);
1090
		return 0;
1091
	}
1092

1093
	flags = fcntl(sockfd, F_GETFD, 0);
1094
	if (flags >= 0)
1095
		fcntl(sockfd, F_SETFD, flags | FD_CLOEXEC);
1096

1097
	ALLOC_GROW(socklist->list, socklist->nr + 1, socklist->alloc);
1098
	socklist->list[socklist->nr++] = sockfd;
1099
	return 1;
1100
}
1101

1102
#endif
1103

1104
static void socksetup(struct string_list *listen_addr, int listen_port, struct socketlist *socklist)
1105
{
1106
	if (!listen_addr->nr)
1107
		setup_named_sock(NULL, listen_port, socklist);
1108
	else {
1109
		int i, socknum;
1110
		for (i = 0; i < listen_addr->nr; i++) {
1111
			socknum = setup_named_sock(listen_addr->items[i].string,
1112
						   listen_port, socklist);
1113

1114
			if (socknum == 0)
1115
				logerror("unable to allocate any listen sockets for host %s on port %u",
1116
					 listen_addr->items[i].string, listen_port);
1117
		}
1118
	}
1119
}
1120

1121
static int service_loop(struct socketlist *socklist)
1122
{
1123
	struct pollfd *pfd;
1124
	int i;
1125

1126
	CALLOC_ARRAY(pfd, socklist->nr);
1127

1128
	for (i = 0; i < socklist->nr; i++) {
1129
		pfd[i].fd = socklist->list[i];
1130
		pfd[i].events = POLLIN;
1131
	}
1132

1133
	signal(SIGCHLD, child_handler);
1134

1135
	for (;;) {
1136
		int i;
1137

1138
		check_dead_children();
1139

1140
		if (poll(pfd, socklist->nr, -1) < 0) {
1141
			if (errno != EINTR) {
1142
				logerror("Poll failed, resuming: %s",
1143
				      strerror(errno));
1144
				sleep(1);
1145
			}
1146
			continue;
1147
		}
1148

1149
		for (i = 0; i < socklist->nr; i++) {
1150
			if (pfd[i].revents & POLLIN) {
1151
				union {
1152
					struct sockaddr sa;
1153
					struct sockaddr_in sai;
1154
#ifndef NO_IPV6
1155
					struct sockaddr_in6 sai6;
1156
#endif
1157
				} ss;
1158
				socklen_t sslen = sizeof(ss);
1159
				int incoming = accept(pfd[i].fd, &ss.sa, &sslen);
1160
				if (incoming < 0) {
1161
					switch (errno) {
1162
					case EAGAIN:
1163
					case EINTR:
1164
					case ECONNABORTED:
1165
						continue;
1166
					default:
1167
						die_errno("accept returned");
1168
					}
1169
				}
1170
				handle(incoming, &ss.sa, sslen);
1171
			}
1172
		}
1173
	}
1174
}
1175

1176
#ifdef NO_POSIX_GOODIES
1177

1178
struct credentials;
1179

1180
static void drop_privileges(struct credentials *cred UNUSED)
1181
{
1182
	/* nothing */
1183
}
1184

1185
static struct credentials *prepare_credentials(const char *user_name UNUSED,
1186
					       const char *group_name UNUSED)
1187
{
1188
	die("--user not supported on this platform");
1189
}
1190

1191
#else
1192

1193
struct credentials {
1194
	struct passwd *pass;
1195
	gid_t gid;
1196
};
1197

1198
static void drop_privileges(struct credentials *cred)
1199
{
1200
	if (cred && (initgroups(cred->pass->pw_name, cred->gid) ||
1201
	    setgid (cred->gid) || setuid(cred->pass->pw_uid)))
1202
		die("cannot drop privileges");
1203
}
1204

1205
static struct credentials *prepare_credentials(const char *user_name,
1206
    const char *group_name)
1207
{
1208
	static struct credentials c;
1209

1210
	c.pass = getpwnam(user_name);
1211
	if (!c.pass)
1212
		die("user not found - %s", user_name);
1213

1214
	if (!group_name)
1215
		c.gid = c.pass->pw_gid;
1216
	else {
1217
		struct group *group = getgrnam(group_name);
1218
		if (!group)
1219
			die("group not found - %s", group_name);
1220

1221
		c.gid = group->gr_gid;
1222
	}
1223

1224
	return &c;
1225
}
1226
#endif
1227

1228
static int serve(struct string_list *listen_addr, int listen_port,
1229
    struct credentials *cred)
1230
{
1231
	struct socketlist socklist = { NULL, 0, 0 };
1232

1233
	socksetup(listen_addr, listen_port, &socklist);
1234
	if (socklist.nr == 0)
1235
		die("unable to allocate any listen sockets on port %u",
1236
		    listen_port);
1237

1238
	drop_privileges(cred);
1239

1240
	loginfo("Ready to rumble");
1241

1242
	return service_loop(&socklist);
1243
}
1244

1245
int cmd_main(int argc, const char **argv)
1246
{
1247
	int listen_port = 0;
1248
	struct string_list listen_addr = STRING_LIST_INIT_DUP;
1249
	int serve_mode = 0, inetd_mode = 0;
1250
	const char *pid_file = NULL, *user_name = NULL, *group_name = NULL;
1251
	int detach = 0;
1252
	struct credentials *cred = NULL;
1253
	int i;
1254
	int ret;
1255

1256
	for (i = 1; i < argc; i++) {
1257
		const char *arg = argv[i];
1258
		const char *v;
1259

1260
		if (skip_prefix(arg, "--listen=", &v)) {
1261
			string_list_append_nodup(&listen_addr, xstrdup_tolower(v));
1262
			continue;
1263
		}
1264
		if (skip_prefix(arg, "--port=", &v)) {
1265
			char *end;
1266
			unsigned long n;
1267
			n = strtoul(v, &end, 0);
1268
			if (*v && !*end) {
1269
				listen_port = n;
1270
				continue;
1271
			}
1272
		}
1273
		if (!strcmp(arg, "--serve")) {
1274
			serve_mode = 1;
1275
			continue;
1276
		}
1277
		if (!strcmp(arg, "--inetd")) {
1278
			inetd_mode = 1;
1279
			continue;
1280
		}
1281
		if (!strcmp(arg, "--verbose")) {
1282
			verbose = 1;
1283
			continue;
1284
		}
1285
		if (!strcmp(arg, "--syslog")) {
1286
			log_destination = LOG_DESTINATION_SYSLOG;
1287
			continue;
1288
		}
1289
		if (skip_prefix(arg, "--log-destination=", &v)) {
1290
			if (!strcmp(v, "syslog")) {
1291
				log_destination = LOG_DESTINATION_SYSLOG;
1292
				continue;
1293
			} else if (!strcmp(v, "stderr")) {
1294
				log_destination = LOG_DESTINATION_STDERR;
1295
				continue;
1296
			} else if (!strcmp(v, "none")) {
1297
				log_destination = LOG_DESTINATION_NONE;
1298
				continue;
1299
			} else
1300
				die("unknown log destination '%s'", v);
1301
		}
1302
		if (!strcmp(arg, "--export-all")) {
1303
			export_all_trees = 1;
1304
			continue;
1305
		}
1306
		if (skip_prefix(arg, "--access-hook=", &v)) {
1307
			access_hook = v;
1308
			continue;
1309
		}
1310
		if (skip_prefix(arg, "--timeout=", &v)) {
1311
			timeout = atoi(v);
1312
			continue;
1313
		}
1314
		if (skip_prefix(arg, "--init-timeout=", &v)) {
1315
			init_timeout = atoi(v);
1316
			continue;
1317
		}
1318
		if (skip_prefix(arg, "--max-connections=", &v)) {
1319
			max_connections = atoi(v);
1320
			if (max_connections < 0)
1321
				max_connections = 0;	        /* unlimited */
1322
			continue;
1323
		}
1324
		if (!strcmp(arg, "--strict-paths")) {
1325
			strict_paths = 1;
1326
			continue;
1327
		}
1328
		if (skip_prefix(arg, "--base-path=", &v)) {
1329
			base_path = v;
1330
			continue;
1331
		}
1332
		if (!strcmp(arg, "--base-path-relaxed")) {
1333
			base_path_relaxed = 1;
1334
			continue;
1335
		}
1336
		if (skip_prefix(arg, "--interpolated-path=", &v)) {
1337
			interpolated_path = v;
1338
			continue;
1339
		}
1340
		if (!strcmp(arg, "--reuseaddr")) {
1341
			reuseaddr = 1;
1342
			continue;
1343
		}
1344
		if (!strcmp(arg, "--user-path")) {
1345
			user_path = "";
1346
			continue;
1347
		}
1348
		if (skip_prefix(arg, "--user-path=", &v)) {
1349
			user_path = v;
1350
			continue;
1351
		}
1352
		if (skip_prefix(arg, "--pid-file=", &v)) {
1353
			pid_file = v;
1354
			continue;
1355
		}
1356
		if (!strcmp(arg, "--detach")) {
1357
			detach = 1;
1358
			continue;
1359
		}
1360
		if (skip_prefix(arg, "--user=", &v)) {
1361
			user_name = v;
1362
			continue;
1363
		}
1364
		if (skip_prefix(arg, "--group=", &v)) {
1365
			group_name = v;
1366
			continue;
1367
		}
1368
		if (skip_prefix(arg, "--enable=", &v)) {
1369
			enable_service(v, 1);
1370
			continue;
1371
		}
1372
		if (skip_prefix(arg, "--disable=", &v)) {
1373
			enable_service(v, 0);
1374
			continue;
1375
		}
1376
		if (skip_prefix(arg, "--allow-override=", &v)) {
1377
			make_service_overridable(v, 1);
1378
			continue;
1379
		}
1380
		if (skip_prefix(arg, "--forbid-override=", &v)) {
1381
			make_service_overridable(v, 0);
1382
			continue;
1383
		}
1384
		if (!strcmp(arg, "--informative-errors")) {
1385
			informative_errors = 1;
1386
			continue;
1387
		}
1388
		if (!strcmp(arg, "--no-informative-errors")) {
1389
			informative_errors = 0;
1390
			continue;
1391
		}
1392
		if (!strcmp(arg, "--")) {
1393
			ok_paths = &argv[i+1];
1394
			break;
1395
		} else if (arg[0] != '-') {
1396
			ok_paths = &argv[i];
1397
			break;
1398
		}
1399

1400
		usage(daemon_usage);
1401
	}
1402

1403
	if (log_destination == LOG_DESTINATION_UNSET) {
1404
		if (inetd_mode || detach)
1405
			log_destination = LOG_DESTINATION_SYSLOG;
1406
		else
1407
			log_destination = LOG_DESTINATION_STDERR;
1408
	}
1409

1410
	if (log_destination == LOG_DESTINATION_SYSLOG) {
1411
		openlog("git-daemon", LOG_PID, LOG_DAEMON);
1412
		set_die_routine(daemon_die);
1413
	} else
1414
		/* avoid splitting a message in the middle */
1415
		setvbuf(stderr, NULL, _IOFBF, 4096);
1416

1417
	if (inetd_mode && (detach || group_name || user_name))
1418
		die("--detach, --user and --group are incompatible with --inetd");
1419

1420
	if (inetd_mode && (listen_port || (listen_addr.nr > 0)))
1421
		die("--listen= and --port= are incompatible with --inetd");
1422
	else if (listen_port == 0)
1423
		listen_port = DEFAULT_GIT_PORT;
1424

1425
	if (group_name && !user_name)
1426
		die("--group supplied without --user");
1427

1428
	if (user_name)
1429
		cred = prepare_credentials(user_name, group_name);
1430

1431
	if (strict_paths && (!ok_paths || !*ok_paths))
1432
		die("option --strict-paths requires '<directory>' arguments");
1433

1434
	if (base_path && !is_directory(base_path))
1435
		die("base-path '%s' does not exist or is not a directory",
1436
		    base_path);
1437

1438
	if (log_destination != LOG_DESTINATION_STDERR) {
1439
		if (!freopen("/dev/null", "w", stderr))
1440
			die_errno("failed to redirect stderr to /dev/null");
1441
	}
1442

1443
	if (inetd_mode || serve_mode) {
1444
		ret = execute();
1445
	} else {
1446
		if (detach) {
1447
			if (daemonize())
1448
				die("--detach not supported on this platform");
1449
		}
1450

1451
		if (pid_file)
1452
			write_file(pid_file, "%"PRIuMAX, (uintmax_t) getpid());
1453

1454
		/* prepare argv for serving-processes */
1455
		strvec_push(&cld_argv, argv[0]); /* git-daemon */
1456
		strvec_push(&cld_argv, "--serve");
1457
		for (i = 1; i < argc; ++i)
1458
			strvec_push(&cld_argv, argv[i]);
1459

1460
		ret = serve(&listen_addr, listen_port, cred);
1461
	}
1462

1463
	string_list_clear(&listen_addr, 0);
1464
	return ret;
1465
}
1466

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.