From: Thomas Weißschuh Date: Tue, 11 Jul 2023 11:36:19 +0000 (+0200) Subject: lsclocks: automatically discover dynamic clocks X-Git-Tag: v2.40-rc1~332^2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=6fcb27e7193e9b9292ee7bfb2d67af67e6018408;p=thirdparty%2Futil-linux.git lsclocks: automatically discover dynamic clocks Signed-off-by: Thomas Weißschuh --- diff --git a/misc-utils/lsclocks.1.adoc b/misc-utils/lsclocks.1.adoc index 644887bacb..90497fba52 100644 --- a/misc-utils/lsclocks.1.adoc +++ b/misc-utils/lsclocks.1.adoc @@ -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. diff --git a/misc-utils/lsclocks.c b/misc-utils/lsclocks.c index a0fe55bd4d..ec27b9a155 100644 --- a/misc-utils/lsclocks.c +++ b/misc-utils/lsclocks.c @@ -23,6 +23,7 @@ #include #include #include +#include #include @@ -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 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 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); diff --git a/tests/ts/misc/lsclocks b/tests/ts/misc/lsclocks index a5f2936ee9..48cd7d9092 100755 --- a/tests/ts/misc/lsclocks +++ b/tests/ts/misc/lsclocks @@ -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