tty.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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. /**
  109. * tty_check_change - check for POSIX terminal changes
  110. * @tty: tty to check
  111. *
  112. * If we try to write to, or set the state of, a terminal and we're
  113. * not in the foreground, send a SIGTTOU. If the signal is blocked or
  114. * ignored, go ahead and perform the operation. (POSIX 7.2)
  115. *
  116. * Locking: ctrl_lock
  117. */
  118. int __tty_check_change(struct tty_struct *tty, int sig)
  119. {
  120. pid_t pgrp = 0, tty_pgrp = 0;
  121. int ret = 0;
  122. int level = 0;
  123. level = rt_hw_interrupt_disable();
  124. if (current == RT_NULL)
  125. {
  126. rt_hw_interrupt_enable(level);
  127. return 0;
  128. }
  129. if (current->tty != tty)
  130. {
  131. rt_hw_interrupt_enable(level);
  132. return 0;
  133. }
  134. pgrp = current->__pgrp;
  135. tty_pgrp = tty->pgrp;
  136. if (tty_pgrp && (pgrp != tty->pgrp))
  137. {
  138. lwp_signal_kill(current, sig, SI_USER, 0);
  139. }
  140. rt_hw_interrupt_enable(level);
  141. if (!tty_pgrp)
  142. {
  143. LOG_D("sig=%d, tty->pgrp == -1!\n", sig);
  144. }
  145. return ret;
  146. }
  147. int tty_check_change(struct tty_struct *tty)
  148. {
  149. return __tty_check_change(tty, SIGTTOU);
  150. }
  151. static int tty_open(struct dfs_file *fd)
  152. {
  153. int ret = 0;
  154. int noctty = 0;
  155. struct tty_struct *tty = RT_NULL;
  156. struct tty_ldisc *ld = RT_NULL;
  157. tty = (struct tty_struct *)fd->vnode->data;
  158. RT_ASSERT(tty != RT_NULL);
  159. ld = tty->ldisc;
  160. if (ld->ops->open)
  161. {
  162. ret = ld->ops->open(fd);
  163. }
  164. noctty = (fd->flags & O_NOCTTY);
  165. rt_device_t device = (rt_device_t)fd->vnode->data;
  166. if (fd->vnode->ref_count == 1)
  167. {
  168. ret = rt_device_open(device, fd->flags);
  169. }
  170. if (current == RT_NULL) //kernel mode not lwp
  171. {
  172. return ret;
  173. }
  174. if (!noctty &&
  175. current->leader &&
  176. !current->tty &&
  177. tty->session == -1)
  178. {
  179. current->tty = tty;
  180. current->tty_old_pgrp = 0;
  181. tty->session = current->session;
  182. tty->pgrp = current->__pgrp;
  183. tty->foreground = current;
  184. }
  185. return ret;
  186. }
  187. static int tty_close(struct dfs_file *fd)
  188. {
  189. int ret = 0;
  190. struct tty_struct *tty = RT_NULL;
  191. struct tty_ldisc *ld = RT_NULL;
  192. tty = (struct tty_struct *)fd->vnode->data;
  193. RT_ASSERT(tty != RT_NULL);
  194. ld = tty->ldisc;
  195. if (ld->ops->close)
  196. {
  197. //ld->ops->close(tty);
  198. }
  199. if (fd->vnode->ref_count == 1)
  200. {
  201. ret = rt_device_close((rt_device_t)tty);
  202. }
  203. return ret;
  204. }
  205. static int tiocsctty(struct tty_struct *tty, int arg)
  206. {
  207. if (current->leader &&
  208. (current->session == tty->session))
  209. {
  210. return 0;
  211. }
  212. /*
  213. * The process must be a session leader and
  214. * not have a controlling tty already.
  215. */
  216. if (!current->leader || current->tty)
  217. {
  218. return -EPERM;
  219. }
  220. if (tty->session > 0)
  221. {
  222. LOG_E("this tty have control process\n");
  223. }
  224. current->tty = tty;
  225. current->tty_old_pgrp = 0;
  226. tty->session = current->session;
  227. tty->pgrp = current->__pgrp;
  228. tty->foreground = current;
  229. if (tty->type == TTY_DRIVER_TYPE_PTY)
  230. {
  231. tty->other_struct->foreground = current;
  232. }
  233. return 0;
  234. }
  235. static int tty_ioctl(struct dfs_file *fd, int cmd, void *args)
  236. {
  237. int ret = 0;
  238. struct tty_struct *tty = RT_NULL;
  239. struct tty_struct *real_tty = RT_NULL;
  240. struct tty_ldisc *ld = RT_NULL;
  241. tty = (struct tty_struct *)fd->vnode->data;
  242. RT_ASSERT(tty != RT_NULL);
  243. if (tty->type == TTY_DRIVER_TYPE_PTY && tty->subtype == PTY_TYPE_MASTER)
  244. {
  245. real_tty = tty->other_struct;
  246. }
  247. else
  248. {
  249. real_tty = tty;
  250. }
  251. switch (cmd)
  252. {
  253. case TIOCSCTTY:
  254. return tiocsctty(real_tty, 1);
  255. }
  256. ld = tty->ldisc;
  257. if (ld->ops->ioctl)
  258. {
  259. ret = ld->ops->ioctl(fd, cmd, args);
  260. }
  261. return ret;
  262. }
  263. #ifdef RT_USING_DFS_V2
  264. static ssize_t tty_read(struct dfs_file *fd, void *buf, size_t count, off_t *pos)
  265. #else
  266. static ssize_t tty_read(struct dfs_file *fd, void *buf, size_t count)
  267. #endif
  268. {
  269. int ret = 0;
  270. struct tty_struct *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. ld = tty->ldisc;
  275. if (ld && ld->ops->read)
  276. {
  277. ret = ld->ops->read(fd, buf, count);
  278. }
  279. return ret;
  280. }
  281. #ifdef RT_USING_DFS_V2
  282. static ssize_t tty_write(struct dfs_file *fd, const void *buf, size_t count, off_t *pos)
  283. #else
  284. static ssize_t tty_write(struct dfs_file *fd, const void *buf, size_t count )
  285. #endif
  286. {
  287. int ret = 0;
  288. struct tty_struct *tty = RT_NULL;
  289. struct tty_ldisc *ld = RT_NULL;
  290. tty = (struct tty_struct *)fd->vnode->data;
  291. RT_ASSERT(tty != RT_NULL);
  292. ld = tty->ldisc;
  293. if (ld && ld->ops->write)
  294. {
  295. ret = ld->ops->write(fd, buf, count);
  296. }
  297. return ret;
  298. }
  299. static int tty_poll(struct dfs_file *fd, struct rt_pollreq *req)
  300. {
  301. int ret = 0;
  302. struct tty_struct *tty = RT_NULL;
  303. struct tty_ldisc *ld = RT_NULL;
  304. tty = (struct tty_struct *)fd->vnode->data;
  305. RT_ASSERT(tty != RT_NULL);
  306. ld = tty->ldisc;
  307. if (ld->ops->poll)
  308. {
  309. ret = ld->ops->poll(fd, req);
  310. }
  311. return ret;
  312. }
  313. static const struct dfs_file_ops tty_fops =
  314. {
  315. .open = tty_open,
  316. .close = tty_close,
  317. .ioctl = tty_ioctl,
  318. .read = tty_read,
  319. .write = tty_write,
  320. .poll = tty_poll,
  321. };
  322. const struct dfs_file_ops *tty_get_fops(void)
  323. {
  324. return &tty_fops;
  325. }
  326. int tty_init(struct tty_struct *tty, int type, int subtype, struct rt_device *iodev)
  327. {
  328. if (tty)
  329. {
  330. struct tty_node *node = NULL;
  331. node = rt_calloc(1, sizeof(struct tty_node));
  332. if (node)
  333. {
  334. tty->type = type;
  335. tty->subtype = subtype;
  336. tty->io_dev = iodev;
  337. tty->head = node;
  338. tty_initstack(tty->head);
  339. tty->pgrp = -1;
  340. tty->session = -1;
  341. tty->foreground = RT_NULL;
  342. rt_mutex_init(&tty->lock, "ttyLock", RT_IPC_FLAG_PRIO);
  343. rt_wqueue_init(&tty->wait_queue);
  344. tty_ldisc_init(tty);
  345. tty->init_termios = tty_std_termios;
  346. tty->init_flag = TTY_INIT_FLAG_REGED;
  347. }
  348. }
  349. return 0;
  350. }