xterm.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
  4. */
  5. #include <stddef.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <unistd.h>
  9. #include <errno.h>
  10. #include <string.h>
  11. #include <termios.h>
  12. #include "chan_user.h"
  13. #include <os.h>
  14. #include <um_malloc.h>
  15. #include "xterm.h"
  16. struct xterm_chan {
  17. int pid;
  18. int helper_pid;
  19. int chan_fd;
  20. char *title;
  21. int device;
  22. int raw;
  23. struct termios tt;
  24. };
  25. static void *xterm_init(char *str, int device, const struct chan_opts *opts)
  26. {
  27. struct xterm_chan *data;
  28. data = uml_kmalloc(sizeof(*data), UM_GFP_KERNEL);
  29. if (data == NULL)
  30. return NULL;
  31. *data = ((struct xterm_chan) { .pid = -1,
  32. .helper_pid = -1,
  33. .chan_fd = -1,
  34. .device = device,
  35. .title = opts->xterm_title,
  36. .raw = opts->raw } );
  37. return data;
  38. }
  39. /* Only changed by xterm_setup, which is a setup */
  40. static char *terminal_emulator = CONFIG_XTERM_CHAN_DEFAULT_EMULATOR;
  41. static char *title_switch = "-T";
  42. static char *exec_switch = "-e";
  43. static int __init xterm_setup(char *line, int *add)
  44. {
  45. *add = 0;
  46. terminal_emulator = line;
  47. line = strchr(line, ',');
  48. if (line == NULL)
  49. return 0;
  50. *line++ = '\0';
  51. if (*line)
  52. title_switch = line;
  53. line = strchr(line, ',');
  54. if (line == NULL)
  55. return 0;
  56. *line++ = '\0';
  57. if (*line)
  58. exec_switch = line;
  59. return 0;
  60. }
  61. __uml_setup("xterm=", xterm_setup,
  62. "xterm=<terminal emulator>,<title switch>,<exec switch>\n"
  63. " Specifies an alternate terminal emulator to use for the debugger,\n"
  64. " consoles, and serial lines when they are attached to the xterm channel.\n"
  65. " The values are the terminal emulator binary, the switch it uses to set\n"
  66. " its title, and the switch it uses to execute a subprocess,\n"
  67. " respectively. The title switch must have the form '<switch> title',\n"
  68. " not '<switch>=title'. Similarly, the exec switch must have the form\n"
  69. " '<switch> command arg1 arg2 ...'.\n"
  70. " The default values are 'xterm=" CONFIG_XTERM_CHAN_DEFAULT_EMULATOR
  71. ",-T,-e'.\n"
  72. " Values for gnome-terminal are 'xterm=gnome-terminal,-t,--'.\n\n"
  73. );
  74. static int xterm_open(int input, int output, int primary, void *d,
  75. char **dev_out)
  76. {
  77. struct xterm_chan *data = d;
  78. int pid, fd, new, err;
  79. char title[256], file[] = "/tmp/xterm-pipeXXXXXX";
  80. char *argv[] = { terminal_emulator, title_switch, title, exec_switch,
  81. OS_LIB_PATH "/uml/port-helper", "-uml-socket",
  82. file, NULL };
  83. if (access(argv[4], X_OK) < 0)
  84. argv[4] = "port-helper";
  85. /* Ensure we are running on Xorg or Wayland. */
  86. if (!getenv("DISPLAY") && !getenv("WAYLAND_DISPLAY")) {
  87. printk(UM_KERN_ERR "xterm_open : neither $DISPLAY nor $WAYLAND_DISPLAY is set.\n");
  88. return -ENODEV;
  89. }
  90. /*
  91. * This business of getting a descriptor to a temp file,
  92. * deleting the file and closing the descriptor is just to get
  93. * a known-unused name for the Unix socket that we really
  94. * want.
  95. */
  96. fd = mkstemp(file);
  97. if (fd < 0) {
  98. err = -errno;
  99. printk(UM_KERN_ERR "xterm_open : mkstemp failed, errno = %d\n",
  100. errno);
  101. return err;
  102. }
  103. if (unlink(file)) {
  104. err = -errno;
  105. printk(UM_KERN_ERR "xterm_open : unlink failed, errno = %d\n",
  106. errno);
  107. close(fd);
  108. return err;
  109. }
  110. close(fd);
  111. fd = os_create_unix_socket(file, sizeof(file), 1);
  112. if (fd < 0) {
  113. printk(UM_KERN_ERR "xterm_open : create_unix_socket failed, "
  114. "errno = %d\n", -fd);
  115. return fd;
  116. }
  117. sprintf(title, data->title, data->device);
  118. pid = run_helper(NULL, NULL, argv);
  119. if (pid < 0) {
  120. err = pid;
  121. printk(UM_KERN_ERR "xterm_open : run_helper failed, "
  122. "errno = %d\n", -err);
  123. goto out_close1;
  124. }
  125. err = os_set_fd_block(fd, 0);
  126. if (err < 0) {
  127. printk(UM_KERN_ERR "xterm_open : failed to set descriptor "
  128. "non-blocking, err = %d\n", -err);
  129. goto out_kill;
  130. }
  131. data->chan_fd = fd;
  132. new = xterm_fd(fd, &data->helper_pid);
  133. if (new < 0) {
  134. err = new;
  135. printk(UM_KERN_ERR "xterm_open : xterm_fd failed, err = %d\n",
  136. -err);
  137. goto out_kill;
  138. }
  139. err = os_set_fd_block(new, 0);
  140. if (err) {
  141. printk(UM_KERN_ERR "xterm_open : failed to set xterm "
  142. "descriptor non-blocking, err = %d\n", -err);
  143. goto out_close2;
  144. }
  145. CATCH_EINTR(err = tcgetattr(new, &data->tt));
  146. if (err) {
  147. new = err;
  148. goto out_close2;
  149. }
  150. if (data->raw) {
  151. err = raw(new);
  152. if (err) {
  153. new = err;
  154. goto out_close2;
  155. }
  156. }
  157. unlink(file);
  158. data->pid = pid;
  159. *dev_out = NULL;
  160. return new;
  161. out_close2:
  162. close(new);
  163. out_kill:
  164. os_kill_process(pid, 1);
  165. out_close1:
  166. close(fd);
  167. return err;
  168. }
  169. static void xterm_close(int fd, void *d)
  170. {
  171. struct xterm_chan *data = d;
  172. if (data->pid != -1)
  173. os_kill_process(data->pid, 1);
  174. data->pid = -1;
  175. if (data->helper_pid != -1)
  176. os_kill_process(data->helper_pid, 0);
  177. data->helper_pid = -1;
  178. if (data->chan_fd != -1)
  179. os_close_file(data->chan_fd);
  180. os_close_file(fd);
  181. }
  182. const struct chan_ops xterm_ops = {
  183. .type = "xterm",
  184. .init = xterm_init,
  185. .open = xterm_open,
  186. .close = xterm_close,
  187. .read = generic_read,
  188. .write = generic_write,
  189. .console_write = generic_console_write,
  190. .window_size = generic_window_size,
  191. .free = generic_free,
  192. .winch = 1,
  193. };