xterm.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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, 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->stack == 0) free_stack(stack, 0);
  122. if (data->direct_rcv) {
  123. new = os_rcv_fd(fd, &data->helper_pid);
  124. } else {
  125. err = os_set_fd_block(fd, 0);
  126. if(err < 0){
  127. printk("xterm_open : failed to set descriptor "
  128. "non-blocking, err = %d\n", -err);
  129. return(err);
  130. }
  131. new = xterm_fd(fd, &data->helper_pid);
  132. }
  133. if(new < 0){
  134. printk("xterm_open : os_rcv_fd failed, err = %d\n", -new);
  135. goto out;
  136. }
  137. CATCH_EINTR(err = tcgetattr(new, &data->tt));
  138. if(err){
  139. new = err;
  140. goto out;
  141. }
  142. if(data->raw){
  143. err = raw(new);
  144. if(err){
  145. new = err;
  146. goto out;
  147. }
  148. }
  149. data->pid = pid;
  150. *dev_out = NULL;
  151. out:
  152. unlink(file);
  153. return(new);
  154. }
  155. /* Not static because it's called directly by the tt mode gdb code */
  156. void xterm_close(int fd, void *d)
  157. {
  158. struct xterm_chan *data = d;
  159. if(data->pid != -1)
  160. os_kill_process(data->pid, 1);
  161. data->pid = -1;
  162. if(data->helper_pid != -1)
  163. os_kill_process(data->helper_pid, 0);
  164. data->helper_pid = -1;
  165. os_close_file(fd);
  166. }
  167. static void xterm_free(void *d)
  168. {
  169. free(d);
  170. }
  171. struct chan_ops xterm_ops = {
  172. .type = "xterm",
  173. .init = xterm_init,
  174. .open = xterm_open,
  175. .close = xterm_close,
  176. .read = generic_read,
  177. .write = generic_write,
  178. .console_write = generic_console_write,
  179. .window_size = generic_window_size,
  180. .free = xterm_free,
  181. .winch = 1,
  182. };
  183. /*
  184. * Overrides for Emacs so that we follow Linus's tabbing style.
  185. * Emacs will notice this stuff at the end of the file and automatically
  186. * adjust the settings for this buffer only. This must remain at the end
  187. * of the file.
  188. * ---------------------------------------------------------------------------
  189. * Local variables:
  190. * c-file-style: "linux"
  191. * End:
  192. */