2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2001 Dag-Erling Coïdan Smørgrav
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer
12 * in this position and unchanged.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include "opt_compat.h"
35 #include <sys/param.h>
37 #include <sys/mutex.h>
38 #include <sys/pioctl.h>
41 #include <sys/signalvar.h>
42 #include <sys/systm.h>
44 #include <fs/pseudofs/pseudofs.h>
45 #include <fs/procfs/procfs.h>
47 #ifdef COMPAT_FREEBSD32
48 struct procfs_status32 {
49 int state; /* Running, stopped, something else? */
50 int flags; /* Any flags */
51 unsigned int events; /* Events to stop on */
52 int why; /* What event, if any, proc stopped on */
53 unsigned int val; /* Any extra data */
56 #define PIOCWAIT32 _IOR('p', 4, struct procfs_status32)
57 #define PIOCSTATUS32 _IOR('p', 6, struct procfs_status32)
64 procfs_ioctl(PFS_IOCTL_ARGS)
66 struct procfs_status *ps;
67 #ifdef COMPAT_FREEBSD32
68 struct procfs_status32 *ps32;
70 int error, flags, sig;
71 #ifdef COMPAT_FREEBSD6
76 ("%s() called without a process", __func__));
77 PROC_LOCK_ASSERT(p, MA_OWNED);
81 #if defined(COMPAT_FREEBSD5) || defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
82 case _IOC(IOC_IN, 'p', 1, 0):
84 #ifdef COMPAT_FREEBSD6
86 ival = IOCPARM_IVAL(data);
90 p->p_stops |= *(unsigned int *)data;
92 #if defined(COMPAT_FREEBSD5) || defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
93 case _IOC(IOC_IN, 'p', 2, 0):
95 #ifdef COMPAT_FREEBSD6
97 ival = IOCPARM_IVAL(data);
101 p->p_stops &= ~*(unsigned int *)data;
103 #if defined(COMPAT_FREEBSD5) || defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
104 case _IOC(IOC_IN, 'p', 3, 0):
106 #ifdef COMPAT_FREEBSD6
108 ival = IOCPARM_IVAL(data);
112 flags = *(unsigned int *)data;
113 if (flags & PF_ISUGID) {
115 * XXXRW: Is this specific check required here, as
116 * p_candebug() should implement it, or other checks
119 error = priv_check(td, PRIV_DEBUG_SUGID);
123 p->p_pfsflags = flags;
126 *(unsigned int *)data = p->p_pfsflags;
129 while (p->p_step == 0 && (p->p_flag & P_WEXIT) == 0) {
130 /* sleep until p stops */
132 error = msleep(&p->p_stype, &p->p_mtx,
133 PWAIT|PCATCH, "pioctl", 0);
138 /* fall through to PIOCSTATUS */
140 ps = (struct procfs_status *)data;
141 ps->state = (p->p_step == 0);
142 ps->flags = 0; /* nope */
143 ps->events = p->p_stops;
144 ps->why = p->p_step ? p->p_stype : 0;
145 ps->val = p->p_step ? p->p_xsig : 0;
147 #ifdef COMPAT_FREEBSD32
149 while (p->p_step == 0 && (p->p_flag & P_WEXIT) == 0) {
150 /* sleep until p stops */
152 error = msleep(&p->p_stype, &p->p_mtx,
153 PWAIT|PCATCH, "pioctl", 0);
158 /* fall through to PIOCSTATUS32 */
160 ps32 = (struct procfs_status32 *)data;
161 ps32->state = (p->p_step == 0);
162 ps32->flags = 0; /* nope */
163 ps32->events = p->p_stops;
164 ps32->why = p->p_step ? p->p_stype : 0;
165 ps32->val = p->p_step ? p->p_xsig : 0;
168 #if defined(COMPAT_FREEBSD5) || defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
169 case _IOC(IOC_IN, 'p', 5, 0):
171 #ifdef COMPAT_FREEBSD6
173 ival = IOCPARM_IVAL(data);
179 sig = *(unsigned int *)data;
180 if (sig != 0 && !_SIG_VALID(sig)) {
186 if (P_SHOULDSTOP(p)) {
188 p->p_flag &= ~(P_STOPPED_TRACE|P_STOPPED_SIG);
193 kern_psignal(p, sig);
196 kern_psignal(p, sig);
209 * Clean up on last close
212 procfs_close(PFS_CLOSE_ARGS)
214 if (p != NULL && (p->p_pfsflags & PF_LINGER) == 0) {
215 PROC_LOCK_ASSERT(p, MA_OWNED);