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/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 #ifdef COMPAT_IA32
45 struct procfs_status32 {
46 int state; /* Running, stopped, something else? */
47 int flags; /* Any flags */
48 unsigned int events; /* Events to stop on */
49 int why; /* What event, if any, proc stopped on */
50 unsigned int val; /* Any extra data */
51 };
52
53 #define PIOCWAIT32 _IOR('p', 4, struct procfs_status32)
54 #define PIOCSTATUS32 _IOR('p', 6, struct procfs_status32)
55 #endif
56
57 /*
58 * Process ioctls
59 */
60 int
61 procfs_ioctl(PFS_IOCTL_ARGS)
62 {
63 struct procfs_status *ps;
64 #ifdef COMPAT_IA32
65 struct procfs_status32 *ps32;
66 #endif
67 int error, flags, sig;
68 int ival;
69
70 PROC_LOCK(p);
71 error = 0;
72 switch (cmd) {
73 #if defined(COMPAT_FREEBSD5) || defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
74 case _IOC(IOC_IN, 'p', 1, 0):
75 #endif
76 case _IO('p', 1):
77 ival = IOCPARM_IVAL(data);
78 data = &ival;
79 case PIOCBIS:
80 p->p_stops |= *(unsigned int *)data;
81 break;
82 #if defined(COMPAT_FREEBSD5) || defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
83 case _IOC(IOC_IN, 'p', 2, 0):
84 #endif
85 case _IO('p', 2):
86 ival = IOCPARM_IVAL(data);
87 data = &ival;
88 case PIOCBIC:
89 p->p_stops &= ~*(unsigned int *)data;
90 break;
91 #if defined(COMPAT_FREEBSD5) || defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
92 case _IOC(IOC_IN, 'p', 3, 0):
93 #endif
94 case _IO('p', 3):
95 ival = IOCPARM_IVAL(data);
96 data = &ival;
97 case PIOCSFL:
98 flags = *(unsigned int *)data;
99 if (flags & PF_ISUGID && (error = suser(td)) != 0)
100 break;
101 p->p_pfsflags = flags;
102 break;
103 case PIOCGFL:
104 *(unsigned int *)data = p->p_pfsflags;
105 break;
106 case PIOCWAIT:
107 while (p->p_step == 0 && (p->p_flag & P_WEXIT) == 0) {
108 /* sleep until p stops */
109 error = msleep(&p->p_stype, &p->p_mtx,
110 PWAIT|PCATCH, "pioctl", 0);
111 if (error != 0)
112 break;
113 }
114 /* fall through to PIOCSTATUS */
115 case PIOCSTATUS:
116 ps = (struct procfs_status *)data;
117 ps->state = (p->p_step == 0);
118 ps->flags = 0; /* nope */
119 ps->events = p->p_stops;
120 ps->why = p->p_step ? p->p_stype : 0;
121 ps->val = p->p_step ? p->p_xstat : 0;
122 break;
123 #ifdef COMPAT_IA32
124 case PIOCWAIT32:
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 PIOCSTATUS32 */
133 case PIOCSTATUS32:
134 ps32 = (struct procfs_status32 *)data;
135 ps32->state = (p->p_step == 0);
136 ps32->flags = 0; /* nope */
137 ps32->events = p->p_stops;
138 ps32->why = p->p_step ? p->p_stype : 0;
139 ps32->val = p->p_step ? p->p_xstat : 0;
140 break;
141 #endif
142 #if defined(COMPAT_FREEBSD5) || defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
143 case _IOC(IOC_IN, 'p', 5, 0):
144 #endif
145 case _IO('p', 5):
146 ival = IOCPARM_IVAL(data);
147 data = &ival;
148 case PIOCCONT:
149 if (p->p_step == 0)
150 break;
151 sig = *(unsigned int *)data;
152 if (sig != 0 && !_SIG_VALID(sig)) {
153 error = EINVAL;
154 break;
155 }
156 #if 0
157 p->p_step = 0;
158 if (P_SHOULDSTOP(p)) {
159 p->p_xstat = sig;
160 p->p_flag &= ~(P_STOPPED_TRACE|P_STOPPED_SIG);
161 mtx_lock_spin(&sched_lock);
162 thread_unsuspend(p);
163 mtx_unlock_spin(&sched_lock);
164 } else if (sig)
165 psignal(p, sig);
166 #else
167 if (sig)
168 psignal(p, sig);
169 p->p_step = 0;
170 wakeup(&p->p_step);
171 #endif
172 break;
173 default:
174 error = (ENOTTY);
175 }
176 PROC_UNLOCK(p);
177
178 return (error);
179 }
180
181 /*
182 * Clean up on last close
183 */
184 int
185 procfs_close(PFS_CLOSE_ARGS)
186 {
187 if (p != NULL && (p->p_pfsflags & PF_LINGER) == 0) {
188 PROC_LOCK_ASSERT(p, MA_OWNED);
189 p->p_pfsflags = 0;
190 p->p_stops = 0;
191 p->p_step = 0;
192 wakeup(&p->p_step);
193 }
194 return (0);
195 }
Cache object: 8e9a85aa93841aa862f438b1cf0d8c6d
|