xref: /dragonfly/sys/vfs/procfs/procfs_status.c (revision 9bb2a92d)
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.5 2003/10/02 19:21:06 drhodus 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/jail.h>
49 #include <sys/vnode.h>
50 #include <sys/tty.h>
51 #include <sys/resourcevar.h>
52 #include <vfs/procfs/procfs.h>
53 
54 #include <vm/vm.h>
55 #include <vm/pmap.h>
56 #include <vm/vm_param.h>
57 #include <sys/exec.h>
58 
59 #define DOCHECK() do { if (ps >= psbuf+sizeof(psbuf)) goto bailout; } while (0)
60 int
61 procfs_dostatus(curp, p, pfs, uio)
62 	struct proc *curp;
63 	struct proc *p;
64 	struct pfsnode *pfs;
65 	struct uio *uio;
66 {
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 	int i;
74 	int xlen;
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 += snprintf(ps, psbuf + sizeof(psbuf) - ps,
99 	    " %d %d %d %d ", pid, ppid, pgid, sid);
100 	DOCHECK();
101 	if ((p->p_flag&P_CONTROLT) && (tp = sess->s_ttyp))
102 		ps += snprintf(ps, psbuf + sizeof(psbuf) - ps,
103 		    "%d,%d ", major(tp->t_dev), minor(tp->t_dev));
104 	else
105 		ps += snprintf(ps, psbuf + sizeof(psbuf) - ps,
106 		    "%d,%d ", -1, -1);
107 	DOCHECK();
108 
109 	sep = "";
110 	if (sess->s_ttyvp) {
111 		ps += snprintf(ps, psbuf + sizeof(psbuf) - ps, "%sctty", sep);
112 		sep = ",";
113 		DOCHECK();
114 	}
115 	if (SESS_LEADER(p)) {
116 		ps += snprintf(ps, psbuf + sizeof(psbuf) - ps, "%ssldr", sep);
117 		sep = ",";
118 		DOCHECK();
119 	}
120 	if (*sep != ',') {
121 		ps += snprintf(ps, psbuf + sizeof(psbuf) - ps, "noflags");
122 		DOCHECK();
123 	}
124 
125 	if (p->p_flag & P_INMEM) {
126 		struct timeval ut, st;
127 
128 		calcru(p, &ut, &st, (struct timeval *) NULL);
129 		ps += snprintf(ps, psbuf + sizeof(psbuf) - ps,
130 		    " %ld,%ld %ld,%ld %ld,%ld",
131 		    p->p_stats->p_start.tv_sec,
132 		    p->p_stats->p_start.tv_usec,
133 		    ut.tv_sec, ut.tv_usec,
134 		    st.tv_sec, st.tv_usec);
135 	} else
136 		ps += snprintf(ps, psbuf + sizeof(psbuf) - ps,
137 		    " -1,-1 -1,-1 -1,-1");
138 	DOCHECK();
139 
140 	ps += snprintf(ps, psbuf + sizeof(psbuf) - ps, " %s",
141 		(p->p_wchan && p->p_wmesg) ? p->p_wmesg : "nochan");
142 	DOCHECK();
143 
144 	cr = p->p_ucred;
145 
146 	ps += snprintf(ps, psbuf + sizeof(psbuf) - ps, " %lu %lu %lu",
147 		(u_long)cr->cr_uid,
148 		(u_long)p->p_ucred->cr_ruid,
149 		(u_long)p->p_ucred->cr_rgid);
150 	DOCHECK();
151 
152 	/* egid (p->p_ucred->cr_svgid) is equal to cr_ngroups[0]
153 	   see also getegid(2) in /sys/kern/kern_prot.c */
154 
155 	for (i = 0; i < cr->cr_ngroups; i++) {
156 		ps += snprintf(ps, psbuf + sizeof(psbuf) - ps,
157 		    ",%lu", (u_long)cr->cr_groups[i]);
158 		DOCHECK();
159 	}
160 
161 	if (p->p_ucred->cr_prison)
162 		ps += snprintf(ps, psbuf + sizeof(psbuf) - ps,
163 		    " %s", p->p_ucred->cr_prison->pr_host);
164 	else
165 		ps += snprintf(ps, psbuf + sizeof(psbuf) - ps, " -");
166 	DOCHECK();
167 	ps += snprintf(ps, psbuf + sizeof(psbuf) - ps, "\n");
168 	DOCHECK();
169 
170 	xlen = ps - psbuf;
171 	xlen -= uio->uio_offset;
172 	ps = psbuf + uio->uio_offset;
173 	xlen = imin(xlen, uio->uio_resid);
174 	if (xlen <= 0)
175 		error = 0;
176 	else
177 		error = uiomove_frombuf(psbuf, ps - psbuf, uio);
178 
179 	return (error);
180 
181 bailout:
182 	return (ENOMEM);
183 }
184 
185 int
186 procfs_docmdline(curp, p, pfs, uio)
187 	struct proc *curp;
188 	struct proc *p;
189 	struct pfsnode *pfs;
190 	struct uio *uio;
191 {
192 	char *ps;
193 	int xlen;
194 	int error;
195 	char *buf, *bp;
196 	int buflen;
197 	struct ps_strings pstr;
198 	int i;
199 	size_t bytes_left, done;
200 
201 	if (uio->uio_rw != UIO_READ)
202 		return (EOPNOTSUPP);
203 
204 	/*
205 	 * If we are using the ps/cmdline caching, use that.  Otherwise
206 	 * revert back to the old way which only implements full cmdline
207 	 * for the currept process and just p->p_comm for all other
208 	 * processes.
209 	 * Note that if the argv is no longer available, we deliberately
210 	 * don't fall back on p->p_comm or return an error: the authentic
211 	 * Linux behaviour is to return zero-length in this case.
212 	 */
213 
214 	if (p->p_args &&
215 	    (ps_argsopen || (CHECKIO(curp, p) &&
216 	     (p->p_flag & P_INEXEC) == 0 && !p_trespass(curp->p_ucred, p->p_ucred)))
217 	 ) {
218 		bp = p->p_args->ar_args;
219 		buflen = p->p_args->ar_length;
220 		buf = 0;
221 	} else if (p != curp) {
222 		bp = p->p_comm;
223 		buflen = MAXCOMLEN;
224 		buf = 0;
225 	} else {
226 		buflen = 256;
227 		MALLOC(buf, char *, buflen + 1, M_TEMP, M_WAITOK);
228 		bp = buf;
229 		ps = buf;
230 		error = copyin((void*)PS_STRINGS, &pstr, sizeof(pstr));
231 		if (error) {
232 			FREE(buf, M_TEMP);
233 			return (error);
234 		}
235 		bytes_left = buflen;
236 		for (i = 0; bytes_left && (i < pstr.ps_nargvstr); i++) {
237 			error = copyinstr(pstr.ps_argvstr[i], ps,
238 					  bytes_left, &done);
239 			/* If too long or malformed, just truncate */
240 			if (error) {
241 				error = 0;
242 				break;
243 			}
244 			ps += done;
245 			bytes_left -= done;
246 		}
247 		buflen = ps - buf;
248 	}
249 
250 	buflen -= uio->uio_offset;
251 	ps = bp + uio->uio_offset;
252 	xlen = min(buflen, uio->uio_resid);
253 	if (xlen <= 0)
254 		error = 0;
255 	else
256 		error = uiomove_frombuf(bp, buflen, uio);
257 	if (buf)
258 		FREE(buf, M_TEMP);
259 	return (error);
260 }
261