Skip to content

Commit 0c591ca

Browse files
lucaswerkmeistergitster
authored andcommitted
daemon: add --log-destination=(stderr|syslog|none)
This new option can be used to override the implicit --syslog of --inetd, or to disable all logging. (While --detach also implies --syslog, --log-destination=stderr with --detach is useless since --detach disassociates the process from the original stderr.) --syslog is retained as an alias for --log-destination=syslog. --log-destination always overrides implicit --syslog regardless of option order. This is different than the “last one wins” logic that applies to some implicit options elsewhere in Git, but should hopefully be less confusing. (I also don’t know if *all* implicit options in Git follow “last one wins”.) The combination of --inetd with --log-destination=stderr is useful, for instance, when running `git daemon` as an instanced systemd service (with associated socket unit). In this case, log messages sent via syslog are received by the journal daemon, but run the risk of being processed at a time when the `git daemon` process has already exited (especially if the process was very short-lived, e.g. due to client error), so that the journal daemon can no longer read its cgroup and attach the message to the correct systemd unit (see systemd/systemd#2913 [1]). Logging to stderr instead can solve this problem, because systemd can connect stderr directly to the journal daemon, which then already knows which unit is associated with this stream. [1]: systemd/systemd#2913 Helped-by: Ævar Arnfjörð Bjarmason <[email protected]> Helped-by: Junio C Hamano <[email protected]> Helped-by: Eric Sunshine <[email protected]> Signed-off-by: Lucas Werkmeister <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 8279ed0 commit 0c591ca

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

@@ -1289,17 +1303,29 @@ int cmd_main(int argc, const char **argv)
12891303
}
12901304
if (!strcmp(arg, "--inetd")) {
12911305
inetd_mode = 1;
1292-
log_syslog = 1;
12931306
continue;
12941307
}
12951308
if (!strcmp(arg, "--verbose")) {
12961309
verbose = 1;
12971310
continue;
12981311
}
12991312
if (!strcmp(arg, "--syslog")) {
1300-
log_syslog = 1;
1313+
log_destination = LOG_DESTINATION_SYSLOG;
13011314
continue;
13021315
}
1316+
if (skip_prefix(arg, "--log-destination=", &v)) {
1317+
if (!strcmp(v, "syslog")) {
1318+
log_destination = LOG_DESTINATION_SYSLOG;
1319+
continue;
1320+
} else if (!strcmp(v, "stderr")) {
1321+
log_destination = LOG_DESTINATION_STDERR;
1322+
continue;
1323+
} else if (!strcmp(v, "none")) {
1324+
log_destination = LOG_DESTINATION_NONE;
1325+
continue;
1326+
} else
1327+
die("unknown log destination '%s'", v);
1328+
}
13031329
if (!strcmp(arg, "--export-all")) {
13041330
export_all_trees = 1;
13051331
continue;
@@ -1356,7 +1382,6 @@ int cmd_main(int argc, const char **argv)
13561382
}
13571383
if (!strcmp(arg, "--detach")) {
13581384
detach = 1;
1359-
log_syslog = 1;
13601385
continue;
13611386
}
13621387
if (skip_prefix(arg, "--user=", &v)) {
@@ -1402,7 +1427,14 @@ int cmd_main(int argc, const char **argv)
14021427
usage(daemon_usage);
14031428
}
14041429

1405-
if (log_syslog) {
1430+
if (log_destination == LOG_DESTINATION_UNSET) {
1431+
if (inetd_mode || detach)
1432+
log_destination = LOG_DESTINATION_SYSLOG;
1433+
else
1434+
log_destination = LOG_DESTINATION_STDERR;
1435+
}
1436+
1437+
if (log_destination == LOG_DESTINATION_SYSLOG) {
14061438
openlog("git-daemon", LOG_PID, LOG_DAEMON);
14071439
set_die_routine(daemon_die);
14081440
} else

0 commit comments

Comments
 (0)