xref: /freebsd/sys/fs/procfs/procfs_status.c (revision e0f9d2869a163a8cdc8c70df5e60781c7604b7d8)
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>
456153cb20SPoul-Henning Kamp #include <sys/malloc.h>
46df8bae1dSRodney W. Grimes #include <sys/proc.h>
4775c13541SPoul-Henning Kamp #include <sys/jail.h>
48df8bae1dSRodney W. Grimes #include <sys/vnode.h>
49df8bae1dSRodney W. Grimes #include <sys/tty.h>
50df8bae1dSRodney W. Grimes #include <sys/resourcevar.h>
51df8bae1dSRodney W. Grimes #include <miscfs/procfs/procfs.h>
52df8bae1dSRodney W. Grimes 
5363a99273SMarcel Moolenaar #include <vm/vm.h>
5463a99273SMarcel Moolenaar #include <vm/pmap.h>
5563a99273SMarcel Moolenaar #include <vm/vm_param.h>
5663a99273SMarcel Moolenaar #include <sys/exec.h>
5763a99273SMarcel Moolenaar 
58df8bae1dSRodney W. Grimes int
59df8bae1dSRodney W. Grimes procfs_dostatus(curp, p, pfs, uio)
60df8bae1dSRodney W. Grimes 	struct proc *curp;
61df8bae1dSRodney W. Grimes 	struct proc *p;
62df8bae1dSRodney W. Grimes 	struct pfsnode *pfs;
63df8bae1dSRodney W. Grimes 	struct uio *uio;
64df8bae1dSRodney W. Grimes {
65df8bae1dSRodney W. Grimes 	struct session *sess;
66df8bae1dSRodney W. Grimes 	struct tty *tp;
67df8bae1dSRodney W. Grimes 	struct ucred *cr;
68df8bae1dSRodney W. Grimes 	char *ps;
69df8bae1dSRodney W. Grimes 	char *sep;
70df8bae1dSRodney W. Grimes 	int pid, ppid, pgid, sid;
71df8bae1dSRodney W. Grimes 	int i;
72df8bae1dSRodney W. Grimes 	int xlen;
73df8bae1dSRodney W. Grimes 	int error;
74df8bae1dSRodney W. Grimes 	char psbuf[256];		/* XXX - conservative */
75df8bae1dSRodney W. Grimes 
76df8bae1dSRodney W. Grimes 	if (uio->uio_rw != UIO_READ)
77df8bae1dSRodney W. Grimes 		return (EOPNOTSUPP);
78df8bae1dSRodney W. Grimes 
79df8bae1dSRodney W. Grimes 	pid = p->p_pid;
80e0f9d286SPeter Wemm 	ppid = p->p_pptr ? p->p_pptr->p_pid : 0;
81df8bae1dSRodney W. Grimes 	pgid = p->p_pgrp->pg_id;
82df8bae1dSRodney W. Grimes 	sess = p->p_pgrp->pg_session;
83df8bae1dSRodney W. Grimes 	sid = sess->s_leader ? sess->s_leader->p_pid : 0;
84df8bae1dSRodney W. Grimes 
851d08058fSWolfram Schneider /* comm pid ppid pgid sid maj,min ctty,sldr start ut st wmsg
861d08058fSWolfram Schneider                                 euid ruid rgid,egid,groups[1 .. NGROUPS]
871d08058fSWolfram Schneider */
88df8bae1dSRodney W. Grimes 	ps = psbuf;
89df8bae1dSRodney W. Grimes 	bcopy(p->p_comm, ps, MAXCOMLEN);
90df8bae1dSRodney W. Grimes 	ps[MAXCOMLEN] = '\0';
91df8bae1dSRodney W. Grimes 	ps += strlen(ps);
92df8bae1dSRodney W. Grimes 	ps += sprintf(ps, " %d %d %d %d ", pid, ppid, pgid, sid);
93df8bae1dSRodney W. Grimes 
94df8bae1dSRodney W. Grimes 	if ((p->p_flag&P_CONTROLT) && (tp = sess->s_ttyp))
95df8bae1dSRodney W. Grimes 		ps += sprintf(ps, "%d,%d ", major(tp->t_dev), minor(tp->t_dev));
96df8bae1dSRodney W. Grimes 	else
97df8bae1dSRodney W. Grimes 		ps += sprintf(ps, "%d,%d ", -1, -1);
98df8bae1dSRodney W. Grimes 
99df8bae1dSRodney W. Grimes 	sep = "";
100df8bae1dSRodney W. Grimes 	if (sess->s_ttyvp) {
101df8bae1dSRodney W. Grimes 		ps += sprintf(ps, "%sctty", sep);
102df8bae1dSRodney W. Grimes 		sep = ",";
103df8bae1dSRodney W. Grimes 	}
104df8bae1dSRodney W. Grimes 	if (SESS_LEADER(p)) {
105df8bae1dSRodney W. Grimes 		ps += sprintf(ps, "%ssldr", sep);
106df8bae1dSRodney W. Grimes 		sep = ",";
107df8bae1dSRodney W. Grimes 	}
108df8bae1dSRodney W. Grimes 	if (*sep != ',')
109df8bae1dSRodney W. Grimes 		ps += sprintf(ps, "noflags");
110df8bae1dSRodney W. Grimes 
1119d3a4425SDmitrij Tejblum 	if (p->p_flag & P_INMEM) {
112df8bae1dSRodney W. Grimes 		struct timeval ut, st;
113df8bae1dSRodney W. Grimes 
1149d3a4425SDmitrij Tejblum 		calcru(p, &ut, &st, (struct timeval *) NULL);
1159d3a4425SDmitrij Tejblum 		ps += sprintf(ps, " %ld,%ld %ld,%ld %ld,%ld",
1169d3a4425SDmitrij Tejblum 		    p->p_stats->p_start.tv_sec,
1179d3a4425SDmitrij Tejblum 		    p->p_stats->p_start.tv_usec,
1189d3a4425SDmitrij Tejblum 		    ut.tv_sec, ut.tv_usec,
1199d3a4425SDmitrij Tejblum 		    st.tv_sec, st.tv_usec);
1209d3a4425SDmitrij Tejblum 	} else
1219d3a4425SDmitrij Tejblum 		ps += sprintf(ps, " -1,-1 -1,-1 -1,-1");
122df8bae1dSRodney W. Grimes 
123df8bae1dSRodney W. Grimes 	ps += sprintf(ps, " %s",
124df8bae1dSRodney W. Grimes 		(p->p_wchan && p->p_wmesg) ? p->p_wmesg : "nochan");
125df8bae1dSRodney W. Grimes 
126df8bae1dSRodney W. Grimes 	cr = p->p_ucred;
127df8bae1dSRodney W. Grimes 
128ac1e407bSBruce Evans 	ps += sprintf(ps, " %lu %lu %lu",
129ac1e407bSBruce Evans 		(u_long)cr->cr_uid,
130ac1e407bSBruce Evans 		(u_long)p->p_cred->p_ruid,
131ac1e407bSBruce Evans 		(u_long)p->p_cred->p_rgid);
1321d08058fSWolfram Schneider 
1331d08058fSWolfram Schneider 	/* egid (p->p_cred->p_svgid) is equal to cr_ngroups[0]
1341d08058fSWolfram Schneider 	   see also getegid(2) in /sys/kern/kern_prot.c */
1351d08058fSWolfram Schneider 
136df8bae1dSRodney W. Grimes 	for (i = 0; i < cr->cr_ngroups; i++)
137ac1e407bSBruce Evans 		ps += sprintf(ps, ",%lu", (u_long)cr->cr_groups[i]);
13875c13541SPoul-Henning Kamp 
13975c13541SPoul-Henning Kamp 	if (p->p_prison)
14075c13541SPoul-Henning Kamp 		ps += sprintf(ps, " %s", p->p_prison->pr_host);
14175c13541SPoul-Henning Kamp 	else
14275c13541SPoul-Henning Kamp 		ps += sprintf(ps, " -");
143df8bae1dSRodney W. Grimes 	ps += sprintf(ps, "\n");
144df8bae1dSRodney W. Grimes 
145df8bae1dSRodney W. Grimes 	xlen = ps - psbuf;
146df8bae1dSRodney W. Grimes 	xlen -= uio->uio_offset;
147df8bae1dSRodney W. Grimes 	ps = psbuf + uio->uio_offset;
148996c772fSJohn Dyson 	xlen = imin(xlen, uio->uio_resid);
149df8bae1dSRodney W. Grimes 	if (xlen <= 0)
150df8bae1dSRodney W. Grimes 		error = 0;
151df8bae1dSRodney W. Grimes 	else
152df8bae1dSRodney W. Grimes 		error = uiomove(ps, xlen, uio);
153df8bae1dSRodney W. Grimes 
154df8bae1dSRodney W. Grimes 	return (error);
155df8bae1dSRodney W. Grimes }
15675ba7757SPeter Wemm 
15775ba7757SPeter Wemm int
15875ba7757SPeter Wemm procfs_docmdline(curp, p, pfs, uio)
15975ba7757SPeter Wemm 	struct proc *curp;
16075ba7757SPeter Wemm 	struct proc *p;
16175ba7757SPeter Wemm 	struct pfsnode *pfs;
16275ba7757SPeter Wemm 	struct uio *uio;
16375ba7757SPeter Wemm {
16475ba7757SPeter Wemm 	char *ps;
16575ba7757SPeter Wemm 	int xlen;
16675ba7757SPeter Wemm 	int error;
1676153cb20SPoul-Henning Kamp 	char *buf, *bp;
1686153cb20SPoul-Henning Kamp 	int buflen;
16963a99273SMarcel Moolenaar 	struct ps_strings pstr;
17063a99273SMarcel Moolenaar 	int i;
17163a99273SMarcel Moolenaar 	size_t bytes_left, done;
17263a99273SMarcel Moolenaar 
17375ba7757SPeter Wemm 	if (uio->uio_rw != UIO_READ)
17475ba7757SPeter Wemm 		return (EOPNOTSUPP);
17575ba7757SPeter Wemm 
17675ba7757SPeter Wemm 	/*
1776153cb20SPoul-Henning Kamp 	 * If we are using the ps/cmdline caching, use that.  Otherwise
1786153cb20SPoul-Henning Kamp 	 * revert back to the old way which only implements full cmdline
1796153cb20SPoul-Henning Kamp 	 * for the currept process and just p->p_comm for all other
1806153cb20SPoul-Henning Kamp 	 * processes.
18163a99273SMarcel Moolenaar 	 * Note that if the argv is no longer available, we deliberately
18263a99273SMarcel Moolenaar 	 * don't fall back on p->p_comm or return an error: the authentic
18363a99273SMarcel Moolenaar 	 * Linux behaviour is to return zero-length in this case.
18475ba7757SPeter Wemm 	 */
18563a99273SMarcel Moolenaar 
186a8704f89SPoul-Henning Kamp 	if (p->p_args && (ps_argsopen ||!p_trespass(curp, p))) {
1876153cb20SPoul-Henning Kamp 		bp = p->p_args->ar_args;
1886153cb20SPoul-Henning Kamp 		buflen = p->p_args->ar_length;
1896153cb20SPoul-Henning Kamp 		buf = 0;
1906153cb20SPoul-Henning Kamp 	} else if (p != curp) {
1916153cb20SPoul-Henning Kamp 		bp = p->p_comm;
1926153cb20SPoul-Henning Kamp 		buflen = MAXCOMLEN;
1936153cb20SPoul-Henning Kamp 		buf = 0;
1946153cb20SPoul-Henning Kamp 	} else {
1956153cb20SPoul-Henning Kamp 		buflen = 256;
1966153cb20SPoul-Henning Kamp 		MALLOC(buf, char *, buflen + 1, M_TEMP, M_WAITOK);
1976153cb20SPoul-Henning Kamp 		bp = buf;
1986153cb20SPoul-Henning Kamp 		ps = buf;
19963a99273SMarcel Moolenaar 		error = copyin((void*)PS_STRINGS, &pstr, sizeof(pstr));
2006153cb20SPoul-Henning Kamp 		if (error) {
2016153cb20SPoul-Henning Kamp 			FREE(buf, M_TEMP);
20263a99273SMarcel Moolenaar 			return (error);
2036153cb20SPoul-Henning Kamp 		}
2046153cb20SPoul-Henning Kamp 		bytes_left = buflen;
20563a99273SMarcel Moolenaar 		for (i = 0; bytes_left && (i < pstr.ps_nargvstr); i++) {
20663a99273SMarcel Moolenaar 			error = copyinstr(pstr.ps_argvstr[i], ps,
20763a99273SMarcel Moolenaar 					  bytes_left, &done);
20863a99273SMarcel Moolenaar 			/* If too long or malformed, just truncate */
20963a99273SMarcel Moolenaar 			if (error) {
21063a99273SMarcel Moolenaar 				error = 0;
21163a99273SMarcel Moolenaar 				break;
21263a99273SMarcel Moolenaar 			}
21363a99273SMarcel Moolenaar 			ps += done;
21463a99273SMarcel Moolenaar 			bytes_left -= done;
21563a99273SMarcel Moolenaar 		}
2166153cb20SPoul-Henning Kamp 		buflen = ps - buf;
21763a99273SMarcel Moolenaar 	}
21875ba7757SPeter Wemm 
2196153cb20SPoul-Henning Kamp 	buflen -= uio->uio_offset;
2206153cb20SPoul-Henning Kamp 	ps = bp + uio->uio_offset;
2216153cb20SPoul-Henning Kamp 	xlen = min(buflen, uio->uio_resid);
22275ba7757SPeter Wemm 	if (xlen <= 0)
22375ba7757SPeter Wemm 		error = 0;
22475ba7757SPeter Wemm 	else
22575ba7757SPeter Wemm 		error = uiomove(ps, xlen, uio);
2266153cb20SPoul-Henning Kamp 	if (buf)
2276153cb20SPoul-Henning Kamp 		FREE(buf, M_TEMP);
22875ba7757SPeter Wemm 	return (error);
22975ba7757SPeter Wemm }
230