procfs_status.c revision 76166
1/*
2 * Copyright (c) 1993 Jan-Simon Pendry
3 * Copyright (c) 1993
4 *	The Regents of the University of California.  All rights reserved.
5 *
6 * This code is derived from software contributed to Berkeley by
7 * Jan-Simon Pendry.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 *    notice, this list of conditions and the following disclaimer in the
16 *    documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 *    must display the following acknowledgement:
19 *	This product includes software developed by the University of
20 *	California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 *    may be used to endorse or promote products derived from this software
23 *    without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 *
37 *	@(#)procfs_status.c	8.4 (Berkeley) 6/15/94
38 *
39 * From:
40 * $FreeBSD: head/sys/fs/procfs/procfs_status.c 76166 2001-05-01 08:13:21Z markm $
41 */
42
43#include <sys/param.h>
44#include <sys/systm.h>
45#include <sys/exec.h>
46#include <sys/jail.h>
47#include <sys/lock.h>
48#include <sys/malloc.h>
49#include <sys/mutex.h>
50#include <sys/proc.h>
51#include <sys/resourcevar.h>
52#include <sys/tty.h>
53#include <sys/vnode.h>
54
55#include <vm/vm.h>
56#include <vm/pmap.h>
57#include <vm/vm_param.h>
58
59#include <miscfs/procfs/procfs.h>
60
61#define DOCHECK() do { if (ps >= psbuf+sizeof(psbuf)) goto bailout; } while (0)
62int
63procfs_dostatus(curp, p, pfs, uio)
64	struct proc *curp;
65	struct proc *p;
66	struct pfsnode *pfs;
67	struct uio *uio;
68{
69	struct session *sess;
70	struct tty *tp;
71	struct ucred *cr;
72	char *ps;
73	char *sep;
74	int pid, ppid, pgid, sid;
75	int i;
76	int xlen;
77	int error;
78	char psbuf[256];	/* XXX - conservative */
79
80	if (uio->uio_rw != UIO_READ)
81		return (EOPNOTSUPP);
82
83	pid = p->p_pid;
84	PROC_LOCK(p);
85	ppid = p->p_pptr ? p->p_pptr->p_pid : 0;
86	PROC_UNLOCK(p);
87	pgid = p->p_pgrp->pg_id;
88	sess = p->p_pgrp->pg_session;
89	sid = sess->s_leader ? sess->s_leader->p_pid : 0;
90
91/* comm pid ppid pgid sid maj,min ctty,sldr start ut st wmsg
92                                euid ruid rgid,egid,groups[1 .. NGROUPS]
93*/
94	KASSERT(sizeof(psbuf) > MAXCOMLEN,
95			("Too short buffer for new MAXCOMLEN"));
96
97	ps = psbuf;
98	bcopy(p->p_comm, ps, MAXCOMLEN);
99	ps[MAXCOMLEN] = '\0';
100	ps += strlen(ps);
101	DOCHECK();
102	ps += snprintf(ps, psbuf + sizeof(psbuf) - ps,
103	    " %d %d %d %d ", pid, ppid, pgid, sid);
104	DOCHECK();
105	if ((p->p_flag&P_CONTROLT) && (tp = sess->s_ttyp))
106		ps += snprintf(ps, psbuf + sizeof(psbuf) - ps,
107		    "%d,%d ", major(tp->t_dev), minor(tp->t_dev));
108	else
109		ps += snprintf(ps, psbuf + sizeof(psbuf) - ps,
110		    "%d,%d ", -1, -1);
111	DOCHECK();
112
113	sep = "";
114	if (sess->s_ttyvp) {
115		ps += snprintf(ps, psbuf + sizeof(psbuf) - ps, "%sctty", sep);
116		sep = ",";
117		DOCHECK();
118	}
119	if (SESS_LEADER(p)) {
120		ps += snprintf(ps, psbuf + sizeof(psbuf) - ps, "%ssldr", sep);
121		sep = ",";
122		DOCHECK();
123	}
124	if (*sep != ',') {
125		ps += snprintf(ps, psbuf + sizeof(psbuf) - ps, "noflags");
126		DOCHECK();
127	}
128
129	mtx_lock_spin(&sched_lock);
130	if (p->p_sflag & PS_INMEM) {
131		struct timeval ut, st;
132
133		calcru(p, &ut, &st, (struct timeval *) NULL);
134		mtx_unlock_spin(&sched_lock);
135		ps += snprintf(ps, psbuf + sizeof(psbuf) - ps,
136		    " %ld,%ld %ld,%ld %ld,%ld",
137		    p->p_stats->p_start.tv_sec,
138		    p->p_stats->p_start.tv_usec,
139		    ut.tv_sec, ut.tv_usec,
140		    st.tv_sec, st.tv_usec);
141	} else {
142		mtx_unlock_spin(&sched_lock);
143		ps += snprintf(ps, psbuf + sizeof(psbuf) - ps,
144		    " -1,-1 -1,-1 -1,-1");
145	}
146	DOCHECK();
147
148	ps += snprintf(ps, psbuf + sizeof(psbuf) - ps, " %s",
149		(p->p_wchan && p->p_wmesg) ? p->p_wmesg : "nochan");
150	DOCHECK();
151
152	cr = p->p_ucred;
153
154	ps += snprintf(ps, psbuf + sizeof(psbuf) - ps, " %lu %lu %lu",
155		(u_long)cr->cr_uid,
156		(u_long)p->p_cred->p_ruid,
157		(u_long)p->p_cred->p_rgid);
158	DOCHECK();
159
160	/* egid (p->p_cred->p_svgid) is equal to cr_ngroups[0]
161	   see also getegid(2) in /sys/kern/kern_prot.c */
162
163	for (i = 0; i < cr->cr_ngroups; i++) {
164		ps += snprintf(ps, psbuf + sizeof(psbuf) - ps,
165		    ",%lu", (u_long)cr->cr_groups[i]);
166		DOCHECK();
167	}
168
169	if (jailed(p->p_ucred))
170		ps += snprintf(ps, psbuf + sizeof(psbuf) - ps,
171		    " %s", p->p_ucred->cr_prison->pr_host);
172	else
173		ps += snprintf(ps, psbuf + sizeof(psbuf) - ps, " -");
174	DOCHECK();
175	ps += snprintf(ps, psbuf + sizeof(psbuf) - ps, "\n");
176	DOCHECK();
177
178	xlen = ps - psbuf;
179	xlen -= uio->uio_offset;
180	ps = psbuf + uio->uio_offset;
181	xlen = imin(xlen, uio->uio_resid);
182	if (xlen <= 0)
183		error = 0;
184	else
185		error = uiomove(ps, xlen, uio);
186
187	return (error);
188
189bailout:
190	return (ENOMEM);
191}
192
193int
194procfs_docmdline(curp, p, pfs, uio)
195	struct proc *curp;
196	struct proc *p;
197	struct pfsnode *pfs;
198	struct uio *uio;
199{
200	char *ps;
201	int xlen;
202	int error;
203	char *buf, *bp;
204	int buflen;
205	struct ps_strings pstr;
206	int i;
207	size_t bytes_left, done;
208
209	if (uio->uio_rw != UIO_READ)
210		return (EOPNOTSUPP);
211
212	/*
213	 * If we are using the ps/cmdline caching, use that.  Otherwise
214	 * revert back to the old way which only implements full cmdline
215	 * for the currept process and just p->p_comm for all other
216	 * processes.
217	 * Note that if the argv is no longer available, we deliberately
218	 * don't fall back on p->p_comm or return an error: the authentic
219	 * Linux behaviour is to return zero-length in this case.
220	 */
221
222	if (p->p_args && (ps_argsopen || !p_can(curp, p, P_CAN_SEE, NULL))) {
223		bp = p->p_args->ar_args;
224		buflen = p->p_args->ar_length;
225		buf = 0;
226	} else if (p != curp) {
227		bp = p->p_comm;
228		buflen = MAXCOMLEN;
229		buf = 0;
230	} else {
231		buflen = 256;
232		MALLOC(buf, char *, buflen + 1, M_TEMP, M_WAITOK);
233		bp = buf;
234		ps = buf;
235		error = copyin((void*)PS_STRINGS, &pstr, sizeof(pstr));
236		if (error) {
237			FREE(buf, M_TEMP);
238			return (error);
239		}
240		bytes_left = buflen;
241		for (i = 0; bytes_left && (i < pstr.ps_nargvstr); i++) {
242			error = copyinstr(pstr.ps_argvstr[i], ps,
243					  bytes_left, &done);
244			/* If too long or malformed, just truncate */
245			if (error) {
246				error = 0;
247				break;
248			}
249			ps += done;
250			bytes_left -= done;
251		}
252		buflen = ps - buf;
253	}
254
255	buflen -= uio->uio_offset;
256	ps = bp + uio->uio_offset;
257	xlen = min(buflen, uio->uio_resid);
258	if (xlen <= 0)
259		error = 0;
260	else
261		error = uiomove(ps, xlen, uio);
262	if (buf)
263		FREE(buf, M_TEMP);
264	return (error);
265}
266