xref: /netbsd-src/sys/miscfs/procfs/procfs_status.c (revision 76c7fc5f6b13ed0b1508e6b313e88e59977ed78e)
1 /*	$NetBSD: procfs_status.c,v 1.39 2017/09/29 17:27:26 kre Exp $	*/
2 
3 /*
4  * Copyright (c) 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Jan-Simon Pendry.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  *	@(#)procfs_status.c	8.4 (Berkeley) 6/15/94
35  */
36 
37 /*
38  * Copyright (c) 1993 Jan-Simon Pendry
39  *
40  * This code is derived from software contributed to Berkeley by
41  * Jan-Simon Pendry.
42  *
43  * Redistribution and use in source and binary forms, with or without
44  * modification, are permitted provided that the following conditions
45  * are met:
46  * 1. Redistributions of source code must retain the above copyright
47  *    notice, this list of conditions and the following disclaimer.
48  * 2. Redistributions in binary form must reproduce the above copyright
49  *    notice, this list of conditions and the following disclaimer in the
50  *    documentation and/or other materials provided with the distribution.
51  * 3. All advertising materials mentioning features or use of this software
52  *    must display the following acknowledgement:
53  *	This product includes software developed by the University of
54  *	California, Berkeley and its contributors.
55  * 4. Neither the name of the University nor the names of its contributors
56  *    may be used to endorse or promote products derived from this software
57  *    without specific prior written permission.
58  *
59  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69  * SUCH DAMAGE.
70  *
71  *	@(#)procfs_status.c	8.4 (Berkeley) 6/15/94
72  */
73 
74 #include <sys/cdefs.h>
75 __KERNEL_RCSID(0, "$NetBSD: procfs_status.c,v 1.39 2017/09/29 17:27:26 kre Exp $");
76 
77 #include <sys/param.h>
78 #include <sys/systm.h>
79 #include <sys/time.h>
80 #include <sys/kernel.h>
81 #include <sys/proc.h>
82 #include <sys/vnode.h>
83 #include <sys/ioctl.h>
84 #include <sys/tty.h>
85 #include <sys/resource.h>
86 #include <sys/resourcevar.h>
87 #include <sys/kauth.h>
88 
89 #include <miscfs/procfs/procfs.h>
90 
91 static int
92 procfs_status_netbsd(struct lwp *l, struct uio *uio)
93 {
94 	struct session *sess;
95 	struct tty *tp;
96 	kauth_cred_t cr;
97 	struct proc *p = l->l_proc;
98 	char *ps;
99 	const char *sep;
100 	const char *emulname = curlwp->l_proc->p_emul->e_name;
101 	int pid, ppid, pgid, sid;
102 	u_int i;
103 	char psbuf[256+MAXHOSTNAMELEN];		/* XXX - conservative */
104 	uint16_t ngroups;
105 
106 
107 	mutex_enter(proc_lock);
108 	mutex_enter(p->p_lock);
109 
110 	pid = p->p_pid;
111 	ppid = p->p_ppid;
112 	pgid = p->p_pgrp->pg_id;
113 	sess = p->p_pgrp->pg_session;
114 	sid = sess->s_sid;
115 
116 	ps = psbuf;
117 	if (strncmp(emulname, "linux", 5) == 0) {
118 		ps += snprintf(ps, sizeof(psbuf), "Name:\t%s\n", p->p_comm);
119 		ps += snprintf(ps, sizeof(psbuf) - (ps - psbuf), "Pid:\t%d\n", pid);
120 		ps += snprintf(ps, sizeof(psbuf) - (ps - psbuf), "PPid:\t%d\n", ppid);
121 		ps += snprintf(ps, sizeof(psbuf) - (ps - psbuf), "TracerPid:\t%d\n", 0);
122 
123 		cr = p->p_cred;
124 		ngroups = kauth_cred_ngroups(cr);
125 		ps += snprintf(ps, sizeof(psbuf) - (ps - psbuf), "Groups:\t");
126 		for (i = 0; i < ngroups; i++)
127 			ps += snprintf(ps, sizeof(psbuf) - (ps - psbuf), "%d ",
128 			    kauth_cred_group(cr, i));
129 		ps += snprintf(ps, sizeof(psbuf) - (ps - psbuf), "\n");
130 
131 		ps += snprintf(ps, sizeof(psbuf) - (ps - psbuf),
132 		    "VmPeak:\t%8ju kB\n", (intmax_t)p->p_rlimit[RLIMIT_DATA].rlim_cur / 1024);
133 		ps += snprintf(ps, sizeof(psbuf) - (ps - psbuf),
134 		    "VmSize:\t%8ju kB\n", (intmax_t)p->p_rlimit[RLIMIT_DATA].rlim_cur / 1024);
135 		ps += snprintf(ps, sizeof(psbuf) - (ps - psbuf),
136 		    "VmRSS:\t%8ju kB\n", (intmax_t)p->p_rlimit[RLIMIT_RSS].rlim_cur / 1024);
137 
138 	} else {
139 /* comm pid ppid pgid sid maj,min ctty,sldr start ut st wmsg uid gid groups ... */
140 
141 		memcpy(ps, p->p_comm, MAXCOMLEN);
142 		ps[MAXCOMLEN] = '\0';
143 		ps += strlen(ps);
144 		ps += snprintf(ps, sizeof(psbuf) - (ps - psbuf), " %d %d %d %d ",
145 		    pid, ppid, pgid, sid);
146 
147 		if ((p->p_lflag & PL_CONTROLT) && (tp = sess->s_ttyp))
148 			ps += snprintf(ps, sizeof(psbuf) - (ps - psbuf), "%llu,%llu ",
149 			    (unsigned long long)major(tp->t_dev),
150 			    (unsigned long long)minor(tp->t_dev));
151 		else
152 			ps += snprintf(ps, sizeof(psbuf) - (ps - psbuf), "%d,%d ",
153 			    -1, -1);
154 
155 		sep = "";
156 		if (sess->s_ttyvp) {
157 			ps += snprintf(ps, sizeof(psbuf) - (ps - psbuf), "%sctty", sep);
158 			sep = ",";
159 		}
160 		if (SESS_LEADER(p)) {
161 			ps += snprintf(ps, sizeof(psbuf) - (ps - psbuf), "%ssldr", sep);
162 			sep = ",";
163 		}
164 		if (*sep != ',')
165 			ps += snprintf(ps, sizeof(psbuf) - (ps - psbuf), "noflags");
166 
167 		ps += snprintf(ps, sizeof(psbuf) - (ps - psbuf), " %lld,%ld",
168 		    (long long)p->p_stats->p_start.tv_sec,
169 		    (long)p->p_stats->p_start.tv_usec);
170 
171 		{
172 			struct timeval ut, st;
173 
174 			calcru(p, &ut, &st, (void *) 0, NULL);
175 			ps += snprintf(ps, sizeof(psbuf) - (ps - psbuf),
176 			    " %lld,%ld %lld,%ld", (long long)ut.tv_sec,
177 			    (long)ut.tv_usec, (long long)st.tv_sec, (long)st.tv_usec);
178 		}
179 
180 		lwp_lock(l);
181 		ps += snprintf(ps, sizeof(psbuf) - (ps - psbuf), " %s",
182 		    (l->l_wchan && l->l_wmesg) ? l->l_wmesg : "nochan");
183 		lwp_unlock(l);
184 
185 		cr = p->p_cred;
186 
187 		ps += snprintf(ps, sizeof(psbuf) - (ps - psbuf), " %d",
188 		    kauth_cred_geteuid(cr));
189 		ps += snprintf(ps, sizeof(psbuf) - (ps - psbuf), " %d",
190 		    kauth_cred_getegid(cr));
191 		ngroups = kauth_cred_ngroups(cr);
192 		for (i = 0; i < ngroups; i++)
193 			ps += snprintf(ps, sizeof(psbuf) - (ps - psbuf), ",%d",
194 			    kauth_cred_group(cr, i));
195 		ps += snprintf(ps, sizeof(psbuf) - (ps - psbuf), "\n");
196 	}
197 	mutex_exit(p->p_lock);
198 	mutex_exit(proc_lock);
199 
200 	return (uiomove_frombuf(psbuf, ps - psbuf, uio));
201 }
202 
203 static int
204 procfs_status_linux(struct lwp *l, struct uio *uio)
205 {
206 	kauth_cred_t cr;
207 	struct proc *p = l->l_proc;
208 	char *ps;
209 	int pid, ppid;
210 	u_int i;
211 	char psbuf[256+MAXHOSTNAMELEN];		/* XXX - conservative */
212 	uint16_t ngroups;
213 
214 	mutex_enter(proc_lock);
215 	mutex_enter(p->p_lock);
216 
217 	pid = p->p_pid;
218 	ppid = p->p_ppid;
219 
220 	ps = psbuf;
221 	ps += snprintf(ps, sizeof(psbuf), "Name:\t%s\n", p->p_comm);
222 	ps += snprintf(ps, sizeof(psbuf) - (ps - psbuf), "Pid:\t%d\n", pid);
223 	ps += snprintf(ps, sizeof(psbuf) - (ps - psbuf), "PPid:\t%d\n", ppid);
224 	ps += snprintf(ps, sizeof(psbuf) - (ps - psbuf), "TracerPid:\t%d\n", 0);
225 
226 	cr = p->p_cred;
227 	ngroups = kauth_cred_ngroups(cr);
228 	ps += snprintf(ps, sizeof(psbuf) - (ps - psbuf), "Groups:\t");
229 	for (i = 0; i < ngroups; i++)
230 		ps += snprintf(ps, sizeof(psbuf) - (ps - psbuf), "%d ",
231 		    kauth_cred_group(cr, i));
232 	ps += snprintf(ps, sizeof(psbuf) - (ps - psbuf), "\n");
233 
234 	ps += snprintf(ps, sizeof(psbuf) - (ps - psbuf),
235 	    "VmPeak:\t%8" PRIu64 " kB\n", p->p_rlimit[RLIMIT_DATA].rlim_cur / 1024);
236 	ps += snprintf(ps, sizeof(psbuf) - (ps - psbuf),
237 	    "VmSize:\t%8" PRIu64 " kB\n", p->p_rlimit[RLIMIT_DATA].rlim_cur / 1024);
238 	ps += snprintf(ps, sizeof(psbuf) - (ps - psbuf),
239 	    "VmRSS:\t%8" PRIu64 " kB\n", p->p_rlimit[RLIMIT_RSS].rlim_cur / 1024);
240 
241 	mutex_exit(p->p_lock);
242 	mutex_exit(proc_lock);
243 
244 	return (uiomove_frombuf(psbuf, ps - psbuf, uio));
245 }
246 
247 int
248 procfs_dostatus(struct lwp *curl, struct lwp *l, struct pfsnode *pfs,
249     struct uio *uio)
250 {
251 	const char *emulname = curlwp->l_proc->p_emul->e_name;
252 
253 	if (uio->uio_rw != UIO_READ)
254 		return (EOPNOTSUPP);
255 
256 	if (strncmp(emulname, "linux", 5) == 0) {
257 		return procfs_status_linux(l, uio);
258 	} else {
259 		return procfs_status_netbsd(l, uio);
260 	}
261 }
262