posix-stubs.c 5.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * Dummy stubs used when CONFIG_POSIX_TIMERS=n
 *
 * Created by:  Nicolas Pitre, July 2016
 * Copyright:   (C) 2016 Linaro Limited
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#include <linux/linkage.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/errno.h>
#include <linux/syscalls.h>
#include <linux/ktime.h>
#include <linux/timekeeping.h>
#include <linux/posix-timers.h>
20
#include <linux/compat.h>
21

22 23 24 25 26
#ifdef CONFIG_ARCH_HAS_SYSCALL_WRAPPER
/* Architectures may override SYS_NI and COMPAT_SYS_NI */
#include <asm/syscall_wrapper.h>
#endif

27 28 29 30 31 32 33 34
asmlinkage long sys_ni_posix_timers(void)
{
	pr_err_once("process %d (%s) attempted a POSIX timer syscall "
		    "while CONFIG_POSIX_TIMERS is not set\n",
		    current->pid, current->comm);
	return -ENOSYS;
}

35
#ifndef SYS_NI
36
#define SYS_NI(name)  SYSCALL_ALIAS(sys_##name, sys_ni_posix_timers)
37 38 39
#endif

#ifndef COMPAT_SYS_NI
40
#define COMPAT_SYS_NI(name)  SYSCALL_ALIAS(compat_sys_##name, sys_ni_posix_timers)
41
#endif
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63

SYS_NI(timer_create);
SYS_NI(timer_gettime);
SYS_NI(timer_getoverrun);
SYS_NI(timer_settime);
SYS_NI(timer_delete);
SYS_NI(clock_adjtime);
SYS_NI(getitimer);
SYS_NI(setitimer);
#ifdef __ARCH_WANT_SYS_ALARM
SYS_NI(alarm);
#endif

/*
 * We preserve minimal support for CLOCK_REALTIME and CLOCK_MONOTONIC
 * as it is easy to remain compatible with little code. CLOCK_BOOTTIME
 * is also included for convenience as at least systemd uses it.
 */

SYSCALL_DEFINE2(clock_settime, const clockid_t, which_clock,
		const struct timespec __user *, tp)
{
64
	struct timespec64 new_tp;
65 66 67

	if (which_clock != CLOCK_REALTIME)
		return -EINVAL;
68
	if (get_timespec64(&new_tp, tp))
69
		return -EFAULT;
70

71
	return do_sys_settimeofday64(&new_tp, NULL);
72 73
}

74
int do_clock_gettime(clockid_t which_clock, struct timespec64 *tp)
75 76
{
	switch (which_clock) {
77 78 79 80 81 82 83 84 85 86 87
	case CLOCK_REALTIME:
		ktime_get_real_ts64(tp);
		break;
	case CLOCK_MONOTONIC:
		ktime_get_ts64(tp);
		break;
	case CLOCK_BOOTTIME:
		get_monotonic_boottime64(tp);
		break;
	default:
		return -EINVAL;
88
	}
89

90 91 92 93 94 95 96 97 98 99 100 101 102
	return 0;
}
SYSCALL_DEFINE2(clock_gettime, const clockid_t, which_clock,
		struct timespec __user *, tp)
{
	int ret;
	struct timespec64 kernel_tp;

	ret = do_clock_gettime(which_clock, &kernel_tp);
	if (ret)
		return ret;

	if (put_timespec64(&kernel_tp, tp))
103 104 105 106 107 108
		return -EFAULT;
	return 0;
}

SYSCALL_DEFINE2(clock_getres, const clockid_t, which_clock, struct timespec __user *, tp)
{
109
	struct timespec64 rtn_tp = {
110 111 112 113 114 115 116 117
		.tv_sec = 0,
		.tv_nsec = hrtimer_resolution,
	};

	switch (which_clock) {
	case CLOCK_REALTIME:
	case CLOCK_MONOTONIC:
	case CLOCK_BOOTTIME:
118
		if (put_timespec64(&rtn_tp, tp))
119 120 121 122 123 124 125 126 127 128 129
			return -EFAULT;
		return 0;
	default:
		return -EINVAL;
	}
}

SYSCALL_DEFINE4(clock_nanosleep, const clockid_t, which_clock, int, flags,
		const struct timespec __user *, rqtp,
		struct timespec __user *, rmtp)
{
130
	struct timespec64 t;
131 132 133 134 135

	switch (which_clock) {
	case CLOCK_REALTIME:
	case CLOCK_MONOTONIC:
	case CLOCK_BOOTTIME:
136
		break;
137 138 139
	default:
		return -EINVAL;
	}
140

141
	if (get_timespec64(&t, rqtp))
142
		return -EFAULT;
143
	if (!timespec64_valid(&t))
144 145 146 147 148
		return -EINVAL;
	if (flags & TIMER_ABSTIME)
		rmtp = NULL;
	current->restart_block.nanosleep.type = rmtp ? TT_NATIVE : TT_NONE;
	current->restart_block.nanosleep.rmtp = rmtp;
149
	return hrtimer_nanosleep(&t, flags & TIMER_ABSTIME ?
150 151
				 HRTIMER_MODE_ABS : HRTIMER_MODE_REL,
				 which_clock);
152 153 154
}

#ifdef CONFIG_COMPAT
155 156 157 158 159 160 161
COMPAT_SYS_NI(timer_create);
COMPAT_SYS_NI(clock_adjtime);
COMPAT_SYS_NI(timer_settime);
COMPAT_SYS_NI(timer_gettime);
COMPAT_SYS_NI(getitimer);
COMPAT_SYS_NI(setitimer);

162 163 164
COMPAT_SYSCALL_DEFINE2(clock_settime, const clockid_t, which_clock,
		       struct compat_timespec __user *, tp)
{
165
	struct timespec64 new_tp;
166 167 168

	if (which_clock != CLOCK_REALTIME)
		return -EINVAL;
169
	if (compat_get_timespec64(&new_tp, tp))
170 171
		return -EFAULT;

172
	return do_sys_settimeofday64(&new_tp, NULL);
173 174
}

175 176
COMPAT_SYSCALL_DEFINE2(clock_gettime, clockid_t, which_clock,
		       struct compat_timespec __user *, tp)
177
{
178 179
	int ret;
	struct timespec64 kernel_tp;
180

181 182 183
	ret = do_clock_gettime(which_clock, &kernel_tp);
	if (ret)
		return ret;
184

185
	if (compat_put_timespec64(&kernel_tp, tp))
186 187 188 189
		return -EFAULT;
	return 0;
}

190
COMPAT_SYSCALL_DEFINE2(clock_getres, clockid_t, which_clock,
191 192
		       struct compat_timespec __user *, tp)
{
193
	struct timespec64 rtn_tp = {
194 195 196 197 198 199 200 201
		.tv_sec = 0,
		.tv_nsec = hrtimer_resolution,
	};

	switch (which_clock) {
	case CLOCK_REALTIME:
	case CLOCK_MONOTONIC:
	case CLOCK_BOOTTIME:
202
		if (compat_put_timespec64(&rtn_tp, tp))
203 204 205 206 207 208
			return -EFAULT;
		return 0;
	default:
		return -EINVAL;
	}
}
209

210 211 212
COMPAT_SYSCALL_DEFINE4(clock_nanosleep, clockid_t, which_clock, int, flags,
		       struct compat_timespec __user *, rqtp,
		       struct compat_timespec __user *, rmtp)
213
{
214
	struct timespec64 t;
215 216 217 218 219 220 221 222 223 224

	switch (which_clock) {
	case CLOCK_REALTIME:
	case CLOCK_MONOTONIC:
	case CLOCK_BOOTTIME:
		break;
	default:
		return -EINVAL;
	}

225
	if (compat_get_timespec64(&t, rqtp))
226
		return -EFAULT;
227
	if (!timespec64_valid(&t))
228 229 230 231 232
		return -EINVAL;
	if (flags & TIMER_ABSTIME)
		rmtp = NULL;
	current->restart_block.nanosleep.type = rmtp ? TT_COMPAT : TT_NONE;
	current->restart_block.nanosleep.compat_rmtp = rmtp;
233
	return hrtimer_nanosleep(&t, flags & TIMER_ABSTIME ?
234 235
				 HRTIMER_MODE_ABS : HRTIMER_MODE_REL,
				 which_clock);
236 237
}
#endif