posix-stubs.c 5.2 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 27 28 29 30

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;
}

#define SYS_NI(name)  SYSCALL_ALIAS(sys_##name, sys_ni_posix_timers)
31
#define COMPAT_SYS_NI(name)  SYSCALL_ALIAS(compat_sys_##name, sys_ni_posix_timers)
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53

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)
{
54
	struct timespec64 new_tp;
55 56 57

	if (which_clock != CLOCK_REALTIME)
		return -EINVAL;
58
	if (get_timespec64(&new_tp, tp))
59
		return -EFAULT;
60

61
	return do_sys_settimeofday64(&new_tp, NULL);
62 63
}

64
int do_clock_gettime(clockid_t which_clock, struct timespec64 *tp)
65 66
{
	switch (which_clock) {
67 68 69 70 71 72 73 74 75 76 77
	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;
78
	}
79

80 81 82 83 84 85 86 87 88 89 90 91 92
	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))
93 94 95 96 97 98
		return -EFAULT;
	return 0;
}

SYSCALL_DEFINE2(clock_getres, const clockid_t, which_clock, struct timespec __user *, tp)
{
99
	struct timespec64 rtn_tp = {
100 101 102 103 104 105 106 107
		.tv_sec = 0,
		.tv_nsec = hrtimer_resolution,
	};

	switch (which_clock) {
	case CLOCK_REALTIME:
	case CLOCK_MONOTONIC:
	case CLOCK_BOOTTIME:
108
		if (put_timespec64(&rtn_tp, tp))
109 110 111 112 113 114 115 116 117 118 119
			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)
{
120
	struct timespec64 t64;
121 122 123 124 125 126
	struct timespec t;

	switch (which_clock) {
	case CLOCK_REALTIME:
	case CLOCK_MONOTONIC:
	case CLOCK_BOOTTIME:
127
		break;
128 129 130
	default:
		return -EINVAL;
	}
131 132 133 134 135 136 137 138 139 140 141 142 143

	if (copy_from_user(&t, rqtp, sizeof (struct timespec)))
		return -EFAULT;
	t64 = timespec_to_timespec64(t);
	if (!timespec64_valid(&t64))
		return -EINVAL;
	if (flags & TIMER_ABSTIME)
		rmtp = NULL;
	current->restart_block.nanosleep.type = rmtp ? TT_NATIVE : TT_NONE;
	current->restart_block.nanosleep.rmtp = rmtp;
	return hrtimer_nanosleep(&t64, flags & TIMER_ABSTIME ?
				 HRTIMER_MODE_ABS : HRTIMER_MODE_REL,
				 which_clock);
144 145 146
}

#ifdef CONFIG_COMPAT
147 148 149 150 151 152 153
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);

154 155 156
COMPAT_SYSCALL_DEFINE2(clock_settime, const clockid_t, which_clock,
		       struct compat_timespec __user *, tp)
{
157
	struct timespec64 new_tp;
158 159 160

	if (which_clock != CLOCK_REALTIME)
		return -EINVAL;
161
	if (compat_get_timespec64(&new_tp, tp))
162 163
		return -EFAULT;

164
	return do_sys_settimeofday64(&new_tp, NULL);
165 166
}

167 168
COMPAT_SYSCALL_DEFINE2(clock_gettime, clockid_t, which_clock,
		       struct compat_timespec __user *, tp)
169
{
170 171
	int ret;
	struct timespec64 kernel_tp;
172

173 174 175
	ret = do_clock_gettime(which_clock, &kernel_tp);
	if (ret)
		return ret;
176

177
	if (compat_put_timespec64(&kernel_tp, tp))
178 179 180 181
		return -EFAULT;
	return 0;
}

182
COMPAT_SYSCALL_DEFINE2(clock_getres, clockid_t, which_clock,
183 184
		       struct compat_timespec __user *, tp)
{
185
	struct timespec64 rtn_tp = {
186 187 188 189 190 191 192 193
		.tv_sec = 0,
		.tv_nsec = hrtimer_resolution,
	};

	switch (which_clock) {
	case CLOCK_REALTIME:
	case CLOCK_MONOTONIC:
	case CLOCK_BOOTTIME:
194
		if (compat_put_timespec64(&rtn_tp, tp))
195 196 197 198 199 200
			return -EFAULT;
		return 0;
	default:
		return -EINVAL;
	}
}
201

202 203 204
COMPAT_SYSCALL_DEFINE4(clock_nanosleep, clockid_t, which_clock, int, flags,
		       struct compat_timespec __user *, rqtp,
		       struct compat_timespec __user *, rmtp)
205
{
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
	struct timespec64 t64;
	struct timespec t;

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

	if (compat_get_timespec(&t, rqtp))
		return -EFAULT;
	t64 = timespec_to_timespec64(t);
	if (!timespec64_valid(&t64))
		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;
	return hrtimer_nanosleep(&t64, flags & TIMER_ABSTIME ?
				 HRTIMER_MODE_ABS : HRTIMER_MODE_REL,
				 which_clock);
230 231
}
#endif