posix-stubs.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. * Dummy stubs used when CONFIG_POSIX_TIMERS=n
  3. *
  4. * Created by: Nicolas Pitre, July 2016
  5. * Copyright: (C) 2016 Linaro Limited
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/linkage.h>
  12. #include <linux/kernel.h>
  13. #include <linux/sched.h>
  14. #include <linux/errno.h>
  15. #include <linux/syscalls.h>
  16. #include <linux/ktime.h>
  17. #include <linux/timekeeping.h>
  18. #include <linux/posix-timers.h>
  19. #include <linux/compat.h>
  20. asmlinkage long sys_ni_posix_timers(void)
  21. {
  22. pr_err_once("process %d (%s) attempted a POSIX timer syscall "
  23. "while CONFIG_POSIX_TIMERS is not set\n",
  24. current->pid, current->comm);
  25. return -ENOSYS;
  26. }
  27. #define SYS_NI(name) SYSCALL_ALIAS(sys_##name, sys_ni_posix_timers)
  28. #define COMPAT_SYS_NI(name) SYSCALL_ALIAS(compat_sys_##name, sys_ni_posix_timers)
  29. SYS_NI(timer_create);
  30. SYS_NI(timer_gettime);
  31. SYS_NI(timer_getoverrun);
  32. SYS_NI(timer_settime);
  33. SYS_NI(timer_delete);
  34. SYS_NI(clock_adjtime);
  35. SYS_NI(getitimer);
  36. SYS_NI(setitimer);
  37. #ifdef __ARCH_WANT_SYS_ALARM
  38. SYS_NI(alarm);
  39. #endif
  40. /*
  41. * We preserve minimal support for CLOCK_REALTIME and CLOCK_MONOTONIC
  42. * as it is easy to remain compatible with little code. CLOCK_BOOTTIME
  43. * is also included for convenience as at least systemd uses it.
  44. */
  45. SYSCALL_DEFINE2(clock_settime, const clockid_t, which_clock,
  46. const struct timespec __user *, tp)
  47. {
  48. struct timespec64 new_tp;
  49. if (which_clock != CLOCK_REALTIME)
  50. return -EINVAL;
  51. if (get_timespec64(&new_tp, tp))
  52. return -EFAULT;
  53. return do_sys_settimeofday64(&new_tp, NULL);
  54. }
  55. int do_clock_gettime(clockid_t which_clock, struct timespec64 *tp)
  56. {
  57. switch (which_clock) {
  58. case CLOCK_REALTIME:
  59. ktime_get_real_ts64(tp);
  60. break;
  61. case CLOCK_MONOTONIC:
  62. ktime_get_ts64(tp);
  63. break;
  64. case CLOCK_BOOTTIME:
  65. get_monotonic_boottime64(tp);
  66. break;
  67. case CLOCK_MONOTONIC_ACTIVE:
  68. ktime_get_active_ts64(tp);
  69. default:
  70. return -EINVAL;
  71. }
  72. return 0;
  73. }
  74. SYSCALL_DEFINE2(clock_gettime, const clockid_t, which_clock,
  75. struct timespec __user *, tp)
  76. {
  77. int ret;
  78. struct timespec64 kernel_tp;
  79. ret = do_clock_gettime(which_clock, &kernel_tp);
  80. if (ret)
  81. return ret;
  82. if (put_timespec64(&kernel_tp, tp))
  83. return -EFAULT;
  84. return 0;
  85. }
  86. SYSCALL_DEFINE2(clock_getres, const clockid_t, which_clock, struct timespec __user *, tp)
  87. {
  88. struct timespec64 rtn_tp = {
  89. .tv_sec = 0,
  90. .tv_nsec = hrtimer_resolution,
  91. };
  92. switch (which_clock) {
  93. case CLOCK_REALTIME:
  94. case CLOCK_MONOTONIC:
  95. case CLOCK_BOOTTIME:
  96. if (put_timespec64(&rtn_tp, tp))
  97. return -EFAULT;
  98. return 0;
  99. default:
  100. return -EINVAL;
  101. }
  102. }
  103. SYSCALL_DEFINE4(clock_nanosleep, const clockid_t, which_clock, int, flags,
  104. const struct timespec __user *, rqtp,
  105. struct timespec __user *, rmtp)
  106. {
  107. struct timespec64 t;
  108. switch (which_clock) {
  109. case CLOCK_REALTIME:
  110. case CLOCK_MONOTONIC:
  111. case CLOCK_BOOTTIME:
  112. break;
  113. default:
  114. return -EINVAL;
  115. }
  116. if (get_timespec64(&t, rqtp))
  117. return -EFAULT;
  118. if (!timespec64_valid(&t))
  119. return -EINVAL;
  120. if (flags & TIMER_ABSTIME)
  121. rmtp = NULL;
  122. current->restart_block.nanosleep.type = rmtp ? TT_NATIVE : TT_NONE;
  123. current->restart_block.nanosleep.rmtp = rmtp;
  124. return hrtimer_nanosleep(&t, flags & TIMER_ABSTIME ?
  125. HRTIMER_MODE_ABS : HRTIMER_MODE_REL,
  126. which_clock);
  127. }
  128. #ifdef CONFIG_COMPAT
  129. COMPAT_SYS_NI(timer_create);
  130. COMPAT_SYS_NI(clock_adjtime);
  131. COMPAT_SYS_NI(timer_settime);
  132. COMPAT_SYS_NI(timer_gettime);
  133. COMPAT_SYS_NI(getitimer);
  134. COMPAT_SYS_NI(setitimer);
  135. COMPAT_SYSCALL_DEFINE2(clock_settime, const clockid_t, which_clock,
  136. struct compat_timespec __user *, tp)
  137. {
  138. struct timespec64 new_tp;
  139. if (which_clock != CLOCK_REALTIME)
  140. return -EINVAL;
  141. if (compat_get_timespec64(&new_tp, tp))
  142. return -EFAULT;
  143. return do_sys_settimeofday64(&new_tp, NULL);
  144. }
  145. COMPAT_SYSCALL_DEFINE2(clock_gettime, clockid_t, which_clock,
  146. struct compat_timespec __user *, tp)
  147. {
  148. int ret;
  149. struct timespec64 kernel_tp;
  150. ret = do_clock_gettime(which_clock, &kernel_tp);
  151. if (ret)
  152. return ret;
  153. if (compat_put_timespec64(&kernel_tp, tp))
  154. return -EFAULT;
  155. return 0;
  156. }
  157. COMPAT_SYSCALL_DEFINE2(clock_getres, clockid_t, which_clock,
  158. struct compat_timespec __user *, tp)
  159. {
  160. struct timespec64 rtn_tp = {
  161. .tv_sec = 0,
  162. .tv_nsec = hrtimer_resolution,
  163. };
  164. switch (which_clock) {
  165. case CLOCK_REALTIME:
  166. case CLOCK_MONOTONIC:
  167. case CLOCK_BOOTTIME:
  168. if (compat_put_timespec64(&rtn_tp, tp))
  169. return -EFAULT;
  170. return 0;
  171. default:
  172. return -EINVAL;
  173. }
  174. }
  175. COMPAT_SYSCALL_DEFINE4(clock_nanosleep, clockid_t, which_clock, int, flags,
  176. struct compat_timespec __user *, rqtp,
  177. struct compat_timespec __user *, rmtp)
  178. {
  179. struct timespec64 t;
  180. switch (which_clock) {
  181. case CLOCK_REALTIME:
  182. case CLOCK_MONOTONIC:
  183. case CLOCK_BOOTTIME:
  184. break;
  185. default:
  186. return -EINVAL;
  187. }
  188. if (compat_get_timespec64(&t, rqtp))
  189. return -EFAULT;
  190. if (!timespec64_valid(&t))
  191. return -EINVAL;
  192. if (flags & TIMER_ABSTIME)
  193. rmtp = NULL;
  194. current->restart_block.nanosleep.type = rmtp ? TT_COMPAT : TT_NONE;
  195. current->restart_block.nanosleep.compat_rmtp = rmtp;
  196. return hrtimer_nanosleep(&t, flags & TIMER_ABSTIME ?
  197. HRTIMER_MODE_ABS : HRTIMER_MODE_REL,
  198. which_clock);
  199. }
  200. #endif