]> Projects (at) Tadryanom (dot) Me - AdrOS.git/commitdiff
fix: init PID 1, ls -l permissions/size, doom dynamic linking
authorTulio A M Mendes <[email protected]>
Tue, 17 Feb 2026 07:01:05 +0000 (04:01 -0300)
committerTulio A M Mendes <[email protected]>
Tue, 17 Feb 2026 07:01:05 +0000 (04:01 -0300)
1. Init process now gets PID 1 (like Linux): next_pid starts at 2,
   sched_assign_pid1() explicitly assigns PID 1 to the init process
   after it loads. Kernel threads and AP idles get PIDs 2+.

2. ls -l now shows permissions, nlink, size via stat() on each entry
   instead of just the type character.

3. doom.elf Makefile switched from static linking (libulibc.a) to
   dynamic linking (libc.so via ld.so) like all other user commands.

src/arch/x86/arch_platform.c
src/kernel/scheduler.c
user/doom/Makefile
user/ls.c

index de8e28c806edc8da9c6734b86d4120037bc57602..78e01855a6bd6e6fd1d870a1474f88417db31c26 100644 (file)
@@ -62,9 +62,9 @@ static void userspace_init_thread(void) {
     current_process->cmdline[sizeof(current_process->cmdline) - 1] = '\0';
     vmm_as_activate(user_as);
 
-    /* Register this process as "init" for orphan reparenting */
-    extern void sched_set_init_pid(uint32_t);
-    sched_set_init_pid(current_process->pid);
+    /* Assign PID 1 to init, like Linux */
+    extern void sched_assign_pid1(struct process*);
+    sched_assign_pid1(current_process);
 
     /* Open /dev/console as fd 0, 1, 2 — mirrors Linux kernel_init:
      *   sys_open("/dev/console", O_RDWR, 0);
index 67ccd00db31b90e14022196f88c40cb3f22ae0dc..9067b76475cfced60ed463361aabb1211f7db577 100644 (file)
@@ -19,7 +19,7 @@ struct process* current_process = NULL;
 #endif
 struct process* ready_queue_head = NULL;
 struct process* ready_queue_tail = NULL;
-static uint32_t next_pid = 1;
+static uint32_t next_pid = 2;  /* PID 1 reserved for init */
 static uint32_t init_pid = 0;  /* PID of the first userspace process ("init") */
 
 spinlock_t sched_lock = {0};
@@ -495,6 +495,15 @@ void sched_set_init_pid(uint32_t pid) {
     init_pid = pid;
 }
 
+void sched_assign_pid1(struct process* p) {
+    if (!p) return;
+    uintptr_t flags = spin_lock_irqsave(&sched_lock);
+    p->pid = 1;
+    p->tgid = 1;
+    init_pid = 1;
+    spin_unlock_irqrestore(&sched_lock, flags);
+}
+
 void process_exit_notify(int status) {
     if (!current_process) return;
 
index e71df9940523f0754ada4d7dac7e602c75cb2ff2..3e10aa2e9535573aa4c5884ea296e5df4375a9b7 100644 (file)
@@ -9,22 +9,21 @@
 # The output is doom.elf which gets packaged into the AdrOS initrd.
 
 CC := i686-elf-gcc
-AR := i686-elf-ar
+LD := i686-elf-ld
 
 ULIBC_DIR := ../ulibc
 ULIBC_INC := $(ULIBC_DIR)/include
-ULIBC_LIB := $(ULIBC_DIR)/libulibc.a
 CRT0      := $(ULIBC_DIR)/src/crt0.o
-LINKER    := ../linker.ld
+DYN_LINKER := ../dyn_linker.ld
 
-CFLAGS := -m32 -O2 -ffreestanding -fno-pie -no-pie -nostdlib \
+CFLAGS := -m32 -O2 -ffreestanding -nostdlib -fPIC -fno-plt \
           -Wall -Wno-unused-parameter -Wno-sign-compare -Wno-pointer-sign \
           -Wno-missing-field-initializers -Wno-implicit-fallthrough \
           -Wno-format \
           -I$(ULIBC_INC) -I. -Idoomgeneric/doomgeneric \
           -DNORMALUNIX -DLINUX
 
-LDFLAGS := -m32 -nostdlib -fno-pie -no-pie -Wl,-T,$(LINKER)
+LDFLAGS := -m elf_i386 --dynamic-linker=/lib/ld.so -T $(DYN_LINKER) -L$(ULIBC_DIR) -rpath /lib
 
 # doomgeneric engine sources (nested inside cloned repo)
 # Exclude platform adapters and files requiring external libraries
@@ -60,8 +59,8 @@ check-doomgeneric:
                exit 1; \
        fi
 
-doom.elf: $(ALL_OBJ) $(ULIBC_LIB) $(CRT0)
-       @$(CC) $(LDFLAGS) -o $@ $(CRT0) $(ALL_OBJ) $(ULIBC_LIB) -lgcc
+doom.elf: $(ALL_OBJ) $(CRT0)
+       @$(LD) $(LDFLAGS) -o $@ $(CRT0) $(ALL_OBJ) -lc
        @echo "  LD      $@"
 
 doomgeneric/doomgeneric/%.o: doomgeneric/doomgeneric/%.c
index b8cc3ed398ee8162f18f91dbe3cf2d15e87d42f1..6ce6c69676338df97f60edbcd26feea919b30f49 100644 (file)
--- a/user/ls.c
+++ b/user/ls.c
@@ -60,12 +60,44 @@ static void ls_dir(const char* path) {
 
     for (int i = 0; i < count; i++) {
         if (lflag) {
+            char fullpath[512];
+            size_t plen = strlen(path);
+            if (plen > 0 && path[plen - 1] == '/')
+                snprintf(fullpath, sizeof(fullpath), "%s%s", path, entries[i].name);
+            else
+                snprintf(fullpath, sizeof(fullpath), "%s/%s", path, entries[i].name);
+
+            struct stat st;
+            int have_stat = (stat(fullpath, &st) == 0);
+
             char type = '-';
             if (entries[i].type == DT_DIR) type = 'd';
             else if (entries[i].type == DT_CHR) type = 'c';
             else if (entries[i].type == DT_LNK) type = 'l';
             else if (entries[i].type == DT_BLK) type = 'b';
-            printf("%c  %s\n", type, entries[i].name);
+
+            char perms[10];
+            if (have_stat) {
+                unsigned m = (unsigned)st.st_mode;
+                perms[0] = (m & S_IRUSR) ? 'r' : '-';
+                perms[1] = (m & S_IWUSR) ? 'w' : '-';
+                perms[2] = (m & S_IXUSR) ? 'x' : '-';
+                perms[3] = (m & S_IRGRP) ? 'r' : '-';
+                perms[4] = (m & S_IWGRP) ? 'w' : '-';
+                perms[5] = (m & S_IXGRP) ? 'x' : '-';
+                perms[6] = (m & S_IROTH) ? 'r' : '-';
+                perms[7] = (m & S_IWOTH) ? 'w' : '-';
+                perms[8] = (m & S_IXOTH) ? 'x' : '-';
+                perms[9] = '\0';
+            } else {
+                strcpy(perms, "---------");
+            }
+
+            unsigned long sz = have_stat ? (unsigned long)st.st_size : 0;
+            unsigned nlink = have_stat ? (unsigned)st.st_nlink : 1;
+
+            printf("%c%s %2u root root %8lu %s\n",
+                   type, perms, nlink, sz, entries[i].name);
         } else {
             printf("%s\n", entries[i].name);
         }