tty.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2023-11-13 Shell init ver.
  9. */
  10. #include "../bsd_porting.h"
  11. #include "../tty_config.h"
  12. #include "../terminal.h"
  13. #include "../tty_internal.h"
  14. #include <rtdef.h>
  15. #include <sys/ioctl.h>
  16. /*-
  17. * SPDX-License-Identifier: BSD-2-Clause
  18. *
  19. * Copyright (c) 2008 Ed Schouten <ed@FreeBSD.org>
  20. * All rights reserved.
  21. *
  22. * Portions of this software were developed under sponsorship from Snow
  23. * B.V., the Netherlands.
  24. *
  25. * Redistribution and use in source and binary forms, with or without
  26. * modification, are permitted provided that the following conditions
  27. * are met:
  28. * 1. Redistributions of source code must retain the above copyright
  29. * notice, this list of conditions and the following disclaimer.
  30. * 2. Redistributions in binary form must reproduce the above copyright
  31. * notice, this list of conditions and the following disclaimer in the
  32. * documentation and/or other materials provided with the distribution.
  33. *
  34. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  35. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  36. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  37. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  38. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  39. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  40. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  41. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  42. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  43. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  44. * SUCH DAMAGE.
  45. */
  46. static void tty_rel_free(struct lwp_tty *tp);
  47. /* Character device of /dev/console. */
  48. static struct rt_device *dev_console;
  49. #ifdef USING_BSD_CONSOLE_NAME
  50. static const char *dev_console_filename;
  51. #endif
  52. /*
  53. * Flags that are supported and stored by this implementation.
  54. */
  55. #ifndef ALTWERASE
  56. #define ALTWERASE 0
  57. #endif
  58. #ifndef NOKERNINFO
  59. #define NOKERNINFO 0
  60. #endif
  61. #define TTYSUP_IFLAG \
  62. (IGNBRK | BRKINT | IGNPAR | PARMRK | INPCK | ISTRIP | INLCR | IGNCR | \
  63. ICRNL | IXON | IXOFF | IXANY | IMAXBEL | IUTF8)
  64. #define TTYSUP_OFLAG (OPOST | ONLCR | TAB3 | ONOEOT | OCRNL | ONOCR | ONLRET)
  65. #define TTYSUP_LFLAG \
  66. (ECHOKE | ECHOE | ECHOK | ECHO | ECHONL | ECHOPRT | ECHOCTL | ISIG | \
  67. ICANON | ALTWERASE | IEXTEN | TOSTOP | FLUSHO | NOKERNINFO | NOFLSH)
  68. #define TTYSUP_CFLAG \
  69. (CIGNORE | CSIZE | CSTOPB | CREAD | PARENB | PARODD | HUPCL | CLOCAL | \
  70. CCTS_OFLOW | CRTS_IFLOW | CDTR_IFLOW | CDSR_OFLOW | CCAR_OFLOW | \
  71. CNO_RTSDTR)
  72. /*
  73. * Set TTY buffer sizes.
  74. */
  75. #define TTYBUF_MAX 65536
  76. #ifdef PRINTF_BUFR_SIZE
  77. #define TTY_PRBUF_SIZE PRINTF_BUFR_SIZE
  78. #else
  79. #define TTY_PRBUF_SIZE 256
  80. #endif
  81. /* Note: access to struct cdev:si_drv0.
  82. Since pull-in, pull-out is not provided, this field
  83. is constant value 0 in smart system */
  84. #define dev2unit(d) (0)
  85. #define TTY_CALLOUT(tp, d) (dev2unit(d) & TTYUNIT_CALLOUT)
  86. /*
  87. * Allocate buffer space if necessary, and set low watermarks, based on speed.
  88. * Note that the ttyxxxq_setsize() functions may drop and then reacquire the tty
  89. * lock during memory allocation. They will return ENXIO if the tty disappears
  90. * while unlocked.
  91. */
  92. static int tty_watermarks(struct lwp_tty *tp)
  93. {
  94. size_t bs = 0;
  95. int error;
  96. /* Provide an input buffer for 2 seconds of data. */
  97. if (tp->t_termios.c_cflag & CREAD)
  98. bs =
  99. MIN(bsd_speed_to_integer(tp->t_termios.__c_ispeed) / 5, TTYBUF_MAX);
  100. error = ttyinq_setsize(&tp->t_inq, tp, bs);
  101. if (error != 0)
  102. return error;
  103. /* Set low watermark at 10% (when 90% is available). */
  104. tp->t_inlow = (ttyinq_getallocatedsize(&tp->t_inq) * 9) / 10;
  105. /* Provide an output buffer for 2 seconds of data. */
  106. bs = MIN(bsd_speed_to_integer(tp->t_termios.__c_ospeed) / 5, TTYBUF_MAX);
  107. error = ttyoutq_setsize(&tp->t_outq, tp, bs);
  108. if (error != 0)
  109. return error;
  110. /* Set low watermark at 10% (when 90% is available). */
  111. tp->t_outlow = (ttyoutq_getallocatedsize(&tp->t_outq) * 9) / 10;
  112. return 0;
  113. }
  114. /**
  115. * Drain outq
  116. */
  117. static int tty_drain(struct lwp_tty *tp, int leaving)
  118. {
  119. rt_tick_t timeout_tick;
  120. size_t bytes;
  121. int error;
  122. #ifdef USING_BSD_HOOK
  123. if (ttyhook_hashook(tp, getc_inject))
  124. /* buffer is inaccessible */
  125. return 0;
  126. #endif /* USING_BSD_HOOK */
  127. /*
  128. * For close(), use the recent historic timeout of "1 second without
  129. * making progress". For tcdrain(), use t_drainwait as the timeout,
  130. * with zero meaning "no timeout" which gives POSIX behavior.
  131. */
  132. if (leaving)
  133. timeout_tick = rt_tick_get() + RT_TICK_PER_SECOND;
  134. else if (tp->t_drainwait != 0)
  135. timeout_tick = rt_tick_get() + RT_TICK_PER_SECOND * tp->t_drainwait;
  136. else
  137. timeout_tick = 0;
  138. /*
  139. * Poll the output buffer and the hardware for completion, at 10 Hz.
  140. * Polling is required for devices which are not able to signal an
  141. * interrupt when the transmitter becomes idle (most USB serial devs).
  142. * The unusual structure of this loop ensures we check for busy one more
  143. * time after tty_timedwait() returns EWOULDBLOCK, so that success has
  144. * higher priority than timeout if the IO completed in the last 100mS.
  145. */
  146. error = 0;
  147. bytes = ttyoutq_bytesused(&tp->t_outq);
  148. for (;;)
  149. {
  150. if (ttyoutq_bytesused(&tp->t_outq) == 0 && !ttydevsw_busy(tp))
  151. return 0;
  152. if (error != 0)
  153. return error;
  154. ttydevsw_outwakeup(tp);
  155. error = tty_timedwait(tp, &tp->t_outwait, RT_TICK_PER_SECOND / 10);
  156. if (error != 0 && error != EWOULDBLOCK)
  157. return error;
  158. else if (timeout_tick == 0 || rt_tick_get() < timeout_tick)
  159. error = 0;
  160. else if (leaving && ttyoutq_bytesused(&tp->t_outq) < bytes)
  161. {
  162. /* In close, making progress, grant an extra second. */
  163. error = 0;
  164. timeout_tick += RT_TICK_PER_SECOND;
  165. bytes = ttyoutq_bytesused(&tp->t_outq);
  166. }
  167. }
  168. }
  169. /*
  170. * Though ttydev_enter() and ttydev_leave() seem to be related, they
  171. * don't have to be used together. ttydev_enter() is used by the cdev
  172. * operations to prevent an actual operation from being processed when
  173. * the TTY has been abandoned. ttydev_leave() is used by ttydev_open()
  174. * and ttydev_close() to determine whether per-TTY data should be
  175. * deallocated.
  176. */
  177. rt_inline int ttydev_enter(struct lwp_tty *tp)
  178. {
  179. rt_err_t error = tty_lock(tp);
  180. if (error)
  181. RT_ASSERT(0);
  182. if (tty_gone(tp) || !tty_opened(tp))
  183. {
  184. /* Device is already gone. */
  185. tty_unlock(tp);
  186. return -ENXIO;
  187. }
  188. return 0;
  189. }
  190. static void ttydev_leave(struct lwp_tty *tp)
  191. {
  192. tty_assert_locked(tp);
  193. if (tty_opened(tp) || tp->t_flags & TF_OPENCLOSE)
  194. {
  195. /* Device is still opened somewhere. */
  196. tty_unlock(tp);
  197. return;
  198. }
  199. tp->t_flags |= TF_OPENCLOSE;
  200. /* Remove console TTY. */
  201. constty_clear(tp);
  202. /* Drain any output. */
  203. if (!tty_gone(tp))
  204. tty_drain(tp, 1);
  205. ttydisc_close(tp);
  206. /* Free i/o queues now since they might be large. */
  207. ttyinq_free(&tp->t_inq);
  208. tp->t_inlow = 0;
  209. ttyoutq_free(&tp->t_outq);
  210. tp->t_outlow = 0;
  211. #ifdef USING_BSD_KNOTE
  212. knlist_clear(&tp->t_inpoll.si_note, 1);
  213. knlist_clear(&tp->t_outpoll.si_note, 1);
  214. #endif
  215. if (!tty_gone(tp))
  216. ttydevsw_close(tp);
  217. tp->t_flags &= ~TF_OPENCLOSE;
  218. cv_broadcast(&tp->t_dcdwait);
  219. tty_rel_free(tp);
  220. }
  221. /*
  222. * Operations that are exposed through the character device in /dev.
  223. */
  224. static int ttydev_open(struct lwp_tty *tp, int oflags, int devtype,
  225. struct rt_thread *td)
  226. {
  227. rt_device_t dev = &tp->parent;
  228. int error;
  229. error = 0;
  230. tty_lock(tp);
  231. if (tty_gone(tp))
  232. {
  233. /* Device is already gone. */
  234. tty_unlock(tp);
  235. return -ENXIO;
  236. }
  237. /*
  238. * Block when other processes are currently opening or closing
  239. * the TTY.
  240. */
  241. while (tp->t_flags & TF_OPENCLOSE)
  242. {
  243. error = tty_wait(tp, &tp->t_dcdwait);
  244. if (error != 0)
  245. {
  246. tty_unlock(tp);
  247. return error;
  248. }
  249. }
  250. tp->t_flags |= TF_OPENCLOSE;
  251. /*
  252. * Make sure the "tty" and "cua" device cannot be opened at the
  253. * same time. The console is a "tty" device.
  254. */
  255. if (TTY_CALLOUT(tp, dev))
  256. {
  257. if (tp->t_flags & (TF_OPENED_CONS | TF_OPENED_IN))
  258. {
  259. error = EBUSY;
  260. goto done;
  261. }
  262. }
  263. else
  264. {
  265. if (tp->t_flags & TF_OPENED_OUT)
  266. {
  267. error = EBUSY;
  268. goto done;
  269. }
  270. }
  271. if (tp->t_flags & TF_EXCLUDE && priv_check(td, PRIV_TTY_EXCLUSIVE))
  272. {
  273. error = EBUSY;
  274. goto done;
  275. }
  276. if (!tty_opened(tp))
  277. {
  278. /* Set proper termios flags. */
  279. if (TTY_CALLOUT(tp, dev))
  280. #ifdef USING_BSD_INIT_LOCK_DEVICE
  281. tp->t_termios = tp->t_termios_init_out;
  282. #else
  283. ;
  284. #endif /* USING_BSD_INIT_LOCK_DEVICE */
  285. else
  286. tp->t_termios = tp->t_termios_init_in;
  287. ttydevsw_param(tp, &tp->t_termios);
  288. /* Prevent modem control on callout devices and /dev/console. */
  289. if (TTY_CALLOUT(tp, dev) || dev == dev_console)
  290. tp->t_termios.c_cflag |= CLOCAL;
  291. if ((tp->t_termios.c_cflag & CNO_RTSDTR) == 0)
  292. ttydevsw_modem(tp, SER_DTR | SER_RTS, 0);
  293. error = ttydevsw_open(tp);
  294. if (error != 0)
  295. goto done;
  296. ttydisc_open(tp);
  297. error = tty_watermarks(tp);
  298. if (error != 0)
  299. goto done;
  300. }
  301. /* Wait for Carrier Detect. */
  302. if ((oflags & O_NONBLOCK) == 0 && (tp->t_termios.c_cflag & CLOCAL) == 0)
  303. {
  304. while ((ttydevsw_modem(tp, 0, 0) & SER_DCD) == 0)
  305. {
  306. error = tty_wait(tp, &tp->t_dcdwait);
  307. if (error != 0)
  308. goto done;
  309. }
  310. }
  311. if (dev == dev_console)
  312. tp->t_flags |= TF_OPENED_CONS;
  313. else if (TTY_CALLOUT(tp, dev))
  314. tp->t_flags |= TF_OPENED_OUT;
  315. else
  316. tp->t_flags |= TF_OPENED_IN;
  317. MPASS((tp->t_flags & (TF_OPENED_CONS | TF_OPENED_IN)) == 0 ||
  318. (tp->t_flags & TF_OPENED_OUT) == 0);
  319. done:
  320. tp->t_flags &= ~TF_OPENCLOSE;
  321. cv_broadcast(&tp->t_dcdwait);
  322. ttydev_leave(tp);
  323. return error;
  324. }
  325. static int ttydev_close(struct lwp_tty *tp, int fflag, int devtype __unused,
  326. struct rt_thread *td __unused)
  327. {
  328. rt_device_t dev = &tp->parent;
  329. tty_lock(tp);
  330. /*
  331. * Don't actually close the device if it is being used as the
  332. * console.
  333. */
  334. MPASS((tp->t_flags & (TF_OPENED_CONS | TF_OPENED_IN)) == 0 ||
  335. (tp->t_flags & TF_OPENED_OUT) == 0);
  336. if (dev == dev_console)
  337. tp->t_flags &= ~TF_OPENED_CONS;
  338. else
  339. tp->t_flags &= ~(TF_OPENED_IN | TF_OPENED_OUT);
  340. if (tp->t_flags & TF_OPENED)
  341. {
  342. tty_unlock(tp);
  343. return 0;
  344. }
  345. /* If revoking, flush output now to avoid draining it later. */
  346. if (fflag & FREVOKE)
  347. tty_flush(tp, FWRITE);
  348. tp->t_flags &= ~TF_EXCLUDE;
  349. /* Properly wake up threads that are stuck - revoke(). */
  350. tp->t_revokecnt++;
  351. tty_wakeup(tp, FREAD | FWRITE);
  352. cv_broadcast(&tp->t_bgwait);
  353. cv_broadcast(&tp->t_dcdwait);
  354. ttydev_leave(tp);
  355. return 0;
  356. }
  357. int tty_wait_background(struct lwp_tty *tp, struct rt_thread *td, int sig)
  358. {
  359. struct rt_lwp *p;
  360. struct rt_processgroup *pg;
  361. int error;
  362. MPASS(sig == SIGTTIN || sig == SIGTTOU);
  363. tty_assert_locked(tp);
  364. p = td->lwp;
  365. for (;;)
  366. {
  367. pg = p->pgrp;
  368. PGRP_LOCK(pg);
  369. LWP_LOCK(p);
  370. /*
  371. * pg may no longer be our process group.
  372. * Re-check after locking.
  373. */
  374. if (p->pgrp != pg)
  375. {
  376. LWP_UNLOCK(p);
  377. PGRP_UNLOCK(pg);
  378. continue;
  379. }
  380. /*
  381. * The process should only sleep, when:
  382. * - This terminal is the controlling terminal
  383. * - Its process group is not the foreground process
  384. * group
  385. * - The parent process isn't waiting for the child to
  386. * exit
  387. * - the signal to send to the process isn't masked
  388. */
  389. if (!tty_is_ctty(tp, p) || pg == tp->t_pgrp)
  390. {
  391. /* Allow the action to happen. */
  392. LWP_UNLOCK(p);
  393. PGRP_UNLOCK(pg);
  394. return 0;
  395. }
  396. /* Note: process itself don't have a sigmask in smart */
  397. if (lwp_sigisign(p, sig) ||
  398. lwp_sigismember(&td->signal.sigset_mask, sig))
  399. {
  400. /* Only allow them in write()/ioctl(). */
  401. LWP_UNLOCK(p);
  402. PGRP_UNLOCK(pg);
  403. return (sig == SIGTTOU ? 0 : -EIO);
  404. }
  405. #ifdef USING_VFORK_FLAG
  406. if ((p->p_flag & P_PPWAIT) != 0 || pg->is_orphaned)
  407. #else
  408. if (pg->is_orphaned)
  409. #endif
  410. {
  411. /* Don't allow the action to happen. */
  412. LWP_UNLOCK(p);
  413. PGRP_UNLOCK(pg);
  414. return -EIO;
  415. }
  416. LWP_UNLOCK(p);
  417. /*
  418. * Send the signal and sleep until we're the new
  419. * foreground process group.
  420. */
  421. if (sig != 0)
  422. {
  423. lwp_pgrp_signal_kill(pg, sig, SI_KERNEL, 0);
  424. }
  425. PGRP_UNLOCK(pg);
  426. error = tty_wait(tp, &tp->t_bgwait);
  427. if (error)
  428. return error;
  429. }
  430. }
  431. static int ttydev_read(struct lwp_tty *tp, struct uio *uio, int ioflag)
  432. {
  433. int error;
  434. error = ttydev_enter(tp);
  435. if (error)
  436. goto done;
  437. error = ttydisc_read(tp, uio, ioflag);
  438. tty_unlock(tp);
  439. /*
  440. * The read() call should not throw an error when the device is
  441. * being destroyed. Silently convert it to an EOF.
  442. */
  443. done:
  444. if (error == -ENXIO)
  445. error = 0;
  446. return error;
  447. }
  448. static int ttydev_write(struct lwp_tty *tp, struct uio *uio, int ioflag)
  449. {
  450. #ifdef USING_BSD_DEFER_STOP
  451. int defer;
  452. #endif
  453. int error;
  454. error = ttydev_enter(tp);
  455. if (error)
  456. return error;
  457. if (tp->t_termios.c_lflag & TOSTOP)
  458. {
  459. error = tty_wait_background(tp, curthread, SIGTTOU);
  460. if (error)
  461. goto done;
  462. }
  463. if (ioflag & IO_NDELAY && tp->t_flags & TF_BUSY_OUT)
  464. {
  465. /* Allow non-blocking writes to bypass serialization. */
  466. error = ttydisc_write(tp, uio, ioflag);
  467. }
  468. else
  469. {
  470. /* Serialize write() calls. */
  471. while (tp->t_flags & TF_BUSY_OUT)
  472. {
  473. error = tty_wait(tp, &tp->t_outserwait);
  474. if (error)
  475. goto done;
  476. }
  477. tp->t_flags |= TF_BUSY_OUT;
  478. #ifdef USING_BSD_DEFER_STOP
  479. defer = sigdeferstop(SIGDEFERSTOP_ERESTART);
  480. #endif
  481. error = ttydisc_write(tp, uio, ioflag);
  482. #ifdef USING_BSD_DEFER_STOP
  483. sigallowstop(defer);
  484. #endif
  485. tp->t_flags &= ~TF_BUSY_OUT;
  486. cv_signal(&tp->t_outserwait);
  487. }
  488. done:
  489. tty_unlock(tp);
  490. return error;
  491. }
  492. static int ttydev_ioctl(struct lwp_tty *tp, rt_ubase_t cmd, rt_caddr_t data, int fflag,
  493. struct rt_thread *td)
  494. {
  495. int error;
  496. error = ttydev_enter(tp);
  497. if (error)
  498. return (error);
  499. switch (cmd)
  500. {
  501. case TIOCCBRK:
  502. case TIOCCONS:
  503. case TIOCDRAIN:
  504. case TIOCEXCL:
  505. case TIOCFLUSH:
  506. case TIOCNXCL:
  507. case TIOCSBRK:
  508. case TIOCSCTTY:
  509. case TIOCSETA:
  510. case TIOCSETAF:
  511. case TIOCSETAW:
  512. case TIOCSPGRP:
  513. case TIOCSTART:
  514. case TIOCSTAT:
  515. case TIOCSTI:
  516. case TIOCSTOP:
  517. case TIOCSWINSZ:
  518. #if USING_BSD_TIOCSDRAINWAIT
  519. case TIOCSDRAINWAIT:
  520. case TIOCSETD:
  521. #endif /* USING_BSD_TIOCSDRAINWAIT */
  522. #ifdef COMPAT_43TTY
  523. case TIOCLBIC:
  524. case TIOCLBIS:
  525. case TIOCLSET:
  526. case TIOCSETC:
  527. case OTIOCSETD:
  528. case TIOCSETN:
  529. case TIOCSETP:
  530. case TIOCSLTC:
  531. #endif /* COMPAT_43TTY */
  532. /*
  533. * If the ioctl() causes the TTY to be modified, let it
  534. * wait in the background.
  535. */
  536. error = tty_wait_background(tp, curthread, SIGTTOU);
  537. if (error)
  538. goto done;
  539. }
  540. #ifdef USING_BSD_INIT_LOCK_DEVICE
  541. if (cmd == TIOCSETA || cmd == TIOCSETAW || cmd == TIOCSETAF)
  542. {
  543. struct termios *old = &tp->t_termios;
  544. struct termios *new = (struct termios *)data;
  545. struct termios *lock = TTY_CALLOUT(tp, dev) ? &tp->t_termios_lock_out
  546. : &tp->t_termios_lock_in;
  547. int cc;
  548. /*
  549. * Lock state devices. Just overwrite the values of the
  550. * commands that are currently in use.
  551. */
  552. new->c_iflag =
  553. (old->c_iflag & lock->c_iflag) | (new->c_iflag & ~lock->c_iflag);
  554. new->c_oflag =
  555. (old->c_oflag & lock->c_oflag) | (new->c_oflag & ~lock->c_oflag);
  556. new->c_cflag =
  557. (old->c_cflag & lock->c_cflag) | (new->c_cflag & ~lock->c_cflag);
  558. new->c_lflag =
  559. (old->c_lflag & lock->c_lflag) | (new->c_lflag & ~lock->c_lflag);
  560. for (cc = 0; cc < NCCS; ++cc)
  561. if (lock->c_cc[cc])
  562. new->c_cc[cc] = old->c_cc[cc];
  563. if (lock->__c_ispeed)
  564. new->__c_ispeed = old->__c_ispeed;
  565. if (lock->__c_ospeed)
  566. new->__c_ospeed = old->__c_ospeed;
  567. }
  568. #endif /* USING_BSD_INIT_LOCK_DEVICE */
  569. error = tty_ioctl(tp, cmd, data, fflag, td);
  570. done:
  571. tty_unlock(tp);
  572. return (error);
  573. }
  574. static int ttydev_poll(struct lwp_tty *tp, rt_pollreq_t *req, struct rt_thread *td)
  575. {
  576. int events = req->_key;
  577. int error, revents = 0;
  578. error = ttydev_enter(tp);
  579. if (error)
  580. return ((events & (POLLIN | POLLRDNORM)) | POLLHUP);
  581. if (events & (POLLIN | POLLRDNORM))
  582. {
  583. /* See if we can read something. */
  584. if (ttydisc_read_poll(tp) > 0)
  585. revents |= events & (POLLIN | POLLRDNORM);
  586. }
  587. if (tp->t_flags & TF_ZOMBIE)
  588. {
  589. /* Hangup flag on zombie state. */
  590. revents |= POLLHUP;
  591. }
  592. else if (events & (POLLOUT | POLLWRNORM))
  593. {
  594. /* See if we can write something. */
  595. if (ttydisc_write_poll(tp) > 0)
  596. revents |= events & (POLLOUT | POLLWRNORM);
  597. }
  598. if (revents == 0)
  599. {
  600. if (events & (POLLIN | POLLRDNORM))
  601. rt_poll_add(&tp->t_inpoll, req);
  602. if (events & (POLLOUT | POLLWRNORM))
  603. rt_poll_add(&tp->t_outpoll, req);
  604. }
  605. tty_unlock(tp);
  606. return revents;
  607. }
  608. static struct cdevsw ttydev_cdevsw = {
  609. .d_open = ttydev_open,
  610. .d_close = ttydev_close,
  611. .d_read = ttydev_read,
  612. .d_write = ttydev_write,
  613. .d_ioctl = ttydev_ioctl,
  614. #if 0
  615. .d_kqfilter = ttydev_kqfilter,
  616. #endif
  617. .d_poll = ttydev_poll,
  618. #if 0
  619. .d_mmap = ttydev_mmap,
  620. #endif
  621. #ifdef USING_BSD_RAW_CDEVSW
  622. .d_version = D_VERSION.d_name = "ttydev",
  623. .d_flags = D_TTY,
  624. #endif /* USING_BSD_RAW_CDEVSW */
  625. };
  626. extern struct cdevsw bsd_ttydev_methods __attribute__((alias("ttydev_cdevsw")));
  627. /*
  628. * Standard device routine implementations, mostly meant for
  629. * pseudo-terminal device drivers. When a driver creates a new terminal
  630. * device class, missing routines are patched.
  631. */
  632. #define panic(msg) RT_ASSERT(0 && msg)
  633. static int ttydevsw_defopen(struct lwp_tty *tp __unused)
  634. {
  635. return 0;
  636. }
  637. static void ttydevsw_defclose(struct lwp_tty *tp __unused)
  638. {
  639. }
  640. static void ttydevsw_defoutwakeup(struct lwp_tty *tp __unused)
  641. {
  642. panic("Terminal device has output, while not implemented");
  643. }
  644. static void ttydevsw_definwakeup(struct lwp_tty *tp __unused)
  645. {
  646. }
  647. static int ttydevsw_defioctl(struct lwp_tty *tp __unused, rt_ubase_t cmd __unused,
  648. rt_caddr_t data __unused,
  649. struct rt_thread *td __unused)
  650. {
  651. return -ENOSYS;
  652. }
  653. static int ttydevsw_defcioctl(struct lwp_tty *tp __unused, int unit __unused,
  654. rt_ubase_t cmd __unused, rt_caddr_t data __unused,
  655. struct rt_thread *td __unused)
  656. {
  657. return -ENOSYS;
  658. }
  659. static int ttydevsw_defparam(struct lwp_tty *tp __unused, struct termios *t)
  660. {
  661. /*
  662. * Allow the baud rate to be adjusted for pseudo-devices, but at
  663. * least restrict it to 115200 to prevent excessive buffer
  664. * usage. Also disallow 0, to prevent foot shooting.
  665. */
  666. if (t->__c_ispeed < B50)
  667. t->__c_ispeed = B50;
  668. else if (t->__c_ispeed > B115200)
  669. t->__c_ispeed = B115200;
  670. if (t->__c_ospeed < B50)
  671. t->__c_ospeed = B50;
  672. else if (t->__c_ospeed > B115200)
  673. t->__c_ospeed = B115200;
  674. t->c_cflag |= CREAD;
  675. return 0;
  676. }
  677. static int ttydevsw_defmodem(struct lwp_tty *tp __unused, int sigon __unused,
  678. int sigoff __unused)
  679. {
  680. /* Simulate a carrier to make the TTY layer happy. */
  681. return (SER_DCD);
  682. }
  683. static int ttydevsw_defmmap(struct lwp_tty *tp __unused,
  684. vm_ooffset_t offset __unused,
  685. vm_paddr_t *paddr __unused, int nprot __unused,
  686. vm_memattr_t *memattr __unused)
  687. {
  688. return (-1);
  689. }
  690. static void ttydevsw_defpktnotify(struct lwp_tty *tp __unused,
  691. char event __unused)
  692. {
  693. }
  694. static void ttydevsw_deffree(void *softc __unused)
  695. {
  696. panic("Terminal device freed without a free-handler");
  697. }
  698. static rt_bool_t ttydevsw_defbusy(struct lwp_tty *tp __unused)
  699. {
  700. return (RT_FALSE);
  701. }
  702. void bsd_devsw_init(struct lwp_ttydevsw *tsw)
  703. {
  704. /* Make sure the driver defines all routines. */
  705. #define PATCH_FUNC(x) \
  706. do \
  707. { \
  708. if (tsw->tsw_##x == NULL) \
  709. tsw->tsw_##x = ttydevsw_def##x; \
  710. } while (0)
  711. PATCH_FUNC(open);
  712. PATCH_FUNC(close);
  713. PATCH_FUNC(outwakeup);
  714. PATCH_FUNC(inwakeup);
  715. PATCH_FUNC(ioctl);
  716. PATCH_FUNC(cioctl);
  717. PATCH_FUNC(param);
  718. PATCH_FUNC(modem);
  719. PATCH_FUNC(mmap);
  720. PATCH_FUNC(pktnotify);
  721. PATCH_FUNC(free);
  722. PATCH_FUNC(busy);
  723. #undef PATCH_FUNC
  724. }
  725. /* release tty, and free the cdev resource */
  726. static void tty_rel_free(struct lwp_tty *tp)
  727. {
  728. #ifdef USING_BSD_CHAR_DEVICE
  729. struct cdev *dev;
  730. #endif
  731. tty_assert_locked(tp);
  732. #define TF_ACTIVITY (TF_GONE | TF_OPENED | TF_HOOK | TF_OPENCLOSE)
  733. if (tp->t_sessioncnt != 0 || (tp->t_flags & TF_ACTIVITY) != TF_GONE)
  734. {
  735. /* TTY is still in use. */
  736. tty_unlock(tp);
  737. return;
  738. }
  739. #ifdef USING_BSD_AIO
  740. /* Stop asynchronous I/O. */
  741. funsetown(&tp->t_sigio);
  742. #endif /* USING_BSD_AIO */
  743. #ifdef USING_BSD_CHAR_DEVICE
  744. /* TTY can be deallocated. */
  745. dev = tp->t_dev;
  746. tp->t_dev = NULL;
  747. #endif /* USING_BSD_CHAR_DEVICE */
  748. tty_unlock(tp);
  749. #ifdef USING_BSD_CHAR_DEVICE
  750. if (dev != NULL)
  751. {
  752. sx_xlock(&tty_list_sx);
  753. TAILQ_REMOVE(&tty_list, tp, t_list);
  754. tty_list_count--;
  755. sx_xunlock(&tty_list_sx);
  756. destroy_dev_sched_cb(dev, tty_dealloc, tp);
  757. }
  758. #else
  759. lwp_tty_delete(tp);
  760. #endif
  761. }
  762. void tty_rel_pgrp(struct lwp_tty *tp, struct rt_processgroup *pg)
  763. {
  764. MPASS(tp->t_sessioncnt > 0);
  765. tty_assert_locked(tp);
  766. if (tp->t_pgrp == pg)
  767. tp->t_pgrp = NULL;
  768. tty_unlock(tp);
  769. }
  770. void tty_rel_sess(struct lwp_tty *tp, struct rt_session *sess)
  771. {
  772. MPASS(tp->t_sessioncnt > 0);
  773. /* Current session has left. */
  774. if (tp->t_session == sess)
  775. {
  776. tp->t_session = NULL;
  777. MPASS(tp->t_pgrp == NULL);
  778. }
  779. tp->t_sessioncnt--;
  780. tty_rel_free(tp);
  781. }
  782. /* deallocate the tty */
  783. void tty_rel_gone(struct lwp_tty *tp)
  784. {
  785. tty_assert_locked(tp);
  786. MPASS(!tty_gone(tp));
  787. /* Simulate carrier removal. */
  788. ttydisc_modem(tp, 0);
  789. /* Wake up all blocked threads. */
  790. tty_wakeup(tp, FREAD | FWRITE);
  791. cv_broadcast(&tp->t_bgwait);
  792. cv_broadcast(&tp->t_dcdwait);
  793. tp->t_flags |= TF_GONE;
  794. tty_rel_free(tp);
  795. }
  796. static int tty_drop_ctty(struct lwp_tty *tp, struct rt_lwp *p)
  797. {
  798. struct rt_session *session;
  799. #ifdef USING_BSD_VNODE
  800. struct vnode *vp;
  801. #endif
  802. /*
  803. * This looks terrible, but it's generally safe as long as the tty
  804. * hasn't gone away while we had the lock dropped. All of our sanity
  805. * checking that this operation is OK happens after we've picked it back
  806. * up, so other state changes are generally not fatal and the potential
  807. * for this particular operation to happen out-of-order in a
  808. * multithreaded scenario is likely a non-issue.
  809. */
  810. tty_unlock(tp);
  811. LWP_LOCK(p);
  812. tty_lock(tp);
  813. if (tty_gone(tp))
  814. {
  815. LWP_UNLOCK(p);
  816. return -ENODEV;
  817. }
  818. /*
  819. * If the session doesn't have a controlling TTY, or if we weren't
  820. * invoked on the controlling TTY, we'll return ENOIOCTL as we've
  821. * historically done.
  822. */
  823. session = p->pgrp->session;
  824. if (session->ctty == NULL || session->ctty != tp)
  825. {
  826. LWP_UNLOCK(p);
  827. return -ENOTTY;
  828. }
  829. if (!is_sess_leader(p))
  830. {
  831. LWP_UNLOCK(p);
  832. return -EPERM;
  833. }
  834. SESS_LOCK(session);
  835. #ifdef USING_BSD_VNODE
  836. vp = session->s_ttyvp;
  837. #endif
  838. session->ctty = NULL;
  839. #ifdef USING_BSD_VNODE
  840. session->s_ttyvp = NULL;
  841. session->s_ttydp = NULL;
  842. #endif
  843. SESS_UNLOCK(session);
  844. tp->t_sessioncnt--;
  845. p->term_ctrlterm = RT_FALSE;
  846. LWP_UNLOCK(p);
  847. #ifdef USING_BSD_VNODE
  848. /*
  849. * If we did have a vnode, release our reference. Ordinarily we manage
  850. * these at the devfs layer, but we can't necessarily know that we were
  851. * invoked on the vnode referenced in the session (i.e. the vnode we
  852. * hold a reference to). We explicitly don't check VBAD/VIRF_DOOMED here
  853. * to avoid a vnode leak -- in circumstances elsewhere where we'd hit a
  854. * VIRF_DOOMED vnode, release has been deferred until the controlling TTY
  855. * is either changed or released.
  856. */
  857. if (vp != NULL)
  858. devfs_ctty_unref(vp);
  859. #endif
  860. return 0;
  861. }
  862. void tty_wakeup(struct lwp_tty *tp, int flags)
  863. {
  864. #ifdef USING_BSD_AIO
  865. if (tp->t_flags & TF_ASYNC && tp->t_sigio != NULL)
  866. pgsigio(&tp->t_sigio, SIGIO, (tp->t_session != NULL));
  867. #endif
  868. if (flags & FWRITE)
  869. {
  870. cv_broadcast(&tp->t_outwait);
  871. #ifdef USING_BSD_POLL
  872. selwakeup(&tp->t_outpoll);
  873. KNOTE_LOCKED(&tp->t_outpoll.si_note, 0);
  874. #else
  875. rt_wqueue_wakeup_all(&tp->t_outpoll, (void *)POLLOUT);
  876. #endif
  877. }
  878. if (flags & FREAD)
  879. {
  880. cv_broadcast(&tp->t_inwait);
  881. #ifdef USING_BSD_POLL
  882. selwakeup(&tp->t_inpoll);
  883. KNOTE_LOCKED(&tp->t_inpoll.si_note, 0);
  884. #else
  885. rt_wqueue_wakeup_all(&tp->t_inpoll, (void *)POLLIN);
  886. #endif
  887. }
  888. }
  889. int tty_wait(struct lwp_tty *tp, struct rt_condvar *cv)
  890. {
  891. int error;
  892. int revokecnt = tp->t_revokecnt;
  893. tty_lock_assert(tp, MA_OWNED | MA_NOTRECURSED);
  894. MPASS(!tty_gone(tp));
  895. error = cv_wait_sig(cv, tp->t_mtx);
  896. /* Bail out when the device slipped away. */
  897. if (tty_gone(tp))
  898. return -ENXIO;
  899. /* Restart the system call when we may have been revoked. */
  900. if (tp->t_revokecnt != revokecnt)
  901. return -ERESTART;
  902. return error;
  903. }
  904. int tty_timedwait(struct lwp_tty *tp, struct rt_condvar *cv, rt_tick_t timeout)
  905. {
  906. int error;
  907. int revokecnt = tp->t_revokecnt;
  908. tty_lock_assert(tp, MA_OWNED | MA_NOTRECURSED);
  909. MPASS(!tty_gone(tp));
  910. error = cv_timedwait_sig(cv, tp->t_mtx, timeout);
  911. /* Bail out when the device slipped away. */
  912. if (tty_gone(tp))
  913. return -ENXIO;
  914. /* Restart the system call when we may have been revoked. */
  915. if (tp->t_revokecnt != revokecnt)
  916. return -ERESTART;
  917. return error;
  918. }
  919. /* discard data in I/O buffers */
  920. void tty_flush(struct lwp_tty *tp, int flags)
  921. {
  922. if (flags & FWRITE)
  923. {
  924. tp->t_flags &= ~TF_HIWAT_OUT;
  925. ttyoutq_flush(&tp->t_outq);
  926. tty_wakeup(tp, FWRITE);
  927. if (!tty_gone(tp))
  928. {
  929. ttydevsw_outwakeup(tp);
  930. ttydevsw_pktnotify(tp, TIOCPKT_FLUSHWRITE);
  931. }
  932. }
  933. if (flags & FREAD)
  934. {
  935. tty_hiwat_in_unblock(tp);
  936. ttyinq_flush(&tp->t_inq);
  937. tty_wakeup(tp, FREAD);
  938. if (!tty_gone(tp))
  939. {
  940. ttydevsw_inwakeup(tp);
  941. ttydevsw_pktnotify(tp, TIOCPKT_FLUSHREAD);
  942. }
  943. }
  944. }
  945. void tty_set_winsize(struct lwp_tty *tp, const struct winsize *wsz)
  946. {
  947. if (memcmp(&tp->t_winsize, wsz, sizeof(*wsz)) == 0)
  948. return;
  949. tp->t_winsize = *wsz;
  950. lwp_tty_signal_pgrp(tp, SIGWINCH);
  951. }
  952. static int tty_generic_ioctl(struct lwp_tty *tp, rt_ubase_t cmd, void *data,
  953. int fflag, struct rt_thread *td)
  954. {
  955. int error;
  956. switch (cmd)
  957. {
  958. /*
  959. * Modem commands.
  960. * The SER_* and TIOCM_* flags are the same, but one bit
  961. * shifted. I don't know why.
  962. */
  963. case TIOCSDTR:
  964. ttydevsw_modem(tp, SER_DTR, 0);
  965. return 0;
  966. case TIOCCDTR:
  967. ttydevsw_modem(tp, 0, SER_DTR);
  968. return 0;
  969. case TIOCMSET: {
  970. int bits = *(int *)data;
  971. ttydevsw_modem(tp, (bits & (TIOCM_DTR | TIOCM_RTS)) >> 1,
  972. ((~bits) & (TIOCM_DTR | TIOCM_RTS)) >> 1);
  973. return 0;
  974. }
  975. case TIOCMBIS: {
  976. int bits = *(int *)data;
  977. ttydevsw_modem(tp, (bits & (TIOCM_DTR | TIOCM_RTS)) >> 1, 0);
  978. return 0;
  979. }
  980. case TIOCMBIC: {
  981. int bits = *(int *)data;
  982. ttydevsw_modem(tp, 0, (bits & (TIOCM_DTR | TIOCM_RTS)) >> 1);
  983. return 0;
  984. }
  985. case TIOCMGET:
  986. *(int *)data = TIOCM_LE + (ttydevsw_modem(tp, 0, 0) << 1);
  987. return 0;
  988. case FIOASYNC:
  989. if (*(int *)data)
  990. tp->t_flags |= TF_ASYNC;
  991. else
  992. tp->t_flags &= ~TF_ASYNC;
  993. return 0;
  994. case FIONBIO:
  995. /* This device supports non-blocking operation. */
  996. return 0;
  997. case FIONREAD:
  998. *(int *)data = ttyinq_bytescanonicalized(&tp->t_inq);
  999. return 0;
  1000. case FIONWRITE:
  1001. case TIOCOUTQ:
  1002. *(int *)data = ttyoutq_bytesused(&tp->t_outq);
  1003. return 0;
  1004. #if BSD_USING_FIO_OWNERSHIP
  1005. case FIOSETOWN:
  1006. if (tp->t_session != NULL && !tty_is_ctty(tp, td->lwp))
  1007. /* Not allowed to set ownership. */
  1008. return -ENOTTY;
  1009. /* Temporarily unlock the TTY to set ownership. */
  1010. tty_unlock(tp);
  1011. error = fsetown(*(int *)data, &tp->t_sigio);
  1012. tty_lock(tp);
  1013. return (error);
  1014. case FIOGETOWN:
  1015. if (tp->t_session != NULL && !tty_is_ctty(tp, td->lwp))
  1016. /* Not allowed to set ownership. */
  1017. return -ENOTTY;
  1018. /* Get ownership. */
  1019. *(int *)data = fgetown(&tp->t_sigio);
  1020. return 0;
  1021. #endif
  1022. case TIOCGETA:
  1023. /* Obtain terminal flags through tcgetattr(). */
  1024. *(struct termios *)data = tp->t_termios;
  1025. return 0;
  1026. case TIOCSETA:
  1027. case TIOCSETAW:
  1028. case TIOCSETAF: {
  1029. struct termios *t = data;
  1030. /*
  1031. * Who makes up these funny rules? According to POSIX,
  1032. * input baud rate is set equal to the output baud rate
  1033. * when zero.
  1034. */
  1035. if (t->__c_ispeed == 0)
  1036. t->__c_ispeed = t->__c_ospeed;
  1037. /* Discard any unsupported bits. */
  1038. t->c_iflag &= TTYSUP_IFLAG;
  1039. t->c_oflag &= TTYSUP_OFLAG;
  1040. t->c_lflag &= TTYSUP_LFLAG;
  1041. t->c_cflag &= TTYSUP_CFLAG;
  1042. /* Set terminal flags through tcsetattr(). */
  1043. if (cmd == TIOCSETAW || cmd == TIOCSETAF)
  1044. {
  1045. error = tty_drain(tp, 0);
  1046. if (error)
  1047. return (error);
  1048. if (cmd == TIOCSETAF)
  1049. tty_flush(tp, FREAD);
  1050. }
  1051. /*
  1052. * Only call param() when the flags really change.
  1053. */
  1054. if ((t->c_cflag & CIGNORE) == 0 &&
  1055. (tp->t_termios.c_cflag != t->c_cflag ||
  1056. ((tp->t_termios.c_iflag ^ t->c_iflag) &
  1057. (IXON | IXOFF | IXANY)) ||
  1058. tp->t_termios.__c_ispeed != t->__c_ispeed ||
  1059. tp->t_termios.__c_ospeed != t->__c_ospeed))
  1060. {
  1061. error = ttydevsw_param(tp, t);
  1062. if (error)
  1063. return (error);
  1064. /* XXX: CLOCAL? */
  1065. tp->t_termios.c_cflag = t->c_cflag & ~CIGNORE;
  1066. tp->t_termios.__c_ispeed = t->__c_ispeed;
  1067. tp->t_termios.__c_ospeed = t->__c_ospeed;
  1068. /* Baud rate has changed - update watermarks. */
  1069. error = tty_watermarks(tp);
  1070. if (error)
  1071. return (error);
  1072. }
  1073. /* Copy new non-device driver parameters. */
  1074. tp->t_termios.c_iflag = t->c_iflag;
  1075. tp->t_termios.c_oflag = t->c_oflag;
  1076. tp->t_termios.c_lflag = t->c_lflag;
  1077. memcpy(&tp->t_termios.c_cc, t->c_cc, sizeof t->c_cc);
  1078. ttydisc_optimize(tp);
  1079. if ((t->c_lflag & ICANON) == 0)
  1080. {
  1081. /*
  1082. * When in non-canonical mode, wake up all
  1083. * readers. Canonicalize any partial input. VMIN
  1084. * and VTIME could also be adjusted.
  1085. */
  1086. ttyinq_canonicalize(&tp->t_inq);
  1087. tty_wakeup(tp, FREAD);
  1088. }
  1089. /**
  1090. * For packet mode: notify the PTY consumer that VSTOP
  1091. * and VSTART may have been changed.
  1092. *
  1093. * TODO: change the _CONTROL('S') to a CSTOP?
  1094. */
  1095. if (tp->t_termios.c_iflag & IXON &&
  1096. tp->t_termios.c_cc[VSTOP] == _CONTROL('S') &&
  1097. tp->t_termios.c_cc[VSTART] == _CONTROL('Q'))
  1098. ttydevsw_pktnotify(tp, TIOCPKT_DOSTOP);
  1099. else
  1100. ttydevsw_pktnotify(tp, TIOCPKT_NOSTOP);
  1101. return 0;
  1102. }
  1103. case TIOCGETD:
  1104. /* For compatibility - we only support TTYDISC. */
  1105. *(int *)data = TTYDISC;
  1106. return 0;
  1107. case TIOCGPGRP:
  1108. if (!tty_is_ctty(tp, td->lwp))
  1109. return -ENOTTY;
  1110. if (tp->t_pgrp != NULL)
  1111. *(int *)data = tp->t_pgrp->pgid;
  1112. else
  1113. *(int *)data = NO_PID;
  1114. return 0;
  1115. case TIOCGSID:
  1116. if (!tty_is_ctty(tp, td->lwp))
  1117. return -ENOTTY;
  1118. MPASS(tp->t_session);
  1119. *(int *)data = tp->t_session->sid;
  1120. return 0;
  1121. case TIOCNOTTY:
  1122. return tty_drop_ctty(tp, td->lwp);
  1123. case TIOCSCTTY:
  1124. return lwp_tty_set_ctrl_proc(tp, td);
  1125. case TIOCSPGRP: {
  1126. int pgid;
  1127. if (lwp_in_user_space((void *)data))
  1128. {
  1129. if (lwp_get_from_user(&pgid, data, sizeof(int)) != sizeof(int))
  1130. return -EFAULT;
  1131. }
  1132. else
  1133. {
  1134. pgid = *(int *)data;
  1135. }
  1136. return lwp_tty_assign_foreground(tp, td, pgid);
  1137. }
  1138. case TIOCFLUSH: {
  1139. int flags = *(int *)data;
  1140. if (flags == 0)
  1141. flags = (FREAD | FWRITE);
  1142. else
  1143. flags &= (FREAD | FWRITE);
  1144. tty_flush(tp, flags);
  1145. return 0;
  1146. }
  1147. case TIOCDRAIN:
  1148. /* Drain TTY output. */
  1149. return tty_drain(tp, 0);
  1150. case TIOCGDRAINWAIT:
  1151. *(int *)data = tp->t_drainwait;
  1152. return 0;
  1153. case TIOCSDRAINWAIT:
  1154. error = priv_check(td, PRIV_TTY_DRAINWAIT);
  1155. if (error == 0)
  1156. tp->t_drainwait = *(int *)data;
  1157. return (error);
  1158. case TIOCCONS:
  1159. /* Set terminal as console TTY. */
  1160. if (*(int *)data)
  1161. {
  1162. error = priv_check(td, PRIV_TTY_CONSOLE);
  1163. if (error)
  1164. return (error);
  1165. error = constty_set(tp);
  1166. }
  1167. else
  1168. {
  1169. error = constty_clear(tp);
  1170. }
  1171. return (error);
  1172. case TIOCGWINSZ:
  1173. /* Obtain window size. */
  1174. *(struct winsize *)data = tp->t_winsize;
  1175. return 0;
  1176. case TIOCSWINSZ:
  1177. /* Set window size. */
  1178. tty_set_winsize(tp, data);
  1179. return 0;
  1180. case TIOCEXCL:
  1181. tp->t_flags |= TF_EXCLUDE;
  1182. return 0;
  1183. case TIOCNXCL:
  1184. tp->t_flags &= ~TF_EXCLUDE;
  1185. return 0;
  1186. case TIOCSTOP:
  1187. tp->t_flags |= TF_STOPPED;
  1188. ttydevsw_pktnotify(tp, TIOCPKT_STOP);
  1189. return 0;
  1190. case TIOCSTART:
  1191. tp->t_flags &= ~TF_STOPPED;
  1192. tp->t_termios.c_lflag &= ~FLUSHO;
  1193. ttydevsw_outwakeup(tp);
  1194. ttydevsw_pktnotify(tp, TIOCPKT_START);
  1195. return 0;
  1196. case TIOCSTAT:
  1197. tty_info(tp);
  1198. return 0;
  1199. case TIOCSTI:
  1200. if ((fflag & FREAD) == 0 && priv_check(td, PRIV_TTY_STI))
  1201. return -EPERM;
  1202. if (!tty_is_ctty(tp, td->lwp) && priv_check(td, PRIV_TTY_STI))
  1203. return -EACCES;
  1204. ttydisc_rint(tp, *(char *)data, 0);
  1205. ttydisc_rint_done(tp);
  1206. return 0;
  1207. }
  1208. #ifdef COMPAT_43TTY
  1209. return tty_ioctl_compat(tp, cmd, data, fflag, td);
  1210. #else /* !COMPAT_43TTY */
  1211. return -ENOIOCTL;
  1212. #endif /* COMPAT_43TTY */
  1213. }
  1214. int tty_ioctl(struct lwp_tty *tp, rt_ubase_t cmd, void *data, int fflag,
  1215. struct rt_thread *td)
  1216. {
  1217. int error;
  1218. tty_assert_locked(tp);
  1219. if (tty_gone(tp))
  1220. return -ENXIO;
  1221. error = ttydevsw_ioctl(tp, cmd, data, td);
  1222. if (error == -ENOIOCTL)
  1223. error = tty_generic_ioctl(tp, cmd, data, fflag, td);
  1224. return error;
  1225. }
  1226. int tty_checkoutq(struct lwp_tty *tp)
  1227. {
  1228. /* 256 bytes should be enough to print a log message. */
  1229. return (ttyoutq_bytesleft(&tp->t_outq) >= 256);
  1230. }
  1231. void tty_hiwat_in_block(struct lwp_tty *tp)
  1232. {
  1233. if ((tp->t_flags & TF_HIWAT_IN) == 0 && tp->t_termios.c_iflag & IXOFF &&
  1234. tp->t_termios.c_cc[VSTOP] != _POSIX_VDISABLE)
  1235. {
  1236. /*
  1237. * Input flow control. Only enter the high watermark when we
  1238. * can successfully store the VSTOP character.
  1239. */
  1240. if (ttyoutq_write_nofrag(&tp->t_outq, &tp->t_termios.c_cc[VSTOP], 1) ==
  1241. 0)
  1242. tp->t_flags |= TF_HIWAT_IN;
  1243. }
  1244. else
  1245. {
  1246. /* No input flow control. */
  1247. tp->t_flags |= TF_HIWAT_IN;
  1248. }
  1249. }
  1250. void tty_hiwat_in_unblock(struct lwp_tty *tp)
  1251. {
  1252. if (tp->t_flags & TF_HIWAT_IN && tp->t_termios.c_iflag & IXOFF &&
  1253. tp->t_termios.c_cc[VSTART] != _POSIX_VDISABLE)
  1254. {
  1255. /*
  1256. * Input flow control. Only leave the high watermark when we
  1257. * can successfully store the VSTART character.
  1258. */
  1259. if (ttyoutq_write_nofrag(&tp->t_outq, &tp->t_termios.c_cc[VSTART], 1) ==
  1260. 0)
  1261. tp->t_flags &= ~TF_HIWAT_IN;
  1262. }
  1263. else
  1264. {
  1265. /* No input flow control. */
  1266. tp->t_flags &= ~TF_HIWAT_IN;
  1267. }
  1268. if (!tty_gone(tp))
  1269. ttydevsw_inwakeup(tp);
  1270. }