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

View file

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

View file

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