]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/fs/procfs/procfs_ioctl.c
Merge libc++ trunk r321414 to contrib/libc++.
[FreeBSD/FreeBSD.git] / sys / fs / procfs / procfs_ioctl.c
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 2001 Dag-Erling Coïdan Smørgrav
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
18  *
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.
29  *
30  *      $FreeBSD$
31  */
32
33 #include "opt_compat.h"
34
35 #include <sys/param.h>
36 #include <sys/lock.h>
37 #include <sys/mutex.h>
38 #include <sys/pioctl.h>
39 #include <sys/priv.h>
40 #include <sys/proc.h>
41 #include <sys/signalvar.h>
42 #include <sys/systm.h>
43
44 #include <fs/pseudofs/pseudofs.h>
45 #include <fs/procfs/procfs.h>
46
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 */
54 };
55
56 #define PIOCWAIT32      _IOR('p', 4, struct procfs_status32)
57 #define PIOCSTATUS32    _IOR('p', 6, struct procfs_status32)
58 #endif
59
60 /*
61  * Process ioctls
62  */
63 int
64 procfs_ioctl(PFS_IOCTL_ARGS)
65 {
66         struct procfs_status *ps;
67 #ifdef COMPAT_FREEBSD32
68         struct procfs_status32 *ps32;
69 #endif
70         int error, flags, sig;
71 #ifdef COMPAT_FREEBSD6
72         int ival;
73 #endif
74
75         KASSERT(p != NULL,
76             ("%s() called without a process", __func__));
77         PROC_LOCK_ASSERT(p, MA_OWNED);
78
79         error = 0;
80         switch (cmd) {
81 #if defined(COMPAT_FREEBSD5) || defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
82         case _IOC(IOC_IN, 'p', 1, 0):
83 #endif
84 #ifdef COMPAT_FREEBSD6
85         case _IO('p', 1):
86                 ival = IOCPARM_IVAL(data);
87                 data = &ival;
88 #endif
89         case PIOCBIS:
90                 p->p_stops |= *(unsigned int *)data;
91                 break;
92 #if defined(COMPAT_FREEBSD5) || defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
93         case _IOC(IOC_IN, 'p', 2, 0):
94 #endif
95 #ifdef COMPAT_FREEBSD6
96         case _IO('p', 2):
97                 ival = IOCPARM_IVAL(data);
98                 data = &ival;
99 #endif
100         case PIOCBIC:
101                 p->p_stops &= ~*(unsigned int *)data;
102                 break;
103 #if defined(COMPAT_FREEBSD5) || defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
104         case _IOC(IOC_IN, 'p', 3, 0):
105 #endif
106 #ifdef COMPAT_FREEBSD6
107         case _IO('p', 3):
108                 ival = IOCPARM_IVAL(data);
109                 data = &ival;
110 #endif
111         case PIOCSFL:
112                 flags = *(unsigned int *)data;
113                 if (flags & PF_ISUGID) {
114                         /*
115                          * XXXRW: Is this specific check required here, as
116                          * p_candebug() should implement it, or other checks
117                          * are missing.
118                          */
119                         error = priv_check(td, PRIV_DEBUG_SUGID);
120                         if (error)
121                                 break;
122                 }
123                 p->p_pfsflags = flags;
124                 break;
125         case PIOCGFL:
126                 *(unsigned int *)data = p->p_pfsflags;
127                 break;
128         case PIOCWAIT:
129                 while (p->p_step == 0 && (p->p_flag & P_WEXIT) == 0) {
130                         /* sleep until p stops */
131                         _PHOLD(p);
132                         error = msleep(&p->p_stype, &p->p_mtx,
133                             PWAIT|PCATCH, "pioctl", 0);
134                         _PRELE(p);
135                         if (error != 0)
136                                 break;
137                 }
138                 /* fall through to PIOCSTATUS */
139         case 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;
146                 break;
147 #ifdef COMPAT_FREEBSD32
148         case PIOCWAIT32:
149                 while (p->p_step == 0 && (p->p_flag & P_WEXIT) == 0) {
150                         /* sleep until p stops */
151                         _PHOLD(p);
152                         error = msleep(&p->p_stype, &p->p_mtx,
153                             PWAIT|PCATCH, "pioctl", 0);
154                         _PRELE(p);
155                         if (error != 0)
156                                 break;
157                 }
158                 /* fall through to PIOCSTATUS32 */
159         case 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;
166                 break;
167 #endif
168 #if defined(COMPAT_FREEBSD5) || defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
169         case _IOC(IOC_IN, 'p', 5, 0):
170 #endif
171 #ifdef COMPAT_FREEBSD6
172         case _IO('p', 5):
173                 ival = IOCPARM_IVAL(data);
174                 data = &ival;
175 #endif
176         case PIOCCONT:
177                 if (p->p_step == 0)
178                         break;
179                 sig = *(unsigned int *)data;
180                 if (sig != 0 && !_SIG_VALID(sig)) {
181                         error = EINVAL;
182                         break;
183                 }
184 #if 0
185                 p->p_step = 0;
186                 if (P_SHOULDSTOP(p)) {
187                         p->p_xsig = sig;
188                         p->p_flag &= ~(P_STOPPED_TRACE|P_STOPPED_SIG);
189                         PROC_SLOCK(p);
190                         thread_unsuspend(p);
191                         PROC_SUNLOCK(p);
192                 } else if (sig)
193                         kern_psignal(p, sig);
194 #else
195                 if (sig)
196                         kern_psignal(p, sig);
197                 p->p_step = 0;
198                 wakeup(&p->p_step);
199 #endif
200                 break;
201         default:
202                 error = (ENOTTY);
203         }
204
205         return (error);
206 }
207
208 /*
209  * Clean up on last close
210  */
211 int
212 procfs_close(PFS_CLOSE_ARGS)
213 {
214         if (p != NULL && (p->p_pfsflags & PF_LINGER) == 0) {
215                 PROC_LOCK_ASSERT(p, MA_OWNED);
216                 p->p_pfsflags = 0;
217                 p->p_stops = 0;
218                 p->p_step = 0;
219                 wakeup(&p->p_step);
220         }
221         return (0);
222 }