posix-stubs.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. default:
  68. return -EINVAL;
  69. }
  70. return 0;
  71. }
  72. SYSCALL_DEFINE2(clock_gettime, const clockid_t, which_clock,
  73. struct timespec __user *, tp)
  74. {
  75. int ret;
  76. struct timespec64 kernel_tp;
  77. ret = do_clock_gettime(which_clock, &kernel_tp);
  78. if (ret)
  79. return ret;
  80. if (put_timespec64(&kernel_tp, tp))
  81. return -EFAULT;
  82. return 0;
  83. }
  84. SYSCALL_DEFINE2(clock_getres, const clockid_t, which_clock, struct timespec __user *, tp)
  85. {
  86. struct timespec64 rtn_tp = {
  87. .tv_sec = 0,
  88. .tv_nsec = hrtimer_resolution,
  89. };
  90. switch (which_clock) {
  91. case CLOCK_REALTIME:
  92. case CLOCK_MONOTONIC:
  93. case CLOCK_BOOTTIME:
  94. if (put_timespec64(&rtn_tp, tp))
  95. return -EFAULT;
  96. return 0;
  97. default:
  98. return -EINVAL;
  99. }
  100. }
  101. SYSCALL_DEFINE4(clock_nanosleep, const clockid_t, which_clock, int, flags,
  102. const struct timespec __user *, rqtp,
  103. struct timespec __user *, rmtp)
  104. {
  105. struct timespec64 t;
  106. switch (which_clock) {
  107. case CLOCK_REALTIME:
  108. case CLOCK_MONOTONIC:
  109. case CLOCK_BOOTTIME:
  110. break;
  111. default:
  112. return -EINVAL;
  113. }
  114. if (get_timespec64(&t, rqtp))
  115. return -EFAULT;
  116. if (!timespec64_valid(&t))
  117. return -EINVAL;
  118. if (flags & TIMER_ABSTIME)
  119. rmtp = NULL;
  120. current->restart_block.nanosleep.type = rmtp ? TT_NATIVE : TT_NONE;
  121. current->restart_block.nanosleep.rmtp = rmtp;
  122. return hrtimer_nanosleep(&t, flags & TIMER_ABSTIME ?
  123. HRTIMER_MODE_ABS : HRTIMER_MODE_REL,
  124. which_clock);
  125. }
  126. #ifdef CONFIG_COMPAT
  127. COMPAT_SYS_NI(timer_create);
  128. COMPAT_SYS_NI(clock_adjtime);
  129. COMPAT_SYS_NI(timer_settime);
  130. COMPAT_SYS_NI(timer_gettime);
  131. COMPAT_SYS_NI(getitimer);
  132. COMPAT_SYS_NI(setitimer);
  133. COMPAT_SYSCALL_DEFINE2(clock_settime, const clockid_t, which_clock,
  134. struct compat_timespec __user *, tp)
  135. {
  136. struct timespec64 new_tp;
  137. if (which_clock != CLOCK_REALTIME)
  138. return -EINVAL;
  139. if (compat_get_timespec64(&new_tp, tp))
  140. return -EFAULT;
  141. return do_sys_settimeofday64(&new_tp, NULL);
  142. }
  143. COMPAT_SYSCALL_DEFINE2(clock_gettime, clockid_t, which_clock,
  144. struct compat_timespec __user *, tp)
  145. {
  146. int ret;
  147. struct timespec64 kernel_tp;
  148. ret = do_clock_gettime(which_clock, &kernel_tp);
  149. if (ret)
  150. return ret;
  151. if (compat_put_timespec64(&kernel_tp, tp))
  152. return -EFAULT;
  153. return 0;
  154. }
  155. COMPAT_SYSCALL_DEFINE2(clock_getres, clockid_t, which_clock,
  156. struct compat_timespec __user *, tp)
  157. {
  158. struct timespec64 rtn_tp = {
  159. .tv_sec = 0,
  160. .tv_nsec = hrtimer_resolution,
  161. };
  162. switch (which_clock) {
  163. case CLOCK_REALTIME:
  164. case CLOCK_MONOTONIC:
  165. case CLOCK_BOOTTIME:
  166. if (compat_put_timespec64(&rtn_tp, tp))
  167. return -EFAULT;
  168. return 0;
  169. default:
  170. return -EINVAL;
  171. }
  172. }
  173. COMPAT_SYSCALL_DEFINE4(clock_nanosleep, clockid_t, which_clock, int, flags,
  174. struct compat_timespec __user *, rqtp,
  175. struct compat_timespec __user *, rmtp)
  176. {
  177. struct timespec64 t;
  178. switch (which_clock) {
  179. case CLOCK_REALTIME:
  180. case CLOCK_MONOTONIC:
  181. case CLOCK_BOOTTIME:
  182. break;
  183. default:
  184. return -EINVAL;
  185. }
  186. if (compat_get_timespec64(&t, rqtp))
  187. return -EFAULT;
  188. if (!timespec64_valid(&t))
  189. return -EINVAL;
  190. if (flags & TIMER_ABSTIME)
  191. rmtp = NULL;
  192. current->restart_block.nanosleep.type = rmtp ? TT_COMPAT : TT_NONE;
  193. current->restart_block.nanosleep.compat_rmtp = rmtp;
  194. return hrtimer_nanosleep(&t, flags & TIMER_ABSTIME ?
  195. HRTIMER_MODE_ABS : HRTIMER_MODE_REL,
  196. which_clock);
  197. }
  198. #endif