diff mbox series

testptp: Fix compile with musl libc

Message ID 20210214170940.32358-1-hauke@hauke-m.de (mailing list archive)
State New
Headers show
Series testptp: Fix compile with musl libc | expand

Commit Message

Hauke Mehrtens Feb. 14, 2021, 5:09 p.m. UTC
Musl libc does not define the glibc specific macro __GLIBC_PREREQ(), but
it has the clock_adjtime() function. Assume that a libc implementation
which does not define __GLIBC_PREREQ at all still implements
clock_adjtime().

This fixes a build problem with musl libc because the __GLIBC_PREREQ()
macro is missing.

Fixes: 42e1358e103d ("ptp: In the testptp utility, use clock_adjtime from glibc when available")
Signed-off-by: Hauke Mehrtens <hauke@hauke-m.de>
---
 tools/testing/selftests/ptp/testptp.c | 2 ++
 1 file changed, 2 insertions(+)

Comments

Vladimir Oltean Feb. 14, 2021, 5:37 p.m. UTC | #1
Hi Hauke,

On Sun, Feb 14, 2021 at 06:09:40PM +0100, Hauke Mehrtens wrote:
> Musl libc does not define the glibc specific macro __GLIBC_PREREQ(), but
> it has the clock_adjtime() function. Assume that a libc implementation
> which does not define __GLIBC_PREREQ at all still implements
> clock_adjtime().
> 
> This fixes a build problem with musl libc because the __GLIBC_PREREQ()
> macro is missing.
> 
> Fixes: 42e1358e103d ("ptp: In the testptp utility, use clock_adjtime from glibc when available")
> Signed-off-by: Hauke Mehrtens <hauke@hauke-m.de>
> ---
>  tools/testing/selftests/ptp/testptp.c | 2 ++
>  1 file changed, 2 insertions(+)
> 
> diff --git a/tools/testing/selftests/ptp/testptp.c b/tools/testing/selftests/ptp/testptp.c
> index f7911aaeb007..ecffe2c78543 100644
> --- a/tools/testing/selftests/ptp/testptp.c
> +++ b/tools/testing/selftests/ptp/testptp.c
> @@ -38,6 +38,7 @@
>  #define NSEC_PER_SEC 1000000000LL
>  
>  /* clock_adjtime is not available in GLIBC < 2.14 */
> +#ifdef __GLIBC_PREREQ
>  #if !__GLIBC_PREREQ(2, 14)
>  #include <sys/syscall.h>
>  static int clock_adjtime(clockid_t id, struct timex *tx)
> @@ -45,6 +46,7 @@ static int clock_adjtime(clockid_t id, struct timex *tx)
>  	return syscall(__NR_clock_adjtime, id, tx);
>  }
>  #endif
> +#endif /* __GLIBC_PREREQ */

I guess this works, but as you say, there is still an assumption to be
made there, which is that all other C libraries provide the clock_adjtime
syscall definition. So it is likely that this set of #if's and #ifdef's
will be revisited again and again and ...

Maybe this is a matter of personal preference, but I wonder if it's not
actually preferable to do something like this?

#include <sys/syscall.h>

static int compat_clock_adjtime(clockid_t id, struct timex *tx)
{
	return syscall(__NR_clock_adjtime, id, tx);
}

#define clock_adjtime compat_clock_adjtime

This way, everybody uses the same definition of clock_adjtime, and we
bypass the definition provided by libc if there is one, and we provide
our own if there is none.
Hauke Mehrtens Feb. 14, 2021, 6 p.m. UTC | #2
On 2/14/21 6:37 PM, Vladimir Oltean wrote:
> Hi Hauke,
> 
> On Sun, Feb 14, 2021 at 06:09:40PM +0100, Hauke Mehrtens wrote:
>> Musl libc does not define the glibc specific macro __GLIBC_PREREQ(), but
>> it has the clock_adjtime() function. Assume that a libc implementation
>> which does not define __GLIBC_PREREQ at all still implements
>> clock_adjtime().
>>
>> This fixes a build problem with musl libc because the __GLIBC_PREREQ()
>> macro is missing.
>>
>> Fixes: 42e1358e103d ("ptp: In the testptp utility, use clock_adjtime from glibc when available")
>> Signed-off-by: Hauke Mehrtens <hauke@hauke-m.de>
>> ---
>>   tools/testing/selftests/ptp/testptp.c | 2 ++
>>   1 file changed, 2 insertions(+)
>>
>> diff --git a/tools/testing/selftests/ptp/testptp.c b/tools/testing/selftests/ptp/testptp.c
>> index f7911aaeb007..ecffe2c78543 100644
>> --- a/tools/testing/selftests/ptp/testptp.c
>> +++ b/tools/testing/selftests/ptp/testptp.c
>> @@ -38,6 +38,7 @@
>>   #define NSEC_PER_SEC 1000000000LL
>>   
>>   /* clock_adjtime is not available in GLIBC < 2.14 */
>> +#ifdef __GLIBC_PREREQ
>>   #if !__GLIBC_PREREQ(2, 14)
>>   #include <sys/syscall.h>
>>   static int clock_adjtime(clockid_t id, struct timex *tx)
>> @@ -45,6 +46,7 @@ static int clock_adjtime(clockid_t id, struct timex *tx)
>>   	return syscall(__NR_clock_adjtime, id, tx);
>>   }
>>   #endif
>> +#endif /* __GLIBC_PREREQ */
> 
> I guess this works, but as you say, there is still an assumption to be
> made there, which is that all other C libraries provide the clock_adjtime
> syscall definition. So it is likely that this set of #if's and #ifdef's
> will be revisited again and again and ...
> 
> Maybe this is a matter of personal preference, but I wonder if it's not
> actually preferable to do something like this?
> 
> #include <sys/syscall.h>
> 
> static int compat_clock_adjtime(clockid_t id, struct timex *tx)
> {
> 	return syscall(__NR_clock_adjtime, id, tx);
> }
> 
> #define clock_adjtime compat_clock_adjtime
> 
> This way, everybody uses the same definition of clock_adjtime, and we
> bypass the definition provided by libc if there is one, and we provide
> our own if there is none.
> 
Hi,

glibc 2.14 was released 1. June 2011 and musl libc also supports this 
function since version v0.9.7 28. October 2012. I would just assume that 
the libc has this function.

musl also does some special handling, probably for the 64 bit time 
handling, in this function:
https://git.musl-libc.org/cgit/musl/tree/src/linux/clock_adjtime.c#n37
I am not sure if this will also work fine with the compat function.

Hauke
Vladimir Oltean Feb. 15, 2021, 11:24 a.m. UTC | #3
On Sun, Feb 14, 2021 at 07:00:39PM +0100, Hauke Mehrtens wrote:
> glibc 2.14 was released 1. June 2011 and musl libc also supports this
> function since version v0.9.7 28. October 2012. I would just assume that the
> libc has this function.

Considering that somebody wanted it to work with a GNU libc prior to
2011, why would we not assume that somebody will eventually want it to
work with a musl libc prior to 2012?

> musl also does some special handling, probably for the 64 bit time handling,
> in this function:
> https://git.musl-libc.org/cgit/musl/tree/src/linux/clock_adjtime.c#n37
> I am not sure if this will also work fine with the compat function.

I've stared at commit
https://git.musl-libc.org/cgit/musl/commit/src/linux/clock_adjtime.c?id=2b4fd6f75b4fa66d28cddcf165ad48e8fda486d1
which added the conversion logic between struct ktimex64/struct ktimex
and struct timex, and sadly I don't understand what is the point of
wrapping the clock_adjtime function around the SYS_clock_adjtime64
syscall, when the struct timex::time member will still not be Y2038 safe
on 32-bit systems...

That being said, I didn't know that musl libc provides any wrappers
around these syscalls.
diff mbox series

Patch

diff --git a/tools/testing/selftests/ptp/testptp.c b/tools/testing/selftests/ptp/testptp.c
index f7911aaeb007..ecffe2c78543 100644
--- a/tools/testing/selftests/ptp/testptp.c
+++ b/tools/testing/selftests/ptp/testptp.c
@@ -38,6 +38,7 @@ 
 #define NSEC_PER_SEC 1000000000LL
 
 /* clock_adjtime is not available in GLIBC < 2.14 */
+#ifdef __GLIBC_PREREQ
 #if !__GLIBC_PREREQ(2, 14)
 #include <sys/syscall.h>
 static int clock_adjtime(clockid_t id, struct timex *tx)
@@ -45,6 +46,7 @@  static int clock_adjtime(clockid_t id, struct timex *tx)
 	return syscall(__NR_clock_adjtime, id, tx);
 }
 #endif
+#endif /* __GLIBC_PREREQ */
 
 static void show_flag_test(int rq_index, unsigned int flags, int err)
 {