aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEugene Grosbein <eugen@FreeBSD.org>2023-04-27 16:43:16 +0000
committerEugene Grosbein <eugen@FreeBSD.org>2023-07-18 07:24:26 +0000
commit829dbdcac20d1237ac9693d2e5489857e151754a (patch)
tree04d17968c3e0f5a24b3e5ab8ef3d97ef15d2d8ec
parent8cf8bbfef50908edd2a61356e13c94044ef49fc3 (diff)
downloadsrc-829dbdcac20d1237ac9693d2e5489857e151754a.tar.gz
src-829dbdcac20d1237ac9693d2e5489857e151754a.zip
logger(1): MFC: fix timestamps in case of long run
An example: ( echo test; sleep 2; echo test2 ) | logger -h /var/run/log Before fix, logger assigned same timestamp to both records. Fixes: 65547fb33db901a9f352aacb0ed45ce68b0bd275 Reported by: Vadim Goncharov (cherry picked from commit 83fd35b3f3fa580d2b99874abd1f67ee61dcb659)
-rw-r--r--usr.bin/logger/logger.c18
1 files changed, 12 insertions, 6 deletions
diff --git a/usr.bin/logger/logger.c b/usr.bin/logger/logger.c
index 936bfc98803c..a99db27f6b1b 100644
--- a/usr.bin/logger/logger.c
+++ b/usr.bin/logger/logger.c
@@ -175,22 +175,23 @@ main(int argc, char *argv[])
openlog(tag, logflags, 0);
(void) fclose(stdout);
- (void )time(&now);
- (void )ctime_r(&now, tbuf);
- tbuf[19] = '\0';
- timestamp = tbuf + 4;
-
if (hostname == NULL) {
hostname = hbuf;
(void )gethostname(hbuf, MAXHOSTNAMELEN);
*strchrnul(hostname, '.') = '\0';
}
+ timestamp = tbuf + 4;
+
/* log input line if appropriate */
if (argc > 0) {
char *p, *endp;
size_t len;
+ (void )time(&now);
+ (void )ctime_r(&now, tbuf);
+ tbuf[19] = '\0';
+
for (p = buf, endp = buf + sizeof(buf) - 2; *argv;) {
len = strlen(*argv);
if (p + len > endp && p > buf) {
@@ -212,9 +213,14 @@ main(int argc, char *argv[])
logmessage(pri, timestamp, hostname, tag, socks, nsock,
buf);
} else
- while (fgets(buf, sizeof(buf), stdin) != NULL)
+ while (fgets(buf, sizeof(buf), stdin) != NULL) {
+ (void )time(&now);
+ (void )ctime_r(&now, tbuf);
+ tbuf[19] = '\0';
+
logmessage(pri, timestamp, hostname, tag, socks, nsock,
buf);
+ }
exit(0);
}