From nobody Mon Aug 22 02:30:40 2022 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4M9xDJ6Y6Gz4ZVTw; Mon, 22 Aug 2022 02:30:40 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4M9xDJ64tBz45K5; Mon, 22 Aug 2022 02:30:40 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1661135440; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=4zyDJ4cPGoT7NDGVxGS5KdxX8k3XLacsxeDu53t64m8=; b=Mfsv/V6ROSiEi63NYyKKMQax6fy3AaE4BLMl/is9F1OKzUvFOvNaA88Zq8cbgB5ASk65Cd bJVI+D4Wm/HBNePbQJTDB/9e3QyLMbMzpMI5USOoiX+owYm2Zb6vVgGH7bSrLUaAyLKTr3 ASOSgvmLm1rwoJ+nr9WOznWYgIUXlOl9xRX4BtHplCajljTQe6OJPIvuqwrzuterD9GwRa 8F7DvRu0UMWvQ+Q67H4voik+4aX/51AAWwFdTglFo1vgBv2HenLwrYtyFEDaJQ9niRpLyu PKjiFrA43E35/FIhndGN3qjG1XBOihCasvI10QjCTM1LmBmMO4x2CrfFE/UYIg== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4M9xDJ5829zYYK; Mon, 22 Aug 2022 02:30:40 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 27M2UecC080348; Mon, 22 Aug 2022 02:30:40 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 27M2Ue40080347; Mon, 22 Aug 2022 02:30:40 GMT (envelope-from git) Date: Mon, 22 Aug 2022 02:30:40 GMT Message-Id: <202208220230.27M2Ue40080347@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Eugene Grosbein Subject: git: 2067574b27ca - stable/13 - MFC: syslog(3): unbreak log generation using fabricated PID List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: eugen X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: 2067574b27ca5329c893df31bcfc850f64a77af2 Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1661135440; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=4zyDJ4cPGoT7NDGVxGS5KdxX8k3XLacsxeDu53t64m8=; b=mh7ZxglPBZSrR2ZXGZmZ0s1Gv9qTzP722RCGOhaaho6TbWl2zVVWzA36aNlfJqfRnt65lg 2P3tOi/hbVBf0QvG3Hkraho/6zR3KuwYmuJOmOmBzIMrDt8Oqe1py4Dh8TYL1HBK0HEfzV grhwN9jaPT0cj26EntNJzEfSIOFZ4WXktrZJPujTViF6prXA6THzbIjARvUxqtUbf2NR0I KbaWbjU9uVMD4M+EFNnoyOjVyEwCasTh8/YbzFawHuXcM4ejiFk81Hv8mcUCozneFBvJJs lTDud/7U9FrYAZjKbGejsu/X8bWcNRGJS01h0hI3MgEKLlUfD5Qv7C4hviN6HQ== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1661135440; a=rsa-sha256; cv=none; b=Mzo5RQ5l5uAmJ5Wy2xrPijJScD3yhwdHUb5yRH8tULUPIm3EqOndda5TLfUZvilBXe2FRU 1+WZAEiTnCUhroMVf5Y8LeV3TZ8Ni1xT38MTcHu09Qw/p/3fPX4Ukl516VxdXfxMVOfrMi eZta37rVrKuxhSY7Emc+JNWjwak1C9Jw9+N7D5X/heo0xoX7vhqU+TWB3FgZKkdugZ9oA/ 6/yppUmAYFJB68YskOzz0PD9EfZFVnPIvICWXE2bvbfKn10TvpsCM87lIB9rjxKei7jaNU TXDd1Xde57U/I4RQauizHRmZ2ME9sjeacdro5UiTqNxQJBtIWJR9VD9m2LpkOQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch stable/13 has been updated by eugen: URL: https://cgit.FreeBSD.org/src/commit/?id=2067574b27ca5329c893df31bcfc850f64a77af2 commit 2067574b27ca5329c893df31bcfc850f64a77af2 Author: Eugene Grosbein AuthorDate: 2022-08-08 22:21:02 +0000 Commit: Eugene Grosbein CommitDate: 2022-08-22 02:30:32 +0000 MFC: syslog(3): unbreak log generation using fabricated PID Recover application ability to supply fabricated PID embedded into ident that was lost when libc switched to generation of RFC 5424 log messages, for example: logger -t "ident[$$]" -p user.notice "test" It is essential for long running scripts. Also, this change unbreaks matching resulted entries by ident in syslog.conf: !ident *.* /var/log/ident.log Without the fix, the log (and matching) was broken: Aug 1 07:36:58 hostname ident[123][86483]: test Now it works as expected and worked before breakage: Aug 1 07:39:40 hostname ident[123]: test Differential: https://reviews.freebsd.org/D36005 (cherry picked from commit e9ae9fa93745669b7dd0341d333257ad6cfe8e37) --- lib/libc/gen/syslog.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 51 insertions(+), 3 deletions(-) diff --git a/lib/libc/gen/syslog.c b/lib/libc/gen/syslog.c index 797c7389d1a2..fe3a2c902015 100644 --- a/lib/libc/gen/syslog.c +++ b/lib/libc/gen/syslog.c @@ -64,7 +64,9 @@ static int LogFile = -1; /* fd for log */ static int status; /* connection status */ static int opened; /* have done openlog() */ static int LogStat = 0; /* status bits, set by openlog() */ +static pid_t LogPid = -1; /* process id to tag the entry with */ static const char *LogTag = NULL; /* string to tag the entry with */ +static int LogTagLength = -1; /* usable part of LogTag */ static int LogFacility = LOG_USER; /* default facility code */ static int LogMask = 0xff; /* mask of priorities to be logged */ static pthread_mutex_t syslog_mutex = PTHREAD_MUTEX_INITIALIZER; @@ -84,6 +86,7 @@ static pthread_mutex_t syslog_mutex = PTHREAD_MUTEX_INITIALIZER; static void disconnectlog(void); /* disconnect from syslogd */ static void connectlog(void); /* (re)connect to syslogd */ static void openlog_unlocked(const char *, int, int); +static void parse_tag(void); /* parse ident[NNN] if needed */ enum { NOCONN = 0, @@ -209,13 +212,20 @@ vsyslog1(int pri, const char *fmt, va_list ap) /* Application name. */ if (LogTag == NULL) LogTag = _getprogname(); - (void)fprintf(fp, "%s ", LogTag == NULL ? NILVALUE : LogTag); + else if (LogTagLength == -1) + parse_tag(); + if (LogTagLength > 0) + (void)fprintf(fp, "%.*s ", LogTagLength, LogTag); + else + (void)fprintf(fp, "%s ", LogTag == NULL ? NILVALUE : LogTag); /* * Provide the process ID regardless of whether LOG_PID has been * specified, as it provides valuable information. Many * applications tend not to use this, even though they should. */ - (void)fprintf(fp, "%d ", getpid()); + if (LogPid == -1) + LogPid = getpid(); + (void)fprintf(fp, "%d ", (int)LogPid); /* Message ID. */ (void)fputs(NILVALUE " ", fp); /* Structured data. */ @@ -457,9 +467,12 @@ connectlog(void) static void openlog_unlocked(const char *ident, int logstat, int logfac) { - if (ident != NULL) + if (ident != NULL) { LogTag = ident; + LogTagLength = -1; + } LogStat = logstat; + parse_tag(); if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0) LogFacility = logfac; @@ -489,6 +502,7 @@ closelog(void) LogFile = -1; } LogTag = NULL; + LogTagLength = -1; status = NOCONN; THREAD_UNLOCK(); } @@ -506,3 +520,37 @@ setlogmask(int pmask) THREAD_UNLOCK(); return (omask); } + +/* + * Obtain LogPid from LogTag formatted as following: ident[NNN] + */ +static void +parse_tag(void) +{ + char *begin, *end, *p; + pid_t pid; + + if (LogTag == NULL || (LogStat & LOG_PID) != 0) + return; + /* + * LogTagLength is -1 if LogTag was not parsed yet. + * Avoid multiple passes over same LogTag. + */ + LogTagLength = 0; + + /* Check for presence of opening [ and non-empty ident. */ + if ((begin = strchr(LogTag, '[')) == NULL || begin == LogTag) + return; + /* Check for presence of closing ] at the very end and non-empty pid. */ + if ((end = strchr(begin + 1, ']')) == NULL || end[1] != 0 || + (end - begin) < 2) + return; + + /* Check for pid to contain digits only. */ + pid = (pid_t)strtol(begin + 1, &p, 10); + if (p != end) + return; + + LogPid = pid; + LogTagLength = begin - LogTag; +}