From c0fe48635b94d30b3f431050a5f275421edfbe48 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sun, 14 Apr 2019 15:19:45 +0200 Subject: [PATCH] Kernel: Add /proc/uptime file (number of seconds since boot.) Also added a simple /bin/uptime to pretty-print this information. :^) --- Kernel/FileSystem/ProcFS.cpp | 9 +++++++++ Kernel/sync.sh | 1 + Userland/.gitignore | 1 + Userland/Makefile | 5 +++++ Userland/uptime.cpp | 31 +++++++++++++++++++++++++++++++ 5 files changed, 47 insertions(+) create mode 100644 Userland/uptime.cpp diff --git a/Kernel/FileSystem/ProcFS.cpp b/Kernel/FileSystem/ProcFS.cpp index 7df1e4096cf..d9c28162bde 100644 --- a/Kernel/FileSystem/ProcFS.cpp +++ b/Kernel/FileSystem/ProcFS.cpp @@ -36,6 +36,7 @@ enum ProcFileType { FI_Root_inodes, FI_Root_dmesg, FI_Root_pci, + FI_Root_uptime, FI_Root_self, // symlink FI_Root_sys, // directory __FI_Root_End, @@ -248,6 +249,13 @@ ByteBuffer procfs$pci(InodeIdentifier) return builder.to_byte_buffer(); } +ByteBuffer procfs$uptime(InodeIdentifier) +{ + StringBuilder builder; + builder.appendf("%u\n", (dword)(g_uptime / 1000)); + return builder.to_byte_buffer(); +} + ByteBuffer procfs$pid_vmo(InodeIdentifier identifier) { auto handle = ProcessInspectionHandle::from_pid(to_pid(identifier)); @@ -1144,6 +1152,7 @@ ProcFS::ProcFS() m_entries[FI_Root_dmesg] = { "dmesg", FI_Root_dmesg, procfs$dmesg }; m_entries[FI_Root_self] = { "self", FI_Root_self, procfs$self }; m_entries[FI_Root_pci] = { "pci", FI_Root_pci, procfs$pci }; + m_entries[FI_Root_uptime] = { "uptime", FI_Root_uptime, procfs$uptime }; m_entries[FI_Root_sys] = { "sys", FI_Root_sys }; m_entries[FI_PID_vm] = { "vm", FI_PID_vm, procfs$pid_vm }; diff --git a/Kernel/sync.sh b/Kernel/sync.sh index e7f8d47f37b..89666e11bbb 100755 --- a/Kernel/sync.sh +++ b/Kernel/sync.sh @@ -80,6 +80,7 @@ cp -v ../Userland/tc mnt/bin/tc cp -v ../Userland/host mnt/bin/host cp -v ../Userland/qs mnt/bin/qs cp -v ../Userland/mv mnt/bin/mv +cp -v ../Userland/uptime mnt/bin/uptime chmod 4755 mnt/bin/su cp -v ../Applications/Terminal/Terminal mnt/bin/Terminal cp -v ../Applications/FontEditor/FontEditor mnt/bin/FontEditor diff --git a/Userland/.gitignore b/Userland/.gitignore index 25f4c5d2b50..66f9fc067d8 100644 --- a/Userland/.gitignore +++ b/Userland/.gitignore @@ -43,3 +43,4 @@ tc host qs mv +uptime diff --git a/Userland/Makefile b/Userland/Makefile index 3235ec5f82a..b4aee315f3e 100644 --- a/Userland/Makefile +++ b/Userland/Makefile @@ -39,6 +39,7 @@ OBJS = \ host.o \ qs.o \ mv.o \ + uptime.o \ rm.o APPS = \ @@ -83,6 +84,7 @@ APPS = \ host \ qs \ mv \ + uptime \ rm ARCH_FLAGS = @@ -228,6 +230,9 @@ qs: qs.o mv: mv.o $(LD) -o $@ $(LDFLAGS) $< -lc +uptime: uptime.o + $(LD) -o $@ $(LDFLAGS) $< -lc + .cpp.o: @echo "CXX $<"; $(CXX) $(CXXFLAGS) -o $@ -c $< diff --git a/Userland/uptime.cpp b/Userland/uptime.cpp new file mode 100644 index 00000000000..fc0b852bfad --- /dev/null +++ b/Userland/uptime.cpp @@ -0,0 +1,31 @@ +#include + +int main(int, char**) +{ + FILE* fp = fopen("/proc/uptime", "r"); + if (!fp) { + perror("fopen(/proc/uptime)"); + return 1; + } + + char buffer[BUFSIZ]; + auto* p = fgets(buffer, sizeof(buffer), fp); + if (!p) { + perror("fgets"); + return 1; + } + + unsigned seconds; + sscanf(buffer, "%u", &seconds); + + printf("Up %d day%s, ", seconds / 86400, (seconds / 86400) == 1 ? "" : "s"); + seconds %= 86400; + printf("%d hour%s, ", seconds / 3600, (seconds / 3600) == 1 ? "" : "s"); + seconds %= 3600; + printf("%d minute%s, ", seconds / 60, (seconds / 60) == 1 ? "" : "s"); + seconds %= 60; + printf("%d second%s\n", seconds, seconds == 1 ? "" : "s"); + + fclose(fp); + return 0; +}