From: Sami Kerola Date: Sat, 28 Jun 2014 15:08:28 +0000 (+0100) Subject: logger: add process --id=parent optional argument X-Git-Tag: v2.26-rc1~576^2~5 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=aab5b44405b9a6ada92e419e5a84cc0d1d4afee9;p=thirdparty%2Futil-linux.git logger: add process --id=parent optional argument When scripts send several messages they will be easier to group together when parent process id is printed rather than id of the each logger process. Signed-off-by: Sami Kerola --- diff --git a/misc-utils/logger.1 b/misc-utils/logger.1 index a624f44fba..1a366db003 100644 --- a/misc-utils/logger.1 +++ b/misc-utils/logger.1 @@ -56,9 +56,13 @@ port defined in /etc/services, which is often \fB\-h\fR, \fB\-\-help\fR Display help text and exit. .TP -\fB\-i\fR, \fB\-\-id\fR -Log the process ID of the logger process with each line. -.TP +\fB\-i\fR, \fB\-\-id\fR=[\fBppid\fR|\fBpid\fR] +Log the PID of the logger process with each line. When optional +argument +.B ppid +is specified PPID is used instead of logger command PID. Use of +.B ppid +is recommended in scripts that send several messages. .TP \fB\-n\fR, \fB\-\-server\fR \fIserver\fR Write to the specified remote syslog diff --git a/misc-utils/logger.c b/misc-utils/logger.c index 4d28d12839..c1af7350c0 100644 --- a/misc-utils/logger.c +++ b/misc-utils/logger.c @@ -92,6 +92,7 @@ struct logger_ctl { void (*syslogfp)(struct logger_ctl *ctl, char *msg); unsigned int prio_prefix:1, /* read priority from intput */ + ppid:1, /* include PPID instead of PID */ rfc5424_time:1, /* include time stamp */ rfc5424_tq:1, /* include time quality markup */ rfc5424_host:1; /* include hostname */ @@ -288,15 +289,25 @@ static char *xgetlogin() return cp; } +static pid_t get_process_id(struct logger_ctl *ctl) +{ + pid_t id = 0; + + if (ctl->logflags & LOG_PID) + id = ctl->ppid ? getppid() : getpid(); + return id; +} + static void syslog_rfc3164(struct logger_ctl *ctl, char *msg) { char buf[1000], pid[30], *cp, *tp; time_t now; + pid_t process; if (ctl->fd < 0) return; - if (ctl->logflags & LOG_PID) - snprintf(pid, sizeof(pid), "[%d]", getpid()); + if ((process = get_process_id(ctl))) + snprintf(pid, sizeof(pid), "[%d]", process); else pid[0] = 0; if (ctl->tag) @@ -318,6 +329,7 @@ static void syslog_rfc5424(struct logger_ctl *ctl, char *msg) char fmt[64], time[64], timeq[80], *hostname; struct timeval tv; struct tm *tm; + pid_t process; if (ctl->fd < 0) return; @@ -346,8 +358,8 @@ static void syslog_rfc5424(struct logger_ctl *ctl, char *msg) tag = xgetlogin(); if (48 < strlen(tag)) errx(EXIT_FAILURE, _("tag '%s' is too long"), tag); - if (ctl->logflags & LOG_PID) - snprintf(pid, sizeof(pid), " %d", getpid()); + if ((process = get_process_id(ctl))) + snprintf(pid, sizeof(pid), " %d", process); else pid[0] = 0; if (ctl->rfc5424_tq) { @@ -471,7 +483,7 @@ static void __attribute__ ((__noreturn__)) usage(FILE *out) fprintf(out, _(" %s [options] []\n"), program_invocation_short_name); fputs(USAGE_OPTIONS, out); - fputs(_(" -i, --id log the process ID too\n"), out); + fputs(_(" -i, --id[=pid|ppid] log PID or PPID (default is PID)\n"), out); fputs(_(" -f, --file log the contents of this file\n"), out); fputs(_(" -p, --priority mark given message with this priority\n"), out); fputs(_(" --prio-prefix look for a prefix on every line read from stdin\n"), out); @@ -508,6 +520,7 @@ int main(int argc, char **argv) struct logger_ctl ctl = { .fd = -1, .logflags = 0, + .ppid = 0, .pri = LOG_NOTICE, .prio_prefix = 0, .tag = NULL, @@ -524,7 +537,7 @@ int main(int argc, char **argv) FILE *jfd = NULL; #endif static const struct option longopts[] = { - { "id", no_argument, 0, 'i' }, + { "id", optional_argument, 0, 'i' }, { "stderr", no_argument, 0, 's' }, { "file", required_argument, 0, 'f' }, { "priority", required_argument, 0, 'p' }, @@ -550,7 +563,7 @@ int main(int argc, char **argv) textdomain(PACKAGE); atexit(close_stdout); - while ((ch = getopt_long(argc, argv, "f:ip:st:u:dTn:P:Vh", + while ((ch = getopt_long(argc, argv, "f:i::p:st:u:dTn:P:Vh", longopts, NULL)) != -1) { switch (ch) { case 'f': /* file to log */ @@ -560,6 +573,14 @@ int main(int argc, char **argv) break; case 'i': /* log process id also */ ctl.logflags |= LOG_PID; + if (optarg) { + if (!strcmp(optarg, "ppid")) + ctl.ppid = 1; + else if (!strcmp(optarg, "pid")) + ctl.ppid = 0; + else + warnx(_("ignoring unknown option argument: %s"), optarg); + } break; case 'p': /* priority */ ctl.pri = pencode(optarg);