tty_pty.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197
  1. /* $OpenBSD: tty_pty.c,v 1.70 2015/02/10 21:56:10 miod Exp $ */
  2. /* $NetBSD: tty_pty.c,v 1.33.4.1 1996/06/02 09:08:11 mrg Exp $ */
  3. /*
  4. * Copyright (c) 1982, 1986, 1989, 1993
  5. * The Regents of the University of California. All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of the University nor the names of its contributors
  16. * may be used to endorse or promote products derived from this software
  17. * without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  23. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  25. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  26. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  27. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  28. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  29. * SUCH DAMAGE.
  30. *
  31. * @(#)tty_pty.c 8.4 (Berkeley) 2/20/95
  32. */
  33. /*
  34. * Pseudo-teletype Driver
  35. * (Actually two drivers, requiring two entries in 'cdevsw')
  36. */
  37. #include <sys/param.h>
  38. #include <sys/systm.h>
  39. #include <sys/namei.h>
  40. #include <sys/mount.h>
  41. #include <sys/ioctl.h>
  42. #include <sys/proc.h>
  43. #include <sys/tty.h>
  44. #include <sys/file.h>
  45. #include <sys/filedesc.h>
  46. #include <sys/uio.h>
  47. #include <sys/kernel.h>
  48. #include <sys/malloc.h>
  49. #include <sys/vnode.h>
  50. #include <sys/signalvar.h>
  51. #include <sys/conf.h>
  52. #include <sys/stat.h>
  53. #include <sys/sysctl.h>
  54. #include <sys/poll.h>
  55. #include <sys/rwlock.h>
  56. #define BUFSIZ 100 /* Chunk size iomoved to/from user */
  57. /*
  58. * pts == /dev/tty[p-zP-T][0-9a-zA-Z]
  59. * ptc == /dev/pty[p-zP-T][0-9a-zA-Z]
  60. */
  61. /* XXX this needs to come from somewhere sane, and work with MAKEDEV */
  62. #define TTY_LETTERS "pqrstuvwxyzPQRST"
  63. #define TTY_SUFFIX "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
  64. static int pts_major;
  65. struct pt_softc {
  66. struct tty *pt_tty;
  67. int pt_flags;
  68. struct selinfo pt_selr, pt_selw;
  69. u_char pt_send;
  70. u_char pt_ucntl;
  71. char pty_pn[11];
  72. char pty_sn[11];
  73. };
  74. #define NPTY_MIN 8 /* number of initial ptys */
  75. #define NPTY_MAX 992 /* maximum number of ptys supported */
  76. static struct pt_softc **pt_softc = NULL; /* pty array */
  77. static int npty = 0; /* size of pty array */
  78. static int maxptys = NPTY_MAX; /* maximum number of ptys */
  79. /* for pty array */
  80. struct rwlock pt_softc_lock = RWLOCK_INITIALIZER("ptarrlk");
  81. #define PF_PKT 0x08 /* packet mode */
  82. #define PF_STOPPED 0x10 /* user told stopped */
  83. #define PF_REMOTE 0x20 /* remote and flow controlled input */
  84. #define PF_NOSTOP 0x40
  85. #define PF_UCNTL 0x80 /* user control mode */
  86. void ptyattach(int);
  87. void ptcwakeup(struct tty *, int);
  88. struct tty *ptytty(dev_t);
  89. void ptsstart(struct tty *);
  90. int sysctl_pty(int *, u_int, void *, size_t *, void *, size_t);
  91. void filt_ptcrdetach(struct knote *);
  92. int filt_ptcread(struct knote *, long);
  93. void filt_ptcwdetach(struct knote *);
  94. int filt_ptcwrite(struct knote *, long);
  95. static struct pt_softc **ptyarralloc(int);
  96. static int check_pty(int);
  97. static gid_t tty_gid = TTY_GID;
  98. void ptydevname(int, struct pt_softc *);
  99. dev_t pty_getfree(void);
  100. void ptmattach(int);
  101. int ptmopen(dev_t, int, int, struct proc *);
  102. int ptmclose(dev_t, int, int, struct proc *);
  103. int ptmioctl(dev_t, u_long, caddr_t, int, struct proc *p);
  104. static int ptm_vn_open(struct nameidata *);
  105. void
  106. ptydevname(int minor, struct pt_softc *pti)
  107. {
  108. char buf[11] = "/dev/XtyXX";
  109. int i, j;
  110. i = minor / (sizeof(TTY_SUFFIX) - 1);
  111. j = minor % (sizeof(TTY_SUFFIX) - 1);
  112. if (i >= sizeof(TTY_LETTERS) - 1) {
  113. pti->pty_pn[0] = '\0';
  114. pti->pty_sn[0] = '\0';
  115. return;
  116. }
  117. buf[5] = 'p';
  118. buf[8] = TTY_LETTERS[i];
  119. buf[9] = TTY_SUFFIX[j];
  120. memcpy(pti->pty_pn, buf, sizeof(buf));
  121. buf[5] = 't';
  122. memcpy(pti->pty_sn, buf, sizeof(buf));
  123. }
  124. /*
  125. * Allocate and zero array of nelem elements.
  126. */
  127. struct pt_softc **
  128. ptyarralloc(int nelem)
  129. {
  130. struct pt_softc **pt;
  131. pt = mallocarray(nelem, sizeof(struct pt_softc *), M_DEVBUF,
  132. M_WAITOK|M_ZERO);
  133. return pt;
  134. }
  135. /*
  136. * Check if the minor is correct and ensure necessary structures
  137. * are properly allocated.
  138. */
  139. int
  140. check_pty(int minor)
  141. {
  142. struct pt_softc *pti;
  143. rw_enter_write(&pt_softc_lock);
  144. if (minor >= npty) {
  145. struct pt_softc **newpt;
  146. int newnpty;
  147. /* check if the requested pty can be granted */
  148. if (minor >= maxptys)
  149. goto limit_reached;
  150. /* grow pty array by powers of two, up to maxptys */
  151. for (newnpty = npty; newnpty <= minor; newnpty *= 2)
  152. ;
  153. if (newnpty > maxptys)
  154. newnpty = maxptys;
  155. newpt = ptyarralloc(newnpty);
  156. memcpy(newpt, pt_softc, npty * sizeof(struct pt_softc *));
  157. free(pt_softc, M_DEVBUF, 0);
  158. pt_softc = newpt;
  159. npty = newnpty;
  160. }
  161. /*
  162. * If the entry is not yet allocated, allocate one.
  163. */
  164. if (!pt_softc[minor]) {
  165. pti = malloc(sizeof(struct pt_softc), M_DEVBUF,
  166. M_WAITOK|M_ZERO);
  167. pti->pt_tty = ttymalloc(0);
  168. ptydevname(minor, pti);
  169. pt_softc[minor] = pti;
  170. }
  171. rw_exit_write(&pt_softc_lock);
  172. return (0);
  173. limit_reached:
  174. rw_exit_write(&pt_softc_lock);
  175. tablefull("pty");
  176. return (ENXIO);
  177. }
  178. /*
  179. * Establish n (or default if n is 1) ptys in the system.
  180. */
  181. void
  182. ptyattach(int n)
  183. {
  184. /* maybe should allow 0 => none? */
  185. if (n <= 1)
  186. n = NPTY_MIN;
  187. pt_softc = ptyarralloc(n);
  188. npty = n;
  189. /*
  190. * If we have pty, we need ptm too.
  191. */
  192. ptmattach(1);
  193. }
  194. /*ARGSUSED*/
  195. int
  196. ptsopen(dev_t dev, int flag, int devtype, struct proc *p)
  197. {
  198. struct pt_softc *pti;
  199. struct tty *tp;
  200. int error;
  201. if ((error = check_pty(minor(dev))))
  202. return (error);
  203. pti = pt_softc[minor(dev)];
  204. if (!pti->pt_tty) {
  205. tp = pti->pt_tty = ttymalloc(0);
  206. } else
  207. tp = pti->pt_tty;
  208. if ((tp->t_state & TS_ISOPEN) == 0) {
  209. tp->t_state |= TS_WOPEN;
  210. ttychars(tp); /* Set up default chars */
  211. tp->t_iflag = TTYDEF_IFLAG;
  212. tp->t_oflag = TTYDEF_OFLAG;
  213. tp->t_lflag = TTYDEF_LFLAG;
  214. tp->t_cflag = TTYDEF_CFLAG;
  215. tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
  216. ttsetwater(tp); /* would be done in xxparam() */
  217. } else if (tp->t_state&TS_XCLUDE && suser(p, 0) != 0)
  218. return (EBUSY);
  219. if (tp->t_oproc) /* Ctrlr still around. */
  220. tp->t_state |= TS_CARR_ON;
  221. while ((tp->t_state & TS_CARR_ON) == 0) {
  222. tp->t_state |= TS_WOPEN;
  223. if (flag&FNONBLOCK)
  224. break;
  225. error = ttysleep(tp, &tp->t_rawq, TTIPRI | PCATCH,
  226. ttopen, 0);
  227. if (error)
  228. return (error);
  229. }
  230. error = (*linesw[tp->t_line].l_open)(dev, tp, p);
  231. ptcwakeup(tp, FREAD|FWRITE);
  232. return (error);
  233. }
  234. int
  235. ptsclose(dev_t dev, int flag, int mode, struct proc *p)
  236. {
  237. struct pt_softc *pti = pt_softc[minor(dev)];
  238. struct tty *tp = pti->pt_tty;
  239. int error;
  240. error = (*linesw[tp->t_line].l_close)(tp, flag, p);
  241. error |= ttyclose(tp);
  242. ptcwakeup(tp, FREAD|FWRITE);
  243. return (error);
  244. }
  245. int
  246. ptsread(dev_t dev, struct uio *uio, int flag)
  247. {
  248. struct proc *p = curproc;
  249. struct process *pr = p->p_p;
  250. struct pt_softc *pti = pt_softc[minor(dev)];
  251. struct tty *tp = pti->pt_tty;
  252. int error = 0;
  253. again:
  254. if (pti->pt_flags & PF_REMOTE) {
  255. while (isbackground(pr, tp)) {
  256. if ((pr->ps_sigacts->ps_sigignore & sigmask(SIGTTIN)) ||
  257. (p->p_sigmask & sigmask(SIGTTIN)) ||
  258. pr->ps_pgrp->pg_jobc == 0 ||
  259. pr->ps_flags & PS_PPWAIT)
  260. return (EIO);
  261. pgsignal(pr->ps_pgrp, SIGTTIN, 1);
  262. error = ttysleep(tp, &lbolt,
  263. TTIPRI | PCATCH, ttybg, 0);
  264. if (error)
  265. return (error);
  266. }
  267. if (tp->t_canq.c_cc == 0) {
  268. if (flag & IO_NDELAY)
  269. return (EWOULDBLOCK);
  270. error = ttysleep(tp, &tp->t_canq,
  271. TTIPRI | PCATCH, ttyin, 0);
  272. if (error)
  273. return (error);
  274. goto again;
  275. }
  276. while (tp->t_canq.c_cc > 1 && uio->uio_resid > 0)
  277. if (ureadc(getc(&tp->t_canq), uio) < 0) {
  278. error = EFAULT;
  279. break;
  280. }
  281. if (tp->t_canq.c_cc == 1)
  282. (void) getc(&tp->t_canq);
  283. if (tp->t_canq.c_cc)
  284. return (error);
  285. } else
  286. if (tp->t_oproc)
  287. error = (*linesw[tp->t_line].l_read)(tp, uio, flag);
  288. ptcwakeup(tp, FWRITE);
  289. return (error);
  290. }
  291. /*
  292. * Write to pseudo-tty.
  293. * Wakeups of controlling tty will happen
  294. * indirectly, when tty driver calls ptsstart.
  295. */
  296. int
  297. ptswrite(dev_t dev, struct uio *uio, int flag)
  298. {
  299. struct pt_softc *pti = pt_softc[minor(dev)];
  300. struct tty *tp = pti->pt_tty;
  301. if (tp->t_oproc == 0)
  302. return (EIO);
  303. return ((*linesw[tp->t_line].l_write)(tp, uio, flag));
  304. }
  305. /*
  306. * Start output on pseudo-tty.
  307. * Wake up process polling or sleeping for input from controlling tty.
  308. */
  309. void
  310. ptsstart(struct tty *tp)
  311. {
  312. struct pt_softc *pti = pt_softc[minor(tp->t_dev)];
  313. if (tp->t_state & TS_TTSTOP)
  314. return;
  315. if (pti->pt_flags & PF_STOPPED) {
  316. pti->pt_flags &= ~PF_STOPPED;
  317. pti->pt_send = TIOCPKT_START;
  318. }
  319. ptcwakeup(tp, FREAD);
  320. }
  321. int
  322. ptsstop(struct tty *tp, int flush)
  323. {
  324. struct pt_softc *pti = pt_softc[minor(tp->t_dev)];
  325. int flag;
  326. /* note: FLUSHREAD and FLUSHWRITE already ok */
  327. if (flush == 0) {
  328. flush = TIOCPKT_STOP;
  329. pti->pt_flags |= PF_STOPPED;
  330. } else
  331. pti->pt_flags &= ~PF_STOPPED;
  332. pti->pt_send |= flush;
  333. /* change of perspective */
  334. flag = 0;
  335. if (flush & FREAD)
  336. flag |= FWRITE;
  337. if (flush & FWRITE)
  338. flag |= FREAD;
  339. ptcwakeup(tp, flag);
  340. return 0;
  341. }
  342. void
  343. ptcwakeup(struct tty *tp, int flag)
  344. {
  345. struct pt_softc *pti = pt_softc[minor(tp->t_dev)];
  346. if (flag & FREAD) {
  347. selwakeup(&pti->pt_selr);
  348. wakeup(&tp->t_outq.c_cf);
  349. }
  350. if (flag & FWRITE) {
  351. selwakeup(&pti->pt_selw);
  352. wakeup(&tp->t_rawq.c_cf);
  353. }
  354. }
  355. int ptcopen(dev_t, int, int, struct proc *);
  356. /*ARGSUSED*/
  357. int
  358. ptcopen(dev_t dev, int flag, int devtype, struct proc *p)
  359. {
  360. struct pt_softc *pti;
  361. struct tty *tp;
  362. int error;
  363. if ((error = check_pty(minor(dev))))
  364. return (error);
  365. pti = pt_softc[minor(dev)];
  366. if (!pti->pt_tty) {
  367. tp = pti->pt_tty = ttymalloc(0);
  368. } else
  369. tp = pti->pt_tty;
  370. if (tp->t_oproc)
  371. return (EIO);
  372. tp->t_oproc = ptsstart;
  373. (void)(*linesw[tp->t_line].l_modem)(tp, 1);
  374. tp->t_lflag &= ~EXTPROC;
  375. pti->pt_flags = 0;
  376. pti->pt_send = 0;
  377. pti->pt_ucntl = 0;
  378. return (0);
  379. }
  380. /*ARGSUSED*/
  381. int
  382. ptcclose(dev_t dev, int flag, int devtype, struct proc *p)
  383. {
  384. struct pt_softc *pti = pt_softc[minor(dev)];
  385. struct tty *tp = pti->pt_tty;
  386. (void)(*linesw[tp->t_line].l_modem)(tp, 0);
  387. tp->t_state &= ~TS_CARR_ON;
  388. tp->t_oproc = 0; /* mark closed */
  389. return (0);
  390. }
  391. int
  392. ptcread(dev_t dev, struct uio *uio, int flag)
  393. {
  394. struct pt_softc *pti = pt_softc[minor(dev)];
  395. struct tty *tp = pti->pt_tty;
  396. char buf[BUFSIZ];
  397. int error = 0, cc, bufcc = 0;
  398. /*
  399. * We want to block until the slave
  400. * is open, and there's something to read;
  401. * but if we lost the slave or we're NBIO,
  402. * then return the appropriate error instead.
  403. */
  404. for (;;) {
  405. if (tp->t_state&TS_ISOPEN) {
  406. if (pti->pt_flags&PF_PKT && pti->pt_send) {
  407. error = ureadc((int)pti->pt_send, uio);
  408. if (error)
  409. return (error);
  410. if (pti->pt_send & TIOCPKT_IOCTL) {
  411. cc = MIN(uio->uio_resid,
  412. sizeof(tp->t_termios));
  413. error = uiomovei(&tp->t_termios, cc, uio);
  414. if (error)
  415. return (error);
  416. }
  417. pti->pt_send = 0;
  418. return (0);
  419. }
  420. if (pti->pt_flags&PF_UCNTL && pti->pt_ucntl) {
  421. error = ureadc((int)pti->pt_ucntl, uio);
  422. if (error)
  423. return (error);
  424. pti->pt_ucntl = 0;
  425. return (0);
  426. }
  427. if (tp->t_outq.c_cc && (tp->t_state&TS_TTSTOP) == 0)
  428. break;
  429. }
  430. if ((tp->t_state&TS_CARR_ON) == 0)
  431. return (0); /* EOF */
  432. if (flag & IO_NDELAY)
  433. return (EWOULDBLOCK);
  434. error = tsleep(&tp->t_outq.c_cf, TTIPRI | PCATCH,
  435. ttyin, 0);
  436. if (error)
  437. return (error);
  438. }
  439. if (pti->pt_flags & (PF_PKT|PF_UCNTL))
  440. error = ureadc(0, uio);
  441. while (uio->uio_resid > 0 && error == 0) {
  442. cc = MIN(uio->uio_resid, BUFSIZ);
  443. cc = q_to_b(&tp->t_outq, buf, cc);
  444. if (cc > bufcc)
  445. bufcc = cc;
  446. if (cc <= 0)
  447. break;
  448. error = uiomovei(buf, cc, uio);
  449. }
  450. ttwakeupwr(tp);
  451. if (bufcc)
  452. explicit_bzero(buf, bufcc);
  453. return (error);
  454. }
  455. int
  456. ptcwrite(dev_t dev, struct uio *uio, int flag)
  457. {
  458. struct pt_softc *pti = pt_softc[minor(dev)];
  459. struct tty *tp = pti->pt_tty;
  460. u_char *cp = NULL;
  461. int cc = 0, bufcc = 0;
  462. u_char buf[BUFSIZ];
  463. size_t cnt = 0;
  464. int error = 0;
  465. again:
  466. if ((tp->t_state&TS_ISOPEN) == 0)
  467. goto block;
  468. if (pti->pt_flags & PF_REMOTE) {
  469. if (tp->t_canq.c_cc)
  470. goto block;
  471. while (uio->uio_resid > 0 && tp->t_canq.c_cc < TTYHOG(tp) - 1) {
  472. if (cc == 0) {
  473. cc = MIN(uio->uio_resid, BUFSIZ);
  474. cc = min(cc, TTYHOG(tp) - 1 - tp->t_canq.c_cc);
  475. if (cc > bufcc)
  476. bufcc = cc;
  477. cp = buf;
  478. error = uiomovei(cp, cc, uio);
  479. if (error)
  480. goto done;
  481. /* check again for safety */
  482. if ((tp->t_state&TS_ISOPEN) == 0) {
  483. error = EIO;
  484. goto done;
  485. }
  486. }
  487. if (cc)
  488. (void) b_to_q((char *)cp, cc, &tp->t_canq);
  489. cc = 0;
  490. }
  491. (void) putc(0, &tp->t_canq);
  492. ttwakeup(tp);
  493. wakeup(&tp->t_canq);
  494. goto done;
  495. }
  496. do {
  497. if (cc == 0) {
  498. cc = MIN(uio->uio_resid, BUFSIZ);
  499. if (cc > bufcc)
  500. bufcc = cc;
  501. cp = buf;
  502. error = uiomovei(cp, cc, uio);
  503. if (error)
  504. goto done;
  505. /* check again for safety */
  506. if ((tp->t_state&TS_ISOPEN) == 0) {
  507. error = EIO;
  508. goto done;
  509. }
  510. }
  511. bufcc = cc;
  512. while (cc > 0) {
  513. if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG(tp) - 2 &&
  514. (tp->t_canq.c_cc > 0 || !ISSET(tp->t_lflag, ICANON))) {
  515. wakeup(&tp->t_rawq);
  516. goto block;
  517. }
  518. (*linesw[tp->t_line].l_rint)(*cp++, tp);
  519. cnt++;
  520. cc--;
  521. }
  522. cc = 0;
  523. } while (uio->uio_resid > 0);
  524. goto done;
  525. block:
  526. /*
  527. * Come here to wait for slave to open, for space
  528. * in outq, or space in rawq.
  529. */
  530. if ((tp->t_state&TS_CARR_ON) == 0) {
  531. error = EIO;
  532. goto done;
  533. }
  534. if (flag & IO_NDELAY) {
  535. /* adjust for data copied in but not written */
  536. uio->uio_resid += cc;
  537. if (cnt == 0)
  538. error = EWOULDBLOCK;
  539. goto done;
  540. }
  541. error = tsleep(&tp->t_rawq.c_cf, TTOPRI | PCATCH,
  542. ttyout, 0);
  543. if (error == 0)
  544. goto again;
  545. /* adjust for data copied in but not written */
  546. uio->uio_resid += cc;
  547. done:
  548. if (bufcc)
  549. explicit_bzero(buf, bufcc);
  550. return (error);
  551. }
  552. int
  553. ptcpoll(dev_t dev, int events, struct proc *p)
  554. {
  555. struct pt_softc *pti = pt_softc[minor(dev)];
  556. struct tty *tp = pti->pt_tty;
  557. int revents = 0, s;
  558. if (!ISSET(tp->t_state, TS_ISOPEN) && ISSET(tp->t_state, TS_CARR_ON))
  559. goto notopen;
  560. if (events & (POLLIN | POLLRDNORM)) {
  561. /*
  562. * Need to protect access to t_outq
  563. */
  564. s = spltty();
  565. if ((tp->t_outq.c_cc && !ISSET(tp->t_state, TS_TTSTOP)) ||
  566. ((pti->pt_flags & PF_PKT) && pti->pt_send) ||
  567. ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl))
  568. revents |= events & (POLLIN | POLLRDNORM);
  569. splx(s);
  570. }
  571. /* NOTE: POLLHUP and POLLOUT/POLLWRNORM are mutually exclusive */
  572. if (!ISSET(tp->t_state, TS_CARR_ON)) {
  573. revents |= POLLHUP;
  574. } else if (events & (POLLOUT | POLLWRNORM)) {
  575. if ((pti->pt_flags & PF_REMOTE) ?
  576. (tp->t_canq.c_cc == 0) :
  577. ((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG(tp) - 2) ||
  578. (tp->t_canq.c_cc == 0 && ISSET(tp->t_lflag, ICANON))))
  579. revents |= events & (POLLOUT | POLLWRNORM);
  580. }
  581. if (events & (POLLPRI | POLLRDBAND)) {
  582. /* If in packet or user control mode, check for data. */
  583. if (((pti->pt_flags & PF_PKT) && pti->pt_send) ||
  584. ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl))
  585. revents |= events & (POLLPRI | POLLRDBAND);
  586. }
  587. if (revents == 0) {
  588. notopen:
  589. if (events & (POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND))
  590. selrecord(p, &pti->pt_selr);
  591. if (events & (POLLOUT | POLLWRNORM))
  592. selrecord(p, &pti->pt_selw);
  593. }
  594. return (revents);
  595. }
  596. void
  597. filt_ptcrdetach(struct knote *kn)
  598. {
  599. struct pt_softc *pti = (struct pt_softc *)kn->kn_hook;
  600. int s;
  601. s = spltty();
  602. SLIST_REMOVE(&pti->pt_selr.si_note, kn, knote, kn_selnext);
  603. splx(s);
  604. }
  605. int
  606. filt_ptcread(struct knote *kn, long hint)
  607. {
  608. struct pt_softc *pti = (struct pt_softc *)kn->kn_hook;
  609. struct tty *tp;
  610. tp = pti->pt_tty;
  611. kn->kn_data = 0;
  612. if (ISSET(tp->t_state, TS_ISOPEN)) {
  613. if (!ISSET(tp->t_state, TS_TTSTOP))
  614. kn->kn_data = tp->t_outq.c_cc;
  615. if (((pti->pt_flags & PF_PKT) && pti->pt_send) ||
  616. ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl))
  617. kn->kn_data++;
  618. }
  619. return (kn->kn_data > 0);
  620. }
  621. void
  622. filt_ptcwdetach(struct knote *kn)
  623. {
  624. struct pt_softc *pti = (struct pt_softc *)kn->kn_hook;
  625. int s;
  626. s = spltty();
  627. SLIST_REMOVE(&pti->pt_selw.si_note, kn, knote, kn_selnext);
  628. splx(s);
  629. }
  630. int
  631. filt_ptcwrite(struct knote *kn, long hint)
  632. {
  633. struct pt_softc *pti = (struct pt_softc *)kn->kn_hook;
  634. struct tty *tp;
  635. tp = pti->pt_tty;
  636. kn->kn_data = 0;
  637. if (ISSET(tp->t_state, TS_ISOPEN)) {
  638. if (ISSET(pti->pt_flags, PF_REMOTE)) {
  639. if (tp->t_canq.c_cc == 0)
  640. kn->kn_data = tp->t_canq.c_cn;
  641. } else if (tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG(tp)-2)
  642. kn->kn_data = tp->t_canq.c_cn -
  643. (tp->t_rawq.c_cc + tp->t_canq.c_cc);
  644. }
  645. return (kn->kn_data > 0);
  646. }
  647. struct filterops ptcread_filtops =
  648. { 1, NULL, filt_ptcrdetach, filt_ptcread };
  649. struct filterops ptcwrite_filtops =
  650. { 1, NULL, filt_ptcwdetach, filt_ptcwrite };
  651. int
  652. ptckqfilter(dev_t dev, struct knote *kn)
  653. {
  654. struct pt_softc *pti = pt_softc[minor(dev)];
  655. struct klist *klist;
  656. int s;
  657. switch (kn->kn_filter) {
  658. case EVFILT_READ:
  659. klist = &pti->pt_selr.si_note;
  660. kn->kn_fop = &ptcread_filtops;
  661. break;
  662. case EVFILT_WRITE:
  663. klist = &pti->pt_selw.si_note;
  664. kn->kn_fop = &ptcwrite_filtops;
  665. break;
  666. default:
  667. return (EINVAL);
  668. }
  669. kn->kn_hook = (caddr_t)pti;
  670. s = spltty();
  671. SLIST_INSERT_HEAD(klist, kn, kn_selnext);
  672. splx(s);
  673. return (0);
  674. }
  675. struct tty *
  676. ptytty(dev_t dev)
  677. {
  678. struct pt_softc *pti = pt_softc[minor(dev)];
  679. struct tty *tp = pti->pt_tty;
  680. return (tp);
  681. }
  682. /*ARGSUSED*/
  683. int
  684. ptyioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
  685. {
  686. struct pt_softc *pti = pt_softc[minor(dev)];
  687. struct tty *tp = pti->pt_tty;
  688. u_char *cc = tp->t_cc;
  689. int stop, error;
  690. /*
  691. * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
  692. * ttywflush(tp) will hang if there are characters in the outq.
  693. */
  694. if (cmd == TIOCEXT) {
  695. /*
  696. * When the EXTPROC bit is being toggled, we need
  697. * to send an TIOCPKT_IOCTL if the packet driver
  698. * is turned on.
  699. */
  700. if (*(int *)data) {
  701. if (pti->pt_flags & PF_PKT) {
  702. pti->pt_send |= TIOCPKT_IOCTL;
  703. ptcwakeup(tp, FREAD);
  704. }
  705. tp->t_lflag |= EXTPROC;
  706. } else {
  707. if ((tp->t_lflag & EXTPROC) &&
  708. (pti->pt_flags & PF_PKT)) {
  709. pti->pt_send |= TIOCPKT_IOCTL;
  710. ptcwakeup(tp, FREAD);
  711. }
  712. tp->t_lflag &= ~EXTPROC;
  713. }
  714. return(0);
  715. } else if (cdevsw[major(dev)].d_open == ptcopen)
  716. switch (cmd) {
  717. case TIOCGPGRP:
  718. /*
  719. * We avoid calling ttioctl on the controller since,
  720. * in that case, tp must be the controlling terminal.
  721. */
  722. *(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
  723. return (0);
  724. case TIOCPKT:
  725. if (*(int *)data) {
  726. if (pti->pt_flags & PF_UCNTL)
  727. return (EINVAL);
  728. pti->pt_flags |= PF_PKT;
  729. } else
  730. pti->pt_flags &= ~PF_PKT;
  731. return (0);
  732. case TIOCUCNTL:
  733. if (*(int *)data) {
  734. if (pti->pt_flags & PF_PKT)
  735. return (EINVAL);
  736. pti->pt_flags |= PF_UCNTL;
  737. } else
  738. pti->pt_flags &= ~PF_UCNTL;
  739. return (0);
  740. case TIOCREMOTE:
  741. if (*(int *)data)
  742. pti->pt_flags |= PF_REMOTE;
  743. else
  744. pti->pt_flags &= ~PF_REMOTE;
  745. ttyflush(tp, FREAD|FWRITE);
  746. return (0);
  747. case TIOCSETD:
  748. case TIOCSETA:
  749. case TIOCSETAW:
  750. case TIOCSETAF:
  751. ndflush(&tp->t_outq, tp->t_outq.c_cc);
  752. break;
  753. case TIOCSIG:
  754. if (*(unsigned int *)data >= NSIG ||
  755. *(unsigned int *)data == 0)
  756. return(EINVAL);
  757. if ((tp->t_lflag&NOFLSH) == 0)
  758. ttyflush(tp, FREAD|FWRITE);
  759. pgsignal(tp->t_pgrp, *(unsigned int *)data, 1);
  760. if ((*(unsigned int *)data == SIGINFO) &&
  761. ((tp->t_lflag&NOKERNINFO) == 0))
  762. ttyinfo(tp);
  763. return (0);
  764. case FIONREAD:
  765. /*
  766. * FIONREAD on the master side must return the amount
  767. * in the output queue rather than the input.
  768. */
  769. *(int *)data = tp->t_outq.c_cc;
  770. return (0);
  771. }
  772. error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
  773. if (error < 0)
  774. error = ttioctl(tp, cmd, data, flag, p);
  775. if (error < 0) {
  776. if (pti->pt_flags & PF_UCNTL &&
  777. (cmd & ~0xff) == UIOCCMD(0)) {
  778. if (cmd & 0xff) {
  779. pti->pt_ucntl = (u_char)cmd;
  780. ptcwakeup(tp, FREAD);
  781. }
  782. return (0);
  783. }
  784. error = ENOTTY;
  785. }
  786. /*
  787. * If external processing and packet mode send ioctl packet.
  788. */
  789. if ((tp->t_lflag&EXTPROC) && (pti->pt_flags & PF_PKT)) {
  790. switch (cmd) {
  791. case TIOCSETA:
  792. case TIOCSETAW:
  793. case TIOCSETAF:
  794. pti->pt_send |= TIOCPKT_IOCTL;
  795. ptcwakeup(tp, FREAD);
  796. default:
  797. break;
  798. }
  799. }
  800. stop = (tp->t_iflag & IXON) && CCEQ(cc[VSTOP], CTRL('s')) &&
  801. CCEQ(cc[VSTART], CTRL('q'));
  802. if (pti->pt_flags & PF_NOSTOP) {
  803. if (stop) {
  804. pti->pt_send &= ~TIOCPKT_NOSTOP;
  805. pti->pt_send |= TIOCPKT_DOSTOP;
  806. pti->pt_flags &= ~PF_NOSTOP;
  807. ptcwakeup(tp, FREAD);
  808. }
  809. } else {
  810. if (!stop) {
  811. pti->pt_send &= ~TIOCPKT_DOSTOP;
  812. pti->pt_send |= TIOCPKT_NOSTOP;
  813. pti->pt_flags |= PF_NOSTOP;
  814. ptcwakeup(tp, FREAD);
  815. }
  816. }
  817. return (error);
  818. }
  819. /*
  820. * Return pty-related information.
  821. */
  822. int
  823. sysctl_pty(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp,
  824. size_t newlen)
  825. {
  826. int error, oldmax;
  827. if (namelen != 1)
  828. return (ENOTDIR);
  829. switch (name[0]) {
  830. case KERN_TTY_MAXPTYS:
  831. if (!newp)
  832. return (sysctl_rdint(oldp, oldlenp, newp, maxptys));
  833. rw_enter_write(&pt_softc_lock);
  834. oldmax = maxptys;
  835. error = sysctl_int(oldp, oldlenp, newp, newlen, &maxptys);
  836. /*
  837. * We can't set the max lower than the current active
  838. * value or to a value bigger than NPTY_MAX.
  839. */
  840. if (error == 0 && (maxptys > NPTY_MAX || maxptys < npty)) {
  841. maxptys = oldmax;
  842. error = ERANGE;
  843. }
  844. rw_exit_write(&pt_softc_lock);
  845. return (error);
  846. case KERN_TTY_NPTYS:
  847. return (sysctl_rdint(oldp, oldlenp, newp, npty));
  848. #ifdef notyet
  849. case KERN_TTY_GID:
  850. return (sysctl_int(oldp, oldlenp, newp, newlen, &tty_gid));
  851. #endif
  852. default:
  853. return (EOPNOTSUPP);
  854. }
  855. /* NOTREACHED */
  856. }
  857. /*
  858. * Check if a pty is free to use.
  859. */
  860. static int
  861. pty_isfree_locked(int minor)
  862. {
  863. struct pt_softc *pt = pt_softc[minor];
  864. return (pt == NULL || pt->pt_tty == NULL ||
  865. pt->pt_tty->t_oproc == NULL);
  866. }
  867. static int
  868. pty_isfree(int minor)
  869. {
  870. int isfree;
  871. rw_enter_read(&pt_softc_lock);
  872. isfree = pty_isfree_locked(minor);
  873. rw_exit_read(&pt_softc_lock);
  874. return(isfree);
  875. }
  876. dev_t
  877. pty_getfree(void)
  878. {
  879. int i;
  880. rw_enter_read(&pt_softc_lock);
  881. for (i = 0; i < npty; i++) {
  882. if (pty_isfree_locked(i))
  883. break;
  884. }
  885. rw_exit_read(&pt_softc_lock);
  886. return (makedev(pts_major, i));
  887. }
  888. /*
  889. * Hacked up version of vn_open. We _only_ handle ptys and only open
  890. * them with FREAD|FWRITE and never deal with creat or stuff like that.
  891. *
  892. * We need it because we have to fake up root credentials to open the pty.
  893. */
  894. static int
  895. ptm_vn_open(struct nameidata *ndp)
  896. {
  897. struct proc *p = ndp->ni_cnd.cn_proc;
  898. struct ucred *cred;
  899. struct vattr vattr;
  900. struct vnode *vp;
  901. int error;
  902. if ((error = namei(ndp)) != 0)
  903. return (error);
  904. vp = ndp->ni_vp;
  905. if (vp->v_type != VCHR) {
  906. error = EINVAL;
  907. goto bad;
  908. }
  909. /*
  910. * Get us a fresh cred with root privileges.
  911. */
  912. cred = crget();
  913. error = VOP_OPEN(vp, FREAD|FWRITE, cred, p);
  914. if (!error) {
  915. /* update atime/mtime */
  916. VATTR_NULL(&vattr);
  917. getnanotime(&vattr.va_atime);
  918. vattr.va_mtime = vattr.va_atime;
  919. vattr.va_vaflags |= VA_UTIMES_NULL;
  920. (void)VOP_SETATTR(vp, &vattr, p->p_ucred, p);
  921. }
  922. crfree(cred);
  923. if (error)
  924. goto bad;
  925. vp->v_writecount++;
  926. return (0);
  927. bad:
  928. vput(vp);
  929. return (error);
  930. }
  931. void
  932. ptmattach(int n)
  933. {
  934. /* find the major and minor of the pty devices */
  935. int i;
  936. for (i = 0; i < nchrdev; i++)
  937. if (cdevsw[i].d_open == ptsopen)
  938. break;
  939. if (i == nchrdev)
  940. panic("ptmattach: Can't find pty slave in cdevsw");
  941. pts_major = i;
  942. }
  943. int
  944. ptmopen(dev_t dev, int flag, int mode, struct proc *p)
  945. {
  946. return(0);
  947. }
  948. int
  949. ptmclose(dev_t dev, int flag, int mode, struct proc *p)
  950. {
  951. return (0);
  952. }
  953. int
  954. ptmioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
  955. {
  956. dev_t newdev, error;
  957. struct pt_softc * pti;
  958. struct nameidata cnd, snd;
  959. struct filedesc *fdp = p->p_fd;
  960. struct file *cfp = NULL, *sfp = NULL;
  961. int cindx, sindx;
  962. uid_t uid;
  963. gid_t gid;
  964. struct vattr vattr;
  965. struct ucred *cred;
  966. struct ptmget *ptm = (struct ptmget *)data;
  967. switch (cmd) {
  968. case PTMGET:
  969. fdplock(fdp);
  970. /* Grab two filedescriptors. */
  971. if ((error = falloc(p, &cfp, &cindx)) != 0) {
  972. fdpunlock(fdp);
  973. break;
  974. }
  975. if ((error = falloc(p, &sfp, &sindx)) != 0) {
  976. fdremove(fdp, cindx);
  977. closef(cfp, p);
  978. fdpunlock(fdp);
  979. break;
  980. }
  981. retry:
  982. /* Find and open a free master pty. */
  983. newdev = pty_getfree();
  984. if ((error = check_pty(minor(newdev))))
  985. goto bad;
  986. pti = pt_softc[minor(newdev)];
  987. NDINIT(&cnd, LOOKUP, NOFOLLOW|LOCKLEAF, UIO_SYSSPACE,
  988. pti->pty_pn, p);
  989. if ((error = ptm_vn_open(&cnd)) != 0) {
  990. /*
  991. * Check if the master open failed because we lost
  992. * the race to grab it.
  993. */
  994. if (error == EIO && !pty_isfree(minor(newdev)))
  995. goto retry;
  996. goto bad;
  997. }
  998. cfp->f_flag = FREAD|FWRITE;
  999. cfp->f_type = DTYPE_VNODE;
  1000. cfp->f_ops = &vnops;
  1001. cfp->f_data = (caddr_t) cnd.ni_vp;
  1002. VOP_UNLOCK(cnd.ni_vp, 0, p);
  1003. /*
  1004. * Open the slave.
  1005. * namei -> setattr -> unlock -> revoke -> vrele ->
  1006. * namei -> open -> unlock
  1007. * Three stage rocket:
  1008. * 1. Change the owner and permissions on the slave.
  1009. * 2. Revoke all the users of the slave.
  1010. * 3. open the slave.
  1011. */
  1012. NDINIT(&snd, LOOKUP, NOFOLLOW|LOCKLEAF, UIO_SYSSPACE,
  1013. pti->pty_sn, p);
  1014. if ((error = namei(&snd)) != 0)
  1015. goto bad;
  1016. if ((snd.ni_vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
  1017. gid = tty_gid;
  1018. /* get real uid */
  1019. uid = p->p_ucred->cr_ruid;
  1020. VATTR_NULL(&vattr);
  1021. vattr.va_uid = uid;
  1022. vattr.va_gid = gid;
  1023. vattr.va_mode = (S_IRUSR|S_IWUSR|S_IWGRP) & ALLPERMS;
  1024. /* Get a fake cred to pretend we're root. */
  1025. cred = crget();
  1026. error = VOP_SETATTR(snd.ni_vp, &vattr, cred, p);
  1027. crfree(cred);
  1028. if (error) {
  1029. vput(snd.ni_vp);
  1030. goto bad;
  1031. }
  1032. }
  1033. VOP_UNLOCK(snd.ni_vp, 0, p);
  1034. if (snd.ni_vp->v_usecount > 1 ||
  1035. (snd.ni_vp->v_flag & (VALIASED)))
  1036. VOP_REVOKE(snd.ni_vp, REVOKEALL);
  1037. /*
  1038. * The vnode is useless after the revoke, we need to
  1039. * namei again.
  1040. */
  1041. vrele(snd.ni_vp);
  1042. NDINIT(&snd, LOOKUP, NOFOLLOW|LOCKLEAF, UIO_SYSSPACE,
  1043. pti->pty_sn, p);
  1044. /* now open it */
  1045. if ((error = ptm_vn_open(&snd)) != 0)
  1046. goto bad;
  1047. sfp->f_flag = FREAD|FWRITE;
  1048. sfp->f_type = DTYPE_VNODE;
  1049. sfp->f_ops = &vnops;
  1050. sfp->f_data = (caddr_t) snd.ni_vp;
  1051. VOP_UNLOCK(snd.ni_vp, 0, p);
  1052. /* now, put the indexen and names into struct ptmget */
  1053. ptm->cfd = cindx;
  1054. ptm->sfd = sindx;
  1055. memcpy(ptm->cn, pti->pty_pn, sizeof(pti->pty_pn));
  1056. memcpy(ptm->sn, pti->pty_sn, sizeof(pti->pty_sn));
  1057. /* mark the files mature now that we've passed all errors */
  1058. FILE_SET_MATURE(cfp, p);
  1059. FILE_SET_MATURE(sfp, p);
  1060. fdpunlock(fdp);
  1061. break;
  1062. default:
  1063. error = EINVAL;
  1064. break;
  1065. }
  1066. return (error);
  1067. bad:
  1068. fdremove(fdp, cindx);
  1069. closef(cfp, p);
  1070. fdremove(fdp, sindx);
  1071. closef(sfp, p);
  1072. fdpunlock(fdp);
  1073. return (error);
  1074. }