notify userspace about time changes
Subsystems (7)
RFC PATCH v4 1/7 notify userspace about time changes
Changes since v3:
- broken out separate patches adding time_change_notify syscall to arm, powerpc, x86, ia64, s390 and blackfin
Changes since v2:
- replaced sysfs interface with a syscall
- added sysctl/procfs handle to set a limit to the number of users
- fixed issues pointed out by Greg.
Changes since v1:
- updated against 2.6.36-rc1,
- added notification/filtering options,
- added Documentation/ABI/sysfs-kernel-time-notify interface description.
Certain userspace applications (like "clock" desktop applets or cron) might want to be notified when some other application changes the system time. It might also be important for an application to be able to distinguish between its own and somebody else's time changes.
This patch implements a notification interface via eventfd mechanism. Proccess wishing to be notified about time changes should create an eventfd and pass it to time_change_notify() syscall along with notification options. After that, any calls to settimeofday()/stime()/adjtimex() made by other processes will be signalled to this eventfd. Credits for suggesting the eventfd mechanism for this purpose go to Kirill Shutemov.
So far, this implementation can only filter out notifications caused by time change calls made by the process that called the time_change_notify() syscall, but not its children which (might) have inherited the eventfd. It is so far not clear to me whether this is bad and more confusing than excluding such children as well.
This patch adds the syscall to asm-generic/unistd.h.
--- Documentation/time-change-notify-example.c | 64 ++++++++++ include/asm-generic/unistd.h | 4 +- include/linux/syscalls.h | 1 + include/linux/time.h | 20 +++ init/Kconfig | 7 + kernel/Makefile | 1 + kernel/sys_ni.c | 3 + kernel/sysctl.c | 11 ++ kernel/time.c | 11 ++- kernel/time_notify.c | 183 ++++++++++++++++++++++++++++ 10 files changed, 302 insertions(+), 3 deletions(-) create mode 100644 Documentation/time-change-notify-example.c create mode 100644 kernel/time_notify.c
Documentation/time-change-notify-example.c new file +64 -0
@@ -0,0 +1,64 @@ +/* +* Simple program to catch system time changes +* +* written by Alexander Shishkin <[email protected]> +*/ + +#include <stdlib.h> +#include <stdio.h> +#include <sys/eventfd.h> +#include <sys/syscall.h> +#include <errno.h> +#include <unistd.h> +#include <poll.h> + +#ifndef SYS_time_change_notify +# include "asm/unistd.h" +# ifdef __NR_time_change_notify +# define SYS_time_change_notify __NR_time_change_notify +# else +# error Cannot figure out time_change_notify syscall number. +# endif +#endif + +static int time_change_notify (int fd,unsigned int flags) +{ +return syscall (SYS_time_change_notify, fd, flags); +} + +int main (int argc,char **argv) +{ +struct pollfd fds = { .events =POLLIN }; + + fds.fd =eventfd (0 ,0 ); +if (fds.fd <0 ) { +perror ("eventfd" ); +return EXIT_FAILURE ; + } + +/* subscribe to all events from all sources */ +if (time_change_notify (fds.fd,0xf )) { +perror ("time_change_notify" ); +return EXIT_FAILURE ; + } + +while (poll (&fds,1 ,-1 ) >0 ) { +eventfd_t data; +ssize_t r; + + r =read (fds.fd, &data,sizeof data); +if (r ==-1 ) { +if (errno ==EINTR ) +continue ; + +break ; + } + +printf ("system time has changed %llu times\n" , data); + } + +puts ("Done polling system time changes.\n" ); + +return EXIT_SUCCESS ; +} +
include/asm-generic/unistd.h +3 -1
@@ -646,9 +646,11 @@ __SYSCALL(__NR_prlimit64, sys_prlimit64) __SYSCALL(__NR_fanotify_init, sys_fanotify_init)#define __NR_fanotify_mark 263 __SYSCALL(__NR_fanotify_mark, sys_fanotify_mark) +#define __NR_time_change_notify 264 +__SYSCALL(__NR_time_change_notify, sys_time_change_notify)#undef __NR_syscalls -#define __NR_syscalls 264 +#define __NR_syscalls 265/* * All syscalls below here should go away really,
include/linux/syscalls.h +1 -0
@@ -819,6 +819,7 @@ asmlinkage long sys_fanotify_init(unsigned int flags, unsigned int event_f_flagsasmlinkage long sys_fanotify_mark (int fanotify_fd,unsigned int flags,u64 mask,int fd,const char __user *pathname); +asmlinkage long sys_time_change_notify (int fd,unsigned int flags);int kernel_execve (const char *filename,const char *const argv[],const char *const envp[]);
include/linux/time.h +20 -0
@@ -252,6 +252,26 @@ static __always_inline void timespec_add_ns(struct timespec *a, u64 ns) a->tv_sec +=__iter_div_u64_rem (a->tv_nsec + ns,NSEC_PER_SEC , &ns); a->tv_nsec = ns; } + +/* time change events */ +#define TIME_EVENT_SET 0 +#define TIME_EVENT_ADJ 1 + +#define TIME_CHANGE_NOTIFY_OTHERS BIT(0) +#define TIME_CHANGE_NOTIFY_OWN BIT(1) +#define TIME_CHANGE_NOTIFY_SET BIT(2) +#define TIME_CHANGE_NOTIFY_ADJUST BIT(3) + +#define TIME_CHANGE_NOTIFY_MAX_USERS 1024 + +#ifdef CONFIG_TIME_NOTIFY +extern unsigned int time_change_notify_max_users; + +void time_notify_all (int type); +#else +static inline void time_notify_all (int type) {} +#endif +#endif /* __KERNEL__ */ #define NFDBITS __NFDBITS
init/Kconfig +7 -0
@@ -980,6 +980,13 @@ config PERF_USE_VMALLOC help See tools/perf/design.txt for details +config TIME_NOTIFY + bool "System time changes notification for userspace" + depends on EVENTFD + help + Enable time change notification events to userspace via + eventfd. + menu "Kernel Performance Events And Counters" config PERF_EVENTS
kernel/Makefile +1 -0
@@ -104,6 +104,7 @@ obj-$(CONFIG_PERF_EVENTS) += perf_event.o obj-$(CONFIG_HAVE_HW_BREAKPOINT) += hw_breakpoint.o obj-$(CONFIG_USER_RETURN_NOTIFIER) += user-return-notifier.o obj-$(CONFIG_PADATA) += padata.o +obj-$(CONFIG_TIME_NOTIFY) += time_notify.o ifneq ($(CONFIG_SCHED_OMIT_FRAME_POINTER),y) # According to Alan Modra <[email protected]>, the -fno-omit-frame-pointer is
kernel/sys_ni.c +3 -0
@@ -185,3 +185,6 @@ cond_syscall(sys_perf_event_open);/* fanotify! */ cond_syscall (sys_fanotify_init);cond_syscall (sys_fanotify_mark); + +/* time change notification */ +cond_syscall (sys_time_change_notify);
kernel/sysctl.c +11 -0
@@ -1441,6 +1441,17 @@ static struct ctl_table fs_table[] = { .proc_handler = proc_doulongvec_minmax, },#endif /* CONFIG_AIO */ +#ifdef CONFIG_TIME_NOTIFY + { + .procname = "time-change-notify-max-users", + .data = &time_change_notify_max_users, + .maxlen = sizeof(unsigned int), + .mode = 0644, + .proc_handler = proc_dointvec_minmax, + .extra1 = &zero, + .extra2 = &ten_thousand, + }, +#endif #ifdef CONFIG_INOTIFY_USER { .procname = "inotify",
kernel/time.c +9 -2
@@ -92,7 +92,9 @@ SYSCALL_DEFINE1(stime, time_t __user *, tptr)if (err)return err; -do_settimeofday (&tv); + err =do_settimeofday (&tv); +if (!err) +time_notify_all (TIME_EVENT_SET );return 0 ; }
@@ -177,7 +179,10 @@ int do_sys_settimeofday(struct timespec *tv, struct timezone *tz)/* SMP safe, again the code in arch/foo/time.c should * globally block out interrupts when it runs. */ -return do_settimeofday (tv); + error =do_settimeofday (tv); +if (!error) +time_notify_all (TIME_EVENT_SET ); +return error; }return 0 ; }
@@ -215,6 +220,8 @@ SYSCALL_DEFINE1(adjtimex, struct timex __user *, txc_p)if (copy_from_user (&txc, txc_p,sizeof (struct timex )))return -EFAULT ; ret =do_adjtimex (&txc); +if (!ret) +time_notify_all (TIME_EVENT_ADJ );return copy_to_user (txc_p, &txc,sizeof (struct timex )) ? -EFAULT : ret; }
kernel/time_notify.c new file +183 -0
183 lines not on this page · raw message
Signature · 1 line
1.7.1
PATCH 2/7 wire up sys_time_change_notify() on ARM
sys_time_change_notify() is a new syscall with number and types of parameters such that no ARM-specific processing is needed.
Tested with 2.6.36-rc3 using Documentation/time-change-notify-example.c.
--- arch/arm/include/asm/unistd.h | 1 + arch/arm/kernel/calls.S | 1 + 2 files changed, 2 insertions(+), 0 deletions(-)
arch/arm/include/asm/unistd.h +1 -0
@@ -393,6 +393,7 @@#define __NR_perf_event_open (__NR_SYSCALL_BASE+364)#define __NR_recvmmsg (__NR_SYSCALL_BASE+365)#define __NR_accept4 (__NR_SYSCALL_BASE+366) +#define __NR_time_change_notify (__NR_SYSCALL_BASE+367)/* * The following SWIs are ARM private.
arch/arm/kernel/calls.S +1 -0
@@ -376,6 +376,7 @@ CALL(sys_perf_event_open) /* 365 */ CALL(sys_recvmmsg) CALL(sys_accept4) + CALL(sys_time_change_notify) #ifndef syscalls_counted .equ syscalls_padding, ((NR_syscalls + 3) & ~3) - NR_syscalls #define syscalls_counted
Signature · 1 line
1.7.1
Re: [PATCH 2/7] wire up sys_time_change_notify() on ARM
Quoted 35 lines · Alexander Shishkin
sys_time_change_notify()is a new syscall with number and types of parameters such that no ARM-specific processing is needed.Tested with 2.6.36-rc3 using
Documentation/time-change-notify-example.c.Signed-off-by Alexander Shishkin <[email protected]>CC Russell King <[email protected]>CC Andrew Morton <[email protected]>CC David Howells <[email protected]>CC Christoph Hellwig <[email protected]>CC "H. Peter Anvin" <[email protected]>CC linux-arm-kernel at lists.infradead.orgCC linux-kernel at vger.kernel.org--- arch/arm/include/asm/unistd.h | 1 + arch/arm/kernel/calls.S | 1 + 2 files changed, 2 insertions(+), 0 deletions(-)arch/arm/include/asm/unistd.h +1
-0@@ -393,6 +393,7 @@#define __NR_perf_event_open (__NR_SYSCALL_BASE+364)#define __NR_recvmmsg (__NR_SYSCALL_BASE+365)#define __NR_accept4 (__NR_SYSCALL_BASE+366) +#define __NR_time_change_notify (__NR_SYSCALL_BASE+367)/* * The following SWIs are ARM private. arch/arm/kernel/calls.S +0
-0
arch/arm/kernel/calls.S line 382
Quoted 7 lines
arch/arm/kernel/calls.S +1
-0@@ -376,6 +376,7 @@ CALL(sys_perf_event_open) /* 365 */ CALL(sys_recvmmsg) CALL(sys_accept4) + CALL(sys_time_change_notify) #ifndef syscalls_counted .equ syscalls_padding, ((NR_syscalls + 3) & ~3) - NR_syscalls #define syscalls_counted
No, you need to use 370 as the syscall number on ARM as 367-369 have been assigned to the fanotify and prlimit64 syscalls; see
http://www.arm.linux.org.uk/developer/patches/viewpatch.php?id=6343/1
/Mikael
Re: [PATCH 2/7] wire up sys_time_change_notify() on ARM
Quoted 47 lines · Mikael Pettersson
Quoted 35 lines · Alexander Shishkin
sys_time_change_notify()is a new syscall with number and types of parameters such that no ARM-specific processing is needed.Tested with 2.6.36-rc3 using
Documentation/time-change-notify-example.c.Signed-off-by Alexander Shishkin <[email protected]>CC Russell King <[email protected]>CC Andrew Morton <[email protected]>CC David Howells <[email protected]>CC Christoph Hellwig <[email protected]>CC "H. Peter Anvin" <[email protected]>--- arch/arm/include/asm/unistd.h | 1 + arch/arm/kernel/calls.S | 1 + 2 files changed, 2 insertions(+), 0 deletions(-)arch/arm/include/asm/unistd.h +1
-0@@ -393,6 +393,7 @@#define __NR_perf_event_open (__NR_SYSCALL_BASE+364)#define __NR_recvmmsg (__NR_SYSCALL_BASE+365)#define __NR_accept4 (__NR_SYSCALL_BASE+366) +#define __NR_time_change_notify (__NR_SYSCALL_BASE+367)/* * The following SWIs are ARM private. arch/arm/kernel/calls.S +0
-0arch/arm/kernel/calls.S line 382
Quoted 7 lines
arch/arm/kernel/calls.S +1
-0@@ -376,6 +376,7 @@ CALL(sys_perf_event_open) /* 365 */ CALL(sys_recvmmsg) CALL(sys_accept4) + CALL(sys_time_change_notify) #ifndef syscalls_counted .equ syscalls_padding, ((NR_syscalls + 3) & ~3) - NR_syscalls #define syscalls_countedNo, you need to use 370 as the syscall number on ARM as 367-369 have been assigned to the fanotify and prlimit64 syscalls; see
I'm aware of that, but I don't know how to synchronize this with Russell's devel branch. This patchset applies against 2.6.36-rc3. I guess, when it comes to merging it, I'll update it against Russell's tree.
Regards,
--
Alex
Re: [PATCH 2/7] wire up sys_time_change_notify() on ARM
In reply to Alexander ShishkinQuoted 2 lines · Alexander Shishkin
sys_time_change_notify()is a new syscall with number and types of parameters such that no ARM-specific processing is needed.
This patch needs to be redone. Please wait until I've finished catching up with the last 10 or so days of email and merged my tree into mainline. Alternatively, update your patch to apply on top of my master branch.
Re: [PATCH 2/7] wire up sys_time_change_notify() on ARM
Quoted 7 lines · Russell King - ARM Linux
Quoted 2 lines · Alexander Shishkin
sys_time_change_notify()is a new syscall with number and types of parameters such that no ARM-specific processing is needed.This patch needs to be redone. Please wait until I've finished catching up with the last 10 or so days of email and merged my tree into mainline. Alternatively, update your patch to apply on top of my master branch.
Ok, I'll wait, no problem.
Regards,
--
Alex
PATCH 6/7 wire up sys_time_change_notify() on ia64
In reply to Alexander Shishkin--- arch/ia64/include/asm/unistd.h | 3 ++- arch/ia64/kernel/entry.S | 1 + 2 files changed, 3 insertions(+), 1 deletions(-)
arch/ia64/include/asm/unistd.h +2 -1
@@ -315,11 +315,12 @@#define __NR_fanotify_init 1323#define __NR_fanotify_mark 1324#define __NR_prlimit64 1325 +#define __NR_time_change_notify 1326#ifdef __KERNEL__ -#define NR_syscalls 302/* length of syscall table */ +#define NR_syscalls 303/* length of syscall table */ /* * The following defines stop scripts/checksyscalls.sh from complaining about
arch/ia64/kernel/entry.S +1 -0
@@ -1771,6 +1771,7 @@ sys_call_table: data8 sys_fanotify_init data8 sys_fanotify_mark data8 sys_prlimit64 // 1325 + data8 sys_time_change_notify .org sys_call_table + 8*NR_syscalls // guard against failures to increase NR_syscalls #endif /* __IA64_ASM_PARAVIRTUALIZED_NATIVE */
Signature · 1 line
1.7.1
PATCH 7/7 wire up sys_time_change_notify() on s390
In reply to Alexander Shishkin--- arch/s390/include/asm/unistd.h | 3 ++- arch/s390/kernel/compat_wrapper.S | 6 ++++++ arch/s390/kernel/syscalls.S | 1 + 3 files changed, 9 insertions(+), 1 deletions(-)
arch/s390/include/asm/unistd.h +2 -1
@@ -272,7 +272,8 @@#define __NR_fanotify_init 332#define __NR_fanotify_mark 333#define __NR_prlimit64 334 -#define NR_syscalls 335 +#define __NR_time_change_notify 335 +#define NR_syscalls 336/* * There are some system calls that are not present on 64 bit, some
arch/s390/kernel/compat_wrapper.S +6 -0
@@ -1877,3 +1877,9 @@ sys_prlimit64_wrapper: llgtr %r4,%r4 # const struct rlimit64 __user * llgtr %r5,%r5 # struct rlimit64 __user * jg sys_prlimit64 # branch to system call + + .globl sys_time_change_notify_wrapper +sys_time_change_notify_wrapper: + lgfr %r2,%r2 # int + llgfr %r3,%r3 # unsigned int + jg sys_time_change_notify # branch to system call
arch/s390/kernel/syscalls.S +1 -0
@@ -343,3 +343,4 @@ SYSCALL(sys_perf_event_open,sys_perf_event_open,sys_perf_event_open_wrapper) SYSCALL(sys_fanotify_init,sys_fanotify_init,sys_fanotify_init_wrapper) SYSCALL(sys_fanotify_mark,sys_fanotify_mark,sys_fanotify_mark_wrapper) SYSCALL(sys_prlimit64,sys_prlimit64,sys_prlimit64_wrapper) +SYSCALL(sys_time_change_notify,sys_time_change_notify,sys_time_change_notify_wrapper) /* 335 */
Signature · 1 line
1.7.1
Re: [PATCH 7/7] wire up sys_time_change_notify() on s390
Quoted 12 lines · On Tue, 31 Aug 2010 14:45:51 +0300 Alexander Shishkin <[email protected]>…
Signed-off-by Alexander Shishkin <[email protected]>CC Martin Schwidefsky <[email protected]>CC Heiko Carstens <[email protected]>CC Andrew Morton <[email protected]>CC Jesper Nilsson <[email protected]>CC David Howells <[email protected]>CC Christian Borntraeger <[email protected]>CC "Eric W. Biederman" <[email protected]>CC Christoph Hellwig <[email protected]>
Quoted 5 lines
--- arch/s390/include/asm/unistd.h | 3 ++- arch/s390/kernel/compat_wrapper.S | 6 ++++++ arch/s390/kernel/syscalls.S | 1 + 3 files changed, 9 insertions(+), 1 deletions(-)
System-call wiring for s390 looks good.
Signature · 4 lines
blue skies, Martin. "Reality continues to ruin my life." - Calvin.
Re: [PATCH 7/7] wire up sys_time_change_notify() on s390
In reply to Alexander Shishkinarch/s390/kernel/syscalls.S SYSCALL(sys_perf_event_open,sys_perf_event_open,sys_perf_event_open_wrapper) · line 346
Quoted 10 lines · Alexander Shishkin
arch/s390/kernel/syscalls.S +1
-0@@ -343,3 +343,4 @@ SYSCALL(sys_perf_event_open,sys_perf_event_open,sys_perf_event_open_wrapper) SYSCALL(sys_fanotify_init,sys_fanotify_init,sys_fanotify_init_wrapper) SYSCALL(sys_fanotify_mark,sys_fanotify_mark,sys_fanotify_mark_wrapper) SYSCALL(sys_prlimit64,sys_prlimit64,sys_prlimit64_wrapper) +SYSCALL(sys_time_change_notify,sys_time_change_notify,sys_time_change_notify_wrapper) /* 335 */
Please add the missing newline. Thanks.
Re: [PATCH 7/7] wire up sys_time_change_notify() on s390
Quoted 13 lines · Heiko Carstens
arch/s390/kernel/syscalls.S SYSCALL(sys_perf_event_open,sys_perf_event_open,sys_perf_event_open_wrapper) · line 346
Quoted 10 lines · Alexander Shishkin
arch/s390/kernel/syscalls.S +1
-0@@ -343,3 +343,4 @@ SYSCALL(sys_perf_event_open,sys_perf_event_open,sys_perf_event_open_wrapper) SYSCALL(sys_fanotify_init,sys_fanotify_init,sys_fanotify_init_wrapper) SYSCALL(sys_fanotify_mark,sys_fanotify_mark,sys_fanotify_mark_wrapper) SYSCALL(sys_prlimit64,sys_prlimit64,sys_prlimit64_wrapper) +SYSCALL(sys_time_change_notify,sys_time_change_notify,sys_time_change_notify_wrapper) /* 335 */Please add the missing newline. Thanks.
Oops. Will do.