shithub: riscv

Download patch

ref: dced7255ec86587b441dc2ac04a8fb268ac5b920
parent: 2e6a5e704646109a7aaf7dd5a9d3a64475fd0055
author: cinap_lenrek <[email protected]>
date: Tue Dec 22 21:31:28 EST 2020

libc: re-implement getuser() by stating /proc/$pid/status

The idea is to avoid the magic files that contain
per process information in devcons when possible.
It will make it easier to deprecate them in the future.

--- a/sys/man/2/getuser
+++ b/sys/man/2/getuser
@@ -18,13 +18,13 @@
 name of the user who
 owns the current process.
 .I Getuser
-reads
-.B /dev/user
+stats the file
+.BI /proc/ pid /status
 to find the name.
 .PP
 .I Sysname
-provides the same service for the file
-.BR #c/sysname ,
+reads the file
+.BR /dev/sysname ,
 which contains the name of the machine.
 Unlike
 .IR getuser ,
@@ -31,9 +31,10 @@
 .I sysname
 caches the string, reading the file only once.
 .SH SOURCE
-.B /sys/src/libc/port/getuser.c
+.B /sys/src/libc/9sys/getuser.c
 .br
 .B /sys/src/libc/9sys/sysname.c 
 .SH SEE ALSO
 .IR intro (2),
+.IR proc (3),
 .IR cons (3)
--- /dev/null
+++ b/sys/src/libc/9sys/getuser.c
@@ -1,0 +1,17 @@
+#include <u.h>
+#include <libc.h>
+
+char *
+getuser(void)
+{
+	static char user[64];
+	char name[32];
+	Dir *dir;
+
+	snprint(name, sizeof(name), "/proc/%lud/status", (ulong)getpid());
+	if((dir = dirstat(name)) == nil)
+		return "none";
+	snprint(user, sizeof(user), "%s", dir->uid);
+	free(dir);
+	return user;
+}
--- a/sys/src/libc/9sys/mkfile
+++ b/sys/src/libc/9sys/mkfile
@@ -24,6 +24,7 @@
 	getenv.$O\
 	getpid.$O\
 	getppid.$O\
+	getuser.$O\
 	getwd.$O\
 	idn.$O\
 	iounit.$O\
--- a/sys/src/libc/port/getuser.c
+++ /dev/null
@@ -1,21 +1,0 @@
-#include <u.h>
-#include <libc.h>
-
-char *
-getuser(void)
-{
-	static char user[64];
-	int fd;
-	int n;
-
-	fd = open("/dev/user", OREAD|OCEXEC);
-	if(fd < 0)
-		return "none";
-	n = read(fd, user, (sizeof user)-1);
-	close(fd);
-	if(n <= 0)
-		strcpy(user, "none");
-	else
-		user[n] = 0;
-	return user;
-}
--- a/sys/src/libc/port/mkfile
+++ b/sys/src/libc/port/mkfile
@@ -32,7 +32,6 @@
 	frexp.c\
 	getcallerpc.c\
 	getfields.c\
-	getuser.c\
 	hangup.c\
 	hypot.c\
 	lnrand.c\