You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
2.5 KiB

  1. From 46da4c4e090e0412cee0777f1e8b219964781da7 Mon Sep 17 00:00:00 2001
  2. From: Eneas U de Queiroz <cotequeiroz@gmail.com>
  3. Date: Fri, 8 Oct 2021 14:39:52 -0300
  4. Subject: [PATCH] Avoid problems with 64-bit time_t
  5. The clock_gettime() calls are being handled by calling
  6. syscall(__NR_clock_gettime, ...), which is not portable between systems
  7. using 32-bit and 64-bit time_t. This is being done to avoid having to
  8. link agains librt.
  9. So, use the standard function, and add a test to see if we can compile
  10. a test without the library, including it otherwise.
  11. Signed-off-by: Eneas U de Queiroz <cotequeiroz@gmail.com>
  12. ---
  13. Makefile.flags | 6 ++++++
  14. coreutils/date.c | 6 ++----
  15. libbb/time.c | 2 +-
  16. 3 files changed, 9 insertions(+), 5 deletions(-)
  17. --- a/Makefile.flags
  18. +++ b/Makefile.flags
  19. @@ -124,6 +124,12 @@ CFLAGS += --sysroot=$(CONFIG_SYSROOT)
  20. export SYSROOT=$(CONFIG_SYSROOT)
  21. endif
  22. +# glibc versions before 2.17 need to link with -rt to use clock_gettime
  23. +RT_NEEDED := $(shell echo 'int main(void){struct timespec tp; return clock_gettime(CLOCK_MONOTONIC, &tp);}' >rttest.c; $(CC) $(CFLAGS) -include time.h -o /dev/null rttest.c >/dev/null 2>&1 || echo "y"; rm rttest.c)
  24. +ifeq ($(RT_NEEDED),y)
  25. +LDLIBS += rt
  26. +endif
  27. +
  28. # Android has no separate crypt library
  29. # gcc-4.2.1 fails if we try to feed C source on stdin:
  30. # echo 'int main(void){return 0;}' | $(CC) $(CFLAGS) -lcrypt -o /dev/null -xc -
  31. --- a/coreutils/date.c
  32. +++ b/coreutils/date.c
  33. @@ -37,7 +37,7 @@
  34. //config:config FEATURE_DATE_NANO
  35. //config: bool "Support %[num]N nanosecond format specifier"
  36. //config: default n
  37. -//config: depends on DATE # syscall(__NR_clock_gettime)
  38. +//config: depends on DATE # clock_gettime()
  39. //config: select PLATFORM_LINUX
  40. //config: help
  41. //config: Support %[num]N format specifier. Adds ~250 bytes of code.
  42. @@ -265,9 +265,7 @@ int date_main(int argc UNUSED_PARAM, cha
  43. #endif
  44. } else {
  45. #if ENABLE_FEATURE_DATE_NANO
  46. - /* libc has incredibly messy way of doing this,
  47. - * typically requiring -lrt. We just skip all this mess */
  48. - syscall(__NR_clock_gettime, CLOCK_REALTIME, &ts);
  49. + clock_gettime(CLOCK_REALTIME, &ts);
  50. #else
  51. time(&ts.tv_sec);
  52. #endif
  53. --- a/libbb/time.c
  54. +++ b/libbb/time.c
  55. @@ -243,7 +243,7 @@ char* FAST_FUNC strftime_YYYYMMDDHHMMSS(
  56. * typically requiring -lrt. We just skip all this mess */
  57. static void get_mono(struct timespec *ts)
  58. {
  59. - if (syscall(__NR_clock_gettime, CLOCK_MONOTONIC, ts))
  60. + if (clock_gettime(CLOCK_MONOTONIC, ts))
  61. bb_error_msg_and_die("clock_gettime(MONOTONIC) failed");
  62. }
  63. unsigned long long FAST_FUNC monotonic_ns(void)