libc: implement getppid() reading /proc/$pid/ppid instead of /dev/ppid

The devcons driver is really the wrong place to
serve per process information.
This commit is contained in:
cinap_lenrek 2020-12-19 15:15:38 +01:00
parent d919ad3b5e
commit 672cf179a1
3 changed files with 21 additions and 27 deletions

View file

@ -13,27 +13,18 @@ int getpid(void)
int getppid(void) int getppid(void)
.SH DESCRIPTION .SH DESCRIPTION
.I Getpid .I Getpid
reads returns the process id of the current process,
.B /dev/pid
(see
.IR cons (3))
and converts it to get the process id of the current process,
a number guaranteed to be unique among all running processes on the machine a number guaranteed to be unique among all running processes on the machine
executing executing
.IR getpid . .IR getpid .
.PP .PP
.I Getppid .I Getppid
reads returns the process id of the parent of the current process.
.B /dev/ppid
(see
.IR cons (3))
and converts it to get the id of the parent of the current process.
.SH SOURCE .SH SOURCE
.B /sys/src/libc/9sys .B /sys/src/libc/9sys
.SH SEE ALSO .SH SEE ALSO
.IR intro (2), .IR intro (2),
.IR exec (2), .IR exec (2),
.IR cons (3),
.IR proc (3) .IR proc (3)
.SH DIAGNOSTICS .SH DIAGNOSTICS
Returns 0 and Returns 0 and

View file

@ -1,6 +1,7 @@
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <fcntl.h> #include <fcntl.h>
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include <string.h> #include <string.h>
@ -9,14 +10,15 @@
pid_t pid_t
getppid(void) getppid(void)
{ {
char b[20]; char buf[32];
int f; int f;
memset(b, 0, sizeof(b)); snprintf(buf, sizeof(buf), "/proc/%d/ppid", getpid());
f = _OPEN("/dev/ppid", OREAD); f = open(buf, 0);
if(f >= 0) { if(f < 0)
_PREAD(f, b, sizeof(b), 0); return 0;
_CLOSE(f); memset(buf, 0, sizeof(buf));
} read(f, buf, sizeof(buf)-1);
return atol(b); close(f);
return atol(buf);
} }

View file

@ -4,14 +4,15 @@
int int
getppid(void) getppid(void)
{ {
char b[20]; char buf[32];
int f; int f;
memset(b, 0, sizeof(b)); snprint(buf, sizeof(buf), "/proc/%lud/ppid", (ulong)getpid());
f = open("/dev/ppid", OREAD|OCEXEC); f = open(buf, OREAD|OCEXEC);
if(f >= 0) { if(f < 0)
read(f, b, sizeof(b)); return 0;
close(f); memset(buf, 0, sizeof(buf));
} read(f, buf, sizeof(buf)-1);
return atol(b); close(f);
return atol(buf);
} }