]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - sys/fs/procfs/procfs_ioctl.c
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / sys / fs / procfs / procfs_ioctl.c
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$
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/priv.h>
38 #include <sys/proc.h>
39 #include <sys/signalvar.h>
40 #include <sys/systm.h>
41
42 #include <fs/pseudofs/pseudofs.h>
43 #include <fs/procfs/procfs.h>
44
45 #ifdef COMPAT_FREEBSD32
46 struct procfs_status32 {
47         int     state;  /* Running, stopped, something else? */
48         int     flags;  /* Any flags */
49         unsigned int    events; /* Events to stop on */
50         int     why;    /* What event, if any, proc stopped on */
51         unsigned int    val;    /* Any extra data */
52 };
53
54 #define PIOCWAIT32      _IOR('p', 4, struct procfs_status32)
55 #define PIOCSTATUS32    _IOR('p', 6, struct procfs_status32)
56 #endif
57
58 /*
59  * Process ioctls
60  */
61 int
62 procfs_ioctl(PFS_IOCTL_ARGS)
63 {
64         struct procfs_status *ps;
65 #ifdef COMPAT_FREEBSD32
66         struct procfs_status32 *ps32;
67 #endif
68         int error, flags, sig;
69 #ifdef COMPAT_FREEBSD6
70         int ival;
71 #endif
72
73         KASSERT(p != NULL,
74             ("%s() called without a process", __func__));
75         PROC_LOCK_ASSERT(p, MA_OWNED);
76
77         error = 0;
78         switch (cmd) {
79 #if defined(COMPAT_FREEBSD5) || defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
80         case _IOC(IOC_IN, 'p', 1, 0):
81 #endif
82 #ifdef COMPAT_FREEBSD6
83         case _IO('p', 1):
84                 ival = IOCPARM_IVAL(data);
85                 data = &ival;
86 #endif
87         case PIOCBIS:
88                 p->p_stops |= *(unsigned int *)data;
89                 break;
90 #if defined(COMPAT_FREEBSD5) || defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
91         case _IOC(IOC_IN, 'p', 2, 0):
92 #endif
93 #ifdef COMPAT_FREEBSD6
94         case _IO('p', 2):
95                 ival = IOCPARM_IVAL(data);
96                 data = &ival;
97 #endif
98         case PIOCBIC:
99                 p->p_stops &= ~*(unsigned int *)data;
100                 break;
101 #if defined(COMPAT_FREEBSD5) || defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
102         case _IOC(IOC_IN, 'p', 3, 0):
103 #endif
104 #ifdef COMPAT_FREEBSD6
105         case _IO('p', 3):
106                 ival = IOCPARM_IVAL(data);
107                 data = &ival;
108 #endif
109         case PIOCSFL:
110                 flags = *(unsigned int *)data;
111                 if (flags & PF_ISUGID) {
112                         /*
113                          * XXXRW: Is this specific check required here, as
114                          * p_candebug() should implement it, or other checks
115                          * are missing.
116                          */
117                         error = priv_check(td, PRIV_DEBUG_SUGID);
118                         if (error)
119                                 break;
120                 }
121                 p->p_pfsflags = flags;
122                 break;
123         case PIOCGFL:
124                 *(unsigned int *)data = p->p_pfsflags;
125                 break;
126         case PIOCWAIT:
127                 while (p->p_step == 0 && (p->p_flag & P_WEXIT) == 0) {
128                         /* sleep until p stops */
129                         _PHOLD(p);
130                         error = msleep(&p->p_stype, &p->p_mtx,
131                             PWAIT|PCATCH, "pioctl", 0);
132                         _PRELE(p);
133                         if (error != 0)
134                                 break;
135                 }
136                 /* fall through to PIOCSTATUS */
137         case PIOCSTATUS:
138                 ps = (struct procfs_status *)data;
139                 ps->state = (p->p_step == 0);
140                 ps->flags = 0; /* nope */
141                 ps->events = p->p_stops;
142                 ps->why = p->p_step ? p->p_stype : 0;
143                 ps->val = p->p_step ? p->p_xstat : 0;
144                 break;
145 #ifdef COMPAT_FREEBSD32
146         case PIOCWAIT32:
147                 while (p->p_step == 0 && (p->p_flag & P_WEXIT) == 0) {
148                         /* sleep until p stops */
149                         _PHOLD(p);
150                         error = msleep(&p->p_stype, &p->p_mtx,
151                             PWAIT|PCATCH, "pioctl", 0);
152                         _PRELE(p);
153                         if (error != 0)
154                                 break;
155                 }
156                 /* fall through to PIOCSTATUS32 */
157         case PIOCSTATUS32:
158                 ps32 = (struct procfs_status32 *)data;
159                 ps32->state = (p->p_step == 0);
160                 ps32->flags = 0; /* nope */
161                 ps32->events = p->p_stops;
162                 ps32->why = p->p_step ? p->p_stype : 0;
163                 ps32->val = p->p_step ? p->p_xstat : 0;
164                 break;
165 #endif
166 #if defined(COMPAT_FREEBSD5) || defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
167         case _IOC(IOC_IN, 'p', 5, 0):
168 #endif
169 #ifdef COMPAT_FREEBSD6
170         case _IO('p', 5):
171                 ival = IOCPARM_IVAL(data);
172                 data = &ival;
173 #endif
174         case PIOCCONT:
175                 if (p->p_step == 0)
176                         break;
177                 sig = *(unsigned int *)data;
178                 if (sig != 0 && !_SIG_VALID(sig)) {
179                         error = EINVAL;
180                         break;
181                 }
182 #if 0
183                 p->p_step = 0;
184                 if (P_SHOULDSTOP(p)) {
185                         p->p_xstat = sig;
186                         p->p_flag &= ~(P_STOPPED_TRACE|P_STOPPED_SIG);
187                         PROC_SLOCK(p);
188                         thread_unsuspend(p);
189                         PROC_SUNLOCK(p);
190                 } else if (sig)
191                         kern_psignal(p, sig);
192 #else
193                 if (sig)
194                         kern_psignal(p, sig);
195                 p->p_step = 0;
196                 wakeup(&p->p_step);
197 #endif
198                 break;
199         default:
200                 error = (ENOTTY);
201         }
202
203         return (error);
204 }
205
206 /*
207  * Clean up on last close
208  */
209 int
210 procfs_close(PFS_CLOSE_ARGS)
211 {
212         if (p != NULL && (p->p_pfsflags & PF_LINGER) == 0) {
213                 PROC_LOCK_ASSERT(p, MA_OWNED);
214                 p->p_pfsflags = 0;
215                 p->p_stops = 0;
216                 p->p_step = 0;
217                 wakeup(&p->p_step);
218         }
219         return (0);
220 }