From: Junio C Hamano Date: Wed, 21 Feb 2018 20:45:04 +0000 (-0800) Subject: Merge branch 'lw/daemon-log-destination' X-Git-Tag: v2.17.0-rc0~85 X-Git-Url: https://git.lorimer.id.au/gitweb.git/diff_plain/c2bd43d66dab958d72bee58d319df55600158f2e?ds=inline;hp=-c 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) --- c2bd43d66dab958d72bee58d319df55600158f2e diff --combined daemon.c index 72dfeaf6e2,fb538e3678..fe833ea7de --- a/daemon.c +++ b/daemon.c @@@ -9,7 -9,12 +9,12 @@@ #define initgroups(x, y) (0) /* nothing */ #endif - static int log_syslog; + static enum log_destination { + LOG_DESTINATION_UNSET = -1, + LOG_DESTINATION_NONE = 0, + LOG_DESTINATION_STDERR = 1, + LOG_DESTINATION_SYSLOG = 2, + } log_destination = LOG_DESTINATION_UNSET; static int verbose; static int reuseaddr; static int informative_errors; @@@ -25,6 -30,7 +30,7 @@@ static const char daemon_usage[] " [--access-hook=]\n" " [--inetd | [--listen=] [--port=]\n" " [--detach] [--user= [--group=]]\n" + " [--log-destination=(stderr|syslog|none)]\n" " [...]"; /* List of acceptable pathname prefixes */ @@@ -74,11 -80,14 +80,14 @@@ static const char *get_ip_address(struc static void logreport(int priority, const char *err, va_list params) { - if (log_syslog) { + switch (log_destination) { + case LOG_DESTINATION_SYSLOG: { char buf[1024]; vsnprintf(buf, sizeof(buf), err, params); syslog(priority, "%s", buf); - } else { + break; + } + case LOG_DESTINATION_STDERR: /* * Since stderr is set to buffered mode, the * logging of different processes will not overlap @@@ -88,6 -97,11 +97,11 @@@ vfprintf(stderr, err, params); fputc('\n', stderr); fflush(stderr); + break; + case LOG_DESTINATION_NONE: + break; + case LOG_DESTINATION_UNSET: + BUG("log destination not initialized correctly"); } } @@@ -597,7 -611,6 +611,7 @@@ static char *parse_host_arg(struct host if (strncasecmp("host=", extra_args, 5) == 0) { val = extra_args + 5; vallen = strlen(val) + 1; + loginfo("Extended attribute \"host\": %s", val); if (*val) { /* Split : at colon. */ char *host; @@@ -648,11 -661,9 +662,11 @@@ static void parse_extra_args(struct hos } } - if (git_protocol.len > 0) + if (git_protocol.len > 0) { + loginfo("Extended attribute \"protocol\": %s", git_protocol.buf); argv_array_pushf(env, GIT_PROTOCOL_ENVIRONMENT "=%s", git_protocol.buf); + } strbuf_release(&git_protocol); } @@@ -760,8 -771,14 +774,8 @@@ static int execute(void alarm(0); len = strlen(line); - if (pktlen != len) - loginfo("Extended attributes (%d bytes) exist <%.*s>", - (int) pktlen - len, - (int) pktlen - len, line + len + 1); - if (len && line[len-1] == '\n') { - line[--len] = 0; - pktlen--; - } + if (len && line[len-1] == '\n') + line[len-1] = 0; /* parse additional args hidden behind a NUL byte */ if (len != pktlen) @@@ -1286,7 -1303,6 +1300,6 @@@ int cmd_main(int argc, const char **arg } if (!strcmp(arg, "--inetd")) { inetd_mode = 1; - log_syslog = 1; continue; } if (!strcmp(arg, "--verbose")) { @@@ -1294,9 -1310,22 +1307,22 @@@ continue; } if (!strcmp(arg, "--syslog")) { - log_syslog = 1; + log_destination = LOG_DESTINATION_SYSLOG; continue; } + if (skip_prefix(arg, "--log-destination=", &v)) { + if (!strcmp(v, "syslog")) { + log_destination = LOG_DESTINATION_SYSLOG; + continue; + } else if (!strcmp(v, "stderr")) { + log_destination = LOG_DESTINATION_STDERR; + continue; + } else if (!strcmp(v, "none")) { + log_destination = LOG_DESTINATION_NONE; + continue; + } else + die("unknown log destination '%s'", v); + } if (!strcmp(arg, "--export-all")) { export_all_trees = 1; continue; @@@ -1353,7 -1382,6 +1379,6 @@@ } if (!strcmp(arg, "--detach")) { detach = 1; - log_syslog = 1; continue; } if (skip_prefix(arg, "--user=", &v)) { @@@ -1399,7 -1427,14 +1424,14 @@@ usage(daemon_usage); } - if (log_syslog) { + if (log_destination == LOG_DESTINATION_UNSET) { + if (inetd_mode || detach) + log_destination = LOG_DESTINATION_SYSLOG; + else + log_destination = LOG_DESTINATION_STDERR; + } + + if (log_destination == LOG_DESTINATION_SYSLOG) { openlog("git-daemon", LOG_PID, LOG_DAEMON); set_die_routine(daemon_die); } else