1*14b4bbb2Sad /* $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*14b4bbb2Sad __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
91281933f1Schristos static int
procfs_status_netbsd(struct lwp * l,struct uio * uio)92281933f1Schristos 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;
100281933f1Schristos 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*14b4bbb2Sad mutex_enter(&proc_lock);
108284c2b9aSad mutex_enter(p->p_lock);
109b07ec3fcSad
110699e3be9Scgd pid = p->p_pid;
111c9e8791fSkre ppid = p->p_ppid;
112699e3be9Scgd pgid = p->p_pgrp->pg_id;
113699e3be9Scgd sess = p->p_pgrp->pg_session;
114d2924ae8Sthorpej sid = sess->s_sid;
115699e3be9Scgd
116281933f1Schristos ps = psbuf;
117281933f1Schristos if (strncmp(emulname, "linux", 5) == 0) {
118281933f1Schristos ps += snprintf(ps, sizeof(psbuf), "Name:\t%s\n", p->p_comm);
119281933f1Schristos ps += snprintf(ps, sizeof(psbuf) - (ps - psbuf), "Pid:\t%d\n", pid);
120281933f1Schristos ps += snprintf(ps, sizeof(psbuf) - (ps - psbuf), "PPid:\t%d\n", ppid);
121281933f1Schristos ps += snprintf(ps, sizeof(psbuf) - (ps - psbuf), "TracerPid:\t%d\n", 0);
122281933f1Schristos
123281933f1Schristos cr = p->p_cred;
124281933f1Schristos ngroups = kauth_cred_ngroups(cr);
125281933f1Schristos ps += snprintf(ps, sizeof(psbuf) - (ps - psbuf), "Groups:\t");
126281933f1Schristos for (i = 0; i < ngroups; i++)
127281933f1Schristos ps += snprintf(ps, sizeof(psbuf) - (ps - psbuf), "%d ",
128281933f1Schristos kauth_cred_group(cr, i));
129281933f1Schristos ps += snprintf(ps, sizeof(psbuf) - (ps - psbuf), "\n");
130281933f1Schristos
131281933f1Schristos ps += snprintf(ps, sizeof(psbuf) - (ps - psbuf),
132357af5b3Skre "VmPeak:\t%8ju kB\n", (intmax_t)p->p_rlimit[RLIMIT_DATA].rlim_cur / 1024);
133281933f1Schristos ps += snprintf(ps, sizeof(psbuf) - (ps - psbuf),
134357af5b3Skre "VmSize:\t%8ju kB\n", (intmax_t)p->p_rlimit[RLIMIT_DATA].rlim_cur / 1024);
135281933f1Schristos ps += snprintf(ps, sizeof(psbuf) - (ps - psbuf),
136357af5b3Skre "VmRSS:\t%8ju kB\n", (intmax_t)p->p_rlimit[RLIMIT_RSS].rlim_cur / 1024);
137281933f1Schristos
138281933f1Schristos } 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");
196281933f1Schristos }
197281933f1Schristos mutex_exit(p->p_lock);
198*14b4bbb2Sad mutex_exit(&proc_lock);
199281933f1Schristos
200281933f1Schristos return (uiomove_frombuf(psbuf, ps - psbuf, uio));
201281933f1Schristos }
202281933f1Schristos
203281933f1Schristos static int
procfs_status_linux(struct lwp * l,struct uio * uio)204281933f1Schristos procfs_status_linux(struct lwp *l, struct uio *uio)
205281933f1Schristos {
206281933f1Schristos kauth_cred_t cr;
207281933f1Schristos struct proc *p = l->l_proc;
208281933f1Schristos char *ps;
209281933f1Schristos int pid, ppid;
210281933f1Schristos u_int i;
211281933f1Schristos char psbuf[256+MAXHOSTNAMELEN]; /* XXX - conservative */
212281933f1Schristos uint16_t ngroups;
213281933f1Schristos
214*14b4bbb2Sad mutex_enter(&proc_lock);
215281933f1Schristos mutex_enter(p->p_lock);
216281933f1Schristos
217281933f1Schristos pid = p->p_pid;
218281933f1Schristos ppid = p->p_ppid;
219281933f1Schristos
220281933f1Schristos ps = psbuf;
221281933f1Schristos ps += snprintf(ps, sizeof(psbuf), "Name:\t%s\n", p->p_comm);
222281933f1Schristos ps += snprintf(ps, sizeof(psbuf) - (ps - psbuf), "Pid:\t%d\n", pid);
223281933f1Schristos ps += snprintf(ps, sizeof(psbuf) - (ps - psbuf), "PPid:\t%d\n", ppid);
224281933f1Schristos ps += snprintf(ps, sizeof(psbuf) - (ps - psbuf), "TracerPid:\t%d\n", 0);
225281933f1Schristos
226281933f1Schristos cr = p->p_cred;
227281933f1Schristos ngroups = kauth_cred_ngroups(cr);
228281933f1Schristos ps += snprintf(ps, sizeof(psbuf) - (ps - psbuf), "Groups:\t");
229281933f1Schristos for (i = 0; i < ngroups; i++)
230281933f1Schristos ps += snprintf(ps, sizeof(psbuf) - (ps - psbuf), "%d ",
231281933f1Schristos kauth_cred_group(cr, i));
232281933f1Schristos ps += snprintf(ps, sizeof(psbuf) - (ps - psbuf), "\n");
233281933f1Schristos
234281933f1Schristos ps += snprintf(ps, sizeof(psbuf) - (ps - psbuf),
235281933f1Schristos "VmPeak:\t%8" PRIu64 " kB\n", p->p_rlimit[RLIMIT_DATA].rlim_cur / 1024);
236281933f1Schristos ps += snprintf(ps, sizeof(psbuf) - (ps - psbuf),
237281933f1Schristos "VmSize:\t%8" PRIu64 " kB\n", p->p_rlimit[RLIMIT_DATA].rlim_cur / 1024);
238281933f1Schristos ps += snprintf(ps, sizeof(psbuf) - (ps - psbuf),
239281933f1Schristos "VmRSS:\t%8" PRIu64 " kB\n", p->p_rlimit[RLIMIT_RSS].rlim_cur / 1024);
240699e3be9Scgd
241284c2b9aSad mutex_exit(p->p_lock);
242*14b4bbb2Sad mutex_exit(&proc_lock);
243b07ec3fcSad
2448bcb745dSitojun return (uiomove_frombuf(psbuf, ps - psbuf, uio));
245699e3be9Scgd }
246281933f1Schristos
247281933f1Schristos int
procfs_dostatus(struct lwp * curl,struct lwp * l,struct pfsnode * pfs,struct uio * uio)248281933f1Schristos procfs_dostatus(struct lwp *curl, struct lwp *l, struct pfsnode *pfs,
249281933f1Schristos struct uio *uio)
250281933f1Schristos {
251281933f1Schristos const char *emulname = curlwp->l_proc->p_emul->e_name;
252281933f1Schristos
253281933f1Schristos if (uio->uio_rw != UIO_READ)
254281933f1Schristos return (EOPNOTSUPP);
255281933f1Schristos
256281933f1Schristos if (strncmp(emulname, "linux", 5) == 0) {
257281933f1Schristos return procfs_status_linux(l, uio);
258281933f1Schristos } else {
259281933f1Schristos return procfs_status_netbsd(l, uio);
260281933f1Schristos }
261281933f1Schristos }
262