console.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. #include <stdlib.h>
  14. #include <fcntl.h>
  15. #include <libubox/ustream.h>
  16. #include <libubus.h>
  17. #include <signal.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <unistd.h>
  21. #include <fcntl.h>
  22. #include <errno.h>
  23. #include <sys/types.h>
  24. #include <termios.h>
  25. static inline int setup_tios(int fd, struct termios *oldtios)
  26. {
  27. struct termios newtios;
  28. if (!isatty(fd)) {
  29. return -1;
  30. }
  31. /* Get current termios */
  32. if (tcgetattr(fd, oldtios))
  33. return -1;
  34. newtios = *oldtios;
  35. /* Remove the echo characters and signal reception, the echo
  36. * will be done with master proxying */
  37. newtios.c_iflag &= ~IGNBRK;
  38. newtios.c_iflag &= BRKINT;
  39. newtios.c_lflag &= ~(ECHO|ICANON|ISIG);
  40. newtios.c_cc[VMIN] = 1;
  41. newtios.c_cc[VTIME] = 0;
  42. /* Set new attributes */
  43. if (tcsetattr(fd, TCSAFLUSH, &newtios))
  44. return -1;
  45. return 0;
  46. }
  47. #define OPT_ARGS "i:s:"
  48. static struct ustream_fd cufd;
  49. static struct ustream_fd lufd;
  50. static void usage()
  51. {
  52. fprintf(stderr, "ujail-console -s <service> [-i <instance>]\n");
  53. exit(1);
  54. }
  55. static void client_cb(struct ustream *s, int bytes)
  56. {
  57. char *buf;
  58. int len, rv;
  59. do {
  60. buf = ustream_get_read_buf(s, &len);
  61. if (!buf)
  62. break;
  63. rv = ustream_write(&lufd.stream, buf, len, false);
  64. if (rv > 0)
  65. ustream_consume(s, rv);
  66. if (rv <= len)
  67. break;
  68. } while(1);
  69. }
  70. static void local_cb(struct ustream *s, int bytes)
  71. {
  72. char *buf;
  73. int len, rv;
  74. do {
  75. buf = ustream_get_read_buf(s, &len);
  76. if (!buf)
  77. break;
  78. if ((len > 0) && (buf[0] == 2))
  79. uloop_end();
  80. rv = ustream_write(&cufd.stream, buf, len, false);
  81. if (rv > 0)
  82. ustream_consume(s, rv);
  83. if (rv <= len)
  84. break;
  85. } while(1);
  86. }
  87. int main(int argc, char **argv)
  88. {
  89. struct ubus_context *ctx;
  90. uint32_t id;
  91. static struct blob_buf req;
  92. char *service_name = NULL, *instance_name = NULL;
  93. int client_fd, server_fd, tty_fd;
  94. struct termios oldtermios;
  95. int ch;
  96. while ((ch = getopt(argc, argv, OPT_ARGS)) != -1) {
  97. switch (ch) {
  98. case 'i':
  99. instance_name = optarg;
  100. break;
  101. case 's':
  102. service_name = optarg;
  103. break;
  104. default:
  105. usage();
  106. }
  107. }
  108. if (!service_name)
  109. usage();
  110. ctx = ubus_connect(NULL);
  111. if (!ctx) {
  112. fprintf(stderr, "can't connect to ubus!\n");
  113. return -1;
  114. }
  115. /* open pseudo-terminal pair */
  116. client_fd = posix_openpt(O_RDWR | O_NOCTTY);
  117. if (client_fd < 0) {
  118. fprintf(stderr, "can't create virtual console!\n");
  119. ubus_free(ctx);
  120. return -1;
  121. }
  122. setup_tios(client_fd, &oldtermios);
  123. grantpt(client_fd);
  124. unlockpt(client_fd);
  125. server_fd = open(ptsname(client_fd), O_RDWR | O_NOCTTY);
  126. if (server_fd < 0) {
  127. fprintf(stderr, "can't open virtual console!\n");
  128. close(client_fd);
  129. ubus_free(ctx);
  130. return -1;
  131. }
  132. setup_tios(server_fd, &oldtermios);
  133. tty_fd = open("/dev/tty", O_RDWR);
  134. setup_tios(tty_fd, &oldtermios);
  135. /* register server-side with procd */
  136. blob_buf_init(&req, 0);
  137. blobmsg_add_string(&req, "name", service_name);
  138. if (instance_name)
  139. blobmsg_add_string(&req, "instance", instance_name);
  140. if (ubus_lookup_id(ctx, "service", &id) ||
  141. ubus_invoke_fd(ctx, id, "console_attach", req.head, NULL, NULL, 3000, server_fd)) {
  142. fprintf(stderr, "ubus request failed\n");
  143. close(server_fd);
  144. close(client_fd);
  145. blob_buf_free(&req);
  146. ubus_free(ctx);
  147. return -2;
  148. }
  149. close(server_fd);
  150. blob_buf_free(&req);
  151. ubus_free(ctx);
  152. uloop_init();
  153. /* forward between stdio and client_fd until detach is requested */
  154. lufd.stream.notify_read = local_cb;
  155. ustream_fd_init(&lufd, tty_fd);
  156. cufd.stream.notify_read = client_cb;
  157. /* ToDo: handle remote close and other events */
  158. // cufd.stream.notify_state = client_state_cb;
  159. ustream_fd_init(&cufd, client_fd);
  160. fprintf(stderr, "attaching to jail console. press [CTRL]+[B] to exit.\n");
  161. close(0);
  162. close(1);
  163. close(2);
  164. uloop_run();
  165. tcsetattr(tty_fd, TCSAFLUSH, &oldtermios);
  166. ustream_free(&lufd.stream);
  167. ustream_free(&cufd.stream);
  168. close(client_fd);
  169. return 0;
  170. }