slirp_user.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Copyright (C) 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
  3. * Licensed under the GPL.
  4. */
  5. #include <unistd.h>
  6. #include <errno.h>
  7. #include <string.h>
  8. #include <sys/wait.h>
  9. #include <net_user.h>
  10. #include <os.h>
  11. #include "slirp.h"
  12. static int slirp_user_init(void *data, void *dev)
  13. {
  14. struct slirp_data *pri = data;
  15. pri->dev = dev;
  16. return 0;
  17. }
  18. struct slirp_pre_exec_data {
  19. int stdin_fd;
  20. int stdout_fd;
  21. };
  22. static void slirp_pre_exec(void *arg)
  23. {
  24. struct slirp_pre_exec_data *data = arg;
  25. if (data->stdin_fd != -1)
  26. dup2(data->stdin_fd, 0);
  27. if (data->stdout_fd != -1)
  28. dup2(data->stdout_fd, 1);
  29. }
  30. static int slirp_tramp(char **argv, int fd)
  31. {
  32. struct slirp_pre_exec_data pe_data;
  33. int pid;
  34. pe_data.stdin_fd = fd;
  35. pe_data.stdout_fd = fd;
  36. pid = run_helper(slirp_pre_exec, &pe_data, argv);
  37. return pid;
  38. }
  39. static int slirp_open(void *data)
  40. {
  41. struct slirp_data *pri = data;
  42. int fds[2], pid, err;
  43. err = os_pipe(fds, 1, 1);
  44. if (err)
  45. return err;
  46. err = slirp_tramp(pri->argw.argv, fds[1]);
  47. if (err < 0) {
  48. printk(UM_KERN_ERR "slirp_tramp failed - errno = %d\n", -err);
  49. goto out;
  50. }
  51. pid = err;
  52. pri->slave = fds[1];
  53. pri->slip.pos = 0;
  54. pri->slip.esc = 0;
  55. pri->pid = err;
  56. return fds[0];
  57. out:
  58. close(fds[0]);
  59. close(fds[1]);
  60. return err;
  61. }
  62. static void slirp_close(int fd, void *data)
  63. {
  64. struct slirp_data *pri = data;
  65. int err;
  66. close(fd);
  67. close(pri->slave);
  68. pri->slave = -1;
  69. if (pri->pid<1) {
  70. printk(UM_KERN_ERR "slirp_close: no child process to shut "
  71. "down\n");
  72. return;
  73. }
  74. #if 0
  75. if (kill(pri->pid, SIGHUP)<0) {
  76. printk(UM_KERN_ERR "slirp_close: sending hangup to %d failed "
  77. "(%d)\n", pri->pid, errno);
  78. }
  79. #endif
  80. err = helper_wait(pri->pid);
  81. if (err < 0)
  82. return;
  83. pri->pid = -1;
  84. }
  85. int slirp_user_read(int fd, void *buf, int len, struct slirp_data *pri)
  86. {
  87. return slip_proto_read(fd, buf, len, &pri->slip);
  88. }
  89. int slirp_user_write(int fd, void *buf, int len, struct slirp_data *pri)
  90. {
  91. return slip_proto_write(fd, buf, len, &pri->slip);
  92. }
  93. const struct net_user_info slirp_user_info = {
  94. .init = slirp_user_init,
  95. .open = slirp_open,
  96. .close = slirp_close,
  97. .remove = NULL,
  98. .add_address = NULL,
  99. .delete_address = NULL,
  100. .mtu = BUF_SIZE,
  101. .max_packet = BUF_SIZE,
  102. };