From 380d023abb1fdce0dba9d58ca1abaf2e2de5488f Mon Sep 17 00:00:00 2001 From: Vladimir Oltean Date: Tue, 18 Aug 2020 02:19:29 +0300 Subject: [PATCH] posix_clock_open: derive PHC index from device name if possible Currently the PHC index is retrieved only through an ethtool ioctl if the PHC is specified as an Ethernet interface. If it's a char device such as /dev/ptp5, the phc_index will remain unpopulated. Try to infer it from the char device's path. This is useful when trying to determine whether multiple clocks are in fact the same (such as /dev/ptp3 and sw1p3), just compare their PHC index. Signed-off-by: Vladimir Oltean --- util.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/util.c b/util.c index 296dd59..027d694 100644 --- a/util.c +++ b/util.c @@ -211,6 +211,16 @@ clockid_t posix_clock_open(const char *device, int *phc_index) /* check if device is valid phc device */ clkid = phc_open(device); if (clkid != CLOCK_INVALID) { + if (!strncmp(device, "/dev/ptp", strlen("/dev/ptp"))) { + int r = get_ranged_int(device + strlen("/dev/ptp"), + phc_index, 0, 65535); + if (r) { + fprintf(stderr, + "failed to parse PHC index from %s\n", + device); + return -1; + } + } return clkid; } /* check if device is a valid ethernet device */