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

@ -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\
getpid.$O\
getppid.$O\
getuser.$O\
getwd.$O\
idn.$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\
getcallerpc.c\
getfields.c\
getuser.c\
hangup.c\
hypot.c\
lnrand.c\