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.
This commit is contained in:
cinap_lenrek 2020-12-23 02:31:28 +01:00
parent 2e6a5e7046
commit dced7255ec
5 changed files with 24 additions and 27 deletions

View file

@ -18,22 +18,23 @@ null-terminated
name of the user who name of the user who
owns the current process. owns the current process.
.I Getuser .I Getuser
reads stats the file
.B /dev/user .BI /proc/ pid /status
to find the name. to find the name.
.PP .PP
.I Sysname .I Sysname
provides the same service for the file reads the file
.BR #c/sysname , .BR /dev/sysname ,
which contains the name of the machine. which contains the name of the machine.
Unlike Unlike
.IR getuser , .IR getuser ,
.I sysname .I sysname
caches the string, reading the file only once. caches the string, reading the file only once.
.SH SOURCE .SH SOURCE
.B /sys/src/libc/port/getuser.c .B /sys/src/libc/9sys/getuser.c
.br .br
.B /sys/src/libc/9sys/sysname.c .B /sys/src/libc/9sys/sysname.c
.SH SEE ALSO .SH SEE ALSO
.IR intro (2), .IR intro (2),
.IR proc (3),
.IR cons (3) .IR cons (3)

View file

@ -0,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;
}

View file

@ -24,6 +24,7 @@ OFILES=\
getenv.$O\ getenv.$O\
getpid.$O\ getpid.$O\
getppid.$O\ getppid.$O\
getuser.$O\
getwd.$O\ getwd.$O\
idn.$O\ idn.$O\
iounit.$O\ iounit.$O\

View file

@ -1,21 +0,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;
}

View file

@ -32,7 +32,6 @@ CFILES=\
frexp.c\ frexp.c\
getcallerpc.c\ getcallerpc.c\
getfields.c\ getfields.c\
getuser.c\
hangup.c\ hangup.c\
hypot.c\ hypot.c\
lnrand.c\ lnrand.c\