From 2ad6ee8ed15d1bf898645a16dbc06991a3c1425e Mon Sep 17 00:00:00 2001
From: dzwdz
Date: Wed, 25 Jan 2023 19:22:18 +0100
Subject: user: process titles, /bin/ps

---
 src/user/lib/_start2.c        | 11 +++++++++++
 src/user/lib/elfload.c        |  2 +-
 src/user/lib/fs/misc.c        |  2 ++
 src/user/lib/include/_proc.h  |  7 +++++++
 src/user/lib/include/stdlib.h |  1 +
 src/user/lib/stdlib.c         | 14 ++++++++++++++
 6 files changed, 36 insertions(+), 1 deletion(-)
 create mode 100644 src/user/lib/include/_proc.h

(limited to 'src/user/lib')

diff --git a/src/user/lib/_start2.c b/src/user/lib/_start2.c
index 495f046..2cf8667 100644
--- a/src/user/lib/_start2.c
+++ b/src/user/lib/_start2.c
@@ -1,3 +1,5 @@
+#include <_proc.h>
+#include <camellia/flags.h>
 #include <camellia/syscalls.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -22,6 +24,15 @@ void intr_trampoline(void); /* intr.s */
 _Noreturn void _start2(struct execdata *ed) {
 	const char *progname;
 	elf_selfreloc();
+
+	/* done first so it isn't allocated elsewhere by accident */
+	_syscall_memflag(_libc_psdata, 1, MEMFLAG_PRESENT);
+	if (ed->argv[0]) {
+		strcpy(_libc_psdata, ed->argv[0]);
+	} else {
+		strcpy(_libc_psdata, "?");
+	}
+
 	_syscall_intr_set(intr_trampoline);
 	intr_set(intr_default);
 	__setinitialcwd(ed->cwd);
diff --git a/src/user/lib/elfload.c b/src/user/lib/elfload.c
index 7cad10f..a4ee91e 100644
--- a/src/user/lib/elfload.c
+++ b/src/user/lib/elfload.c
@@ -160,7 +160,7 @@ void elf_exec(void *base, char **argv, char **envp) {
 	void *exebase = elf_loadmem(ehdr);
 	if (!exebase) return;
 
-	void *newstack = _syscall_memflag((void*)0x1000, 0x1000, MEMFLAG_FINDFREE | MEMFLAG_PRESENT);
+	void *newstack = _syscall_memflag((void*)0x11000, 0x1000, MEMFLAG_FINDFREE | MEMFLAG_PRESENT);
 	if (!newstack) return;
 
 	_freejmp_chstack(exebase + ehdr->e_entry, exebase, elf_spread(ehdr) + 0x1000, (const char**)argv, envp, newstack);
diff --git a/src/user/lib/fs/misc.c b/src/user/lib/fs/misc.c
index 11428b8..ee0b54c 100644
--- a/src/user/lib/fs/misc.c
+++ b/src/user/lib/fs/misc.c
@@ -233,11 +233,13 @@ bool mount_at_pred(const char *path) {
 	if (!fork2_n_mount(path)) {
 		/* child -> go into the for body */
 		_klogf("%s: impl", path);
+		setproctitle("i'%s'", path);
 		return true;
 	}
 
 	if (strcmp("/", path) && !fork2_n_mount("/")) {
 		_klogf("%s: dir", path);
+		setproctitle("d'%s'", path);
 		fs_dir_inject(path);
 		exit(1);
 	}
diff --git a/src/user/lib/include/_proc.h b/src/user/lib/include/_proc.h
new file mode 100644
index 0000000..c8fd70b
--- /dev/null
+++ b/src/user/lib/include/_proc.h
@@ -0,0 +1,7 @@
+#pragma once
+
+/* That page contains a null-terminated string that describes the process
+ * for tools such as ps. It's first allocated in the bootstrap process, and
+ * then it's freed on every exec() - just to be immediately reallocated by
+ * _start2(). */
+static char *const _libc_psdata = (void*)0x10000;
diff --git a/src/user/lib/include/stdlib.h b/src/user/lib/include/stdlib.h
index 8a86265..050ca80 100644
--- a/src/user/lib/include/stdlib.h
+++ b/src/user/lib/include/stdlib.h
@@ -14,6 +14,7 @@ _Noreturn void exit(int);
 
 const char *getprogname(void);
 void setprogname(const char *progname);
+void setproctitle(const char *fmt, ...);
 
 int mkstemp(char *template);
 char *getenv(const char *name);
diff --git a/src/user/lib/stdlib.c b/src/user/lib/stdlib.c
index 5d1b09f..4e471ba 100644
--- a/src/user/lib/stdlib.c
+++ b/src/user/lib/stdlib.c
@@ -1,3 +1,4 @@
+#include <_proc.h>
 #include <camellia.h>
 #include <camellia/syscalls.h>
 #include <errno.h>
@@ -16,6 +17,19 @@ void setprogname(const char *pg) {
 	progname = pg;
 }
 
+void setproctitle(const char *fmt, ...) {
+	if (!fmt) {
+		strcpy(_libc_psdata, progname);
+		return;
+	}
+	sprintf(_libc_psdata, "%s: ", progname);
+
+	va_list argp;
+	va_start(argp, fmt);
+	vsnprintf(_libc_psdata + strlen(_libc_psdata), 128, fmt, argp);
+	va_end(argp);
+}
+
 int mkstemp(char *template) {
 	// TODO randomize template
 	handle_t h = camellia_open(template, OPEN_CREATE | OPEN_RW);
-- 
cgit v1.2.3