xterm.c 4.9 KB

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