From 78cb60b644538a33e0479f25393d6c861e3605f8 Mon Sep 17 00:00:00 2001 From: dzwdz Date: Sun, 4 Jun 2023 20:43:51 +0200 Subject: kernel: rework /proc/ and process IDs I'm yet to write proper docs but the TL;DR is: Mounting /proc/ creates a new pid namespace. You're still visible in the old namespace with your old pid, but your children won't be. You see your own pid as 1. Current pids of children will be preserved, pids will be allocated starting from the highest one of your children. --- src/user/app/ps/ps.c | 77 +++++++++++++++++----------------------------- src/user/app/shell/shell.c | 10 ++++-- 2 files changed, 36 insertions(+), 51 deletions(-) (limited to 'src/user') diff --git a/src/user/app/ps/ps.c b/src/user/app/ps/ps.c index 78d4204..d2b9ded 100644 --- a/src/user/app/ps/ps.c +++ b/src/user/app/ps/ps.c @@ -5,69 +5,50 @@ #include #include -/* returns a pointer that can be set to NUL to undo the strcat */ -static char * -strtcat(char *dst, const char *src) -{ - char *s = dst + strlen(dst); - strcpy(s, src); - return s; -} - -static void -do_proc(char *path) +int +main(void) { - const int bufl = 4096; - char *buf = malloc(bufl); - FILE *f; - - { /* read the psdata into buf */ - char *s = strtcat(path, "mem"); - f = fopen(path, "r"); - *s = '\0'; - if (!f) errx(1, "couldn't open '%s'", path); - fseek(f, (long)_libc_psdata, SEEK_SET); - if (fread(buf, 1, 128, f) <= 0) { - strcpy(buf, "(no psdata)"); - } - buf[128] = '\0'; - fclose(f); + char *readbuf = malloc(4096); + char *procbuf = malloc(4096); + FILE *f = fopen("/proc/", "r"); + if (!f) { + err(1, "couldn't open /proc/"); } - printf("%20s %s\n", path, buf); - - f = fopen(path, "r"); - if (!f) errx(1, "couldn't open '%s'", path); - // TODO library for iterating over directories for (;;) { - int len = fread(buf, 1, bufl, f); + int len = fread(readbuf, 1, 4096, f); if (len <= 0) break; for (int pos = 0; pos < len; ) { - const char *end = memchr(buf + pos, 0, len - pos); + char *end = memchr(readbuf + pos, 0, len - pos); if (!end) { errx(1, "unimplemented: buffer overflow"); } - size_t entryl = end - (buf + pos) + 1; - if (isdigit(buf[pos])) { - /* yup, no overflow check */ - char *s = strtcat(path, buf + pos); - do_proc(path); - *s = '\0'; + size_t entryl = end - (readbuf+pos) + 1; + if (isdigit(readbuf[pos])) { + FILE *g; + sprintf(procbuf, "/proc/%smem", readbuf+pos); + g = fopen(procbuf, "r"); + if (!g) { + warn("couldn't open \"%s\"", procbuf); + strcpy(procbuf, "(can't peek)"); + } else { + fseek(g, (long)_libc_psdata, SEEK_SET); + if (fread(procbuf, 1, 128, g) <= 0) { + strcpy(procbuf, "(no psdata)"); + } + procbuf[128] = '\0'; + fclose(g); + } + end[-1] = '\0'; /* remove trailing slash */ + printf("%s\t%s\n", readbuf+pos, procbuf); } pos += entryl; } } - free(buf); + free(readbuf); + free(procbuf); fclose(f); -} - -int -main(void) -{ - char *buf = malloc(4096); - strcpy(buf, "/proc/"); - do_proc(buf); return 0; } diff --git a/src/user/app/shell/shell.c b/src/user/app/shell/shell.c index d2d7e37..becc8d8 100644 --- a/src/user/app/shell/shell.c +++ b/src/user/app/shell/shell.c @@ -58,10 +58,14 @@ void run_args(int argc, char **argv, struct redir *redir) { return; } _sys_mount(HANDLE_PROCFS, argv[1], strlen(argv[1])); - if (!fork2_n_mount("/")) { - fs_dir_inject(argv[1]); - exit(1); + /* + if (!(3 <= argc && !strcmp(argv[2], "raw"))) { + if (!fork2_n_mount("/")) { + fs_dir_inject(argv[1]); + exit(1); + } } + */ return; } else if (!strcmp(argv[0], "cd")) { if (chdir(argc > 1 ? argv[1] : "/") < 0) -- cgit v1.2.3