xterm.c 5.0 KB

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