tty.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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. * 2021-12-07 linzhenxing first version
  9. * 2023-06-26 WangXiaoyao fix bug on foreground app switch
  10. */
  11. #include <dfs_file.h>
  12. #include <dfs_fs.h>
  13. #include <lwp.h>
  14. #include <rtdevice.h>
  15. #include <rtthread.h>
  16. #include <tty.h>
  17. #include <tty_ldisc.h>
  18. #if defined(RT_USING_POSIX_DEVIO)
  19. #include <termios.h>
  20. #endif
  21. #define DBG_TAG "TTY"
  22. #ifdef RT_TTY_DEBUG
  23. #define DBG_LVL DBG_LOG
  24. #else
  25. #define DBG_LVL DBG_INFO
  26. #endif /* RT_TTY_DEBUG */
  27. #include <rtdbg.h>
  28. const struct termios tty_std_termios = { /* for the benefit of tty drivers */
  29. .c_iflag = IMAXBEL | IUCLC | INLCR | ICRNL | IGNPAR,
  30. .c_oflag = OPOST,
  31. .c_cflag = B38400 | CS8 | CREAD | HUPCL,
  32. .c_lflag = ISIG | ECHOE | TOSTOP | NOFLSH,
  33. RT_NULL,/* .c_line = N_TTY, */
  34. .c_cc = INIT_C_CC,
  35. .__c_ispeed = 38400,
  36. .__c_ospeed = 38400
  37. };
  38. void tty_initstack(struct tty_node *node)
  39. {
  40. node->lwp = RT_NULL;
  41. node->next = RT_NULL;
  42. }
  43. static struct tty_node tty_node_cache = { RT_NULL, RT_NULL };
  44. static struct tty_node *_tty_node_alloc(void)
  45. {
  46. struct tty_node *node = tty_node_cache.next;
  47. if (node == RT_NULL)
  48. {
  49. node = rt_calloc(1, sizeof(struct tty_node));
  50. }
  51. else
  52. {
  53. tty_node_cache.next = node->next;
  54. }
  55. return node;
  56. }
  57. static void _tty_node_free(struct tty_node *node)
  58. {
  59. node->next = tty_node_cache.next;
  60. tty_node_cache.next = node;
  61. }
  62. int tty_push(struct tty_node **head, struct rt_lwp *lwp)
  63. {
  64. struct tty_node *node = _tty_node_alloc();
  65. if (!node)
  66. {
  67. return -1;
  68. }
  69. node->lwp = lwp;
  70. node->next = *head;
  71. *head = node;
  72. return 0;
  73. }
  74. struct rt_lwp *tty_pop(struct tty_node **head, struct rt_lwp *target_lwp)
  75. {
  76. struct tty_node *node;
  77. struct rt_lwp *lwp = RT_NULL;
  78. if (!head || !*head)
  79. {
  80. return RT_NULL;
  81. }
  82. node = *head;
  83. if (target_lwp != RT_NULL && node->lwp != target_lwp)
  84. {
  85. struct tty_node *prev = RT_NULL;
  86. while (node != RT_NULL && node->lwp != target_lwp)
  87. {
  88. prev = node;
  89. node = node->next;
  90. }
  91. if (node != RT_NULL)
  92. {
  93. /* prev is impossible equ RT_NULL */
  94. prev->next = node->next;
  95. lwp = target_lwp;
  96. _tty_node_free(node);
  97. }
  98. }
  99. else
  100. {
  101. lwp = (*head)->lwp;
  102. *head = (*head)->next;
  103. node->lwp = RT_NULL;
  104. _tty_node_free(node);
  105. }
  106. return lwp;
  107. }
  108. rt_inline int tty_sigismember(lwp_sigset_t *set, int _sig)
  109. {
  110. unsigned long sig = _sig - 1;
  111. if (_LWP_NSIG_WORDS == 1)
  112. {
  113. return 1 & (set->sig[0] >> sig);
  114. }
  115. else
  116. {
  117. return 1 & (set->sig[sig / _LWP_NSIG_BPW] >> (sig % _LWP_NSIG_BPW));
  118. }
  119. }
  120. static int is_ignored(int sig)
  121. {
  122. return (tty_sigismember(&current->signal_mask, sig) ||
  123. current->signal_handler[sig-1] == SIG_IGN);
  124. }
  125. /**
  126. * tty_check_change - check for POSIX terminal changes
  127. * @tty: tty to check
  128. *
  129. * If we try to write to, or set the state of, a terminal and we're
  130. * not in the foreground, send a SIGTTOU. If the signal is blocked or
  131. * ignored, go ahead and perform the operation. (POSIX 7.2)
  132. *
  133. * Locking: ctrl_lock
  134. */
  135. int __tty_check_change(struct tty_struct *tty, int sig)
  136. {
  137. pid_t pgrp = 0, tty_pgrp = 0;
  138. struct rt_lwp *lwp = tty->foreground;
  139. int ret = 0;
  140. int level = 0;
  141. level = rt_hw_interrupt_disable();
  142. if (current == RT_NULL)
  143. {
  144. rt_hw_interrupt_enable(level);
  145. return 0;
  146. }
  147. if (current->tty != tty)
  148. {
  149. rt_hw_interrupt_enable(level);
  150. return 0;
  151. }
  152. pgrp = current->__pgrp;
  153. tty_pgrp = tty->pgrp;
  154. if (tty_pgrp && (pgrp != tty->pgrp))
  155. {
  156. if (is_ignored(sig))
  157. {
  158. if (sig == SIGTTIN)
  159. {
  160. ret = -EIO;
  161. }
  162. }
  163. else
  164. {
  165. if (lwp)
  166. {
  167. lwp_kill(lwp_to_pid(lwp), sig);
  168. }
  169. }
  170. }
  171. rt_hw_interrupt_enable(level);
  172. if (!tty_pgrp)
  173. {
  174. LOG_D("sig=%d, tty->pgrp == -1!\n", sig);
  175. }
  176. return ret;
  177. }
  178. int tty_check_change(struct tty_struct *tty)
  179. {
  180. return __tty_check_change(tty, SIGTTOU);
  181. }
  182. static int tty_open(struct dfs_file *fd)
  183. {
  184. int ret = 0;
  185. int noctty = 0;
  186. struct tty_struct *tty = RT_NULL;
  187. struct tty_ldisc *ld = RT_NULL;
  188. tty = (struct tty_struct *)fd->vnode->data;
  189. RT_ASSERT(tty != RT_NULL);
  190. ld = tty->ldisc;
  191. if (ld->ops->open)
  192. {
  193. ret = ld->ops->open(fd);
  194. }
  195. noctty = (fd->flags & O_NOCTTY);
  196. rt_device_t device = (rt_device_t)fd->vnode->data;
  197. if (fd->vnode->ref_count == 1)
  198. {
  199. ret = rt_device_open(device, fd->flags);
  200. }
  201. if (current == RT_NULL) //kernel mode not lwp
  202. {
  203. return ret;
  204. }
  205. if (!noctty &&
  206. current->leader &&
  207. !current->tty &&
  208. tty->session == -1)
  209. {
  210. current->tty = tty;
  211. current->tty_old_pgrp = 0;
  212. tty->session = current->session;
  213. tty->pgrp = current->__pgrp;
  214. tty->foreground = current;
  215. }
  216. return ret;
  217. }
  218. static int tty_close(struct dfs_file *fd)
  219. {
  220. int ret = 0;
  221. struct tty_struct *tty = RT_NULL;
  222. struct tty_ldisc *ld = RT_NULL;
  223. tty = (struct tty_struct *)fd->vnode->data;
  224. RT_ASSERT(tty != RT_NULL);
  225. ld = tty->ldisc;
  226. if (ld->ops->close)
  227. {
  228. //ld->ops->close(tty);
  229. }
  230. if (fd->vnode->ref_count == 1)
  231. {
  232. ret = rt_device_close((rt_device_t)tty);
  233. }
  234. return ret;
  235. }
  236. static int tiocsctty(struct tty_struct *tty, int arg)
  237. {
  238. if (current->leader &&
  239. (current->session == tty->session))
  240. {
  241. return 0;
  242. }
  243. /*
  244. * The process must be a session leader and
  245. * not have a controlling tty already.
  246. */
  247. if (!current->leader || current->tty)
  248. {
  249. return -EPERM;
  250. }
  251. if (tty->session > 0)
  252. {
  253. LOG_E("this tty have control process\n");
  254. }
  255. current->tty = tty;
  256. current->tty_old_pgrp = 0;
  257. tty->session = current->session;
  258. tty->pgrp = current->__pgrp;
  259. tty->foreground = current;
  260. if (tty->type == TTY_DRIVER_TYPE_PTY)
  261. {
  262. tty->other_struct->foreground = current;
  263. }
  264. return 0;
  265. }
  266. static int tty_ioctl(struct dfs_file *fd, int cmd, void *args)
  267. {
  268. int ret = 0;
  269. struct tty_struct *tty = RT_NULL;
  270. struct tty_struct *real_tty = RT_NULL;
  271. struct tty_ldisc *ld = RT_NULL;
  272. tty = (struct tty_struct *)fd->vnode->data;
  273. RT_ASSERT(tty != RT_NULL);
  274. if (tty->type == TTY_DRIVER_TYPE_PTY && tty->subtype == PTY_TYPE_MASTER)
  275. {
  276. real_tty = tty->other_struct;
  277. }
  278. else
  279. {
  280. real_tty = tty;
  281. }
  282. switch (cmd)
  283. {
  284. case TIOCSCTTY:
  285. return tiocsctty(real_tty, 1);
  286. }
  287. ld = tty->ldisc;
  288. if (ld->ops->ioctl)
  289. {
  290. ret = ld->ops->ioctl(fd, cmd, args);
  291. }
  292. return ret;
  293. }
  294. #ifdef RT_USING_DFS_V2
  295. static int tty_read(struct dfs_file *fd, void *buf, size_t count, off_t *pos)
  296. #else
  297. static int tty_read(struct dfs_file *fd, void *buf, size_t count)
  298. #endif
  299. {
  300. int ret = 0;
  301. struct tty_struct *tty = RT_NULL;
  302. struct tty_ldisc *ld = RT_NULL;
  303. tty = (struct tty_struct *)fd->vnode->data;
  304. RT_ASSERT(tty != RT_NULL);
  305. ld = tty->ldisc;
  306. if (ld && ld->ops->read)
  307. {
  308. ret = ld->ops->read(fd, buf, count);
  309. }
  310. return ret;
  311. }
  312. #ifdef RT_USING_DFS_V2
  313. static int tty_write(struct dfs_file *fd, const void *buf, size_t count, off_t *pos)
  314. #else
  315. static int tty_write(struct dfs_file *fd, const void *buf, size_t count )
  316. #endif
  317. {
  318. int ret = 0;
  319. struct tty_struct *tty = RT_NULL;
  320. struct tty_ldisc *ld = RT_NULL;
  321. tty = (struct tty_struct *)fd->vnode->data;
  322. RT_ASSERT(tty != RT_NULL);
  323. ld = tty->ldisc;
  324. if (ld && ld->ops->write)
  325. {
  326. ret = ld->ops->write(fd, buf, count);
  327. }
  328. return ret;
  329. }
  330. static int tty_poll(struct dfs_file *fd, struct rt_pollreq *req)
  331. {
  332. int ret = 0;
  333. struct tty_struct *tty = RT_NULL;
  334. struct tty_ldisc *ld = RT_NULL;
  335. tty = (struct tty_struct *)fd->vnode->data;
  336. RT_ASSERT(tty != RT_NULL);
  337. ld = tty->ldisc;
  338. if (ld->ops->poll)
  339. {
  340. ret = ld->ops->poll(fd, req);
  341. }
  342. return ret;
  343. }
  344. static const struct dfs_file_ops tty_fops =
  345. {
  346. .open = tty_open,
  347. .close = tty_close,
  348. .ioctl = tty_ioctl,
  349. .read = tty_read,
  350. .write = tty_write,
  351. .poll = tty_poll,
  352. };
  353. const struct dfs_file_ops *tty_get_fops(void)
  354. {
  355. return &tty_fops;
  356. }
  357. int tty_init(struct tty_struct *tty, int type, int subtype, struct rt_device *iodev)
  358. {
  359. if (tty)
  360. {
  361. struct tty_node *node = NULL;
  362. node = rt_calloc(1, sizeof(struct tty_node));
  363. if (node)
  364. {
  365. tty->type = type;
  366. tty->subtype = subtype;
  367. tty->io_dev = iodev;
  368. tty->head = node;
  369. tty_initstack(tty->head);
  370. tty->pgrp = -1;
  371. tty->session = -1;
  372. tty->foreground = RT_NULL;
  373. rt_mutex_init(&tty->lock, "ttyLock", RT_IPC_FLAG_PRIO);
  374. rt_wqueue_init(&tty->wait_queue);
  375. tty_ldisc_init(tty);
  376. tty->init_termios = tty_std_termios;
  377. tty->init_flag = TTY_INIT_FLAG_REGED;
  378. }
  379. }
  380. return 0;
  381. }