port_user.c 4.3 KB

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