xref: /netbsd-src/sys/miscfs/procfs/procfs_status.c (revision 0eaaa024ea5a271b8f91fe07fe846a86f31dd9b9)
1*0eaaa024Sad /*	$NetBSD: procfs_status.c,v 1.40 2020/05/23 23:42:43 ad Exp $	*/
2aad01611Sagc 
3aad01611Sagc /*
4aad01611Sagc  * Copyright (c) 1993
5aad01611Sagc  *	The Regents of the University of California.  All rights reserved.
6aad01611Sagc  *
7aad01611Sagc  * This code is derived from software contributed to Berkeley by
8aad01611Sagc  * Jan-Simon Pendry.
9aad01611Sagc  *
10aad01611Sagc  * Redistribution and use in source and binary forms, with or without
11aad01611Sagc  * modification, are permitted provided that the following conditions
12aad01611Sagc  * are met:
13aad01611Sagc  * 1. Redistributions of source code must retain the above copyright
14aad01611Sagc  *    notice, this list of conditions and the following disclaimer.
15aad01611Sagc  * 2. Redistributions in binary form must reproduce the above copyright
16aad01611Sagc  *    notice, this list of conditions and the following disclaimer in the
17aad01611Sagc  *    documentation and/or other materials provided with the distribution.
18aad01611Sagc  * 3. Neither the name of the University nor the names of its contributors
19aad01611Sagc  *    may be used to endorse or promote products derived from this software
20aad01611Sagc  *    without specific prior written permission.
21aad01611Sagc  *
22aad01611Sagc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23aad01611Sagc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24aad01611Sagc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25aad01611Sagc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26aad01611Sagc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27aad01611Sagc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28aad01611Sagc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29aad01611Sagc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30aad01611Sagc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31aad01611Sagc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32aad01611Sagc  * SUCH DAMAGE.
33aad01611Sagc  *
34aad01611Sagc  *	@(#)procfs_status.c	8.4 (Berkeley) 6/15/94
35aad01611Sagc  */
36cf92afd6Scgd 
37699e3be9Scgd /*
38699e3be9Scgd  * Copyright (c) 1993 Jan-Simon Pendry
39699e3be9Scgd  *
40699e3be9Scgd  * This code is derived from software contributed to Berkeley by
41699e3be9Scgd  * Jan-Simon Pendry.
42699e3be9Scgd  *
43699e3be9Scgd  * Redistribution and use in source and binary forms, with or without
44699e3be9Scgd  * modification, are permitted provided that the following conditions
45699e3be9Scgd  * are met:
46699e3be9Scgd  * 1. Redistributions of source code must retain the above copyright
47699e3be9Scgd  *    notice, this list of conditions and the following disclaimer.
48699e3be9Scgd  * 2. Redistributions in binary form must reproduce the above copyright
49699e3be9Scgd  *    notice, this list of conditions and the following disclaimer in the
50699e3be9Scgd  *    documentation and/or other materials provided with the distribution.
51699e3be9Scgd  * 3. All advertising materials mentioning features or use of this software
52699e3be9Scgd  *    must display the following acknowledgement:
53699e3be9Scgd  *	This product includes software developed by the University of
54699e3be9Scgd  *	California, Berkeley and its contributors.
55699e3be9Scgd  * 4. Neither the name of the University nor the names of its contributors
56699e3be9Scgd  *    may be used to endorse or promote products derived from this software
57699e3be9Scgd  *    without specific prior written permission.
58699e3be9Scgd  *
59699e3be9Scgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60699e3be9Scgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61699e3be9Scgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62699e3be9Scgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63699e3be9Scgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64699e3be9Scgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65699e3be9Scgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66699e3be9Scgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67699e3be9Scgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68699e3be9Scgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69699e3be9Scgd  * SUCH DAMAGE.
70699e3be9Scgd  *
71cf92afd6Scgd  *	@(#)procfs_status.c	8.4 (Berkeley) 6/15/94
72699e3be9Scgd  */
73699e3be9Scgd 
74e4b00f43Slukem #include <sys/cdefs.h>
75*0eaaa024Sad __KERNEL_RCSID(0, "$NetBSD: procfs_status.c,v 1.40 2020/05/23 23:42:43 ad Exp $");
76e4b00f43Slukem 
77699e3be9Scgd #include <sys/param.h>
78699e3be9Scgd #include <sys/systm.h>
79699e3be9Scgd #include <sys/time.h>
80699e3be9Scgd #include <sys/kernel.h>
81699e3be9Scgd #include <sys/proc.h>
82699e3be9Scgd #include <sys/vnode.h>
83699e3be9Scgd #include <sys/ioctl.h>
84699e3be9Scgd #include <sys/tty.h>
85699e3be9Scgd #include <sys/resource.h>
86699e3be9Scgd #include <sys/resourcevar.h>
87fc9422c9Selad #include <sys/kauth.h>
88fc9422c9Selad 
89699e3be9Scgd #include <miscfs/procfs/procfs.h>
90699e3be9Scgd 
91e48fa123Schristos static int
procfs_status_netbsd(struct lwp * l,struct uio * uio)92e48fa123Schristos procfs_status_netbsd(struct lwp *l, struct uio *uio)
93699e3be9Scgd {
94699e3be9Scgd 	struct session *sess;
95699e3be9Scgd 	struct tty *tp;
96fc9422c9Selad 	kauth_cred_t cr;
97b78f59b4Sthorpej 	struct proc *p = l->l_proc;
98699e3be9Scgd 	char *ps;
99c107ef9eSchristos 	const char *sep;
100e48fa123Schristos 	const char *emulname = curlwp->l_proc->p_emul->e_name;
101699e3be9Scgd 	int pid, ppid, pgid, sid;
102b7d2ca25Sthorpej 	u_int i;
103d0ec2167Sdavid 	char psbuf[256+MAXHOSTNAMELEN];		/* XXX - conservative */
104fc9422c9Selad 	uint16_t ngroups;
105699e3be9Scgd 
106699e3be9Scgd 
107*0eaaa024Sad 	mutex_enter(&proc_lock);
108284c2b9aSad 	mutex_enter(p->p_lock);
109b07ec3fcSad 
110699e3be9Scgd 	pid = p->p_pid;
11175973081Skre 	ppid = p->p_ppid;
112699e3be9Scgd 	pgid = p->p_pgrp->pg_id;
113699e3be9Scgd 	sess = p->p_pgrp->pg_session;
114d2924ae8Sthorpej 	sid = sess->s_sid;
115699e3be9Scgd 
116e48fa123Schristos 	ps = psbuf;
117e48fa123Schristos 	if (strncmp(emulname, "linux", 5) == 0) {
118e48fa123Schristos 		ps += snprintf(ps, sizeof(psbuf), "Name:\t%s\n", p->p_comm);
119e48fa123Schristos 		ps += snprintf(ps, sizeof(psbuf) - (ps - psbuf), "Pid:\t%d\n", pid);
120e48fa123Schristos 		ps += snprintf(ps, sizeof(psbuf) - (ps - psbuf), "PPid:\t%d\n", ppid);
121e48fa123Schristos 		ps += snprintf(ps, sizeof(psbuf) - (ps - psbuf), "TracerPid:\t%d\n", 0);
122e48fa123Schristos 
123e48fa123Schristos 		cr = p->p_cred;
124e48fa123Schristos 		ngroups = kauth_cred_ngroups(cr);
125e48fa123Schristos 		ps += snprintf(ps, sizeof(psbuf) - (ps - psbuf), "Groups:\t");
126e48fa123Schristos 		for (i = 0; i < ngroups; i++)
127e48fa123Schristos 			ps += snprintf(ps, sizeof(psbuf) - (ps - psbuf), "%d ",
128e48fa123Schristos 			    kauth_cred_group(cr, i));
129e48fa123Schristos 		ps += snprintf(ps, sizeof(psbuf) - (ps - psbuf), "\n");
130e48fa123Schristos 
131e48fa123Schristos 		ps += snprintf(ps, sizeof(psbuf) - (ps - psbuf),
1326bf17633Skre 		    "VmPeak:\t%8ju kB\n", (intmax_t)p->p_rlimit[RLIMIT_DATA].rlim_cur / 1024);
133e48fa123Schristos 		ps += snprintf(ps, sizeof(psbuf) - (ps - psbuf),
1346bf17633Skre 		    "VmSize:\t%8ju kB\n", (intmax_t)p->p_rlimit[RLIMIT_DATA].rlim_cur / 1024);
135e48fa123Schristos 		ps += snprintf(ps, sizeof(psbuf) - (ps - psbuf),
1366bf17633Skre 		    "VmRSS:\t%8ju kB\n", (intmax_t)p->p_rlimit[RLIMIT_RSS].rlim_cur / 1024);
137e48fa123Schristos 
138e48fa123Schristos 	} else {
13995ded74fSjtc /* comm pid ppid pgid sid maj,min ctty,sldr start ut st wmsg uid gid groups ... */
140699e3be9Scgd 
1414522c799Sperry 		memcpy(ps, p->p_comm, MAXCOMLEN);
142699e3be9Scgd 		ps[MAXCOMLEN] = '\0';
143699e3be9Scgd 		ps += strlen(ps);
1448bcb745dSitojun 		ps += snprintf(ps, sizeof(psbuf) - (ps - psbuf), " %d %d %d %d ",
1458bcb745dSitojun 		    pid, ppid, pgid, sid);
146699e3be9Scgd 
147b07ec3fcSad 		if ((p->p_lflag & PL_CONTROLT) && (tp = sess->s_ttyp))
148461a86f9Schristos 			ps += snprintf(ps, sizeof(psbuf) - (ps - psbuf), "%llu,%llu ",
149461a86f9Schristos 			    (unsigned long long)major(tp->t_dev),
150461a86f9Schristos 			    (unsigned long long)minor(tp->t_dev));
151699e3be9Scgd 		else
1528bcb745dSitojun 			ps += snprintf(ps, sizeof(psbuf) - (ps - psbuf), "%d,%d ",
1538bcb745dSitojun 			    -1, -1);
154699e3be9Scgd 
155699e3be9Scgd 		sep = "";
156699e3be9Scgd 		if (sess->s_ttyvp) {
1578bcb745dSitojun 			ps += snprintf(ps, sizeof(psbuf) - (ps - psbuf), "%sctty", sep);
158699e3be9Scgd 			sep = ",";
159699e3be9Scgd 		}
160699e3be9Scgd 		if (SESS_LEADER(p)) {
1618bcb745dSitojun 			ps += snprintf(ps, sizeof(psbuf) - (ps - psbuf), "%ssldr", sep);
162699e3be9Scgd 			sep = ",";
163699e3be9Scgd 		}
164699e3be9Scgd 		if (*sep != ',')
1658bcb745dSitojun 			ps += snprintf(ps, sizeof(psbuf) - (ps - psbuf), "noflags");
166699e3be9Scgd 
167461a86f9Schristos 		ps += snprintf(ps, sizeof(psbuf) - (ps - psbuf), " %lld,%ld",
168461a86f9Schristos 		    (long long)p->p_stats->p_start.tv_sec,
169461a86f9Schristos 		    (long)p->p_stats->p_start.tv_usec);
170699e3be9Scgd 
171a0a74294Scgd 		{
172a0a74294Scgd 			struct timeval ut, st;
173a0a74294Scgd 
174b07ec3fcSad 			calcru(p, &ut, &st, (void *) 0, NULL);
1758bcb745dSitojun 			ps += snprintf(ps, sizeof(psbuf) - (ps - psbuf),
176461a86f9Schristos 			    " %lld,%ld %lld,%ld", (long long)ut.tv_sec,
177461a86f9Schristos 			    (long)ut.tv_usec, (long long)st.tv_sec, (long)st.tv_usec);
178a0a74294Scgd 		}
179699e3be9Scgd 
180b07ec3fcSad 		lwp_lock(l);
1818bcb745dSitojun 		ps += snprintf(ps, sizeof(psbuf) - (ps - psbuf), " %s",
182b78f59b4Sthorpej 		    (l->l_wchan && l->l_wmesg) ? l->l_wmesg : "nochan");
183b07ec3fcSad 		lwp_unlock(l);
184699e3be9Scgd 
185fc9422c9Selad 		cr = p->p_cred;
186699e3be9Scgd 
187fc9422c9Selad 		ps += snprintf(ps, sizeof(psbuf) - (ps - psbuf), " %d",
188fc9422c9Selad 		    kauth_cred_geteuid(cr));
189fc9422c9Selad 		ps += snprintf(ps, sizeof(psbuf) - (ps - psbuf), " %d",
190fc9422c9Selad 		    kauth_cred_getegid(cr));
191fc9422c9Selad 		ngroups = kauth_cred_ngroups(cr);
192fc9422c9Selad 		for (i = 0; i < ngroups; i++)
1938bcb745dSitojun 			ps += snprintf(ps, sizeof(psbuf) - (ps - psbuf), ",%d",
194fc9422c9Selad 			    kauth_cred_group(cr, i));
1958bcb745dSitojun 		ps += snprintf(ps, sizeof(psbuf) - (ps - psbuf), "\n");
196e48fa123Schristos 	}
197e48fa123Schristos 	mutex_exit(p->p_lock);
198*0eaaa024Sad 	mutex_exit(&proc_lock);
199e48fa123Schristos 
200e48fa123Schristos 	return (uiomove_frombuf(psbuf, ps - psbuf, uio));
201e48fa123Schristos }
202e48fa123Schristos 
203e48fa123Schristos static int
procfs_status_linux(struct lwp * l,struct uio * uio)204e48fa123Schristos procfs_status_linux(struct lwp *l, struct uio *uio)
205e48fa123Schristos {
206e48fa123Schristos 	kauth_cred_t cr;
207e48fa123Schristos 	struct proc *p = l->l_proc;
208e48fa123Schristos 	char *ps;
209e48fa123Schristos 	int pid, ppid;
210e48fa123Schristos 	u_int i;
211e48fa123Schristos 	char psbuf[256+MAXHOSTNAMELEN];		/* XXX - conservative */
212e48fa123Schristos 	uint16_t ngroups;
213e48fa123Schristos 
214*0eaaa024Sad 	mutex_enter(&proc_lock);
215e48fa123Schristos 	mutex_enter(p->p_lock);
216e48fa123Schristos 
217e48fa123Schristos 	pid = p->p_pid;
218e48fa123Schristos 	ppid = p->p_ppid;
219e48fa123Schristos 
220e48fa123Schristos 	ps = psbuf;
221e48fa123Schristos 	ps += snprintf(ps, sizeof(psbuf), "Name:\t%s\n", p->p_comm);
222e48fa123Schristos 	ps += snprintf(ps, sizeof(psbuf) - (ps - psbuf), "Pid:\t%d\n", pid);
223e48fa123Schristos 	ps += snprintf(ps, sizeof(psbuf) - (ps - psbuf), "PPid:\t%d\n", ppid);
224e48fa123Schristos 	ps += snprintf(ps, sizeof(psbuf) - (ps - psbuf), "TracerPid:\t%d\n", 0);
225e48fa123Schristos 
226e48fa123Schristos 	cr = p->p_cred;
227e48fa123Schristos 	ngroups = kauth_cred_ngroups(cr);
228e48fa123Schristos 	ps += snprintf(ps, sizeof(psbuf) - (ps - psbuf), "Groups:\t");
229e48fa123Schristos 	for (i = 0; i < ngroups; i++)
230e48fa123Schristos 		ps += snprintf(ps, sizeof(psbuf) - (ps - psbuf), "%d ",
231e48fa123Schristos 		    kauth_cred_group(cr, i));
232e48fa123Schristos 	ps += snprintf(ps, sizeof(psbuf) - (ps - psbuf), "\n");
233e48fa123Schristos 
234e48fa123Schristos 	ps += snprintf(ps, sizeof(psbuf) - (ps - psbuf),
235e48fa123Schristos 	    "VmPeak:\t%8" PRIu64 " kB\n", p->p_rlimit[RLIMIT_DATA].rlim_cur / 1024);
236e48fa123Schristos 	ps += snprintf(ps, sizeof(psbuf) - (ps - psbuf),
237e48fa123Schristos 	    "VmSize:\t%8" PRIu64 " kB\n", p->p_rlimit[RLIMIT_DATA].rlim_cur / 1024);
238e48fa123Schristos 	ps += snprintf(ps, sizeof(psbuf) - (ps - psbuf),
239e48fa123Schristos 	    "VmRSS:\t%8" PRIu64 " kB\n", p->p_rlimit[RLIMIT_RSS].rlim_cur / 1024);
240699e3be9Scgd 
241284c2b9aSad 	mutex_exit(p->p_lock);
242*0eaaa024Sad 	mutex_exit(&proc_lock);
243b07ec3fcSad 
2448bcb745dSitojun 	return (uiomove_frombuf(psbuf, ps - psbuf, uio));
245699e3be9Scgd }
246e48fa123Schristos 
247e48fa123Schristos int
procfs_dostatus(struct lwp * curl,struct lwp * l,struct pfsnode * pfs,struct uio * uio)248e48fa123Schristos procfs_dostatus(struct lwp *curl, struct lwp *l, struct pfsnode *pfs,
249e48fa123Schristos     struct uio *uio)
250e48fa123Schristos {
251e48fa123Schristos 	const char *emulname = curlwp->l_proc->p_emul->e_name;
252e48fa123Schristos 
253e48fa123Schristos 	if (uio->uio_rw != UIO_READ)
254e48fa123Schristos 		return (EOPNOTSUPP);
255e48fa123Schristos 
256e48fa123Schristos 	if (strncmp(emulname, "linux", 5) == 0) {
257e48fa123Schristos 		return procfs_status_linux(l, uio);
258e48fa123Schristos 	} else {
259e48fa123Schristos 		return procfs_status_netbsd(l, uio);
260e48fa123Schristos 	}
261e48fa123Schristos }
262