Skip to content

Commit c2bd43d

Browse files
committed
Merge branch 'lw/daemon-log-destination'
The log from "git daemon" can be redirected with a new option; one relevant use case is to send the log to standard error (instead of syslog) when running it from inetd. * lw/daemon-log-destination: daemon: add --log-destination=(stderr|syslog|none)
2 parents e469e9c + 0c591ca commit c2bd43d

File tree

2 files changed

+64
-10
lines changed

2 files changed

+64
-10
lines changed

Documentation/git-daemon.txt

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ SYNOPSIS
2020
[--inetd |
2121
[--listen=<host_or_ipaddr>] [--port=<n>]
2222
[--user=<user> [--group=<group>]]]
23+
[--log-destination=(stderr|syslog|none)]
2324
[<directory>...]
2425

2526
DESCRIPTION
@@ -80,7 +81,8 @@ OPTIONS
8081
do not have the 'git-daemon-export-ok' file.
8182

8283
--inetd::
83-
Have the server run as an inetd service. Implies --syslog.
84+
Have the server run as an inetd service. Implies --syslog (may be
85+
overridden with `--log-destination=`).
8486
Incompatible with --detach, --port, --listen, --user and --group
8587
options.
8688

@@ -110,8 +112,28 @@ OPTIONS
110112
zero for no limit.
111113

112114
--syslog::
113-
Log to syslog instead of stderr. Note that this option does not imply
114-
--verbose, thus by default only error conditions will be logged.
115+
Short for `--log-destination=syslog`.
116+
117+
--log-destination=<destination>::
118+
Send log messages to the specified destination.
119+
Note that this option does not imply --verbose,
120+
thus by default only error conditions will be logged.
121+
The <destination> must be one of:
122+
+
123+
--
124+
stderr::
125+
Write to standard error.
126+
Note that if `--detach` is specified,
127+
the process disconnects from the real standard error,
128+
making this destination effectively equivalent to `none`.
129+
syslog::
130+
Write to syslog, using the `git-daemon` identifier.
131+
none::
132+
Disable all logging.
133+
--
134+
+
135+
The default destination is `syslog` if `--inetd` or `--detach` is specified,
136+
otherwise `stderr`.
115137

116138
--user-path::
117139
--user-path=<path>::

daemon.c

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@
99
#define initgroups(x, y) (0) /* nothing */
1010
#endif
1111

12-
static int log_syslog;
12+
static enum log_destination {
13+
LOG_DESTINATION_UNSET = -1,
14+
LOG_DESTINATION_NONE = 0,
15+
LOG_DESTINATION_STDERR = 1,
16+
LOG_DESTINATION_SYSLOG = 2,
17+
} log_destination = LOG_DESTINATION_UNSET;
1318
static int verbose;
1419
static int reuseaddr;
1520
static int informative_errors;
@@ -25,6 +30,7 @@ static const char daemon_usage[] =
2530
" [--access-hook=<path>]\n"
2631
" [--inetd | [--listen=<host_or_ipaddr>] [--port=<n>]\n"
2732
" [--detach] [--user=<user> [--group=<group>]]\n"
33+
" [--log-destination=(stderr|syslog|none)]\n"
2834
" [<directory>...]";
2935

3036
/* List of acceptable pathname prefixes */
@@ -74,11 +80,14 @@ static const char *get_ip_address(struct hostinfo *hi)
7480

7581
static void logreport(int priority, const char *err, va_list params)
7682
{
77-
if (log_syslog) {
83+
switch (log_destination) {
84+
case LOG_DESTINATION_SYSLOG: {
7885
char buf[1024];
7986
vsnprintf(buf, sizeof(buf), err, params);
8087
syslog(priority, "%s", buf);
81-
} else {
88+
break;
89+
}
90+
case LOG_DESTINATION_STDERR:
8291
/*
8392
* Since stderr is set to buffered mode, the
8493
* logging of different processes will not overlap
@@ -88,6 +97,11 @@ static void logreport(int priority, const char *err, va_list params)
8897
vfprintf(stderr, err, params);
8998
fputc('\n', stderr);
9099
fflush(stderr);
100+
break;
101+
case LOG_DESTINATION_NONE:
102+
break;
103+
case LOG_DESTINATION_UNSET:
104+
BUG("log destination not initialized correctly");
91105
}
92106
}
93107

@@ -1286,17 +1300,29 @@ int cmd_main(int argc, const char **argv)
12861300
}
12871301
if (!strcmp(arg, "--inetd")) {
12881302
inetd_mode = 1;
1289-
log_syslog = 1;
12901303
continue;
12911304
}
12921305
if (!strcmp(arg, "--verbose")) {
12931306
verbose = 1;
12941307
continue;
12951308
}
12961309
if (!strcmp(arg, "--syslog")) {
1297-
log_syslog = 1;
1310+
log_destination = LOG_DESTINATION_SYSLOG;
12981311
continue;
12991312
}
1313+
if (skip_prefix(arg, "--log-destination=", &v)) {
1314+
if (!strcmp(v, "syslog")) {
1315+
log_destination = LOG_DESTINATION_SYSLOG;
1316+
continue;
1317+
} else if (!strcmp(v, "stderr")) {
1318+
log_destination = LOG_DESTINATION_STDERR;
1319+
continue;
1320+
} else if (!strcmp(v, "none")) {
1321+
log_destination = LOG_DESTINATION_NONE;
1322+
continue;
1323+
} else
1324+
die("unknown log destination '%s'", v);
1325+
}
13001326
if (!strcmp(arg, "--export-all")) {
13011327
export_all_trees = 1;
13021328
continue;
@@ -1353,7 +1379,6 @@ int cmd_main(int argc, const char **argv)
13531379
}
13541380
if (!strcmp(arg, "--detach")) {
13551381
detach = 1;
1356-
log_syslog = 1;
13571382
continue;
13581383
}
13591384
if (skip_prefix(arg, "--user=", &v)) {
@@ -1399,7 +1424,14 @@ int cmd_main(int argc, const char **argv)
13991424
usage(daemon_usage);
14001425
}
14011426

1402-
if (log_syslog) {
1427+
if (log_destination == LOG_DESTINATION_UNSET) {
1428+
if (inetd_mode || detach)
1429+
log_destination = LOG_DESTINATION_SYSLOG;
1430+
else
1431+
log_destination = LOG_DESTINATION_STDERR;
1432+
}
1433+
1434+
if (log_destination == LOG_DESTINATION_SYSLOG) {
14031435
openlog("git-daemon", LOG_PID, LOG_DAEMON);
14041436
set_die_routine(daemon_die);
14051437
} else

0 commit comments

Comments
 (0)