git-daemon: use LOG_PID, simplify logging code
Make git-daemon use LOG_PID like most daemons, instead of prepending the pid to the message ourselves, when using syslog(3). Simplify the logging code by setting stderr to line buffered, instead of building a single string and writing it out with a single write(2). Give an extra log message at the daemon start-up. Signed-off-by: Stephen R. van den Berg <srb@cuci.nl> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:

committed by
Junio C Hamano

parent
df0daf8ac0
commit
6a992e9e1d
47
daemon.c
47
daemon.c
@ -78,38 +78,19 @@ static struct interp interp_table[] = {
|
|||||||
|
|
||||||
static void logreport(int priority, const char *err, va_list params)
|
static void logreport(int priority, const char *err, va_list params)
|
||||||
{
|
{
|
||||||
/* We should do a single write so that it is atomic and output
|
|
||||||
* of several processes do not get intermingled. */
|
|
||||||
char buf[1024];
|
|
||||||
int buflen;
|
|
||||||
int maxlen, msglen;
|
|
||||||
|
|
||||||
/* sizeof(buf) should be big enough for "[pid] \n" */
|
|
||||||
buflen = snprintf(buf, sizeof(buf), "[%ld] ", (long) getpid());
|
|
||||||
|
|
||||||
maxlen = sizeof(buf) - buflen - 1; /* -1 for our own LF */
|
|
||||||
msglen = vsnprintf(buf + buflen, maxlen, err, params);
|
|
||||||
|
|
||||||
if (log_syslog) {
|
if (log_syslog) {
|
||||||
|
char buf[1024];
|
||||||
|
vsnprintf(buf, sizeof(buf), err, params);
|
||||||
syslog(priority, "%s", buf);
|
syslog(priority, "%s", buf);
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
/* maxlen counted our own LF but also counts space given to
|
/* Since stderr is set to linebuffered mode, the
|
||||||
* vsnprintf for the terminating NUL. We want to make sure that
|
* logging of different processes will not overlap
|
||||||
* we have space for our own LF and NUL after the "meat" of the
|
*/
|
||||||
* message, so truncate it at maxlen - 1.
|
fprintf(stderr, "[%d] ", (int)getpid());
|
||||||
*/
|
vfprintf(stderr, err, params);
|
||||||
if (msglen > maxlen - 1)
|
fputc('\n', stderr);
|
||||||
msglen = maxlen - 1;
|
}
|
||||||
else if (msglen < 0)
|
|
||||||
msglen = 0; /* Protect against weird return values. */
|
|
||||||
buflen += msglen;
|
|
||||||
|
|
||||||
buf[buflen++] = '\n';
|
|
||||||
buf[buflen] = '\0';
|
|
||||||
|
|
||||||
write_in_full(2, buf, buflen);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void logerror(const char *err, ...)
|
static void logerror(const char *err, ...)
|
||||||
@ -1178,9 +1159,11 @@ int main(int argc, char **argv)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (log_syslog) {
|
if (log_syslog) {
|
||||||
openlog("git-daemon", 0, LOG_DAEMON);
|
openlog("git-daemon", LOG_PID, LOG_DAEMON);
|
||||||
set_die_routine(daemon_die);
|
set_die_routine(daemon_die);
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
setlinebuf(stderr); /* avoid splitting a message in the middle */
|
||||||
|
|
||||||
if (inetd_mode && (group_name || user_name))
|
if (inetd_mode && (group_name || user_name))
|
||||||
die("--user and --group are incompatible with --inetd");
|
die("--user and --group are incompatible with --inetd");
|
||||||
@ -1233,8 +1216,10 @@ int main(int argc, char **argv)
|
|||||||
return execute(peer);
|
return execute(peer);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (detach)
|
if (detach) {
|
||||||
daemonize();
|
daemonize();
|
||||||
|
loginfo("Ready to rumble");
|
||||||
|
}
|
||||||
else
|
else
|
||||||
sanitize_stdfds();
|
sanitize_stdfds();
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user