Bladeren bron

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

Also added a simple /bin/uptime to pretty-print this information. :^)
Andreas Kling 6 jaren geleden
bovenliggende
commit
c0fe48635b
5 gewijzigde bestanden met toevoegingen van 47 en 0 verwijderingen
  1. 9 0
      Kernel/FileSystem/ProcFS.cpp
  2. 1 0
      Kernel/sync.sh
  3. 1 0
      Userland/.gitignore
  4. 5 0
      Userland/Makefile
  5. 31 0
      Userland/uptime.cpp

+ 9 - 0
Kernel/FileSystem/ProcFS.cpp

@@ -36,6 +36,7 @@ enum ProcFileType {
     FI_Root_inodes,
     FI_Root_inodes,
     FI_Root_dmesg,
     FI_Root_dmesg,
     FI_Root_pci,
     FI_Root_pci,
+    FI_Root_uptime,
     FI_Root_self, // symlink
     FI_Root_self, // symlink
     FI_Root_sys, // directory
     FI_Root_sys, // directory
     __FI_Root_End,
     __FI_Root_End,
@@ -248,6 +249,13 @@ ByteBuffer procfs$pci(InodeIdentifier)
     return builder.to_byte_buffer();
     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)
 ByteBuffer procfs$pid_vmo(InodeIdentifier identifier)
 {
 {
     auto handle = ProcessInspectionHandle::from_pid(to_pid(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_dmesg] = { "dmesg", FI_Root_dmesg, procfs$dmesg };
     m_entries[FI_Root_self] = { "self", FI_Root_self, procfs$self };
     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_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_Root_sys] = { "sys", FI_Root_sys };
 
 
     m_entries[FI_PID_vm] = { "vm", FI_PID_vm, procfs$pid_vm };
     m_entries[FI_PID_vm] = { "vm", FI_PID_vm, procfs$pid_vm };

+ 1 - 0
Kernel/sync.sh

@@ -80,6 +80,7 @@ cp -v ../Userland/tc mnt/bin/tc
 cp -v ../Userland/host mnt/bin/host
 cp -v ../Userland/host mnt/bin/host
 cp -v ../Userland/qs mnt/bin/qs
 cp -v ../Userland/qs mnt/bin/qs
 cp -v ../Userland/mv mnt/bin/mv
 cp -v ../Userland/mv mnt/bin/mv
+cp -v ../Userland/uptime mnt/bin/uptime
 chmod 4755 mnt/bin/su
 chmod 4755 mnt/bin/su
 cp -v ../Applications/Terminal/Terminal mnt/bin/Terminal
 cp -v ../Applications/Terminal/Terminal mnt/bin/Terminal
 cp -v ../Applications/FontEditor/FontEditor mnt/bin/FontEditor
 cp -v ../Applications/FontEditor/FontEditor mnt/bin/FontEditor

+ 1 - 0
Userland/.gitignore

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

+ 5 - 0
Userland/Makefile

@@ -39,6 +39,7 @@ OBJS = \
        host.o \
        host.o \
        qs.o \
        qs.o \
        mv.o \
        mv.o \
+       uptime.o \
        rm.o
        rm.o
 
 
 APPS = \
 APPS = \
@@ -83,6 +84,7 @@ APPS = \
        host \
        host \
        qs \
        qs \
        mv \
        mv \
+       uptime \
        rm
        rm
 
 
 ARCH_FLAGS =
 ARCH_FLAGS =
@@ -228,6 +230,9 @@ qs: qs.o
 mv: mv.o
 mv: mv.o
 	$(LD) -o $@ $(LDFLAGS) $< -lc
 	$(LD) -o $@ $(LDFLAGS) $< -lc
 
 
+uptime: uptime.o
+	$(LD) -o $@ $(LDFLAGS) $< -lc
+
 .cpp.o:
 .cpp.o:
 	@echo "CXX $<"; $(CXX) $(CXXFLAGS) -o $@ -c $<
 	@echo "CXX $<"; $(CXX) $(CXXFLAGS) -o $@ -c $<
 
 

+ 31 - 0
Userland/uptime.cpp

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