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