From 67c925f45944bb992a9c7edf1133aedd33a75573 Mon Sep 17 00:00:00 2001 From: Ken ICHIKAWA Date: Thu, 6 Jun 2013 17:17:43 +0900 Subject: [PATCH] Don't return bogus clock id phc_open() can open any device and return clkid even if the device is not phc for example /dev/kvm and so on. As a result, phc2sys keeps running with reading bogus clock as below: # phc2sys -s /dev/kvm -O 0 -q -m phc2sys[687019.699]: failed to read clock: Invalid argument phc2sys[687020.699]: failed to read clock: Invalid argument phc2sys[687021.699]: failed to read clock: Invalid argument phc2sys[687022.699]: failed to read clock: Invalid argument ... This patch fixes that problem. Signed-off-by: Ken ICHIKAWA --- phc.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/phc.c b/phc.c index 4b9347c..40fa6a1 100644 --- a/phc.c +++ b/phc.c @@ -36,11 +36,25 @@ #define BITS_PER_LONG (sizeof(long)*8) #define MAX_PPB_32 32767999 /* 2^31 - 1 / 65.536 */ +static int phc_get_caps(clockid_t clkid, struct ptp_clock_caps *caps); + clockid_t phc_open(char *phc) { + clockid_t clkid; + struct ptp_clock_caps caps; int fd = open(phc, O_RDWR); - return fd < 0 ? CLOCK_INVALID : FD_TO_CLOCKID(fd); + if (fd < 0) + return CLOCK_INVALID; + + clkid = FD_TO_CLOCKID(fd); + /* check if clkid is valid */ + if (phc_get_caps(clkid, &caps)) { + close(fd); + return CLOCK_INVALID; + } + + return clkid; } void phc_close(clockid_t clkid)