]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/fs/procfs/procfs_ioctl.c
This commit was generated by cvs2svn to compensate for changes in r167961,
[FreeBSD/FreeBSD.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_IA32
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_IA32
66         struct procfs_status32 *ps32;
67 #endif
68         int error, flags, sig;
69 #ifdef COMPAT_FREEBSD6
70         int ival;
71 #endif
72
73         PROC_LOCK(p);
74         error = 0;
75         switch (cmd) {
76 #if defined(COMPAT_FREEBSD5) || defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
77         case _IOC(IOC_IN, 'p', 1, 0):
78 #endif
79 #ifdef COMPAT_FREEBSD6
80         case _IO('p', 1):
81                 ival = IOCPARM_IVAL(data);
82                 data = &ival;
83 #endif
84         case PIOCBIS:
85                 p->p_stops |= *(unsigned int *)data;
86                 break;
87 #if defined(COMPAT_FREEBSD5) || defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
88         case _IOC(IOC_IN, 'p', 2, 0):
89 #endif
90 #ifdef COMPAT_FREEBSD6
91         case _IO('p', 2):
92                 ival = IOCPARM_IVAL(data);
93                 data = &ival;
94 #endif
95         case PIOCBIC:
96                 p->p_stops &= ~*(unsigned int *)data;
97                 break;
98 #if defined(COMPAT_FREEBSD5) || defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
99         case _IOC(IOC_IN, 'p', 3, 0):
100 #endif
101 #ifdef COMPAT_FREEBSD6
102         case _IO('p', 3):
103                 ival = IOCPARM_IVAL(data);
104                 data = &ival;
105 #endif
106         case PIOCSFL:
107                 flags = *(unsigned int *)data;
108                 if (flags & PF_ISUGID) {
109                         /*
110                          * XXXRW: Is this specific check required here, as
111                          * p_candebug() should implement it, or other checks
112                          * are missing.
113                          */
114                         error = priv_check_cred(td->td_ucred,
115                             PRIV_DEBUG_SUGID, SUSER_ALLOWJAIL);
116                         if (error)
117                                 break;
118                 }
119                 p->p_pfsflags = flags;
120                 break;
121         case PIOCGFL:
122                 *(unsigned int *)data = p->p_pfsflags;
123                 break;
124         case PIOCWAIT:
125                 while (p->p_step == 0 && (p->p_flag & P_WEXIT) == 0) {
126                         /* sleep until p stops */
127                         error = msleep(&p->p_stype, &p->p_mtx,
128                             PWAIT|PCATCH, "pioctl", 0);
129                         if (error != 0)
130                                 break;
131                 }
132                 /* fall through to PIOCSTATUS */
133         case PIOCSTATUS:
134                 ps = (struct procfs_status *)data;
135                 ps->state = (p->p_step == 0);
136                 ps->flags = 0; /* nope */
137                 ps->events = p->p_stops;
138                 ps->why = p->p_step ? p->p_stype : 0;
139                 ps->val = p->p_step ? p->p_xstat : 0;
140                 break;
141 #ifdef COMPAT_IA32
142         case PIOCWAIT32:
143                 while (p->p_step == 0 && (p->p_flag & P_WEXIT) == 0) {
144                         /* sleep until p stops */
145                         error = msleep(&p->p_stype, &p->p_mtx,
146                             PWAIT|PCATCH, "pioctl", 0);
147                         if (error != 0)
148                                 break;
149                 }
150                 /* fall through to PIOCSTATUS32 */
151         case PIOCSTATUS32:
152                 ps32 = (struct procfs_status32 *)data;
153                 ps32->state = (p->p_step == 0);
154                 ps32->flags = 0; /* nope */
155                 ps32->events = p->p_stops;
156                 ps32->why = p->p_step ? p->p_stype : 0;
157                 ps32->val = p->p_step ? p->p_xstat : 0;
158                 break;
159 #endif
160 #if defined(COMPAT_FREEBSD5) || defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
161         case _IOC(IOC_IN, 'p', 5, 0):
162 #endif
163 #ifdef COMPAT_FREEBSD6
164         case _IO('p', 5):
165                 ival = IOCPARM_IVAL(data);
166                 data = &ival;
167 #endif
168         case PIOCCONT:
169                 if (p->p_step == 0)
170                         break;
171                 sig = *(unsigned int *)data;
172                 if (sig != 0 && !_SIG_VALID(sig)) {
173                         error = EINVAL;
174                         break;
175                 }
176 #if 0
177                 p->p_step = 0;
178                 if (P_SHOULDSTOP(p)) {
179                         p->p_xstat = sig;
180                         p->p_flag &= ~(P_STOPPED_TRACE|P_STOPPED_SIG);
181                         mtx_lock_spin(&sched_lock);
182                         thread_unsuspend(p);
183                         mtx_unlock_spin(&sched_lock);
184                 } else if (sig)
185                         psignal(p, sig);
186 #else
187                 if (sig)
188                         psignal(p, sig);
189                 p->p_step = 0;
190                 wakeup(&p->p_step);
191 #endif
192                 break;
193         default:
194                 error = (ENOTTY);
195         }
196         PROC_UNLOCK(p);
197
198         return (error);
199 }
200
201 /*
202  * Clean up on last close
203  */
204 int
205 procfs_close(PFS_CLOSE_ARGS)
206 {
207         if (p != NULL && (p->p_pfsflags & PF_LINGER) == 0) {
208                 PROC_LOCK_ASSERT(p, MA_OWNED);
209                 p->p_pfsflags = 0;
210                 p->p_stops = 0;
211                 p->p_step = 0;
212                 wakeup(&p->p_step);
213         }
214         return (0);
215 }