]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Copyright (c) 1997-2006 Apple Computer, Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ | |
5 | * | |
6 | * This file contains Original Code and/or Modifications of Original Code | |
7 | * as defined in and that are subject to the Apple Public Source License | |
8 | * Version 2.0 (the 'License'). You may not use this file except in | |
9 | * compliance with the License. The rights granted to you under the License | |
10 | * may not be used to create, or enable the creation or redistribution of, | |
11 | * unlawful or unlicensed copies of an Apple operating system, or to | |
12 | * circumvent, violate, or enable the circumvention or violation of, any | |
13 | * terms of an Apple operating system software license agreement. | |
14 | * | |
15 | * Please obtain a copy of the License at | |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
17 | * | |
18 | * The Original Code and all software distributed under the License are | |
19 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, | |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
22 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. | |
23 | * Please see the License for the specific language governing rights and | |
24 | * limitations under the License. | |
25 | * | |
26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ | |
27 | */ | |
28 | /* | |
29 | * Copyright (c) 1982, 1986, 1989, 1993 | |
30 | * The Regents of the University of California. All rights reserved. | |
31 | * | |
32 | * Redistribution and use in source and binary forms, with or without | |
33 | * modification, are permitted provided that the following conditions | |
34 | * are met: | |
35 | * 1. Redistributions of source code must retain the above copyright | |
36 | * notice, this list of conditions and the following disclaimer. | |
37 | * 2. Redistributions in binary form must reproduce the above copyright | |
38 | * notice, this list of conditions and the following disclaimer in the | |
39 | * documentation and/or other materials provided with the distribution. | |
40 | * 3. All advertising materials mentioning features or use of this software | |
41 | * must display the following acknowledgement: | |
42 | * This product includes software developed by the University of | |
43 | * California, Berkeley and its contributors. | |
44 | * 4. Neither the name of the University nor the names of its contributors | |
45 | * may be used to endorse or promote products derived from this software | |
46 | * without specific prior written permission. | |
47 | * | |
48 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
49 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
50 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
51 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
52 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
53 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
54 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
55 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
56 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
57 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
58 | * SUCH DAMAGE. | |
59 | * | |
60 | * @(#)tty_pty.c 8.4 (Berkeley) 2/20/95 | |
61 | */ | |
62 | ||
63 | /* | |
64 | * Pseudo-teletype Driver | |
65 | * (Actually two drivers, requiring two entries in 'cdevsw') | |
66 | */ | |
67 | #include "pty.h" /* XXX */ | |
68 | ||
69 | #include <sys/param.h> | |
70 | #include <sys/systm.h> | |
71 | #include <sys/ioctl.h> | |
72 | #include <sys/proc_internal.h> | |
73 | #include <sys/kauth.h> | |
74 | #include <sys/tty.h> | |
75 | #include <sys/conf.h> | |
76 | #include <sys/file_internal.h> | |
77 | #include <sys/uio.h> | |
78 | #include <sys/kernel.h> | |
79 | #include <sys/vnode.h> | |
80 | #include <sys/vnode_internal.h> /* _devfs_setattr() */ | |
81 | #include <sys/stat.h> /* _devfs_setattr() */ | |
82 | #include <sys/user.h> | |
83 | #include <sys/signalvar.h> | |
84 | #include <sys/sysctl.h> | |
85 | #include <miscfs/devfs/devfs.h> | |
86 | #include <miscfs/devfs/devfsdefs.h> /* DEVFS_LOCK()/DEVFS_UNLOCK() */ | |
87 | ||
88 | /* XXX belongs in devfs somewhere - LATER */ | |
89 | int _devfs_setattr(void *, unsigned short, uid_t, gid_t); | |
90 | ||
91 | ||
92 | #define FREE_BSDSTATIC __private_extern__ | |
93 | #define d_devtotty_t struct tty ** | |
94 | ||
95 | /* | |
96 | * Forward declarations | |
97 | */ | |
98 | int ptmx_init(int n_ptys); | |
99 | static void ptsd_start(struct tty *tp); | |
100 | static void ptmx_wakeup(struct tty *tp, int flag); | |
101 | FREE_BSDSTATIC d_open_t ptsd_open; | |
102 | FREE_BSDSTATIC d_close_t ptsd_close; | |
103 | FREE_BSDSTATIC d_read_t ptsd_read; | |
104 | FREE_BSDSTATIC d_write_t ptsd_write; | |
105 | FREE_BSDSTATIC d_ioctl_t cptyioctl; /* common ioctl */ | |
106 | FREE_BSDSTATIC d_stop_t ptsd_stop; | |
107 | FREE_BSDSTATIC d_reset_t ptsd_reset; | |
108 | FREE_BSDSTATIC d_devtotty_t ptydevtotty; | |
109 | FREE_BSDSTATIC d_open_t ptmx_open; | |
110 | FREE_BSDSTATIC d_close_t ptmx_close; | |
111 | FREE_BSDSTATIC d_read_t ptmx_read; | |
112 | FREE_BSDSTATIC d_write_t ptmx_write; | |
113 | FREE_BSDSTATIC d_stop_t ptmx_stop; /* NO-OP */ | |
114 | FREE_BSDSTATIC d_reset_t ptmx_reset; | |
115 | FREE_BSDSTATIC d_select_t ptmx_select; | |
116 | FREE_BSDSTATIC d_select_t ptsd_select; | |
117 | ||
118 | static int ptmx_major; /* dynamically assigned major number */ | |
119 | static struct cdevsw ptmx_cdev = { | |
120 | ptmx_open, ptmx_close, ptmx_read, ptmx_write, | |
121 | cptyioctl, ptmx_stop, ptmx_reset, 0, | |
122 | ptmx_select, eno_mmap, eno_strat, eno_getc, | |
123 | eno_putc, D_TTY | |
124 | }; | |
125 | ||
126 | static int ptsd_major; /* dynamically assigned major number */ | |
127 | static struct cdevsw ptsd_cdev = { | |
128 | ptsd_open, ptsd_close, ptsd_read, ptsd_write, | |
129 | cptyioctl, ptsd_stop, ptsd_reset, 0, | |
130 | ptsd_select, eno_mmap, eno_strat, eno_getc, | |
131 | eno_putc, D_TTY | |
132 | }; | |
133 | ||
134 | /* | |
135 | * XXX Should be devfs function... and use VATTR mechanisms, per | |
136 | * XXX vnode_setattr2(); only we maybe can't really get back to the | |
137 | * XXX vnode here for cloning devices (but it works for *cloned* devices | |
138 | * XXX that are not themselves cloning). | |
139 | * | |
140 | * Returns: 0 Success | |
141 | * namei:??? | |
142 | * vnode_setattr:??? | |
143 | */ | |
144 | int | |
145 | _devfs_setattr(void * handle, unsigned short mode, uid_t uid, gid_t gid) | |
146 | { | |
147 | devdirent_t *direntp = (devdirent_t *)handle; | |
148 | devnode_t *devnodep; | |
149 | int error = EACCES; | |
150 | vfs_context_t ctx = vfs_context_current();; | |
151 | struct vnode_attr va; | |
152 | ||
153 | VATTR_INIT(&va); | |
154 | VATTR_SET(&va, va_uid, uid); | |
155 | VATTR_SET(&va, va_gid, gid); | |
156 | VATTR_SET(&va, va_mode, mode & ALLPERMS); | |
157 | ||
158 | /* | |
159 | * If the TIOCPTYGRANT loses the race with the clone operation because | |
160 | * this function is not part of devfs, and therefore can't take the | |
161 | * devfs lock to protect the direntp update, then force user space to | |
162 | * redrive the grant request. | |
163 | */ | |
164 | if (direntp == NULL || (devnodep = direntp->de_dnp) == NULL) { | |
165 | error = ERESTART; | |
166 | goto out; | |
167 | } | |
168 | ||
169 | /* | |
170 | * Only do this if we are operating on device that doesn't clone | |
171 | * each time it's referenced. We perform a lookup on the device | |
172 | * to insure we get the right instance. We can't just use the call | |
173 | * to devfs_dntovn() to get the vp for the operation, because | |
174 | * dn_dvm may not have been initialized. | |
175 | */ | |
176 | if (devnodep->dn_clone == NULL) { | |
177 | struct nameidata nd; | |
178 | char name[128]; | |
179 | ||
180 | snprintf(name, sizeof(name), "/dev/%s", direntp->de_name); | |
181 | NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, CAST_USER_ADDR_T(name), ctx); | |
182 | error = namei(&nd); | |
183 | if (error) | |
184 | goto out; | |
185 | error = vnode_setattr(nd.ni_vp, &va, ctx); | |
186 | vnode_put(nd.ni_vp); | |
187 | nameidone(&nd); | |
188 | goto out; | |
189 | } | |
190 | ||
191 | out: | |
192 | return(error); | |
193 | } | |
194 | ||
195 | ||
196 | ||
197 | #define BUFSIZ 100 /* Chunk size iomoved to/from user */ | |
198 | ||
199 | /* | |
200 | * ptmx == /dev/ptmx | |
201 | * ptsd == /dev/pts[0123456789]{3} | |
202 | */ | |
203 | #define PTMX_TEMPLATE "ptmx" | |
204 | #define PTSD_TEMPLATE "ttys%03d" | |
205 | ||
206 | /* | |
207 | * System-wide limit on the max number of cloned ptys | |
208 | */ | |
209 | #define PTMX_MAX_DEFAULT 127 /* 128 entries */ | |
210 | #define PTMX_MAX_HARD 999 /* 1000 entries, due to PTSD_TEMPLATE */ | |
211 | ||
212 | static int ptmx_max = PTMX_MAX_DEFAULT; /* default # of clones we allow */ | |
213 | ||
214 | /* Range enforcement for the sysctl */ | |
215 | static int | |
216 | sysctl_ptmx_max(__unused struct sysctl_oid *oidp, __unused void *arg1, | |
217 | __unused int arg2, struct sysctl_req *req) | |
218 | { | |
219 | int new_value, changed; | |
220 | int error = sysctl_io_number(req, ptmx_max, sizeof(int), &new_value, &changed); | |
221 | if (changed) { | |
222 | if (new_value > 0 && new_value <= PTMX_MAX_HARD) | |
223 | ptmx_max = new_value; | |
224 | else | |
225 | error = EINVAL; | |
226 | } | |
227 | return(error); | |
228 | } | |
229 | ||
230 | SYSCTL_NODE(_kern, KERN_TTY, tty, CTLFLAG_RW|CTLFLAG_LOCKED, 0, "TTY"); | |
231 | SYSCTL_PROC(_kern_tty, OID_AUTO, ptmx_max, | |
232 | CTLTYPE_INT | CTLFLAG_RW, | |
233 | &ptmx_max, 0, &sysctl_ptmx_max, "I", "ptmx_max"); | |
234 | ||
235 | ||
236 | /* | |
237 | * ptmx_ioctl is a pointer to a list of pointers to tty structures which is | |
238 | * grown, as necessary, copied, and replaced, but never shrunk. The ioctl | |
239 | * structures themselves pointed to from this list come and go as needed. | |
240 | */ | |
241 | struct ptmx_ioctl { | |
242 | struct tty *pt_tty; /* pointer to ttymalloc()'ed data */ | |
243 | int pt_flags; | |
244 | struct selinfo pt_selr; | |
245 | struct selinfo pt_selw; | |
246 | u_char pt_send; | |
247 | u_char pt_ucntl; | |
248 | void *pt_devhandle; /* cloned slave device handle */ | |
249 | }; | |
250 | ||
251 | #define PF_PKT 0x0008 /* packet mode */ | |
252 | #define PF_STOPPED 0x0010 /* user told stopped */ | |
253 | #define PF_REMOTE 0x0020 /* remote and flow controlled input */ | |
254 | #define PF_NOSTOP 0x0040 | |
255 | #define PF_UCNTL 0x0080 /* user control mode */ | |
256 | #define PF_UNLOCKED 0x0100 /* slave unlock (master open resets) */ | |
257 | #define PF_OPEN_M 0x0200 /* master is open */ | |
258 | #define PF_OPEN_S 0x0400 /* slave is open */ | |
259 | ||
260 | static int ptmx_clone(dev_t dev, int minor); | |
261 | ||
262 | int | |
263 | ptmx_init( __unused int config_count) | |
264 | { | |
265 | /* | |
266 | * We start looking at slot 10, since there are inits that will | |
267 | * stomp explicit slots (e.g. vndevice stomps 1) below that. | |
268 | */ | |
269 | ||
270 | /* Get a major number for /dev/ptmx */ | |
271 | if((ptmx_major = cdevsw_add(-15, &ptmx_cdev)) == -1) { | |
272 | printf("ptmx_init: failed to obtain /dev/ptmx major number\n"); | |
273 | return (ENOENT); | |
274 | } | |
275 | ||
276 | /* Get a major number for /dev/pts/nnn */ | |
277 | if ((ptsd_major = cdevsw_add(-15, &ptsd_cdev)) == -1) { | |
278 | (void)cdevsw_remove(ptmx_major, &ptmx_cdev); | |
279 | printf("ptmx_init: failed to obtain /dev/ptmx major number\n"); | |
280 | return (ENOENT); | |
281 | } | |
282 | ||
283 | /* Create the /dev/ptmx device {<major>,0} */ | |
284 | (void)devfs_make_node_clone(makedev(ptmx_major, 0), | |
285 | DEVFS_CHAR, UID_ROOT, GID_TTY, 0666, | |
286 | ptmx_clone, PTMX_TEMPLATE); | |
287 | return (0); | |
288 | } | |
289 | ||
290 | ||
291 | static struct _ptmx_ioctl_state { | |
292 | struct ptmx_ioctl **pis_ioctl_list; /* pointer vector */ | |
293 | int pis_total; /* total slots */ | |
294 | int pis_free; /* free slots */ | |
295 | } _state; | |
296 | #define PTMX_GROW_VECTOR 16 /* Grow by this many slots at a time */ | |
297 | ||
298 | /* | |
299 | * Given a minor number, return the corresponding structure for that minor | |
300 | * number. If there isn't one, and the create flag is specified, we create | |
301 | * one if possible. | |
302 | * | |
303 | * Parameters: minor Minor number of ptmx device | |
304 | * open_flag PF_OPEN_M First open of master | |
305 | * PF_OPEN_S First open of slave | |
306 | * 0 Just want ioctl struct | |
307 | * | |
308 | * Returns: NULL Did not exist/could not create | |
309 | * !NULL structure corresponding minor number | |
310 | */ | |
311 | static struct ptmx_ioctl * | |
312 | ptmx_get_ioctl(int minor, int open_flag) | |
313 | { | |
314 | struct ptmx_ioctl *new_ptmx_ioctl; | |
315 | ||
316 | if (open_flag & PF_OPEN_M) { | |
317 | ||
318 | /* | |
319 | * If we are about to allocate more memory, but we have | |
320 | * already hit the administrative limit, then fail the | |
321 | * operation. | |
322 | * | |
323 | * Note: Subtract free from total when making this | |
324 | * check to allow unit increments, rather than | |
325 | * snapping to the nearest PTMX_GROW_VECTOR... | |
326 | */ | |
327 | if ((_state.pis_total - _state.pis_free) >= ptmx_max) { | |
328 | return (NULL); | |
329 | } | |
330 | ||
331 | MALLOC(new_ptmx_ioctl, struct ptmx_ioctl *, sizeof(struct ptmx_ioctl), M_TTYS, M_WAITOK|M_ZERO); | |
332 | if (new_ptmx_ioctl == NULL) { | |
333 | return (NULL); | |
334 | } | |
335 | ||
336 | if ((new_ptmx_ioctl->pt_tty = ttymalloc()) == NULL) { | |
337 | FREE(new_ptmx_ioctl, M_TTYS); | |
338 | return (NULL); | |
339 | } | |
340 | ||
341 | /* | |
342 | * Hold the DEVFS_LOCK() over this whole operation; devfs | |
343 | * itself does this over malloc/free as well, so this should | |
344 | * be safe to do. We hold it longer than we want to, but | |
345 | * doing so avoids a reallocation race on the minor number. | |
346 | */ | |
347 | DEVFS_LOCK(); | |
348 | /* Need to allocate a larger vector? */ | |
349 | if (_state.pis_free == 0) { | |
350 | struct ptmx_ioctl **new_pis_ioctl_list; | |
351 | struct ptmx_ioctl **old_pis_ioctl_list = NULL; | |
352 | ||
353 | /* Yes. */ | |
354 | MALLOC(new_pis_ioctl_list, struct ptmx_ioctl **, sizeof(struct ptmx_ioctl *) * (_state.pis_total + PTMX_GROW_VECTOR), M_TTYS, M_WAITOK|M_ZERO); | |
355 | if (new_pis_ioctl_list == NULL) { | |
356 | ttyfree(new_ptmx_ioctl->pt_tty); | |
357 | DEVFS_UNLOCK(); | |
358 | FREE(new_ptmx_ioctl, M_TTYS); | |
359 | return (NULL); | |
360 | } | |
361 | ||
362 | /* If this is not the first time, copy the old over */ | |
363 | bcopy(_state.pis_ioctl_list, new_pis_ioctl_list, sizeof(struct ptmx_ioctl *) * _state.pis_total); | |
364 | old_pis_ioctl_list = _state.pis_ioctl_list; | |
365 | _state.pis_ioctl_list = new_pis_ioctl_list; | |
366 | _state.pis_free += PTMX_GROW_VECTOR; | |
367 | _state.pis_total += PTMX_GROW_VECTOR; | |
368 | if (old_pis_ioctl_list) | |
369 | FREE(old_pis_ioctl_list, M_TTYS); | |
370 | } | |
371 | ||
372 | if (_state.pis_ioctl_list[minor] != NULL) { | |
373 | ttyfree(new_ptmx_ioctl->pt_tty); | |
374 | DEVFS_UNLOCK(); | |
375 | FREE(new_ptmx_ioctl, M_TTYS); | |
376 | ||
377 | /* Special error value so we know to redrive the open, we've been raced */ | |
378 | return (struct ptmx_ioctl*)-1; | |
379 | ||
380 | } | |
381 | ||
382 | /* Vector is large enough; grab a new ptmx_ioctl */ | |
383 | ||
384 | /* Now grab a free slot... */ | |
385 | _state.pis_ioctl_list[minor] = new_ptmx_ioctl; | |
386 | ||
387 | /* reduce free count */ | |
388 | _state.pis_free--; | |
389 | ||
390 | _state.pis_ioctl_list[minor]->pt_flags |= PF_OPEN_M; | |
391 | DEVFS_UNLOCK(); | |
392 | ||
393 | /* Create the /dev/ttysXXX device {<major>,XXX} */ | |
394 | _state.pis_ioctl_list[minor]->pt_devhandle = devfs_make_node( | |
395 | makedev(ptsd_major, minor), | |
396 | DEVFS_CHAR, UID_ROOT, GID_TTY, 0620, | |
397 | PTSD_TEMPLATE, minor); | |
398 | } else if (open_flag & PF_OPEN_S) { | |
399 | DEVFS_LOCK(); | |
400 | _state.pis_ioctl_list[minor]->pt_flags |= PF_OPEN_S; | |
401 | DEVFS_UNLOCK(); | |
402 | } | |
403 | return (_state.pis_ioctl_list[minor]); | |
404 | } | |
405 | ||
406 | static int | |
407 | ptmx_free_ioctl(int minor, int open_flag) | |
408 | { | |
409 | struct ptmx_ioctl *old_ptmx_ioctl = NULL; | |
410 | ||
411 | DEVFS_LOCK(); | |
412 | #if 5161374 | |
413 | /* | |
414 | * We have to check after taking the DEVFS_LOCK, since the pointer | |
415 | * is protected by the lock | |
416 | */ | |
417 | if (_state.pis_ioctl_list[minor] == NULL) { | |
418 | DEVFS_UNLOCK(); | |
419 | return (ENXIO); | |
420 | } | |
421 | #endif /* 5161374 */ | |
422 | _state.pis_ioctl_list[minor]->pt_flags &= ~(open_flag); | |
423 | ||
424 | /* | |
425 | * Was this the last close? We will recognize it because we only get | |
426 | * a notification on the last close of a device, and we will have | |
427 | * cleared both the master and the slave open bits in the flags. | |
428 | */ | |
429 | if (!(_state.pis_ioctl_list[minor]->pt_flags & (PF_OPEN_M|PF_OPEN_S))) { | |
430 | /* Mark as free so it can be reallocated later */ | |
431 | old_ptmx_ioctl = _state.pis_ioctl_list[ minor]; | |
432 | } | |
433 | DEVFS_UNLOCK(); | |
434 | ||
435 | /* Free old after dropping lock */ | |
436 | if (old_ptmx_ioctl != NULL) { | |
437 | /* | |
438 | * XXX See <rdar://5348651> and <rdar://4854638> | |
439 | * | |
440 | * XXX Conditional to be removed when/if tty/pty reference | |
441 | * XXX counting and mutex implemented. | |
442 | */ | |
443 | if (old_ptmx_ioctl->pt_devhandle != NULL) | |
444 | devfs_remove(old_ptmx_ioctl->pt_devhandle); | |
445 | ttyfree(old_ptmx_ioctl->pt_tty); | |
446 | FREE(old_ptmx_ioctl, M_TTYS); | |
447 | ||
448 | /* Don't remove the entry until the devfs slot is free */ | |
449 | DEVFS_LOCK(); | |
450 | _state.pis_ioctl_list[ minor] = NULL; | |
451 | _state.pis_free++; | |
452 | DEVFS_UNLOCK(); | |
453 | } | |
454 | ||
455 | return (0); /* Success */ | |
456 | } | |
457 | ||
458 | ||
459 | ||
460 | ||
461 | /* | |
462 | * Given the dev entry that's being opened, we clone the device. This driver | |
463 | * doesn't actually use the dev entry, since we alreaqdy know who we are by | |
464 | * being called from this code. This routine is a callback registered from | |
465 | * devfs_make_node_clone() in ptmx_init(); it's purpose is to provide a new | |
466 | * minor number, or to return -1, if one can't be provided. | |
467 | * | |
468 | * Parameters: dev The device we are cloning from | |
469 | * | |
470 | * Returns: >= 0 A new minor device number | |
471 | * -1 Error: ENOMEM ("Can't alloc device") | |
472 | * | |
473 | * NOTE: Called with DEVFS_LOCK() held | |
474 | */ | |
475 | static int | |
476 | ptmx_clone(__unused dev_t dev, int action) | |
477 | { | |
478 | int i; | |
479 | ||
480 | if (action == DEVFS_CLONE_ALLOC) { | |
481 | /* First one */ | |
482 | if (_state.pis_total == 0) | |
483 | return (0); | |
484 | ||
485 | /* | |
486 | * Note: We can add hinting on free slots, if this linear search | |
487 | * ends up being a performance bottleneck... | |
488 | */ | |
489 | for(i = 0; i < _state.pis_total; i++) { | |
490 | if (_state.pis_ioctl_list[ i] == NULL) | |
491 | break; | |
492 | } | |
493 | ||
494 | /* | |
495 | * XXX We fall off the end here; if we did this twice at the | |
496 | * XXX same time, we could return the same minor to two | |
497 | * XXX callers; we should probably exand the pointer vector | |
498 | * XXX here, but I need more information on the MALLOC/FREE | |
499 | * XXX locking to ensure against a deadlock. Maybe we can | |
500 | * XXX just high watermark it at 1/2 of PTMX_GROW_VECTOR? | |
501 | * XXX That would require returning &minor as implict return | |
502 | * XXX and an error code ("EAGAIN/ERESTART") or 0 as our | |
503 | * XXX explicit return. | |
504 | */ | |
505 | ||
506 | return (i); /* empty slot or next slot */ | |
507 | } | |
508 | return(-1); | |
509 | } | |
510 | ||
511 | FREE_BSDSTATIC int | |
512 | ptsd_open(dev_t dev, int flag, __unused int devtype, __unused proc_t p) | |
513 | { | |
514 | struct tty *tp; | |
515 | struct ptmx_ioctl *pti; | |
516 | int error; | |
517 | boolean_t funnel_state; | |
518 | ||
519 | if ((pti = ptmx_get_ioctl(minor(dev), 0)) == NULL) { | |
520 | return (ENXIO); | |
521 | } | |
522 | tp = pti->pt_tty; | |
523 | ||
524 | if (!(pti->pt_flags & PF_UNLOCKED)) { | |
525 | return (EAGAIN); | |
526 | } | |
527 | ||
528 | funnel_state = thread_funnel_set(kernel_flock, TRUE); | |
529 | ||
530 | if ((tp->t_state & TS_ISOPEN) == 0) { | |
531 | ttychars(tp); /* Set up default chars */ | |
532 | tp->t_iflag = TTYDEF_IFLAG; | |
533 | tp->t_oflag = TTYDEF_OFLAG; | |
534 | tp->t_lflag = TTYDEF_LFLAG; | |
535 | tp->t_cflag = TTYDEF_CFLAG; | |
536 | tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED; | |
537 | ttsetwater(tp); /* would be done in xxparam() */ | |
538 | } else if (tp->t_state&TS_XCLUDE && suser(kauth_cred_get(), NULL)) { | |
539 | error = EBUSY; | |
540 | goto out; | |
541 | } | |
542 | if (tp->t_oproc) /* Ctrlr still around. */ | |
543 | (void)(*linesw[tp->t_line].l_modem)(tp, 1); | |
544 | while ((tp->t_state & TS_CARR_ON) == 0) { | |
545 | if (flag&FNONBLOCK) | |
546 | break; | |
547 | error = ttysleep(tp, TSA_CARR_ON(tp), TTIPRI | PCATCH, | |
548 | "ptsd_opn", 0); | |
549 | if (error) | |
550 | goto out; | |
551 | } | |
552 | error = (*linesw[tp->t_line].l_open)(dev, tp); | |
553 | /* Successful open; mark as open by the slave */ | |
554 | pti->pt_flags |= PF_OPEN_S; | |
555 | if (error == 0) | |
556 | ptmx_wakeup(tp, FREAD|FWRITE); | |
557 | out: | |
558 | (void) thread_funnel_set(kernel_flock, funnel_state); | |
559 | return (error); | |
560 | } | |
561 | ||
562 | FREE_BSDSTATIC int | |
563 | ptsd_close(dev_t dev, int flag, __unused int mode, __unused proc_t p) | |
564 | { | |
565 | struct tty *tp; | |
566 | struct ptmx_ioctl *pti; | |
567 | int err; | |
568 | boolean_t funnel_state; | |
569 | ||
570 | /* | |
571 | * This is temporary until the VSX conformance tests | |
572 | * are fixed. They are hanging with a deadlock | |
573 | * where close(ptsd) will not complete without t_timeout set | |
574 | */ | |
575 | #define FIX_VSX_HANG 1 | |
576 | #ifdef FIX_VSX_HANG | |
577 | int save_timeout; | |
578 | #endif | |
579 | pti = ptmx_get_ioctl(minor(dev), 0); | |
580 | #if 5161374 | |
581 | if (pti == NULL || pti->pt_tty == NULL) | |
582 | return(ENXIO); | |
583 | #endif /* 5161374 */ | |
584 | tp = pti->pt_tty; | |
585 | ||
586 | funnel_state = thread_funnel_set(kernel_flock, TRUE); | |
587 | ||
588 | #ifdef FIX_VSX_HANG | |
589 | save_timeout = tp->t_timeout; | |
590 | tp->t_timeout = 60; | |
591 | #endif | |
592 | err = (*linesw[tp->t_line].l_close)(tp, flag); | |
593 | ptsd_stop(tp, FREAD|FWRITE); | |
594 | (void) ttyclose(tp); | |
595 | #ifdef FIX_VSX_HANG | |
596 | tp->t_timeout = save_timeout; | |
597 | #endif | |
598 | (void) thread_funnel_set(kernel_flock, funnel_state); | |
599 | ||
600 | /* unconditional, just like ttyclose() */ | |
601 | ptmx_free_ioctl(minor(dev), PF_OPEN_S); | |
602 | ||
603 | return (err); | |
604 | } | |
605 | ||
606 | FREE_BSDSTATIC int | |
607 | ptsd_read(dev_t dev, struct uio *uio, int flag) | |
608 | { | |
609 | proc_t p = current_proc(); | |
610 | ||
611 | struct tty *tp; | |
612 | struct ptmx_ioctl *pti; | |
613 | int error = 0; | |
614 | struct uthread *ut; | |
615 | boolean_t funnel_state; | |
616 | struct pgrp * pg; | |
617 | ||
618 | pti = ptmx_get_ioctl(minor(dev), 0); | |
619 | #if 5161374 | |
620 | if (pti == NULL || pti->pt_tty == NULL) | |
621 | return(ENXIO); | |
622 | #endif /* 5161374 */ | |
623 | tp = pti->pt_tty; | |
624 | ||
625 | funnel_state = thread_funnel_set(kernel_flock, TRUE); | |
626 | ||
627 | ||
628 | ut = (struct uthread *)get_bsdthread_info(current_thread()); | |
629 | again: | |
630 | if (pti->pt_flags & PF_REMOTE) { | |
631 | while (isbackground(p, tp)) { | |
632 | if ((p->p_sigignore & sigmask(SIGTTIN)) || | |
633 | (ut->uu_sigmask & sigmask(SIGTTIN)) || | |
634 | p->p_lflag & P_LPPWAIT) { | |
635 | error = EIO; | |
636 | goto out; | |
637 | } | |
638 | pg = proc_pgrp(p); | |
639 | if (pg == PGRP_NULL) { | |
640 | error = EIO; | |
641 | goto out; | |
642 | } | |
643 | if (pg->pg_jobc == 0) { | |
644 | pg_rele(pg); | |
645 | error = EIO; | |
646 | goto out; | |
647 | } | |
648 | pgsignal(pg, SIGTTIN, 1); | |
649 | pg_rele(pg); | |
650 | ||
651 | error = ttysleep(tp, &lbolt, TTIPRI | PCATCH | PTTYBLOCK, "ptsd_bg", | |
652 | 0); | |
653 | if (error) | |
654 | goto out; | |
655 | } | |
656 | if (tp->t_canq.c_cc == 0) { | |
657 | if (flag & IO_NDELAY) | |
658 | return (EWOULDBLOCK); | |
659 | error = ttysleep(tp, TSA_PTS_READ(tp), TTIPRI | PCATCH, | |
660 | "ptsd_in", 0); | |
661 | if (error) | |
662 | goto out; | |
663 | goto again; | |
664 | } | |
665 | while (tp->t_canq.c_cc > 1 && uio_resid(uio) > 0) { | |
666 | int cc; | |
667 | char buf[BUFSIZ]; | |
668 | ||
669 | cc = min(uio_resid(uio), BUFSIZ); | |
670 | // Don't copy the very last byte | |
671 | cc = min(cc, tp->t_canq.c_cc - 1); | |
672 | cc = q_to_b(&tp->t_canq, (u_char *)buf, cc); | |
673 | error = uiomove(buf, cc, uio); | |
674 | if (error) | |
675 | break; | |
676 | } | |
677 | if (tp->t_canq.c_cc == 1) | |
678 | (void) getc(&tp->t_canq); | |
679 | if (tp->t_canq.c_cc) | |
680 | goto out; | |
681 | } else | |
682 | if (tp->t_oproc) | |
683 | error = (*linesw[tp->t_line].l_read)(tp, uio, flag); | |
684 | ptmx_wakeup(tp, FWRITE); | |
685 | out: | |
686 | (void) thread_funnel_set(kernel_flock, funnel_state); | |
687 | return (error); | |
688 | } | |
689 | ||
690 | /* | |
691 | * Write to pseudo-tty. | |
692 | * Wakeups of controlling tty will happen | |
693 | * indirectly, when tty driver calls ptsd_start. | |
694 | */ | |
695 | FREE_BSDSTATIC int | |
696 | ptsd_write(dev_t dev, struct uio *uio, int flag) | |
697 | { | |
698 | struct tty *tp; | |
699 | struct ptmx_ioctl *pti; | |
700 | int error; | |
701 | boolean_t funnel_state; | |
702 | ||
703 | funnel_state = thread_funnel_set(kernel_flock, TRUE); | |
704 | ||
705 | pti = ptmx_get_ioctl(minor(dev), 0); | |
706 | #if 5161374 | |
707 | if (pti == NULL || pti->pt_tty == NULL) | |
708 | return(ENXIO); | |
709 | #endif /* 5161374 */ | |
710 | tp = pti->pt_tty; | |
711 | ||
712 | if (tp->t_oproc == 0) | |
713 | error = EIO; | |
714 | else | |
715 | error = (*linesw[tp->t_line].l_write)(tp, uio, flag); | |
716 | ||
717 | (void) thread_funnel_set(kernel_flock, funnel_state); | |
718 | return (error); | |
719 | } | |
720 | ||
721 | /* | |
722 | * Start output on pseudo-tty. | |
723 | * Wake up process selecting or sleeping for input from controlling tty. | |
724 | */ | |
725 | static void | |
726 | ptsd_start(struct tty *tp) | |
727 | { | |
728 | struct ptmx_ioctl *pti; | |
729 | boolean_t funnel_state; | |
730 | ||
731 | pti = ptmx_get_ioctl(minor(tp->t_dev), 0); | |
732 | #if 5161374 | |
733 | if (pti == NULL) | |
734 | return; /* XXX ENXIO, but this function is void! */ | |
735 | #endif /* 5161374 */ | |
736 | ||
737 | funnel_state = thread_funnel_set(kernel_flock, TRUE); | |
738 | ||
739 | if (tp->t_state & TS_TTSTOP) | |
740 | goto out; | |
741 | if (pti->pt_flags & PF_STOPPED) { | |
742 | pti->pt_flags &= ~PF_STOPPED; | |
743 | pti->pt_send = TIOCPKT_START; | |
744 | } | |
745 | ptmx_wakeup(tp, FREAD); | |
746 | out: | |
747 | (void) thread_funnel_set(kernel_flock, funnel_state); | |
748 | return; | |
749 | } | |
750 | ||
751 | static void | |
752 | ptmx_wakeup(struct tty *tp, int flag) | |
753 | { | |
754 | struct ptmx_ioctl *pti; | |
755 | boolean_t funnel_state; | |
756 | ||
757 | pti = ptmx_get_ioctl(minor(tp->t_dev), 0); | |
758 | #if 5161374 | |
759 | if (pti == NULL) | |
760 | return; /* XXX ENXIO, but this function is void! */ | |
761 | #endif /* 5161374 */ | |
762 | ||
763 | funnel_state = thread_funnel_set(kernel_flock, TRUE); | |
764 | ||
765 | if (flag & FREAD) { | |
766 | selwakeup(&pti->pt_selr); | |
767 | wakeup(TSA_PTC_READ(tp)); | |
768 | } | |
769 | if (flag & FWRITE) { | |
770 | selwakeup(&pti->pt_selw); | |
771 | wakeup(TSA_PTC_WRITE(tp)); | |
772 | } | |
773 | (void) thread_funnel_set(kernel_flock, funnel_state); | |
774 | } | |
775 | ||
776 | FREE_BSDSTATIC int | |
777 | ptmx_open(dev_t dev, __unused int flag, __unused int devtype, __unused proc_t p) | |
778 | { | |
779 | struct tty *tp; | |
780 | struct ptmx_ioctl *pti; | |
781 | int error = 0; | |
782 | boolean_t funnel_state; | |
783 | ||
784 | pti = ptmx_get_ioctl(minor(dev), PF_OPEN_M); | |
785 | if (pti == NULL) { | |
786 | return (ENXIO); | |
787 | } else if (pti == (struct ptmx_ioctl*)-1) { | |
788 | return (EREDRIVEOPEN); | |
789 | } | |
790 | tp = pti->pt_tty; | |
791 | ||
792 | funnel_state = thread_funnel_set(kernel_flock, TRUE); | |
793 | ||
794 | /* If master is open OR slave is still draining, pty is still busy */ | |
795 | if (tp->t_oproc || (tp->t_state & TS_ISOPEN)) { | |
796 | /* | |
797 | * If master is closed, we are the only reference, so we | |
798 | * need to clear the master open bit | |
799 | */ | |
800 | if (!tp->t_oproc) | |
801 | ptmx_free_ioctl(minor(dev), PF_OPEN_M); | |
802 | error = EBUSY; | |
803 | goto out; | |
804 | } | |
805 | tp->t_oproc = ptsd_start; | |
806 | CLR(tp->t_state, TS_ZOMBIE); | |
807 | #ifdef sun4c | |
808 | tp->t_stop = ptsd_stop; | |
809 | #endif | |
810 | (void)(*linesw[tp->t_line].l_modem)(tp, 1); | |
811 | tp->t_lflag &= ~EXTPROC; | |
812 | ||
813 | out: | |
814 | (void) thread_funnel_set(kernel_flock, funnel_state); | |
815 | return (error); | |
816 | } | |
817 | ||
818 | FREE_BSDSTATIC int | |
819 | ptmx_close(dev_t dev, __unused int flags, __unused int fmt, __unused proc_t p) | |
820 | { | |
821 | struct tty *tp; | |
822 | struct ptmx_ioctl *pti; | |
823 | boolean_t funnel_state; | |
824 | ||
825 | pti = ptmx_get_ioctl(minor(dev), 0); | |
826 | #if 5161374 | |
827 | if (pti == NULL || pti->pt_tty == NULL) | |
828 | return(ENXIO); | |
829 | #endif /* 5161374 */ | |
830 | tp = pti->pt_tty; | |
831 | ||
832 | funnel_state = thread_funnel_set(kernel_flock, TRUE); | |
833 | ||
834 | (void)(*linesw[tp->t_line].l_modem)(tp, 0); | |
835 | ||
836 | /* | |
837 | * XXX MDMBUF makes no sense for ptys but would inhibit the above | |
838 | * l_modem(). CLOCAL makes sense but isn't supported. Special | |
839 | * l_modem()s that ignore carrier drop make no sense for ptys but | |
840 | * may be in use because other parts of the line discipline make | |
841 | * sense for ptys. Recover by doing everything that a normal | |
842 | * ttymodem() would have done except for sending a SIGHUP. | |
843 | */ | |
844 | if (tp->t_state & TS_ISOPEN) { | |
845 | tp->t_state &= ~(TS_CARR_ON | TS_CONNECTED); | |
846 | tp->t_state |= TS_ZOMBIE; | |
847 | ttyflush(tp, FREAD | FWRITE); | |
848 | } | |
849 | ||
850 | tp->t_oproc = 0; /* mark closed */ | |
851 | ||
852 | (void) thread_funnel_set(kernel_flock, funnel_state); | |
853 | ||
854 | ptmx_free_ioctl(minor(dev), PF_OPEN_M); | |
855 | ||
856 | return (0); | |
857 | } | |
858 | ||
859 | FREE_BSDSTATIC int | |
860 | ptmx_read(dev_t dev, struct uio *uio, int flag) | |
861 | { | |
862 | struct tty *tp; | |
863 | struct ptmx_ioctl *pti; | |
864 | char buf[BUFSIZ]; | |
865 | int error = 0, cc; | |
866 | boolean_t funnel_state; | |
867 | ||
868 | pti = ptmx_get_ioctl(minor(dev), 0); | |
869 | #if 5161374 | |
870 | if (pti == NULL || pti->pt_tty == NULL) | |
871 | return(ENXIO); | |
872 | #endif /* 5161374 */ | |
873 | tp = pti->pt_tty; | |
874 | ||
875 | funnel_state = thread_funnel_set(kernel_flock, TRUE); | |
876 | ||
877 | /* | |
878 | * We want to block until the slave | |
879 | * is open, and there's something to read; | |
880 | * but if we lost the slave or we're NBIO, | |
881 | * then return the appropriate error instead. | |
882 | */ | |
883 | for (;;) { | |
884 | if (tp->t_state&TS_ISOPEN) { | |
885 | if (pti->pt_flags & PF_PKT && pti->pt_send) { | |
886 | error = ureadc((int)pti->pt_send, uio); | |
887 | if (error) | |
888 | goto out; | |
889 | if (pti->pt_send & TIOCPKT_IOCTL) { | |
890 | cc = min(uio_resid(uio), | |
891 | sizeof(tp->t_termios)); | |
892 | uiomove((caddr_t)&tp->t_termios, cc, | |
893 | uio); | |
894 | } | |
895 | pti->pt_send = 0; | |
896 | goto out; | |
897 | } | |
898 | if (pti->pt_flags & PF_UCNTL && pti->pt_ucntl) { | |
899 | error = ureadc((int)pti->pt_ucntl, uio); | |
900 | if (error) | |
901 | goto out; | |
902 | pti->pt_ucntl = 0; | |
903 | goto out; | |
904 | } | |
905 | if (tp->t_outq.c_cc && (tp->t_state&TS_TTSTOP) == 0) | |
906 | break; | |
907 | } | |
908 | if ((tp->t_state & TS_CONNECTED) == 0) | |
909 | goto out; /* EOF */ | |
910 | if (flag & IO_NDELAY) { | |
911 | error = EWOULDBLOCK; | |
912 | goto out; | |
913 | } | |
914 | error = tsleep(TSA_PTC_READ(tp), TTIPRI | PCATCH, "ptmx_in", 0); | |
915 | if (error) | |
916 | goto out; | |
917 | } | |
918 | if (pti->pt_flags & (PF_PKT|PF_UCNTL)) | |
919 | error = ureadc(0, uio); | |
920 | while (uio_resid(uio) > 0 && error == 0) { | |
921 | cc = q_to_b(&tp->t_outq, (u_char *)buf, min(uio_resid(uio), BUFSIZ)); | |
922 | if (cc <= 0) | |
923 | break; | |
924 | error = uiomove(buf, cc, uio); | |
925 | } | |
926 | (*linesw[tp->t_line].l_start)(tp); | |
927 | ||
928 | out: | |
929 | (void) thread_funnel_set(kernel_flock, funnel_state); | |
930 | return (error); | |
931 | } | |
932 | ||
933 | FREE_BSDSTATIC int | |
934 | ptsd_stop(struct tty *tp, int flush) | |
935 | { | |
936 | struct ptmx_ioctl *pti; | |
937 | int flag; | |
938 | boolean_t funnel_state; | |
939 | ||
940 | pti = ptmx_get_ioctl(minor(tp->t_dev), 0); | |
941 | #if 5161374 | |
942 | if (pti == NULL) | |
943 | return(ENXIO); | |
944 | #endif /* 5161374 */ | |
945 | ||
946 | funnel_state = thread_funnel_set(kernel_flock, TRUE); | |
947 | ||
948 | /* note: FLUSHREAD and FLUSHWRITE already ok */ | |
949 | if (flush == 0) { | |
950 | flush = TIOCPKT_STOP; | |
951 | pti->pt_flags |= PF_STOPPED; | |
952 | } else | |
953 | pti->pt_flags &= ~PF_STOPPED; | |
954 | pti->pt_send |= flush; | |
955 | /* change of perspective */ | |
956 | flag = 0; | |
957 | if (flush & FREAD) | |
958 | flag |= FWRITE; | |
959 | if (flush & FWRITE) | |
960 | flag |= FREAD; | |
961 | ptmx_wakeup(tp, flag); | |
962 | ||
963 | (void) thread_funnel_set(kernel_flock, funnel_state); | |
964 | ||
965 | return (0); | |
966 | } | |
967 | ||
968 | FREE_BSDSTATIC int | |
969 | ptsd_reset(__unused int uban) | |
970 | { | |
971 | return (0); | |
972 | } | |
973 | ||
974 | /* | |
975 | * Reinput pending characters after state switch | |
976 | * call at spltty(). | |
977 | * | |
978 | * XXX Code duplication: static function, should be inlined | |
979 | */ | |
980 | static void | |
981 | ttypend(struct tty *tp) | |
982 | { | |
983 | struct clist tq; | |
984 | int c; | |
985 | ||
986 | CLR(tp->t_lflag, PENDIN); | |
987 | SET(tp->t_state, TS_TYPEN); | |
988 | tq = tp->t_rawq; | |
989 | tp->t_rawq.c_cc = 0; | |
990 | tp->t_rawq.c_cf = tp->t_rawq.c_cl = 0; | |
991 | while ((c = getc(&tq)) >= 0) | |
992 | ttyinput(c, tp); | |
993 | CLR(tp->t_state, TS_TYPEN); | |
994 | } | |
995 | ||
996 | /* | |
997 | * Must be called at spltty(). | |
998 | * | |
999 | * XXX Code duplication: static function, should be inlined | |
1000 | */ | |
1001 | static int | |
1002 | ttnread(struct tty *tp) | |
1003 | { | |
1004 | int nread; | |
1005 | ||
1006 | if (ISSET(tp->t_lflag, PENDIN)) | |
1007 | ttypend(tp); | |
1008 | nread = tp->t_canq.c_cc; | |
1009 | if (!ISSET(tp->t_lflag, ICANON)) { | |
1010 | nread += tp->t_rawq.c_cc; | |
1011 | if (nread < tp->t_cc[VMIN] && tp->t_cc[VTIME] == 0) | |
1012 | nread = 0; | |
1013 | } | |
1014 | return (nread); | |
1015 | } | |
1016 | ||
1017 | int | |
1018 | ptsd_select(dev_t dev, int rw, void *wql, proc_t p) | |
1019 | { | |
1020 | struct ptmx_ioctl *pti; | |
1021 | struct tty *tp; | |
1022 | ||
1023 | pti = ptmx_get_ioctl(minor(dev), 0); | |
1024 | #if 5161374 | |
1025 | if (pti == NULL || pti->pt_tty == NULL) | |
1026 | return(ENXIO); | |
1027 | #endif /* 5161374 */ | |
1028 | tp = pti->pt_tty; | |
1029 | ||
1030 | if (tp == NULL) | |
1031 | return (ENXIO); | |
1032 | ||
1033 | switch (rw) { | |
1034 | case FREAD: | |
1035 | if (ttnread(tp) > 0 || ISSET(tp->t_state, TS_ZOMBIE)) | |
1036 | return(1); | |
1037 | selrecord(p, &tp->t_rsel, wql); | |
1038 | break; | |
1039 | case FWRITE: | |
1040 | if ((tp->t_outq.c_cc <= tp->t_lowat && | |
1041 | ISSET(tp->t_state, TS_CONNECTED)) | |
1042 | || ISSET(tp->t_state, TS_ZOMBIE)) { | |
1043 | return (1); | |
1044 | } | |
1045 | selrecord(p, &tp->t_wsel, wql); | |
1046 | break; | |
1047 | } | |
1048 | return (0); | |
1049 | } | |
1050 | ||
1051 | FREE_BSDSTATIC int | |
1052 | ptmx_select(dev_t dev, int rw, void *wql, proc_t p) | |
1053 | { | |
1054 | struct tty *tp; | |
1055 | struct ptmx_ioctl *pti; | |
1056 | int retval = 0; | |
1057 | boolean_t funnel_state; | |
1058 | ||
1059 | pti = ptmx_get_ioctl(minor(dev), 0); | |
1060 | #if 5161374 | |
1061 | if (pti == NULL || pti->pt_tty == NULL) | |
1062 | return(ENXIO); | |
1063 | #endif /* 5161374 */ | |
1064 | tp = pti->pt_tty; | |
1065 | ||
1066 | funnel_state = thread_funnel_set(kernel_flock, TRUE); | |
1067 | ||
1068 | if ((tp->t_state & TS_CONNECTED) == 0) { | |
1069 | retval = 1; | |
1070 | goto out; | |
1071 | } | |
1072 | switch (rw) { | |
1073 | ||
1074 | case FREAD: | |
1075 | /* | |
1076 | * Need to block timeouts (ttrstart). | |
1077 | */ | |
1078 | if ((tp->t_state&TS_ISOPEN) && | |
1079 | tp->t_outq.c_cc && (tp->t_state&TS_TTSTOP) == 0) { | |
1080 | retval = 1; | |
1081 | goto out; | |
1082 | } | |
1083 | /* FALLTHROUGH */ | |
1084 | ||
1085 | case 0: /* exceptional */ | |
1086 | if ((tp->t_state&TS_ISOPEN) && | |
1087 | ((pti->pt_flags & PF_PKT && pti->pt_send) || | |
1088 | (pti->pt_flags & PF_UCNTL && pti->pt_ucntl))) { | |
1089 | retval = 1; | |
1090 | goto out; | |
1091 | } | |
1092 | selrecord(p, &pti->pt_selr, wql); | |
1093 | break; | |
1094 | ||
1095 | ||
1096 | case FWRITE: | |
1097 | if (tp->t_state&TS_ISOPEN) { | |
1098 | if (pti->pt_flags & PF_REMOTE) { | |
1099 | if (tp->t_canq.c_cc == 0) { | |
1100 | retval = 1; | |
1101 | goto out; | |
1102 | } | |
1103 | } else { | |
1104 | if (tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG-2) { | |
1105 | retval = 1; | |
1106 | goto out; | |
1107 | } | |
1108 | if (tp->t_canq.c_cc == 0 && (tp->t_lflag&ICANON)) { | |
1109 | retval = 1; | |
1110 | goto out; | |
1111 | } | |
1112 | } | |
1113 | } | |
1114 | selrecord(p, &pti->pt_selw, wql); | |
1115 | break; | |
1116 | ||
1117 | } | |
1118 | out: | |
1119 | (void) thread_funnel_set(kernel_flock, funnel_state); | |
1120 | return (retval); | |
1121 | } | |
1122 | ||
1123 | FREE_BSDSTATIC int | |
1124 | ptmx_stop(__unused struct tty *tp, __unused int flush) | |
1125 | { | |
1126 | return (0); | |
1127 | } | |
1128 | ||
1129 | FREE_BSDSTATIC int | |
1130 | ptmx_reset(__unused int uban) | |
1131 | { | |
1132 | return (0); | |
1133 | } | |
1134 | ||
1135 | FREE_BSDSTATIC int | |
1136 | ptmx_write(dev_t dev, struct uio *uio, int flag) | |
1137 | { | |
1138 | struct tty *tp; | |
1139 | struct ptmx_ioctl *pti; | |
1140 | u_char *cp = NULL; | |
1141 | int cc = 0; | |
1142 | u_char locbuf[BUFSIZ]; | |
1143 | int wcnt = 0; | |
1144 | int error = 0; | |
1145 | boolean_t funnel_state; | |
1146 | ||
1147 | pti = ptmx_get_ioctl(minor(dev), 0); | |
1148 | #if 5161374 | |
1149 | if (pti == NULL || pti->pt_tty == NULL) | |
1150 | return(ENXIO); | |
1151 | #endif /* 5161374 */ | |
1152 | tp = pti->pt_tty; | |
1153 | ||
1154 | funnel_state = thread_funnel_set(kernel_flock, TRUE); | |
1155 | ||
1156 | again: | |
1157 | if ((tp->t_state&TS_ISOPEN) == 0) | |
1158 | goto block; | |
1159 | if (pti->pt_flags & PF_REMOTE) { | |
1160 | if (tp->t_canq.c_cc) | |
1161 | goto block; | |
1162 | while ((uio_resid(uio) > 0 || cc > 0) && | |
1163 | tp->t_canq.c_cc < TTYHOG - 1) { | |
1164 | if (cc == 0) { | |
1165 | cc = min(uio_resid(uio), BUFSIZ); | |
1166 | cc = min(cc, TTYHOG - 1 - tp->t_canq.c_cc); | |
1167 | cp = locbuf; | |
1168 | error = uiomove((caddr_t)cp, cc, uio); | |
1169 | if (error) | |
1170 | goto out; | |
1171 | /* check again for safety */ | |
1172 | if ((tp->t_state & TS_ISOPEN) == 0) { | |
1173 | /* adjust as usual */ | |
1174 | uio_setresid(uio, (uio_resid(uio) + cc)); | |
1175 | error = EIO; | |
1176 | goto out; | |
1177 | } | |
1178 | } | |
1179 | if (cc > 0) { | |
1180 | cc = b_to_q((u_char *)cp, cc, &tp->t_canq); | |
1181 | /* | |
1182 | * XXX we don't guarantee that the canq size | |
1183 | * is >= TTYHOG, so the above b_to_q() may | |
1184 | * leave some bytes uncopied. However, space | |
1185 | * is guaranteed for the null terminator if | |
1186 | * we don't fail here since (TTYHOG - 1) is | |
1187 | * not a multiple of CBSIZE. | |
1188 | */ | |
1189 | if (cc > 0) | |
1190 | break; | |
1191 | } | |
1192 | } | |
1193 | /* adjust for data copied in but not written */ | |
1194 | uio_setresid(uio, (uio_resid(uio) + cc)); | |
1195 | (void) putc(0, &tp->t_canq); | |
1196 | ttwakeup(tp); | |
1197 | wakeup(TSA_PTS_READ(tp)); | |
1198 | goto out; | |
1199 | } | |
1200 | while (uio_resid(uio) > 0 || cc > 0) { | |
1201 | if (cc == 0) { | |
1202 | cc = min(uio_resid(uio), BUFSIZ); | |
1203 | cp = locbuf; | |
1204 | error = uiomove((caddr_t)cp, cc, uio); | |
1205 | if (error) | |
1206 | goto out; | |
1207 | /* check again for safety */ | |
1208 | if ((tp->t_state & TS_ISOPEN) == 0) { | |
1209 | /* adjust for data copied in but not written */ | |
1210 | uio_setresid(uio, (uio_resid(uio) + cc)); | |
1211 | error = EIO; | |
1212 | goto out; | |
1213 | } | |
1214 | } | |
1215 | while (cc > 0) { | |
1216 | if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 && | |
1217 | (tp->t_canq.c_cc > 0 || !(tp->t_lflag&ICANON))) { | |
1218 | wakeup(TSA_HUP_OR_INPUT(tp)); | |
1219 | goto block; | |
1220 | } | |
1221 | (*linesw[tp->t_line].l_rint)(*cp++, tp); | |
1222 | wcnt++; | |
1223 | cc--; | |
1224 | } | |
1225 | cc = 0; | |
1226 | } | |
1227 | out: | |
1228 | (void) thread_funnel_set(kernel_flock, funnel_state); | |
1229 | return (error); | |
1230 | block: | |
1231 | /* | |
1232 | * Come here to wait for slave to open, for space | |
1233 | * in outq, or space in rawq, or an empty canq. | |
1234 | */ | |
1235 | if ((tp->t_state & TS_CONNECTED) == 0) { | |
1236 | /* adjust for data copied in but not written */ | |
1237 | uio_setresid(uio, (uio_resid(uio) + cc)); | |
1238 | error = EIO; | |
1239 | goto out; | |
1240 | } | |
1241 | if (flag & IO_NDELAY) { | |
1242 | /* adjust for data copied in but not written */ | |
1243 | uio_setresid(uio, (uio_resid(uio) + cc)); | |
1244 | if (wcnt == 0) | |
1245 | error = EWOULDBLOCK; | |
1246 | goto out; | |
1247 | } | |
1248 | error = tsleep(TSA_PTC_WRITE(tp), TTOPRI | PCATCH, "ptmx_out", 0); | |
1249 | if (error) { | |
1250 | /* adjust for data copied in but not written */ | |
1251 | uio_setresid(uio, (uio_resid(uio) + cc)); | |
1252 | goto out; | |
1253 | } | |
1254 | goto again; | |
1255 | } | |
1256 | ||
1257 | ||
1258 | FREE_BSDSTATIC int | |
1259 | cptyioctl(dev_t dev, u_long cmd, caddr_t data, int flag, proc_t p) | |
1260 | { | |
1261 | struct tty *tp; | |
1262 | struct ptmx_ioctl *pti; | |
1263 | u_char *cc; | |
1264 | int stop, error = 0; | |
1265 | boolean_t funnel_state; | |
1266 | ||
1267 | pti = ptmx_get_ioctl(minor(dev), 0); | |
1268 | #if 5161374 | |
1269 | if (pti == NULL || pti->pt_tty == NULL) | |
1270 | return(ENXIO); | |
1271 | #endif /* 5161374 */ | |
1272 | tp = pti->pt_tty; | |
1273 | cc = tp->t_cc; | |
1274 | ||
1275 | funnel_state = thread_funnel_set(kernel_flock, TRUE); | |
1276 | ||
1277 | /* | |
1278 | * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG. | |
1279 | * ttywflush(tp) will hang if there are characters in the outq. | |
1280 | */ | |
1281 | if (cmd == TIOCEXT) { | |
1282 | /* | |
1283 | * When the EXTPROC bit is being toggled, we need | |
1284 | * to send an TIOCPKT_IOCTL if the packet driver | |
1285 | * is turned on. | |
1286 | */ | |
1287 | if (*(int *)data) { | |
1288 | if (pti->pt_flags & PF_PKT) { | |
1289 | pti->pt_send |= TIOCPKT_IOCTL; | |
1290 | ptmx_wakeup(tp, FREAD); | |
1291 | } | |
1292 | tp->t_lflag |= EXTPROC; | |
1293 | } else { | |
1294 | if ((tp->t_lflag & EXTPROC) && | |
1295 | (pti->pt_flags & PF_PKT)) { | |
1296 | pti->pt_send |= TIOCPKT_IOCTL; | |
1297 | ptmx_wakeup(tp, FREAD); | |
1298 | } | |
1299 | tp->t_lflag &= ~EXTPROC; | |
1300 | } | |
1301 | goto out; | |
1302 | } else | |
1303 | if (cdevsw[major(dev)].d_open == ptmx_open) | |
1304 | switch (cmd) { | |
1305 | ||
1306 | case TIOCGPGRP: | |
1307 | /* | |
1308 | * We aviod calling ttioctl on the controller since, | |
1309 | * in that case, tp must be the controlling terminal. | |
1310 | */ | |
1311 | *(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0; | |
1312 | goto out; | |
1313 | ||
1314 | case TIOCPKT: | |
1315 | if (*(int *)data) { | |
1316 | if (pti->pt_flags & PF_UCNTL) { | |
1317 | error = EINVAL; | |
1318 | goto out; | |
1319 | } | |
1320 | pti->pt_flags |= PF_PKT; | |
1321 | } else | |
1322 | pti->pt_flags &= ~PF_PKT; | |
1323 | goto out; | |
1324 | ||
1325 | case TIOCUCNTL: | |
1326 | if (*(int *)data) { | |
1327 | if (pti->pt_flags & PF_PKT) { | |
1328 | error = EINVAL; | |
1329 | goto out; | |
1330 | } | |
1331 | pti->pt_flags |= PF_UCNTL; | |
1332 | } else | |
1333 | pti->pt_flags &= ~PF_UCNTL; | |
1334 | goto out; | |
1335 | ||
1336 | case TIOCREMOTE: | |
1337 | if (*(int *)data) | |
1338 | pti->pt_flags |= PF_REMOTE; | |
1339 | else | |
1340 | pti->pt_flags &= ~PF_REMOTE; | |
1341 | ttyflush(tp, FREAD|FWRITE); | |
1342 | goto out; | |
1343 | ||
1344 | #if COMPAT_43_TTY | |
1345 | case TIOCSETP: | |
1346 | case TIOCSETN: | |
1347 | #endif | |
1348 | case TIOCSETD: | |
1349 | case TIOCSETA: | |
1350 | case TIOCSETAW: | |
1351 | case TIOCSETAF: | |
1352 | ndflush(&tp->t_outq, tp->t_outq.c_cc); | |
1353 | break; | |
1354 | ||
1355 | case TIOCSIG: | |
1356 | if (*(unsigned int *)data >= NSIG || | |
1357 | *(unsigned int *)data == 0) { | |
1358 | error = EINVAL; | |
1359 | goto out; | |
1360 | } | |
1361 | if ((tp->t_lflag&NOFLSH) == 0) | |
1362 | ttyflush(tp, FREAD|FWRITE); | |
1363 | tty_pgsignal(tp, *(unsigned int *)data, 1); | |
1364 | if ((*(unsigned int *)data == SIGINFO) && | |
1365 | ((tp->t_lflag&NOKERNINFO) == 0)) | |
1366 | ttyinfo(tp); | |
1367 | goto out; | |
1368 | ||
1369 | case TIOCPTYGRANT: /* grantpt(3) */ | |
1370 | /* | |
1371 | * Change the uid of the slave to that of the calling | |
1372 | * thread, change the gid of the slave to GID_TTY, | |
1373 | * change the mode to 0620 (rw--w----). | |
1374 | */ | |
1375 | { | |
1376 | error = _devfs_setattr(pti->pt_devhandle, 0620, kauth_getuid(), GID_TTY); | |
1377 | goto out; | |
1378 | } | |
1379 | ||
1380 | case TIOCPTYGNAME: /* ptsname(3) */ | |
1381 | /* | |
1382 | * Report the name of the slave device in *data | |
1383 | * (128 bytes max.). Use the same template string | |
1384 | * used for calling devfs_make_node() to create it. | |
1385 | */ | |
1386 | snprintf(data, 128, "/dev/" PTSD_TEMPLATE, minor(dev)); | |
1387 | error = 0; | |
1388 | goto out; | |
1389 | ||
1390 | case TIOCPTYUNLK: /* unlockpt(3) */ | |
1391 | /* | |
1392 | * Unlock the slave device so that it can be opened. | |
1393 | */ | |
1394 | pti->pt_flags |= PF_UNLOCKED; | |
1395 | error = 0; | |
1396 | goto out; | |
1397 | } | |
1398 | error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p); | |
1399 | if (error == ENOTTY) { | |
1400 | error = ttioctl(tp, cmd, data, flag, p); | |
1401 | if (error == ENOTTY) { | |
1402 | if (pti->pt_flags & PF_UCNTL && (cmd & ~0xff) == UIOCCMD(0)) { | |
1403 | /* Process the UIOCMD ioctl group */ | |
1404 | if (cmd & 0xff) { | |
1405 | pti->pt_ucntl = (u_char)cmd; | |
1406 | ptmx_wakeup(tp, FREAD); | |
1407 | } | |
1408 | error = 0; | |
1409 | goto out; | |
1410 | } else if (cmd == TIOCSBRK || cmd == TIOCCBRK) { | |
1411 | /* | |
1412 | * POSIX conformance; rdar://3936338 | |
1413 | * | |
1414 | * Clear ENOTTY in the case of setting or | |
1415 | * clearing a break failing because pty's | |
1416 | * don't support break like real serial | |
1417 | * ports. | |
1418 | */ | |
1419 | error = 0; | |
1420 | goto out; | |
1421 | } | |
1422 | } | |
1423 | } | |
1424 | ||
1425 | /* | |
1426 | * If external processing and packet mode send ioctl packet. | |
1427 | */ | |
1428 | if ((tp->t_lflag&EXTPROC) && (pti->pt_flags & PF_PKT)) { | |
1429 | switch(cmd) { | |
1430 | case TIOCSETA: | |
1431 | case TIOCSETAW: | |
1432 | case TIOCSETAF: | |
1433 | #if COMPAT_43_TTY | |
1434 | case TIOCSETP: | |
1435 | case TIOCSETN: | |
1436 | #endif | |
1437 | #if COMPAT_43_TTY || defined(COMPAT_SUNOS) | |
1438 | case TIOCSETC: | |
1439 | case TIOCSLTC: | |
1440 | case TIOCLBIS: | |
1441 | case TIOCLBIC: | |
1442 | case TIOCLSET: | |
1443 | #endif | |
1444 | pti->pt_send |= TIOCPKT_IOCTL; | |
1445 | ptmx_wakeup(tp, FREAD); | |
1446 | default: | |
1447 | break; | |
1448 | } | |
1449 | } | |
1450 | stop = (tp->t_iflag & IXON) && CCEQ(cc[VSTOP], CTRL('s')) | |
1451 | && CCEQ(cc[VSTART], CTRL('q')); | |
1452 | if (pti->pt_flags & PF_NOSTOP) { | |
1453 | if (stop) { | |
1454 | pti->pt_send &= ~TIOCPKT_NOSTOP; | |
1455 | pti->pt_send |= TIOCPKT_DOSTOP; | |
1456 | pti->pt_flags &= ~PF_NOSTOP; | |
1457 | ptmx_wakeup(tp, FREAD); | |
1458 | } | |
1459 | } else { | |
1460 | if (!stop) { | |
1461 | pti->pt_send &= ~TIOCPKT_DOSTOP; | |
1462 | pti->pt_send |= TIOCPKT_NOSTOP; | |
1463 | pti->pt_flags |= PF_NOSTOP; | |
1464 | ptmx_wakeup(tp, FREAD); | |
1465 | } | |
1466 | } | |
1467 | out: | |
1468 | (void) thread_funnel_set(kernel_flock, funnel_state); | |
1469 | return (error); | |
1470 | } |