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