xterm.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
  3. * Licensed under the GPL
  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. char *title;
  20. int device;
  21. int raw;
  22. struct termios tt;
  23. };
  24. static void *xterm_init(char *str, int device, const struct chan_opts *opts)
  25. {
  26. struct xterm_chan *data;
  27. data = uml_kmalloc(sizeof(*data), UM_GFP_KERNEL);
  28. if (data == NULL)
  29. return NULL;
  30. *data = ((struct xterm_chan) { .pid = -1,
  31. .helper_pid = -1,
  32. .device = device,
  33. .title = opts->xterm_title,
  34. .raw = opts->raw } );
  35. return data;
  36. }
  37. /* Only changed by xterm_setup, which is a setup */
  38. static char *terminal_emulator = "xterm";
  39. static char *title_switch = "-T";
  40. static char *exec_switch = "-e";
  41. static int __init xterm_setup(char *line, int *add)
  42. {
  43. *add = 0;
  44. terminal_emulator = line;
  45. line = strchr(line, ',');
  46. if (line == NULL)
  47. return 0;
  48. *line++ = '\0';
  49. if (*line)
  50. title_switch = line;
  51. line = strchr(line, ',');
  52. if (line == NULL)
  53. return 0;
  54. *line++ = '\0';
  55. if (*line)
  56. exec_switch = line;
  57. return 0;
  58. }
  59. __uml_setup("xterm=", xterm_setup,
  60. "xterm=<terminal emulator>,<title switch>,<exec switch>\n"
  61. " Specifies an alternate terminal emulator to use for the debugger,\n"
  62. " consoles, and serial lines when they are attached to the xterm channel.\n"
  63. " The values are the terminal emulator binary, the switch it uses to set\n"
  64. " its title, and the switch it uses to execute a subprocess,\n"
  65. " respectively. The title switch must have the form '<switch> title',\n"
  66. " not '<switch>=title'. Similarly, the exec switch must have the form\n"
  67. " '<switch> command arg1 arg2 ...'.\n"
  68. " The default values are 'xterm=xterm,-T,-e'. Values for gnome-terminal\n"
  69. " are 'xterm=gnome-terminal,-t,-x'.\n\n"
  70. );
  71. static int xterm_open(int input, int output, int primary, void *d,
  72. char **dev_out)
  73. {
  74. struct xterm_chan *data = d;
  75. int pid, fd, new, err;
  76. char title[256], file[] = "/tmp/xterm-pipeXXXXXX";
  77. char *argv[] = { terminal_emulator, title_switch, title, exec_switch,
  78. OS_LIB_PATH "/uml/port-helper", "-uml-socket",
  79. file, NULL };
  80. if (access(argv[4], X_OK) < 0)
  81. argv[4] = "port-helper";
  82. /*
  83. * Check that DISPLAY is set, this doesn't guarantee the xterm
  84. * will work but w/o it we can be pretty sure it won't.
  85. */
  86. if (getenv("DISPLAY") == NULL) {
  87. printk(UM_KERN_ERR "xterm_open: $DISPLAY not 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. new = xterm_fd(fd, &data->helper_pid);
  132. if (new < 0) {
  133. err = new;
  134. printk(UM_KERN_ERR "xterm_open : os_rcv_fd failed, err = %d\n",
  135. -err);
  136. goto out_kill;
  137. }
  138. err = os_set_fd_block(new, 0);
  139. if (err) {
  140. printk(UM_KERN_ERR "xterm_open : failed to set xterm "
  141. "descriptor non-blocking, err = %d\n", -err);
  142. goto out_close2;
  143. }
  144. CATCH_EINTR(err = tcgetattr(new, &data->tt));
  145. if (err) {
  146. new = err;
  147. goto out_close2;
  148. }
  149. if (data->raw) {
  150. err = raw(new);
  151. if (err) {
  152. new = err;
  153. goto out_close2;
  154. }
  155. }
  156. unlink(file);
  157. data->pid = pid;
  158. *dev_out = NULL;
  159. return new;
  160. out_close2:
  161. close(new);
  162. out_kill:
  163. os_kill_process(pid, 1);
  164. out_close1:
  165. close(fd);
  166. return err;
  167. }
  168. static void xterm_close(int fd, void *d)
  169. {
  170. struct xterm_chan *data = d;
  171. if (data->pid != -1)
  172. os_kill_process(data->pid, 1);
  173. data->pid = -1;
  174. if (data->helper_pid != -1)
  175. os_kill_process(data->helper_pid, 0);
  176. data->helper_pid = -1;
  177. os_close_file(fd);
  178. }
  179. const struct chan_ops xterm_ops = {
  180. .type = "xterm",
  181. .init = xterm_init,
  182. .open = xterm_open,
  183. .close = xterm_close,
  184. .read = generic_read,
  185. .write = generic_write,
  186. .console_write = generic_console_write,
  187. .window_size = generic_window_size,
  188. .free = generic_free,
  189. .winch = 1,
  190. };