tty.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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. #include <shell.h>
  19. #if defined(RT_USING_POSIX_DEVIO)
  20. #include <termios.h>
  21. #endif
  22. #define DBG_TAG "TTY"
  23. #ifdef RT_TTY_DEBUG
  24. #define DBG_LVL DBG_LOG
  25. #else
  26. #define DBG_LVL DBG_INFO
  27. #endif /* RT_TTY_DEBUG */
  28. #include <rtdbg.h>
  29. const struct termios tty_std_termios = { /* for the benefit of tty drivers */
  30. .c_iflag = IMAXBEL | IUCLC | INLCR | ICRNL | IGNPAR,
  31. .c_oflag = OPOST,
  32. .c_cflag = B38400 | CS8 | CREAD | HUPCL,
  33. .c_lflag = ISIG | ECHOE | TOSTOP | NOFLSH,
  34. RT_NULL,/* .c_line = N_TTY, */
  35. .c_cc = INIT_C_CC,
  36. .__c_ispeed = 38400,
  37. .__c_ospeed = 38400
  38. };
  39. void tty_initstack(struct tty_node *node)
  40. {
  41. node->lwp = RT_NULL;
  42. node->next = RT_NULL;
  43. }
  44. static struct tty_node tty_node_cache = { RT_NULL, RT_NULL };
  45. static struct tty_node *_tty_node_alloc(void)
  46. {
  47. struct tty_node *node = tty_node_cache.next;
  48. if (node == RT_NULL)
  49. {
  50. node = rt_calloc(1, sizeof(struct tty_node));
  51. }
  52. else
  53. {
  54. tty_node_cache.next = node->next;
  55. }
  56. return node;
  57. }
  58. static void _tty_node_free(struct tty_node *node)
  59. {
  60. node->next = tty_node_cache.next;
  61. tty_node_cache.next = node;
  62. }
  63. int tty_push(struct tty_node **head, struct rt_lwp *lwp)
  64. {
  65. struct tty_node *node = _tty_node_alloc();
  66. if (!node)
  67. {
  68. return -1;
  69. }
  70. node->lwp = lwp;
  71. node->next = *head;
  72. *head = node;
  73. return 0;
  74. }
  75. struct rt_lwp *tty_pop(struct tty_node **head, struct rt_lwp *target_lwp)
  76. {
  77. struct tty_node *node;
  78. struct rt_lwp *lwp = RT_NULL;
  79. if (!head || !*head)
  80. {
  81. return RT_NULL;
  82. }
  83. node = *head;
  84. if (target_lwp != RT_NULL && node->lwp != target_lwp)
  85. {
  86. struct tty_node *prev = RT_NULL;
  87. while (node != RT_NULL && node->lwp != target_lwp)
  88. {
  89. prev = node;
  90. node = node->next;
  91. }
  92. if (node != RT_NULL)
  93. {
  94. /* prev is impossible equ RT_NULL */
  95. prev->next = node->next;
  96. lwp = target_lwp;
  97. _tty_node_free(node);
  98. }
  99. }
  100. else
  101. {
  102. lwp = (*head)->lwp;
  103. *head = (*head)->next;
  104. node->lwp = RT_NULL;
  105. _tty_node_free(node);
  106. }
  107. return lwp;
  108. }
  109. /**
  110. * tty_check_change - check for POSIX terminal changes
  111. * @tty: tty to check
  112. *
  113. * If we try to write to, or set the state of, a terminal and we're
  114. * not in the foreground, send a SIGTTOU. If the signal is blocked or
  115. * ignored, go ahead and perform the operation. (POSIX 7.2)
  116. *
  117. * Locking: ctrl_lock
  118. */
  119. int __tty_check_change(struct tty_struct *tty, int sig)
  120. {
  121. pid_t pgrp = 0, tty_pgrp = 0;
  122. int ret = 0;
  123. struct rt_lwp *lwp;
  124. lwp = lwp_self();
  125. if (lwp == RT_NULL)
  126. {
  127. return 0;
  128. }
  129. if (lwp->tty != tty)
  130. {
  131. return 0;
  132. }
  133. pgrp = lwp->__pgrp;
  134. tty_pgrp = tty->pgrp;
  135. if (tty_pgrp && (pgrp != tty->pgrp))
  136. {
  137. lwp_signal_kill(lwp, sig, SI_USER, 0);
  138. }
  139. if (!tty_pgrp)
  140. {
  141. LOG_D("sig=%d, tty->pgrp == -1!\n", sig);
  142. }
  143. return ret;
  144. }
  145. int tty_check_change(struct tty_struct *tty)
  146. {
  147. return __tty_check_change(tty, SIGTTOU);
  148. }
  149. static int tty_open(struct dfs_file *fd)
  150. {
  151. int ret = 0;
  152. int noctty = 0;
  153. struct tty_struct *tty = RT_NULL;
  154. struct tty_ldisc *ld = RT_NULL;
  155. tty = (struct tty_struct *)fd->vnode->data;
  156. RT_ASSERT(tty != RT_NULL);
  157. ld = tty->ldisc;
  158. if (ld->ops->open)
  159. {
  160. ret = ld->ops->open(fd);
  161. }
  162. noctty = (fd->flags & O_NOCTTY);
  163. rt_device_t device = (rt_device_t)fd->vnode->data;
  164. if (fd->vnode->ref_count == 1)
  165. {
  166. ret = rt_device_open(device, fd->flags);
  167. }
  168. if (current == RT_NULL) //kernel mode not lwp
  169. {
  170. return ret;
  171. }
  172. if (!noctty &&
  173. current->leader &&
  174. !current->tty &&
  175. tty->session == -1)
  176. {
  177. current->tty = tty;
  178. current->tty_old_pgrp = 0;
  179. tty->session = current->session;
  180. tty->pgrp = current->__pgrp;
  181. tty->foreground = current;
  182. }
  183. return ret;
  184. }
  185. static int tty_close(struct dfs_file *fd)
  186. {
  187. int ret = 0;
  188. struct tty_struct *tty = RT_NULL;
  189. struct tty_ldisc *ld = RT_NULL;
  190. tty = (struct tty_struct *)fd->vnode->data;
  191. RT_ASSERT(tty != RT_NULL);
  192. ld = tty->ldisc;
  193. if (ld->ops->close)
  194. {
  195. //ld->ops->close(tty);
  196. }
  197. if (fd->vnode->ref_count == 1)
  198. {
  199. ret = rt_device_close((rt_device_t)tty);
  200. }
  201. return ret;
  202. }
  203. static int tiocsctty(struct tty_struct *tty, int arg)
  204. {
  205. if (current->leader &&
  206. (current->session == tty->session))
  207. {
  208. return 0;
  209. }
  210. /*
  211. * The process must be a session leader and
  212. * not have a controlling tty already.
  213. */
  214. if (!current->leader || current->tty)
  215. {
  216. return -EPERM;
  217. }
  218. if (tty->session > 0)
  219. {
  220. LOG_E("this tty have control process\n");
  221. }
  222. current->tty = tty;
  223. current->tty_old_pgrp = 0;
  224. tty->session = current->session;
  225. tty->pgrp = current->__pgrp;
  226. tty->foreground = current;
  227. if (tty->type == TTY_DRIVER_TYPE_PTY)
  228. {
  229. tty->other_struct->foreground = current;
  230. }
  231. return 0;
  232. }
  233. static int tiocswinsz(struct tty_struct *tty, struct winsize *p_winsize)
  234. {
  235. rt_kprintf("\x1b[8;%d;%dt", p_winsize->ws_col, p_winsize->ws_row);
  236. return 0;
  237. }
  238. static int tiocgwinsz(struct tty_struct *tty, struct winsize *p_winsize)
  239. {
  240. if(rt_thread_self() != rt_thread_find(FINSH_THREAD_NAME))
  241. {
  242. /* only can be used in tshell thread; otherwise, return default size */
  243. p_winsize->ws_col = 80;
  244. p_winsize->ws_row = 24;
  245. }
  246. else
  247. {
  248. #define _TIO_BUFLEN 20
  249. char _tio_buf[_TIO_BUFLEN];
  250. unsigned char cnt1, cnt2, cnt3, i;
  251. char row_s[4], col_s[4];
  252. char *p;
  253. rt_memset(_tio_buf, 0, _TIO_BUFLEN);
  254. /* send the command to terminal for getting the window size of the terminal */
  255. rt_kprintf("\033[18t");
  256. /* waiting for the response from the terminal */
  257. i = 0;
  258. while(i < _TIO_BUFLEN)
  259. {
  260. _tio_buf[i] = finsh_getchar();
  261. if(_tio_buf[i] != 't')
  262. {
  263. i ++;
  264. }
  265. else
  266. {
  267. break;
  268. }
  269. }
  270. if(i == _TIO_BUFLEN)
  271. {
  272. /* buffer overloaded, and return default size */
  273. p_winsize->ws_col = 80;
  274. p_winsize->ws_row = 24;
  275. return 0;
  276. }
  277. /* interpreting data eg: "\033[8;1;15t" which means row is 1 and col is 15 (unit: size of ONE character) */
  278. rt_memset(row_s,0,4);
  279. rt_memset(col_s,0,4);
  280. cnt1 = 0;
  281. while(cnt1 < _TIO_BUFLEN && _tio_buf[cnt1] != ';')
  282. {
  283. cnt1++;
  284. }
  285. cnt2 = ++cnt1;
  286. while(cnt2 < _TIO_BUFLEN && _tio_buf[cnt2] != ';')
  287. {
  288. cnt2++;
  289. }
  290. p = row_s;
  291. while(cnt1 < cnt2)
  292. {
  293. *p++ = _tio_buf[cnt1++];
  294. }
  295. p = col_s;
  296. cnt2++;
  297. cnt3 = rt_strlen(_tio_buf) - 1;
  298. while(cnt2 < cnt3)
  299. {
  300. *p++ = _tio_buf[cnt2++];
  301. }
  302. /* load the window size date */
  303. p_winsize->ws_col = atoi(col_s);
  304. p_winsize->ws_row = atoi(row_s);
  305. #undef _TIO_BUFLEN
  306. }
  307. p_winsize->ws_xpixel = 0;/* unused */
  308. p_winsize->ws_ypixel = 0;/* unused */
  309. return 0;
  310. }
  311. static int tty_ioctl(struct dfs_file *fd, int cmd, void *args)
  312. {
  313. int ret = 0;
  314. void *p = (void *)args;
  315. struct tty_struct *tty = RT_NULL;
  316. struct tty_struct *real_tty = RT_NULL;
  317. struct tty_ldisc *ld = RT_NULL;
  318. tty = (struct tty_struct *)fd->vnode->data;
  319. RT_ASSERT(tty != RT_NULL);
  320. if (tty->type == TTY_DRIVER_TYPE_PTY && tty->subtype == PTY_TYPE_MASTER)
  321. {
  322. real_tty = tty->other_struct;
  323. }
  324. else
  325. {
  326. real_tty = tty;
  327. }
  328. switch (cmd)
  329. {
  330. case TIOCSCTTY:
  331. return tiocsctty(real_tty, 1);
  332. case TIOCGWINSZ:
  333. return tiocgwinsz(real_tty, p);
  334. case TIOCSWINSZ:
  335. return tiocswinsz(real_tty, p);
  336. }
  337. ld = tty->ldisc;
  338. if (ld->ops->ioctl)
  339. {
  340. ret = ld->ops->ioctl(fd, cmd, args);
  341. }
  342. return ret;
  343. }
  344. #ifdef RT_USING_DFS_V2
  345. static ssize_t tty_read(struct dfs_file *fd, void *buf, size_t count, off_t *pos)
  346. #else
  347. static ssize_t tty_read(struct dfs_file *fd, void *buf, size_t count)
  348. #endif
  349. {
  350. int ret = 0;
  351. struct tty_struct *tty = RT_NULL;
  352. struct tty_ldisc *ld = RT_NULL;
  353. tty = (struct tty_struct *)fd->vnode->data;
  354. RT_ASSERT(tty != RT_NULL);
  355. ld = tty->ldisc;
  356. if (ld && ld->ops->read)
  357. {
  358. ret = ld->ops->read(fd, buf, count);
  359. }
  360. return ret;
  361. }
  362. #ifdef RT_USING_DFS_V2
  363. static ssize_t tty_write(struct dfs_file *fd, const void *buf, size_t count, off_t *pos)
  364. #else
  365. static ssize_t tty_write(struct dfs_file *fd, const void *buf, size_t count )
  366. #endif
  367. {
  368. int ret = 0;
  369. struct tty_struct *tty = RT_NULL;
  370. struct tty_ldisc *ld = RT_NULL;
  371. tty = (struct tty_struct *)fd->vnode->data;
  372. RT_ASSERT(tty != RT_NULL);
  373. ld = tty->ldisc;
  374. if (ld && ld->ops->write)
  375. {
  376. ret = ld->ops->write(fd, buf, count);
  377. }
  378. return ret;
  379. }
  380. static int tty_poll(struct dfs_file *fd, struct rt_pollreq *req)
  381. {
  382. int ret = 0;
  383. struct tty_struct *tty = RT_NULL;
  384. struct tty_ldisc *ld = RT_NULL;
  385. tty = (struct tty_struct *)fd->vnode->data;
  386. RT_ASSERT(tty != RT_NULL);
  387. ld = tty->ldisc;
  388. if (ld->ops->poll)
  389. {
  390. ret = ld->ops->poll(fd, req);
  391. }
  392. return ret;
  393. }
  394. static const struct dfs_file_ops tty_fops =
  395. {
  396. .open = tty_open,
  397. .close = tty_close,
  398. .ioctl = tty_ioctl,
  399. .read = tty_read,
  400. .write = tty_write,
  401. .poll = tty_poll,
  402. };
  403. const struct dfs_file_ops *tty_get_fops(void)
  404. {
  405. return &tty_fops;
  406. }
  407. int tty_init(struct tty_struct *tty, int type, int subtype, struct rt_device *iodev)
  408. {
  409. if (tty)
  410. {
  411. struct tty_node *node = NULL;
  412. node = rt_calloc(1, sizeof(struct tty_node));
  413. if (node)
  414. {
  415. tty->type = type;
  416. tty->subtype = subtype;
  417. tty->io_dev = iodev;
  418. tty->head = node;
  419. tty_initstack(tty->head);
  420. tty->pgrp = -1;
  421. tty->session = -1;
  422. tty->foreground = RT_NULL;
  423. rt_mutex_init(&tty->lock, "ttyLock", RT_IPC_FLAG_PRIO);
  424. rt_wqueue_init(&tty->wait_queue);
  425. rt_spin_lock_init(&tty->spinlock);
  426. tty_ldisc_init(tty);
  427. tty->init_termios = tty_std_termios;
  428. tty->init_flag = TTY_INIT_FLAG_REGED;
  429. }
  430. }
  431. return 0;
  432. }