port_user.c 3.6 KB

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