diff options
author | dzwdz | 2023-06-04 20:43:51 +0200 |
---|---|---|
committer | dzwdz | 2023-06-04 20:43:51 +0200 |
commit | 78cb60b644538a33e0479f25393d6c861e3605f8 (patch) | |
tree | 15d310b2bba5cce086633c025080155ca36e7c43 /src/user/app/ps | |
parent | 8fd4943b2721696f86783d22dd2e8d593a22a766 (diff) |
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.
Diffstat (limited to 'src/user/app/ps')
-rw-r--r-- | src/user/app/ps/ps.c | 77 |
1 files changed, 29 insertions, 48 deletions
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 <stdlib.h> #include <string.h> -/* 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; } |