From: Sami Kerola Date: Fri, 8 Aug 2014 23:49:46 +0000 (+0100) Subject: logger: allow use of --id=ppid when logging locally X-Git-Tag: v2.26-rc1~498^2~4 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1d57503378bdcd838365d625f6d2d0a09da9c29d;p=thirdparty%2Futil-linux.git logger: allow use of --id=ppid when logging locally There is no obvious way to make syslog(3) to print both pid or ppid, so duplicate the libc syslog() to logger. Making the ppid printing work using unix socket has side effect of local becoming capable to use both rfc format output, which is hopefully seen as good thing. The syslog_local() is format wise one-to-one copy with glibc syslog(3) format. Signed-off-by: Sami Kerola --- diff --git a/misc-utils/logger.c b/misc-utils/logger.c index 4836b8f6e6..d1b93d0bd0 100644 --- a/misc-utils/logger.c +++ b/misc-utils/logger.c @@ -418,7 +418,30 @@ static void parse_rfc5424_flags(struct logger_ctl *ctl, char *optarg) static void syslog_local(struct logger_ctl *ctl, char *msg) { - syslog(ctl->pri, "%s", msg); + char *buf, *tag; + char time[32], pid[32]; + struct timeval tv; + struct tm *tm; + pid_t process; + int len; + + gettimeofday(&tv, NULL); + tm = localtime(&tv.tv_sec); + strftime(time, sizeof(time), "%h %e %T", tm); + + tag = ctl->tag ? ctl->tag : program_invocation_short_name; + + if ((process = get_process_id(ctl))) + snprintf(pid, sizeof(pid), "[%d]", process); + else + pid[0] = '\0'; + + len = xasprintf(&buf, "<%d>%s %s%s: %s", ctl->pri, time, tag, pid, msg); + if (write_all(ctl->fd, buf, len) < 0) + warn(_("write failed")); + if (ctl->logflags & LOG_PERROR) + fprintf(stderr, "%s\n", buf); + free(buf); } static void logger_open(struct logger_ctl *ctl) @@ -435,12 +458,7 @@ static void logger_open(struct logger_ctl *ctl) ctl->syslogfp = syslog_rfc5424; return; } - - if (ctl->syslogfp == syslog_rfc5424 || ctl->syslogfp == syslog_rfc3164) - errx(EXIT_FAILURE, _("--server or --socket are required to " - "log by --rfc5424 or --rfc3164")); - - openlog(ctl->tag ? ctl->tag : xgetlogin(), ctl->logflags, 0); + ctl->fd = unix_socket("/dev/log", ctl->socket_type); ctl->syslogfp = syslog_local; } @@ -493,10 +511,8 @@ static void logger_stdin(struct logger_ctl *ctl) static void logger_close(struct logger_ctl *ctl) { - if (!ctl->unix_socket && !ctl->server) - closelog(); - else - close(ctl->fd); + if (close(ctl->fd) != 0) + err(EXIT_FAILURE, _("close failed")); } static void __attribute__ ((__noreturn__)) usage(FILE *out)