ref: 672cf179a1a8a17a4a977eeada60a035a27ed98d
parent: d919ad3b5e8e98d7470275d724772de221e060f6
author: cinap_lenrek <cinap_lenrek@felloff.net>
date: Sat Dec 19 10:15:38 EST 2020
libc: implement getppid() reading /proc/$pid/ppid instead of /dev/ppid The devcons driver is really the wrong place to serve per process information.
--- a/sys/man/2/getpid
+++ b/sys/man/2/getpid
@@ -13,27 +13,18 @@
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
--- a/sys/src/ape/lib/ap/plan9/getppid.c
+++ b/sys/src/ape/lib/ap/plan9/getppid.c
@@ -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);
}
--- a/sys/src/libc/9sys/getppid.c
+++ b/sys/src/libc/9sys/getppid.c
@@ -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);
}