xterm.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 "helper.h"
  17. #include "user_util.h"
  18. #include "user.h"
  19. #include "os.h"
  20. #include "xterm.h"
  21. struct xterm_chan {
  22. int pid;
  23. int helper_pid;
  24. char *title;
  25. int device;
  26. int raw;
  27. struct termios tt;
  28. unsigned long stack;
  29. int direct_rcv;
  30. };
  31. /* Not static because it's called directly by the tt mode gdb code */
  32. void *xterm_init(char *str, int device, struct chan_opts *opts)
  33. {
  34. struct xterm_chan *data;
  35. data = malloc(sizeof(*data));
  36. if(data == NULL) return(NULL);
  37. *data = ((struct xterm_chan) { .pid = -1,
  38. .helper_pid = -1,
  39. .device = device,
  40. .title = opts->xterm_title,
  41. .raw = opts->raw,
  42. .stack = opts->tramp_stack,
  43. .direct_rcv = !opts->in_kernel } );
  44. return(data);
  45. }
  46. /* Only changed by xterm_setup, which is a setup */
  47. static char *terminal_emulator = "xterm";
  48. static char *title_switch = "-T";
  49. static char *exec_switch = "-e";
  50. static int __init xterm_setup(char *line, int *add)
  51. {
  52. *add = 0;
  53. terminal_emulator = line;
  54. line = strchr(line, ',');
  55. if(line == NULL) return(0);
  56. *line++ = '\0';
  57. if(*line) title_switch = line;
  58. line = strchr(line, ',');
  59. if(line == NULL) return(0);
  60. *line++ = '\0';
  61. if(*line) exec_switch = line;
  62. return(0);
  63. }
  64. __uml_setup("xterm=", xterm_setup,
  65. "xterm=<terminal emulator>,<title switch>,<exec switch>\n"
  66. " Specifies an alternate terminal emulator to use for the debugger,\n"
  67. " consoles, and serial lines when they are attached to the xterm channel.\n"
  68. " The values are the terminal emulator binary, the switch it uses to set\n"
  69. " its title, and the switch it uses to execute a subprocess,\n"
  70. " respectively. The title switch must have the form '<switch> title',\n"
  71. " not '<switch>=title'. Similarly, the exec switch must have the form\n"
  72. " '<switch> command arg1 arg2 ...'.\n"
  73. " The default values are 'xterm=xterm,-T,-e'. Values for gnome-terminal\n"
  74. " are 'xterm=gnome-terminal,-t,-x'.\n\n"
  75. );
  76. /* XXX This badly needs some cleaning up in the error paths
  77. * Not static because it's called directly by the tt mode gdb code
  78. */
  79. int xterm_open(int input, int output, int primary, void *d,
  80. char **dev_out)
  81. {
  82. struct xterm_chan *data = d;
  83. unsigned long stack;
  84. int pid, fd, new, err;
  85. char title[256], file[] = "/tmp/xterm-pipeXXXXXX";
  86. char *argv[] = { terminal_emulator, title_switch, title, exec_switch,
  87. "/usr/lib/uml/port-helper", "-uml-socket",
  88. file, NULL };
  89. if(os_access(argv[4], OS_ACC_X_OK) < 0)
  90. argv[4] = "port-helper";
  91. /* Check that DISPLAY is set, this doesn't guarantee the xterm
  92. * will work but w/o it we can be pretty sure it won't. */
  93. if (!getenv("DISPLAY")) {
  94. printk("xterm_open: $DISPLAY not set.\n");
  95. return -ENODEV;
  96. }
  97. fd = mkstemp(file);
  98. if(fd < 0){
  99. printk("xterm_open : mkstemp failed, errno = %d\n", errno);
  100. return(-errno);
  101. }
  102. if(unlink(file)){
  103. printk("xterm_open : unlink failed, errno = %d\n", errno);
  104. return(-errno);
  105. }
  106. os_close_file(fd);
  107. fd = os_create_unix_socket(file, sizeof(file), 1);
  108. if(fd < 0){
  109. printk("xterm_open : create_unix_socket failed, errno = %d\n",
  110. -fd);
  111. return(fd);
  112. }
  113. sprintf(title, data->title, data->device);
  114. stack = data->stack;
  115. pid = run_helper(NULL, NULL, argv, &stack);
  116. if(pid < 0){
  117. printk("xterm_open : run_helper failed, errno = %d\n", -pid);
  118. return(pid);
  119. }
  120. if(data->stack == 0) free_stack(stack, 0);
  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. static int xterm_console_write(int fd, const char *buf, int n, void *d)
  171. {
  172. struct xterm_chan *data = d;
  173. return(generic_console_write(fd, buf, n, &data->tt));
  174. }
  175. struct chan_ops xterm_ops = {
  176. .type = "xterm",
  177. .init = xterm_init,
  178. .open = xterm_open,
  179. .close = xterm_close,
  180. .read = generic_read,
  181. .write = generic_write,
  182. .console_write = xterm_console_write,
  183. .window_size = generic_window_size,
  184. .free = xterm_free,
  185. .winch = 1,
  186. };
  187. /*
  188. * Overrides for Emacs so that we follow Linus's tabbing style.
  189. * Emacs will notice this stuff at the end of the file and automatically
  190. * adjust the settings for this buffer only. This must remain at the end
  191. * of the file.
  192. * ---------------------------------------------------------------------------
  193. * Local variables:
  194. * c-file-style: "linux"
  195. * End:
  196. */