From: Tulio A M Mendes Date: Tue, 17 Feb 2026 07:01:05 +0000 (-0300) Subject: fix: init PID 1, ls -l permissions/size, doom dynamic linking X-Git-Url: https://projects.tadryanom.me/?a=commitdiff_plain;h=12ca6440ef0f776f74e458e62f151a28de327842;p=AdrOS.git fix: init PID 1, ls -l permissions/size, doom dynamic linking 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. --- diff --git a/src/arch/x86/arch_platform.c b/src/arch/x86/arch_platform.c index 4cdd1af1..1d74e56a 100644 --- a/src/arch/x86/arch_platform.c +++ b/src/arch/x86/arch_platform.c @@ -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); diff --git a/src/kernel/scheduler.c b/src/kernel/scheduler.c index 8309f2e8..77431c79 100644 --- a/src/kernel/scheduler.c +++ b/src/kernel/scheduler.c @@ -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; diff --git a/user/doom/Makefile b/user/doom/Makefile index e55a912c..89fdace3 100644 --- a/user/doom/Makefile +++ b/user/doom/Makefile @@ -18,22 +18,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 @@ -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 diff --git a/user/ls.c b/user/ls.c index 815fb09d..396efdf9 100644 --- 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); }