Skip to content

Instantly share code, notes, and snippets.

@ramnes
Created October 19, 2014 13:48
Show Gist options
  • Save ramnes/9c3f02300d55f7b96b32 to your computer and use it in GitHub Desktop.
Save ramnes/9c3f02300d55f7b96b32 to your computer and use it in GitHub Desktop.
From a48de0703be228ffadc465f91cbf228b565a24b9 Mon Sep 17 00:00:00 2001
From: Guillaume Gelin <[email protected]>
Date: Sun, 19 Oct 2014 15:47:07 +0200
Subject: [PATCH] Add an uptime system call.
Signed-off-by: Guillaume Gelin <[email protected]>
---
arch/x86/syscalls/syscall_32.tbl | 1 +
arch/x86/syscalls/syscall_64.tbl | 1 +
fs/proc/uptime.c | 13 +++++++++++++
include/linux/syscalls.h | 1 +
include/uapi/asm-generic/unistd.h | 6 +++++-
5 files changed, 21 insertions(+), 1 deletion(-)
diff --git a/arch/x86/syscalls/syscall_32.tbl b/arch/x86/syscalls/syscall_32.tbl
index 9fe1b5d..73733a1 100644
--- a/arch/x86/syscalls/syscall_32.tbl
+++ b/arch/x86/syscalls/syscall_32.tbl
@@ -364,3 +364,4 @@
355 i386 getrandom sys_getrandom
356 i386 memfd_create sys_memfd_create
357 i386 bpf sys_bpf
+358 i386 uptime sys_uptime
diff --git a/arch/x86/syscalls/syscall_64.tbl b/arch/x86/syscalls/syscall_64.tbl
index 281150b..a5d9e57 100644
--- a/arch/x86/syscalls/syscall_64.tbl
+++ b/arch/x86/syscalls/syscall_64.tbl
@@ -328,6 +328,7 @@
319 common memfd_create sys_memfd_create
320 common kexec_file_load sys_kexec_file_load
321 common bpf sys_bpf
+322 common uptime sys_uptime
#
# x32-specific system call numbers start at 512 to avoid cache impact
diff --git a/fs/proc/uptime.c b/fs/proc/uptime.c
index 33de567..ef0ec65 100644
--- a/fs/proc/uptime.c
+++ b/fs/proc/uptime.c
@@ -6,6 +6,19 @@
#include <linux/time.h>
#include <linux/kernel_stat.h>
#include <linux/cputime.h>
+#include <linux/syscalls.h>
+
+#include <asm/uaccess.h>
+
+SYSCALL_DEFINE1(uptime, struct timespec __user *, uptime_uptr)
+{
+ struct timespec uptime;
+
+ get_monotonic_boottime(&uptime);
+ if (uptime_uptr)
+ return copy_to_user(uptime_uptr, &uptime, sizeof(uptime));
+ return uptime.tv_sec;
+}
static int uptime_proc_show(struct seq_file *m, void *v)
{
diff --git a/include/linux/syscalls.h b/include/linux/syscalls.h
index bda9b81..ae6cedb 100644
--- a/include/linux/syscalls.h
+++ b/include/linux/syscalls.h
@@ -877,4 +877,5 @@ asmlinkage long sys_seccomp(unsigned int op, unsigned int flags,
asmlinkage long sys_getrandom(char __user *buf, size_t count,
unsigned int flags);
asmlinkage long sys_bpf(int cmd, union bpf_attr *attr, unsigned int size);
+asmlinkage long sys_uptime(struct timespec __user *uptime_uptr);
#endif
diff --git a/include/uapi/asm-generic/unistd.h b/include/uapi/asm-generic/unistd.h
index 22749c1..3b1a42b 100644
--- a/include/uapi/asm-generic/unistd.h
+++ b/include/uapi/asm-generic/unistd.h
@@ -656,11 +656,15 @@ __SYSCALL(__NR_accept4, sys_accept4)
#define __NR_recvmmsg 243
__SC_COMP(__NR_recvmmsg, sys_recvmmsg, compat_sys_recvmmsg)
+/* fs/uptime.c */
+#define __NR_uptime 244
+__SYSCALL(__NR_uptime, sys_uptime)
+
/*
* Architectures may provide up to 16 syscalls of their own
* starting with this value.
*/
-#define __NR_arch_specific_syscall 244
+#define __NR_arch_specific_syscall 245
#define __NR_wait4 260
__SC_COMP(__NR_wait4, sys_wait4, compat_sys_wait4)
--
2.0.2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment