posix-stubs.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. #ifdef CONFIG_ARCH_HAS_SYSCALL_WRAPPER
  21. /* Architectures may override SYS_NI and COMPAT_SYS_NI */
  22. #include <asm/syscall_wrapper.h>
  23. #endif
  24. asmlinkage long sys_ni_posix_timers(void)
  25. {
  26. pr_err_once("process %d (%s) attempted a POSIX timer syscall "
  27. "while CONFIG_POSIX_TIMERS is not set\n",
  28. current->pid, current->comm);
  29. return -ENOSYS;
  30. }
  31. #ifndef SYS_NI
  32. #define SYS_NI(name) SYSCALL_ALIAS(sys_##name, sys_ni_posix_timers)
  33. #endif
  34. #ifndef COMPAT_SYS_NI
  35. #define COMPAT_SYS_NI(name) SYSCALL_ALIAS(compat_sys_##name, sys_ni_posix_timers)
  36. #endif
  37. SYS_NI(timer_create);
  38. SYS_NI(timer_gettime);
  39. SYS_NI(timer_getoverrun);
  40. SYS_NI(timer_settime);
  41. SYS_NI(timer_delete);
  42. SYS_NI(clock_adjtime);
  43. SYS_NI(getitimer);
  44. SYS_NI(setitimer);
  45. #ifdef __ARCH_WANT_SYS_ALARM
  46. SYS_NI(alarm);
  47. #endif
  48. /*
  49. * We preserve minimal support for CLOCK_REALTIME and CLOCK_MONOTONIC
  50. * as it is easy to remain compatible with little code. CLOCK_BOOTTIME
  51. * is also included for convenience as at least systemd uses it.
  52. */
  53. SYSCALL_DEFINE2(clock_settime, const clockid_t, which_clock,
  54. const struct __kernel_timespec __user *, tp)
  55. {
  56. struct timespec64 new_tp;
  57. if (which_clock != CLOCK_REALTIME)
  58. return -EINVAL;
  59. if (get_timespec64(&new_tp, tp))
  60. return -EFAULT;
  61. return do_sys_settimeofday64(&new_tp, NULL);
  62. }
  63. int do_clock_gettime(clockid_t which_clock, struct timespec64 *tp)
  64. {
  65. switch (which_clock) {
  66. case CLOCK_REALTIME:
  67. ktime_get_real_ts64(tp);
  68. break;
  69. case CLOCK_MONOTONIC:
  70. ktime_get_ts64(tp);
  71. break;
  72. case CLOCK_BOOTTIME:
  73. ktime_get_boottime_ts64(tp);
  74. break;
  75. default:
  76. return -EINVAL;
  77. }
  78. return 0;
  79. }
  80. SYSCALL_DEFINE2(clock_gettime, const clockid_t, which_clock,
  81. struct __kernel_timespec __user *, tp)
  82. {
  83. int ret;
  84. struct timespec64 kernel_tp;
  85. ret = do_clock_gettime(which_clock, &kernel_tp);
  86. if (ret)
  87. return ret;
  88. if (put_timespec64(&kernel_tp, tp))
  89. return -EFAULT;
  90. return 0;
  91. }
  92. SYSCALL_DEFINE2(clock_getres, const clockid_t, which_clock, struct __kernel_timespec __user *, tp)
  93. {
  94. struct timespec64 rtn_tp = {
  95. .tv_sec = 0,
  96. .tv_nsec = hrtimer_resolution,
  97. };
  98. switch (which_clock) {
  99. case CLOCK_REALTIME:
  100. case CLOCK_MONOTONIC:
  101. case CLOCK_BOOTTIME:
  102. if (put_timespec64(&rtn_tp, tp))
  103. return -EFAULT;
  104. return 0;
  105. default:
  106. return -EINVAL;
  107. }
  108. }
  109. SYSCALL_DEFINE4(clock_nanosleep, const clockid_t, which_clock, int, flags,
  110. const struct __kernel_timespec __user *, rqtp,
  111. struct __kernel_timespec __user *, rmtp)
  112. {
  113. struct timespec64 t;
  114. switch (which_clock) {
  115. case CLOCK_REALTIME:
  116. case CLOCK_MONOTONIC:
  117. case CLOCK_BOOTTIME:
  118. break;
  119. default:
  120. return -EINVAL;
  121. }
  122. if (get_timespec64(&t, rqtp))
  123. return -EFAULT;
  124. if (!timespec64_valid(&t))
  125. return -EINVAL;
  126. if (flags & TIMER_ABSTIME)
  127. rmtp = NULL;
  128. current->restart_block.nanosleep.type = rmtp ? TT_NATIVE : TT_NONE;
  129. current->restart_block.nanosleep.rmtp = rmtp;
  130. return hrtimer_nanosleep(&t, flags & TIMER_ABSTIME ?
  131. HRTIMER_MODE_ABS : HRTIMER_MODE_REL,
  132. which_clock);
  133. }
  134. #ifdef CONFIG_COMPAT
  135. COMPAT_SYS_NI(timer_create);
  136. COMPAT_SYS_NI(clock_adjtime);
  137. COMPAT_SYS_NI(timer_settime);
  138. COMPAT_SYS_NI(timer_gettime);
  139. COMPAT_SYS_NI(getitimer);
  140. COMPAT_SYS_NI(setitimer);
  141. #endif
  142. #ifdef CONFIG_COMPAT_32BIT_TIME
  143. COMPAT_SYSCALL_DEFINE2(clock_settime, const clockid_t, which_clock,
  144. struct compat_timespec __user *, tp)
  145. {
  146. struct timespec64 new_tp;
  147. if (which_clock != CLOCK_REALTIME)
  148. return -EINVAL;
  149. if (compat_get_timespec64(&new_tp, tp))
  150. return -EFAULT;
  151. return do_sys_settimeofday64(&new_tp, NULL);
  152. }
  153. COMPAT_SYSCALL_DEFINE2(clock_gettime, clockid_t, which_clock,
  154. struct compat_timespec __user *, tp)
  155. {
  156. int ret;
  157. struct timespec64 kernel_tp;
  158. ret = do_clock_gettime(which_clock, &kernel_tp);
  159. if (ret)
  160. return ret;
  161. if (compat_put_timespec64(&kernel_tp, tp))
  162. return -EFAULT;
  163. return 0;
  164. }
  165. COMPAT_SYSCALL_DEFINE2(clock_getres, clockid_t, which_clock,
  166. struct compat_timespec __user *, tp)
  167. {
  168. struct timespec64 rtn_tp = {
  169. .tv_sec = 0,
  170. .tv_nsec = hrtimer_resolution,
  171. };
  172. switch (which_clock) {
  173. case CLOCK_REALTIME:
  174. case CLOCK_MONOTONIC:
  175. case CLOCK_BOOTTIME:
  176. if (compat_put_timespec64(&rtn_tp, tp))
  177. return -EFAULT;
  178. return 0;
  179. default:
  180. return -EINVAL;
  181. }
  182. }
  183. COMPAT_SYSCALL_DEFINE4(clock_nanosleep, clockid_t, which_clock, int, flags,
  184. struct compat_timespec __user *, rqtp,
  185. struct compat_timespec __user *, rmtp)
  186. {
  187. struct timespec64 t;
  188. switch (which_clock) {
  189. case CLOCK_REALTIME:
  190. case CLOCK_MONOTONIC:
  191. case CLOCK_BOOTTIME:
  192. break;
  193. default:
  194. return -EINVAL;
  195. }
  196. if (compat_get_timespec64(&t, rqtp))
  197. return -EFAULT;
  198. if (!timespec64_valid(&t))
  199. return -EINVAL;
  200. if (flags & TIMER_ABSTIME)
  201. rmtp = NULL;
  202. current->restart_block.nanosleep.type = rmtp ? TT_COMPAT : TT_NONE;
  203. current->restart_block.nanosleep.compat_rmtp = rmtp;
  204. return hrtimer_nanosleep(&t, flags & TIMER_ABSTIME ?
  205. HRTIMER_MODE_ABS : HRTIMER_MODE_REL,
  206. which_clock);
  207. }
  208. #endif