Kernel: Add /proc/uptime file (number of seconds since boot.)

Also added a simple /bin/uptime to pretty-print this information. :^)
This commit is contained in:
Andreas Kling 2019-04-14 15:19:45 +02:00
parent 0e30f4e412
commit c0fe48635b
Notes: sideshowbarker 2024-07-19 14:43:12 +09:00
5 changed files with 47 additions and 0 deletions

View file

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

View file

@ -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

1
Userland/.gitignore vendored
View file

@ -43,3 +43,4 @@ tc
host
qs
mv
uptime

View file

@ -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 $<

31
Userland/uptime.cpp Normal file
View file

@ -0,0 +1,31 @@
#include <stdio.h>
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;
}