xref: /minix/minix/lib/libsys/getepinfo.c (revision 433d6423)
1 #include "syslib.h"
2 #include <string.h>
3 #include <unistd.h>
4 
5 #include <sys/ucred.h>
6 
7 static pid_t
8 getepinfo(endpoint_t proc_ep, uid_t *uid, gid_t *gid)
9 {
10 	message m;
11 	int r;
12 
13 	memset(&m, 0, sizeof(m));
14 	m.m_lsys_pm_getepinfo.endpt = proc_ep;
15 
16 	if ((r = _taskcall(PM_PROC_NR, PM_GETEPINFO, &m)) < 0)
17 		return r;
18 
19 	if (uid != NULL)
20 		*uid = m.m_pm_lsys_getepinfo.uid;
21 	if (gid != NULL)
22 		*gid = m.m_pm_lsys_getepinfo.gid;
23 	return (pid_t) r;
24 }
25 
26 pid_t
27 getnpid(endpoint_t proc_ep)
28 {
29 	return getepinfo(proc_ep, NULL, NULL);
30 }
31 
32 uid_t
33 getnuid(endpoint_t proc_ep)
34 {
35 	uid_t uid;
36 	int r;
37 
38 	if ((r = getepinfo(proc_ep, &uid, NULL)) < 0)
39 		return (uid_t) r;
40 
41 	return uid;
42 }
43 
44 gid_t
45 getngid(endpoint_t proc_ep)
46 {
47 	gid_t gid;
48 	int r;
49 
50 	if ((r = getepinfo(proc_ep, NULL, &gid)) < 0)
51 		return (gid_t) r;
52 
53 	return gid;
54 }
55 
56 int
57 getnucred(endpoint_t proc_ep, struct uucred *ucred)
58 {
59 	uid_t uid;
60 	gid_t gid;
61 	int r;
62 
63 	if (ucred == NULL)
64 		return EFAULT;
65 
66 	if ((r = getepinfo(proc_ep, &uid, &gid)) < 0)
67 		return r;
68 
69 	/* Only two fields are used for now; ensure the rest is zeroed out. */
70 	memset(ucred, 0, sizeof(struct uucred));
71 	ucred->cr_uid = uid;
72 	ucred->cr_gid = gid;
73 
74 	return r;
75 }
76