xref: /freebsd/sys/fs/procfs/procfs_status.c (revision fb919e4d5a2c1baca52ac70d1064f140fffdda71)
1df8bae1dSRodney W. Grimes /*
2df8bae1dSRodney W. Grimes  * Copyright (c) 1993 Jan-Simon Pendry
3df8bae1dSRodney W. Grimes  * Copyright (c) 1993
4df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
5df8bae1dSRodney W. Grimes  *
6df8bae1dSRodney W. Grimes  * This code is derived from software contributed to Berkeley by
7df8bae1dSRodney W. Grimes  * Jan-Simon Pendry.
8df8bae1dSRodney W. Grimes  *
9df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
10df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
11df8bae1dSRodney W. Grimes  * are met:
12df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
13df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
14df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
15df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
16df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
17df8bae1dSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
18df8bae1dSRodney W. Grimes  *    must display the following acknowledgement:
19df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
20df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
21df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
22df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
23df8bae1dSRodney W. Grimes  *    without specific prior written permission.
24df8bae1dSRodney W. Grimes  *
25df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
36df8bae1dSRodney W. Grimes  *
37996c772fSJohn Dyson  *	@(#)procfs_status.c	8.4 (Berkeley) 6/15/94
38df8bae1dSRodney W. Grimes  *
39996c772fSJohn Dyson  * From:
40c3aac50fSPeter Wemm  * $FreeBSD$
41df8bae1dSRodney W. Grimes  */
42df8bae1dSRodney W. Grimes 
43df8bae1dSRodney W. Grimes #include <sys/param.h>
44df8bae1dSRodney W. Grimes #include <sys/systm.h>
45fb919e4dSMark Murray #include <sys/exec.h>
4675c13541SPoul-Henning Kamp #include <sys/jail.h>
47fb919e4dSMark Murray #include <sys/lock.h>
48fb919e4dSMark Murray #include <sys/malloc.h>
49fb919e4dSMark Murray #include <sys/mutex.h>
50fb919e4dSMark Murray #include <sys/proc.h>
51df8bae1dSRodney W. Grimes #include <sys/resourcevar.h>
52fb919e4dSMark Murray #include <sys/tty.h>
53fb919e4dSMark Murray #include <sys/vnode.h>
54df8bae1dSRodney W. Grimes 
5563a99273SMarcel Moolenaar #include <vm/vm.h>
5663a99273SMarcel Moolenaar #include <vm/pmap.h>
5763a99273SMarcel Moolenaar #include <vm/vm_param.h>
58fb919e4dSMark Murray 
59fb919e4dSMark Murray #include <miscfs/procfs/procfs.h>
6063a99273SMarcel Moolenaar 
61b8c8516aSEivind Eklund #define DOCHECK() do { if (ps >= psbuf+sizeof(psbuf)) goto bailout; } while (0)
62df8bae1dSRodney W. Grimes int
63df8bae1dSRodney W. Grimes procfs_dostatus(curp, p, pfs, uio)
64df8bae1dSRodney W. Grimes 	struct proc *curp;
65df8bae1dSRodney W. Grimes 	struct proc *p;
66df8bae1dSRodney W. Grimes 	struct pfsnode *pfs;
67df8bae1dSRodney W. Grimes 	struct uio *uio;
68df8bae1dSRodney W. Grimes {
69df8bae1dSRodney W. Grimes 	struct session *sess;
70df8bae1dSRodney W. Grimes 	struct tty *tp;
71df8bae1dSRodney W. Grimes 	struct ucred *cr;
72df8bae1dSRodney W. Grimes 	char *ps;
73df8bae1dSRodney W. Grimes 	char *sep;
74df8bae1dSRodney W. Grimes 	int pid, ppid, pgid, sid;
75df8bae1dSRodney W. Grimes 	int i;
76df8bae1dSRodney W. Grimes 	int xlen;
77df8bae1dSRodney W. Grimes 	int error;
78b8c8516aSEivind Eklund 	char psbuf[256];	/* XXX - conservative */
79df8bae1dSRodney W. Grimes 
80df8bae1dSRodney W. Grimes 	if (uio->uio_rw != UIO_READ)
81df8bae1dSRodney W. Grimes 		return (EOPNOTSUPP);
82df8bae1dSRodney W. Grimes 
83df8bae1dSRodney W. Grimes 	pid = p->p_pid;
8430ac5d0fSJohn Baldwin 	PROC_LOCK(p);
85e0f9d286SPeter Wemm 	ppid = p->p_pptr ? p->p_pptr->p_pid : 0;
8630ac5d0fSJohn Baldwin 	PROC_UNLOCK(p);
87df8bae1dSRodney W. Grimes 	pgid = p->p_pgrp->pg_id;
88df8bae1dSRodney W. Grimes 	sess = p->p_pgrp->pg_session;
89df8bae1dSRodney W. Grimes 	sid = sess->s_leader ? sess->s_leader->p_pid : 0;
90df8bae1dSRodney W. Grimes 
911d08058fSWolfram Schneider /* comm pid ppid pgid sid maj,min ctty,sldr start ut st wmsg
921d08058fSWolfram Schneider                                 euid ruid rgid,egid,groups[1 .. NGROUPS]
931d08058fSWolfram Schneider */
94b8c8516aSEivind Eklund 	KASSERT(sizeof(psbuf) > MAXCOMLEN,
95b8c8516aSEivind Eklund 			("Too short buffer for new MAXCOMLEN"));
96b8c8516aSEivind Eklund 
97df8bae1dSRodney W. Grimes 	ps = psbuf;
98df8bae1dSRodney W. Grimes 	bcopy(p->p_comm, ps, MAXCOMLEN);
99df8bae1dSRodney W. Grimes 	ps[MAXCOMLEN] = '\0';
100df8bae1dSRodney W. Grimes 	ps += strlen(ps);
101b8c8516aSEivind Eklund 	DOCHECK();
102b8c8516aSEivind Eklund 	ps += snprintf(ps, psbuf + sizeof(psbuf) - ps,
103b8c8516aSEivind Eklund 	    " %d %d %d %d ", pid, ppid, pgid, sid);
104b8c8516aSEivind Eklund 	DOCHECK();
105df8bae1dSRodney W. Grimes 	if ((p->p_flag&P_CONTROLT) && (tp = sess->s_ttyp))
106b8c8516aSEivind Eklund 		ps += snprintf(ps, psbuf + sizeof(psbuf) - ps,
107b8c8516aSEivind Eklund 		    "%d,%d ", major(tp->t_dev), minor(tp->t_dev));
108df8bae1dSRodney W. Grimes 	else
109b8c8516aSEivind Eklund 		ps += snprintf(ps, psbuf + sizeof(psbuf) - ps,
110b8c8516aSEivind Eklund 		    "%d,%d ", -1, -1);
111b8c8516aSEivind Eklund 	DOCHECK();
112df8bae1dSRodney W. Grimes 
113df8bae1dSRodney W. Grimes 	sep = "";
114df8bae1dSRodney W. Grimes 	if (sess->s_ttyvp) {
115b8c8516aSEivind Eklund 		ps += snprintf(ps, psbuf + sizeof(psbuf) - ps, "%sctty", sep);
116df8bae1dSRodney W. Grimes 		sep = ",";
117b8c8516aSEivind Eklund 		DOCHECK();
118df8bae1dSRodney W. Grimes 	}
119df8bae1dSRodney W. Grimes 	if (SESS_LEADER(p)) {
120b8c8516aSEivind Eklund 		ps += snprintf(ps, psbuf + sizeof(psbuf) - ps, "%ssldr", sep);
121df8bae1dSRodney W. Grimes 		sep = ",";
122b8c8516aSEivind Eklund 		DOCHECK();
123df8bae1dSRodney W. Grimes 	}
124b8c8516aSEivind Eklund 	if (*sep != ',') {
125b8c8516aSEivind Eklund 		ps += snprintf(ps, psbuf + sizeof(psbuf) - ps, "noflags");
126b8c8516aSEivind Eklund 		DOCHECK();
127b8c8516aSEivind Eklund 	}
128df8bae1dSRodney W. Grimes 
1299ed346baSBosko Milekic 	mtx_lock_spin(&sched_lock);
130b9393356SJohn Baldwin 	if (p->p_sflag & PS_INMEM) {
131df8bae1dSRodney W. Grimes 		struct timeval ut, st;
132df8bae1dSRodney W. Grimes 
1339d3a4425SDmitrij Tejblum 		calcru(p, &ut, &st, (struct timeval *) NULL);
1349ed346baSBosko Milekic 		mtx_unlock_spin(&sched_lock);
135b8c8516aSEivind Eklund 		ps += snprintf(ps, psbuf + sizeof(psbuf) - ps,
136b8c8516aSEivind Eklund 		    " %ld,%ld %ld,%ld %ld,%ld",
1379d3a4425SDmitrij Tejblum 		    p->p_stats->p_start.tv_sec,
1389d3a4425SDmitrij Tejblum 		    p->p_stats->p_start.tv_usec,
1399d3a4425SDmitrij Tejblum 		    ut.tv_sec, ut.tv_usec,
1409d3a4425SDmitrij Tejblum 		    st.tv_sec, st.tv_usec);
141b9393356SJohn Baldwin 	} else {
1429ed346baSBosko Milekic 		mtx_unlock_spin(&sched_lock);
143b8c8516aSEivind Eklund 		ps += snprintf(ps, psbuf + sizeof(psbuf) - ps,
144b8c8516aSEivind Eklund 		    " -1,-1 -1,-1 -1,-1");
145b9393356SJohn Baldwin 	}
146b8c8516aSEivind Eklund 	DOCHECK();
147df8bae1dSRodney W. Grimes 
148b8c8516aSEivind Eklund 	ps += snprintf(ps, psbuf + sizeof(psbuf) - ps, " %s",
149df8bae1dSRodney W. Grimes 		(p->p_wchan && p->p_wmesg) ? p->p_wmesg : "nochan");
150b8c8516aSEivind Eklund 	DOCHECK();
151df8bae1dSRodney W. Grimes 
152df8bae1dSRodney W. Grimes 	cr = p->p_ucred;
153df8bae1dSRodney W. Grimes 
154b8c8516aSEivind Eklund 	ps += snprintf(ps, psbuf + sizeof(psbuf) - ps, " %lu %lu %lu",
155ac1e407bSBruce Evans 		(u_long)cr->cr_uid,
156ac1e407bSBruce Evans 		(u_long)p->p_cred->p_ruid,
157ac1e407bSBruce Evans 		(u_long)p->p_cred->p_rgid);
158b8c8516aSEivind Eklund 	DOCHECK();
1591d08058fSWolfram Schneider 
1601d08058fSWolfram Schneider 	/* egid (p->p_cred->p_svgid) is equal to cr_ngroups[0]
1611d08058fSWolfram Schneider 	   see also getegid(2) in /sys/kern/kern_prot.c */
1621d08058fSWolfram Schneider 
163b8c8516aSEivind Eklund 	for (i = 0; i < cr->cr_ngroups; i++) {
164b8c8516aSEivind Eklund 		ps += snprintf(ps, psbuf + sizeof(psbuf) - ps,
165b8c8516aSEivind Eklund 		    ",%lu", (u_long)cr->cr_groups[i]);
166b8c8516aSEivind Eklund 		DOCHECK();
167b8c8516aSEivind Eklund 	}
16875c13541SPoul-Henning Kamp 
16991421ba2SRobert Watson 	if (jailed(p->p_ucred))
170b8c8516aSEivind Eklund 		ps += snprintf(ps, psbuf + sizeof(psbuf) - ps,
17191421ba2SRobert Watson 		    " %s", p->p_ucred->cr_prison->pr_host);
17275c13541SPoul-Henning Kamp 	else
173b8c8516aSEivind Eklund 		ps += snprintf(ps, psbuf + sizeof(psbuf) - ps, " -");
174b8c8516aSEivind Eklund 	DOCHECK();
175b8c8516aSEivind Eklund 	ps += snprintf(ps, psbuf + sizeof(psbuf) - ps, "\n");
176b8c8516aSEivind Eklund 	DOCHECK();
177df8bae1dSRodney W. Grimes 
178df8bae1dSRodney W. Grimes 	xlen = ps - psbuf;
179df8bae1dSRodney W. Grimes 	xlen -= uio->uio_offset;
180df8bae1dSRodney W. Grimes 	ps = psbuf + uio->uio_offset;
181996c772fSJohn Dyson 	xlen = imin(xlen, uio->uio_resid);
182df8bae1dSRodney W. Grimes 	if (xlen <= 0)
183df8bae1dSRodney W. Grimes 		error = 0;
184df8bae1dSRodney W. Grimes 	else
185df8bae1dSRodney W. Grimes 		error = uiomove(ps, xlen, uio);
186df8bae1dSRodney W. Grimes 
187df8bae1dSRodney W. Grimes 	return (error);
188b8c8516aSEivind Eklund 
189b8c8516aSEivind Eklund bailout:
190b8c8516aSEivind Eklund 	return (ENOMEM);
191df8bae1dSRodney W. Grimes }
19275ba7757SPeter Wemm 
19375ba7757SPeter Wemm int
19475ba7757SPeter Wemm procfs_docmdline(curp, p, pfs, uio)
19575ba7757SPeter Wemm 	struct proc *curp;
19675ba7757SPeter Wemm 	struct proc *p;
19775ba7757SPeter Wemm 	struct pfsnode *pfs;
19875ba7757SPeter Wemm 	struct uio *uio;
19975ba7757SPeter Wemm {
20075ba7757SPeter Wemm 	char *ps;
20175ba7757SPeter Wemm 	int xlen;
20275ba7757SPeter Wemm 	int error;
2036153cb20SPoul-Henning Kamp 	char *buf, *bp;
2046153cb20SPoul-Henning Kamp 	int buflen;
20563a99273SMarcel Moolenaar 	struct ps_strings pstr;
20663a99273SMarcel Moolenaar 	int i;
20763a99273SMarcel Moolenaar 	size_t bytes_left, done;
20863a99273SMarcel Moolenaar 
20975ba7757SPeter Wemm 	if (uio->uio_rw != UIO_READ)
21075ba7757SPeter Wemm 		return (EOPNOTSUPP);
21175ba7757SPeter Wemm 
21275ba7757SPeter Wemm 	/*
2136153cb20SPoul-Henning Kamp 	 * If we are using the ps/cmdline caching, use that.  Otherwise
2146153cb20SPoul-Henning Kamp 	 * revert back to the old way which only implements full cmdline
2156153cb20SPoul-Henning Kamp 	 * for the currept process and just p->p_comm for all other
2166153cb20SPoul-Henning Kamp 	 * processes.
21763a99273SMarcel Moolenaar 	 * Note that if the argv is no longer available, we deliberately
21863a99273SMarcel Moolenaar 	 * don't fall back on p->p_comm or return an error: the authentic
21963a99273SMarcel Moolenaar 	 * Linux behaviour is to return zero-length in this case.
22075ba7757SPeter Wemm 	 */
22163a99273SMarcel Moolenaar 
222387d2c03SRobert Watson 	if (p->p_args && (ps_argsopen || !p_can(curp, p, P_CAN_SEE, NULL))) {
2236153cb20SPoul-Henning Kamp 		bp = p->p_args->ar_args;
2246153cb20SPoul-Henning Kamp 		buflen = p->p_args->ar_length;
2256153cb20SPoul-Henning Kamp 		buf = 0;
2266153cb20SPoul-Henning Kamp 	} else if (p != curp) {
2276153cb20SPoul-Henning Kamp 		bp = p->p_comm;
2286153cb20SPoul-Henning Kamp 		buflen = MAXCOMLEN;
2296153cb20SPoul-Henning Kamp 		buf = 0;
2306153cb20SPoul-Henning Kamp 	} else {
2316153cb20SPoul-Henning Kamp 		buflen = 256;
2326153cb20SPoul-Henning Kamp 		MALLOC(buf, char *, buflen + 1, M_TEMP, M_WAITOK);
2336153cb20SPoul-Henning Kamp 		bp = buf;
2346153cb20SPoul-Henning Kamp 		ps = buf;
23563a99273SMarcel Moolenaar 		error = copyin((void*)PS_STRINGS, &pstr, sizeof(pstr));
2366153cb20SPoul-Henning Kamp 		if (error) {
2376153cb20SPoul-Henning Kamp 			FREE(buf, M_TEMP);
23863a99273SMarcel Moolenaar 			return (error);
2396153cb20SPoul-Henning Kamp 		}
2406153cb20SPoul-Henning Kamp 		bytes_left = buflen;
24163a99273SMarcel Moolenaar 		for (i = 0; bytes_left && (i < pstr.ps_nargvstr); i++) {
24263a99273SMarcel Moolenaar 			error = copyinstr(pstr.ps_argvstr[i], ps,
24363a99273SMarcel Moolenaar 					  bytes_left, &done);
24463a99273SMarcel Moolenaar 			/* If too long or malformed, just truncate */
24563a99273SMarcel Moolenaar 			if (error) {
24663a99273SMarcel Moolenaar 				error = 0;
24763a99273SMarcel Moolenaar 				break;
24863a99273SMarcel Moolenaar 			}
24963a99273SMarcel Moolenaar 			ps += done;
25063a99273SMarcel Moolenaar 			bytes_left -= done;
25163a99273SMarcel Moolenaar 		}
2526153cb20SPoul-Henning Kamp 		buflen = ps - buf;
25363a99273SMarcel Moolenaar 	}
25475ba7757SPeter Wemm 
2556153cb20SPoul-Henning Kamp 	buflen -= uio->uio_offset;
2566153cb20SPoul-Henning Kamp 	ps = bp + uio->uio_offset;
2576153cb20SPoul-Henning Kamp 	xlen = min(buflen, uio->uio_resid);
25875ba7757SPeter Wemm 	if (xlen <= 0)
25975ba7757SPeter Wemm 		error = 0;
26075ba7757SPeter Wemm 	else
26175ba7757SPeter Wemm 		error = uiomove(ps, xlen, uio);
2626153cb20SPoul-Henning Kamp 	if (buf)
2636153cb20SPoul-Henning Kamp 		FREE(buf, M_TEMP);
26475ba7757SPeter Wemm 	return (error);
26575ba7757SPeter Wemm }
266