clock_gettime.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #include <time.h>
  2. #include <errno.h>
  3. #include <stdint.h>
  4. #include "syscall.h"
  5. #include "atomic.h"
  6. #ifdef VDSO_CGT_SYM
  7. static void *volatile vdso_func;
  8. #ifdef VDSO_CGT32_SYM
  9. static void *volatile vdso_func_32;
  10. static int cgt_time32_wrap(clockid_t clk, struct timespec *ts)
  11. {
  12. long ts32[2];
  13. int (*f)(clockid_t, long[2]) =
  14. (int (*)(clockid_t, long[2]))vdso_func_32;
  15. int r = f(clk, ts32);
  16. if (!r) {
  17. /* Fallback to syscalls if time32 overflowed. Maybe
  18. * we lucked out and somehow migrated to a kernel with
  19. * time64 syscalls available. */
  20. if (ts32[0] < 0) {
  21. a_cas_p(&vdso_func, (void *)cgt_time32_wrap, 0);
  22. return -ENOSYS;
  23. }
  24. ts->tv_sec = ts32[0];
  25. ts->tv_nsec = ts32[1];
  26. }
  27. return r;
  28. }
  29. #endif
  30. static int cgt_init(clockid_t clk, struct timespec *ts)
  31. {
  32. void *p = __vdsosym(VDSO_CGT_VER, VDSO_CGT_SYM);
  33. #ifdef VDSO_CGT32_SYM
  34. if (!p) {
  35. void *q = __vdsosym(VDSO_CGT32_VER, VDSO_CGT32_SYM);
  36. if (q) {
  37. a_cas_p(&vdso_func_32, 0, q);
  38. p = cgt_time32_wrap;
  39. }
  40. }
  41. #endif
  42. int (*f)(clockid_t, struct timespec *) =
  43. (int (*)(clockid_t, struct timespec *))p;
  44. a_cas_p(&vdso_func, (void *)cgt_init, p);
  45. return f ? f(clk, ts) : -ENOSYS;
  46. }
  47. static void *volatile vdso_func = (void *)cgt_init;
  48. #endif
  49. int __clock_gettime(clockid_t clk, struct timespec *ts)
  50. {
  51. int r;
  52. #ifdef VDSO_CGT_SYM
  53. int (*f)(clockid_t, struct timespec *) =
  54. (int (*)(clockid_t, struct timespec *))vdso_func;
  55. if (f) {
  56. r = f(clk, ts);
  57. if (!r) return r;
  58. if (r == -EINVAL) return __syscall_ret(r);
  59. /* Fall through on errors other than EINVAL. Some buggy
  60. * vdso implementations return ENOSYS for clocks they
  61. * can't handle, rather than making the syscall. This
  62. * also handles the case where cgt_init fails to find
  63. * a vdso function to use. */
  64. }
  65. #endif
  66. #ifdef SYS_clock_gettime64
  67. r = -ENOSYS;
  68. if (sizeof(time_t) > 4)
  69. r = __syscall(SYS_clock_gettime64, clk, ts);
  70. if (SYS_clock_gettime == SYS_clock_gettime64 || r!=-ENOSYS)
  71. return __syscall_ret(r);
  72. long ts32[2];
  73. r = __syscall(SYS_clock_gettime, clk, ts32);
  74. if (r==-ENOSYS && clk==CLOCK_REALTIME) {
  75. r = __syscall(SYS_gettimeofday, ts32, 0);
  76. ts32[1] *= 1000;
  77. }
  78. if (!r) {
  79. ts->tv_sec = ts32[0];
  80. ts->tv_nsec = ts32[1];
  81. return r;
  82. }
  83. return __syscall_ret(r);
  84. #else
  85. r = __syscall(SYS_clock_gettime, clk, ts);
  86. if (r == -ENOSYS) {
  87. if (clk == CLOCK_REALTIME) {
  88. __syscall(SYS_gettimeofday, ts, 0);
  89. ts->tv_nsec = (int)ts->tv_nsec * 1000;
  90. return 0;
  91. }
  92. r = -EINVAL;
  93. }
  94. return __syscall_ret(r);
  95. #endif
  96. }
  97. weak_alias(__clock_gettime, clock_gettime);