PageRenderTime 37ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/arch/um/drivers/xterm.c

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