posix-stubs.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 t64;
  106. struct timespec t;
  107. switch (which_clock) {
  108. case CLOCK_REALTIME:
  109. case CLOCK_MONOTONIC:
  110. case CLOCK_BOOTTIME:
  111. break;
  112. default:
  113. return -EINVAL;
  114. }
  115. if (copy_from_user(&t, rqtp, sizeof (struct timespec)))
  116. return -EFAULT;
  117. t64 = timespec_to_timespec64(t);
  118. if (!timespec64_valid(&t64))
  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(&t64, 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 t64;
  180. struct timespec t;
  181. switch (which_clock) {
  182. case CLOCK_REALTIME:
  183. case CLOCK_MONOTONIC:
  184. case CLOCK_BOOTTIME:
  185. break;
  186. default:
  187. return -EINVAL;
  188. }
  189. if (compat_get_timespec(&t, rqtp))
  190. return -EFAULT;
  191. t64 = timespec_to_timespec64(t);
  192. if (!timespec64_valid(&t64))
  193. return -EINVAL;
  194. if (flags & TIMER_ABSTIME)
  195. rmtp = NULL;
  196. current->restart_block.nanosleep.type = rmtp ? TT_COMPAT : TT_NONE;
  197. current->restart_block.nanosleep.compat_rmtp = rmtp;
  198. return hrtimer_nanosleep(&t64, flags & TIMER_ABSTIME ?
  199. HRTIMER_MODE_ABS : HRTIMER_MODE_REL,
  200. which_clock);
  201. }
  202. #endif