]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lsclocks: automatically discover dynamic clocks
authorThomas Weißschuh <thomas@t-8ch.de>
Tue, 11 Jul 2023 11:36:19 +0000 (13:36 +0200)
committerThomas Weißschuh <thomas@t-8ch.de>
Wed, 12 Jul 2023 14:53:00 +0000 (16:53 +0200)
Signed-off-by: Thomas Weißschuh <thomas@t-8ch.de>
misc-utils/lsclocks.1.adoc
misc-utils/lsclocks.c
tests/ts/misc/lsclocks

index 644887bacb0da3a373c542079f33d01d3060959c..90497fba525fd26b1e88b7ddf5d467cdee8b05ac 100644 (file)
@@ -42,6 +42,9 @@ Use raw output format.
 *-r*, *--time* _clock_
 Show current time of one specific clocks.
 
+*--no-discover-dynamic*
+Do not try to discover dynamic clocks.
+
 *-d*, *--dynamic-clock* _path_
 Also display specified dynamic clock.
 
index a0fe55bd4de04855c74b6c49774d5c2dd61b1ae7..ec27b9a1557637eba871807860e5b8772a4f6e09 100644 (file)
@@ -23,6 +23,7 @@
 #include <time.h>
 #include <inttypes.h>
 #include <getopt.h>
+#include <glob.h>
 
 #include <libsmartcols.h>
 
@@ -194,6 +195,7 @@ static void __attribute__((__noreturn__)) usage(void)
        fputs(_("     --output-all           output all columns\n"), out);
        fputs(_(" -r, --raw                  use raw output format\n"), out);
        fputs(_(" -t, --time <clock>         show current time of single clock\n"), out);
+       fputs(_(" --no-discover-dynamic      do not try to discover dynamic clocks\n"), out);
        fputs(_(" -d, --dynamic-clock <path> also display specified dynamic clock\n"), out);
 
        fputs(USAGE_SEPARATOR, out);
@@ -376,11 +378,15 @@ struct dynamic_clock {
 
 static void add_dynamic_clock_from_path(struct libscols_table *tb,
                                        const int *columns, size_t ncolumns,
-                                       const char *path)
+                                       const char *path, bool explicit)
 {
        int fd = open(path, O_RDONLY);
-       if (fd == -1)
-               err(EXIT_FAILURE, _("Could not open %s"), path);
+       if (fd == -1) {
+               if (explicit)
+                       err(EXIT_FAILURE, _("Could not open %s"), path);
+               else
+                       return;
+       }
 
        struct clockinfo clockinfo = {
                .type = CT_PTP,
@@ -392,6 +398,24 @@ static void add_dynamic_clock_from_path(struct libscols_table *tb,
        close(fd);
 }
 
+static void add_dynamic_clocks_from_discovery(struct libscols_table *tb,
+                                             const int *columns, size_t ncolumns)
+{
+       int rc;
+       size_t i;
+       glob_t state;
+
+       rc = glob("/dev/ptp*", 0, NULL, &state);
+       if (rc)
+               errx(EXIT_FAILURE, _("Could not glob: %d"), rc);
+
+       for (i = 0; i < state.gl_pathc; i++)
+               add_dynamic_clock_from_path(tb, columns, ncolumns,
+                                           state.gl_pathv[i], false);
+
+       globfree(&state);
+}
+
 int main(int argc, char **argv)
 {
        size_t i;
@@ -401,7 +425,7 @@ int main(int argc, char **argv)
        struct libscols_table *tb;
        struct libscols_column *col;
 
-       bool noheadings = false, raw = false, json = false;
+       bool noheadings = false, raw = false, json = false, disc_dynamic = true;
        const char *outarg = NULL;
        int columns[ARRAY_SIZE(infos) * 2];
        size_t ncolumns = 0;
@@ -413,18 +437,20 @@ int main(int argc, char **argv)
        struct timespec now;
 
        enum {
-               OPT_OUTPUT_ALL = CHAR_MAX + 1
+               OPT_OUTPUT_ALL = CHAR_MAX + 1,
+               OPT_NO_DISC_DYN,
        };
        static const struct option longopts[] = {
-               { "noheadings",    no_argument,       NULL, 'n' },
-               { "output",        required_argument, NULL, 'o' },
-               { "output-all",    no_argument,       NULL, OPT_OUTPUT_ALL },
-               { "version",       no_argument,       NULL, 'V' },
-               { "help",          no_argument,       NULL, 'h' },
-               { "json",          no_argument,       NULL, 'J' },
-               { "raw",           no_argument,       NULL, 'r' },
-               { "time",          required_argument, NULL, 't' },
-               { "dynamic-clock", required_argument, NULL, 'd' },
+               { "noheadings",          no_argument,       NULL, 'n' },
+               { "output",              required_argument, NULL, 'o' },
+               { "output-all",          no_argument,       NULL, OPT_OUTPUT_ALL },
+               { "version",             no_argument,       NULL, 'V' },
+               { "help",                no_argument,       NULL, 'h' },
+               { "json",                no_argument,       NULL, 'J' },
+               { "raw",                 no_argument,       NULL, 'r' },
+               { "time",                required_argument, NULL, 't' },
+               { "no-discover-dynamic", no_argument,       NULL, OPT_NO_DISC_DYN },
+               { "dynamic-clock",       required_argument, NULL, 'd' },
                { 0 }
        };
 
@@ -461,6 +487,9 @@ int main(int argc, char **argv)
                        dynamic_clock->path = optarg;
                        list_add(&dynamic_clock->head, &dynamic_clocks);
                        break;
+               case OPT_NO_DISC_DYN:
+                       disc_dynamic = false;
+                       break;
                case 'V':
                        print_version(EXIT_SUCCESS);
                case 'h':
@@ -514,9 +543,12 @@ int main(int argc, char **argv)
        for (i = 0; i < ARRAY_SIZE(clocks); i++)
                add_clock_line(tb, columns, ncolumns, &clocks[i]);
 
+       if (disc_dynamic)
+               add_dynamic_clocks_from_discovery(tb, columns, ncolumns);
+
        list_for_each(current_dynamic_clock, &dynamic_clocks) {
                dynamic_clock = list_entry(current_dynamic_clock, struct dynamic_clock, head);
-               add_dynamic_clock_from_path(tb, columns, ncolumns, dynamic_clock->path);
+               add_dynamic_clock_from_path(tb, columns, ncolumns, dynamic_clock->path, true);
        }
 
        list_free(&dynamic_clocks, struct dynamic_clock, head, free);
index a5f2936ee9c47e2c1e459e7419999840dc371d33..48cd7d9092fc4d676d2a3f1612e1aa038f49575f 100755 (executable)
@@ -29,7 +29,7 @@ mask_timestamps() {
 
 ts_init_subtest basic
 
-"$TS_CMD_LSCLOCKS" -o TYPE,ID,CLOCK,NAME > "$TS_OUTPUT" 2>> "$TS_ERRLOG"
+"$TS_CMD_LSCLOCKS" --no-discover-dynamic -o TYPE,ID,CLOCK,NAME > "$TS_OUTPUT" 2>> "$TS_ERRLOG"
 
 ts_finalize_subtest
 
@@ -42,7 +42,7 @@ ts_finalize_subtest
 ts_init_subtest dynamic
 
 if [ -c /dev/ptp0 ] && [ -r /dev/ptp0 ]; then
-       "$TS_CMD_LSCLOCKS" --dynamic-clock /dev/ptp0 --output TYPE,ID,CLOCK,NAME \
+       "$TS_CMD_LSCLOCKS" --no-discover-dynamic --dynamic-clock /dev/ptp0 --output TYPE,ID,CLOCK,NAME \
                | tail -1 > "$TS_OUTPUT" 2>> "$TS_ERRLOG"
        ts_finalize_subtest
 else