]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
dmesg: add subsecond granularity for --since and --until
authorThomas Weißschuh <thomas@t-8ch.de>
Fri, 20 Jan 2023 06:18:36 +0000 (06:18 +0000)
committerThomas Weißschuh <thomas@t-8ch.de>
Tue, 24 Jan 2023 16:04:52 +0000 (16:04 +0000)
Fixes #1708

Signed-off-by: Thomas Weißschuh <thomas@t-8ch.de>
sys-utils/dmesg.c
tests/expected/dmesg/limit
tests/ts/dmesg/limit

index 6bd6880adb11ed05838765b152879a1259fde4dd..fdf0f4631441640e852797ae2eae22ab0cae9be6 100644 (file)
@@ -181,8 +181,8 @@ struct dmesg_control {
        ssize_t         kmsg_first_read;/* initial read() return code */
        char            kmsg_buf[BUFSIZ];/* buffer to read kmsg data */
 
-       time_t          since;          /* filter records by time */
-       time_t          until;          /* filter records by time */
+       usec_t          since;          /* filter records by time */
+       usec_t          until;          /* filter records by time */
 
        /*
         * For the --file option we mmap whole file. The unnecessary (already
@@ -784,9 +784,11 @@ static int get_next_syslog_record(struct dmesg_control *ctl,
        return 1;
 }
 
-static time_t record_time(struct dmesg_control *ctl, struct dmesg_record *rec)
+static usec_t record_time(struct dmesg_control *ctl, struct dmesg_record *rec)
 {
-       return ctl->boot_time.tv_sec + ctl->suspended_time / USEC_PER_SEC + rec->tv.tv_sec;
+       return timeval_to_usec(&ctl->boot_time) +
+               ctl->suspended_time +
+               timeval_to_usec(&rec->tv);
 }
 
 static int accept_record(struct dmesg_control *ctl, struct dmesg_record *rec)
@@ -842,7 +844,7 @@ static struct tm *record_localtime(struct dmesg_control *ctl,
                                   struct dmesg_record *rec,
                                   struct tm *tm)
 {
-       time_t t = record_time(ctl, rec);
+       time_t t = record_time(ctl, rec) / USEC_PER_SEC;
        return localtime_r(&t, tm);
 }
 
@@ -1554,18 +1556,14 @@ int main(int argc, char *argv[])
                        break;
                case OPT_SINCE:
                {
-                       usec_t p;
-                       if (parse_timestamp(optarg, &p) < 0)
+                       if (parse_timestamp(optarg, &ctl.since) < 0)
                                errx(EXIT_FAILURE, _("invalid time value \"%s\""), optarg);
-                       ctl.since = (time_t) (p / 1000000);
                        break;
                }
                case OPT_UNTIL:
                {
-                       usec_t p;
-                       if (parse_timestamp(optarg, &p) < 0)
+                       if (parse_timestamp(optarg, &ctl.until) < 0)
                                errx(EXIT_FAILURE, _("invalid time value \"%s\""), optarg);
-                       ctl.until = (time_t) (p / 1000000);
                        break;
                }
                case 'h':
index 9dbbc4e62eaa7c3190626d64db2863c38291c2a7..03569ab79e9aab99226f02f6e0fef8955b97b023 100644 (file)
@@ -1,3 +1,4 @@
+[    1.000000] example[1]
 [    8.000000] example[2]
 [   27.000000] example[3]
 [   64.000000] example[4]
index 9772784a29b62c6cea9a8c860eea0447db3389b5..f3f9643f47d1255c8eeb3816a624f27f43f657b7 100755 (executable)
@@ -23,7 +23,7 @@ ts_check_test_command "$TS_HELPER_DMESG"
 export TZ="GMT"
 export DMESG_TEST_BOOTIME="1234567890.123456"
 
-$TS_HELPER_DMESG --since @1234567892 --until @1234567991 -F $TS_SELF/input \
+$TS_HELPER_DMESG --since @1234567890.124 --until @1234567991 -F $TS_SELF/input \
        >> $TS_OUTPUT 2> $TS_ERRLOG
 
 ts_finalize