xterm.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. * Copyright (C) 2001, 2002 Jeff Dike (jdike@karaya.com)
  3. * Licensed under the GPL
  4. */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <unistd.h>
  8. #include <string.h>
  9. #include <errno.h>
  10. #include <termios.h>
  11. #include <signal.h>
  12. #include <sched.h>
  13. #include <sys/socket.h>
  14. #include "kern_util.h"
  15. #include "chan_user.h"
  16. #include "user_util.h"
  17. #include "user.h"
  18. #include "os.h"
  19. #include "xterm.h"
  20. struct xterm_chan {
  21. int pid;
  22. int helper_pid;
  23. char *title;
  24. int device;
  25. int raw;
  26. struct termios tt;
  27. unsigned long stack;
  28. int direct_rcv;
  29. };
  30. /* Not static because it's called directly by the tt mode gdb code */
  31. void *xterm_init(char *str, int device, const struct chan_opts *opts)
  32. {
  33. struct xterm_chan *data;
  34. data = malloc(sizeof(*data));
  35. if(data == NULL) return(NULL);
  36. *data = ((struct xterm_chan) { .pid = -1,
  37. .helper_pid = -1,
  38. .device = device,
  39. .title = opts->xterm_title,
  40. .raw = opts->raw,
  41. .stack = opts->tramp_stack,
  42. .direct_rcv = !opts->in_kernel } );
  43. return(data);
  44. }
  45. /* Only changed by xterm_setup, which is a setup */
  46. static char *terminal_emulator = "xterm";
  47. static char *title_switch = "-T";
  48. static char *exec_switch = "-e";
  49. static int __init xterm_setup(char *line, int *add)
  50. {
  51. *add = 0;
  52. terminal_emulator = line;
  53. line = strchr(line, ',');
  54. if(line == NULL) return(0);
  55. *line++ = '\0';
  56. if(*line) title_switch = line;
  57. line = strchr(line, ',');
  58. if(line == NULL) return(0);
  59. *line++ = '\0';
  60. if(*line) exec_switch = line;
  61. return(0);
  62. }
  63. __uml_setup("xterm=", xterm_setup,
  64. "xterm=<terminal emulator>,<title switch>,<exec switch>\n"
  65. " Specifies an alternate terminal emulator to use for the debugger,\n"
  66. " consoles, and serial lines when they are attached to the xterm channel.\n"
  67. " The values are the terminal emulator binary, the switch it uses to set\n"
  68. " its title, and the switch it uses to execute a subprocess,\n"
  69. " respectively. The title switch must have the form '<switch> title',\n"
  70. " not '<switch>=title'. Similarly, the exec switch must have the form\n"
  71. " '<switch> command arg1 arg2 ...'.\n"
  72. " The default values are 'xterm=xterm,-T,-e'. Values for gnome-terminal\n"
  73. " are 'xterm=gnome-terminal,-t,-x'.\n\n"
  74. );
  75. /* XXX This badly needs some cleaning up in the error paths
  76. * Not static because it's called directly by the tt mode gdb code
  77. */
  78. int xterm_open(int input, int output, int primary, void *d,
  79. char **dev_out)
  80. {
  81. struct xterm_chan *data = d;
  82. unsigned long stack;
  83. int pid, fd, new, err;
  84. char title[256], file[] = "/tmp/xterm-pipeXXXXXX";
  85. char *argv[] = { terminal_emulator, title_switch, title, exec_switch,
  86. "/usr/lib/uml/port-helper", "-uml-socket",
  87. file, NULL };
  88. if(os_access(argv[4], OS_ACC_X_OK) < 0)
  89. argv[4] = "port-helper";
  90. /* Check that DISPLAY is set, this doesn't guarantee the xterm
  91. * will work but w/o it we can be pretty sure it won't. */
  92. if (!getenv("DISPLAY")) {
  93. printk("xterm_open: $DISPLAY not set.\n");
  94. return -ENODEV;
  95. }
  96. fd = mkstemp(file);
  97. if(fd < 0){
  98. err = -errno;
  99. printk("xterm_open : mkstemp failed, errno = %d\n", errno);
  100. return err;
  101. }
  102. if(unlink(file)){
  103. err = -errno;
  104. printk("xterm_open : unlink failed, errno = %d\n", errno);
  105. return err;
  106. }
  107. os_close_file(fd);
  108. fd = os_create_unix_socket(file, sizeof(file), 1);
  109. if(fd < 0){
  110. printk("xterm_open : create_unix_socket failed, errno = %d\n",
  111. -fd);
  112. return(fd);
  113. }
  114. sprintf(title, data->title, data->device);
  115. stack = data->stack;
  116. pid = run_helper(NULL, NULL, argv, &stack);
  117. if(pid < 0){
  118. printk("xterm_open : run_helper failed, errno = %d\n", -pid);
  119. return(pid);
  120. }
  121. if (data->direct_rcv) {
  122. new = os_rcv_fd(fd, &data->helper_pid);
  123. } else {
  124. err = os_set_fd_block(fd, 0);
  125. if(err < 0){
  126. printk("xterm_open : failed to set descriptor "
  127. "non-blocking, err = %d\n", -err);
  128. return(err);
  129. }
  130. new = xterm_fd(fd, &data->helper_pid);
  131. }
  132. if(new < 0){
  133. printk("xterm_open : os_rcv_fd failed, err = %d\n", -new);
  134. goto out;
  135. }
  136. CATCH_EINTR(err = tcgetattr(new, &data->tt));
  137. if(err){
  138. new = err;
  139. goto out;
  140. }
  141. if(data->raw){
  142. err = raw(new);
  143. if(err){
  144. new = err;
  145. goto out;
  146. }
  147. }
  148. data->pid = pid;
  149. *dev_out = NULL;
  150. out:
  151. unlink(file);
  152. return(new);
  153. }
  154. /* Not static because it's called directly by the tt mode gdb code */
  155. void xterm_close(int fd, void *d)
  156. {
  157. struct xterm_chan *data = d;
  158. if(data->pid != -1)
  159. os_kill_process(data->pid, 1);
  160. data->pid = -1;
  161. if(data->helper_pid != -1)
  162. os_kill_process(data->helper_pid, 0);
  163. data->helper_pid = -1;
  164. os_close_file(fd);
  165. }
  166. static void xterm_free(void *d)
  167. {
  168. free(d);
  169. }
  170. const struct chan_ops xterm_ops = {
  171. .type = "xterm",
  172. .init = xterm_init,
  173. .open = xterm_open,
  174. .close = xterm_close,
  175. .read = generic_read,
  176. .write = generic_write,
  177. .console_write = generic_console_write,
  178. .window_size = generic_window_size,
  179. .free = xterm_free,
  180. .winch = 1,
  181. };
  182. /*
  183. * Overrides for Emacs so that we follow Linus's tabbing style.
  184. * Emacs will notice this stuff at the end of the file and automatically
  185. * adjust the settings for this buffer only. This must remain at the end
  186. * of the file.
  187. * ---------------------------------------------------------------------------
  188. * Local variables:
  189. * c-file-style: "linux"
  190. * End:
  191. */