port_user.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. * Copyright (C) 2001 Jeff Dike (jdike@karaya.com)
  3. * Licensed under the GPL
  4. */
  5. #include <stdio.h>
  6. #include <stddef.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <errno.h>
  10. #include <unistd.h>
  11. #include <termios.h>
  12. #include <sys/socket.h>
  13. #include <sys/un.h>
  14. #include <netinet/in.h>
  15. #include "user_util.h"
  16. #include "kern_util.h"
  17. #include "user.h"
  18. #include "chan_user.h"
  19. #include "port.h"
  20. #include "os.h"
  21. struct port_chan {
  22. int raw;
  23. struct termios tt;
  24. void *kernel_data;
  25. char dev[sizeof("32768\0")];
  26. };
  27. static void *port_init(char *str, int device, struct chan_opts *opts)
  28. {
  29. struct port_chan *data;
  30. void *kern_data;
  31. char *end;
  32. int port;
  33. if(*str != ':'){
  34. printk("port_init : channel type 'port' must specify a "
  35. "port number\n");
  36. return(NULL);
  37. }
  38. str++;
  39. port = strtoul(str, &end, 0);
  40. if((*end != '\0') || (end == str)){
  41. printk("port_init : couldn't parse port '%s'\n", str);
  42. return(NULL);
  43. }
  44. kern_data = port_data(port);
  45. if(kern_data == NULL)
  46. return(NULL);
  47. data = um_kmalloc(sizeof(*data));
  48. if(data == NULL)
  49. goto err;
  50. *data = ((struct port_chan) { .raw = opts->raw,
  51. .kernel_data = kern_data });
  52. sprintf(data->dev, "%d", port);
  53. return(data);
  54. err:
  55. port_kern_free(kern_data);
  56. return(NULL);
  57. }
  58. static void port_free(void *d)
  59. {
  60. struct port_chan *data = d;
  61. port_kern_free(data->kernel_data);
  62. kfree(data);
  63. }
  64. static int port_open(int input, int output, int primary, void *d,
  65. char **dev_out)
  66. {
  67. struct port_chan *data = d;
  68. int fd, err;
  69. fd = port_wait(data->kernel_data);
  70. if((fd >= 0) && data->raw){
  71. CATCH_EINTR(err = tcgetattr(fd, &data->tt));
  72. if(err)
  73. return(err);
  74. err = raw(fd);
  75. if(err)
  76. return(err);
  77. }
  78. *dev_out = data->dev;
  79. return(fd);
  80. }
  81. static void port_close(int fd, void *d)
  82. {
  83. struct port_chan *data = d;
  84. port_remove_dev(data->kernel_data);
  85. os_close_file(fd);
  86. }
  87. struct chan_ops port_ops = {
  88. .type = "port",
  89. .init = port_init,
  90. .open = port_open,
  91. .close = port_close,
  92. .read = generic_read,
  93. .write = generic_write,
  94. .console_write = generic_console_write,
  95. .window_size = generic_window_size,
  96. .free = port_free,
  97. .winch = 1,
  98. };
  99. int port_listen_fd(int port)
  100. {
  101. struct sockaddr_in addr;
  102. int fd, err, arg;
  103. fd = socket(PF_INET, SOCK_STREAM, 0);
  104. if(fd == -1)
  105. return(-errno);
  106. arg = 1;
  107. if(setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &arg, sizeof(arg)) < 0){
  108. err = -errno;
  109. goto out;
  110. }
  111. addr.sin_family = AF_INET;
  112. addr.sin_port = htons(port);
  113. addr.sin_addr.s_addr = htonl(INADDR_ANY);
  114. if(bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0){
  115. err = -errno;
  116. goto out;
  117. }
  118. if(listen(fd, 1) < 0){
  119. err = -errno;
  120. goto out;
  121. }
  122. err = os_set_fd_block(fd, 0);
  123. if(err < 0)
  124. goto out;
  125. return(fd);
  126. out:
  127. os_close_file(fd);
  128. return(err);
  129. }
  130. struct port_pre_exec_data {
  131. int sock_fd;
  132. int pipe_fd;
  133. };
  134. void port_pre_exec(void *arg)
  135. {
  136. struct port_pre_exec_data *data = arg;
  137. dup2(data->sock_fd, 0);
  138. dup2(data->sock_fd, 1);
  139. dup2(data->sock_fd, 2);
  140. os_close_file(data->sock_fd);
  141. dup2(data->pipe_fd, 3);
  142. os_shutdown_socket(3, 1, 0);
  143. os_close_file(data->pipe_fd);
  144. }
  145. int port_connection(int fd, int *socket, int *pid_out)
  146. {
  147. int new, err;
  148. char *argv[] = { "/usr/sbin/in.telnetd", "-L",
  149. "/usr/lib/uml/port-helper", NULL };
  150. struct port_pre_exec_data data;
  151. new = os_accept_connection(fd);
  152. if(new < 0)
  153. return(new);
  154. err = os_pipe(socket, 0, 0);
  155. if(err < 0)
  156. goto out_close;
  157. data = ((struct port_pre_exec_data)
  158. { .sock_fd = new,
  159. .pipe_fd = socket[1] });
  160. err = run_helper(port_pre_exec, &data, argv, NULL);
  161. if(err < 0)
  162. goto out_shutdown;
  163. *pid_out = err;
  164. return(new);
  165. out_shutdown:
  166. os_shutdown_socket(socket[0], 1, 1);
  167. os_close_file(socket[0]);
  168. os_shutdown_socket(socket[1], 1, 1);
  169. os_close_file(socket[1]);
  170. out_close:
  171. os_close_file(new);
  172. return(err);
  173. }
  174. /*
  175. * Overrides for Emacs so that we follow Linus's tabbing style.
  176. * Emacs will notice this stuff at the end of the file and automatically
  177. * adjust the settings for this buffer only. This must remain at the end
  178. * of the file.
  179. * ---------------------------------------------------------------------------
  180. * Local variables:
  181. * c-file-style: "linux"
  182. * End:
  183. */