console.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * Copyright (C) 2020 Daniel Golle <daniel@makrotopia.org>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU Lesser General Public License version 2.1
  6. * as published by the Free Software Foundation
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #ifndef _GNU_SOURCE
  14. #define _GNU_SOURCE
  15. #endif
  16. #include <stdlib.h>
  17. #include <fcntl.h>
  18. #include <libubox/ustream.h>
  19. #include <libubus.h>
  20. #include <signal.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <unistd.h>
  24. #include <fcntl.h>
  25. #include <errno.h>
  26. #include <sys/types.h>
  27. #include <termios.h>
  28. static inline int setup_tios(int fd, struct termios *oldtios)
  29. {
  30. struct termios newtios;
  31. if (!isatty(fd)) {
  32. return -1;
  33. }
  34. /* Get current termios */
  35. if (tcgetattr(fd, oldtios))
  36. return -1;
  37. newtios = *oldtios;
  38. /* We use the same settings that ssh does. */
  39. newtios.c_iflag |= IGNPAR;
  40. newtios.c_iflag &= ~(ISTRIP | INLCR | IGNCR | ICRNL | IXON | IXANY | IXOFF);
  41. newtios.c_lflag &= ~(TOSTOP | ISIG | ICANON | ECHO | ECHOE | ECHOK | ECHONL);
  42. newtios.c_oflag &= ~ONLCR;
  43. newtios.c_oflag |= OPOST;
  44. newtios.c_cc[VMIN] = 1;
  45. newtios.c_cc[VTIME] = 0;
  46. /* Set new attributes */
  47. if (tcsetattr(fd, TCSAFLUSH, &newtios))
  48. return -1;
  49. return 0;
  50. }
  51. #define OPT_ARGS "i:c:"
  52. static struct ustream_fd cufd;
  53. static struct ustream_fd lufd;
  54. static void usage()
  55. {
  56. fprintf(stderr, "ujail-console -c <container> [-i <instance>]\n");
  57. exit(1);
  58. }
  59. static void client_cb(struct ustream *s, int bytes)
  60. {
  61. char *buf;
  62. int len, rv;
  63. do {
  64. buf = ustream_get_read_buf(s, &len);
  65. if (!buf)
  66. break;
  67. rv = ustream_write(&lufd.stream, buf, len, false);
  68. if (rv > 0)
  69. ustream_consume(s, rv);
  70. if (rv <= len)
  71. break;
  72. } while(1);
  73. }
  74. static void local_cb(struct ustream *s, int bytes)
  75. {
  76. char *buf;
  77. int len, rv;
  78. do {
  79. buf = ustream_get_read_buf(s, &len);
  80. if (!buf)
  81. break;
  82. if ((len > 0) && (buf[0] == 2))
  83. uloop_end();
  84. rv = ustream_write(&cufd.stream, buf, len, false);
  85. if (rv > 0)
  86. ustream_consume(s, rv);
  87. if (rv <= len)
  88. break;
  89. } while(1);
  90. }
  91. int main(int argc, char **argv)
  92. {
  93. struct ubus_context *ctx;
  94. uint32_t id;
  95. static struct blob_buf req;
  96. char *container_name = NULL, *instance_name = NULL;
  97. int client_fd, server_fd, tty_fd;
  98. struct termios oldtermios;
  99. int ch;
  100. while ((ch = getopt(argc, argv, OPT_ARGS)) != -1) {
  101. switch (ch) {
  102. case 'i':
  103. instance_name = optarg;
  104. break;
  105. case 'c':
  106. container_name = optarg;
  107. break;
  108. default:
  109. usage();
  110. }
  111. }
  112. if (!container_name)
  113. usage();
  114. ctx = ubus_connect(NULL);
  115. if (!ctx) {
  116. fprintf(stderr, "can't connect to ubus!\n");
  117. return -1;
  118. }
  119. /* open pseudo-terminal pair */
  120. client_fd = posix_openpt(O_RDWR | O_NOCTTY);
  121. if (client_fd < 0) {
  122. fprintf(stderr, "can't create virtual console!\n");
  123. ubus_free(ctx);
  124. return -1;
  125. }
  126. setup_tios(client_fd, &oldtermios);
  127. grantpt(client_fd);
  128. unlockpt(client_fd);
  129. server_fd = open(ptsname(client_fd), O_RDWR | O_NOCTTY);
  130. if (server_fd < 0) {
  131. fprintf(stderr, "can't open virtual console!\n");
  132. close(client_fd);
  133. ubus_free(ctx);
  134. return -1;
  135. }
  136. setup_tios(server_fd, &oldtermios);
  137. tty_fd = open("/dev/tty", O_RDWR);
  138. setup_tios(tty_fd, &oldtermios);
  139. /* register server-side with procd */
  140. blob_buf_init(&req, 0);
  141. blobmsg_add_string(&req, "name", container_name);
  142. if (instance_name)
  143. blobmsg_add_string(&req, "instance", instance_name);
  144. if (ubus_lookup_id(ctx, "container", &id) ||
  145. ubus_invoke_fd(ctx, id, "console_attach", req.head, NULL, NULL, 3000, server_fd)) {
  146. fprintf(stderr, "ubus request failed\n");
  147. close(server_fd);
  148. close(client_fd);
  149. blob_buf_free(&req);
  150. ubus_free(ctx);
  151. return -2;
  152. }
  153. close(server_fd);
  154. blob_buf_free(&req);
  155. ubus_free(ctx);
  156. uloop_init();
  157. /* forward between stdio and client_fd until detach is requested */
  158. lufd.stream.notify_read = local_cb;
  159. ustream_fd_init(&lufd, tty_fd);
  160. cufd.stream.notify_read = client_cb;
  161. /* ToDo: handle remote close and other events */
  162. // cufd.stream.notify_state = client_state_cb;
  163. ustream_fd_init(&cufd, client_fd);
  164. fprintf(stderr, "attaching to jail console. press [CTRL]+[B] to exit.\n");
  165. close(0);
  166. close(1);
  167. close(2);
  168. uloop_run();
  169. tcsetattr(tty_fd, TCSAFLUSH, &oldtermios);
  170. ustream_free(&lufd.stream);
  171. ustream_free(&cufd.stream);
  172. close(client_fd);
  173. return 0;
  174. }