tty_pty.c revision 1.70.2.3 1 1.70.2.3 skrll /* $NetBSD: tty_pty.c,v 1.70.2.3 2004/09/18 14:53:04 skrll Exp $ */
2 1.23 cgd
3 1.1 cgd /*
4 1.22 cgd * Copyright (c) 1982, 1986, 1989, 1993
5 1.22 cgd * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.70.2.2 skrll * 3. Neither the name of the University nor the names of its contributors
16 1.1 cgd * may be used to endorse or promote products derived from this software
17 1.1 cgd * without specific prior written permission.
18 1.1 cgd *
19 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 cgd * SUCH DAMAGE.
30 1.1 cgd *
31 1.39 fvdl * @(#)tty_pty.c 8.4 (Berkeley) 2/20/95
32 1.1 cgd */
33 1.1 cgd
34 1.1 cgd /*
35 1.1 cgd * Pseudo-teletype Driver
36 1.1 cgd * (Actually two drivers, requiring two entries in 'cdevsw')
37 1.70.2.2 skrll * Additional multiplexor driver /dev/ptm{,x}
38 1.1 cgd */
39 1.57 lukem
40 1.57 lukem #include <sys/cdefs.h>
41 1.70.2.3 skrll __KERNEL_RCSID(0, "$NetBSD: tty_pty.c,v 1.70.2.3 2004/09/18 14:53:04 skrll Exp $");
42 1.41 thorpej
43 1.41 thorpej #include "opt_compat_sunos.h"
44 1.70.2.2 skrll #include "opt_ptm.h"
45 1.41 thorpej
46 1.16 mycroft #include <sys/param.h>
47 1.16 mycroft #include <sys/systm.h>
48 1.16 mycroft #include <sys/ioctl.h>
49 1.22 cgd #include <sys/proc.h>
50 1.16 mycroft #include <sys/tty.h>
51 1.70.2.2 skrll #include <sys/stat.h>
52 1.16 mycroft #include <sys/file.h>
53 1.16 mycroft #include <sys/uio.h>
54 1.16 mycroft #include <sys/kernel.h>
55 1.16 mycroft #include <sys/vnode.h>
56 1.70.2.2 skrll #include <sys/namei.h>
57 1.31 christos #include <sys/signalvar.h>
58 1.31 christos #include <sys/uio.h>
59 1.70.2.2 skrll #include <sys/filedesc.h>
60 1.33 christos #include <sys/conf.h>
61 1.38 mycroft #include <sys/poll.h>
62 1.47 jdolecek #include <sys/malloc.h>
63 1.31 christos
64 1.47 jdolecek #define DEFAULT_NPTYS 16 /* default number of initial ptys */
65 1.58 tls #define DEFAULT_MAXPTYS 992 /* default maximum number of ptys */
66 1.1 cgd
67 1.70.2.2 skrll #ifdef DEBUG_PTM
68 1.70.2.2 skrll #define DPRINTF(a) uprintf a
69 1.70.2.2 skrll #else
70 1.70.2.2 skrll #define DPRINTF(a)
71 1.70.2.2 skrll #endif
72 1.70.2.2 skrll
73 1.37 mycroft /* Macros to clear/set/test flags. */
74 1.37 mycroft #define SET(t, f) (t) |= (f)
75 1.37 mycroft #define CLR(t, f) (t) &= ~((unsigned)(f))
76 1.37 mycroft #define ISSET(t, f) ((t) & (f))
77 1.37 mycroft
78 1.1 cgd #define BUFSIZ 100 /* Chunk size iomoved to/from user */
79 1.1 cgd
80 1.1 cgd /*
81 1.1 cgd * pts == /dev/tty[pqrs]?
82 1.1 cgd * ptc == /dev/pty[pqrs]?
83 1.1 cgd */
84 1.70.2.2 skrll
85 1.70.2.2 skrll #define TTY_TEMPLATE "/dev/XtyXX"
86 1.70.2.2 skrll #define TTY_NAMESIZE sizeof(TTY_TEMPLATE)
87 1.70.2.2 skrll /* XXX this needs to come from somewhere sane, and work with MAKEDEV */
88 1.70.2.2 skrll #define TTY_LETTERS "pqrstuvwxyzPQRST"
89 1.70.2.2 skrll #define TTY_OLD_SUFFIX "0123456789abcdef"
90 1.70.2.2 skrll #define TTY_NEW_SUFFIX "ghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
91 1.70.2.2 skrll
92 1.27 mycroft struct pt_softc {
93 1.27 mycroft struct tty *pt_tty;
94 1.1 cgd int pt_flags;
95 1.22 cgd struct selinfo pt_selr, pt_selw;
96 1.1 cgd u_char pt_send;
97 1.1 cgd u_char pt_ucntl;
98 1.47 jdolecek };
99 1.47 jdolecek
100 1.48 jdolecek static struct pt_softc **pt_softc = NULL; /* pty array */
101 1.48 jdolecek static int npty = 0; /* for pstat -t */
102 1.48 jdolecek static int maxptys = DEFAULT_MAXPTYS; /* maximum number of ptys (sysctable) */
103 1.48 jdolecek static struct simplelock pt_softc_mutex = SIMPLELOCK_INITIALIZER;
104 1.1 cgd
105 1.1 cgd #define PF_PKT 0x08 /* packet mode */
106 1.1 cgd #define PF_STOPPED 0x10 /* user told stopped */
107 1.1 cgd #define PF_REMOTE 0x20 /* remote and flow controlled input */
108 1.1 cgd #define PF_NOSTOP 0x40
109 1.1 cgd #define PF_UCNTL 0x80 /* user control mode */
110 1.1 cgd
111 1.70.2.2 skrll void ptyattach(int);
112 1.70.2.2 skrll void ptcwakeup(struct tty *, int);
113 1.70.2.2 skrll void ptsstart(struct tty *);
114 1.70.2.2 skrll int pty_maxptys(int, int);
115 1.70.2.2 skrll
116 1.70.2.2 skrll static struct pt_softc **ptyarralloc(int);
117 1.70.2.2 skrll static int check_pty(int);
118 1.70.2.2 skrll
119 1.70.2.2 skrll #ifndef NO_DEV_PTM
120 1.70.2.2 skrll
121 1.70.2.2 skrll static int pts_major;
122 1.70.2.2 skrll
123 1.70.2.2 skrll static dev_t pty_getfree(void);
124 1.70.2.2 skrll static char *pty_makename(char *, dev_t, char);
125 1.70.2.3 skrll static int pty_grant_slave(struct proc *, dev_t);
126 1.70.2.3 skrll static int pty_alloc_master(struct proc *, int *, dev_t *);
127 1.70.2.3 skrll static int pty_alloc_slave(struct proc *, int *, dev_t);
128 1.70.2.2 skrll static void pty_fill_ptmget(dev_t, int, int, void *);
129 1.70.2.2 skrll
130 1.70.2.2 skrll void ptmattach(int);
131 1.70.2.2 skrll
132 1.70.2.2 skrll dev_type_open(ptmopen);
133 1.70.2.2 skrll dev_type_close(ptmclose);
134 1.70.2.2 skrll dev_type_ioctl(ptmioctl);
135 1.70.2.2 skrll
136 1.70.2.2 skrll const struct cdevsw ptm_cdevsw = {
137 1.70.2.2 skrll ptmopen, ptmclose, noread, nowrite, ptmioctl,
138 1.70.2.2 skrll nullstop, notty, nopoll, nommap, nokqfilter, D_TTY
139 1.70.2.2 skrll };
140 1.70.2.2 skrll #else
141 1.70.2.2 skrll const struct cdevsw ptm_cdevsw = {
142 1.70.2.2 skrll noopen, noclose, noread, nowrite, noioctl,
143 1.70.2.2 skrll nostop, notty, nopoll, nommap, nokqfilter, D_TTY
144 1.70.2.2 skrll };
145 1.70.2.2 skrll #endif
146 1.47 jdolecek
147 1.63 gehenna dev_type_open(ptcopen);
148 1.63 gehenna dev_type_close(ptcclose);
149 1.63 gehenna dev_type_read(ptcread);
150 1.63 gehenna dev_type_write(ptcwrite);
151 1.63 gehenna dev_type_poll(ptcpoll);
152 1.65 jdolecek dev_type_kqfilter(ptckqfilter);
153 1.63 gehenna
154 1.63 gehenna dev_type_open(ptsopen);
155 1.63 gehenna dev_type_close(ptsclose);
156 1.63 gehenna dev_type_read(ptsread);
157 1.63 gehenna dev_type_write(ptswrite);
158 1.63 gehenna dev_type_stop(ptsstop);
159 1.63 gehenna dev_type_poll(ptspoll);
160 1.63 gehenna
161 1.63 gehenna dev_type_ioctl(ptyioctl);
162 1.63 gehenna dev_type_tty(ptytty);
163 1.63 gehenna
164 1.63 gehenna const struct cdevsw ptc_cdevsw = {
165 1.63 gehenna ptcopen, ptcclose, ptcread, ptcwrite, ptyioctl,
166 1.65 jdolecek nullstop, ptytty, ptcpoll, nommap, ptckqfilter, D_TTY
167 1.63 gehenna };
168 1.63 gehenna
169 1.63 gehenna const struct cdevsw pts_cdevsw = {
170 1.63 gehenna ptsopen, ptsclose, ptsread, ptswrite, ptyioctl,
171 1.65 jdolecek ptsstop, ptytty, ptspoll, nommap, ttykqfilter, D_TTY
172 1.63 gehenna };
173 1.63 gehenna
174 1.63 gehenna #if defined(pmax)
175 1.63 gehenna const struct cdevsw ptc_ultrix_cdevsw = {
176 1.63 gehenna ptcopen, ptcclose, ptcread, ptcwrite, ptyioctl,
177 1.65 jdolecek nullstop, ptytty, ptcpoll, nommap, ptckqfilter, D_TTY
178 1.63 gehenna };
179 1.63 gehenna
180 1.63 gehenna const struct cdevsw pts_ultrix_cdevsw = {
181 1.63 gehenna ptsopen, ptsclose, ptsread, ptswrite, ptyioctl,
182 1.65 jdolecek ptsstop, ptytty, ptspoll, nommap, ttykqfilter, D_TTY
183 1.63 gehenna };
184 1.63 gehenna #endif /* defined(pmax) */
185 1.63 gehenna
186 1.47 jdolecek /*
187 1.48 jdolecek * Allocate and zero array of nelem elements.
188 1.47 jdolecek */
189 1.47 jdolecek static struct pt_softc **
190 1.47 jdolecek ptyarralloc(nelem)
191 1.47 jdolecek int nelem;
192 1.47 jdolecek {
193 1.47 jdolecek struct pt_softc **pt;
194 1.47 jdolecek nelem += 10;
195 1.70.2.2 skrll pt = malloc(nelem * sizeof *pt, M_DEVBUF, M_WAITOK | M_ZERO);
196 1.47 jdolecek return pt;
197 1.47 jdolecek }
198 1.47 jdolecek
199 1.47 jdolecek /*
200 1.47 jdolecek * Check if the minor is correct and ensure necessary structures
201 1.47 jdolecek * are properly allocated.
202 1.47 jdolecek */
203 1.47 jdolecek static int
204 1.70.2.2 skrll check_pty(int ptn)
205 1.47 jdolecek {
206 1.47 jdolecek struct pt_softc *pti;
207 1.47 jdolecek
208 1.70.2.2 skrll if (ptn >= npty) {
209 1.70.2.2 skrll struct pt_softc **newpt, **oldpt;
210 1.48 jdolecek int newnpty;
211 1.47 jdolecek
212 1.48 jdolecek /* check if the requested pty can be granted */
213 1.70.2.2 skrll if (ptn >= maxptys) {
214 1.48 jdolecek limit_reached:
215 1.48 jdolecek tablefull("pty", "increase kern.maxptys");
216 1.48 jdolecek return (ENXIO);
217 1.48 jdolecek }
218 1.47 jdolecek
219 1.70.2.2 skrll /* Allocate a larger pty array */
220 1.70.2.2 skrll for (newnpty = npty; newnpty <= ptn;)
221 1.70.2.2 skrll newnpty *= 2;
222 1.70.2.2 skrll if (newnpty > maxptys)
223 1.70.2.2 skrll newnpty = maxptys;
224 1.70.2.2 skrll newpt = ptyarralloc(newnpty);
225 1.70.2.2 skrll
226 1.47 jdolecek /*
227 1.48 jdolecek * Now grab the pty array mutex - we need to ensure
228 1.47 jdolecek * that the pty array is consistent while copying it's
229 1.48 jdolecek * content to newly allocated, larger space; we also
230 1.48 jdolecek * need to be safe against pty_maxptys().
231 1.47 jdolecek */
232 1.48 jdolecek simple_lock(&pt_softc_mutex);
233 1.48 jdolecek
234 1.70.2.2 skrll if (newnpty >= maxptys) {
235 1.70.2.2 skrll /* limit cut away beneath us... */
236 1.70.2.2 skrll newnpty = maxptys;
237 1.70.2.2 skrll if (ptn >= newnpty) {
238 1.54 enami simple_unlock(&pt_softc_mutex);
239 1.70.2.2 skrll free(newpt, M_DEVBUF);
240 1.48 jdolecek goto limit_reached;
241 1.48 jdolecek }
242 1.70.2.2 skrll }
243 1.47 jdolecek
244 1.47 jdolecek /*
245 1.48 jdolecek * If the pty array was not enlarged while we were waiting
246 1.48 jdolecek * for mutex, copy current contents of pt_softc[] to newly
247 1.48 jdolecek * allocated array and start using the new bigger array.
248 1.47 jdolecek */
249 1.70.2.2 skrll if (newnpty > npty) {
250 1.47 jdolecek memcpy(newpt, pt_softc, npty*sizeof(struct pt_softc *));
251 1.70.2.2 skrll oldpt = pt_softc;
252 1.47 jdolecek pt_softc = newpt;
253 1.47 jdolecek npty = newnpty;
254 1.47 jdolecek } else {
255 1.70.2.2 skrll /* was enlarged when waited for lock, free new space */
256 1.70.2.2 skrll oldpt = newpt;
257 1.47 jdolecek }
258 1.48 jdolecek
259 1.48 jdolecek simple_unlock(&pt_softc_mutex);
260 1.70.2.2 skrll free(oldpt, M_DEVBUF);
261 1.47 jdolecek }
262 1.70.2.2 skrll
263 1.47 jdolecek /*
264 1.48 jdolecek * If the entry is not yet allocated, allocate one. The mutex is
265 1.48 jdolecek * needed so that the state of pt_softc[] array is consistant
266 1.70.2.2 skrll * in case it has been lengthened above.
267 1.47 jdolecek */
268 1.70.2.2 skrll if (!pt_softc[ptn]) {
269 1.48 jdolecek MALLOC(pti, struct pt_softc *, sizeof(struct pt_softc),
270 1.48 jdolecek M_DEVBUF, M_WAITOK);
271 1.64 jdolecek memset(pti, 0, sizeof(struct pt_softc));
272 1.48 jdolecek
273 1.48 jdolecek pti->pt_tty = ttymalloc();
274 1.48 jdolecek
275 1.48 jdolecek simple_lock(&pt_softc_mutex);
276 1.47 jdolecek
277 1.47 jdolecek /*
278 1.48 jdolecek * Check the entry again - it might have been
279 1.48 jdolecek * added while we were waiting for mutex.
280 1.47 jdolecek */
281 1.70.2.2 skrll if (!pt_softc[ptn]) {
282 1.47 jdolecek tty_attach(pti->pt_tty);
283 1.70.2.2 skrll pt_softc[ptn] = pti;
284 1.48 jdolecek } else {
285 1.48 jdolecek ttyfree(pti->pt_tty);
286 1.70.2.2 skrll free(pti, M_DEVBUF);
287 1.47 jdolecek }
288 1.48 jdolecek
289 1.48 jdolecek simple_unlock(&pt_softc_mutex);
290 1.48 jdolecek }
291 1.48 jdolecek
292 1.48 jdolecek return (0);
293 1.48 jdolecek }
294 1.48 jdolecek
295 1.48 jdolecek /*
296 1.48 jdolecek * Set maxpty in thread-safe way. Returns 0 in case of error, otherwise
297 1.48 jdolecek * new value of maxptys.
298 1.48 jdolecek */
299 1.48 jdolecek int
300 1.48 jdolecek pty_maxptys(newmax, set)
301 1.48 jdolecek int newmax, set;
302 1.48 jdolecek {
303 1.48 jdolecek if (!set)
304 1.48 jdolecek return (maxptys);
305 1.48 jdolecek
306 1.48 jdolecek /*
307 1.48 jdolecek * We have to grab the pt_softc lock, so that we would pick correct
308 1.48 jdolecek * value of npty (might be modified in check_pty()).
309 1.48 jdolecek */
310 1.48 jdolecek simple_lock(&pt_softc_mutex);
311 1.48 jdolecek
312 1.70.2.2 skrll /*
313 1.70.2.2 skrll * The value cannot be set to value lower than the highest pty
314 1.70.2.2 skrll * number ever allocated.
315 1.70.2.2 skrll */
316 1.70.2.2 skrll if (newmax >= npty)
317 1.48 jdolecek maxptys = newmax;
318 1.70.2.2 skrll else
319 1.70.2.2 skrll newmax = 0;
320 1.47 jdolecek
321 1.48 jdolecek simple_unlock(&pt_softc_mutex);
322 1.48 jdolecek
323 1.70.2.2 skrll return newmax;
324 1.47 jdolecek }
325 1.47 jdolecek
326 1.22 cgd /*
327 1.22 cgd * Establish n (or default if n is 1) ptys in the system.
328 1.22 cgd */
329 1.15 deraadt void
330 1.15 deraadt ptyattach(n)
331 1.15 deraadt int n;
332 1.15 deraadt {
333 1.22 cgd /* maybe should allow 0 => none? */
334 1.22 cgd if (n <= 1)
335 1.47 jdolecek n = DEFAULT_NPTYS;
336 1.47 jdolecek pt_softc = ptyarralloc(n);
337 1.22 cgd npty = n;
338 1.70.2.2 skrll #ifndef NO_DEV_PTM
339 1.70.2.2 skrll ptmattach(1);
340 1.70.2.2 skrll #endif
341 1.15 deraadt }
342 1.7 andrew
343 1.1 cgd /*ARGSUSED*/
344 1.31 christos int
345 1.70.2.3 skrll ptsopen(dev, flag, devtype, p)
346 1.1 cgd dev_t dev;
347 1.7 andrew int flag, devtype;
348 1.70.2.3 skrll struct proc *p;
349 1.1 cgd {
350 1.27 mycroft struct pt_softc *pti;
351 1.43 augustss struct tty *tp;
352 1.1 cgd int error;
353 1.70.2.2 skrll int ptn = minor(dev);
354 1.70.2.2 skrll int s;
355 1.1 cgd
356 1.70.2.2 skrll if ((error = check_pty(ptn)))
357 1.47 jdolecek return (error);
358 1.47 jdolecek
359 1.70.2.2 skrll pti = pt_softc[ptn];
360 1.47 jdolecek tp = pti->pt_tty;
361 1.47 jdolecek
362 1.37 mycroft if (!ISSET(tp->t_state, TS_ISOPEN)) {
363 1.1 cgd ttychars(tp); /* Set up default chars */
364 1.1 cgd tp->t_iflag = TTYDEF_IFLAG;
365 1.1 cgd tp->t_oflag = TTYDEF_OFLAG;
366 1.1 cgd tp->t_lflag = TTYDEF_LFLAG;
367 1.1 cgd tp->t_cflag = TTYDEF_CFLAG;
368 1.1 cgd tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
369 1.1 cgd ttsetwater(tp); /* would be done in xxparam() */
370 1.37 mycroft } else if (ISSET(tp->t_state, TS_XCLUDE) && p->p_ucred->cr_uid != 0)
371 1.1 cgd return (EBUSY);
372 1.1 cgd if (tp->t_oproc) /* Ctrlr still around. */
373 1.37 mycroft SET(tp->t_state, TS_CARR_ON);
374 1.70.2.2 skrll
375 1.68 pk if (!ISSET(flag, O_NONBLOCK)) {
376 1.70.2.2 skrll s = spltty();
377 1.68 pk TTY_LOCK(tp);
378 1.40 mycroft while (!ISSET(tp->t_state, TS_CARR_ON)) {
379 1.40 mycroft tp->t_wopen++;
380 1.40 mycroft error = ttysleep(tp, &tp->t_rawq, TTIPRI | PCATCH,
381 1.40 mycroft ttopen, 0);
382 1.40 mycroft tp->t_wopen--;
383 1.68 pk if (error) {
384 1.68 pk TTY_UNLOCK(tp);
385 1.70.2.2 skrll splx(s);
386 1.40 mycroft return (error);
387 1.68 pk }
388 1.40 mycroft }
389 1.68 pk TTY_UNLOCK(tp);
390 1.70.2.2 skrll splx(s);
391 1.68 pk }
392 1.50 eeh error = (*tp->t_linesw->l_open)(dev, tp);
393 1.1 cgd ptcwakeup(tp, FREAD|FWRITE);
394 1.22 cgd return (error);
395 1.1 cgd }
396 1.1 cgd
397 1.31 christos int
398 1.70.2.3 skrll ptsclose(dev, flag, mode, p)
399 1.1 cgd dev_t dev;
400 1.1 cgd int flag, mode;
401 1.70.2.3 skrll struct proc *p;
402 1.1 cgd {
403 1.47 jdolecek struct pt_softc *pti = pt_softc[minor(dev)];
404 1.43 augustss struct tty *tp = pti->pt_tty;
405 1.31 christos int error;
406 1.1 cgd
407 1.50 eeh error = (*tp->t_linesw->l_close)(tp, flag);
408 1.31 christos error |= ttyclose(tp);
409 1.1 cgd ptcwakeup(tp, FREAD|FWRITE);
410 1.31 christos return (error);
411 1.1 cgd }
412 1.1 cgd
413 1.31 christos int
414 1.1 cgd ptsread(dev, uio, flag)
415 1.1 cgd dev_t dev;
416 1.1 cgd struct uio *uio;
417 1.7 andrew int flag;
418 1.1 cgd {
419 1.1 cgd struct proc *p = curproc;
420 1.47 jdolecek struct pt_softc *pti = pt_softc[minor(dev)];
421 1.43 augustss struct tty *tp = pti->pt_tty;
422 1.1 cgd int error = 0;
423 1.68 pk int cc;
424 1.70.2.2 skrll int s;
425 1.1 cgd
426 1.1 cgd again:
427 1.1 cgd if (pti->pt_flags & PF_REMOTE) {
428 1.1 cgd while (isbackground(p, tp)) {
429 1.51 jdolecek if (sigismasked(p, SIGTTIN) ||
430 1.1 cgd p->p_pgrp->pg_jobc == 0 ||
431 1.22 cgd p->p_flag & P_PPWAIT)
432 1.1 cgd return (EIO);
433 1.1 cgd pgsignal(p->p_pgrp, SIGTTIN, 1);
434 1.70.2.2 skrll s = spltty();
435 1.68 pk TTY_LOCK(tp);
436 1.35 pk error = ttysleep(tp, (caddr_t)&lbolt,
437 1.68 pk TTIPRI | PCATCH | PNORELOCK, ttybg, 0);
438 1.70.2.2 skrll splx(s);
439 1.31 christos if (error)
440 1.1 cgd return (error);
441 1.1 cgd }
442 1.70.2.2 skrll s = spltty();
443 1.68 pk TTY_LOCK(tp);
444 1.8 mycroft if (tp->t_canq.c_cc == 0) {
445 1.68 pk if (flag & IO_NDELAY) {
446 1.68 pk TTY_UNLOCK(tp);
447 1.70.2.2 skrll splx(s);
448 1.1 cgd return (EWOULDBLOCK);
449 1.68 pk }
450 1.31 christos error = ttysleep(tp, (caddr_t)&tp->t_canq,
451 1.68 pk TTIPRI | PCATCH | PNORELOCK, ttyin, 0);
452 1.31 christos if (error)
453 1.1 cgd return (error);
454 1.1 cgd goto again;
455 1.1 cgd }
456 1.68 pk while(error == 0 && tp->t_canq.c_cc > 1 && uio->uio_resid > 0) {
457 1.68 pk TTY_UNLOCK(tp);
458 1.70.2.2 skrll splx(s);
459 1.68 pk error = ureadc(getc(&tp->t_canq), uio);
460 1.70.2.2 skrll s = spltty();
461 1.68 pk TTY_LOCK(tp);
462 1.68 pk /* Re-check terminal state here? */
463 1.68 pk }
464 1.8 mycroft if (tp->t_canq.c_cc == 1)
465 1.8 mycroft (void) getc(&tp->t_canq);
466 1.68 pk cc = tp->t_canq.c_cc;
467 1.68 pk TTY_UNLOCK(tp);
468 1.70.2.2 skrll splx(s);
469 1.68 pk if (cc)
470 1.1 cgd return (error);
471 1.1 cgd } else
472 1.1 cgd if (tp->t_oproc)
473 1.50 eeh error = (*tp->t_linesw->l_read)(tp, uio, flag);
474 1.1 cgd ptcwakeup(tp, FWRITE);
475 1.1 cgd return (error);
476 1.1 cgd }
477 1.1 cgd
478 1.1 cgd /*
479 1.1 cgd * Write to pseudo-tty.
480 1.1 cgd * Wakeups of controlling tty will happen
481 1.1 cgd * indirectly, when tty driver calls ptsstart.
482 1.1 cgd */
483 1.31 christos int
484 1.1 cgd ptswrite(dev, uio, flag)
485 1.1 cgd dev_t dev;
486 1.1 cgd struct uio *uio;
487 1.7 andrew int flag;
488 1.1 cgd {
489 1.47 jdolecek struct pt_softc *pti = pt_softc[minor(dev)];
490 1.43 augustss struct tty *tp = pti->pt_tty;
491 1.1 cgd
492 1.1 cgd if (tp->t_oproc == 0)
493 1.1 cgd return (EIO);
494 1.50 eeh return ((*tp->t_linesw->l_write)(tp, uio, flag));
495 1.56 scw }
496 1.56 scw
497 1.56 scw /*
498 1.56 scw * Poll pseudo-tty.
499 1.56 scw */
500 1.56 scw int
501 1.70.2.3 skrll ptspoll(dev, events, p)
502 1.56 scw dev_t dev;
503 1.56 scw int events;
504 1.70.2.3 skrll struct proc *p;
505 1.56 scw {
506 1.56 scw struct pt_softc *pti = pt_softc[minor(dev)];
507 1.56 scw struct tty *tp = pti->pt_tty;
508 1.56 scw
509 1.56 scw if (tp->t_oproc == 0)
510 1.56 scw return (EIO);
511 1.56 scw
512 1.70.2.3 skrll return ((*tp->t_linesw->l_poll)(tp, events, p));
513 1.1 cgd }
514 1.1 cgd
515 1.1 cgd /*
516 1.1 cgd * Start output on pseudo-tty.
517 1.38 mycroft * Wake up process polling or sleeping for input from controlling tty.
518 1.68 pk * Called with tty slock held.
519 1.1 cgd */
520 1.12 deraadt void
521 1.1 cgd ptsstart(tp)
522 1.1 cgd struct tty *tp;
523 1.1 cgd {
524 1.47 jdolecek struct pt_softc *pti = pt_softc[minor(tp->t_dev)];
525 1.1 cgd
526 1.37 mycroft if (ISSET(tp->t_state, TS_TTSTOP))
527 1.12 deraadt return;
528 1.1 cgd if (pti->pt_flags & PF_STOPPED) {
529 1.1 cgd pti->pt_flags &= ~PF_STOPPED;
530 1.1 cgd pti->pt_send = TIOCPKT_START;
531 1.1 cgd }
532 1.68 pk
533 1.70.2.2 skrll selnotify(&pti->pt_selr, NOTE_SUBMIT);
534 1.68 pk wakeup((caddr_t)&tp->t_outq.c_cf);
535 1.1 cgd }
536 1.1 cgd
537 1.68 pk /*
538 1.68 pk * Stop output.
539 1.68 pk * Called with tty slock held.
540 1.68 pk */
541 1.36 mycroft void
542 1.31 christos ptsstop(tp, flush)
543 1.43 augustss struct tty *tp;
544 1.31 christos int flush;
545 1.31 christos {
546 1.47 jdolecek struct pt_softc *pti = pt_softc[minor(tp->t_dev)];
547 1.31 christos
548 1.31 christos /* note: FLUSHREAD and FLUSHWRITE already ok */
549 1.31 christos if (flush == 0) {
550 1.31 christos flush = TIOCPKT_STOP;
551 1.31 christos pti->pt_flags |= PF_STOPPED;
552 1.31 christos } else
553 1.31 christos pti->pt_flags &= ~PF_STOPPED;
554 1.31 christos pti->pt_send |= flush;
555 1.68 pk
556 1.31 christos /* change of perspective */
557 1.68 pk if (flush & FREAD) {
558 1.70.2.2 skrll selnotify(&pti->pt_selw, NOTE_SUBMIT);
559 1.68 pk wakeup((caddr_t)&tp->t_rawq.c_cf);
560 1.68 pk }
561 1.68 pk if (flush & FWRITE) {
562 1.70.2.2 skrll selnotify(&pti->pt_selr, NOTE_SUBMIT);
563 1.68 pk wakeup((caddr_t)&tp->t_outq.c_cf);
564 1.68 pk }
565 1.31 christos }
566 1.31 christos
567 1.31 christos void
568 1.1 cgd ptcwakeup(tp, flag)
569 1.1 cgd struct tty *tp;
570 1.7 andrew int flag;
571 1.1 cgd {
572 1.47 jdolecek struct pt_softc *pti = pt_softc[minor(tp->t_dev)];
573 1.70.2.2 skrll int s;
574 1.1 cgd
575 1.70.2.2 skrll s = spltty();
576 1.68 pk TTY_LOCK(tp);
577 1.1 cgd if (flag & FREAD) {
578 1.70.2.2 skrll selnotify(&pti->pt_selr, NOTE_SUBMIT);
579 1.22 cgd wakeup((caddr_t)&tp->t_outq.c_cf);
580 1.1 cgd }
581 1.1 cgd if (flag & FWRITE) {
582 1.70.2.2 skrll selnotify(&pti->pt_selw, NOTE_SUBMIT);
583 1.8 mycroft wakeup((caddr_t)&tp->t_rawq.c_cf);
584 1.1 cgd }
585 1.68 pk TTY_UNLOCK(tp);
586 1.70.2.2 skrll splx(s);
587 1.1 cgd }
588 1.1 cgd
589 1.1 cgd /*ARGSUSED*/
590 1.25 mycroft int
591 1.70.2.3 skrll ptcopen(dev, flag, devtype, p)
592 1.1 cgd dev_t dev;
593 1.1 cgd int flag, devtype;
594 1.70.2.3 skrll struct proc *p;
595 1.1 cgd {
596 1.27 mycroft struct pt_softc *pti;
597 1.43 augustss struct tty *tp;
598 1.47 jdolecek int error;
599 1.70.2.2 skrll int ptn = minor(dev);
600 1.70.2.2 skrll int s;
601 1.47 jdolecek
602 1.70.2.2 skrll if ((error = check_pty(ptn)))
603 1.47 jdolecek return (error);
604 1.47 jdolecek
605 1.70.2.2 skrll pti = pt_softc[ptn];
606 1.47 jdolecek tp = pti->pt_tty;
607 1.1 cgd
608 1.70.2.2 skrll s = spltty();
609 1.70.2.2 skrll TTY_LOCK(tp);
610 1.70.2.2 skrll if (tp->t_oproc) {
611 1.70.2.2 skrll TTY_UNLOCK(tp);
612 1.70.2.2 skrll splx(s);
613 1.1 cgd return (EIO);
614 1.70.2.2 skrll }
615 1.1 cgd tp->t_oproc = ptsstart;
616 1.70.2.2 skrll TTY_UNLOCK(tp);
617 1.70.2.2 skrll splx(s);
618 1.50 eeh (void)(*tp->t_linesw->l_modem)(tp, 1);
619 1.37 mycroft CLR(tp->t_lflag, EXTPROC);
620 1.22 cgd pti->pt_flags = 0;
621 1.1 cgd pti->pt_send = 0;
622 1.1 cgd pti->pt_ucntl = 0;
623 1.1 cgd return (0);
624 1.1 cgd }
625 1.1 cgd
626 1.31 christos /*ARGSUSED*/
627 1.25 mycroft int
628 1.70.2.3 skrll ptcclose(dev, flag, devtype, p)
629 1.1 cgd dev_t dev;
630 1.31 christos int flag, devtype;
631 1.70.2.3 skrll struct proc *p;
632 1.1 cgd {
633 1.47 jdolecek struct pt_softc *pti = pt_softc[minor(dev)];
634 1.43 augustss struct tty *tp = pti->pt_tty;
635 1.70.2.2 skrll int s;
636 1.1 cgd
637 1.50 eeh (void)(*tp->t_linesw->l_modem)(tp, 0);
638 1.37 mycroft CLR(tp->t_state, TS_CARR_ON);
639 1.70.2.2 skrll s = spltty();
640 1.70.2.2 skrll TTY_LOCK(tp);
641 1.1 cgd tp->t_oproc = 0; /* mark closed */
642 1.70.2.2 skrll TTY_UNLOCK(tp);
643 1.70.2.2 skrll splx(s);
644 1.2 cgd return (0);
645 1.1 cgd }
646 1.1 cgd
647 1.31 christos int
648 1.1 cgd ptcread(dev, uio, flag)
649 1.1 cgd dev_t dev;
650 1.1 cgd struct uio *uio;
651 1.7 andrew int flag;
652 1.1 cgd {
653 1.47 jdolecek struct pt_softc *pti = pt_softc[minor(dev)];
654 1.43 augustss struct tty *tp = pti->pt_tty;
655 1.67 simonb u_char buf[BUFSIZ];
656 1.1 cgd int error = 0, cc;
657 1.70.2.2 skrll int s;
658 1.1 cgd
659 1.1 cgd /*
660 1.1 cgd * We want to block until the slave
661 1.1 cgd * is open, and there's something to read;
662 1.1 cgd * but if we lost the slave or we're NBIO,
663 1.1 cgd * then return the appropriate error instead.
664 1.1 cgd */
665 1.70.2.2 skrll s = spltty();
666 1.68 pk TTY_LOCK(tp);
667 1.1 cgd for (;;) {
668 1.37 mycroft if (ISSET(tp->t_state, TS_ISOPEN)) {
669 1.70.2.2 skrll if (pti->pt_flags & PF_PKT && pti->pt_send) {
670 1.68 pk TTY_UNLOCK(tp);
671 1.70.2.2 skrll splx(s);
672 1.1 cgd error = ureadc((int)pti->pt_send, uio);
673 1.1 cgd if (error)
674 1.1 cgd return (error);
675 1.68 pk /*
676 1.68 pk * Since we don't have the tty locked, there's
677 1.68 pk * a risk of messing up `t_termios'. This is
678 1.68 pk * relevant only if the tty got closed and then
679 1.68 pk * opened again while we were out uiomoving.
680 1.68 pk */
681 1.1 cgd if (pti->pt_send & TIOCPKT_IOCTL) {
682 1.22 cgd cc = min(uio->uio_resid,
683 1.1 cgd sizeof(tp->t_termios));
684 1.31 christos uiomove((caddr_t) &tp->t_termios,
685 1.31 christos cc, uio);
686 1.1 cgd }
687 1.1 cgd pti->pt_send = 0;
688 1.1 cgd return (0);
689 1.1 cgd }
690 1.70.2.2 skrll if (pti->pt_flags & PF_UCNTL && pti->pt_ucntl) {
691 1.68 pk TTY_UNLOCK(tp);
692 1.70.2.2 skrll splx(s);
693 1.1 cgd error = ureadc((int)pti->pt_ucntl, uio);
694 1.1 cgd if (error)
695 1.1 cgd return (error);
696 1.1 cgd pti->pt_ucntl = 0;
697 1.1 cgd return (0);
698 1.1 cgd }
699 1.37 mycroft if (tp->t_outq.c_cc && !ISSET(tp->t_state, TS_TTSTOP))
700 1.1 cgd break;
701 1.1 cgd }
702 1.68 pk if (!ISSET(tp->t_state, TS_CARR_ON)) {
703 1.68 pk error = 0; /* EOF */
704 1.68 pk goto out;
705 1.68 pk }
706 1.68 pk if (flag & IO_NDELAY) {
707 1.68 pk error = EWOULDBLOCK;
708 1.68 pk goto out;
709 1.68 pk }
710 1.68 pk error = ltsleep((caddr_t)&tp->t_outq.c_cf, TTIPRI | PCATCH,
711 1.68 pk ttyin, 0, &tp->t_slock);
712 1.31 christos if (error)
713 1.68 pk goto out;
714 1.1 cgd }
715 1.68 pk
716 1.68 pk if (pti->pt_flags & (PF_PKT|PF_UCNTL)) {
717 1.68 pk TTY_UNLOCK(tp);
718 1.70.2.2 skrll splx(s);
719 1.1 cgd error = ureadc(0, uio);
720 1.70.2.2 skrll s = spltty();
721 1.68 pk TTY_LOCK(tp);
722 1.68 pk if (error == 0 && !ISSET(tp->t_state, TS_ISOPEN))
723 1.68 pk error = EIO;
724 1.68 pk }
725 1.1 cgd while (uio->uio_resid > 0 && error == 0) {
726 1.22 cgd cc = q_to_b(&tp->t_outq, buf, min(uio->uio_resid, BUFSIZ));
727 1.1 cgd if (cc <= 0)
728 1.1 cgd break;
729 1.68 pk TTY_UNLOCK(tp);
730 1.70.2.2 skrll splx(s);
731 1.1 cgd error = uiomove(buf, cc, uio);
732 1.70.2.2 skrll s = spltty();
733 1.68 pk TTY_LOCK(tp);
734 1.68 pk if (error == 0 && !ISSET(tp->t_state, TS_ISOPEN))
735 1.68 pk error = EIO;
736 1.1 cgd }
737 1.68 pk
738 1.8 mycroft if (tp->t_outq.c_cc <= tp->t_lowat) {
739 1.37 mycroft if (ISSET(tp->t_state, TS_ASLEEP)) {
740 1.37 mycroft CLR(tp->t_state, TS_ASLEEP);
741 1.8 mycroft wakeup((caddr_t)&tp->t_outq);
742 1.1 cgd }
743 1.70.2.2 skrll selnotify(&tp->t_wsel, NOTE_SUBMIT);
744 1.1 cgd }
745 1.68 pk out:
746 1.68 pk TTY_UNLOCK(tp);
747 1.70.2.2 skrll splx(s);
748 1.1 cgd return (error);
749 1.1 cgd }
750 1.1 cgd
751 1.1 cgd
752 1.31 christos int
753 1.1 cgd ptcwrite(dev, uio, flag)
754 1.1 cgd dev_t dev;
755 1.43 augustss struct uio *uio;
756 1.7 andrew int flag;
757 1.1 cgd {
758 1.47 jdolecek struct pt_softc *pti = pt_softc[minor(dev)];
759 1.43 augustss struct tty *tp = pti->pt_tty;
760 1.43 augustss u_char *cp = NULL;
761 1.43 augustss int cc = 0;
762 1.1 cgd u_char locbuf[BUFSIZ];
763 1.1 cgd int cnt = 0;
764 1.1 cgd int error = 0;
765 1.70.2.2 skrll int s;
766 1.1 cgd
767 1.1 cgd again:
768 1.70.2.2 skrll s = spltty();
769 1.68 pk TTY_LOCK(tp);
770 1.37 mycroft if (!ISSET(tp->t_state, TS_ISOPEN))
771 1.1 cgd goto block;
772 1.1 cgd if (pti->pt_flags & PF_REMOTE) {
773 1.8 mycroft if (tp->t_canq.c_cc)
774 1.1 cgd goto block;
775 1.8 mycroft while (uio->uio_resid > 0 && tp->t_canq.c_cc < TTYHOG - 1) {
776 1.1 cgd if (cc == 0) {
777 1.1 cgd cc = min(uio->uio_resid, BUFSIZ);
778 1.8 mycroft cc = min(cc, TTYHOG - 1 - tp->t_canq.c_cc);
779 1.1 cgd cp = locbuf;
780 1.68 pk TTY_UNLOCK(tp);
781 1.70.2.2 skrll splx(s);
782 1.1 cgd error = uiomove((caddr_t)cp, cc, uio);
783 1.1 cgd if (error)
784 1.1 cgd return (error);
785 1.70.2.2 skrll s = spltty();
786 1.68 pk TTY_LOCK(tp);
787 1.1 cgd /* check again for safety */
788 1.68 pk if (!ISSET(tp->t_state, TS_ISOPEN)) {
789 1.68 pk error = EIO;
790 1.68 pk goto out;
791 1.68 pk }
792 1.1 cgd }
793 1.1 cgd if (cc)
794 1.67 simonb (void) b_to_q(cp, cc, &tp->t_canq);
795 1.1 cgd cc = 0;
796 1.1 cgd }
797 1.8 mycroft (void) putc(0, &tp->t_canq);
798 1.1 cgd ttwakeup(tp);
799 1.8 mycroft wakeup((caddr_t)&tp->t_canq);
800 1.68 pk error = 0;
801 1.68 pk goto out;
802 1.1 cgd }
803 1.1 cgd while (uio->uio_resid > 0) {
804 1.1 cgd if (cc == 0) {
805 1.1 cgd cc = min(uio->uio_resid, BUFSIZ);
806 1.1 cgd cp = locbuf;
807 1.68 pk TTY_UNLOCK(tp);
808 1.70.2.2 skrll splx(s);
809 1.1 cgd error = uiomove((caddr_t)cp, cc, uio);
810 1.1 cgd if (error)
811 1.1 cgd return (error);
812 1.70.2.2 skrll s = spltty();
813 1.68 pk TTY_LOCK(tp);
814 1.1 cgd /* check again for safety */
815 1.68 pk if (!ISSET(tp->t_state, TS_ISOPEN)) {
816 1.68 pk error = EIO;
817 1.68 pk goto out;
818 1.68 pk }
819 1.1 cgd }
820 1.1 cgd while (cc > 0) {
821 1.8 mycroft if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 &&
822 1.59 christos (tp->t_canq.c_cc > 0 || !ISSET(tp->t_lflag, ICANON))) {
823 1.8 mycroft wakeup((caddr_t)&tp->t_rawq);
824 1.1 cgd goto block;
825 1.1 cgd }
826 1.68 pk /* XXX - should change l_rint to be called with lock
827 1.68 pk * see also tty.c:ttyinput_wlock()
828 1.68 pk */
829 1.68 pk TTY_UNLOCK(tp);
830 1.70.2.2 skrll splx(s);
831 1.50 eeh (*tp->t_linesw->l_rint)(*cp++, tp);
832 1.70.2.2 skrll s = spltty();
833 1.68 pk TTY_LOCK(tp);
834 1.1 cgd cnt++;
835 1.1 cgd cc--;
836 1.1 cgd }
837 1.1 cgd cc = 0;
838 1.1 cgd }
839 1.68 pk error = 0;
840 1.68 pk goto out;
841 1.68 pk
842 1.1 cgd block:
843 1.1 cgd /*
844 1.1 cgd * Come here to wait for slave to open, for space
845 1.1 cgd * in outq, or space in rawq.
846 1.1 cgd */
847 1.68 pk if (!ISSET(tp->t_state, TS_CARR_ON)) {
848 1.68 pk error = EIO;
849 1.68 pk goto out;
850 1.68 pk }
851 1.1 cgd if (flag & IO_NDELAY) {
852 1.1 cgd /* adjust for data copied in but not written */
853 1.1 cgd uio->uio_resid += cc;
854 1.68 pk error = cnt == 0 ? EWOULDBLOCK : 0;
855 1.68 pk goto out;
856 1.1 cgd }
857 1.68 pk error = ltsleep((caddr_t)&tp->t_rawq.c_cf, TTOPRI | PCATCH | PNORELOCK,
858 1.68 pk ttyout, 0, &tp->t_slock);
859 1.70.2.2 skrll splx(s);
860 1.31 christos if (error) {
861 1.1 cgd /* adjust for data copied in but not written */
862 1.1 cgd uio->uio_resid += cc;
863 1.1 cgd return (error);
864 1.1 cgd }
865 1.1 cgd goto again;
866 1.68 pk
867 1.68 pk out:
868 1.68 pk TTY_UNLOCK(tp);
869 1.70.2.2 skrll splx(s);
870 1.68 pk return (error);
871 1.29 mycroft }
872 1.29 mycroft
873 1.31 christos int
874 1.70.2.3 skrll ptcpoll(dev, events, p)
875 1.31 christos dev_t dev;
876 1.38 mycroft int events;
877 1.70.2.3 skrll struct proc *p;
878 1.31 christos {
879 1.47 jdolecek struct pt_softc *pti = pt_softc[minor(dev)];
880 1.43 augustss struct tty *tp = pti->pt_tty;
881 1.38 mycroft int revents = 0;
882 1.38 mycroft int s = splsoftclock();
883 1.31 christos
884 1.38 mycroft if (events & (POLLIN | POLLRDNORM))
885 1.37 mycroft if (ISSET(tp->t_state, TS_ISOPEN) &&
886 1.38 mycroft ((tp->t_outq.c_cc > 0 && !ISSET(tp->t_state, TS_TTSTOP)) ||
887 1.38 mycroft ((pti->pt_flags & PF_PKT) && pti->pt_send) ||
888 1.38 mycroft ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl)))
889 1.38 mycroft revents |= events & (POLLIN | POLLRDNORM);
890 1.31 christos
891 1.38 mycroft if (events & (POLLOUT | POLLWRNORM))
892 1.37 mycroft if (ISSET(tp->t_state, TS_ISOPEN) &&
893 1.38 mycroft ((pti->pt_flags & PF_REMOTE) ?
894 1.38 mycroft (tp->t_canq.c_cc == 0) :
895 1.38 mycroft ((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG-2) ||
896 1.59 christos (tp->t_canq.c_cc == 0 && ISSET(tp->t_lflag, ICANON)))))
897 1.38 mycroft revents |= events & (POLLOUT | POLLWRNORM);
898 1.31 christos
899 1.38 mycroft if (events & POLLHUP)
900 1.38 mycroft if (!ISSET(tp->t_state, TS_CARR_ON))
901 1.38 mycroft revents |= POLLHUP;
902 1.31 christos
903 1.38 mycroft if (revents == 0) {
904 1.38 mycroft if (events & (POLLIN | POLLHUP | POLLRDNORM))
905 1.70.2.3 skrll selrecord(p, &pti->pt_selr);
906 1.31 christos
907 1.38 mycroft if (events & (POLLOUT | POLLWRNORM))
908 1.70.2.3 skrll selrecord(p, &pti->pt_selw);
909 1.31 christos }
910 1.38 mycroft
911 1.38 mycroft splx(s);
912 1.38 mycroft return (revents);
913 1.31 christos }
914 1.31 christos
915 1.65 jdolecek static void
916 1.65 jdolecek filt_ptcrdetach(struct knote *kn)
917 1.65 jdolecek {
918 1.65 jdolecek struct pt_softc *pti;
919 1.65 jdolecek int s;
920 1.65 jdolecek
921 1.65 jdolecek pti = kn->kn_hook;
922 1.65 jdolecek s = spltty();
923 1.66 christos SLIST_REMOVE(&pti->pt_selr.sel_klist, kn, knote, kn_selnext);
924 1.65 jdolecek splx(s);
925 1.65 jdolecek }
926 1.65 jdolecek
927 1.65 jdolecek static int
928 1.65 jdolecek filt_ptcread(struct knote *kn, long hint)
929 1.65 jdolecek {
930 1.65 jdolecek struct pt_softc *pti;
931 1.65 jdolecek struct tty *tp;
932 1.65 jdolecek int canread;
933 1.65 jdolecek
934 1.65 jdolecek pti = kn->kn_hook;
935 1.65 jdolecek tp = pti->pt_tty;
936 1.65 jdolecek
937 1.65 jdolecek canread = (ISSET(tp->t_state, TS_ISOPEN) &&
938 1.65 jdolecek ((tp->t_outq.c_cc > 0 && !ISSET(tp->t_state, TS_TTSTOP)) ||
939 1.65 jdolecek ((pti->pt_flags & PF_PKT) && pti->pt_send) ||
940 1.65 jdolecek ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl)));
941 1.65 jdolecek
942 1.65 jdolecek if (canread) {
943 1.65 jdolecek /*
944 1.65 jdolecek * c_cc is number of characters after output post-processing;
945 1.65 jdolecek * the amount of data actually read(2) depends on
946 1.65 jdolecek * setting of input flags for the terminal.
947 1.65 jdolecek */
948 1.65 jdolecek kn->kn_data = tp->t_outq.c_cc;
949 1.65 jdolecek if (((pti->pt_flags & PF_PKT) && pti->pt_send) ||
950 1.65 jdolecek ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl))
951 1.65 jdolecek kn->kn_data++;
952 1.65 jdolecek }
953 1.65 jdolecek
954 1.65 jdolecek return (canread);
955 1.65 jdolecek }
956 1.65 jdolecek
957 1.65 jdolecek static void
958 1.65 jdolecek filt_ptcwdetach(struct knote *kn)
959 1.65 jdolecek {
960 1.65 jdolecek struct pt_softc *pti;
961 1.65 jdolecek int s;
962 1.65 jdolecek
963 1.65 jdolecek pti = kn->kn_hook;
964 1.65 jdolecek s = spltty();
965 1.66 christos SLIST_REMOVE(&pti->pt_selw.sel_klist, kn, knote, kn_selnext);
966 1.65 jdolecek splx(s);
967 1.65 jdolecek }
968 1.65 jdolecek
969 1.65 jdolecek static int
970 1.65 jdolecek filt_ptcwrite(struct knote *kn, long hint)
971 1.65 jdolecek {
972 1.65 jdolecek struct pt_softc *pti;
973 1.65 jdolecek struct tty *tp;
974 1.65 jdolecek int canwrite;
975 1.65 jdolecek int nwrite;
976 1.65 jdolecek
977 1.65 jdolecek pti = kn->kn_hook;
978 1.65 jdolecek tp = pti->pt_tty;
979 1.65 jdolecek
980 1.65 jdolecek canwrite = (ISSET(tp->t_state, TS_ISOPEN) &&
981 1.65 jdolecek ((pti->pt_flags & PF_REMOTE) ?
982 1.65 jdolecek (tp->t_canq.c_cc == 0) :
983 1.65 jdolecek ((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG-2) ||
984 1.65 jdolecek (tp->t_canq.c_cc == 0 && ISSET(tp->t_lflag, ICANON)))));
985 1.65 jdolecek
986 1.65 jdolecek if (canwrite) {
987 1.65 jdolecek if (pti->pt_flags & PF_REMOTE)
988 1.65 jdolecek nwrite = tp->t_canq.c_cn;
989 1.65 jdolecek else {
990 1.65 jdolecek /* this is guaranteed to be > 0 due to above check */
991 1.65 jdolecek nwrite = tp->t_canq.c_cn
992 1.65 jdolecek - (tp->t_rawq.c_cc + tp->t_canq.c_cc);
993 1.65 jdolecek }
994 1.65 jdolecek kn->kn_data = nwrite;
995 1.65 jdolecek }
996 1.65 jdolecek
997 1.65 jdolecek return (canwrite);
998 1.65 jdolecek }
999 1.65 jdolecek
1000 1.65 jdolecek static const struct filterops ptcread_filtops =
1001 1.65 jdolecek { 1, NULL, filt_ptcrdetach, filt_ptcread };
1002 1.65 jdolecek static const struct filterops ptcwrite_filtops =
1003 1.65 jdolecek { 1, NULL, filt_ptcwdetach, filt_ptcwrite };
1004 1.65 jdolecek
1005 1.65 jdolecek int
1006 1.65 jdolecek ptckqfilter(dev_t dev, struct knote *kn)
1007 1.65 jdolecek {
1008 1.65 jdolecek struct pt_softc *pti = pt_softc[minor(dev)];
1009 1.65 jdolecek struct klist *klist;
1010 1.65 jdolecek int s;
1011 1.65 jdolecek
1012 1.65 jdolecek switch (kn->kn_filter) {
1013 1.65 jdolecek case EVFILT_READ:
1014 1.66 christos klist = &pti->pt_selr.sel_klist;
1015 1.65 jdolecek kn->kn_fop = &ptcread_filtops;
1016 1.65 jdolecek break;
1017 1.65 jdolecek case EVFILT_WRITE:
1018 1.66 christos klist = &pti->pt_selw.sel_klist;
1019 1.65 jdolecek kn->kn_fop = &ptcwrite_filtops;
1020 1.65 jdolecek break;
1021 1.65 jdolecek default:
1022 1.65 jdolecek return (1);
1023 1.65 jdolecek }
1024 1.65 jdolecek
1025 1.65 jdolecek kn->kn_hook = pti;
1026 1.65 jdolecek
1027 1.65 jdolecek s = spltty();
1028 1.65 jdolecek SLIST_INSERT_HEAD(klist, kn, kn_selnext);
1029 1.65 jdolecek splx(s);
1030 1.65 jdolecek
1031 1.65 jdolecek return (0);
1032 1.65 jdolecek }
1033 1.31 christos
1034 1.29 mycroft struct tty *
1035 1.29 mycroft ptytty(dev)
1036 1.29 mycroft dev_t dev;
1037 1.29 mycroft {
1038 1.47 jdolecek struct pt_softc *pti = pt_softc[minor(dev)];
1039 1.43 augustss struct tty *tp = pti->pt_tty;
1040 1.29 mycroft
1041 1.29 mycroft return (tp);
1042 1.1 cgd }
1043 1.1 cgd
1044 1.1 cgd /*ARGSUSED*/
1045 1.31 christos int
1046 1.70.2.3 skrll ptyioctl(dev, cmd, data, flag, p)
1047 1.18 mycroft dev_t dev;
1048 1.24 cgd u_long cmd;
1049 1.1 cgd caddr_t data;
1050 1.18 mycroft int flag;
1051 1.70.2.3 skrll struct proc *p;
1052 1.1 cgd {
1053 1.47 jdolecek struct pt_softc *pti = pt_softc[minor(dev)];
1054 1.43 augustss struct tty *tp = pti->pt_tty;
1055 1.63 gehenna const struct cdevsw *cdev;
1056 1.43 augustss u_char *cc = tp->t_cc;
1057 1.44 fvdl int stop, error, sig;
1058 1.70.2.2 skrll int s;
1059 1.1 cgd
1060 1.63 gehenna cdev = cdevsw_lookup(dev);
1061 1.1 cgd /*
1062 1.1 cgd * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
1063 1.1 cgd * ttywflush(tp) will hang if there are characters in the outq.
1064 1.1 cgd */
1065 1.1 cgd if (cmd == TIOCEXT) {
1066 1.1 cgd /*
1067 1.1 cgd * When the EXTPROC bit is being toggled, we need
1068 1.1 cgd * to send an TIOCPKT_IOCTL if the packet driver
1069 1.1 cgd * is turned on.
1070 1.1 cgd */
1071 1.1 cgd if (*(int *)data) {
1072 1.1 cgd if (pti->pt_flags & PF_PKT) {
1073 1.1 cgd pti->pt_send |= TIOCPKT_IOCTL;
1074 1.1 cgd ptcwakeup(tp, FREAD);
1075 1.1 cgd }
1076 1.37 mycroft SET(tp->t_lflag, EXTPROC);
1077 1.1 cgd } else {
1078 1.39 fvdl if (ISSET(tp->t_lflag, EXTPROC) &&
1079 1.1 cgd (pti->pt_flags & PF_PKT)) {
1080 1.1 cgd pti->pt_send |= TIOCPKT_IOCTL;
1081 1.1 cgd ptcwakeup(tp, FREAD);
1082 1.1 cgd }
1083 1.37 mycroft CLR(tp->t_lflag, EXTPROC);
1084 1.1 cgd }
1085 1.1 cgd return(0);
1086 1.70.2.2 skrll }
1087 1.70.2.2 skrll
1088 1.63 gehenna if (cdev != NULL && cdev->d_open == ptcopen)
1089 1.1 cgd switch (cmd) {
1090 1.70.2.2 skrll #ifndef NO_DEV_PTM
1091 1.70.2.2 skrll case TIOCGRANTPT:
1092 1.70.2.3 skrll return pty_grant_slave(p, dev);
1093 1.70.2.2 skrll
1094 1.70.2.2 skrll case TIOCPTSNAME:
1095 1.70.2.2 skrll pty_fill_ptmget(dev, -1, -1, data);
1096 1.70.2.2 skrll return 0;
1097 1.70.2.2 skrll #endif
1098 1.1 cgd
1099 1.1 cgd case TIOCGPGRP:
1100 1.35 pk /*
1101 1.35 pk * We avoid calling ttioctl on the controller since,
1102 1.1 cgd * in that case, tp must be the controlling terminal.
1103 1.1 cgd */
1104 1.1 cgd *(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
1105 1.1 cgd return (0);
1106 1.1 cgd
1107 1.1 cgd case TIOCPKT:
1108 1.1 cgd if (*(int *)data) {
1109 1.1 cgd if (pti->pt_flags & PF_UCNTL)
1110 1.1 cgd return (EINVAL);
1111 1.1 cgd pti->pt_flags |= PF_PKT;
1112 1.1 cgd } else
1113 1.1 cgd pti->pt_flags &= ~PF_PKT;
1114 1.1 cgd return (0);
1115 1.1 cgd
1116 1.1 cgd case TIOCUCNTL:
1117 1.1 cgd if (*(int *)data) {
1118 1.1 cgd if (pti->pt_flags & PF_PKT)
1119 1.1 cgd return (EINVAL);
1120 1.1 cgd pti->pt_flags |= PF_UCNTL;
1121 1.1 cgd } else
1122 1.1 cgd pti->pt_flags &= ~PF_UCNTL;
1123 1.1 cgd return (0);
1124 1.1 cgd
1125 1.1 cgd case TIOCREMOTE:
1126 1.1 cgd if (*(int *)data)
1127 1.1 cgd pti->pt_flags |= PF_REMOTE;
1128 1.1 cgd else
1129 1.1 cgd pti->pt_flags &= ~PF_REMOTE;
1130 1.70.2.2 skrll s = spltty();
1131 1.68 pk TTY_LOCK(tp);
1132 1.1 cgd ttyflush(tp, FREAD|FWRITE);
1133 1.68 pk TTY_UNLOCK(tp);
1134 1.70.2.2 skrll splx(s);
1135 1.1 cgd return (0);
1136 1.1 cgd
1137 1.32 christos #ifdef COMPAT_OLDTTY
1138 1.35 pk case TIOCSETP:
1139 1.1 cgd case TIOCSETN:
1140 1.2 cgd #endif
1141 1.1 cgd case TIOCSETD:
1142 1.1 cgd case TIOCSETA:
1143 1.1 cgd case TIOCSETAW:
1144 1.1 cgd case TIOCSETAF:
1145 1.68 pk TTY_LOCK(tp);
1146 1.21 cgd ndflush(&tp->t_outq, tp->t_outq.c_cc);
1147 1.68 pk TTY_UNLOCK(tp);
1148 1.1 cgd break;
1149 1.1 cgd
1150 1.1 cgd case TIOCSIG:
1151 1.46 mrg sig = (int)(long)*(caddr_t *)data;
1152 1.44 fvdl if (sig <= 0 || sig >= NSIG)
1153 1.44 fvdl return (EINVAL);
1154 1.68 pk TTY_LOCK(tp);
1155 1.37 mycroft if (!ISSET(tp->t_lflag, NOFLSH))
1156 1.1 cgd ttyflush(tp, FREAD|FWRITE);
1157 1.44 fvdl if ((sig == SIGINFO) &&
1158 1.37 mycroft (!ISSET(tp->t_lflag, NOKERNINFO)))
1159 1.1 cgd ttyinfo(tp);
1160 1.68 pk TTY_UNLOCK(tp);
1161 1.62 itohy pgsignal(tp->t_pgrp, sig, 1);
1162 1.1 cgd return(0);
1163 1.1 cgd }
1164 1.70.2.2 skrll
1165 1.70.2.3 skrll error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, p);
1166 1.61 atatat if (error == EPASSTHROUGH)
1167 1.70.2.3 skrll error = ttioctl(tp, cmd, data, flag, p);
1168 1.61 atatat if (error == EPASSTHROUGH) {
1169 1.1 cgd if (pti->pt_flags & PF_UCNTL &&
1170 1.1 cgd (cmd & ~0xff) == UIOCCMD(0)) {
1171 1.1 cgd if (cmd & 0xff) {
1172 1.1 cgd pti->pt_ucntl = (u_char)cmd;
1173 1.1 cgd ptcwakeup(tp, FREAD);
1174 1.1 cgd }
1175 1.1 cgd return (0);
1176 1.1 cgd }
1177 1.1 cgd }
1178 1.1 cgd /*
1179 1.1 cgd * If external processing and packet mode send ioctl packet.
1180 1.1 cgd */
1181 1.37 mycroft if (ISSET(tp->t_lflag, EXTPROC) && (pti->pt_flags & PF_PKT)) {
1182 1.1 cgd switch(cmd) {
1183 1.1 cgd case TIOCSETA:
1184 1.1 cgd case TIOCSETAW:
1185 1.1 cgd case TIOCSETAF:
1186 1.32 christos #ifdef COMPAT_OLDTTY
1187 1.1 cgd case TIOCSETP:
1188 1.1 cgd case TIOCSETN:
1189 1.1 cgd case TIOCSETC:
1190 1.1 cgd case TIOCSLTC:
1191 1.1 cgd case TIOCLBIS:
1192 1.1 cgd case TIOCLBIC:
1193 1.1 cgd case TIOCLSET:
1194 1.1 cgd #endif
1195 1.1 cgd pti->pt_send |= TIOCPKT_IOCTL;
1196 1.22 cgd ptcwakeup(tp, FREAD);
1197 1.1 cgd default:
1198 1.1 cgd break;
1199 1.1 cgd }
1200 1.1 cgd }
1201 1.37 mycroft stop = ISSET(tp->t_iflag, IXON) && CCEQ(cc[VSTOP], CTRL('s'))
1202 1.1 cgd && CCEQ(cc[VSTART], CTRL('q'));
1203 1.1 cgd if (pti->pt_flags & PF_NOSTOP) {
1204 1.1 cgd if (stop) {
1205 1.1 cgd pti->pt_send &= ~TIOCPKT_NOSTOP;
1206 1.1 cgd pti->pt_send |= TIOCPKT_DOSTOP;
1207 1.1 cgd pti->pt_flags &= ~PF_NOSTOP;
1208 1.1 cgd ptcwakeup(tp, FREAD);
1209 1.1 cgd }
1210 1.1 cgd } else {
1211 1.1 cgd if (!stop) {
1212 1.1 cgd pti->pt_send &= ~TIOCPKT_DOSTOP;
1213 1.1 cgd pti->pt_send |= TIOCPKT_NOSTOP;
1214 1.1 cgd pti->pt_flags |= PF_NOSTOP;
1215 1.1 cgd ptcwakeup(tp, FREAD);
1216 1.1 cgd }
1217 1.1 cgd }
1218 1.1 cgd return (error);
1219 1.1 cgd }
1220 1.70.2.2 skrll
1221 1.70.2.2 skrll #ifndef NO_DEV_PTM
1222 1.70.2.2 skrll /*
1223 1.70.2.2 skrll * Check if a pty is free to use.
1224 1.70.2.2 skrll */
1225 1.70.2.2 skrll static __inline int
1226 1.70.2.2 skrll pty_isfree_locked(int minor)
1227 1.70.2.2 skrll {
1228 1.70.2.2 skrll struct pt_softc *pt = pt_softc[minor];
1229 1.70.2.2 skrll return (pt == NULL || pt->pt_tty == NULL ||
1230 1.70.2.2 skrll pt->pt_tty->t_oproc == NULL);
1231 1.70.2.2 skrll }
1232 1.70.2.2 skrll
1233 1.70.2.2 skrll static int
1234 1.70.2.2 skrll pty_isfree(int minor)
1235 1.70.2.2 skrll {
1236 1.70.2.2 skrll int isfree;
1237 1.70.2.2 skrll
1238 1.70.2.2 skrll simple_lock(&pt_softc_mutex);
1239 1.70.2.2 skrll isfree = pty_isfree_locked(minor);
1240 1.70.2.2 skrll simple_unlock(&pt_softc_mutex);
1241 1.70.2.2 skrll return(isfree);
1242 1.70.2.2 skrll }
1243 1.70.2.2 skrll
1244 1.70.2.2 skrll static char *
1245 1.70.2.2 skrll pty_makename(char *buf, dev_t dev, char c)
1246 1.70.2.2 skrll {
1247 1.70.2.2 skrll size_t nt;
1248 1.70.2.2 skrll dev_t minor = minor(dev);
1249 1.70.2.2 skrll
1250 1.70.2.2 skrll (void)memcpy(buf, TTY_TEMPLATE, TTY_NAMESIZE);
1251 1.70.2.2 skrll
1252 1.70.2.2 skrll buf[5] = c;
1253 1.70.2.2 skrll
1254 1.70.2.2 skrll if (minor < 256) {
1255 1.70.2.2 skrll nt = sizeof(TTY_OLD_SUFFIX) - 1;
1256 1.70.2.2 skrll buf[8] = TTY_LETTERS[minor / nt];
1257 1.70.2.2 skrll buf[9] = TTY_OLD_SUFFIX[minor % nt];
1258 1.70.2.2 skrll } else {
1259 1.70.2.2 skrll minor -= 256;
1260 1.70.2.2 skrll nt = sizeof(TTY_NEW_SUFFIX) - sizeof(TTY_OLD_SUFFIX);
1261 1.70.2.2 skrll buf[8] = TTY_LETTERS[minor / nt];
1262 1.70.2.2 skrll buf[9] = TTY_NEW_SUFFIX[minor % nt];
1263 1.70.2.2 skrll }
1264 1.70.2.2 skrll return buf;
1265 1.70.2.2 skrll }
1266 1.70.2.2 skrll
1267 1.70.2.2 skrll static dev_t
1268 1.70.2.2 skrll pty_getfree(void)
1269 1.70.2.2 skrll {
1270 1.70.2.2 skrll int i;
1271 1.70.2.2 skrll
1272 1.70.2.2 skrll simple_lock(&pt_softc_mutex);
1273 1.70.2.2 skrll for (i = 0; i < npty; i++) {
1274 1.70.2.2 skrll if (pty_isfree_locked(i))
1275 1.70.2.2 skrll break;
1276 1.70.2.2 skrll }
1277 1.70.2.2 skrll simple_unlock(&pt_softc_mutex);
1278 1.70.2.2 skrll return (makedev(pts_major, i));
1279 1.70.2.2 skrll }
1280 1.70.2.2 skrll
1281 1.70.2.2 skrll /*
1282 1.70.2.2 skrll * Hacked up version of vn_open. We _only_ handle ptys and only open
1283 1.70.2.2 skrll * them with FREAD|FWRITE and never deal with creat or stuff like that.
1284 1.70.2.2 skrll *
1285 1.70.2.2 skrll * We need it because we have to fake up root credentials to open the pty.
1286 1.70.2.2 skrll */
1287 1.70.2.2 skrll static int
1288 1.70.2.2 skrll ptm_vn_open(struct nameidata *ndp)
1289 1.70.2.2 skrll {
1290 1.70.2.2 skrll struct vnode *vp;
1291 1.70.2.3 skrll struct proc *p = ndp->ni_cnd.cn_proc;
1292 1.70.2.2 skrll struct ucred *cred;
1293 1.70.2.2 skrll int error;
1294 1.70.2.2 skrll
1295 1.70.2.2 skrll if ((error = namei(ndp)) != 0)
1296 1.70.2.2 skrll return (error);
1297 1.70.2.2 skrll vp = ndp->ni_vp;
1298 1.70.2.2 skrll if (vp->v_type != VCHR) {
1299 1.70.2.2 skrll error = EINVAL;
1300 1.70.2.2 skrll goto bad;
1301 1.70.2.2 skrll }
1302 1.70.2.2 skrll
1303 1.70.2.2 skrll /*
1304 1.70.2.2 skrll * Get us a fresh cred with root privileges.
1305 1.70.2.2 skrll */
1306 1.70.2.2 skrll cred = crget();
1307 1.70.2.3 skrll error = VOP_OPEN(vp, FREAD|FWRITE, cred, p);
1308 1.70.2.2 skrll crfree(cred);
1309 1.70.2.2 skrll
1310 1.70.2.2 skrll if (error)
1311 1.70.2.2 skrll goto bad;
1312 1.70.2.2 skrll
1313 1.70.2.2 skrll vp->v_writecount++;
1314 1.70.2.2 skrll
1315 1.70.2.2 skrll return (0);
1316 1.70.2.2 skrll bad:
1317 1.70.2.2 skrll vput(vp);
1318 1.70.2.2 skrll return (error);
1319 1.70.2.2 skrll }
1320 1.70.2.2 skrll
1321 1.70.2.2 skrll static int
1322 1.70.2.3 skrll pty_alloc_master(struct proc *p, int *fd, dev_t *dev)
1323 1.70.2.2 skrll {
1324 1.70.2.2 skrll int error;
1325 1.70.2.2 skrll struct nameidata nd;
1326 1.70.2.2 skrll struct pt_softc *pti;
1327 1.70.2.2 skrll struct file *fp;
1328 1.70.2.2 skrll int md;
1329 1.70.2.2 skrll char name[TTY_NAMESIZE];
1330 1.70.2.2 skrll
1331 1.70.2.2 skrll if ((error = falloc(p, &fp, fd)) != 0) {
1332 1.70.2.2 skrll DPRINTF(("falloc %d\n", error));
1333 1.70.2.2 skrll return error;
1334 1.70.2.2 skrll }
1335 1.70.2.2 skrll retry:
1336 1.70.2.2 skrll /* Find and open a free master pty. */
1337 1.70.2.2 skrll *dev = pty_getfree();
1338 1.70.2.2 skrll md = minor(*dev);
1339 1.70.2.2 skrll if ((error = check_pty(md)) != 0) {
1340 1.70.2.2 skrll DPRINTF(("ckeck_pty %d\n", error));
1341 1.70.2.2 skrll goto bad;
1342 1.70.2.2 skrll }
1343 1.70.2.2 skrll pti = pt_softc[md];
1344 1.70.2.2 skrll NDINIT(&nd, LOOKUP, NOFOLLOW|LOCKLEAF, UIO_SYSSPACE,
1345 1.70.2.3 skrll pty_makename(name, *dev, 'p'), p);
1346 1.70.2.2 skrll if ((error = ptm_vn_open(&nd)) != 0) {
1347 1.70.2.2 skrll /*
1348 1.70.2.2 skrll * Check if the master open failed because we lost
1349 1.70.2.2 skrll * the race to grab it.
1350 1.70.2.2 skrll */
1351 1.70.2.2 skrll if (error == EIO && !pty_isfree(md))
1352 1.70.2.2 skrll goto retry;
1353 1.70.2.2 skrll DPRINTF(("ptm_vn_open %d\n", error));
1354 1.70.2.2 skrll goto bad;
1355 1.70.2.2 skrll }
1356 1.70.2.2 skrll fp->f_flag = FREAD|FWRITE;
1357 1.70.2.2 skrll fp->f_type = DTYPE_VNODE;
1358 1.70.2.2 skrll fp->f_ops = &vnops;
1359 1.70.2.2 skrll fp->f_data = nd.ni_vp;
1360 1.70.2.2 skrll VOP_UNLOCK(nd.ni_vp, 0);
1361 1.70.2.2 skrll FILE_SET_MATURE(fp);
1362 1.70.2.3 skrll FILE_UNUSE(fp, p);
1363 1.70.2.2 skrll return 0;
1364 1.70.2.2 skrll bad:
1365 1.70.2.3 skrll FILE_UNUSE(fp, p);
1366 1.70.2.2 skrll fdremove(p->p_fd, *fd);
1367 1.70.2.2 skrll ffree(fp);
1368 1.70.2.2 skrll return error;
1369 1.70.2.2 skrll }
1370 1.70.2.2 skrll
1371 1.70.2.2 skrll static int
1372 1.70.2.3 skrll pty_grant_slave(struct proc *p, dev_t dev)
1373 1.70.2.2 skrll {
1374 1.70.2.2 skrll int error;
1375 1.70.2.2 skrll struct nameidata nd;
1376 1.70.2.2 skrll char name[TTY_NAMESIZE];
1377 1.70.2.2 skrll
1378 1.70.2.2 skrll /*
1379 1.70.2.2 skrll * Open the slave.
1380 1.70.2.2 skrll * namei -> setattr -> unlock -> revoke -> vrele ->
1381 1.70.2.2 skrll * namei -> open -> unlock
1382 1.70.2.2 skrll * Three stage rocket:
1383 1.70.2.2 skrll * 1. Change the owner and permissions on the slave.
1384 1.70.2.2 skrll * 2. Revoke all the users of the slave.
1385 1.70.2.2 skrll * 3. open the slave.
1386 1.70.2.2 skrll */
1387 1.70.2.2 skrll NDINIT(&nd, LOOKUP, NOFOLLOW|LOCKLEAF, UIO_SYSSPACE,
1388 1.70.2.3 skrll pty_makename(name, dev, 't'), p);
1389 1.70.2.2 skrll if ((error = namei(&nd)) != 0) {
1390 1.70.2.2 skrll DPRINTF(("namei %d\n", error));
1391 1.70.2.2 skrll return error;
1392 1.70.2.2 skrll }
1393 1.70.2.2 skrll if ((nd.ni_vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
1394 1.70.2.2 skrll struct vattr vattr;
1395 1.70.2.2 skrll struct ucred *cred;
1396 1.70.2.2 skrll gid_t gid = _TTY_GID;
1397 1.70.2.2 skrll /* get real uid */
1398 1.70.2.3 skrll uid_t uid = p->p_cred->p_ruid;
1399 1.70.2.2 skrll
1400 1.70.2.2 skrll VATTR_NULL(&vattr);
1401 1.70.2.2 skrll vattr.va_uid = uid;
1402 1.70.2.2 skrll vattr.va_gid = gid;
1403 1.70.2.2 skrll vattr.va_mode = (S_IRUSR|S_IWUSR|S_IWGRP) & ALLPERMS;
1404 1.70.2.2 skrll /* Get a fake cred to pretend we're root. */
1405 1.70.2.2 skrll cred = crget();
1406 1.70.2.3 skrll error = VOP_SETATTR(nd.ni_vp, &vattr, cred, p);
1407 1.70.2.2 skrll crfree(cred);
1408 1.70.2.2 skrll if (error) {
1409 1.70.2.2 skrll DPRINTF(("setattr %d\n", error));
1410 1.70.2.2 skrll VOP_UNLOCK(nd.ni_vp, 0);
1411 1.70.2.2 skrll vrele(nd.ni_vp);
1412 1.70.2.2 skrll return error;
1413 1.70.2.2 skrll }
1414 1.70.2.2 skrll }
1415 1.70.2.2 skrll VOP_UNLOCK(nd.ni_vp, 0);
1416 1.70.2.2 skrll if (nd.ni_vp->v_usecount > 1 ||
1417 1.70.2.2 skrll (nd.ni_vp->v_flag & (VALIASED | VLAYER)))
1418 1.70.2.2 skrll VOP_REVOKE(nd.ni_vp, REVOKEALL);
1419 1.70.2.2 skrll
1420 1.70.2.2 skrll /*
1421 1.70.2.2 skrll * The vnode is useless after the revoke, we need to
1422 1.70.2.2 skrll * namei again.
1423 1.70.2.2 skrll */
1424 1.70.2.2 skrll vrele(nd.ni_vp);
1425 1.70.2.2 skrll return 0;
1426 1.70.2.2 skrll }
1427 1.70.2.2 skrll
1428 1.70.2.2 skrll static int
1429 1.70.2.3 skrll pty_alloc_slave(struct proc *p, int *fd, dev_t dev)
1430 1.70.2.2 skrll {
1431 1.70.2.2 skrll int error;
1432 1.70.2.2 skrll struct file *fp;
1433 1.70.2.2 skrll struct nameidata nd;
1434 1.70.2.2 skrll char name[TTY_NAMESIZE];
1435 1.70.2.2 skrll
1436 1.70.2.2 skrll /* Grab a filedescriptor for the slave */
1437 1.70.2.2 skrll if ((error = falloc(p, &fp, fd)) != 0) {
1438 1.70.2.2 skrll DPRINTF(("falloc %d\n", error));
1439 1.70.2.2 skrll return error;
1440 1.70.2.2 skrll }
1441 1.70.2.2 skrll
1442 1.70.2.2 skrll NDINIT(&nd, LOOKUP, NOFOLLOW|LOCKLEAF, UIO_SYSSPACE,
1443 1.70.2.3 skrll pty_makename(name, dev, 't'), p);
1444 1.70.2.2 skrll
1445 1.70.2.2 skrll /* now open it */
1446 1.70.2.2 skrll if ((error = ptm_vn_open(&nd)) != 0) {
1447 1.70.2.2 skrll DPRINTF(("vn_open %d\n", error));
1448 1.70.2.3 skrll FILE_UNUSE(fp, p);
1449 1.70.2.2 skrll fdremove(p->p_fd, *fd);
1450 1.70.2.2 skrll ffree(fp);
1451 1.70.2.2 skrll return error;
1452 1.70.2.2 skrll }
1453 1.70.2.2 skrll fp->f_flag = FREAD|FWRITE;
1454 1.70.2.2 skrll fp->f_type = DTYPE_VNODE;
1455 1.70.2.2 skrll fp->f_ops = &vnops;
1456 1.70.2.2 skrll fp->f_data = nd.ni_vp;
1457 1.70.2.2 skrll VOP_UNLOCK(nd.ni_vp, 0);
1458 1.70.2.2 skrll FILE_SET_MATURE(fp);
1459 1.70.2.3 skrll FILE_UNUSE(fp, p);
1460 1.70.2.2 skrll return 0;
1461 1.70.2.2 skrll }
1462 1.70.2.2 skrll
1463 1.70.2.2 skrll static void
1464 1.70.2.2 skrll pty_fill_ptmget(dev_t dev, int cfd, int sfd, void *data)
1465 1.70.2.2 skrll {
1466 1.70.2.2 skrll struct ptmget *ptm = data;
1467 1.70.2.2 skrll ptm->cfd = cfd;
1468 1.70.2.2 skrll ptm->sfd = sfd;
1469 1.70.2.2 skrll (void)pty_makename(ptm->cn, dev, 'p');
1470 1.70.2.2 skrll (void)pty_makename(ptm->sn, dev, 't');
1471 1.70.2.2 skrll }
1472 1.70.2.2 skrll
1473 1.70.2.2 skrll void
1474 1.70.2.2 skrll /*ARGSUSED*/
1475 1.70.2.2 skrll ptmattach(int n)
1476 1.70.2.2 skrll {
1477 1.70.2.2 skrll /* find the major and minor of the pty devices */
1478 1.70.2.2 skrll if ((pts_major = cdevsw_lookup_major(&pts_cdevsw)) == -1)
1479 1.70.2.2 skrll panic("ptmattach: Can't find pty slave in cdevsw");
1480 1.70.2.2 skrll }
1481 1.70.2.2 skrll
1482 1.70.2.2 skrll int
1483 1.70.2.2 skrll /*ARGSUSED*/
1484 1.70.2.3 skrll ptmopen(dev_t dev, int flag, int mode, struct proc *p)
1485 1.70.2.2 skrll {
1486 1.70.2.2 skrll int error;
1487 1.70.2.2 skrll int fd;
1488 1.70.2.2 skrll
1489 1.70.2.2 skrll switch(minor(dev)) {
1490 1.70.2.2 skrll case 0: /* /dev/ptmx */
1491 1.70.2.3 skrll if ((error = pty_alloc_master(p, &fd, &dev)) != 0)
1492 1.70.2.2 skrll return error;
1493 1.70.2.2 skrll curlwp->l_dupfd = fd;
1494 1.70.2.2 skrll return ENXIO;
1495 1.70.2.2 skrll case 1: /* /dev/ptm */
1496 1.70.2.2 skrll return 0;
1497 1.70.2.2 skrll default:
1498 1.70.2.2 skrll return ENODEV;
1499 1.70.2.2 skrll }
1500 1.70.2.2 skrll
1501 1.70.2.2 skrll }
1502 1.70.2.2 skrll
1503 1.70.2.2 skrll int
1504 1.70.2.2 skrll /*ARGSUSED*/
1505 1.70.2.3 skrll ptmclose(dev_t dev, int flag, int mode, struct proc *p)
1506 1.70.2.2 skrll {
1507 1.70.2.2 skrll return (0);
1508 1.70.2.2 skrll }
1509 1.70.2.2 skrll
1510 1.70.2.2 skrll int
1511 1.70.2.2 skrll /*ARGSUSED*/
1512 1.70.2.3 skrll ptmioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
1513 1.70.2.2 skrll {
1514 1.70.2.2 skrll int error;
1515 1.70.2.2 skrll dev_t newdev;
1516 1.70.2.2 skrll int cfd, sfd;
1517 1.70.2.2 skrll struct file *fp;
1518 1.70.2.2 skrll
1519 1.70.2.2 skrll error = 0;
1520 1.70.2.2 skrll switch (cmd) {
1521 1.70.2.2 skrll case TIOCPTMGET:
1522 1.70.2.3 skrll if ((error = pty_alloc_master(p, &cfd, &newdev)) != 0)
1523 1.70.2.2 skrll return error;
1524 1.70.2.2 skrll
1525 1.70.2.3 skrll if ((error = pty_grant_slave(p, newdev)) != 0)
1526 1.70.2.2 skrll goto bad;
1527 1.70.2.2 skrll
1528 1.70.2.3 skrll if ((error = pty_alloc_slave(p, &sfd, newdev)) != 0)
1529 1.70.2.2 skrll goto bad;
1530 1.70.2.2 skrll
1531 1.70.2.2 skrll /* now, put the indices and names into struct ptmget */
1532 1.70.2.2 skrll pty_fill_ptmget(newdev, cfd, sfd, data);
1533 1.70.2.2 skrll return 0;
1534 1.70.2.2 skrll default:
1535 1.70.2.2 skrll DPRINTF(("ptmioctl EINVAL\n"));
1536 1.70.2.2 skrll return EINVAL;
1537 1.70.2.2 skrll }
1538 1.70.2.2 skrll bad:
1539 1.70.2.2 skrll fp = fd_getfile(p->p_fd, cfd);
1540 1.70.2.2 skrll fdremove(p->p_fd, cfd);
1541 1.70.2.2 skrll ffree(fp);
1542 1.70.2.2 skrll return error;
1543 1.70.2.2 skrll }
1544 1.70.2.2 skrll #endif
1545