xref: /illumos-gate/usr/src/uts/common/fs/proc/prvnops.c (revision 086d9687)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Copyright (c) 2018, Joyent, Inc.
25  * Copyright (c) 2017 by Delphix. All rights reserved.
26  * Copyright 2020 OmniOS Community Edition (OmniOSce) Association.
27  */
28 
29 /*	Copyright (c) 1984,	 1986, 1987, 1988, 1989 AT&T	*/
30 /*	  All Rights Reserved	*/
31 
32 #include <sys/types.h>
33 #include <sys/param.h>
34 #include <sys/time.h>
35 #include <sys/cred.h>
36 #include <sys/policy.h>
37 #include <sys/debug.h>
38 #include <sys/dirent.h>
39 #include <sys/errno.h>
40 #include <sys/file.h>
41 #include <sys/inline.h>
42 #include <sys/kmem.h>
43 #include <sys/pathname.h>
44 #include <sys/proc.h>
45 #include <sys/brand.h>
46 #include <sys/signal.h>
47 #include <sys/stat.h>
48 #include <sys/sysmacros.h>
49 #include <sys/systm.h>
50 #include <sys/zone.h>
51 #include <sys/uio.h>
52 #include <sys/var.h>
53 #include <sys/mode.h>
54 #include <sys/poll.h>
55 #include <sys/user.h>
56 #include <sys/vfs.h>
57 #include <sys/vfs_opreg.h>
58 #include <sys/gfs.h>
59 #include <sys/vnode.h>
60 #include <sys/fault.h>
61 #include <sys/syscall.h>
62 #include <sys/procfs.h>
63 #include <sys/atomic.h>
64 #include <sys/cmn_err.h>
65 #include <sys/contract_impl.h>
66 #include <sys/ctfs.h>
67 #include <sys/avl.h>
68 #include <sys/ctype.h>
69 #include <fs/fs_subr.h>
70 #include <vm/rm.h>
71 #include <vm/as.h>
72 #include <vm/seg.h>
73 #include <vm/seg_vn.h>
74 #include <vm/hat.h>
75 #include <fs/proc/prdata.h>
76 #if defined(__sparc)
77 #include <sys/regset.h>
78 #endif
79 #if defined(__x86)
80 #include <sys/sysi86.h>
81 #endif
82 
83 /*
84  * Created by prinit.
85  */
86 vnodeops_t *prvnodeops;
87 
88 /*
89  * Directory characteristics (patterned after the s5 file system).
90  */
91 #define	PRROOTINO	2
92 
93 #define	PRDIRSIZE	14
94 struct prdirect {
95 	ushort_t	d_ino;
96 	char		d_name[PRDIRSIZE];
97 };
98 
99 #define	PRSDSIZE	(sizeof (struct prdirect))
100 
101 /*
102  * Directory characteristics.
103  */
104 typedef struct prdirent {
105 	ino64_t		d_ino;		/* "inode number" of entry */
106 	off64_t		d_off;		/* offset of disk directory entry */
107 	unsigned short	d_reclen;	/* length of this record */
108 	char		d_name[14];	/* name of file */
109 } prdirent_t;
110 
111 /*
112  * Contents of a /proc/<pid> directory.
113  * Reuse d_ino field for the /proc file type.
114  */
115 static prdirent_t piddir[] = {
116 	{ PR_PIDDIR,	 1 * sizeof (prdirent_t), sizeof (prdirent_t),
117 		"." },
118 	{ PR_PROCDIR,	 2 * sizeof (prdirent_t), sizeof (prdirent_t),
119 		".." },
120 	{ PR_AS,	 3 * sizeof (prdirent_t), sizeof (prdirent_t),
121 		"as" },
122 	{ PR_CTL,	 4 * sizeof (prdirent_t), sizeof (prdirent_t),
123 		"ctl" },
124 	{ PR_STATUS,	 5 * sizeof (prdirent_t), sizeof (prdirent_t),
125 		"status" },
126 	{ PR_LSTATUS,	 6 * sizeof (prdirent_t), sizeof (prdirent_t),
127 		"lstatus" },
128 	{ PR_PSINFO,	 7 * sizeof (prdirent_t), sizeof (prdirent_t),
129 		"psinfo" },
130 	{ PR_LPSINFO,	 8 * sizeof (prdirent_t), sizeof (prdirent_t),
131 		"lpsinfo" },
132 	{ PR_MAP,	 9 * sizeof (prdirent_t), sizeof (prdirent_t),
133 		"map" },
134 	{ PR_RMAP,	10 * sizeof (prdirent_t), sizeof (prdirent_t),
135 		"rmap" },
136 	{ PR_XMAP,	11 * sizeof (prdirent_t), sizeof (prdirent_t),
137 		"xmap" },
138 	{ PR_CRED,	12 * sizeof (prdirent_t), sizeof (prdirent_t),
139 		"cred" },
140 	{ PR_SIGACT,	13 * sizeof (prdirent_t), sizeof (prdirent_t),
141 		"sigact" },
142 	{ PR_AUXV,	14 * sizeof (prdirent_t), sizeof (prdirent_t),
143 		"auxv" },
144 	{ PR_USAGE,	15 * sizeof (prdirent_t), sizeof (prdirent_t),
145 		"usage" },
146 	{ PR_LUSAGE,	16 * sizeof (prdirent_t), sizeof (prdirent_t),
147 		"lusage" },
148 	{ PR_PAGEDATA,	17 * sizeof (prdirent_t), sizeof (prdirent_t),
149 		"pagedata" },
150 	{ PR_WATCH,	18 * sizeof (prdirent_t), sizeof (prdirent_t),
151 		"watch" },
152 	{ PR_CURDIR,	19 * sizeof (prdirent_t), sizeof (prdirent_t),
153 		"cwd" },
154 	{ PR_ROOTDIR,	20 * sizeof (prdirent_t), sizeof (prdirent_t),
155 		"root" },
156 	{ PR_FDDIR,	21 * sizeof (prdirent_t), sizeof (prdirent_t),
157 		"fd" },
158 	{ PR_FDINFODIR,	22 * sizeof (prdirent_t), sizeof (prdirent_t),
159 		"fdinfo" },
160 	{ PR_OBJECTDIR,	23 * sizeof (prdirent_t), sizeof (prdirent_t),
161 		"object" },
162 	{ PR_LWPDIR,	24 * sizeof (prdirent_t), sizeof (prdirent_t),
163 		"lwp" },
164 	{ PR_PRIV,	25 * sizeof (prdirent_t), sizeof (prdirent_t),
165 		"priv" },
166 	{ PR_PATHDIR,	26 * sizeof (prdirent_t), sizeof (prdirent_t),
167 		"path" },
168 	{ PR_CTDIR,	27 * sizeof (prdirent_t), sizeof (prdirent_t),
169 		"contracts" },
170 	{ PR_SECFLAGS,	28 * sizeof (prdirent_t), sizeof (prdirent_t),
171 		"secflags" },
172 #if defined(__x86)
173 	{ PR_LDT,	29 * sizeof (prdirent_t), sizeof (prdirent_t),
174 		"ldt" },
175 #endif
176 };
177 
178 #define	NPIDDIRFILES	(sizeof (piddir) / sizeof (piddir[0]) - 2)
179 
180 /*
181  * Contents of a /proc/<pid>/lwp/<lwpid> directory.
182  */
183 static prdirent_t lwpiddir[] = {
184 	{ PR_LWPIDDIR,	 1 * sizeof (prdirent_t), sizeof (prdirent_t),
185 		"." },
186 	{ PR_LWPDIR,	 2 * sizeof (prdirent_t), sizeof (prdirent_t),
187 		".." },
188 	{ PR_LWPCTL,	 3 * sizeof (prdirent_t), sizeof (prdirent_t),
189 		"lwpctl" },
190 	{ PR_LWPNAME,	 4 * sizeof (prdirent_t), sizeof (prdirent_t),
191 		"lwpname" },
192 	{ PR_LWPSTATUS,	 5 * sizeof (prdirent_t), sizeof (prdirent_t),
193 		"lwpstatus" },
194 	{ PR_LWPSINFO,	 6 * sizeof (prdirent_t), sizeof (prdirent_t),
195 		"lwpsinfo" },
196 	{ PR_LWPUSAGE,	 7 * sizeof (prdirent_t), sizeof (prdirent_t),
197 		"lwpusage" },
198 	{ PR_XREGS,	 8 * sizeof (prdirent_t), sizeof (prdirent_t),
199 		"xregs" },
200 	{ PR_TMPLDIR,	 9 * sizeof (prdirent_t), sizeof (prdirent_t),
201 		"templates" },
202 	{ PR_SPYMASTER,	 10 * sizeof (prdirent_t), sizeof (prdirent_t),
203 		"spymaster" },
204 #if defined(__sparc)
205 	{ PR_GWINDOWS,	11 * sizeof (prdirent_t), sizeof (prdirent_t),
206 		"gwindows" },
207 	{ PR_ASRS,	12 * sizeof (prdirent_t), sizeof (prdirent_t),
208 		"asrs" },
209 #endif
210 };
211 
212 #define	NLWPIDDIRFILES	(sizeof (lwpiddir) / sizeof (lwpiddir[0]) - 2)
213 
214 /*
215  * Span of entries in the array files (lstatus, lpsinfo, lusage).
216  * We make the span larger than the size of the structure on purpose,
217  * to make sure that programs cannot use the structure size by mistake.
218  * Align _ILP32 structures at 8 bytes, _LP64 structures at 16 bytes.
219  */
220 #ifdef _LP64
221 #define	LSPAN(type)	(round16(sizeof (type)) + 16)
222 #define	LSPAN32(type)	(round8(sizeof (type)) + 8)
223 #else
224 #define	LSPAN(type)	(round8(sizeof (type)) + 8)
225 #endif
226 
227 static void rebuild_objdir(struct as *);
228 static void prfreecommon(prcommon_t *);
229 static int praccess(vnode_t *, int, int, cred_t *, caller_context_t *);
230 
231 static int
propen(vnode_t ** vpp,int flag,cred_t * cr,caller_context_t * ct)232 propen(vnode_t **vpp, int flag, cred_t *cr, caller_context_t *ct)
233 {
234 	vnode_t *vp = *vpp;
235 	prnode_t *pnp = VTOP(vp);
236 	prcommon_t *pcp = pnp->pr_pcommon;
237 	prnodetype_t type = pnp->pr_type;
238 	vnode_t *rvp;
239 	vtype_t vtype;
240 	proc_t *p;
241 	int error = 0;
242 	prnode_t *npnp = NULL;
243 
244 	/*
245 	 * Nothing to do for the /proc directory itself.
246 	 */
247 	if (type == PR_PROCDIR)
248 		return (0);
249 
250 	/*
251 	 * If we are opening an underlying mapped object, reject opens
252 	 * for writing regardless of the objects's access modes.
253 	 * If we are opening a file in the /proc/pid/fd directory,
254 	 * reject the open for any but a regular file or directory.
255 	 * Just do it if we are opening the current or root directory.
256 	 */
257 	switch (type) {
258 	case PR_OBJECT:
259 	case PR_FD:
260 	case PR_CURDIR:
261 	case PR_ROOTDIR:
262 		rvp = pnp->pr_realvp;
263 		vtype = rvp->v_type;
264 		if ((type == PR_OBJECT && (flag & FWRITE)) ||
265 		    (type == PR_FD && vtype != VREG && vtype != VDIR))
266 			error = EACCES;
267 		else {
268 			/*
269 			 * Need to hold rvp since VOP_OPEN() may release it.
270 			 */
271 			VN_HOLD(rvp);
272 			error = VOP_OPEN(&rvp, flag, cr, ct);
273 			if (error) {
274 				VN_RELE(rvp);
275 			} else {
276 				*vpp = rvp;
277 				VN_RELE(vp);
278 			}
279 		}
280 		return (error);
281 	default:
282 		break;
283 	}
284 
285 	/*
286 	 * If we are opening the pagedata file, allocate a prnode now
287 	 * to avoid calling kmem_alloc() while holding p->p_lock.
288 	 */
289 	if (type == PR_PAGEDATA || type == PR_OPAGEDATA)
290 		npnp = prgetnode(vp, type);
291 
292 	/*
293 	 * If the process exists, lock it now.
294 	 * Otherwise we have a race condition with prclose().
295 	 */
296 	p = pr_p_lock(pnp);
297 	mutex_exit(&pr_pidlock);
298 	if (p == NULL) {
299 		if (npnp != NULL)
300 			prfreenode(npnp);
301 		return (ENOENT);
302 	}
303 	ASSERT(p == pcp->prc_proc);
304 	ASSERT(p->p_proc_flag & P_PR_LOCK);
305 
306 	/*
307 	 * Maintain a count of opens for write.  Allow exactly one
308 	 * O_WRITE|O_EXCL request and fail subsequent ones.
309 	 * Don't fail opens of old (bletch!) /proc lwp files.
310 	 * Special case for open by the process itself:
311 	 * Always allow the open by self and discount this
312 	 * open for other opens for writing.
313 	 */
314 	if (flag & FWRITE) {
315 		if (p == curproc) {
316 			pcp->prc_selfopens++;
317 			pnp->pr_flags |= PR_ISSELF;
318 		} else if (type == PR_LWPIDFILE) {
319 			/* EMPTY */;
320 		} else if (flag & FEXCL) {
321 			if (pcp->prc_writers > pcp->prc_selfopens) {
322 				error = EBUSY;
323 				goto out;
324 			}
325 			/* semantic for old /proc interface */
326 			if (type == PR_PIDDIR)
327 				pcp->prc_flags |= PRC_EXCL;
328 		} else if (pcp->prc_flags & PRC_EXCL) {
329 			ASSERT(pcp->prc_writers > pcp->prc_selfopens);
330 			error = secpolicy_proc_excl_open(cr);
331 			if (error)
332 				goto out;
333 		}
334 		pcp->prc_writers++;
335 		/*
336 		 * The vnode may have become invalid between the
337 		 * VOP_LOOKUP() of the /proc vnode and the VOP_OPEN().
338 		 * If so, do now what prinvalidate() should have done.
339 		 */
340 		if ((pnp->pr_flags & PR_INVAL) ||
341 		    (type == PR_PIDDIR &&
342 		    (VTOP(pnp->pr_pidfile)->pr_flags & PR_INVAL))) {
343 			if (p != curproc)
344 				pcp->prc_selfopens++;
345 			ASSERT(pcp->prc_selfopens <= pcp->prc_writers);
346 			if (pcp->prc_selfopens == pcp->prc_writers)
347 				pcp->prc_flags &= ~PRC_EXCL;
348 		}
349 	}
350 
351 	/*
352 	 * If this is a large file open, indicate that in our flags -- some
353 	 * procfs structures are not off_t-neutral (e.g., priovec_t), and
354 	 * the open will need to be differentiated where 32-bit processes
355 	 * pass these structures across the user/kernel boundary.
356 	 */
357 	if (flag & FOFFMAX)
358 		pnp->pr_flags |= PR_OFFMAX;
359 
360 	/*
361 	 * Do file-specific things.
362 	 */
363 	switch (type) {
364 	default:
365 		break;
366 	case PR_PAGEDATA:
367 	case PR_OPAGEDATA:
368 		/*
369 		 * Enable data collection for page data file;
370 		 * get unique id from the hat layer.
371 		 */
372 		{
373 			int id;
374 
375 			/*
376 			 * Drop p->p_lock to call hat_startstat()
377 			 */
378 			mutex_exit(&p->p_lock);
379 			if ((p->p_flag & SSYS) || p->p_as == &kas ||
380 			    (id = hat_startstat(p->p_as)) == -1) {
381 				mutex_enter(&p->p_lock);
382 				error = ENOMEM;
383 			} else if (pnp->pr_hatid == 0) {
384 				mutex_enter(&p->p_lock);
385 				pnp->pr_hatid = (uint_t)id;
386 			} else {
387 				mutex_enter(&p->p_lock);
388 				/*
389 				 * Use our newly allocated prnode.
390 				 */
391 				npnp->pr_hatid = (uint_t)id;
392 				/*
393 				 * prgetnode() initialized most of the prnode.
394 				 * Duplicate the remainder.
395 				 */
396 				npnp->pr_ino = pnp->pr_ino;
397 				npnp->pr_common = pnp->pr_common;
398 				npnp->pr_pcommon = pnp->pr_pcommon;
399 				npnp->pr_parent = pnp->pr_parent;
400 				VN_HOLD(npnp->pr_parent);
401 				npnp->pr_index = pnp->pr_index;
402 
403 				npnp->pr_next = p->p_plist;
404 				p->p_plist = PTOV(npnp);
405 
406 				VN_RELE(PTOV(pnp));
407 				pnp = npnp;
408 				npnp = NULL;
409 				*vpp = PTOV(pnp);
410 			}
411 		}
412 		break;
413 	}
414 
415 out:
416 	prunlock(pnp);
417 
418 	if (npnp != NULL)
419 		prfreenode(npnp);
420 	return (error);
421 }
422 
423 /* ARGSUSED */
424 static int
prclose(vnode_t * vp,int flag,int count,offset_t offset,cred_t * cr,caller_context_t * ct)425 prclose(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr,
426     caller_context_t *ct)
427 {
428 	prnode_t *pnp = VTOP(vp);
429 	prcommon_t *pcp = pnp->pr_pcommon;
430 	prnodetype_t type = pnp->pr_type;
431 	proc_t *p;
432 	kthread_t *t;
433 	user_t *up;
434 
435 	/*
436 	 * Nothing to do for the /proc directory itself.
437 	 */
438 	if (type == PR_PROCDIR)
439 		return (0);
440 
441 	ASSERT(type != PR_OBJECT && type != PR_FD &&
442 	    type != PR_CURDIR && type != PR_ROOTDIR);
443 
444 	/*
445 	 * If the process exists, lock it now.
446 	 * Otherwise we have a race condition with propen().
447 	 * Hold pr_pidlock across the reference to prc_selfopens,
448 	 * and prc_writers in case there is no process anymore,
449 	 * to cover the case of concurrent calls to prclose()
450 	 * after the process has been reaped by freeproc().
451 	 */
452 	p = pr_p_lock(pnp);
453 
454 	/*
455 	 * There is nothing more to do until the last close of
456 	 * the file table entry except to clear the pr_owner
457 	 * field of the prnode and notify any waiters
458 	 * (their file descriptor may have just been closed).
459 	 */
460 	if (count > 1) {
461 		mutex_exit(&pr_pidlock);
462 		if (pnp->pr_owner == curproc && !fisopen(vp))
463 			pnp->pr_owner = NULL;
464 		if (p != NULL) {
465 			prnotify(vp);
466 			prunlock(pnp);
467 		}
468 		return (0);
469 	}
470 
471 	/*
472 	 * Decrement the count of self-opens for writing.
473 	 * Decrement the total count of opens for writing.
474 	 * Cancel exclusive opens when only self-opens remain.
475 	 */
476 	if (flag & FWRITE) {
477 		/*
478 		 * prc_selfopens also contains the count of
479 		 * invalid writers.  See prinvalidate().
480 		 */
481 		if ((pnp->pr_flags & (PR_ISSELF|PR_INVAL)) ||
482 		    (type == PR_PIDDIR &&
483 		    (VTOP(pnp->pr_pidfile)->pr_flags & PR_INVAL))) {
484 			ASSERT(pcp->prc_selfopens != 0);
485 			--pcp->prc_selfopens;
486 		}
487 		ASSERT(pcp->prc_writers != 0);
488 		if (--pcp->prc_writers == pcp->prc_selfopens)
489 			pcp->prc_flags &= ~PRC_EXCL;
490 	}
491 	ASSERT(pcp->prc_writers >= pcp->prc_selfopens);
492 	mutex_exit(&pr_pidlock);
493 	if (pnp->pr_owner == curproc && !fisopen(vp))
494 		pnp->pr_owner = NULL;
495 
496 	/*
497 	 * If there is no process, there is nothing more to do.
498 	 */
499 	if (p == NULL)
500 		return (0);
501 
502 	ASSERT(p == pcp->prc_proc);
503 	prnotify(vp);	/* notify waiters */
504 
505 	/*
506 	 * Do file-specific things.
507 	 */
508 	switch (type) {
509 	default:
510 		break;
511 	case PR_PAGEDATA:
512 	case PR_OPAGEDATA:
513 		/*
514 		 * This is a page data file.
515 		 * Free the hat level statistics.
516 		 * Drop p->p_lock before calling hat_freestat().
517 		 */
518 		mutex_exit(&p->p_lock);
519 		if (p->p_as != &kas && pnp->pr_hatid != 0)
520 			hat_freestat(p->p_as, pnp->pr_hatid);
521 		mutex_enter(&p->p_lock);
522 		pnp->pr_hatid = 0;
523 		break;
524 	}
525 
526 	/*
527 	 * On last close of all writable file descriptors,
528 	 * perform run-on-last-close and/or kill-on-last-close logic.
529 	 * Can't do this is the /proc agent lwp still exists.
530 	 */
531 	if (pcp->prc_writers == 0 &&
532 	    p->p_agenttp == NULL &&
533 	    !(pcp->prc_flags & PRC_DESTROY) &&
534 	    p->p_stat != SZOMB &&
535 	    (p->p_proc_flag & (P_PR_RUNLCL|P_PR_KILLCL))) {
536 		int killproc;
537 
538 		/*
539 		 * Cancel any watchpoints currently in effect.
540 		 * The process might disappear during this operation.
541 		 */
542 		if (pr_cancel_watch(pnp) == NULL)
543 			return (0);
544 		/*
545 		 * If any tracing flags are set, clear them.
546 		 */
547 		if (p->p_proc_flag & P_PR_TRACE) {
548 			up = PTOU(p);
549 			premptyset(&up->u_entrymask);
550 			premptyset(&up->u_exitmask);
551 			up->u_systrap = 0;
552 		}
553 		premptyset(&p->p_sigmask);
554 		premptyset(&p->p_fltmask);
555 		killproc = (p->p_proc_flag & P_PR_KILLCL);
556 		p->p_proc_flag &= ~(P_PR_RUNLCL|P_PR_KILLCL|P_PR_TRACE);
557 		/*
558 		 * Cancel any outstanding single-step requests.
559 		 */
560 		if ((t = p->p_tlist) != NULL) {
561 			/*
562 			 * Drop p_lock because prnostep() touches the stack.
563 			 * The loop is safe because the process is P_PR_LOCK'd.
564 			 */
565 			mutex_exit(&p->p_lock);
566 			do {
567 				prnostep(ttolwp(t));
568 			} while ((t = t->t_forw) != p->p_tlist);
569 			mutex_enter(&p->p_lock);
570 		}
571 		/*
572 		 * Set runnable all lwps stopped by /proc.
573 		 */
574 		if (killproc)
575 			sigtoproc(p, NULL, SIGKILL);
576 		else
577 			allsetrun(p);
578 	}
579 
580 	prunlock(pnp);
581 	return (0);
582 }
583 
584 /*
585  * Array of read functions, indexed by /proc file type.
586  */
587 static int pr_read_inval(), pr_read_as(), pr_read_status(),
588 	pr_read_lstatus(), pr_read_psinfo(), pr_read_lpsinfo(),
589 	pr_read_map(), pr_read_rmap(), pr_read_xmap(),
590 	pr_read_cred(), pr_read_sigact(), pr_read_auxv(),
591 #if defined(__x86)
592 	pr_read_ldt(),
593 #endif
594 	pr_read_usage(), pr_read_lusage(), pr_read_pagedata(),
595 	pr_read_watch(), pr_read_lwpstatus(), pr_read_lwpsinfo(),
596 	pr_read_lwpusage(), pr_read_lwpname(),
597 	pr_read_xregs(), pr_read_priv(),
598 	pr_read_spymaster(), pr_read_secflags(),
599 #if defined(__sparc)
600 	pr_read_gwindows(), pr_read_asrs(),
601 #endif
602 	pr_read_piddir(), pr_read_pidfile(), pr_read_opagedata(),
603 	pr_read_fdinfo();
604 
605 static int (*pr_read_function[PR_NFILES])() = {
606 	pr_read_inval,		/* /proc				*/
607 	pr_read_inval,		/* /proc/self				*/
608 	pr_read_piddir,		/* /proc/<pid> (old /proc read())	*/
609 	pr_read_as,		/* /proc/<pid>/as			*/
610 	pr_read_inval,		/* /proc/<pid>/ctl			*/
611 	pr_read_status,		/* /proc/<pid>/status			*/
612 	pr_read_lstatus,	/* /proc/<pid>/lstatus			*/
613 	pr_read_psinfo,		/* /proc/<pid>/psinfo			*/
614 	pr_read_lpsinfo,	/* /proc/<pid>/lpsinfo			*/
615 	pr_read_map,		/* /proc/<pid>/map			*/
616 	pr_read_rmap,		/* /proc/<pid>/rmap			*/
617 	pr_read_xmap,		/* /proc/<pid>/xmap			*/
618 	pr_read_cred,		/* /proc/<pid>/cred			*/
619 	pr_read_sigact,		/* /proc/<pid>/sigact			*/
620 	pr_read_auxv,		/* /proc/<pid>/auxv			*/
621 #if defined(__x86)
622 	pr_read_ldt,		/* /proc/<pid>/ldt			*/
623 #endif
624 	pr_read_usage,		/* /proc/<pid>/usage			*/
625 	pr_read_lusage,		/* /proc/<pid>/lusage			*/
626 	pr_read_pagedata,	/* /proc/<pid>/pagedata			*/
627 	pr_read_watch,		/* /proc/<pid>/watch			*/
628 	pr_read_inval,		/* /proc/<pid>/cwd			*/
629 	pr_read_inval,		/* /proc/<pid>/root			*/
630 	pr_read_inval,		/* /proc/<pid>/fd			*/
631 	pr_read_inval,		/* /proc/<pid>/fd/nn			*/
632 	pr_read_inval,		/* /proc/<pid>/fdinfo			*/
633 	pr_read_fdinfo,		/* /proc/<pid>/fdinfo/nn		*/
634 	pr_read_inval,		/* /proc/<pid>/object			*/
635 	pr_read_inval,		/* /proc/<pid>/object/xxx		*/
636 	pr_read_inval,		/* /proc/<pid>/lwp			*/
637 	pr_read_inval,		/* /proc/<pid>/lwp/<lwpid>		*/
638 	pr_read_inval,		/* /proc/<pid>/lwp/<lwpid>/lwpctl	*/
639 	pr_read_lwpname,	/* /proc/<pid>/lwp/<lwpid>/lwpname	*/
640 	pr_read_lwpstatus,	/* /proc/<pid>/lwp/<lwpid>/lwpstatus	*/
641 	pr_read_lwpsinfo,	/* /proc/<pid>/lwp/<lwpid>/lwpsinfo	*/
642 	pr_read_lwpusage,	/* /proc/<pid>/lwp/<lwpid>/lwpusage	*/
643 	pr_read_xregs,		/* /proc/<pid>/lwp/<lwpid>/xregs	*/
644 	pr_read_inval,		/* /proc/<pid>/lwp/<lwpid>/templates	*/
645 	pr_read_inval,		/* /proc/<pid>/lwp/<lwpid>/templates/<id> */
646 	pr_read_spymaster,	/* /proc/<pid>/lwp/<lwpid>/spymaster	*/
647 #if defined(__sparc)
648 	pr_read_gwindows,	/* /proc/<pid>/lwp/<lwpid>/gwindows	*/
649 	pr_read_asrs,		/* /proc/<pid>/lwp/<lwpid>/asrs		*/
650 #endif
651 	pr_read_priv,		/* /proc/<pid>/priv			*/
652 	pr_read_inval,		/* /proc/<pid>/path			*/
653 	pr_read_inval,		/* /proc/<pid>/path/xxx			*/
654 	pr_read_inval,		/* /proc/<pid>/contracts		*/
655 	pr_read_inval,		/* /proc/<pid>/contracts/<ctid>		*/
656 	pr_read_secflags,	/* /proc/<pid>/secflags			*/
657 	pr_read_pidfile,	/* old process file			*/
658 	pr_read_pidfile,	/* old lwp file				*/
659 	pr_read_opagedata,	/* old pagedata file			*/
660 };
661 
662 /* ARGSUSED */
663 static int
pr_read_inval(prnode_t * pnp,uio_t * uiop,cred_t * cr)664 pr_read_inval(prnode_t *pnp, uio_t *uiop, cred_t *cr)
665 {
666 	/*
667 	 * No read() on any /proc directory, use getdents(2) instead.
668 	 * Cannot read a control file either.
669 	 * An underlying mapped object file cannot get here.
670 	 */
671 	return (EINVAL);
672 }
673 
674 static int
pr_uioread(void * base,long count,uio_t * uiop)675 pr_uioread(void *base, long count, uio_t *uiop)
676 {
677 	int error = 0;
678 
679 	ASSERT(count >= 0);
680 	count -= uiop->uio_offset;
681 	if (count > 0 && uiop->uio_offset >= 0) {
682 		error = uiomove((char *)base + uiop->uio_offset,
683 		    count, UIO_READ, uiop);
684 	}
685 
686 	return (error);
687 }
688 
689 static int
pr_read_as(prnode_t * pnp,uio_t * uiop)690 pr_read_as(prnode_t *pnp, uio_t *uiop)
691 {
692 	int error;
693 
694 	ASSERT(pnp->pr_type == PR_AS);
695 
696 	if ((error = prlock(pnp, ZNO)) == 0) {
697 		proc_t *p = pnp->pr_common->prc_proc;
698 		struct as *as = p->p_as;
699 
700 		/*
701 		 * /proc I/O cannot be done to a system process.
702 		 * A 32-bit process cannot read a 64-bit process.
703 		 */
704 		if ((p->p_flag & SSYS) || as == &kas) {
705 			error = 0;
706 #ifdef _SYSCALL32_IMPL
707 		} else if (curproc->p_model == DATAMODEL_ILP32 &&
708 		    PROCESS_NOT_32BIT(p)) {
709 			error = EOVERFLOW;
710 #endif
711 		} else {
712 			/*
713 			 * We don't hold p_lock over an i/o operation because
714 			 * that could lead to deadlock with the clock thread.
715 			 */
716 			mutex_exit(&p->p_lock);
717 			error = prusrio(p, UIO_READ, uiop, 0);
718 			mutex_enter(&p->p_lock);
719 		}
720 		prunlock(pnp);
721 	}
722 
723 	return (error);
724 }
725 
726 static int
pr_read_status(prnode_t * pnp,uio_t * uiop,cred_t * cr)727 pr_read_status(prnode_t *pnp, uio_t *uiop, cred_t *cr)
728 {
729 	pstatus_t *sp;
730 	int error;
731 
732 	ASSERT(pnp->pr_type == PR_STATUS);
733 
734 	/*
735 	 * We kmem_alloc() the pstatus structure because
736 	 * it is so big it might blow the kernel stack.
737 	 */
738 	sp = kmem_alloc(sizeof (*sp), KM_SLEEP);
739 	if ((error = prlock(pnp, ZNO)) == 0) {
740 		prgetstatus(pnp->pr_common->prc_proc, sp, VTOZONE(PTOV(pnp)));
741 		prunlock(pnp);
742 		error = pr_uioread(sp, sizeof (*sp), uiop);
743 	}
744 	kmem_free(sp, sizeof (*sp));
745 	return (error);
746 }
747 
748 static int
pr_read_lstatus(prnode_t * pnp,uio_t * uiop,cred_t * cr)749 pr_read_lstatus(prnode_t *pnp, uio_t *uiop, cred_t *cr)
750 {
751 	proc_t *p;
752 	kthread_t *t;
753 	lwpdir_t *ldp;
754 	size_t size;
755 	prheader_t *php;
756 	lwpstatus_t *sp;
757 	int error;
758 	int nlwp;
759 	int i;
760 
761 	ASSERT(pnp->pr_type == PR_LSTATUS);
762 
763 	if ((error = prlock(pnp, ZNO)) != 0)
764 		return (error);
765 	p = pnp->pr_common->prc_proc;
766 	nlwp = p->p_lwpcnt;
767 	size = sizeof (prheader_t) + nlwp * LSPAN(lwpstatus_t);
768 
769 	/* drop p->p_lock to do kmem_alloc(KM_SLEEP) */
770 	mutex_exit(&p->p_lock);
771 	php = kmem_zalloc(size, KM_SLEEP);
772 	mutex_enter(&p->p_lock);
773 	/* p->p_lwpcnt can't change while process is locked */
774 	ASSERT(nlwp == p->p_lwpcnt);
775 
776 	php->pr_nent = nlwp;
777 	php->pr_entsize = LSPAN(lwpstatus_t);
778 
779 	sp = (lwpstatus_t *)(php + 1);
780 	for (ldp = p->p_lwpdir, i = 0; i < p->p_lwpdir_sz; i++, ldp++) {
781 		if (ldp->ld_entry == NULL ||
782 		    (t = ldp->ld_entry->le_thread) == NULL)
783 			continue;
784 		prgetlwpstatus(t, sp, VTOZONE(PTOV(pnp)));
785 		sp = (lwpstatus_t *)((caddr_t)sp + LSPAN(lwpstatus_t));
786 	}
787 	prunlock(pnp);
788 
789 	error = pr_uioread(php, size, uiop);
790 	kmem_free(php, size);
791 	return (error);
792 }
793 
794 static int
pr_read_psinfo(prnode_t * pnp,uio_t * uiop,cred_t * cr)795 pr_read_psinfo(prnode_t *pnp, uio_t *uiop, cred_t *cr)
796 {
797 	psinfo_t psinfo;
798 	proc_t *p;
799 	int error = 0;
800 
801 	ASSERT(pnp->pr_type == PR_PSINFO);
802 
803 	/*
804 	 * We don't want the full treatment of prlock(pnp) here.
805 	 * This file is world-readable and never goes invalid.
806 	 * It doesn't matter if we are in the middle of an exec().
807 	 */
808 	p = pr_p_lock(pnp);
809 	mutex_exit(&pr_pidlock);
810 	if (p == NULL)
811 		error = ENOENT;
812 	else {
813 		ASSERT(p == pnp->pr_common->prc_proc);
814 		prgetpsinfo(p, &psinfo);
815 		prunlock(pnp);
816 		error = pr_uioread(&psinfo, sizeof (psinfo), uiop);
817 	}
818 	return (error);
819 }
820 
821 static int
pr_read_fdinfo(prnode_t * pnp,uio_t * uiop,cred_t * cr)822 pr_read_fdinfo(prnode_t *pnp, uio_t *uiop, cred_t *cr)
823 {
824 	prfdinfo_t *fdinfo;
825 	list_t data;
826 	proc_t *p;
827 	vnode_t *vp;
828 	uint_t fd;
829 	file_t *fp;
830 	cred_t *file_cred;
831 	short ufp_flag;
832 	int error = 0;
833 
834 	ASSERT(pnp->pr_type == PR_FDINFO);
835 
836 	/*
837 	 * This is a guess at the size of the structure that needs to
838 	 * be returned. It's a balance between not allocating too much more
839 	 * space than is required and not requiring too many subsequent
840 	 * reallocations. Allocate it before acquiring the process lock.
841 	 */
842 	pr_iol_initlist(&data, sizeof (prfdinfo_t) + MAXPATHLEN + 2, 1);
843 
844 	if ((error = prlock(pnp, ZNO)) != 0) {
845 		pr_iol_freelist(&data);
846 		return (error);
847 	}
848 
849 	p = pnp->pr_common->prc_proc;
850 
851 	if ((p->p_flag & SSYS) || p->p_as == &kas) {
852 		prunlock(pnp);
853 		pr_iol_freelist(&data);
854 		return (0);
855 	}
856 
857 	fd = pnp->pr_index;
858 
859 	/* Fetch and lock the file_t for this descriptor */
860 	fp = pr_getf(p, fd, &ufp_flag);
861 
862 	if (fp == NULL) {
863 		error = ENOENT;
864 		prunlock(pnp);
865 		goto out;
866 	}
867 
868 	vp = fp->f_vnode;
869 	VN_HOLD(vp);
870 
871 	/*
872 	 * For fdinfo, we don't want to include the placeholder pr_misc at the
873 	 * end of the struct. We'll terminate the data with an empty pr_misc
874 	 * header before returning.
875 	 */
876 
877 	fdinfo = pr_iol_newbuf(&data, offsetof(prfdinfo_t, pr_misc));
878 	fdinfo->pr_fd = fd;
879 	fdinfo->pr_fdflags = ufp_flag;
880 	fdinfo->pr_fileflags = fp->f_flag2 << 16 | fp->f_flag;
881 	if ((fdinfo->pr_fileflags & (FSEARCH | FEXEC)) == 0)
882 		fdinfo->pr_fileflags += FOPEN;
883 	fdinfo->pr_offset = fp->f_offset;
884 	file_cred = fp->f_cred;
885 	crhold(file_cred);
886 	/*
887 	 * Information from the vnode (rather than the file_t) is retrieved
888 	 * later, in prgetfdinfo() - for example sock_getfasync()
889 	 */
890 	pr_releasef(p, fd);
891 
892 	prunlock(pnp);
893 
894 	error = prgetfdinfo(p, vp, fdinfo, cr, file_cred, &data);
895 
896 	crfree(file_cred);
897 
898 	VN_RELE(vp);
899 
900 out:
901 	if (error == 0)
902 		error = pr_iol_uiomove_and_free(&data, uiop, error);
903 	else
904 		pr_iol_freelist(&data);
905 
906 	return (error);
907 }
908 
909 static int
pr_read_lpsinfo(prnode_t * pnp,uio_t * uiop,cred_t * cr)910 pr_read_lpsinfo(prnode_t *pnp, uio_t *uiop, cred_t *cr)
911 {
912 	proc_t *p;
913 	kthread_t *t;
914 	lwpdir_t *ldp;
915 	lwpent_t *lep;
916 	size_t size;
917 	prheader_t *php;
918 	lwpsinfo_t *sp;
919 	int error;
920 	int nlwp;
921 	int i;
922 
923 	ASSERT(pnp->pr_type == PR_LPSINFO);
924 
925 	/*
926 	 * We don't want the full treatment of prlock(pnp) here.
927 	 * This file is world-readable and never goes invalid.
928 	 * It doesn't matter if we are in the middle of an exec().
929 	 */
930 	p = pr_p_lock(pnp);
931 	mutex_exit(&pr_pidlock);
932 	if (p == NULL)
933 		return (ENOENT);
934 	ASSERT(p == pnp->pr_common->prc_proc);
935 	if ((nlwp = p->p_lwpcnt + p->p_zombcnt) == 0) {
936 		prunlock(pnp);
937 		return (ENOENT);
938 	}
939 	size = sizeof (prheader_t) + nlwp * LSPAN(lwpsinfo_t);
940 
941 	/* drop p->p_lock to do kmem_alloc(KM_SLEEP) */
942 	mutex_exit(&p->p_lock);
943 	php = kmem_zalloc(size, KM_SLEEP);
944 	mutex_enter(&p->p_lock);
945 	/* p->p_lwpcnt can't change while process is locked */
946 	ASSERT(nlwp == p->p_lwpcnt + p->p_zombcnt);
947 
948 	php->pr_nent = nlwp;
949 	php->pr_entsize = LSPAN(lwpsinfo_t);
950 
951 	sp = (lwpsinfo_t *)(php + 1);
952 	for (ldp = p->p_lwpdir, i = 0; i < p->p_lwpdir_sz; i++, ldp++) {
953 		if ((lep = ldp->ld_entry) == NULL)
954 			continue;
955 		if ((t = lep->le_thread) != NULL)
956 			prgetlwpsinfo(t, sp);
957 		else {
958 			bzero(sp, sizeof (*sp));
959 			sp->pr_lwpid = lep->le_lwpid;
960 			sp->pr_state = SZOMB;
961 			sp->pr_sname = 'Z';
962 			sp->pr_start.tv_sec = lep->le_start;
963 			sp->pr_bindpro = PBIND_NONE;
964 			sp->pr_bindpset = PS_NONE;
965 		}
966 		sp = (lwpsinfo_t *)((caddr_t)sp + LSPAN(lwpsinfo_t));
967 	}
968 	prunlock(pnp);
969 
970 	error = pr_uioread(php, size, uiop);
971 	kmem_free(php, size);
972 	return (error);
973 }
974 
975 static int
pr_read_map_common(prnode_t * pnp,uio_t * uiop,prnodetype_t type)976 pr_read_map_common(prnode_t *pnp, uio_t *uiop, prnodetype_t type)
977 {
978 	proc_t *p;
979 	struct as *as;
980 	list_t iolhead;
981 	int error;
982 
983 readmap_common:
984 	if ((error = prlock(pnp, ZNO)) != 0)
985 		return (error);
986 
987 	p = pnp->pr_common->prc_proc;
988 	as = p->p_as;
989 
990 	if ((p->p_flag & SSYS) || as == &kas) {
991 		prunlock(pnp);
992 		return (0);
993 	}
994 
995 	if (!AS_LOCK_TRYENTER(as, RW_WRITER)) {
996 		prunlock(pnp);
997 		delay(1);
998 		goto readmap_common;
999 	}
1000 	mutex_exit(&p->p_lock);
1001 
1002 	switch (type) {
1003 	case PR_XMAP:
1004 		error = prgetxmap(p, &iolhead);
1005 		break;
1006 	case PR_RMAP:
1007 		error = prgetmap(p, 1, &iolhead);
1008 		break;
1009 	case PR_MAP:
1010 		error = prgetmap(p, 0, &iolhead);
1011 		break;
1012 	}
1013 
1014 	AS_LOCK_EXIT(as);
1015 	mutex_enter(&p->p_lock);
1016 	prunlock(pnp);
1017 
1018 	error = pr_iol_uiomove_and_free(&iolhead, uiop, error);
1019 
1020 	return (error);
1021 }
1022 
1023 static int
pr_read_map(prnode_t * pnp,uio_t * uiop,cred_t * cr)1024 pr_read_map(prnode_t *pnp, uio_t *uiop, cred_t *cr)
1025 {
1026 	ASSERT(pnp->pr_type == PR_MAP);
1027 	return (pr_read_map_common(pnp, uiop, pnp->pr_type));
1028 }
1029 
1030 static int
pr_read_rmap(prnode_t * pnp,uio_t * uiop,cred_t * cr)1031 pr_read_rmap(prnode_t *pnp, uio_t *uiop, cred_t *cr)
1032 {
1033 	ASSERT(pnp->pr_type == PR_RMAP);
1034 	return (pr_read_map_common(pnp, uiop, pnp->pr_type));
1035 }
1036 
1037 static int
pr_read_xmap(prnode_t * pnp,uio_t * uiop,cred_t * cr)1038 pr_read_xmap(prnode_t *pnp, uio_t *uiop, cred_t *cr)
1039 {
1040 	ASSERT(pnp->pr_type == PR_XMAP);
1041 	return (pr_read_map_common(pnp, uiop, pnp->pr_type));
1042 }
1043 
1044 static int
pr_read_cred(prnode_t * pnp,uio_t * uiop,cred_t * cr)1045 pr_read_cred(prnode_t *pnp, uio_t *uiop, cred_t *cr)
1046 {
1047 	proc_t *p;
1048 	prcred_t *pcrp;
1049 	int error;
1050 	size_t count;
1051 
1052 	ASSERT(pnp->pr_type == PR_CRED);
1053 
1054 	/*
1055 	 * We kmem_alloc() the prcred_t structure because
1056 	 * the number of supplementary groups is variable.
1057 	 */
1058 	pcrp =
1059 	    kmem_alloc(sizeof (prcred_t) + sizeof (gid_t) * (ngroups_max - 1),
1060 	    KM_SLEEP);
1061 
1062 	if ((error = prlock(pnp, ZNO)) != 0)
1063 		goto out;
1064 	p = pnp->pr_common->prc_proc;
1065 	ASSERT(p != NULL);
1066 
1067 	prgetcred(p, pcrp);
1068 	prunlock(pnp);
1069 
1070 	count = sizeof (prcred_t);
1071 	if (pcrp->pr_ngroups > 1)
1072 		count += sizeof (gid_t) * (pcrp->pr_ngroups - 1);
1073 	error = pr_uioread(pcrp, count, uiop);
1074 out:
1075 	kmem_free(pcrp, sizeof (prcred_t) + sizeof (gid_t) * (ngroups_max - 1));
1076 	return (error);
1077 }
1078 
1079 static int
pr_read_priv(prnode_t * pnp,uio_t * uiop,cred_t * cr)1080 pr_read_priv(prnode_t *pnp, uio_t *uiop, cred_t *cr)
1081 {
1082 	proc_t *p;
1083 	size_t psize = prgetprivsize();
1084 	prpriv_t *ppriv = kmem_alloc(psize, KM_SLEEP);
1085 	int error;
1086 
1087 	ASSERT(pnp->pr_type == PR_PRIV);
1088 
1089 	if ((error = prlock(pnp, ZNO)) != 0)
1090 		goto out;
1091 	p = pnp->pr_common->prc_proc;
1092 	ASSERT(p != NULL);
1093 
1094 	prgetpriv(p, ppriv);
1095 	prunlock(pnp);
1096 
1097 	error = pr_uioread(ppriv, psize, uiop);
1098 out:
1099 	kmem_free(ppriv, psize);
1100 	return (error);
1101 }
1102 
1103 static int
pr_read_sigact(prnode_t * pnp,uio_t * uiop,cred_t * cr)1104 pr_read_sigact(prnode_t *pnp, uio_t *uiop, cred_t *cr)
1105 {
1106 	int nsig = PROC_IS_BRANDED(curproc)? BROP(curproc)->b_nsig : NSIG;
1107 	proc_t *p;
1108 	struct sigaction *sap;
1109 	int sig;
1110 	int error;
1111 	user_t *up;
1112 
1113 	ASSERT(pnp->pr_type == PR_SIGACT);
1114 
1115 	/*
1116 	 * We kmem_alloc() the sigaction array because
1117 	 * it is so big it might blow the kernel stack.
1118 	 */
1119 	sap = kmem_alloc((nsig-1) * sizeof (struct sigaction), KM_SLEEP);
1120 
1121 	if ((error = prlock(pnp, ZNO)) != 0)
1122 		goto out;
1123 	p = pnp->pr_common->prc_proc;
1124 	ASSERT(p != NULL);
1125 
1126 	if (uiop->uio_offset >= (nsig-1)*sizeof (struct sigaction)) {
1127 		prunlock(pnp);
1128 		goto out;
1129 	}
1130 
1131 	up = PTOU(p);
1132 	for (sig = 1; sig < nsig; sig++)
1133 		prgetaction(p, up, sig, &sap[sig-1]);
1134 	prunlock(pnp);
1135 
1136 	error = pr_uioread(sap, (nsig - 1) * sizeof (struct sigaction), uiop);
1137 out:
1138 	kmem_free(sap, (nsig-1) * sizeof (struct sigaction));
1139 	return (error);
1140 }
1141 
1142 static int
pr_read_auxv(prnode_t * pnp,uio_t * uiop,cred_t * cr)1143 pr_read_auxv(prnode_t *pnp, uio_t *uiop, cred_t *cr)
1144 {
1145 	auxv_t auxv[__KERN_NAUXV_IMPL];
1146 	proc_t *p;
1147 	user_t *up;
1148 	int error;
1149 
1150 	ASSERT(pnp->pr_type == PR_AUXV);
1151 
1152 	if ((error = prlock(pnp, ZNO)) != 0)
1153 		return (error);
1154 
1155 	if (uiop->uio_offset >= sizeof (auxv)) {
1156 		prunlock(pnp);
1157 		return (0);
1158 	}
1159 
1160 	p = pnp->pr_common->prc_proc;
1161 	up = PTOU(p);
1162 	bcopy(up->u_auxv, auxv, sizeof (auxv));
1163 	prunlock(pnp);
1164 
1165 	return (pr_uioread(auxv, sizeof (auxv), uiop));
1166 }
1167 
1168 #if defined(__x86)
1169 /*
1170  * XX64
1171  *	This is almost certainly broken for the amd64 kernel, because
1172  *	we have two kinds of LDT structures to export -- one for compatibility
1173  *	mode, and one for long mode, sigh.
1174  *
1175  *	For now let's just have a ldt of size 0 for 64-bit processes.
1176  */
1177 static int
pr_read_ldt(prnode_t * pnp,uio_t * uiop,cred_t * cr)1178 pr_read_ldt(prnode_t *pnp, uio_t *uiop, cred_t *cr)
1179 {
1180 	proc_t *p;
1181 	struct ssd *ssd;
1182 	size_t size;
1183 	int error;
1184 
1185 	ASSERT(pnp->pr_type == PR_LDT);
1186 
1187 	if ((error = prlock(pnp, ZNO)) != 0)
1188 		return (error);
1189 	p = pnp->pr_common->prc_proc;
1190 
1191 	mutex_exit(&p->p_lock);
1192 	mutex_enter(&p->p_ldtlock);
1193 	size = prnldt(p) * sizeof (struct ssd);
1194 	if (uiop->uio_offset >= size) {
1195 		mutex_exit(&p->p_ldtlock);
1196 		mutex_enter(&p->p_lock);
1197 		prunlock(pnp);
1198 		return (0);
1199 	}
1200 
1201 	ssd = kmem_alloc(size, KM_SLEEP);
1202 	prgetldt(p, ssd);
1203 	mutex_exit(&p->p_ldtlock);
1204 	mutex_enter(&p->p_lock);
1205 	prunlock(pnp);
1206 
1207 	error = pr_uioread(ssd, size, uiop);
1208 	kmem_free(ssd, size);
1209 	return (error);
1210 }
1211 #endif	/* __x86 */
1212 
1213 static int
pr_read_usage(prnode_t * pnp,uio_t * uiop,cred_t * cr)1214 pr_read_usage(prnode_t *pnp, uio_t *uiop, cred_t *cr)
1215 {
1216 	prhusage_t *pup;
1217 	prusage_t *upup;
1218 	proc_t *p;
1219 	kthread_t *t;
1220 	int error;
1221 
1222 	ASSERT(pnp->pr_type == PR_USAGE);
1223 
1224 	/* allocate now, before locking the process */
1225 	pup = kmem_zalloc(sizeof (*pup), KM_SLEEP);
1226 	upup = kmem_alloc(sizeof (*upup), KM_SLEEP);
1227 
1228 	/*
1229 	 * We don't want the full treatment of prlock(pnp) here.
1230 	 * This file is world-readable and never goes invalid.
1231 	 * It doesn't matter if we are in the middle of an exec().
1232 	 */
1233 	p = pr_p_lock(pnp);
1234 	mutex_exit(&pr_pidlock);
1235 	if (p == NULL) {
1236 		error = ENOENT;
1237 		goto out;
1238 	}
1239 	ASSERT(p == pnp->pr_common->prc_proc);
1240 
1241 	if (uiop->uio_offset >= sizeof (prusage_t)) {
1242 		prunlock(pnp);
1243 		error = 0;
1244 		goto out;
1245 	}
1246 
1247 	pup->pr_tstamp = gethrtime();
1248 
1249 	pup->pr_count  = p->p_defunct;
1250 	pup->pr_create = p->p_mstart;
1251 	pup->pr_term   = p->p_mterm;
1252 
1253 	pup->pr_rtime    = p->p_mlreal;
1254 	pup->pr_utime    = p->p_acct[LMS_USER];
1255 	pup->pr_stime    = p->p_acct[LMS_SYSTEM];
1256 	pup->pr_ttime    = p->p_acct[LMS_TRAP];
1257 	pup->pr_tftime   = p->p_acct[LMS_TFAULT];
1258 	pup->pr_dftime   = p->p_acct[LMS_DFAULT];
1259 	pup->pr_kftime   = p->p_acct[LMS_KFAULT];
1260 	pup->pr_ltime    = p->p_acct[LMS_USER_LOCK];
1261 	pup->pr_slptime  = p->p_acct[LMS_SLEEP];
1262 	pup->pr_wtime    = p->p_acct[LMS_WAIT_CPU];
1263 	pup->pr_stoptime = p->p_acct[LMS_STOPPED];
1264 
1265 	pup->pr_minf  = p->p_ru.minflt;
1266 	pup->pr_majf  = p->p_ru.majflt;
1267 	pup->pr_nswap = p->p_ru.nswap;
1268 	pup->pr_inblk = p->p_ru.inblock;
1269 	pup->pr_oublk = p->p_ru.oublock;
1270 	pup->pr_msnd  = p->p_ru.msgsnd;
1271 	pup->pr_mrcv  = p->p_ru.msgrcv;
1272 	pup->pr_sigs  = p->p_ru.nsignals;
1273 	pup->pr_vctx  = p->p_ru.nvcsw;
1274 	pup->pr_ictx  = p->p_ru.nivcsw;
1275 	pup->pr_sysc  = p->p_ru.sysc;
1276 	pup->pr_ioch  = p->p_ru.ioch;
1277 
1278 	/*
1279 	 * Add the usage information for each active lwp.
1280 	 */
1281 	if ((t = p->p_tlist) != NULL &&
1282 	    !(pnp->pr_pcommon->prc_flags & PRC_DESTROY)) {
1283 		do {
1284 			if (t->t_proc_flag & TP_LWPEXIT)
1285 				continue;
1286 			pup->pr_count++;
1287 			praddusage(t, pup);
1288 		} while ((t = t->t_forw) != p->p_tlist);
1289 	}
1290 
1291 	prunlock(pnp);
1292 
1293 	prcvtusage(pup, upup);
1294 
1295 	error = pr_uioread(upup, sizeof (prusage_t), uiop);
1296 out:
1297 	kmem_free(pup, sizeof (*pup));
1298 	kmem_free(upup, sizeof (*upup));
1299 	return (error);
1300 }
1301 
1302 static int
pr_read_lusage(prnode_t * pnp,uio_t * uiop,cred_t * cr)1303 pr_read_lusage(prnode_t *pnp, uio_t *uiop, cred_t *cr)
1304 {
1305 	int nlwp;
1306 	prhusage_t *pup;
1307 	prheader_t *php;
1308 	prusage_t *upup;
1309 	size_t size;
1310 	hrtime_t curtime;
1311 	proc_t *p;
1312 	kthread_t *t;
1313 	lwpdir_t *ldp;
1314 	int error;
1315 	int i;
1316 
1317 	ASSERT(pnp->pr_type == PR_LUSAGE);
1318 
1319 	/*
1320 	 * We don't want the full treatment of prlock(pnp) here.
1321 	 * This file is world-readable and never goes invalid.
1322 	 * It doesn't matter if we are in the middle of an exec().
1323 	 */
1324 	p = pr_p_lock(pnp);
1325 	mutex_exit(&pr_pidlock);
1326 	if (p == NULL)
1327 		return (ENOENT);
1328 	ASSERT(p == pnp->pr_common->prc_proc);
1329 	if ((nlwp = p->p_lwpcnt) == 0) {
1330 		prunlock(pnp);
1331 		return (ENOENT);
1332 	}
1333 
1334 	size = sizeof (prheader_t) + (nlwp + 1) * LSPAN(prusage_t);
1335 	if (uiop->uio_offset >= size) {
1336 		prunlock(pnp);
1337 		return (0);
1338 	}
1339 
1340 	/* drop p->p_lock to do kmem_alloc(KM_SLEEP) */
1341 	mutex_exit(&p->p_lock);
1342 	pup = kmem_zalloc(size + sizeof (prhusage_t), KM_SLEEP);
1343 	mutex_enter(&p->p_lock);
1344 	/* p->p_lwpcnt can't change while process is locked */
1345 	ASSERT(nlwp == p->p_lwpcnt);
1346 
1347 	php = (prheader_t *)(pup + 1);
1348 	upup = (prusage_t *)(php + 1);
1349 
1350 	php->pr_nent = nlwp + 1;
1351 	php->pr_entsize = LSPAN(prusage_t);
1352 
1353 	curtime = gethrtime();
1354 
1355 	/*
1356 	 * First the summation over defunct lwps.
1357 	 */
1358 	pup->pr_count  = p->p_defunct;
1359 	pup->pr_tstamp = curtime;
1360 	pup->pr_create = p->p_mstart;
1361 	pup->pr_term   = p->p_mterm;
1362 
1363 	pup->pr_rtime    = p->p_mlreal;
1364 	pup->pr_utime    = p->p_acct[LMS_USER];
1365 	pup->pr_stime    = p->p_acct[LMS_SYSTEM];
1366 	pup->pr_ttime    = p->p_acct[LMS_TRAP];
1367 	pup->pr_tftime   = p->p_acct[LMS_TFAULT];
1368 	pup->pr_dftime   = p->p_acct[LMS_DFAULT];
1369 	pup->pr_kftime   = p->p_acct[LMS_KFAULT];
1370 	pup->pr_ltime    = p->p_acct[LMS_USER_LOCK];
1371 	pup->pr_slptime  = p->p_acct[LMS_SLEEP];
1372 	pup->pr_wtime    = p->p_acct[LMS_WAIT_CPU];
1373 	pup->pr_stoptime = p->p_acct[LMS_STOPPED];
1374 
1375 	pup->pr_minf  = p->p_ru.minflt;
1376 	pup->pr_majf  = p->p_ru.majflt;
1377 	pup->pr_nswap = p->p_ru.nswap;
1378 	pup->pr_inblk = p->p_ru.inblock;
1379 	pup->pr_oublk = p->p_ru.oublock;
1380 	pup->pr_msnd  = p->p_ru.msgsnd;
1381 	pup->pr_mrcv  = p->p_ru.msgrcv;
1382 	pup->pr_sigs  = p->p_ru.nsignals;
1383 	pup->pr_vctx  = p->p_ru.nvcsw;
1384 	pup->pr_ictx  = p->p_ru.nivcsw;
1385 	pup->pr_sysc  = p->p_ru.sysc;
1386 	pup->pr_ioch  = p->p_ru.ioch;
1387 
1388 	prcvtusage(pup, upup);
1389 
1390 	/*
1391 	 * Fill one prusage struct for each active lwp.
1392 	 */
1393 	for (ldp = p->p_lwpdir, i = 0; i < p->p_lwpdir_sz; i++, ldp++) {
1394 		if (ldp->ld_entry == NULL ||
1395 		    (t = ldp->ld_entry->le_thread) == NULL)
1396 			continue;
1397 		ASSERT(!(t->t_proc_flag & TP_LWPEXIT));
1398 		ASSERT(nlwp > 0);
1399 		--nlwp;
1400 		upup = (prusage_t *)((caddr_t)upup + LSPAN(prusage_t));
1401 		prgetusage(t, pup);
1402 		prcvtusage(pup, upup);
1403 	}
1404 	ASSERT(nlwp == 0);
1405 
1406 	prunlock(pnp);
1407 
1408 	error = pr_uioread(php, size, uiop);
1409 	kmem_free(pup, size + sizeof (prhusage_t));
1410 	return (error);
1411 }
1412 
1413 static int
pr_read_pagedata(prnode_t * pnp,uio_t * uiop,cred_t * cr)1414 pr_read_pagedata(prnode_t *pnp, uio_t *uiop, cred_t *cr)
1415 {
1416 	proc_t *p;
1417 	int error;
1418 
1419 	ASSERT(pnp->pr_type == PR_PAGEDATA);
1420 
1421 	if ((error = prlock(pnp, ZNO)) != 0)
1422 		return (error);
1423 
1424 	p = pnp->pr_common->prc_proc;
1425 	if ((p->p_flag & SSYS) || p->p_as == &kas) {
1426 		prunlock(pnp);
1427 		return (0);
1428 	}
1429 
1430 	mutex_exit(&p->p_lock);
1431 	error = prpdread(p, pnp->pr_hatid, uiop);
1432 	mutex_enter(&p->p_lock);
1433 
1434 	prunlock(pnp);
1435 	return (error);
1436 }
1437 
1438 static int
pr_read_opagedata(prnode_t * pnp,uio_t * uiop,cred_t * cr)1439 pr_read_opagedata(prnode_t *pnp, uio_t *uiop, cred_t *cr)
1440 {
1441 	proc_t *p;
1442 	struct as *as;
1443 	int error;
1444 
1445 	ASSERT(pnp->pr_type == PR_OPAGEDATA);
1446 
1447 	if ((error = prlock(pnp, ZNO)) != 0)
1448 		return (error);
1449 
1450 	p = pnp->pr_common->prc_proc;
1451 	as = p->p_as;
1452 	if ((p->p_flag & SSYS) || as == &kas) {
1453 		prunlock(pnp);
1454 		return (0);
1455 	}
1456 
1457 	mutex_exit(&p->p_lock);
1458 	error = oprpdread(as, pnp->pr_hatid, uiop);
1459 	mutex_enter(&p->p_lock);
1460 
1461 	prunlock(pnp);
1462 	return (error);
1463 }
1464 
1465 static int
pr_read_watch(prnode_t * pnp,uio_t * uiop,cred_t * cr)1466 pr_read_watch(prnode_t *pnp, uio_t *uiop, cred_t *cr)
1467 {
1468 	proc_t *p;
1469 	int error;
1470 	prwatch_t *Bpwp;
1471 	size_t size;
1472 	prwatch_t *pwp;
1473 	int nwarea;
1474 	struct watched_area *pwarea;
1475 
1476 	ASSERT(pnp->pr_type == PR_WATCH);
1477 
1478 	if ((error = prlock(pnp, ZNO)) != 0)
1479 		return (error);
1480 
1481 	p = pnp->pr_common->prc_proc;
1482 	nwarea = avl_numnodes(&p->p_warea);
1483 	size = nwarea * sizeof (prwatch_t);
1484 	if (uiop->uio_offset >= size) {
1485 		prunlock(pnp);
1486 		return (0);
1487 	}
1488 
1489 	/* drop p->p_lock to do kmem_alloc(KM_SLEEP) */
1490 	mutex_exit(&p->p_lock);
1491 	Bpwp = pwp = kmem_zalloc(size, KM_SLEEP);
1492 	mutex_enter(&p->p_lock);
1493 	/* p->p_nwarea can't change while process is locked */
1494 	ASSERT(nwarea == avl_numnodes(&p->p_warea));
1495 
1496 	/* gather the watched areas */
1497 	for (pwarea = avl_first(&p->p_warea); pwarea != NULL;
1498 	    pwarea = AVL_NEXT(&p->p_warea, pwarea), pwp++) {
1499 		pwp->pr_vaddr = (uintptr_t)pwarea->wa_vaddr;
1500 		pwp->pr_size = pwarea->wa_eaddr - pwarea->wa_vaddr;
1501 		pwp->pr_wflags = (int)pwarea->wa_flags;
1502 	}
1503 
1504 	prunlock(pnp);
1505 
1506 	error = pr_uioread(Bpwp, size, uiop);
1507 	kmem_free(Bpwp, size);
1508 	return (error);
1509 }
1510 
1511 static int
pr_read_lwpstatus(prnode_t * pnp,uio_t * uiop,cred_t * cr)1512 pr_read_lwpstatus(prnode_t *pnp, uio_t *uiop, cred_t *cr)
1513 {
1514 	lwpstatus_t *sp;
1515 	int error;
1516 
1517 	ASSERT(pnp->pr_type == PR_LWPSTATUS);
1518 
1519 	/*
1520 	 * We kmem_alloc() the lwpstatus structure because
1521 	 * it is so big it might blow the kernel stack.
1522 	 */
1523 	sp = kmem_alloc(sizeof (*sp), KM_SLEEP);
1524 
1525 	if ((error = prlock(pnp, ZNO)) != 0)
1526 		goto out;
1527 
1528 	if (uiop->uio_offset >= sizeof (*sp)) {
1529 		prunlock(pnp);
1530 		goto out;
1531 	}
1532 
1533 	prgetlwpstatus(pnp->pr_common->prc_thread, sp, VTOZONE(PTOV(pnp)));
1534 	prunlock(pnp);
1535 
1536 	error = pr_uioread(sp, sizeof (*sp), uiop);
1537 out:
1538 	kmem_free(sp, sizeof (*sp));
1539 	return (error);
1540 }
1541 
1542 static int
pr_read_lwpsinfo(prnode_t * pnp,uio_t * uiop,cred_t * cr)1543 pr_read_lwpsinfo(prnode_t *pnp, uio_t *uiop, cred_t *cr)
1544 {
1545 	lwpsinfo_t lwpsinfo;
1546 	proc_t *p;
1547 	kthread_t *t;
1548 	lwpent_t *lep;
1549 
1550 	ASSERT(pnp->pr_type == PR_LWPSINFO);
1551 
1552 	/*
1553 	 * We don't want the full treatment of prlock(pnp) here.
1554 	 * This file is world-readable and never goes invalid.
1555 	 * It doesn't matter if we are in the middle of an exec().
1556 	 */
1557 	p = pr_p_lock(pnp);
1558 	mutex_exit(&pr_pidlock);
1559 	if (p == NULL)
1560 		return (ENOENT);
1561 	ASSERT(p == pnp->pr_common->prc_proc);
1562 	if (pnp->pr_common->prc_tslot == -1) {
1563 		prunlock(pnp);
1564 		return (ENOENT);
1565 	}
1566 
1567 	if (uiop->uio_offset >= sizeof (lwpsinfo)) {
1568 		prunlock(pnp);
1569 		return (0);
1570 	}
1571 
1572 	if ((t = pnp->pr_common->prc_thread) != NULL)
1573 		prgetlwpsinfo(t, &lwpsinfo);
1574 	else {
1575 		lep = p->p_lwpdir[pnp->pr_common->prc_tslot].ld_entry;
1576 		bzero(&lwpsinfo, sizeof (lwpsinfo));
1577 		lwpsinfo.pr_lwpid = lep->le_lwpid;
1578 		lwpsinfo.pr_state = SZOMB;
1579 		lwpsinfo.pr_sname = 'Z';
1580 		lwpsinfo.pr_start.tv_sec = lep->le_start;
1581 		lwpsinfo.pr_bindpro = PBIND_NONE;
1582 		lwpsinfo.pr_bindpset = PS_NONE;
1583 	}
1584 	prunlock(pnp);
1585 
1586 	return (pr_uioread(&lwpsinfo, sizeof (lwpsinfo), uiop));
1587 }
1588 
1589 static int
pr_read_lwpusage(prnode_t * pnp,uio_t * uiop,cred_t * cr)1590 pr_read_lwpusage(prnode_t *pnp, uio_t *uiop, cred_t *cr)
1591 {
1592 	prhusage_t *pup;
1593 	prusage_t *upup;
1594 	proc_t *p;
1595 	int error;
1596 
1597 	ASSERT(pnp->pr_type == PR_LWPUSAGE);
1598 
1599 	/* allocate now, before locking the process */
1600 	pup = kmem_zalloc(sizeof (*pup), KM_SLEEP);
1601 	upup = kmem_alloc(sizeof (*upup), KM_SLEEP);
1602 
1603 	/*
1604 	 * We don't want the full treatment of prlock(pnp) here.
1605 	 * This file is world-readable and never goes invalid.
1606 	 * It doesn't matter if we are in the middle of an exec().
1607 	 */
1608 	p = pr_p_lock(pnp);
1609 	mutex_exit(&pr_pidlock);
1610 	if (p == NULL) {
1611 		error = ENOENT;
1612 		goto out;
1613 	}
1614 	ASSERT(p == pnp->pr_common->prc_proc);
1615 	if (pnp->pr_common->prc_thread == NULL) {
1616 		prunlock(pnp);
1617 		error = ENOENT;
1618 		goto out;
1619 	}
1620 	if (uiop->uio_offset >= sizeof (prusage_t)) {
1621 		prunlock(pnp);
1622 		error = 0;
1623 		goto out;
1624 	}
1625 
1626 	pup->pr_tstamp = gethrtime();
1627 	prgetusage(pnp->pr_common->prc_thread, pup);
1628 
1629 	prunlock(pnp);
1630 
1631 	prcvtusage(pup, upup);
1632 
1633 	error = pr_uioread(upup, sizeof (prusage_t), uiop);
1634 out:
1635 	kmem_free(pup, sizeof (*pup));
1636 	kmem_free(upup, sizeof (*upup));
1637 	return (error);
1638 }
1639 
1640 static int
pr_read_lwpname(prnode_t * pnp,uio_t * uiop,cred_t * cr)1641 pr_read_lwpname(prnode_t *pnp, uio_t *uiop, cred_t *cr)
1642 {
1643 	char lwpname[THREAD_NAME_MAX];
1644 	kthread_t *t;
1645 	int error;
1646 
1647 	ASSERT(pnp->pr_type == PR_LWPNAME);
1648 
1649 	if (uiop->uio_offset >= THREAD_NAME_MAX)
1650 		return (0);
1651 
1652 	if ((error = prlock(pnp, ZNO)) != 0)
1653 		return (error);
1654 
1655 	bzero(lwpname, sizeof (lwpname));
1656 
1657 	t = pnp->pr_common->prc_thread;
1658 
1659 	if (t->t_name != NULL)
1660 		(void) strlcpy(lwpname, t->t_name, sizeof (lwpname));
1661 
1662 	prunlock(pnp);
1663 
1664 	return (pr_uioread(lwpname, sizeof (lwpname), uiop));
1665 }
1666 
1667 /* ARGSUSED */
1668 static int
pr_read_xregs(prnode_t * pnp,uio_t * uiop,cred_t * cr)1669 pr_read_xregs(prnode_t *pnp, uio_t *uiop, cred_t *cr)
1670 {
1671 #if defined(__sparc)
1672 	proc_t *p;
1673 	kthread_t *t;
1674 	int error;
1675 	char *xreg;
1676 	size_t size;
1677 
1678 	ASSERT(pnp->pr_type == PR_XREGS);
1679 
1680 	xreg = kmem_zalloc(sizeof (prxregset_t), KM_SLEEP);
1681 
1682 	if ((error = prlock(pnp, ZNO)) != 0)
1683 		goto out;
1684 
1685 	p = pnp->pr_common->prc_proc;
1686 	t = pnp->pr_common->prc_thread;
1687 
1688 	size = prhasx(p)? prgetprxregsize(p) : 0;
1689 	if (uiop->uio_offset >= size) {
1690 		prunlock(pnp);
1691 		goto out;
1692 	}
1693 
1694 	/* drop p->p_lock while (possibly) touching the stack */
1695 	mutex_exit(&p->p_lock);
1696 	prgetprxregs(ttolwp(t), xreg);
1697 	mutex_enter(&p->p_lock);
1698 	prunlock(pnp);
1699 
1700 	error = pr_uioread(xreg, size, uiop);
1701 out:
1702 	kmem_free(xreg, sizeof (prxregset_t));
1703 	return (error);
1704 #else
1705 	return (0);
1706 #endif
1707 }
1708 
1709 static int
pr_read_spymaster(prnode_t * pnp,uio_t * uiop,cred_t * cr)1710 pr_read_spymaster(prnode_t *pnp, uio_t *uiop, cred_t *cr)
1711 {
1712 	psinfo_t psinfo;
1713 	int error;
1714 	klwp_t *lwp;
1715 
1716 	ASSERT(pnp->pr_type == PR_SPYMASTER);
1717 
1718 	if ((error = prlock(pnp, ZNO)) != 0)
1719 		return (error);
1720 
1721 	if (pnp->pr_common->prc_thread == NULL) {
1722 		prunlock(pnp);
1723 		return (0);
1724 	}
1725 
1726 	lwp = pnp->pr_common->prc_thread->t_lwp;
1727 
1728 	if (lwp->lwp_spymaster == NULL) {
1729 		prunlock(pnp);
1730 		return (0);
1731 	}
1732 
1733 	bcopy(lwp->lwp_spymaster, &psinfo, sizeof (psinfo_t));
1734 	prunlock(pnp);
1735 
1736 	return (pr_uioread(&psinfo, sizeof (psinfo), uiop));
1737 }
1738 
1739 static int
pr_read_secflags(prnode_t * pnp,uio_t * uiop,cred_t * cr)1740 pr_read_secflags(prnode_t *pnp, uio_t *uiop, cred_t *cr)
1741 {
1742 	prsecflags_t ret;
1743 	int error;
1744 	proc_t *p;
1745 
1746 	ASSERT(pnp->pr_type == PR_SECFLAGS);
1747 
1748 	if ((error = prlock(pnp, ZNO)) != 0)
1749 		return (error);
1750 
1751 	p = pnp->pr_common->prc_proc;
1752 	prgetsecflags(p, &ret);
1753 	prunlock(pnp);
1754 
1755 	return (pr_uioread(&ret, sizeof (ret), uiop));
1756 }
1757 
1758 #if defined(__sparc)
1759 
1760 static int
pr_read_gwindows(prnode_t * pnp,uio_t * uiop,cred_t * cr)1761 pr_read_gwindows(prnode_t *pnp, uio_t *uiop, cred_t *cr)
1762 {
1763 	proc_t *p;
1764 	kthread_t *t;
1765 	gwindows_t *gwp;
1766 	int error;
1767 	size_t size;
1768 
1769 	ASSERT(pnp->pr_type == PR_GWINDOWS);
1770 
1771 	gwp = kmem_zalloc(sizeof (gwindows_t), KM_SLEEP);
1772 
1773 	if ((error = prlock(pnp, ZNO)) != 0)
1774 		goto out;
1775 
1776 	p = pnp->pr_common->prc_proc;
1777 	t = pnp->pr_common->prc_thread;
1778 
1779 	/*
1780 	 * Drop p->p_lock while touching the stack.
1781 	 * The P_PR_LOCK flag prevents the lwp from
1782 	 * disappearing while we do this.
1783 	 */
1784 	mutex_exit(&p->p_lock);
1785 	if ((size = prnwindows(ttolwp(t))) != 0)
1786 		size = sizeof (gwindows_t) -
1787 		    (SPARC_MAXREGWINDOW - size) * sizeof (struct rwindow);
1788 	if (uiop->uio_offset >= size) {
1789 		mutex_enter(&p->p_lock);
1790 		prunlock(pnp);
1791 		goto out;
1792 	}
1793 	prgetwindows(ttolwp(t), gwp);
1794 	mutex_enter(&p->p_lock);
1795 	prunlock(pnp);
1796 
1797 	error = pr_uioread(gwp, size, uiop);
1798 out:
1799 	kmem_free(gwp, sizeof (gwindows_t));
1800 	return (error);
1801 }
1802 
1803 /* ARGSUSED */
1804 static int
pr_read_asrs(prnode_t * pnp,uio_t * uiop,cred_t * cr)1805 pr_read_asrs(prnode_t *pnp, uio_t *uiop, cred_t *cr)
1806 {
1807 	int error;
1808 
1809 	ASSERT(pnp->pr_type == PR_ASRS);
1810 
1811 	/* the asrs file exists only for sparc v9 _LP64 processes */
1812 	if ((error = prlock(pnp, ZNO)) == 0) {
1813 		proc_t *p = pnp->pr_common->prc_proc;
1814 		kthread_t *t = pnp->pr_common->prc_thread;
1815 		asrset_t asrset;
1816 
1817 		if (p->p_model != DATAMODEL_LP64 ||
1818 		    uiop->uio_offset >= sizeof (asrset_t)) {
1819 			prunlock(pnp);
1820 			return (0);
1821 		}
1822 
1823 		/*
1824 		 * Drop p->p_lock while touching the stack.
1825 		 * The P_PR_LOCK flag prevents the lwp from
1826 		 * disappearing while we do this.
1827 		 */
1828 		mutex_exit(&p->p_lock);
1829 		prgetasregs(ttolwp(t), asrset);
1830 		mutex_enter(&p->p_lock);
1831 		prunlock(pnp);
1832 
1833 		error = pr_uioread(&asrset[0], sizeof (asrset_t), uiop);
1834 	}
1835 
1836 	return (error);
1837 }
1838 
1839 #endif	/* __sparc */
1840 
1841 static int
pr_read_piddir(prnode_t * pnp,uio_t * uiop,cred_t * cr)1842 pr_read_piddir(prnode_t *pnp, uio_t *uiop, cred_t *cr)
1843 {
1844 	ASSERT(pnp->pr_type == PR_PIDDIR);
1845 	ASSERT(pnp->pr_pidfile != NULL);
1846 
1847 	/* use the underlying PR_PIDFILE to read the process */
1848 	pnp = VTOP(pnp->pr_pidfile);
1849 	ASSERT(pnp->pr_type == PR_PIDFILE);
1850 
1851 	return (pr_read_pidfile(pnp, uiop));
1852 }
1853 
1854 static int
pr_read_pidfile(prnode_t * pnp,uio_t * uiop,cred_t * cr)1855 pr_read_pidfile(prnode_t *pnp, uio_t *uiop, cred_t *cr)
1856 {
1857 	int error;
1858 
1859 	ASSERT(pnp->pr_type == PR_PIDFILE || pnp->pr_type == PR_LWPIDFILE);
1860 
1861 	if ((error = prlock(pnp, ZNO)) == 0) {
1862 		proc_t *p = pnp->pr_common->prc_proc;
1863 		struct as *as = p->p_as;
1864 
1865 		if ((p->p_flag & SSYS) || as == &kas) {
1866 			/*
1867 			 * /proc I/O cannot be done to a system process.
1868 			 */
1869 			error = EIO;	/* old /proc semantics */
1870 		} else {
1871 			/*
1872 			 * We drop p_lock because we don't want to hold
1873 			 * it over an I/O operation because that could
1874 			 * lead to deadlock with the clock thread.
1875 			 * The process will not disappear and its address
1876 			 * space will not change because it is marked P_PR_LOCK.
1877 			 */
1878 			mutex_exit(&p->p_lock);
1879 			error = prusrio(p, UIO_READ, uiop, 1);
1880 			mutex_enter(&p->p_lock);
1881 		}
1882 		prunlock(pnp);
1883 	}
1884 
1885 	return (error);
1886 }
1887 
1888 #ifdef _SYSCALL32_IMPL
1889 
1890 /*
1891  * Array of ILP32 read functions, indexed by /proc file type.
1892  */
1893 static int pr_read_status_32(),
1894 	pr_read_lstatus_32(), pr_read_psinfo_32(), pr_read_lpsinfo_32(),
1895 	pr_read_map_32(), pr_read_rmap_32(), pr_read_xmap_32(),
1896 	pr_read_sigact_32(), pr_read_auxv_32(),
1897 	pr_read_usage_32(), pr_read_lusage_32(), pr_read_pagedata_32(),
1898 	pr_read_watch_32(), pr_read_lwpstatus_32(), pr_read_lwpsinfo_32(),
1899 	pr_read_lwpusage_32(), pr_read_spymaster_32(),
1900 #if defined(__sparc)
1901 	pr_read_gwindows_32(),
1902 #endif
1903 	pr_read_opagedata_32();
1904 
1905 static int (*pr_read_function_32[PR_NFILES])() = {
1906 	pr_read_inval,		/* /proc				*/
1907 	pr_read_inval,		/* /proc/self				*/
1908 	pr_read_piddir,		/* /proc/<pid> (old /proc read())	*/
1909 	pr_read_as,		/* /proc/<pid>/as			*/
1910 	pr_read_inval,		/* /proc/<pid>/ctl			*/
1911 	pr_read_status_32,	/* /proc/<pid>/status			*/
1912 	pr_read_lstatus_32,	/* /proc/<pid>/lstatus			*/
1913 	pr_read_psinfo_32,	/* /proc/<pid>/psinfo			*/
1914 	pr_read_lpsinfo_32,	/* /proc/<pid>/lpsinfo			*/
1915 	pr_read_map_32,		/* /proc/<pid>/map			*/
1916 	pr_read_rmap_32,	/* /proc/<pid>/rmap			*/
1917 	pr_read_xmap_32,	/* /proc/<pid>/xmap			*/
1918 	pr_read_cred,		/* /proc/<pid>/cred			*/
1919 	pr_read_sigact_32,	/* /proc/<pid>/sigact			*/
1920 	pr_read_auxv_32,	/* /proc/<pid>/auxv			*/
1921 #if defined(__x86)
1922 	pr_read_ldt,		/* /proc/<pid>/ldt			*/
1923 #endif
1924 	pr_read_usage_32,	/* /proc/<pid>/usage			*/
1925 	pr_read_lusage_32,	/* /proc/<pid>/lusage			*/
1926 	pr_read_pagedata_32,	/* /proc/<pid>/pagedata			*/
1927 	pr_read_watch_32,	/* /proc/<pid>/watch			*/
1928 	pr_read_inval,		/* /proc/<pid>/cwd			*/
1929 	pr_read_inval,		/* /proc/<pid>/root			*/
1930 	pr_read_inval,		/* /proc/<pid>/fd			*/
1931 	pr_read_inval,		/* /proc/<pid>/fd/nn			*/
1932 	pr_read_inval,		/* /proc/<pid>/fdinfo			*/
1933 	pr_read_fdinfo,		/* /proc/<pid>/fdinfo/nn		*/
1934 	pr_read_inval,		/* /proc/<pid>/object			*/
1935 	pr_read_inval,		/* /proc/<pid>/object/xxx		*/
1936 	pr_read_inval,		/* /proc/<pid>/lwp			*/
1937 	pr_read_inval,		/* /proc/<pid>/lwp/<lwpid>		*/
1938 	pr_read_inval,		/* /proc/<pid>/lwp/<lwpid>/lwpctl	*/
1939 	pr_read_lwpname,	/* /proc/<pid>/lwp/<lwpid>/lwpname	*/
1940 	pr_read_lwpstatus_32,	/* /proc/<pid>/lwp/<lwpid>/lwpstatus	*/
1941 	pr_read_lwpsinfo_32,	/* /proc/<pid>/lwp/<lwpid>/lwpsinfo	*/
1942 	pr_read_lwpusage_32,	/* /proc/<pid>/lwp/<lwpid>/lwpusage	*/
1943 	pr_read_xregs,		/* /proc/<pid>/lwp/<lwpid>/xregs	*/
1944 	pr_read_inval,		/* /proc/<pid>/lwp/<lwpid>/templates	*/
1945 	pr_read_inval,		/* /proc/<pid>/lwp/<lwpid>/templates/<id> */
1946 	pr_read_spymaster_32,	/* /proc/<pid>/lwp/<lwpid>/spymaster	*/
1947 #if defined(__sparc)
1948 	pr_read_gwindows_32,	/* /proc/<pid>/lwp/<lwpid>/gwindows	*/
1949 	pr_read_asrs,		/* /proc/<pid>/lwp/<lwpid>/asrs		*/
1950 #endif
1951 	pr_read_priv,		/* /proc/<pid>/priv			*/
1952 	pr_read_inval,		/* /proc/<pid>/path			*/
1953 	pr_read_inval,		/* /proc/<pid>/path/xxx			*/
1954 	pr_read_inval,		/* /proc/<pid>/contracts		*/
1955 	pr_read_inval,		/* /proc/<pid>/contracts/<ctid>		*/
1956 	pr_read_secflags,	/* /proc/<pid>/secflags			*/
1957 	pr_read_pidfile,	/* old process file			*/
1958 	pr_read_pidfile,	/* old lwp file				*/
1959 	pr_read_opagedata_32,	/* old pagedata file			*/
1960 };
1961 
1962 static int
pr_read_status_32(prnode_t * pnp,uio_t * uiop,cred_t * cr)1963 pr_read_status_32(prnode_t *pnp, uio_t *uiop, cred_t *cr)
1964 {
1965 	pstatus32_t *sp;
1966 	proc_t *p;
1967 	int error;
1968 
1969 	ASSERT(pnp->pr_type == PR_STATUS);
1970 
1971 	/*
1972 	 * We kmem_alloc() the pstatus structure because
1973 	 * it is so big it might blow the kernel stack.
1974 	 */
1975 	sp = kmem_alloc(sizeof (*sp), KM_SLEEP);
1976 	if ((error = prlock(pnp, ZNO)) == 0) {
1977 		/*
1978 		 * A 32-bit process cannot get the status of a 64-bit process.
1979 		 * The fields for the 64-bit quantities are not large enough.
1980 		 */
1981 		p = pnp->pr_common->prc_proc;
1982 		if (PROCESS_NOT_32BIT(p)) {
1983 			prunlock(pnp);
1984 			error = EOVERFLOW;
1985 		} else {
1986 			prgetstatus32(pnp->pr_common->prc_proc, sp,
1987 			    VTOZONE(PTOV(pnp)));
1988 			prunlock(pnp);
1989 			error = pr_uioread(sp, sizeof (*sp), uiop);
1990 		}
1991 	}
1992 	kmem_free((caddr_t)sp, sizeof (*sp));
1993 	return (error);
1994 }
1995 
1996 static int
pr_read_lstatus_32(prnode_t * pnp,uio_t * uiop,cred_t * cr)1997 pr_read_lstatus_32(prnode_t *pnp, uio_t *uiop, cred_t *cr)
1998 {
1999 	proc_t *p;
2000 	kthread_t *t;
2001 	lwpdir_t *ldp;
2002 	size_t size;
2003 	prheader32_t *php;
2004 	lwpstatus32_t *sp;
2005 	int error;
2006 	int nlwp;
2007 	int i;
2008 
2009 	ASSERT(pnp->pr_type == PR_LSTATUS);
2010 
2011 	if ((error = prlock(pnp, ZNO)) != 0)
2012 		return (error);
2013 	p = pnp->pr_common->prc_proc;
2014 	/*
2015 	 * A 32-bit process cannot get the status of a 64-bit process.
2016 	 * The fields for the 64-bit quantities are not large enough.
2017 	 */
2018 	if (PROCESS_NOT_32BIT(p)) {
2019 		prunlock(pnp);
2020 		return (EOVERFLOW);
2021 	}
2022 	nlwp = p->p_lwpcnt;
2023 	size = sizeof (prheader32_t) + nlwp * LSPAN32(lwpstatus32_t);
2024 
2025 	/* drop p->p_lock to do kmem_alloc(KM_SLEEP) */
2026 	mutex_exit(&p->p_lock);
2027 	php = kmem_zalloc(size, KM_SLEEP);
2028 	mutex_enter(&p->p_lock);
2029 	/* p->p_lwpcnt can't change while process is locked */
2030 	ASSERT(nlwp == p->p_lwpcnt);
2031 
2032 	php->pr_nent = nlwp;
2033 	php->pr_entsize = LSPAN32(lwpstatus32_t);
2034 
2035 	sp = (lwpstatus32_t *)(php + 1);
2036 	for (ldp = p->p_lwpdir, i = 0; i < p->p_lwpdir_sz; i++, ldp++) {
2037 		if (ldp->ld_entry == NULL ||
2038 		    (t = ldp->ld_entry->le_thread) == NULL)
2039 			continue;
2040 		prgetlwpstatus32(t, sp, VTOZONE(PTOV(pnp)));
2041 		sp = (lwpstatus32_t *)((caddr_t)sp + LSPAN32(lwpstatus32_t));
2042 	}
2043 	prunlock(pnp);
2044 
2045 	error = pr_uioread(php, size, uiop);
2046 	kmem_free(php, size);
2047 	return (error);
2048 }
2049 
2050 static int
pr_read_psinfo_32(prnode_t * pnp,uio_t * uiop,cred_t * cr)2051 pr_read_psinfo_32(prnode_t *pnp, uio_t *uiop, cred_t *cr)
2052 {
2053 	psinfo32_t psinfo;
2054 	proc_t *p;
2055 	int error = 0;
2056 
2057 	ASSERT(pnp->pr_type == PR_PSINFO);
2058 
2059 	/*
2060 	 * We don't want the full treatment of prlock(pnp) here.
2061 	 * This file is world-readable and never goes invalid.
2062 	 * It doesn't matter if we are in the middle of an exec().
2063 	 */
2064 	p = pr_p_lock(pnp);
2065 	mutex_exit(&pr_pidlock);
2066 	if (p == NULL)
2067 		error = ENOENT;
2068 	else {
2069 		ASSERT(p == pnp->pr_common->prc_proc);
2070 		prgetpsinfo32(p, &psinfo);
2071 		prunlock(pnp);
2072 		error = pr_uioread(&psinfo, sizeof (psinfo), uiop);
2073 	}
2074 	return (error);
2075 }
2076 
2077 static int
pr_read_lpsinfo_32(prnode_t * pnp,uio_t * uiop,cred_t * cr)2078 pr_read_lpsinfo_32(prnode_t *pnp, uio_t *uiop, cred_t *cr)
2079 {
2080 	proc_t *p;
2081 	kthread_t *t;
2082 	lwpdir_t *ldp;
2083 	lwpent_t *lep;
2084 	size_t size;
2085 	prheader32_t *php;
2086 	lwpsinfo32_t *sp;
2087 	int error;
2088 	int nlwp;
2089 	int i;
2090 
2091 	ASSERT(pnp->pr_type == PR_LPSINFO);
2092 
2093 	/*
2094 	 * We don't want the full treatment of prlock(pnp) here.
2095 	 * This file is world-readable and never goes invalid.
2096 	 * It doesn't matter if we are in the middle of an exec().
2097 	 */
2098 	p = pr_p_lock(pnp);
2099 	mutex_exit(&pr_pidlock);
2100 	if (p == NULL)
2101 		return (ENOENT);
2102 	ASSERT(p == pnp->pr_common->prc_proc);
2103 	if ((nlwp = p->p_lwpcnt + p->p_zombcnt) == 0) {
2104 		prunlock(pnp);
2105 		return (ENOENT);
2106 	}
2107 	size = sizeof (prheader32_t) + nlwp * LSPAN32(lwpsinfo32_t);
2108 
2109 	/* drop p->p_lock to do kmem_alloc(KM_SLEEP) */
2110 	mutex_exit(&p->p_lock);
2111 	php = kmem_zalloc(size, KM_SLEEP);
2112 	mutex_enter(&p->p_lock);
2113 	/* p->p_lwpcnt can't change while process is locked */
2114 	ASSERT(nlwp == p->p_lwpcnt + p->p_zombcnt);
2115 
2116 	php->pr_nent = nlwp;
2117 	php->pr_entsize = LSPAN32(lwpsinfo32_t);
2118 
2119 	sp = (lwpsinfo32_t *)(php + 1);
2120 	for (ldp = p->p_lwpdir, i = 0; i < p->p_lwpdir_sz; i++, ldp++) {
2121 		if ((lep = ldp->ld_entry) == NULL)
2122 			continue;
2123 		if ((t = lep->le_thread) != NULL)
2124 			prgetlwpsinfo32(t, sp);
2125 		else {
2126 			bzero(sp, sizeof (*sp));
2127 			sp->pr_lwpid = lep->le_lwpid;
2128 			sp->pr_state = SZOMB;
2129 			sp->pr_sname = 'Z';
2130 			sp->pr_start.tv_sec = (time32_t)lep->le_start;
2131 		}
2132 		sp = (lwpsinfo32_t *)((caddr_t)sp + LSPAN32(lwpsinfo32_t));
2133 	}
2134 	prunlock(pnp);
2135 
2136 	error = pr_uioread(php, size, uiop);
2137 	kmem_free(php, size);
2138 	return (error);
2139 }
2140 
2141 static int
pr_read_map_common_32(prnode_t * pnp,uio_t * uiop,prnodetype_t type)2142 pr_read_map_common_32(prnode_t *pnp, uio_t *uiop, prnodetype_t type)
2143 {
2144 	proc_t *p;
2145 	struct as *as;
2146 	list_t	iolhead;
2147 	int error;
2148 
2149 readmap32_common:
2150 	if ((error = prlock(pnp, ZNO)) != 0)
2151 		return (error);
2152 
2153 	p = pnp->pr_common->prc_proc;
2154 	as = p->p_as;
2155 
2156 	if ((p->p_flag & SSYS) || as == &kas) {
2157 		prunlock(pnp);
2158 		return (0);
2159 	}
2160 
2161 	if (PROCESS_NOT_32BIT(p)) {
2162 		prunlock(pnp);
2163 		return (EOVERFLOW);
2164 	}
2165 
2166 	if (!AS_LOCK_TRYENTER(as, RW_WRITER)) {
2167 		prunlock(pnp);
2168 		delay(1);
2169 		goto readmap32_common;
2170 	}
2171 	mutex_exit(&p->p_lock);
2172 
2173 	switch (type) {
2174 	case PR_XMAP:
2175 		error = prgetxmap32(p, &iolhead);
2176 		break;
2177 	case PR_RMAP:
2178 		error = prgetmap32(p, 1, &iolhead);
2179 		break;
2180 	case PR_MAP:
2181 		error = prgetmap32(p, 0, &iolhead);
2182 		break;
2183 	}
2184 	AS_LOCK_EXIT(as);
2185 	mutex_enter(&p->p_lock);
2186 	prunlock(pnp);
2187 
2188 	error = pr_iol_uiomove_and_free(&iolhead, uiop, error);
2189 
2190 	return (error);
2191 }
2192 
2193 static int
pr_read_map_32(prnode_t * pnp,uio_t * uiop,cred_t * cr)2194 pr_read_map_32(prnode_t *pnp, uio_t *uiop, cred_t *cr)
2195 {
2196 	ASSERT(pnp->pr_type == PR_MAP);
2197 	return (pr_read_map_common_32(pnp, uiop, pnp->pr_type));
2198 }
2199 
2200 static int
pr_read_rmap_32(prnode_t * pnp,uio_t * uiop,cred_t * cr)2201 pr_read_rmap_32(prnode_t *pnp, uio_t *uiop, cred_t *cr)
2202 {
2203 	ASSERT(pnp->pr_type == PR_RMAP);
2204 	return (pr_read_map_common_32(pnp, uiop, pnp->pr_type));
2205 }
2206 
2207 static int
pr_read_xmap_32(prnode_t * pnp,uio_t * uiop,cred_t * cr)2208 pr_read_xmap_32(prnode_t *pnp, uio_t *uiop, cred_t *cr)
2209 {
2210 	ASSERT(pnp->pr_type == PR_XMAP);
2211 	return (pr_read_map_common_32(pnp, uiop, pnp->pr_type));
2212 }
2213 
2214 static int
pr_read_sigact_32(prnode_t * pnp,uio_t * uiop,cred_t * cr)2215 pr_read_sigact_32(prnode_t *pnp, uio_t *uiop, cred_t *cr)
2216 {
2217 	int nsig = PROC_IS_BRANDED(curproc)? BROP(curproc)->b_nsig : NSIG;
2218 	proc_t *p;
2219 	struct sigaction32 *sap;
2220 	int sig;
2221 	int error;
2222 	user_t *up;
2223 
2224 	ASSERT(pnp->pr_type == PR_SIGACT);
2225 
2226 	/*
2227 	 * We kmem_alloc() the sigaction32 array because
2228 	 * it is so big it might blow the kernel stack.
2229 	 */
2230 	sap = kmem_alloc((nsig-1) * sizeof (struct sigaction32), KM_SLEEP);
2231 
2232 	if ((error = prlock(pnp, ZNO)) != 0)
2233 		goto out;
2234 	p = pnp->pr_common->prc_proc;
2235 
2236 	if (PROCESS_NOT_32BIT(p)) {
2237 		prunlock(pnp);
2238 		error = EOVERFLOW;
2239 		goto out;
2240 	}
2241 
2242 	if (uiop->uio_offset >= (nsig-1) * sizeof (struct sigaction32)) {
2243 		prunlock(pnp);
2244 		goto out;
2245 	}
2246 
2247 	up = PTOU(p);
2248 	for (sig = 1; sig < nsig; sig++)
2249 		prgetaction32(p, up, sig, &sap[sig-1]);
2250 	prunlock(pnp);
2251 
2252 	error = pr_uioread(sap, (nsig - 1) * sizeof (struct sigaction32), uiop);
2253 out:
2254 	kmem_free(sap, (nsig-1) * sizeof (struct sigaction32));
2255 	return (error);
2256 }
2257 
2258 static int
pr_read_auxv_32(prnode_t * pnp,uio_t * uiop,cred_t * cr)2259 pr_read_auxv_32(prnode_t *pnp, uio_t *uiop, cred_t *cr)
2260 {
2261 	auxv32_t auxv[__KERN_NAUXV_IMPL];
2262 	proc_t *p;
2263 	user_t *up;
2264 	int error;
2265 	int i;
2266 
2267 	ASSERT(pnp->pr_type == PR_AUXV);
2268 
2269 	if ((error = prlock(pnp, ZNO)) != 0)
2270 		return (error);
2271 	p = pnp->pr_common->prc_proc;
2272 
2273 	if (PROCESS_NOT_32BIT(p)) {
2274 		prunlock(pnp);
2275 		return (EOVERFLOW);
2276 	}
2277 
2278 	if (uiop->uio_offset >= sizeof (auxv)) {
2279 		prunlock(pnp);
2280 		return (0);
2281 	}
2282 
2283 	up = PTOU(p);
2284 	for (i = 0; i < __KERN_NAUXV_IMPL; i++) {
2285 		auxv[i].a_type = (int32_t)up->u_auxv[i].a_type;
2286 		auxv[i].a_un.a_val = (int32_t)up->u_auxv[i].a_un.a_val;
2287 	}
2288 	prunlock(pnp);
2289 
2290 	return (pr_uioread(auxv, sizeof (auxv), uiop));
2291 }
2292 
2293 static int
pr_read_usage_32(prnode_t * pnp,uio_t * uiop,cred_t * cr)2294 pr_read_usage_32(prnode_t *pnp, uio_t *uiop, cred_t *cr)
2295 {
2296 	prhusage_t *pup;
2297 	prusage32_t *upup;
2298 	proc_t *p;
2299 	kthread_t *t;
2300 	int error;
2301 
2302 	ASSERT(pnp->pr_type == PR_USAGE);
2303 
2304 	/* allocate now, before locking the process */
2305 	pup = kmem_zalloc(sizeof (*pup), KM_SLEEP);
2306 	upup = kmem_alloc(sizeof (*upup), KM_SLEEP);
2307 
2308 	/*
2309 	 * We don't want the full treatment of prlock(pnp) here.
2310 	 * This file is world-readable and never goes invalid.
2311 	 * It doesn't matter if we are in the middle of an exec().
2312 	 */
2313 	p = pr_p_lock(pnp);
2314 	mutex_exit(&pr_pidlock);
2315 	if (p == NULL) {
2316 		error = ENOENT;
2317 		goto out;
2318 	}
2319 	ASSERT(p == pnp->pr_common->prc_proc);
2320 
2321 	if (uiop->uio_offset >= sizeof (prusage32_t)) {
2322 		prunlock(pnp);
2323 		error = 0;
2324 		goto out;
2325 	}
2326 
2327 	pup->pr_tstamp = gethrtime();
2328 
2329 	pup->pr_count  = p->p_defunct;
2330 	pup->pr_create = p->p_mstart;
2331 	pup->pr_term   = p->p_mterm;
2332 
2333 	pup->pr_rtime    = p->p_mlreal;
2334 	pup->pr_utime    = p->p_acct[LMS_USER];
2335 	pup->pr_stime    = p->p_acct[LMS_SYSTEM];
2336 	pup->pr_ttime    = p->p_acct[LMS_TRAP];
2337 	pup->pr_tftime   = p->p_acct[LMS_TFAULT];
2338 	pup->pr_dftime   = p->p_acct[LMS_DFAULT];
2339 	pup->pr_kftime   = p->p_acct[LMS_KFAULT];
2340 	pup->pr_ltime    = p->p_acct[LMS_USER_LOCK];
2341 	pup->pr_slptime  = p->p_acct[LMS_SLEEP];
2342 	pup->pr_wtime    = p->p_acct[LMS_WAIT_CPU];
2343 	pup->pr_stoptime = p->p_acct[LMS_STOPPED];
2344 
2345 	pup->pr_minf  = p->p_ru.minflt;
2346 	pup->pr_majf  = p->p_ru.majflt;
2347 	pup->pr_nswap = p->p_ru.nswap;
2348 	pup->pr_inblk = p->p_ru.inblock;
2349 	pup->pr_oublk = p->p_ru.oublock;
2350 	pup->pr_msnd  = p->p_ru.msgsnd;
2351 	pup->pr_mrcv  = p->p_ru.msgrcv;
2352 	pup->pr_sigs  = p->p_ru.nsignals;
2353 	pup->pr_vctx  = p->p_ru.nvcsw;
2354 	pup->pr_ictx  = p->p_ru.nivcsw;
2355 	pup->pr_sysc  = p->p_ru.sysc;
2356 	pup->pr_ioch  = p->p_ru.ioch;
2357 
2358 	/*
2359 	 * Add the usage information for each active lwp.
2360 	 */
2361 	if ((t = p->p_tlist) != NULL &&
2362 	    !(pnp->pr_pcommon->prc_flags & PRC_DESTROY)) {
2363 		do {
2364 			if (t->t_proc_flag & TP_LWPEXIT)
2365 				continue;
2366 			pup->pr_count++;
2367 			praddusage(t, pup);
2368 		} while ((t = t->t_forw) != p->p_tlist);
2369 	}
2370 
2371 	prunlock(pnp);
2372 
2373 	prcvtusage32(pup, upup);
2374 
2375 	error = pr_uioread(upup, sizeof (prusage32_t), uiop);
2376 out:
2377 	kmem_free(pup, sizeof (*pup));
2378 	kmem_free(upup, sizeof (*upup));
2379 	return (error);
2380 }
2381 
2382 static int
pr_read_lusage_32(prnode_t * pnp,uio_t * uiop,cred_t * cr)2383 pr_read_lusage_32(prnode_t *pnp, uio_t *uiop, cred_t *cr)
2384 {
2385 	int nlwp;
2386 	prhusage_t *pup;
2387 	prheader32_t *php;
2388 	prusage32_t *upup;
2389 	size_t size;
2390 	hrtime_t curtime;
2391 	proc_t *p;
2392 	kthread_t *t;
2393 	lwpdir_t *ldp;
2394 	int error;
2395 	int i;
2396 
2397 	ASSERT(pnp->pr_type == PR_LUSAGE);
2398 
2399 	/*
2400 	 * We don't want the full treatment of prlock(pnp) here.
2401 	 * This file is world-readable and never goes invalid.
2402 	 * It doesn't matter if we are in the middle of an exec().
2403 	 */
2404 	p = pr_p_lock(pnp);
2405 	mutex_exit(&pr_pidlock);
2406 	if (p == NULL)
2407 		return (ENOENT);
2408 	ASSERT(p == pnp->pr_common->prc_proc);
2409 	if ((nlwp = p->p_lwpcnt) == 0) {
2410 		prunlock(pnp);
2411 		return (ENOENT);
2412 	}
2413 
2414 	size = sizeof (prheader32_t) + (nlwp + 1) * LSPAN32(prusage32_t);
2415 	if (uiop->uio_offset >= size) {
2416 		prunlock(pnp);
2417 		return (0);
2418 	}
2419 
2420 	/* drop p->p_lock to do kmem_alloc(KM_SLEEP) */
2421 	mutex_exit(&p->p_lock);
2422 	pup = kmem_zalloc(size + sizeof (prhusage_t), KM_SLEEP);
2423 	mutex_enter(&p->p_lock);
2424 	/* p->p_lwpcnt can't change while process is locked */
2425 	ASSERT(nlwp == p->p_lwpcnt);
2426 
2427 	php = (prheader32_t *)(pup + 1);
2428 	upup = (prusage32_t *)(php + 1);
2429 
2430 	php->pr_nent = nlwp + 1;
2431 	php->pr_entsize = LSPAN32(prusage32_t);
2432 
2433 	curtime = gethrtime();
2434 
2435 	/*
2436 	 * First the summation over defunct lwps.
2437 	 */
2438 	pup->pr_count  = p->p_defunct;
2439 	pup->pr_tstamp = curtime;
2440 	pup->pr_create = p->p_mstart;
2441 	pup->pr_term   = p->p_mterm;
2442 
2443 	pup->pr_rtime    = p->p_mlreal;
2444 	pup->pr_utime    = p->p_acct[LMS_USER];
2445 	pup->pr_stime    = p->p_acct[LMS_SYSTEM];
2446 	pup->pr_ttime    = p->p_acct[LMS_TRAP];
2447 	pup->pr_tftime   = p->p_acct[LMS_TFAULT];
2448 	pup->pr_dftime   = p->p_acct[LMS_DFAULT];
2449 	pup->pr_kftime   = p->p_acct[LMS_KFAULT];
2450 	pup->pr_ltime    = p->p_acct[LMS_USER_LOCK];
2451 	pup->pr_slptime  = p->p_acct[LMS_SLEEP];
2452 	pup->pr_wtime    = p->p_acct[LMS_WAIT_CPU];
2453 	pup->pr_stoptime = p->p_acct[LMS_STOPPED];
2454 
2455 	pup->pr_minf  = p->p_ru.minflt;
2456 	pup->pr_majf  = p->p_ru.majflt;
2457 	pup->pr_nswap = p->p_ru.nswap;
2458 	pup->pr_inblk = p->p_ru.inblock;
2459 	pup->pr_oublk = p->p_ru.oublock;
2460 	pup->pr_msnd  = p->p_ru.msgsnd;
2461 	pup->pr_mrcv  = p->p_ru.msgrcv;
2462 	pup->pr_sigs  = p->p_ru.nsignals;
2463 	pup->pr_vctx  = p->p_ru.nvcsw;
2464 	pup->pr_ictx  = p->p_ru.nivcsw;
2465 	pup->pr_sysc  = p->p_ru.sysc;
2466 	pup->pr_ioch  = p->p_ru.ioch;
2467 
2468 	prcvtusage32(pup, upup);
2469 
2470 	/*
2471 	 * Fill one prusage struct for each active lwp.
2472 	 */
2473 	for (ldp = p->p_lwpdir, i = 0; i < p->p_lwpdir_sz; i++, ldp++) {
2474 		if (ldp->ld_entry == NULL ||
2475 		    (t = ldp->ld_entry->le_thread) == NULL)
2476 			continue;
2477 		ASSERT(!(t->t_proc_flag & TP_LWPEXIT));
2478 		ASSERT(nlwp > 0);
2479 		--nlwp;
2480 		upup = (prusage32_t *)
2481 		    ((caddr_t)upup + LSPAN32(prusage32_t));
2482 		prgetusage(t, pup);
2483 		prcvtusage32(pup, upup);
2484 	}
2485 	ASSERT(nlwp == 0);
2486 
2487 	prunlock(pnp);
2488 
2489 	error = pr_uioread(php, size, uiop);
2490 	kmem_free(pup, size + sizeof (prhusage_t));
2491 	return (error);
2492 }
2493 
2494 static int
pr_read_pagedata_32(prnode_t * pnp,uio_t * uiop,cred_t * cr)2495 pr_read_pagedata_32(prnode_t *pnp, uio_t *uiop, cred_t *cr)
2496 {
2497 	proc_t *p;
2498 	int error;
2499 
2500 	ASSERT(pnp->pr_type == PR_PAGEDATA);
2501 
2502 	if ((error = prlock(pnp, ZNO)) != 0)
2503 		return (error);
2504 
2505 	p = pnp->pr_common->prc_proc;
2506 	if ((p->p_flag & SSYS) || p->p_as == &kas) {
2507 		prunlock(pnp);
2508 		return (0);
2509 	}
2510 
2511 	if (PROCESS_NOT_32BIT(p)) {
2512 		prunlock(pnp);
2513 		return (EOVERFLOW);
2514 	}
2515 
2516 	mutex_exit(&p->p_lock);
2517 	error = prpdread32(p, pnp->pr_hatid, uiop);
2518 	mutex_enter(&p->p_lock);
2519 
2520 	prunlock(pnp);
2521 	return (error);
2522 }
2523 
2524 static int
pr_read_opagedata_32(prnode_t * pnp,uio_t * uiop,cred_t * cr)2525 pr_read_opagedata_32(prnode_t *pnp, uio_t *uiop, cred_t *cr)
2526 {
2527 	proc_t *p;
2528 	struct as *as;
2529 	int error;
2530 
2531 	ASSERT(pnp->pr_type == PR_OPAGEDATA);
2532 
2533 	if ((error = prlock(pnp, ZNO)) != 0)
2534 		return (error);
2535 
2536 	p = pnp->pr_common->prc_proc;
2537 	as = p->p_as;
2538 
2539 	if ((p->p_flag & SSYS) || as == &kas) {
2540 		prunlock(pnp);
2541 		return (0);
2542 	}
2543 
2544 	if (PROCESS_NOT_32BIT(p)) {
2545 		prunlock(pnp);
2546 		return (EOVERFLOW);
2547 	}
2548 
2549 	mutex_exit(&p->p_lock);
2550 	error = oprpdread32(as, pnp->pr_hatid, uiop);
2551 	mutex_enter(&p->p_lock);
2552 
2553 	prunlock(pnp);
2554 	return (error);
2555 }
2556 
2557 static int
pr_read_watch_32(prnode_t * pnp,uio_t * uiop,cred_t * cr)2558 pr_read_watch_32(prnode_t *pnp, uio_t *uiop, cred_t *cr)
2559 {
2560 	proc_t *p;
2561 	int error;
2562 	prwatch32_t *Bpwp;
2563 	size_t size;
2564 	prwatch32_t *pwp;
2565 	int nwarea;
2566 	struct watched_area *pwarea;
2567 
2568 	ASSERT(pnp->pr_type == PR_WATCH);
2569 
2570 	if ((error = prlock(pnp, ZNO)) != 0)
2571 		return (error);
2572 
2573 	p = pnp->pr_common->prc_proc;
2574 	if (PROCESS_NOT_32BIT(p)) {
2575 		prunlock(pnp);
2576 		return (EOVERFLOW);
2577 	}
2578 	nwarea = avl_numnodes(&p->p_warea);
2579 	size = nwarea * sizeof (prwatch32_t);
2580 	if (uiop->uio_offset >= size) {
2581 		prunlock(pnp);
2582 		return (0);
2583 	}
2584 
2585 	/* drop p->p_lock to do kmem_alloc(KM_SLEEP) */
2586 	mutex_exit(&p->p_lock);
2587 	Bpwp = pwp = kmem_zalloc(size, KM_SLEEP);
2588 	mutex_enter(&p->p_lock);
2589 	/* p->p_nwarea can't change while process is locked */
2590 	ASSERT(nwarea == avl_numnodes(&p->p_warea));
2591 
2592 	/* gather the watched areas */
2593 	for (pwarea = avl_first(&p->p_warea); pwarea != NULL;
2594 	    pwarea = AVL_NEXT(&p->p_warea, pwarea), pwp++) {
2595 		pwp->pr_vaddr = (caddr32_t)(uintptr_t)pwarea->wa_vaddr;
2596 		pwp->pr_size = (size32_t)(pwarea->wa_eaddr - pwarea->wa_vaddr);
2597 		pwp->pr_wflags = (int)pwarea->wa_flags;
2598 	}
2599 
2600 	prunlock(pnp);
2601 
2602 	error = pr_uioread(Bpwp, size, uiop);
2603 	kmem_free(Bpwp, size);
2604 	return (error);
2605 }
2606 
2607 static int
pr_read_lwpstatus_32(prnode_t * pnp,uio_t * uiop,cred_t * cr)2608 pr_read_lwpstatus_32(prnode_t *pnp, uio_t *uiop, cred_t *cr)
2609 {
2610 	lwpstatus32_t *sp;
2611 	proc_t *p;
2612 	int error;
2613 
2614 	ASSERT(pnp->pr_type == PR_LWPSTATUS);
2615 
2616 	/*
2617 	 * We kmem_alloc() the lwpstatus structure because
2618 	 * it is so big it might blow the kernel stack.
2619 	 */
2620 	sp = kmem_alloc(sizeof (*sp), KM_SLEEP);
2621 
2622 	if ((error = prlock(pnp, ZNO)) != 0)
2623 		goto out;
2624 
2625 	/*
2626 	 * A 32-bit process cannot get the status of a 64-bit process.
2627 	 * The fields for the 64-bit quantities are not large enough.
2628 	 */
2629 	p = pnp->pr_common->prc_proc;
2630 	if (PROCESS_NOT_32BIT(p)) {
2631 		prunlock(pnp);
2632 		error = EOVERFLOW;
2633 		goto out;
2634 	}
2635 
2636 	if (uiop->uio_offset >= sizeof (*sp)) {
2637 		prunlock(pnp);
2638 		goto out;
2639 	}
2640 
2641 	prgetlwpstatus32(pnp->pr_common->prc_thread, sp, VTOZONE(PTOV(pnp)));
2642 	prunlock(pnp);
2643 
2644 	error = pr_uioread(sp, sizeof (*sp), uiop);
2645 out:
2646 	kmem_free(sp, sizeof (*sp));
2647 	return (error);
2648 }
2649 
2650 static int
pr_read_lwpsinfo_32(prnode_t * pnp,uio_t * uiop,cred_t * cr)2651 pr_read_lwpsinfo_32(prnode_t *pnp, uio_t *uiop, cred_t *cr)
2652 {
2653 	lwpsinfo32_t lwpsinfo;
2654 	proc_t *p;
2655 	kthread_t *t;
2656 	lwpent_t *lep;
2657 
2658 	ASSERT(pnp->pr_type == PR_LWPSINFO);
2659 
2660 	/*
2661 	 * We don't want the full treatment of prlock(pnp) here.
2662 	 * This file is world-readable and never goes invalid.
2663 	 * It doesn't matter if we are in the middle of an exec().
2664 	 */
2665 	p = pr_p_lock(pnp);
2666 	mutex_exit(&pr_pidlock);
2667 	if (p == NULL)
2668 		return (ENOENT);
2669 	ASSERT(p == pnp->pr_common->prc_proc);
2670 	if (pnp->pr_common->prc_tslot == -1) {
2671 		prunlock(pnp);
2672 		return (ENOENT);
2673 	}
2674 
2675 	if (uiop->uio_offset >= sizeof (lwpsinfo)) {
2676 		prunlock(pnp);
2677 		return (0);
2678 	}
2679 
2680 	if ((t = pnp->pr_common->prc_thread) != NULL)
2681 		prgetlwpsinfo32(t, &lwpsinfo);
2682 	else {
2683 		lep = p->p_lwpdir[pnp->pr_common->prc_tslot].ld_entry;
2684 		bzero(&lwpsinfo, sizeof (lwpsinfo));
2685 		lwpsinfo.pr_lwpid = lep->le_lwpid;
2686 		lwpsinfo.pr_state = SZOMB;
2687 		lwpsinfo.pr_sname = 'Z';
2688 		lwpsinfo.pr_start.tv_sec = (time32_t)lep->le_start;
2689 	}
2690 	prunlock(pnp);
2691 
2692 	return (pr_uioread(&lwpsinfo, sizeof (lwpsinfo), uiop));
2693 }
2694 
2695 static int
pr_read_lwpusage_32(prnode_t * pnp,uio_t * uiop,cred_t * cr)2696 pr_read_lwpusage_32(prnode_t *pnp, uio_t *uiop, cred_t *cr)
2697 {
2698 	prhusage_t *pup;
2699 	prusage32_t *upup;
2700 	proc_t *p;
2701 	int error;
2702 
2703 	ASSERT(pnp->pr_type == PR_LWPUSAGE);
2704 
2705 	/* allocate now, before locking the process */
2706 	pup = kmem_zalloc(sizeof (*pup), KM_SLEEP);
2707 	upup = kmem_alloc(sizeof (*upup), KM_SLEEP);
2708 
2709 	/*
2710 	 * We don't want the full treatment of prlock(pnp) here.
2711 	 * This file is world-readable and never goes invalid.
2712 	 * It doesn't matter if we are in the middle of an exec().
2713 	 */
2714 	p = pr_p_lock(pnp);
2715 	mutex_exit(&pr_pidlock);
2716 	if (p == NULL) {
2717 		error = ENOENT;
2718 		goto out;
2719 	}
2720 	ASSERT(p == pnp->pr_common->prc_proc);
2721 	if (pnp->pr_common->prc_thread == NULL) {
2722 		prunlock(pnp);
2723 		error = ENOENT;
2724 		goto out;
2725 	}
2726 	if (uiop->uio_offset >= sizeof (prusage32_t)) {
2727 		prunlock(pnp);
2728 		error = 0;
2729 		goto out;
2730 	}
2731 
2732 	pup->pr_tstamp = gethrtime();
2733 	prgetusage(pnp->pr_common->prc_thread, pup);
2734 
2735 	prunlock(pnp);
2736 
2737 	prcvtusage32(pup, upup);
2738 
2739 	error = pr_uioread(upup, sizeof (prusage32_t), uiop);
2740 out:
2741 	kmem_free(pup, sizeof (*pup));
2742 	kmem_free(upup, sizeof (*upup));
2743 	return (error);
2744 }
2745 
2746 static int
pr_read_spymaster_32(prnode_t * pnp,uio_t * uiop,cred_t * cr)2747 pr_read_spymaster_32(prnode_t *pnp, uio_t *uiop, cred_t *cr)
2748 {
2749 	psinfo32_t psinfo;
2750 	int error;
2751 	klwp_t *lwp;
2752 
2753 	ASSERT(pnp->pr_type == PR_SPYMASTER);
2754 
2755 	if ((error = prlock(pnp, ZNO)) != 0)
2756 		return (error);
2757 
2758 	if (pnp->pr_common->prc_thread == NULL) {
2759 		prunlock(pnp);
2760 		return (0);
2761 	}
2762 
2763 	lwp = pnp->pr_common->prc_thread->t_lwp;
2764 
2765 	if (lwp->lwp_spymaster == NULL) {
2766 		prunlock(pnp);
2767 		return (0);
2768 	}
2769 
2770 	psinfo_kto32(lwp->lwp_spymaster, &psinfo);
2771 	prunlock(pnp);
2772 
2773 	return (pr_uioread(&psinfo, sizeof (psinfo), uiop));
2774 }
2775 
2776 #if defined(__sparc)
2777 static int
pr_read_gwindows_32(prnode_t * pnp,uio_t * uiop,cred_t * cr)2778 pr_read_gwindows_32(prnode_t *pnp, uio_t *uiop, cred_t *cr)
2779 {
2780 	proc_t *p;
2781 	kthread_t *t;
2782 	gwindows32_t *gwp;
2783 	int error;
2784 	size_t size;
2785 
2786 	ASSERT(pnp->pr_type == PR_GWINDOWS);
2787 
2788 	gwp = kmem_zalloc(sizeof (gwindows32_t), KM_SLEEP);
2789 
2790 	if ((error = prlock(pnp, ZNO)) != 0)
2791 		goto out;
2792 
2793 	p = pnp->pr_common->prc_proc;
2794 	t = pnp->pr_common->prc_thread;
2795 
2796 	if (PROCESS_NOT_32BIT(p)) {
2797 		prunlock(pnp);
2798 		error = EOVERFLOW;
2799 		goto out;
2800 	}
2801 
2802 	/*
2803 	 * Drop p->p_lock while touching the stack.
2804 	 * The P_PR_LOCK flag prevents the lwp from
2805 	 * disappearing while we do this.
2806 	 */
2807 	mutex_exit(&p->p_lock);
2808 	if ((size = prnwindows(ttolwp(t))) != 0)
2809 		size = sizeof (gwindows32_t) -
2810 		    (SPARC_MAXREGWINDOW - size) * sizeof (struct rwindow32);
2811 	if (uiop->uio_offset >= size) {
2812 		mutex_enter(&p->p_lock);
2813 		prunlock(pnp);
2814 		goto out;
2815 	}
2816 	prgetwindows32(ttolwp(t), gwp);
2817 	mutex_enter(&p->p_lock);
2818 	prunlock(pnp);
2819 
2820 	error = pr_uioread(gwp, size, uiop);
2821 out:
2822 	kmem_free(gwp, sizeof (gwindows32_t));
2823 	return (error);
2824 }
2825 #endif	/* __sparc */
2826 
2827 #endif	/* _SYSCALL32_IMPL */
2828 
2829 /* ARGSUSED */
2830 static int
prread(vnode_t * vp,uio_t * uiop,int ioflag,cred_t * cr,caller_context_t * ct)2831 prread(vnode_t *vp, uio_t *uiop, int ioflag, cred_t *cr, caller_context_t *ct)
2832 {
2833 	prnode_t *pnp = VTOP(vp);
2834 
2835 	ASSERT(pnp->pr_type < PR_NFILES);
2836 
2837 #ifdef _SYSCALL32_IMPL
2838 	/*
2839 	 * What is read from the /proc files depends on the data
2840 	 * model of the caller.  An LP64 process will see LP64
2841 	 * data.  An ILP32 process will see ILP32 data.
2842 	 */
2843 	if (curproc->p_model == DATAMODEL_LP64)
2844 		return (pr_read_function[pnp->pr_type](pnp, uiop, cr));
2845 	else
2846 		return (pr_read_function_32[pnp->pr_type](pnp, uiop, cr));
2847 #else
2848 	return (pr_read_function[pnp->pr_type](pnp, uiop, cr));
2849 #endif
2850 }
2851 
2852 /* Note we intentionally don't handle partial writes/updates. */
2853 static int
pr_write_lwpname(prnode_t * pnp,uio_t * uiop)2854 pr_write_lwpname(prnode_t *pnp, uio_t *uiop)
2855 {
2856 	kthread_t *t = NULL;
2857 	char *lwpname;
2858 	int error;
2859 
2860 	lwpname = kmem_zalloc(THREAD_NAME_MAX, KM_SLEEP);
2861 
2862 	if ((error = uiomove(lwpname, THREAD_NAME_MAX, UIO_WRITE, uiop)) != 0) {
2863 		kmem_free(lwpname, THREAD_NAME_MAX);
2864 		return (error);
2865 	}
2866 
2867 	/* Somebody tried to write too long a thread name... */
2868 	if (lwpname[THREAD_NAME_MAX - 1] != '\0' || uiop->uio_resid > 0) {
2869 		kmem_free(lwpname, THREAD_NAME_MAX);
2870 		return (EIO);
2871 	}
2872 
2873 	VERIFY3U(lwpname[THREAD_NAME_MAX - 1], ==, '\0');
2874 
2875 	for (size_t i = 0; lwpname[i] != '\0'; i++) {
2876 		if (!ISPRINT(lwpname[i])) {
2877 			kmem_free(lwpname, THREAD_NAME_MAX);
2878 			return (EINVAL);
2879 		}
2880 	}
2881 
2882 	/* Equivalent of thread_setname(), but with the ZNO magic. */
2883 	if ((error = prlock(pnp, ZNO)) != 0) {
2884 		kmem_free(lwpname, THREAD_NAME_MAX);
2885 		return (error);
2886 	}
2887 
2888 	t = pnp->pr_common->prc_thread;
2889 	if (t->t_name == NULL) {
2890 		t->t_name = lwpname;
2891 	} else {
2892 		(void) strlcpy(t->t_name, lwpname, THREAD_NAME_MAX);
2893 		kmem_free(lwpname, THREAD_NAME_MAX);
2894 	}
2895 
2896 	prunlock(pnp);
2897 	return (0);
2898 }
2899 
2900 /* ARGSUSED */
2901 static int
prwrite(vnode_t * vp,uio_t * uiop,int ioflag,cred_t * cr,caller_context_t * ct)2902 prwrite(vnode_t *vp, uio_t *uiop, int ioflag, cred_t *cr, caller_context_t *ct)
2903 {
2904 	prnode_t *pnp = VTOP(vp);
2905 	int old = 0;
2906 	int error;
2907 	ssize_t resid;
2908 
2909 	ASSERT(pnp->pr_type < PR_NFILES);
2910 
2911 	/*
2912 	 * Only a handful of /proc files are writable, enumerate them here.
2913 	 */
2914 	switch (pnp->pr_type) {
2915 	case PR_PIDDIR:		/* directory write()s: visceral revulsion. */
2916 		ASSERT(pnp->pr_pidfile != NULL);
2917 		/* use the underlying PR_PIDFILE to write the process */
2918 		vp = pnp->pr_pidfile;
2919 		pnp = VTOP(vp);
2920 		ASSERT(pnp->pr_type == PR_PIDFILE);
2921 		/* FALLTHROUGH */
2922 	case PR_PIDFILE:
2923 	case PR_LWPIDFILE:
2924 		old = 1;
2925 		/* FALLTHROUGH */
2926 	case PR_AS:
2927 		if ((error = prlock(pnp, ZNO)) == 0) {
2928 			proc_t *p = pnp->pr_common->prc_proc;
2929 			struct as *as = p->p_as;
2930 
2931 			if ((p->p_flag & SSYS) || as == &kas) {
2932 				/*
2933 				 * /proc I/O cannot be done to a system process.
2934 				 */
2935 				error = EIO;
2936 #ifdef _SYSCALL32_IMPL
2937 			} else if (curproc->p_model == DATAMODEL_ILP32 &&
2938 			    PROCESS_NOT_32BIT(p)) {
2939 				error = EOVERFLOW;
2940 #endif
2941 			} else {
2942 				/*
2943 				 * See comments above (pr_read_pidfile)
2944 				 * about this locking dance.
2945 				 */
2946 				mutex_exit(&p->p_lock);
2947 				error = prusrio(p, UIO_WRITE, uiop, old);
2948 				mutex_enter(&p->p_lock);
2949 			}
2950 			prunlock(pnp);
2951 		}
2952 		return (error);
2953 
2954 	case PR_CTL:
2955 	case PR_LWPCTL:
2956 		resid = uiop->uio_resid;
2957 		/*
2958 		 * Perform the action on the control file
2959 		 * by passing curthreads credentials
2960 		 * and not target process's credentials.
2961 		 */
2962 #ifdef _SYSCALL32_IMPL
2963 		if (curproc->p_model == DATAMODEL_ILP32)
2964 			error = prwritectl32(vp, uiop, CRED());
2965 		else
2966 			error = prwritectl(vp, uiop, CRED());
2967 #else
2968 		error = prwritectl(vp, uiop, CRED());
2969 #endif
2970 		/*
2971 		 * This hack makes sure that the EINTR is passed
2972 		 * all the way back to the caller's write() call.
2973 		 */
2974 		if (error == EINTR)
2975 			uiop->uio_resid = resid;
2976 		return (error);
2977 
2978 	case PR_LWPNAME:
2979 		return (pr_write_lwpname(pnp, uiop));
2980 
2981 	default:
2982 		return ((vp->v_type == VDIR)? EISDIR : EBADF);
2983 	}
2984 	/* NOTREACHED */
2985 }
2986 
2987 static int
prgetattr(vnode_t * vp,vattr_t * vap,int flags,cred_t * cr,caller_context_t * ct)2988 prgetattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
2989     caller_context_t *ct)
2990 {
2991 	prnode_t *pnp = VTOP(vp);
2992 	prnodetype_t type = pnp->pr_type;
2993 	prcommon_t *pcp;
2994 	proc_t *p;
2995 	struct as *as;
2996 	int error;
2997 	vnode_t *rvp;
2998 	timestruc_t now;
2999 	extern uint_t nproc;
3000 	int ngroups;
3001 	int nsig;
3002 
3003 	/*
3004 	 * This ugly bit of code allows us to keep both versions of this
3005 	 * function from the same source.
3006 	 */
3007 #ifdef _LP64
3008 	int iam32bit = (curproc->p_model == DATAMODEL_ILP32);
3009 #define	PR_OBJSIZE(obj32, obj64)	\
3010 	(iam32bit ? sizeof (obj32) : sizeof (obj64))
3011 #define	PR_OBJSPAN(obj32, obj64)	\
3012 	(iam32bit ? LSPAN32(obj32) : LSPAN(obj64))
3013 #else
3014 #define	PR_OBJSIZE(obj32, obj64)	\
3015 	(sizeof (obj64))
3016 #define	PR_OBJSPAN(obj32, obj64)	\
3017 	(LSPAN(obj64))
3018 #endif
3019 
3020 	/*
3021 	 * Return all the attributes.  Should be refined
3022 	 * so that it returns only those asked for.
3023