This source file includes following definitions.
- procfs_control
- procfs_doctl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/time.h>
42 #include <sys/kernel.h>
43 #include <sys/proc.h>
44 #include <sys/vnode.h>
45 #include <sys/ioctl.h>
46 #include <sys/tty.h>
47 #include <sys/resource.h>
48 #include <sys/resourcevar.h>
49 #include <sys/signalvar.h>
50 #include <sys/ptrace.h>
51 #include <sys/sched.h>
52 #include <miscfs/procfs/procfs.h>
53
54
55
56
57
58 #define TRACE_WAIT_P(curp, p) \
59 ((p)->p_stat == SSTOP && \
60 (p)->p_pptr == (curp) && \
61 ISSET((p)->p_flag, P_TRACED))
62
63 #ifdef PTRACE
64
65 #define PROCFS_CTL_ATTACH 1
66 #define PROCFS_CTL_DETACH 2
67 #define PROCFS_CTL_STEP 3
68 #define PROCFS_CTL_RUN 4
69 #define PROCFS_CTL_WAIT 5
70
71 static const vfs_namemap_t ctlnames[] = {
72
73 { "attach", PROCFS_CTL_ATTACH },
74 { "detach", PROCFS_CTL_DETACH },
75 { "step", PROCFS_CTL_STEP },
76 { "run", PROCFS_CTL_RUN },
77 { "wait", PROCFS_CTL_WAIT },
78 { 0 },
79 };
80
81 #endif
82
83 static const vfs_namemap_t signames[] = {
84
85 { "hup", SIGHUP }, { "int", SIGINT },
86 { "quit", SIGQUIT }, { "ill", SIGILL },
87 { "trap", SIGTRAP }, { "abrt", SIGABRT },
88 { "iot", SIGIOT }, { "emt", SIGEMT },
89 { "fpe", SIGFPE }, { "kill", SIGKILL },
90 { "bus", SIGBUS }, { "segv", SIGSEGV },
91 { "sys", SIGSYS }, { "pipe", SIGPIPE },
92 { "alrm", SIGALRM }, { "term", SIGTERM },
93 { "urg", SIGURG }, { "stop", SIGSTOP },
94 { "tstp", SIGTSTP }, { "cont", SIGCONT },
95 { "chld", SIGCHLD }, { "ttin", SIGTTIN },
96 { "ttou", SIGTTOU }, { "io", SIGIO },
97 { "xcpu", SIGXCPU }, { "xfsz", SIGXFSZ },
98 { "vtalrm", SIGVTALRM }, { "prof", SIGPROF },
99 { "winch", SIGWINCH }, { "info", SIGINFO },
100 { "usr1", SIGUSR1 }, { "usr2", SIGUSR2 },
101 { 0 },
102 };
103
104 #ifdef PTRACE
105 static int procfs_control(struct proc *, struct proc *, int);
106
107 static int
108 procfs_control(struct proc *curp, struct proc *p, int op)
109
110 {
111 int error;
112 int s;
113
114
115
116
117
118 if (op == PROCFS_CTL_ATTACH) {
119
120 if (p->p_pid == curp->p_pid)
121 return (EINVAL);
122
123
124 if (ISSET(p->p_flag, P_TRACED))
125 return (EBUSY);
126
127 if ((error = process_checkioperm(curp, p)) != 0)
128 return (error);
129
130
131
132
133
134
135
136
137
138 atomic_setbits_int(&p->p_flag, P_TRACED);
139 p->p_xstat = 0;
140 if (p->p_pptr != curp) {
141 p->p_oppid = p->p_pptr->p_pid;
142 proc_reparent(p, curp);
143 }
144 psignal(p, SIGSTOP);
145 return (0);
146 }
147
148
149
150
151
152
153
154 switch (op) {
155 case PROCFS_CTL_DETACH:
156 case PROCFS_CTL_WAIT:
157 break;
158
159 default:
160 if (!TRACE_WAIT_P(curp, p))
161 return (EBUSY);
162 }
163
164
165
166
167 FIX_SSTEP(p);
168
169
170
171
172
173
174 p->p_xstat = 0;
175
176 switch (op) {
177
178
179
180
181 case PROCFS_CTL_DETACH:
182
183 if (!ISSET(p->p_flag, P_TRACED))
184 return (0);
185
186
187 atomic_clearbits_int(&p->p_flag, P_TRACED);
188
189
190 if (p->p_oppid != p->p_pptr->p_pid) {
191 struct proc *pp;
192
193 pp = pfind(p->p_oppid);
194 if (pp)
195 proc_reparent(p, pp);
196 }
197
198 p->p_oppid = 0;
199 atomic_clearbits_int(&p->p_flag, P_WAITED);
200 wakeup(curp);
201
202 break;
203
204
205
206
207 case PROCFS_CTL_STEP:
208 #ifdef PT_STEP
209 error = process_sstep(p, 1);
210 if (error)
211 return (error);
212 break;
213 #else
214 return (EOPNOTSUPP);
215 #endif
216
217
218
219
220
221 case PROCFS_CTL_RUN:
222 break;
223
224
225
226
227
228
229 case PROCFS_CTL_WAIT:
230 error = 0;
231 if (ISSET(p->p_flag, P_TRACED)) {
232 while (error == 0 &&
233 (p->p_stat != SSTOP) &&
234 ISSET(p->p_flag, P_TRACED) &&
235 (p->p_pptr == curp)) {
236 error = tsleep(p, PWAIT|PCATCH, "procfsx", 0);
237 }
238 if (error == 0 && !TRACE_WAIT_P(curp, p))
239 error = EBUSY;
240 } else {
241 while (error == 0 && p->p_stat != SSTOP) {
242 error = tsleep(p, PWAIT|PCATCH, "procfs", 0);
243 }
244 }
245 return (error);
246
247 #ifdef DIAGNOSTIC
248 default:
249 panic("procfs_control");
250 #endif
251 }
252
253 SCHED_LOCK(s);
254 if (p->p_stat == SSTOP)
255 setrunnable(p);
256 SCHED_UNLOCK(s);
257 return (0);
258 }
259 #endif
260
261 int
262 procfs_doctl(struct proc *curp, struct proc *p, struct pfsnode *pfs, struct uio *uio)
263 {
264 int xlen;
265 int error;
266 char msg[PROCFS_CTLLEN+1];
267 const vfs_namemap_t *nm;
268 int s;
269
270 if (uio->uio_rw != UIO_WRITE)
271 return (EOPNOTSUPP);
272
273 xlen = PROCFS_CTLLEN;
274 error = vfs_getuserstr(uio, msg, &xlen);
275 if (error)
276 return (error);
277
278
279
280
281
282
283
284
285
286
287 error = EOPNOTSUPP;
288
289 #ifdef PTRACE
290 nm = vfs_findname(ctlnames, msg, xlen);
291 if (nm) {
292 error = procfs_control(curp, p, nm->nm_val);
293 } else
294 #endif
295 {
296 nm = vfs_findname(signames, msg, xlen);
297 if (nm) {
298 if (TRACE_WAIT_P(curp, p)) {
299 p->p_xstat = nm->nm_val;
300 FIX_SSTEP(p);
301 SCHED_LOCK(s);
302 setrunnable(p);
303 SCHED_UNLOCK(s);
304 } else {
305 psignal(p, nm->nm_val);
306 }
307 error = 0;
308 }
309 }
310
311 return (error);
312 }