tty_pty.c revision 1.70.2.4 1 1.70.2.4 skrll /* $NetBSD: tty_pty.c,v 1.70.2.4 2004/09/21 13:35:16 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.4 skrll __KERNEL_RCSID(0, "$NetBSD: tty_pty.c,v 1.70.2.4 2004/09/21 13:35:16 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.4 skrll static int pty_grant_slave(struct lwp *, dev_t);
126 1.70.2.4 skrll static int pty_alloc_master(struct lwp *, int *, dev_t *);
127 1.70.2.4 skrll static int pty_alloc_slave(struct lwp *, 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.4 skrll ptsopen(dev, flag, devtype, l)
346 1.1 cgd dev_t dev;
347 1.7 andrew int flag, devtype;
348 1.70.2.4 skrll struct lwp *l;
349 1.1 cgd {
350 1.70.2.4 skrll struct proc *p = l->l_proc;
351 1.27 mycroft struct pt_softc *pti;
352 1.43 augustss struct tty *tp;
353 1.1 cgd int error;
354 1.70.2.2 skrll int ptn = minor(dev);
355 1.70.2.2 skrll int s;
356 1.1 cgd
357 1.70.2.2 skrll if ((error = check_pty(ptn)))
358 1.47 jdolecek return (error);
359 1.47 jdolecek
360 1.70.2.2 skrll pti = pt_softc[ptn];
361 1.47 jdolecek tp = pti->pt_tty;
362 1.47 jdolecek
363 1.37 mycroft if (!ISSET(tp->t_state, TS_ISOPEN)) {
364 1.1 cgd ttychars(tp); /* Set up default chars */
365 1.1 cgd tp->t_iflag = TTYDEF_IFLAG;
366 1.1 cgd tp->t_oflag = TTYDEF_OFLAG;
367 1.1 cgd tp->t_lflag = TTYDEF_LFLAG;
368 1.1 cgd tp->t_cflag = TTYDEF_CFLAG;
369 1.1 cgd tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
370 1.1 cgd ttsetwater(tp); /* would be done in xxparam() */
371 1.37 mycroft } else if (ISSET(tp->t_state, TS_XCLUDE) && p->p_ucred->cr_uid != 0)
372 1.1 cgd return (EBUSY);
373 1.1 cgd if (tp->t_oproc) /* Ctrlr still around. */
374 1.37 mycroft SET(tp->t_state, TS_CARR_ON);
375 1.70.2.2 skrll
376 1.68 pk if (!ISSET(flag, O_NONBLOCK)) {
377 1.70.2.2 skrll s = spltty();
378 1.68 pk TTY_LOCK(tp);
379 1.40 mycroft while (!ISSET(tp->t_state, TS_CARR_ON)) {
380 1.40 mycroft tp->t_wopen++;
381 1.40 mycroft error = ttysleep(tp, &tp->t_rawq, TTIPRI | PCATCH,
382 1.40 mycroft ttopen, 0);
383 1.40 mycroft tp->t_wopen--;
384 1.68 pk if (error) {
385 1.68 pk TTY_UNLOCK(tp);
386 1.70.2.2 skrll splx(s);
387 1.40 mycroft return (error);
388 1.68 pk }
389 1.40 mycroft }
390 1.68 pk TTY_UNLOCK(tp);
391 1.70.2.2 skrll splx(s);
392 1.68 pk }
393 1.50 eeh error = (*tp->t_linesw->l_open)(dev, tp);
394 1.1 cgd ptcwakeup(tp, FREAD|FWRITE);
395 1.22 cgd return (error);
396 1.1 cgd }
397 1.1 cgd
398 1.31 christos int
399 1.70.2.4 skrll ptsclose(dev, flag, mode, l)
400 1.1 cgd dev_t dev;
401 1.1 cgd int flag, mode;
402 1.70.2.4 skrll struct lwp *l;
403 1.1 cgd {
404 1.47 jdolecek struct pt_softc *pti = pt_softc[minor(dev)];
405 1.43 augustss struct tty *tp = pti->pt_tty;
406 1.31 christos int error;
407 1.1 cgd
408 1.50 eeh error = (*tp->t_linesw->l_close)(tp, flag);
409 1.31 christos error |= ttyclose(tp);
410 1.1 cgd ptcwakeup(tp, FREAD|FWRITE);
411 1.31 christos return (error);
412 1.1 cgd }
413 1.1 cgd
414 1.31 christos int
415 1.1 cgd ptsread(dev, uio, flag)
416 1.1 cgd dev_t dev;
417 1.1 cgd struct uio *uio;
418 1.7 andrew int flag;
419 1.1 cgd {
420 1.1 cgd struct proc *p = curproc;
421 1.47 jdolecek struct pt_softc *pti = pt_softc[minor(dev)];
422 1.43 augustss struct tty *tp = pti->pt_tty;
423 1.1 cgd int error = 0;
424 1.68 pk int cc;
425 1.70.2.2 skrll int s;
426 1.1 cgd
427 1.1 cgd again:
428 1.1 cgd if (pti->pt_flags & PF_REMOTE) {
429 1.1 cgd while (isbackground(p, tp)) {
430 1.51 jdolecek if (sigismasked(p, SIGTTIN) ||
431 1.1 cgd p->p_pgrp->pg_jobc == 0 ||
432 1.22 cgd p->p_flag & P_PPWAIT)
433 1.1 cgd return (EIO);
434 1.1 cgd pgsignal(p->p_pgrp, SIGTTIN, 1);
435 1.70.2.2 skrll s = spltty();
436 1.68 pk TTY_LOCK(tp);
437 1.35 pk error = ttysleep(tp, (caddr_t)&lbolt,
438 1.68 pk TTIPRI | PCATCH | PNORELOCK, ttybg, 0);
439 1.70.2.2 skrll splx(s);
440 1.31 christos if (error)
441 1.1 cgd return (error);
442 1.1 cgd }
443 1.70.2.2 skrll s = spltty();
444 1.68 pk TTY_LOCK(tp);
445 1.8 mycroft if (tp->t_canq.c_cc == 0) {
446 1.68 pk if (flag & IO_NDELAY) {
447 1.68 pk TTY_UNLOCK(tp);
448 1.70.2.2 skrll splx(s);
449 1.1 cgd return (EWOULDBLOCK);
450 1.68 pk }
451 1.31 christos error = ttysleep(tp, (caddr_t)&tp->t_canq,
452 1.68 pk TTIPRI | PCATCH | PNORELOCK, ttyin, 0);
453 1.31 christos if (error)
454 1.1 cgd return (error);
455 1.1 cgd goto again;
456 1.1 cgd }
457 1.68 pk while(error == 0 && tp->t_canq.c_cc > 1 && uio->uio_resid > 0) {
458 1.68 pk TTY_UNLOCK(tp);
459 1.70.2.2 skrll splx(s);
460 1.68 pk error = ureadc(getc(&tp->t_canq), uio);
461 1.70.2.2 skrll s = spltty();
462 1.68 pk TTY_LOCK(tp);
463 1.68 pk /* Re-check terminal state here? */
464 1.68 pk }
465 1.8 mycroft if (tp->t_canq.c_cc == 1)
466 1.8 mycroft (void) getc(&tp->t_canq);
467 1.68 pk cc = tp->t_canq.c_cc;
468 1.68 pk TTY_UNLOCK(tp);
469 1.70.2.2 skrll splx(s);
470 1.68 pk if (cc)
471 1.1 cgd return (error);
472 1.1 cgd } else
473 1.1 cgd if (tp->t_oproc)
474 1.50 eeh error = (*tp->t_linesw->l_read)(tp, uio, flag);
475 1.1 cgd ptcwakeup(tp, FWRITE);
476 1.1 cgd return (error);
477 1.1 cgd }
478 1.1 cgd
479 1.1 cgd /*
480 1.1 cgd * Write to pseudo-tty.
481 1.1 cgd * Wakeups of controlling tty will happen
482 1.1 cgd * indirectly, when tty driver calls ptsstart.
483 1.1 cgd */
484 1.31 christos int
485 1.1 cgd ptswrite(dev, uio, flag)
486 1.1 cgd dev_t dev;
487 1.1 cgd struct uio *uio;
488 1.7 andrew int flag;
489 1.1 cgd {
490 1.47 jdolecek struct pt_softc *pti = pt_softc[minor(dev)];
491 1.43 augustss struct tty *tp = pti->pt_tty;
492 1.1 cgd
493 1.1 cgd if (tp->t_oproc == 0)
494 1.1 cgd return (EIO);
495 1.50 eeh return ((*tp->t_linesw->l_write)(tp, uio, flag));
496 1.56 scw }
497 1.56 scw
498 1.56 scw /*
499 1.56 scw * Poll pseudo-tty.
500 1.56 scw */
501 1.56 scw int
502 1.70.2.4 skrll ptspoll(dev, events, l)
503 1.56 scw dev_t dev;
504 1.56 scw int events;
505 1.70.2.4 skrll struct lwp *l;
506 1.56 scw {
507 1.56 scw struct pt_softc *pti = pt_softc[minor(dev)];
508 1.56 scw struct tty *tp = pti->pt_tty;
509 1.56 scw
510 1.56 scw if (tp->t_oproc == 0)
511 1.56 scw return (EIO);
512 1.56 scw
513 1.70.2.4 skrll return ((*tp->t_linesw->l_poll)(tp, events, l));
514 1.1 cgd }
515 1.1 cgd
516 1.1 cgd /*
517 1.1 cgd * Start output on pseudo-tty.
518 1.38 mycroft * Wake up process polling or sleeping for input from controlling tty.
519 1.68 pk * Called with tty slock held.
520 1.1 cgd */
521 1.12 deraadt void
522 1.1 cgd ptsstart(tp)
523 1.1 cgd struct tty *tp;
524 1.1 cgd {
525 1.47 jdolecek struct pt_softc *pti = pt_softc[minor(tp->t_dev)];
526 1.1 cgd
527 1.37 mycroft if (ISSET(tp->t_state, TS_TTSTOP))
528 1.12 deraadt return;
529 1.1 cgd if (pti->pt_flags & PF_STOPPED) {
530 1.1 cgd pti->pt_flags &= ~PF_STOPPED;
531 1.1 cgd pti->pt_send = TIOCPKT_START;
532 1.1 cgd }
533 1.68 pk
534 1.70.2.2 skrll selnotify(&pti->pt_selr, NOTE_SUBMIT);
535 1.68 pk wakeup((caddr_t)&tp->t_outq.c_cf);
536 1.1 cgd }
537 1.1 cgd
538 1.68 pk /*
539 1.68 pk * Stop output.
540 1.68 pk * Called with tty slock held.
541 1.68 pk */
542 1.36 mycroft void
543 1.31 christos ptsstop(tp, flush)
544 1.43 augustss struct tty *tp;
545 1.31 christos int flush;
546 1.31 christos {
547 1.47 jdolecek struct pt_softc *pti = pt_softc[minor(tp->t_dev)];
548 1.31 christos
549 1.31 christos /* note: FLUSHREAD and FLUSHWRITE already ok */
550 1.31 christos if (flush == 0) {
551 1.31 christos flush = TIOCPKT_STOP;
552 1.31 christos pti->pt_flags |= PF_STOPPED;
553 1.31 christos } else
554 1.31 christos pti->pt_flags &= ~PF_STOPPED;
555 1.31 christos pti->pt_send |= flush;
556 1.68 pk
557 1.31 christos /* change of perspective */
558 1.68 pk if (flush & FREAD) {
559 1.70.2.2 skrll selnotify(&pti->pt_selw, NOTE_SUBMIT);
560 1.68 pk wakeup((caddr_t)&tp->t_rawq.c_cf);
561 1.68 pk }
562 1.68 pk if (flush & FWRITE) {
563 1.70.2.2 skrll selnotify(&pti->pt_selr, NOTE_SUBMIT);
564 1.68 pk wakeup((caddr_t)&tp->t_outq.c_cf);
565 1.68 pk }
566 1.31 christos }
567 1.31 christos
568 1.31 christos void
569 1.1 cgd ptcwakeup(tp, flag)
570 1.1 cgd struct tty *tp;
571 1.7 andrew int flag;
572 1.1 cgd {
573 1.47 jdolecek struct pt_softc *pti = pt_softc[minor(tp->t_dev)];
574 1.70.2.2 skrll int s;
575 1.1 cgd
576 1.70.2.2 skrll s = spltty();
577 1.68 pk TTY_LOCK(tp);
578 1.1 cgd if (flag & FREAD) {
579 1.70.2.2 skrll selnotify(&pti->pt_selr, NOTE_SUBMIT);
580 1.22 cgd wakeup((caddr_t)&tp->t_outq.c_cf);
581 1.1 cgd }
582 1.1 cgd if (flag & FWRITE) {
583 1.70.2.2 skrll selnotify(&pti->pt_selw, NOTE_SUBMIT);
584 1.8 mycroft wakeup((caddr_t)&tp->t_rawq.c_cf);
585 1.1 cgd }
586 1.68 pk TTY_UNLOCK(tp);
587 1.70.2.2 skrll splx(s);
588 1.1 cgd }
589 1.1 cgd
590 1.1 cgd /*ARGSUSED*/
591 1.25 mycroft int
592 1.70.2.4 skrll ptcopen(dev, flag, devtype, l)
593 1.1 cgd dev_t dev;
594 1.1 cgd int flag, devtype;
595 1.70.2.4 skrll struct lwp *l;
596 1.1 cgd {
597 1.27 mycroft struct pt_softc *pti;
598 1.43 augustss struct tty *tp;
599 1.47 jdolecek int error;
600 1.70.2.2 skrll int ptn = minor(dev);
601 1.70.2.2 skrll int s;
602 1.47 jdolecek
603 1.70.2.2 skrll if ((error = check_pty(ptn)))
604 1.47 jdolecek return (error);
605 1.47 jdolecek
606 1.70.2.2 skrll pti = pt_softc[ptn];
607 1.47 jdolecek tp = pti->pt_tty;
608 1.1 cgd
609 1.70.2.2 skrll s = spltty();
610 1.70.2.2 skrll TTY_LOCK(tp);
611 1.70.2.2 skrll if (tp->t_oproc) {
612 1.70.2.2 skrll TTY_UNLOCK(tp);
613 1.70.2.2 skrll splx(s);
614 1.1 cgd return (EIO);
615 1.70.2.2 skrll }
616 1.1 cgd tp->t_oproc = ptsstart;
617 1.70.2.2 skrll TTY_UNLOCK(tp);
618 1.70.2.2 skrll splx(s);
619 1.50 eeh (void)(*tp->t_linesw->l_modem)(tp, 1);
620 1.37 mycroft CLR(tp->t_lflag, EXTPROC);
621 1.22 cgd pti->pt_flags = 0;
622 1.1 cgd pti->pt_send = 0;
623 1.1 cgd pti->pt_ucntl = 0;
624 1.1 cgd return (0);
625 1.1 cgd }
626 1.1 cgd
627 1.31 christos /*ARGSUSED*/
628 1.25 mycroft int
629 1.70.2.4 skrll ptcclose(dev, flag, devtype, l)
630 1.1 cgd dev_t dev;
631 1.31 christos int flag, devtype;
632 1.70.2.4 skrll struct lwp *l;
633 1.1 cgd {
634 1.47 jdolecek struct pt_softc *pti = pt_softc[minor(dev)];
635 1.43 augustss struct tty *tp = pti->pt_tty;
636 1.70.2.2 skrll int s;
637 1.1 cgd
638 1.50 eeh (void)(*tp->t_linesw->l_modem)(tp, 0);
639 1.37 mycroft CLR(tp->t_state, TS_CARR_ON);
640 1.70.2.2 skrll s = spltty();
641 1.70.2.2 skrll TTY_LOCK(tp);
642 1.1 cgd tp->t_oproc = 0; /* mark closed */
643 1.70.2.2 skrll TTY_UNLOCK(tp);
644 1.70.2.2 skrll splx(s);
645 1.2 cgd return (0);
646 1.1 cgd }
647 1.1 cgd
648 1.31 christos int
649 1.1 cgd ptcread(dev, uio, flag)
650 1.1 cgd dev_t dev;
651 1.1 cgd struct uio *uio;
652 1.7 andrew int flag;
653 1.1 cgd {
654 1.47 jdolecek struct pt_softc *pti = pt_softc[minor(dev)];
655 1.43 augustss struct tty *tp = pti->pt_tty;
656 1.67 simonb u_char buf[BUFSIZ];
657 1.1 cgd int error = 0, cc;
658 1.70.2.2 skrll int s;
659 1.1 cgd
660 1.1 cgd /*
661 1.1 cgd * We want to block until the slave
662 1.1 cgd * is open, and there's something to read;
663 1.1 cgd * but if we lost the slave or we're NBIO,
664 1.1 cgd * then return the appropriate error instead.
665 1.1 cgd */
666 1.70.2.2 skrll s = spltty();
667 1.68 pk TTY_LOCK(tp);
668 1.1 cgd for (;;) {
669 1.37 mycroft if (ISSET(tp->t_state, TS_ISOPEN)) {
670 1.70.2.2 skrll if (pti->pt_flags & PF_PKT && pti->pt_send) {
671 1.68 pk TTY_UNLOCK(tp);
672 1.70.2.2 skrll splx(s);
673 1.1 cgd error = ureadc((int)pti->pt_send, uio);
674 1.1 cgd if (error)
675 1.1 cgd return (error);
676 1.68 pk /*
677 1.68 pk * Since we don't have the tty locked, there's
678 1.68 pk * a risk of messing up `t_termios'. This is
679 1.68 pk * relevant only if the tty got closed and then
680 1.68 pk * opened again while we were out uiomoving.
681 1.68 pk */
682 1.1 cgd if (pti->pt_send & TIOCPKT_IOCTL) {
683 1.22 cgd cc = min(uio->uio_resid,
684 1.1 cgd sizeof(tp->t_termios));
685 1.31 christos uiomove((caddr_t) &tp->t_termios,
686 1.31 christos cc, uio);
687 1.1 cgd }
688 1.1 cgd pti->pt_send = 0;
689 1.1 cgd return (0);
690 1.1 cgd }
691 1.70.2.2 skrll if (pti->pt_flags & PF_UCNTL && pti->pt_ucntl) {
692 1.68 pk TTY_UNLOCK(tp);
693 1.70.2.2 skrll splx(s);
694 1.1 cgd error = ureadc((int)pti->pt_ucntl, uio);
695 1.1 cgd if (error)
696 1.1 cgd return (error);
697 1.1 cgd pti->pt_ucntl = 0;
698 1.1 cgd return (0);
699 1.1 cgd }
700 1.37 mycroft if (tp->t_outq.c_cc && !ISSET(tp->t_state, TS_TTSTOP))
701 1.1 cgd break;
702 1.1 cgd }
703 1.68 pk if (!ISSET(tp->t_state, TS_CARR_ON)) {
704 1.68 pk error = 0; /* EOF */
705 1.68 pk goto out;
706 1.68 pk }
707 1.68 pk if (flag & IO_NDELAY) {
708 1.68 pk error = EWOULDBLOCK;
709 1.68 pk goto out;
710 1.68 pk }
711 1.68 pk error = ltsleep((caddr_t)&tp->t_outq.c_cf, TTIPRI | PCATCH,
712 1.68 pk ttyin, 0, &tp->t_slock);
713 1.31 christos if (error)
714 1.68 pk goto out;
715 1.1 cgd }
716 1.68 pk
717 1.68 pk if (pti->pt_flags & (PF_PKT|PF_UCNTL)) {
718 1.68 pk TTY_UNLOCK(tp);
719 1.70.2.2 skrll splx(s);
720 1.1 cgd error = ureadc(0, uio);
721 1.70.2.2 skrll s = spltty();
722 1.68 pk TTY_LOCK(tp);
723 1.68 pk if (error == 0 && !ISSET(tp->t_state, TS_ISOPEN))
724 1.68 pk error = EIO;
725 1.68 pk }
726 1.1 cgd while (uio->uio_resid > 0 && error == 0) {
727 1.22 cgd cc = q_to_b(&tp->t_outq, buf, min(uio->uio_resid, BUFSIZ));
728 1.1 cgd if (cc <= 0)
729 1.1 cgd break;
730 1.68 pk TTY_UNLOCK(tp);
731 1.70.2.2 skrll splx(s);
732 1.1 cgd error = uiomove(buf, cc, uio);
733 1.70.2.2 skrll s = spltty();
734 1.68 pk TTY_LOCK(tp);
735 1.68 pk if (error == 0 && !ISSET(tp->t_state, TS_ISOPEN))
736 1.68 pk error = EIO;
737 1.1 cgd }
738 1.68 pk
739 1.8 mycroft if (tp->t_outq.c_cc <= tp->t_lowat) {
740 1.37 mycroft if (ISSET(tp->t_state, TS_ASLEEP)) {
741 1.37 mycroft CLR(tp->t_state, TS_ASLEEP);
742 1.8 mycroft wakeup((caddr_t)&tp->t_outq);
743 1.1 cgd }
744 1.70.2.2 skrll selnotify(&tp->t_wsel, NOTE_SUBMIT);
745 1.1 cgd }
746 1.68 pk out:
747 1.68 pk TTY_UNLOCK(tp);
748 1.70.2.2 skrll splx(s);
749 1.1 cgd return (error);
750 1.1 cgd }
751 1.1 cgd
752 1.1 cgd
753 1.31 christos int
754 1.1 cgd ptcwrite(dev, uio, flag)
755 1.1 cgd dev_t dev;
756 1.43 augustss struct uio *uio;
757 1.7 andrew int flag;
758 1.1 cgd {
759 1.47 jdolecek struct pt_softc *pti = pt_softc[minor(dev)];
760 1.43 augustss struct tty *tp = pti->pt_tty;
761 1.43 augustss u_char *cp = NULL;
762 1.43 augustss int cc = 0;
763 1.1 cgd u_char locbuf[BUFSIZ];
764 1.1 cgd int cnt = 0;
765 1.1 cgd int error = 0;
766 1.70.2.2 skrll int s;
767 1.1 cgd
768 1.1 cgd again:
769 1.70.2.2 skrll s = spltty();
770 1.68 pk TTY_LOCK(tp);
771 1.37 mycroft if (!ISSET(tp->t_state, TS_ISOPEN))
772 1.1 cgd goto block;
773 1.1 cgd if (pti->pt_flags & PF_REMOTE) {
774 1.8 mycroft if (tp->t_canq.c_cc)
775 1.1 cgd goto block;
776 1.8 mycroft while (uio->uio_resid > 0 && tp->t_canq.c_cc < TTYHOG - 1) {
777 1.1 cgd if (cc == 0) {
778 1.1 cgd cc = min(uio->uio_resid, BUFSIZ);
779 1.8 mycroft cc = min(cc, TTYHOG - 1 - tp->t_canq.c_cc);
780 1.1 cgd cp = locbuf;
781 1.68 pk TTY_UNLOCK(tp);
782 1.70.2.2 skrll splx(s);
783 1.1 cgd error = uiomove((caddr_t)cp, cc, uio);
784 1.1 cgd if (error)
785 1.1 cgd return (error);
786 1.70.2.2 skrll s = spltty();
787 1.68 pk TTY_LOCK(tp);
788 1.1 cgd /* check again for safety */
789 1.68 pk if (!ISSET(tp->t_state, TS_ISOPEN)) {
790 1.68 pk error = EIO;
791 1.68 pk goto out;
792 1.68 pk }
793 1.1 cgd }
794 1.1 cgd if (cc)
795 1.67 simonb (void) b_to_q(cp, cc, &tp->t_canq);
796 1.1 cgd cc = 0;
797 1.1 cgd }
798 1.8 mycroft (void) putc(0, &tp->t_canq);
799 1.1 cgd ttwakeup(tp);
800 1.8 mycroft wakeup((caddr_t)&tp->t_canq);
801 1.68 pk error = 0;
802 1.68 pk goto out;
803 1.1 cgd }
804 1.1 cgd while (uio->uio_resid > 0) {
805 1.1 cgd if (cc == 0) {
806 1.1 cgd cc = min(uio->uio_resid, BUFSIZ);
807 1.1 cgd cp = locbuf;
808 1.68 pk TTY_UNLOCK(tp);
809 1.70.2.2 skrll splx(s);
810 1.1 cgd error = uiomove((caddr_t)cp, cc, uio);
811 1.1 cgd if (error)
812 1.1 cgd return (error);
813 1.70.2.2 skrll s = spltty();
814 1.68 pk TTY_LOCK(tp);
815 1.1 cgd /* check again for safety */
816 1.68 pk if (!ISSET(tp->t_state, TS_ISOPEN)) {
817 1.68 pk error = EIO;
818 1.68 pk goto out;
819 1.68 pk }
820 1.1 cgd }
821 1.1 cgd while (cc > 0) {
822 1.8 mycroft if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 &&
823 1.59 christos (tp->t_canq.c_cc > 0 || !ISSET(tp->t_lflag, ICANON))) {
824 1.8 mycroft wakeup((caddr_t)&tp->t_rawq);
825 1.1 cgd goto block;
826 1.1 cgd }
827 1.68 pk /* XXX - should change l_rint to be called with lock
828 1.68 pk * see also tty.c:ttyinput_wlock()
829 1.68 pk */
830 1.68 pk TTY_UNLOCK(tp);
831 1.70.2.2 skrll splx(s);
832 1.50 eeh (*tp->t_linesw->l_rint)(*cp++, tp);
833 1.70.2.2 skrll s = spltty();
834 1.68 pk TTY_LOCK(tp);
835 1.1 cgd cnt++;
836 1.1 cgd cc--;
837 1.1 cgd }
838 1.1 cgd cc = 0;
839 1.1 cgd }
840 1.68 pk error = 0;
841 1.68 pk goto out;
842 1.68 pk
843 1.1 cgd block:
844 1.1 cgd /*
845 1.1 cgd * Come here to wait for slave to open, for space
846 1.1 cgd * in outq, or space in rawq.
847 1.1 cgd */
848 1.68 pk if (!ISSET(tp->t_state, TS_CARR_ON)) {
849 1.68 pk error = EIO;
850 1.68 pk goto out;
851 1.68 pk }
852 1.1 cgd if (flag & IO_NDELAY) {
853 1.1 cgd /* adjust for data copied in but not written */
854 1.1 cgd uio->uio_resid += cc;
855 1.68 pk error = cnt == 0 ? EWOULDBLOCK : 0;
856 1.68 pk goto out;
857 1.1 cgd }
858 1.68 pk error = ltsleep((caddr_t)&tp->t_rawq.c_cf, TTOPRI | PCATCH | PNORELOCK,
859 1.68 pk ttyout, 0, &tp->t_slock);
860 1.70.2.2 skrll splx(s);
861 1.31 christos if (error) {
862 1.1 cgd /* adjust for data copied in but not written */
863 1.1 cgd uio->uio_resid += cc;
864 1.1 cgd return (error);
865 1.1 cgd }
866 1.1 cgd goto again;
867 1.68 pk
868 1.68 pk out:
869 1.68 pk TTY_UNLOCK(tp);
870 1.70.2.2 skrll splx(s);
871 1.68 pk return (error);
872 1.29 mycroft }
873 1.29 mycroft
874 1.31 christos int
875 1.70.2.4 skrll ptcpoll(dev, events, l)
876 1.31 christos dev_t dev;
877 1.38 mycroft int events;
878 1.70.2.4 skrll struct lwp *l;
879 1.31 christos {
880 1.47 jdolecek struct pt_softc *pti = pt_softc[minor(dev)];
881 1.43 augustss struct tty *tp = pti->pt_tty;
882 1.38 mycroft int revents = 0;
883 1.38 mycroft int s = splsoftclock();
884 1.31 christos
885 1.38 mycroft if (events & (POLLIN | POLLRDNORM))
886 1.37 mycroft if (ISSET(tp->t_state, TS_ISOPEN) &&
887 1.38 mycroft ((tp->t_outq.c_cc > 0 && !ISSET(tp->t_state, TS_TTSTOP)) ||
888 1.38 mycroft ((pti->pt_flags & PF_PKT) && pti->pt_send) ||
889 1.38 mycroft ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl)))
890 1.38 mycroft revents |= events & (POLLIN | POLLRDNORM);
891 1.31 christos
892 1.38 mycroft if (events & (POLLOUT | POLLWRNORM))
893 1.37 mycroft if (ISSET(tp->t_state, TS_ISOPEN) &&
894 1.38 mycroft ((pti->pt_flags & PF_REMOTE) ?
895 1.38 mycroft (tp->t_canq.c_cc == 0) :
896 1.38 mycroft ((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG-2) ||
897 1.59 christos (tp->t_canq.c_cc == 0 && ISSET(tp->t_lflag, ICANON)))))
898 1.38 mycroft revents |= events & (POLLOUT | POLLWRNORM);
899 1.31 christos
900 1.38 mycroft if (events & POLLHUP)
901 1.38 mycroft if (!ISSET(tp->t_state, TS_CARR_ON))
902 1.38 mycroft revents |= POLLHUP;
903 1.31 christos
904 1.38 mycroft if (revents == 0) {
905 1.38 mycroft if (events & (POLLIN | POLLHUP | POLLRDNORM))
906 1.70.2.4 skrll selrecord(l, &pti->pt_selr);
907 1.31 christos
908 1.38 mycroft if (events & (POLLOUT | POLLWRNORM))
909 1.70.2.4 skrll selrecord(l, &pti->pt_selw);
910 1.31 christos }
911 1.38 mycroft
912 1.38 mycroft splx(s);
913 1.38 mycroft return (revents);
914 1.31 christos }
915 1.31 christos
916 1.65 jdolecek static void
917 1.65 jdolecek filt_ptcrdetach(struct knote *kn)
918 1.65 jdolecek {
919 1.65 jdolecek struct pt_softc *pti;
920 1.65 jdolecek int s;
921 1.65 jdolecek
922 1.65 jdolecek pti = kn->kn_hook;
923 1.65 jdolecek s = spltty();
924 1.66 christos SLIST_REMOVE(&pti->pt_selr.sel_klist, kn, knote, kn_selnext);
925 1.65 jdolecek splx(s);
926 1.65 jdolecek }
927 1.65 jdolecek
928 1.65 jdolecek static int
929 1.65 jdolecek filt_ptcread(struct knote *kn, long hint)
930 1.65 jdolecek {
931 1.65 jdolecek struct pt_softc *pti;
932 1.65 jdolecek struct tty *tp;
933 1.65 jdolecek int canread;
934 1.65 jdolecek
935 1.65 jdolecek pti = kn->kn_hook;
936 1.65 jdolecek tp = pti->pt_tty;
937 1.65 jdolecek
938 1.65 jdolecek canread = (ISSET(tp->t_state, TS_ISOPEN) &&
939 1.65 jdolecek ((tp->t_outq.c_cc > 0 && !ISSET(tp->t_state, TS_TTSTOP)) ||
940 1.65 jdolecek ((pti->pt_flags & PF_PKT) && pti->pt_send) ||
941 1.65 jdolecek ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl)));
942 1.65 jdolecek
943 1.65 jdolecek if (canread) {
944 1.65 jdolecek /*
945 1.65 jdolecek * c_cc is number of characters after output post-processing;
946 1.65 jdolecek * the amount of data actually read(2) depends on
947 1.65 jdolecek * setting of input flags for the terminal.
948 1.65 jdolecek */
949 1.65 jdolecek kn->kn_data = tp->t_outq.c_cc;
950 1.65 jdolecek if (((pti->pt_flags & PF_PKT) && pti->pt_send) ||
951 1.65 jdolecek ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl))
952 1.65 jdolecek kn->kn_data++;
953 1.65 jdolecek }
954 1.65 jdolecek
955 1.65 jdolecek return (canread);
956 1.65 jdolecek }
957 1.65 jdolecek
958 1.65 jdolecek static void
959 1.65 jdolecek filt_ptcwdetach(struct knote *kn)
960 1.65 jdolecek {
961 1.65 jdolecek struct pt_softc *pti;
962 1.65 jdolecek int s;
963 1.65 jdolecek
964 1.65 jdolecek pti = kn->kn_hook;
965 1.65 jdolecek s = spltty();
966 1.66 christos SLIST_REMOVE(&pti->pt_selw.sel_klist, kn, knote, kn_selnext);
967 1.65 jdolecek splx(s);
968 1.65 jdolecek }
969 1.65 jdolecek
970 1.65 jdolecek static int
971 1.65 jdolecek filt_ptcwrite(struct knote *kn, long hint)
972 1.65 jdolecek {
973 1.65 jdolecek struct pt_softc *pti;
974 1.65 jdolecek struct tty *tp;
975 1.65 jdolecek int canwrite;
976 1.65 jdolecek int nwrite;
977 1.65 jdolecek
978 1.65 jdolecek pti = kn->kn_hook;
979 1.65 jdolecek tp = pti->pt_tty;
980 1.65 jdolecek
981 1.65 jdolecek canwrite = (ISSET(tp->t_state, TS_ISOPEN) &&
982 1.65 jdolecek ((pti->pt_flags & PF_REMOTE) ?
983 1.65 jdolecek (tp->t_canq.c_cc == 0) :
984 1.65 jdolecek ((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG-2) ||
985 1.65 jdolecek (tp->t_canq.c_cc == 0 && ISSET(tp->t_lflag, ICANON)))));
986 1.65 jdolecek
987 1.65 jdolecek if (canwrite) {
988 1.65 jdolecek if (pti->pt_flags & PF_REMOTE)
989 1.65 jdolecek nwrite = tp->t_canq.c_cn;
990 1.65 jdolecek else {
991 1.65 jdolecek /* this is guaranteed to be > 0 due to above check */
992 1.65 jdolecek nwrite = tp->t_canq.c_cn
993 1.65 jdolecek - (tp->t_rawq.c_cc + tp->t_canq.c_cc);
994 1.65 jdolecek }
995 1.65 jdolecek kn->kn_data = nwrite;
996 1.65 jdolecek }
997 1.65 jdolecek
998 1.65 jdolecek return (canwrite);
999 1.65 jdolecek }
1000 1.65 jdolecek
1001 1.65 jdolecek static const struct filterops ptcread_filtops =
1002 1.65 jdolecek { 1, NULL, filt_ptcrdetach, filt_ptcread };
1003 1.65 jdolecek static const struct filterops ptcwrite_filtops =
1004 1.65 jdolecek { 1, NULL, filt_ptcwdetach, filt_ptcwrite };
1005 1.65 jdolecek
1006 1.65 jdolecek int
1007 1.65 jdolecek ptckqfilter(dev_t dev, struct knote *kn)
1008 1.65 jdolecek {
1009 1.65 jdolecek struct pt_softc *pti = pt_softc[minor(dev)];
1010 1.65 jdolecek struct klist *klist;
1011 1.65 jdolecek int s;
1012 1.65 jdolecek
1013 1.65 jdolecek switch (kn->kn_filter) {
1014 1.65 jdolecek case EVFILT_READ:
1015 1.66 christos klist = &pti->pt_selr.sel_klist;
1016 1.65 jdolecek kn->kn_fop = &ptcread_filtops;
1017 1.65 jdolecek break;
1018 1.65 jdolecek case EVFILT_WRITE:
1019 1.66 christos klist = &pti->pt_selw.sel_klist;
1020 1.65 jdolecek kn->kn_fop = &ptcwrite_filtops;
1021 1.65 jdolecek break;
1022 1.65 jdolecek default:
1023 1.65 jdolecek return (1);
1024 1.65 jdolecek }
1025 1.65 jdolecek
1026 1.65 jdolecek kn->kn_hook = pti;
1027 1.65 jdolecek
1028 1.65 jdolecek s = spltty();
1029 1.65 jdolecek SLIST_INSERT_HEAD(klist, kn, kn_selnext);
1030 1.65 jdolecek splx(s);
1031 1.65 jdolecek
1032 1.65 jdolecek return (0);
1033 1.65 jdolecek }
1034 1.31 christos
1035 1.29 mycroft struct tty *
1036 1.29 mycroft ptytty(dev)
1037 1.29 mycroft dev_t dev;
1038 1.29 mycroft {
1039 1.47 jdolecek struct pt_softc *pti = pt_softc[minor(dev)];
1040 1.43 augustss struct tty *tp = pti->pt_tty;
1041 1.29 mycroft
1042 1.29 mycroft return (tp);
1043 1.1 cgd }
1044 1.1 cgd
1045 1.1 cgd /*ARGSUSED*/
1046 1.31 christos int
1047 1.70.2.4 skrll ptyioctl(dev, cmd, data, flag, l)
1048 1.18 mycroft dev_t dev;
1049 1.24 cgd u_long cmd;
1050 1.1 cgd caddr_t data;
1051 1.18 mycroft int flag;
1052 1.70.2.4 skrll struct lwp *l;
1053 1.1 cgd {
1054 1.47 jdolecek struct pt_softc *pti = pt_softc[minor(dev)];
1055 1.43 augustss struct tty *tp = pti->pt_tty;
1056 1.63 gehenna const struct cdevsw *cdev;
1057 1.43 augustss u_char *cc = tp->t_cc;
1058 1.44 fvdl int stop, error, sig;
1059 1.70.2.2 skrll int s;
1060 1.1 cgd
1061 1.63 gehenna cdev = cdevsw_lookup(dev);
1062 1.1 cgd /*
1063 1.1 cgd * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
1064 1.1 cgd * ttywflush(tp) will hang if there are characters in the outq.
1065 1.1 cgd */
1066 1.1 cgd if (cmd == TIOCEXT) {
1067 1.1 cgd /*
1068 1.1 cgd * When the EXTPROC bit is being toggled, we need
1069 1.1 cgd * to send an TIOCPKT_IOCTL if the packet driver
1070 1.1 cgd * is turned on.
1071 1.1 cgd */
1072 1.1 cgd if (*(int *)data) {
1073 1.1 cgd if (pti->pt_flags & PF_PKT) {
1074 1.1 cgd pti->pt_send |= TIOCPKT_IOCTL;
1075 1.1 cgd ptcwakeup(tp, FREAD);
1076 1.1 cgd }
1077 1.37 mycroft SET(tp->t_lflag, EXTPROC);
1078 1.1 cgd } else {
1079 1.39 fvdl if (ISSET(tp->t_lflag, EXTPROC) &&
1080 1.1 cgd (pti->pt_flags & PF_PKT)) {
1081 1.1 cgd pti->pt_send |= TIOCPKT_IOCTL;
1082 1.1 cgd ptcwakeup(tp, FREAD);
1083 1.1 cgd }
1084 1.37 mycroft CLR(tp->t_lflag, EXTPROC);
1085 1.1 cgd }
1086 1.1 cgd return(0);
1087 1.70.2.2 skrll }
1088 1.70.2.2 skrll
1089 1.63 gehenna if (cdev != NULL && cdev->d_open == ptcopen)
1090 1.1 cgd switch (cmd) {
1091 1.70.2.2 skrll #ifndef NO_DEV_PTM
1092 1.70.2.2 skrll case TIOCGRANTPT:
1093 1.70.2.4 skrll return pty_grant_slave(l, dev);
1094 1.70.2.2 skrll
1095 1.70.2.2 skrll case TIOCPTSNAME:
1096 1.70.2.2 skrll pty_fill_ptmget(dev, -1, -1, data);
1097 1.70.2.2 skrll return 0;
1098 1.70.2.2 skrll #endif
1099 1.1 cgd
1100 1.1 cgd case TIOCGPGRP:
1101 1.35 pk /*
1102 1.35 pk * We avoid calling ttioctl on the controller since,
1103 1.1 cgd * in that case, tp must be the controlling terminal.
1104 1.1 cgd */
1105 1.1 cgd *(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
1106 1.1 cgd return (0);
1107 1.1 cgd
1108 1.1 cgd case TIOCPKT:
1109 1.1 cgd if (*(int *)data) {
1110 1.1 cgd if (pti->pt_flags & PF_UCNTL)
1111 1.1 cgd return (EINVAL);
1112 1.1 cgd pti->pt_flags |= PF_PKT;
1113 1.1 cgd } else
1114 1.1 cgd pti->pt_flags &= ~PF_PKT;
1115 1.1 cgd return (0);
1116 1.1 cgd
1117 1.1 cgd case TIOCUCNTL:
1118 1.1 cgd if (*(int *)data) {
1119 1.1 cgd if (pti->pt_flags & PF_PKT)
1120 1.1 cgd return (EINVAL);
1121 1.1 cgd pti->pt_flags |= PF_UCNTL;
1122 1.1 cgd } else
1123 1.1 cgd pti->pt_flags &= ~PF_UCNTL;
1124 1.1 cgd return (0);
1125 1.1 cgd
1126 1.1 cgd case TIOCREMOTE:
1127 1.1 cgd if (*(int *)data)
1128 1.1 cgd pti->pt_flags |= PF_REMOTE;
1129 1.1 cgd else
1130 1.1 cgd pti->pt_flags &= ~PF_REMOTE;
1131 1.70.2.2 skrll s = spltty();
1132 1.68 pk TTY_LOCK(tp);
1133 1.1 cgd ttyflush(tp, FREAD|FWRITE);
1134 1.68 pk TTY_UNLOCK(tp);
1135 1.70.2.2 skrll splx(s);
1136 1.1 cgd return (0);
1137 1.1 cgd
1138 1.32 christos #ifdef COMPAT_OLDTTY
1139 1.35 pk case TIOCSETP:
1140 1.1 cgd case TIOCSETN:
1141 1.2 cgd #endif
1142 1.1 cgd case TIOCSETD:
1143 1.1 cgd case TIOCSETA:
1144 1.1 cgd case TIOCSETAW:
1145 1.1 cgd case TIOCSETAF:
1146 1.68 pk TTY_LOCK(tp);
1147 1.21 cgd ndflush(&tp->t_outq, tp->t_outq.c_cc);
1148 1.68 pk TTY_UNLOCK(tp);
1149 1.1 cgd break;
1150 1.1 cgd
1151 1.1 cgd case TIOCSIG:
1152 1.46 mrg sig = (int)(long)*(caddr_t *)data;
1153 1.44 fvdl if (sig <= 0 || sig >= NSIG)
1154 1.44 fvdl return (EINVAL);
1155 1.68 pk TTY_LOCK(tp);
1156 1.37 mycroft if (!ISSET(tp->t_lflag, NOFLSH))
1157 1.1 cgd ttyflush(tp, FREAD|FWRITE);
1158 1.44 fvdl if ((sig == SIGINFO) &&
1159 1.37 mycroft (!ISSET(tp->t_lflag, NOKERNINFO)))
1160 1.1 cgd ttyinfo(tp);
1161 1.68 pk TTY_UNLOCK(tp);
1162 1.62 itohy pgsignal(tp->t_pgrp, sig, 1);
1163 1.1 cgd return(0);
1164 1.1 cgd }
1165 1.70.2.2 skrll
1166 1.70.2.4 skrll error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
1167 1.61 atatat if (error == EPASSTHROUGH)
1168 1.70.2.4 skrll error = ttioctl(tp, cmd, data, flag, l);
1169 1.61 atatat if (error == EPASSTHROUGH) {
1170 1.1 cgd if (pti->pt_flags & PF_UCNTL &&
1171 1.1 cgd (cmd & ~0xff) == UIOCCMD(0)) {
1172 1.1 cgd if (cmd & 0xff) {
1173 1.1 cgd pti->pt_ucntl = (u_char)cmd;
1174 1.1 cgd ptcwakeup(tp, FREAD);
1175 1.1 cgd }
1176 1.1 cgd return (0);
1177 1.1 cgd }
1178 1.1 cgd }
1179 1.1 cgd /*
1180 1.1 cgd * If external processing and packet mode send ioctl packet.
1181 1.1 cgd */
1182 1.37 mycroft if (ISSET(tp->t_lflag, EXTPROC) && (pti->pt_flags & PF_PKT)) {
1183 1.1 cgd switch(cmd) {
1184 1.1 cgd case TIOCSETA:
1185 1.1 cgd case TIOCSETAW:
1186 1.1 cgd case TIOCSETAF:
1187 1.32 christos #ifdef COMPAT_OLDTTY
1188 1.1 cgd case TIOCSETP:
1189 1.1 cgd case TIOCSETN:
1190 1.1 cgd case TIOCSETC:
1191 1.1 cgd case TIOCSLTC:
1192 1.1 cgd case TIOCLBIS:
1193 1.1 cgd case TIOCLBIC:
1194 1.1 cgd case TIOCLSET:
1195 1.1 cgd #endif
1196 1.1 cgd pti->pt_send |= TIOCPKT_IOCTL;
1197 1.22 cgd ptcwakeup(tp, FREAD);
1198 1.1 cgd default:
1199 1.1 cgd break;
1200 1.1 cgd }
1201 1.1 cgd }
1202 1.37 mycroft stop = ISSET(tp->t_iflag, IXON) && CCEQ(cc[VSTOP], CTRL('s'))
1203 1.1 cgd && CCEQ(cc[VSTART], CTRL('q'));
1204 1.1 cgd if (pti->pt_flags & PF_NOSTOP) {
1205 1.1 cgd if (stop) {
1206 1.1 cgd pti->pt_send &= ~TIOCPKT_NOSTOP;
1207 1.1 cgd pti->pt_send |= TIOCPKT_DOSTOP;
1208 1.1 cgd pti->pt_flags &= ~PF_NOSTOP;
1209 1.1 cgd ptcwakeup(tp, FREAD);
1210 1.1 cgd }
1211 1.1 cgd } else {
1212 1.1 cgd if (!stop) {
1213 1.1 cgd pti->pt_send &= ~TIOCPKT_DOSTOP;
1214 1.1 cgd pti->pt_send |= TIOCPKT_NOSTOP;
1215 1.1 cgd pti->pt_flags |= PF_NOSTOP;
1216 1.1 cgd ptcwakeup(tp, FREAD);
1217 1.1 cgd }
1218 1.1 cgd }
1219 1.1 cgd return (error);
1220 1.1 cgd }
1221 1.70.2.2 skrll
1222 1.70.2.2 skrll #ifndef NO_DEV_PTM
1223 1.70.2.2 skrll /*
1224 1.70.2.2 skrll * Check if a pty is free to use.
1225 1.70.2.2 skrll */
1226 1.70.2.2 skrll static __inline int
1227 1.70.2.2 skrll pty_isfree_locked(int minor)
1228 1.70.2.2 skrll {
1229 1.70.2.2 skrll struct pt_softc *pt = pt_softc[minor];
1230 1.70.2.2 skrll return (pt == NULL || pt->pt_tty == NULL ||
1231 1.70.2.2 skrll pt->pt_tty->t_oproc == NULL);
1232 1.70.2.2 skrll }
1233 1.70.2.2 skrll
1234 1.70.2.2 skrll static int
1235 1.70.2.2 skrll pty_isfree(int minor)
1236 1.70.2.2 skrll {
1237 1.70.2.2 skrll int isfree;
1238 1.70.2.2 skrll
1239 1.70.2.2 skrll simple_lock(&pt_softc_mutex);
1240 1.70.2.2 skrll isfree = pty_isfree_locked(minor);
1241 1.70.2.2 skrll simple_unlock(&pt_softc_mutex);
1242 1.70.2.2 skrll return(isfree);
1243 1.70.2.2 skrll }
1244 1.70.2.2 skrll
1245 1.70.2.2 skrll static char *
1246 1.70.2.2 skrll pty_makename(char *buf, dev_t dev, char c)
1247 1.70.2.2 skrll {
1248 1.70.2.2 skrll size_t nt;
1249 1.70.2.2 skrll dev_t minor = minor(dev);
1250 1.70.2.2 skrll
1251 1.70.2.2 skrll (void)memcpy(buf, TTY_TEMPLATE, TTY_NAMESIZE);
1252 1.70.2.2 skrll
1253 1.70.2.2 skrll buf[5] = c;
1254 1.70.2.2 skrll
1255 1.70.2.2 skrll if (minor < 256) {
1256 1.70.2.2 skrll nt = sizeof(TTY_OLD_SUFFIX) - 1;
1257 1.70.2.2 skrll buf[8] = TTY_LETTERS[minor / nt];
1258 1.70.2.2 skrll buf[9] = TTY_OLD_SUFFIX[minor % nt];
1259 1.70.2.2 skrll } else {
1260 1.70.2.2 skrll minor -= 256;
1261 1.70.2.2 skrll nt = sizeof(TTY_NEW_SUFFIX) - sizeof(TTY_OLD_SUFFIX);
1262 1.70.2.2 skrll buf[8] = TTY_LETTERS[minor / nt];
1263 1.70.2.2 skrll buf[9] = TTY_NEW_SUFFIX[minor % nt];
1264 1.70.2.2 skrll }
1265 1.70.2.2 skrll return buf;
1266 1.70.2.2 skrll }
1267 1.70.2.2 skrll
1268 1.70.2.2 skrll static dev_t
1269 1.70.2.2 skrll pty_getfree(void)
1270 1.70.2.2 skrll {
1271 1.70.2.2 skrll int i;
1272 1.70.2.2 skrll
1273 1.70.2.2 skrll simple_lock(&pt_softc_mutex);
1274 1.70.2.2 skrll for (i = 0; i < npty; i++) {
1275 1.70.2.2 skrll if (pty_isfree_locked(i))
1276 1.70.2.2 skrll break;
1277 1.70.2.2 skrll }
1278 1.70.2.2 skrll simple_unlock(&pt_softc_mutex);
1279 1.70.2.2 skrll return (makedev(pts_major, i));
1280 1.70.2.2 skrll }
1281 1.70.2.2 skrll
1282 1.70.2.2 skrll /*
1283 1.70.2.2 skrll * Hacked up version of vn_open. We _only_ handle ptys and only open
1284 1.70.2.2 skrll * them with FREAD|FWRITE and never deal with creat or stuff like that.
1285 1.70.2.2 skrll *
1286 1.70.2.2 skrll * We need it because we have to fake up root credentials to open the pty.
1287 1.70.2.2 skrll */
1288 1.70.2.2 skrll static int
1289 1.70.2.2 skrll ptm_vn_open(struct nameidata *ndp)
1290 1.70.2.2 skrll {
1291 1.70.2.2 skrll struct vnode *vp;
1292 1.70.2.4 skrll struct lwp *l = ndp->ni_cnd.cn_lwp;
1293 1.70.2.2 skrll struct ucred *cred;
1294 1.70.2.2 skrll int error;
1295 1.70.2.2 skrll
1296 1.70.2.2 skrll if ((error = namei(ndp)) != 0)
1297 1.70.2.2 skrll return (error);
1298 1.70.2.2 skrll vp = ndp->ni_vp;
1299 1.70.2.2 skrll if (vp->v_type != VCHR) {
1300 1.70.2.2 skrll error = EINVAL;
1301 1.70.2.2 skrll goto bad;
1302 1.70.2.2 skrll }
1303 1.70.2.2 skrll
1304 1.70.2.2 skrll /*
1305 1.70.2.2 skrll * Get us a fresh cred with root privileges.
1306 1.70.2.2 skrll */
1307 1.70.2.2 skrll cred = crget();
1308 1.70.2.4 skrll error = VOP_OPEN(vp, FREAD|FWRITE, cred, l);
1309 1.70.2.2 skrll crfree(cred);
1310 1.70.2.2 skrll
1311 1.70.2.2 skrll if (error)
1312 1.70.2.2 skrll goto bad;
1313 1.70.2.2 skrll
1314 1.70.2.2 skrll vp->v_writecount++;
1315 1.70.2.2 skrll
1316 1.70.2.2 skrll return (0);
1317 1.70.2.2 skrll bad:
1318 1.70.2.2 skrll vput(vp);
1319 1.70.2.2 skrll return (error);
1320 1.70.2.2 skrll }
1321 1.70.2.2 skrll
1322 1.70.2.2 skrll static int
1323 1.70.2.4 skrll pty_alloc_master(struct lwp *l, int *fd, dev_t *dev)
1324 1.70.2.2 skrll {
1325 1.70.2.2 skrll int error;
1326 1.70.2.2 skrll struct nameidata nd;
1327 1.70.2.2 skrll struct pt_softc *pti;
1328 1.70.2.2 skrll struct file *fp;
1329 1.70.2.4 skrll struct proc *p = l->l_proc;
1330 1.70.2.2 skrll int md;
1331 1.70.2.2 skrll char name[TTY_NAMESIZE];
1332 1.70.2.2 skrll
1333 1.70.2.2 skrll if ((error = falloc(p, &fp, fd)) != 0) {
1334 1.70.2.2 skrll DPRINTF(("falloc %d\n", error));
1335 1.70.2.2 skrll return error;
1336 1.70.2.2 skrll }
1337 1.70.2.2 skrll retry:
1338 1.70.2.2 skrll /* Find and open a free master pty. */
1339 1.70.2.2 skrll *dev = pty_getfree();
1340 1.70.2.2 skrll md = minor(*dev);
1341 1.70.2.2 skrll if ((error = check_pty(md)) != 0) {
1342 1.70.2.2 skrll DPRINTF(("ckeck_pty %d\n", error));
1343 1.70.2.2 skrll goto bad;
1344 1.70.2.2 skrll }
1345 1.70.2.2 skrll pti = pt_softc[md];
1346 1.70.2.2 skrll NDINIT(&nd, LOOKUP, NOFOLLOW|LOCKLEAF, UIO_SYSSPACE,
1347 1.70.2.4 skrll pty_makename(name, *dev, 'p'), l);
1348 1.70.2.2 skrll if ((error = ptm_vn_open(&nd)) != 0) {
1349 1.70.2.2 skrll /*
1350 1.70.2.2 skrll * Check if the master open failed because we lost
1351 1.70.2.2 skrll * the race to grab it.
1352 1.70.2.2 skrll */
1353 1.70.2.2 skrll if (error == EIO && !pty_isfree(md))
1354 1.70.2.2 skrll goto retry;
1355 1.70.2.2 skrll DPRINTF(("ptm_vn_open %d\n", error));
1356 1.70.2.2 skrll goto bad;
1357 1.70.2.2 skrll }
1358 1.70.2.2 skrll fp->f_flag = FREAD|FWRITE;
1359 1.70.2.2 skrll fp->f_type = DTYPE_VNODE;
1360 1.70.2.2 skrll fp->f_ops = &vnops;
1361 1.70.2.2 skrll fp->f_data = nd.ni_vp;
1362 1.70.2.2 skrll VOP_UNLOCK(nd.ni_vp, 0);
1363 1.70.2.2 skrll FILE_SET_MATURE(fp);
1364 1.70.2.4 skrll FILE_UNUSE(fp, l);
1365 1.70.2.2 skrll return 0;
1366 1.70.2.2 skrll bad:
1367 1.70.2.4 skrll FILE_UNUSE(fp, l);
1368 1.70.2.2 skrll fdremove(p->p_fd, *fd);
1369 1.70.2.2 skrll ffree(fp);
1370 1.70.2.2 skrll return error;
1371 1.70.2.2 skrll }
1372 1.70.2.2 skrll
1373 1.70.2.2 skrll static int
1374 1.70.2.4 skrll pty_grant_slave(struct lwp *l, dev_t dev)
1375 1.70.2.2 skrll {
1376 1.70.2.2 skrll int error;
1377 1.70.2.2 skrll struct nameidata nd;
1378 1.70.2.2 skrll char name[TTY_NAMESIZE];
1379 1.70.2.2 skrll
1380 1.70.2.2 skrll /*
1381 1.70.2.2 skrll * Open the slave.
1382 1.70.2.2 skrll * namei -> setattr -> unlock -> revoke -> vrele ->
1383 1.70.2.2 skrll * namei -> open -> unlock
1384 1.70.2.2 skrll * Three stage rocket:
1385 1.70.2.2 skrll * 1. Change the owner and permissions on the slave.
1386 1.70.2.2 skrll * 2. Revoke all the users of the slave.
1387 1.70.2.2 skrll * 3. open the slave.
1388 1.70.2.2 skrll */
1389 1.70.2.2 skrll NDINIT(&nd, LOOKUP, NOFOLLOW|LOCKLEAF, UIO_SYSSPACE,
1390 1.70.2.4 skrll pty_makename(name, dev, 't'), l);
1391 1.70.2.2 skrll if ((error = namei(&nd)) != 0) {
1392 1.70.2.2 skrll DPRINTF(("namei %d\n", error));
1393 1.70.2.2 skrll return error;
1394 1.70.2.2 skrll }
1395 1.70.2.2 skrll if ((nd.ni_vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
1396 1.70.2.2 skrll struct vattr vattr;
1397 1.70.2.2 skrll struct ucred *cred;
1398 1.70.2.2 skrll gid_t gid = _TTY_GID;
1399 1.70.2.2 skrll /* get real uid */
1400 1.70.2.4 skrll uid_t uid = l->l_proc->p_cred->p_ruid;
1401 1.70.2.2 skrll
1402 1.70.2.2 skrll VATTR_NULL(&vattr);
1403 1.70.2.2 skrll vattr.va_uid = uid;
1404 1.70.2.2 skrll vattr.va_gid = gid;
1405 1.70.2.2 skrll vattr.va_mode = (S_IRUSR|S_IWUSR|S_IWGRP) & ALLPERMS;
1406 1.70.2.2 skrll /* Get a fake cred to pretend we're root. */
1407 1.70.2.2 skrll cred = crget();
1408 1.70.2.4 skrll error = VOP_SETATTR(nd.ni_vp, &vattr, cred, l);
1409 1.70.2.2 skrll crfree(cred);
1410 1.70.2.2 skrll if (error) {
1411 1.70.2.2 skrll DPRINTF(("setattr %d\n", error));
1412 1.70.2.2 skrll VOP_UNLOCK(nd.ni_vp, 0);
1413 1.70.2.2 skrll vrele(nd.ni_vp);
1414 1.70.2.2 skrll return error;
1415 1.70.2.2 skrll }
1416 1.70.2.2 skrll }
1417 1.70.2.2 skrll VOP_UNLOCK(nd.ni_vp, 0);
1418 1.70.2.2 skrll if (nd.ni_vp->v_usecount > 1 ||
1419 1.70.2.2 skrll (nd.ni_vp->v_flag & (VALIASED | VLAYER)))
1420 1.70.2.2 skrll VOP_REVOKE(nd.ni_vp, REVOKEALL);
1421 1.70.2.2 skrll
1422 1.70.2.2 skrll /*
1423 1.70.2.2 skrll * The vnode is useless after the revoke, we need to
1424 1.70.2.2 skrll * namei again.
1425 1.70.2.2 skrll */
1426 1.70.2.2 skrll vrele(nd.ni_vp);
1427 1.70.2.2 skrll return 0;
1428 1.70.2.2 skrll }
1429 1.70.2.2 skrll
1430 1.70.2.2 skrll static int
1431 1.70.2.4 skrll pty_alloc_slave(struct lwp *l, int *fd, dev_t dev)
1432 1.70.2.2 skrll {
1433 1.70.2.2 skrll int error;
1434 1.70.2.2 skrll struct file *fp;
1435 1.70.2.4 skrll struct proc *p = l->l_proc;
1436 1.70.2.2 skrll struct nameidata nd;
1437 1.70.2.2 skrll char name[TTY_NAMESIZE];
1438 1.70.2.2 skrll
1439 1.70.2.2 skrll /* Grab a filedescriptor for the slave */
1440 1.70.2.2 skrll if ((error = falloc(p, &fp, fd)) != 0) {
1441 1.70.2.2 skrll DPRINTF(("falloc %d\n", error));
1442 1.70.2.2 skrll return error;
1443 1.70.2.2 skrll }
1444 1.70.2.2 skrll
1445 1.70.2.2 skrll NDINIT(&nd, LOOKUP, NOFOLLOW|LOCKLEAF, UIO_SYSSPACE,
1446 1.70.2.4 skrll pty_makename(name, dev, 't'), l);
1447 1.70.2.2 skrll
1448 1.70.2.2 skrll /* now open it */
1449 1.70.2.2 skrll if ((error = ptm_vn_open(&nd)) != 0) {
1450 1.70.2.2 skrll DPRINTF(("vn_open %d\n", error));
1451 1.70.2.4 skrll FILE_UNUSE(fp, l);
1452 1.70.2.2 skrll fdremove(p->p_fd, *fd);
1453 1.70.2.2 skrll ffree(fp);
1454 1.70.2.2 skrll return error;
1455 1.70.2.2 skrll }
1456 1.70.2.2 skrll fp->f_flag = FREAD|FWRITE;
1457 1.70.2.2 skrll fp->f_type = DTYPE_VNODE;
1458 1.70.2.2 skrll fp->f_ops = &vnops;
1459 1.70.2.2 skrll fp->f_data = nd.ni_vp;
1460 1.70.2.2 skrll VOP_UNLOCK(nd.ni_vp, 0);
1461 1.70.2.2 skrll FILE_SET_MATURE(fp);
1462 1.70.2.4 skrll FILE_UNUSE(fp, l);
1463 1.70.2.2 skrll return 0;
1464 1.70.2.2 skrll }
1465 1.70.2.2 skrll
1466 1.70.2.2 skrll static void
1467 1.70.2.2 skrll pty_fill_ptmget(dev_t dev, int cfd, int sfd, void *data)
1468 1.70.2.2 skrll {
1469 1.70.2.2 skrll struct ptmget *ptm = data;
1470 1.70.2.2 skrll ptm->cfd = cfd;
1471 1.70.2.2 skrll ptm->sfd = sfd;
1472 1.70.2.2 skrll (void)pty_makename(ptm->cn, dev, 'p');
1473 1.70.2.2 skrll (void)pty_makename(ptm->sn, dev, 't');
1474 1.70.2.2 skrll }
1475 1.70.2.2 skrll
1476 1.70.2.2 skrll void
1477 1.70.2.2 skrll /*ARGSUSED*/
1478 1.70.2.2 skrll ptmattach(int n)
1479 1.70.2.2 skrll {
1480 1.70.2.2 skrll /* find the major and minor of the pty devices */
1481 1.70.2.2 skrll if ((pts_major = cdevsw_lookup_major(&pts_cdevsw)) == -1)
1482 1.70.2.2 skrll panic("ptmattach: Can't find pty slave in cdevsw");
1483 1.70.2.2 skrll }
1484 1.70.2.2 skrll
1485 1.70.2.2 skrll int
1486 1.70.2.2 skrll /*ARGSUSED*/
1487 1.70.2.4 skrll ptmopen(dev_t dev, int flag, int mode, struct lwp *l)
1488 1.70.2.2 skrll {
1489 1.70.2.2 skrll int error;
1490 1.70.2.2 skrll int fd;
1491 1.70.2.2 skrll
1492 1.70.2.2 skrll switch(minor(dev)) {
1493 1.70.2.2 skrll case 0: /* /dev/ptmx */
1494 1.70.2.4 skrll if ((error = pty_alloc_master(l, &fd, &dev)) != 0)
1495 1.70.2.2 skrll return error;
1496 1.70.2.2 skrll curlwp->l_dupfd = fd;
1497 1.70.2.2 skrll return ENXIO;
1498 1.70.2.2 skrll case 1: /* /dev/ptm */
1499 1.70.2.2 skrll return 0;
1500 1.70.2.2 skrll default:
1501 1.70.2.2 skrll return ENODEV;
1502 1.70.2.2 skrll }
1503 1.70.2.2 skrll
1504 1.70.2.2 skrll }
1505 1.70.2.2 skrll
1506 1.70.2.2 skrll int
1507 1.70.2.2 skrll /*ARGSUSED*/
1508 1.70.2.4 skrll ptmclose(dev_t dev, int flag, int mode, struct lwp *l)
1509 1.70.2.2 skrll {
1510 1.70.2.2 skrll return (0);
1511 1.70.2.2 skrll }
1512 1.70.2.2 skrll
1513 1.70.2.2 skrll int
1514 1.70.2.2 skrll /*ARGSUSED*/
1515 1.70.2.4 skrll ptmioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct lwp *l)
1516 1.70.2.2 skrll {
1517 1.70.2.2 skrll int error;
1518 1.70.2.2 skrll dev_t newdev;
1519 1.70.2.2 skrll int cfd, sfd;
1520 1.70.2.2 skrll struct file *fp;
1521 1.70.2.4 skrll struct proc *p = l->l_proc;
1522 1.70.2.2 skrll
1523 1.70.2.2 skrll error = 0;
1524 1.70.2.2 skrll switch (cmd) {
1525 1.70.2.2 skrll case TIOCPTMGET:
1526 1.70.2.4 skrll if ((error = pty_alloc_master(l, &cfd, &newdev)) != 0)
1527 1.70.2.2 skrll return error;
1528 1.70.2.2 skrll
1529 1.70.2.4 skrll if ((error = pty_grant_slave(l, newdev)) != 0)
1530 1.70.2.2 skrll goto bad;
1531 1.70.2.2 skrll
1532 1.70.2.4 skrll if ((error = pty_alloc_slave(l, &sfd, newdev)) != 0)
1533 1.70.2.2 skrll goto bad;
1534 1.70.2.2 skrll
1535 1.70.2.2 skrll /* now, put the indices and names into struct ptmget */
1536 1.70.2.2 skrll pty_fill_ptmget(newdev, cfd, sfd, data);
1537 1.70.2.2 skrll return 0;
1538 1.70.2.2 skrll default:
1539 1.70.2.2 skrll DPRINTF(("ptmioctl EINVAL\n"));
1540 1.70.2.2 skrll return EINVAL;
1541 1.70.2.2 skrll }
1542 1.70.2.2 skrll bad:
1543 1.70.2.2 skrll fp = fd_getfile(p->p_fd, cfd);
1544 1.70.2.2 skrll fdremove(p->p_fd, cfd);
1545 1.70.2.2 skrll ffree(fp);
1546 1.70.2.2 skrll return error;
1547 1.70.2.2 skrll }
1548 1.70.2.2 skrll #endif
1549