1 /*
2    This file is part of LibGTop 2.0.
3 
4    LibGTop is free software; you can redistribute it and/or modify it
5    under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 2 of the License,
7    or (at your option) any later version.
8 
9    LibGTop is distributed in the hope that it will be useful, but WITHOUT
10    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12    for more details.
13 
14    You should have received a copy of the GNU General Public License
15    along with LibGTop; see the file COPYING. If not, write to the
16    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17    Boston, MA 02110-1301, USA.
18 */
19 
20 #include <config.h>
21 #include <unistd.h>
22 
23 #include <glibtop.h>
24 #include <glibtop/error.h>
25 #include <glibtop/procuid.h>
26 
27 #include <glibtop_suid.h>
28 
29 #include <sys/param.h>
30 #include <sys/sysctl.h>
31 
32 static const unsigned long _glibtop_sysdeps_proc_uid =
33 (1L << GLIBTOP_PROC_UID_UID) + (1L << GLIBTOP_PROC_UID_EUID) +
34 (1L << GLIBTOP_PROC_UID_GID) + (1L << GLIBTOP_PROC_UID_EGID) +
35 (1L << GLIBTOP_PROC_UID_PID) + (1L << GLIBTOP_PROC_UID_PPID) +
36 (1L << GLIBTOP_PROC_UID_PGRP) + (1L << GLIBTOP_PROC_UID_TTY) +
37 (1L << GLIBTOP_PROC_UID_TPGID);
38 
39 /* Init function. */
40 
41 void
_glibtop_init_proc_uid_p(glibtop * server)42 _glibtop_init_proc_uid_p (glibtop *server)
43 {
44 	server->sysdeps.proc_uid = _glibtop_sysdeps_proc_uid;
45 }
46 
47 /* Provides detailed information about a process. */
48 
49 void
glibtop_get_proc_uid_p(glibtop * server,glibtop_proc_uid * buf,pid_t pid)50 glibtop_get_proc_uid_p (glibtop *server, glibtop_proc_uid *buf,
51 			pid_t pid)
52 {
53 	int mib[4];
54 	size_t length;
55 	struct kinfo_proc kinfo;
56 
57 	glibtop_init_p (server, (1 << GLIBTOP_SYSDEPS_PROC_UID), 0);
58 
59 	memset (buf, 0, sizeof (glibtop_proc_uid));
60 
61 	mib [0] = CTL_KERN;
62 	mib [1] = KERN_PROC;
63 	mib [2] = KERN_PROC_PID;
64 	mib [3] = pid;
65 
66 	length = sizeof (struct kinfo_proc);
67 	if (sysctl (mib, 4, &kinfo, &length, NULL, 0) < 0) {
68 		glibtop_warn_io_r (server, "sysctl (procuid)");
69 		return;
70 	}
71 
72 	buf->uid      = kinfo.kp_eproc.e_pcred.p_ruid;
73 	buf->euid     = kinfo.kp_eproc.e_pcred.p_svuid;
74 	buf->gid      = kinfo.kp_eproc.e_pcred.p_rgid;
75 	buf->egid     = kinfo.kp_eproc.e_pcred.p_svgid;
76 	buf->ppid     = kinfo.kp_eproc.e_ppid;
77 	buf->pgrp     = kinfo.kp_eproc.e_pgid;
78 	buf->tpgid    = kinfo.kp_eproc.e_tpgid;
79 	buf->nice     = kinfo.kp_proc.p_nice;
80 	buf->priority = kinfo.kp_proc.p_priority;
81 
82 	buf->flags    = _glibtop_sysdeps_proc_uid;
83 }
84