xterm.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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,-x'.\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. /*
  86. * Check that DISPLAY is set, this doesn't guarantee the xterm
  87. * will work but w/o it we can be pretty sure it won't.
  88. */
  89. if (getenv("DISPLAY") == NULL) {
  90. printk(UM_KERN_ERR "xterm_open: $DISPLAY not set.\n");
  91. return -ENODEV;
  92. }
  93. /*
  94. * This business of getting a descriptor to a temp file,
  95. * deleting the file and closing the descriptor is just to get
  96. * a known-unused name for the Unix socket that we really
  97. * want.
  98. */
  99. fd = mkstemp(file);
  100. if (fd < 0) {
  101. err = -errno;
  102. printk(UM_KERN_ERR "xterm_open : mkstemp failed, errno = %d\n",
  103. errno);
  104. return err;
  105. }
  106. if (unlink(file)) {
  107. err = -errno;
  108. printk(UM_KERN_ERR "xterm_open : unlink failed, errno = %d\n",
  109. errno);
  110. close(fd);
  111. return err;
  112. }
  113. close(fd);
  114. fd = os_create_unix_socket(file, sizeof(file), 1);
  115. if (fd < 0) {
  116. printk(UM_KERN_ERR "xterm_open : create_unix_socket failed, "
  117. "errno = %d\n", -fd);
  118. return fd;
  119. }
  120. sprintf(title, data->title, data->device);
  121. pid = run_helper(NULL, NULL, argv);
  122. if (pid < 0) {
  123. err = pid;
  124. printk(UM_KERN_ERR "xterm_open : run_helper failed, "
  125. "errno = %d\n", -err);
  126. goto out_close1;
  127. }
  128. err = os_set_fd_block(fd, 0);
  129. if (err < 0) {
  130. printk(UM_KERN_ERR "xterm_open : failed to set descriptor "
  131. "non-blocking, err = %d\n", -err);
  132. goto out_kill;
  133. }
  134. data->chan_fd = fd;
  135. new = xterm_fd(fd, &data->helper_pid);
  136. if (new < 0) {
  137. err = new;
  138. printk(UM_KERN_ERR "xterm_open : os_rcv_fd failed, err = %d\n",
  139. -err);
  140. goto out_kill;
  141. }
  142. err = os_set_fd_block(new, 0);
  143. if (err) {
  144. printk(UM_KERN_ERR "xterm_open : failed to set xterm "
  145. "descriptor non-blocking, err = %d\n", -err);
  146. goto out_close2;
  147. }
  148. CATCH_EINTR(err = tcgetattr(new, &data->tt));
  149. if (err) {
  150. new = err;
  151. goto out_close2;
  152. }
  153. if (data->raw) {
  154. err = raw(new);
  155. if (err) {
  156. new = err;
  157. goto out_close2;
  158. }
  159. }
  160. unlink(file);
  161. data->pid = pid;
  162. *dev_out = NULL;
  163. return new;
  164. out_close2:
  165. close(new);
  166. out_kill:
  167. os_kill_process(pid, 1);
  168. out_close1:
  169. close(fd);
  170. return err;
  171. }
  172. static void xterm_close(int fd, void *d)
  173. {
  174. struct xterm_chan *data = d;
  175. if (data->pid != -1)
  176. os_kill_process(data->pid, 1);
  177. data->pid = -1;
  178. if (data->helper_pid != -1)
  179. os_kill_process(data->helper_pid, 0);
  180. data->helper_pid = -1;
  181. if (data->chan_fd != -1)
  182. os_close_file(data->chan_fd);
  183. os_close_file(fd);
  184. }
  185. const struct chan_ops xterm_ops = {
  186. .type = "xterm",
  187. .init = xterm_init,
  188. .open = xterm_open,
  189. .close = xterm_close,
  190. .read = generic_read,
  191. .write = generic_write,
  192. .console_write = generic_console_write,
  193. .window_size = generic_window_size,
  194. .free = generic_free,
  195. .winch = 1,
  196. };