xref: /freebsd/sys/fs/procfs/procfs_status.c (revision 1a2cdef4962b47be5057809ce730a733b7f3c27c)
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$
41  */
42 
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/malloc.h>
46 #include <sys/proc.h>
47 #include <sys/jail.h>
48 #include <sys/vnode.h>
49 #include <sys/tty.h>
50 #include <sys/resourcevar.h>
51 #include <miscfs/procfs/procfs.h>
52 
53 #include <vm/vm.h>
54 #include <vm/pmap.h>
55 #include <vm/vm_param.h>
56 #include <sys/exec.h>
57 
58 #define DOCHECK() do { if (ps >= psbuf+sizeof(psbuf)) goto bailout; } while (0)
59 int
60 procfs_dostatus(curp, p, pfs, uio)
61 	struct proc *curp;
62 	struct proc *p;
63 	struct pfsnode *pfs;
64 	struct uio *uio;
65 {
66 	struct session *sess;
67 	struct tty *tp;
68 	struct ucred *cr;
69 	char *ps;
70 	char *sep;
71 	int pid, ppid, pgid, sid;
72 	int i;
73 	int xlen;
74 	int error;
75 	char psbuf[256];	/* XXX - conservative */
76 
77 	if (uio->uio_rw != UIO_READ)
78 		return (EOPNOTSUPP);
79 
80 	pid = p->p_pid;
81 	PROC_LOCK(p);
82 	ppid = p->p_pptr ? p->p_pptr->p_pid : 0;
83 	PROC_UNLOCK(p);
84 	pgid = p->p_pgrp->pg_id;
85 	sess = p->p_pgrp->pg_session;
86 	sid = sess->s_leader ? sess->s_leader->p_pid : 0;
87 
88 /* comm pid ppid pgid sid maj,min ctty,sldr start ut st wmsg
89                                 euid ruid rgid,egid,groups[1 .. NGROUPS]
90 */
91 	KASSERT(sizeof(psbuf) > MAXCOMLEN,
92 			("Too short buffer for new MAXCOMLEN"));
93 
94 	ps = psbuf;
95 	bcopy(p->p_comm, ps, MAXCOMLEN);
96 	ps[MAXCOMLEN] = '\0';
97 	ps += strlen(ps);
98 	DOCHECK();
99 	ps += snprintf(ps, psbuf + sizeof(psbuf) - ps,
100 	    " %d %d %d %d ", pid, ppid, pgid, sid);
101 	DOCHECK();
102 	if ((p->p_flag&P_CONTROLT) && (tp = sess->s_ttyp))
103 		ps += snprintf(ps, psbuf + sizeof(psbuf) - ps,
104 		    "%d,%d ", major(tp->t_dev), minor(tp->t_dev));
105 	else
106 		ps += snprintf(ps, psbuf + sizeof(psbuf) - ps,
107 		    "%d,%d ", -1, -1);
108 	DOCHECK();
109 
110 	sep = "";
111 	if (sess->s_ttyvp) {
112 		ps += snprintf(ps, psbuf + sizeof(psbuf) - ps, "%sctty", sep);
113 		sep = ",";
114 		DOCHECK();
115 	}
116 	if (SESS_LEADER(p)) {
117 		ps += snprintf(ps, psbuf + sizeof(psbuf) - ps, "%ssldr", sep);
118 		sep = ",";
119 		DOCHECK();
120 	}
121 	if (*sep != ',') {
122 		ps += snprintf(ps, psbuf + sizeof(psbuf) - ps, "noflags");
123 		DOCHECK();
124 	}
125 
126 	mtx_lock_spin(&sched_lock);
127 	if (p->p_sflag & PS_INMEM) {
128 		struct timeval ut, st;
129 
130 		calcru(p, &ut, &st, (struct timeval *) NULL);
131 		mtx_unlock_spin(&sched_lock);
132 		ps += snprintf(ps, psbuf + sizeof(psbuf) - ps,
133 		    " %ld,%ld %ld,%ld %ld,%ld",
134 		    p->p_stats->p_start.tv_sec,
135 		    p->p_stats->p_start.tv_usec,
136 		    ut.tv_sec, ut.tv_usec,
137 		    st.tv_sec, st.tv_usec);
138 	} else {
139 		mtx_unlock_spin(&sched_lock);
140 		ps += snprintf(ps, psbuf + sizeof(psbuf) - ps,
141 		    " -1,-1 -1,-1 -1,-1");
142 	}
143 	DOCHECK();
144 
145 	ps += snprintf(ps, psbuf + sizeof(psbuf) - ps, " %s",
146 		(p->p_wchan && p->p_wmesg) ? p->p_wmesg : "nochan");
147 	DOCHECK();
148 
149 	cr = p->p_ucred;
150 
151 	ps += snprintf(ps, psbuf + sizeof(psbuf) - ps, " %lu %lu %lu",
152 		(u_long)cr->cr_uid,
153 		(u_long)p->p_cred->p_ruid,
154 		(u_long)p->p_cred->p_rgid);
155 	DOCHECK();
156 
157 	/* egid (p->p_cred->p_svgid) is equal to cr_ngroups[0]
158 	   see also getegid(2) in /sys/kern/kern_prot.c */
159 
160 	for (i = 0; i < cr->cr_ngroups; i++) {
161 		ps += snprintf(ps, psbuf + sizeof(psbuf) - ps,
162 		    ",%lu", (u_long)cr->cr_groups[i]);
163 		DOCHECK();
164 	}
165 
166 	if (jailed(p->p_ucred))
167 		ps += snprintf(ps, psbuf + sizeof(psbuf) - ps,
168 		    " %s", p->p_ucred->cr_prison->pr_host);
169 	else
170 		ps += snprintf(ps, psbuf + sizeof(psbuf) - ps, " -");
171 	DOCHECK();
172 	ps += snprintf(ps, psbuf + sizeof(psbuf) - ps, "\n");
173 	DOCHECK();
174 
175 	xlen = ps - psbuf;
176 	xlen -= uio->uio_offset;
177 	ps = psbuf + uio->uio_offset;
178 	xlen = imin(xlen, uio->uio_resid);
179 	if (xlen <= 0)
180 		error = 0;
181 	else
182 		error = uiomove(ps, xlen, uio);
183 
184 	return (error);
185 
186 bailout:
187 	return (ENOMEM);
188 }
189 
190 int
191 procfs_docmdline(curp, p, pfs, uio)
192 	struct proc *curp;
193 	struct proc *p;
194 	struct pfsnode *pfs;
195 	struct uio *uio;
196 {
197 	char *ps;
198 	int xlen;
199 	int error;
200 	char *buf, *bp;
201 	int buflen;
202 	struct ps_strings pstr;
203 	int i;
204 	size_t bytes_left, done;
205 
206 	if (uio->uio_rw != UIO_READ)
207 		return (EOPNOTSUPP);
208 
209 	/*
210 	 * If we are using the ps/cmdline caching, use that.  Otherwise
211 	 * revert back to the old way which only implements full cmdline
212 	 * for the currept process and just p->p_comm for all other
213 	 * processes.
214 	 * Note that if the argv is no longer available, we deliberately
215 	 * don't fall back on p->p_comm or return an error: the authentic
216 	 * Linux behaviour is to return zero-length in this case.
217 	 */
218 
219 	if (p->p_args && (ps_argsopen || !p_can(curp, p, P_CAN_SEE, NULL))) {
220 		bp = p->p_args->ar_args;
221 		buflen = p->p_args->ar_length;
222 		buf = 0;
223 	} else if (p != curp) {
224 		bp = p->p_comm;
225 		buflen = MAXCOMLEN;
226 		buf = 0;
227 	} else {
228 		buflen = 256;
229 		MALLOC(buf, char *, buflen + 1, M_TEMP, M_WAITOK);
230 		bp = buf;
231 		ps = buf;
232 		error = copyin((void*)PS_STRINGS, &pstr, sizeof(pstr));
233 		if (error) {
234 			FREE(buf, M_TEMP);
235 			return (error);
236 		}
237 		bytes_left = buflen;
238 		for (i = 0; bytes_left && (i < pstr.ps_nargvstr); i++) {
239 			error = copyinstr(pstr.ps_argvstr[i], ps,
240 					  bytes_left, &done);
241 			/* If too long or malformed, just truncate */
242 			if (error) {
243 				error = 0;
244 				break;
245 			}
246 			ps += done;
247 			bytes_left -= done;
248 		}
249 		buflen = ps - buf;
250 	}
251 
252 	buflen -= uio->uio_offset;
253 	ps = bp + uio->uio_offset;
254 	xlen = min(buflen, uio->uio_resid);
255 	if (xlen <= 0)
256 		error = 0;
257 	else
258 		error = uiomove(ps, xlen, uio);
259 	if (buf)
260 		FREE(buf, M_TEMP);
261 	return (error);
262 }
263