procfs_ioctl.c revision 147676
1/*-
2 * Copyright (c) 2001 Dag-Erling Co�dan Sm�rgrav
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer
10 *    in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 *    derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 *      $FreeBSD: head/sys/fs/procfs/procfs_ioctl.c 147676 2005-06-30 00:19:08Z peter $
29 */
30
31#include "opt_compat.h"
32
33#include <sys/param.h>
34#include <sys/lock.h>
35#include <sys/mutex.h>
36#include <sys/pioctl.h>
37#include <sys/proc.h>
38#include <sys/signalvar.h>
39#include <sys/systm.h>
40
41#include <fs/pseudofs/pseudofs.h>
42#include <fs/procfs/procfs.h>
43
44/*
45 * Process ioctls
46 */
47int
48procfs_ioctl(PFS_IOCTL_ARGS)
49{
50	struct procfs_status *ps;
51	int error, flags, sig;
52
53	PROC_LOCK(p);
54	error = 0;
55	switch (cmd) {
56#if defined(COMPAT_FREEBSD5) || defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
57	case _IOC(IOC_IN, 'p', 1, 0):
58#endif
59	case PIOCBIS:
60		p->p_stops |= *(uintptr_t *)data;
61		break;
62#if defined(COMPAT_FREEBSD5) || defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
63	case _IOC(IOC_IN, 'p', 2, 0):
64#endif
65	case PIOCBIC:
66		p->p_stops &= ~*(uintptr_t *)data;
67		break;
68#if defined(COMPAT_FREEBSD5) || defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
69	case _IOC(IOC_IN, 'p', 3, 0):
70#endif
71	case PIOCSFL:
72		flags = *(uintptr_t *)data;
73		if (flags & PF_ISUGID && (error = suser(td)) != 0)
74			break;
75		p->p_pfsflags = flags;
76		break;
77	case PIOCGFL:
78		*(unsigned int *)data = p->p_pfsflags;
79		break;
80	case PIOCWAIT:
81		while (p->p_step == 0) {
82			/* sleep until p stops */
83			error = msleep(&p->p_stype, &p->p_mtx,
84			    PWAIT|PCATCH, "pioctl", 0);
85			if (error != 0)
86				break;
87		}
88		/* fall through to PIOCSTATUS */
89	case PIOCSTATUS:
90		ps = (struct procfs_status *)data;
91		ps->state = (p->p_step == 0);
92		ps->flags = 0; /* nope */
93		ps->events = p->p_stops;
94		ps->why = p->p_step ? p->p_stype : 0;
95		ps->val = p->p_step ? p->p_xstat : 0;
96		break;
97#if defined(COMPAT_FREEBSD5) || defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
98	case _IOC(IOC_IN, 'p', 5, 0):
99#endif
100	case PIOCCONT:
101		if (p->p_step == 0)
102			break;
103		sig = *(uintptr_t *)data;
104		if (sig != 0 && !_SIG_VALID(sig)) {
105			error = EINVAL;
106			break;
107		}
108#if 0
109		p->p_step = 0;
110		if (P_SHOULDSTOP(p)) {
111			p->p_xstat = sig;
112			p->p_flag &= ~(P_STOPPED_TRACE|P_STOPPED_SIG);
113			mtx_lock_spin(&sched_lock);
114			thread_unsuspend(p);
115			mtx_unlock_spin(&sched_lock);
116		} else if (sig)
117			psignal(p, sig);
118#else
119		if (sig)
120			psignal(p, sig);
121		p->p_step = 0;
122		wakeup(&p->p_step);
123#endif
124		break;
125	default:
126		error = (ENOTTY);
127	}
128	PROC_UNLOCK(p);
129
130	return (error);
131}
132
133/*
134 * Clean up on last close
135 */
136int
137procfs_close(PFS_CLOSE_ARGS)
138{
139	if (p != NULL && (p->p_pfsflags & PF_LINGER) == 0) {
140		PROC_LOCK_ASSERT(p, MA_OWNED);
141		p->p_pfsflags = 0;
142		p->p_stops = 0;
143		p->p_step = 0;
144		wakeup(&p->p_step);
145	}
146	return (0);
147}
148