]> 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 4cdd1af11712139971af6df329816f4e71ceafe4..1d74e56ab919cfa67c27c90086b67b840ecc5db4 100644 (file)
@@ -71,9 +71,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 8309f2e8de4e746a6d0885ee29295128e7f5d7b7..77431c798aa2d16794db51a080bd572ec37496f4 100644 (file)
@@ -28,7 +28,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};
@@ -504,6 +504,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 e55a912cf22db5b58b118749c0953df4d6179d53..89fdace3820e4c33ba79c9b5ef96bdd249350cab 100644 (file)
 # 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
@@ -69,8 +68,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 815fb09d13cec9a3ca6bdcf5e6096f7413dedf39..396efdf95007c8e365412f2a6696c542d3f2dbb1 100644 (file)
--- a/user/ls.c
+++ b/user/ls.c
@@ -69,12 +69,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);
         }