Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <unistd.h> |
| 4 | #include <stddef.h> |
| 5 | #include <sched.h> |
| 6 | #include <string.h> |
| 7 | #include <errno.h> |
| 8 | #include <sys/wait.h> |
| 9 | #include <sys/signal.h> |
| 10 | #include "user_util.h" |
| 11 | #include "kern_util.h" |
| 12 | #include "user.h" |
| 13 | #include "net_user.h" |
| 14 | #include "slirp.h" |
Jeff Dike | a3c77c6 | 2005-06-13 15:52:18 -0700 | [diff] [blame] | 15 | #include "slip_common.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include "os.h" |
| 17 | |
| 18 | void slirp_user_init(void *data, void *dev) |
| 19 | { |
| 20 | struct slirp_data *pri = data; |
| 21 | |
| 22 | pri->dev = dev; |
| 23 | } |
| 24 | |
| 25 | struct slirp_pre_exec_data { |
| 26 | int stdin; |
| 27 | int stdout; |
| 28 | }; |
| 29 | |
| 30 | static void slirp_pre_exec(void *arg) |
| 31 | { |
| 32 | struct slirp_pre_exec_data *data = arg; |
| 33 | |
| 34 | if(data->stdin != -1) dup2(data->stdin, 0); |
| 35 | if(data->stdout != -1) dup2(data->stdout, 1); |
| 36 | } |
| 37 | |
| 38 | static int slirp_tramp(char **argv, int fd) |
| 39 | { |
| 40 | struct slirp_pre_exec_data pe_data; |
| 41 | int pid; |
| 42 | |
| 43 | pe_data.stdin = fd; |
| 44 | pe_data.stdout = fd; |
| 45 | pid = run_helper(slirp_pre_exec, &pe_data, argv, NULL); |
| 46 | |
| 47 | return(pid); |
| 48 | } |
| 49 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | static int slirp_open(void *data) |
| 51 | { |
| 52 | struct slirp_data *pri = data; |
Jeff Dike | a3c77c6 | 2005-06-13 15:52:18 -0700 | [diff] [blame] | 53 | int fds[2], pid, err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
Jeff Dike | a3c77c6 | 2005-06-13 15:52:18 -0700 | [diff] [blame] | 55 | err = os_pipe(fds, 1, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | if(err) |
| 57 | return(err); |
| 58 | |
Jeff Dike | a3c77c6 | 2005-06-13 15:52:18 -0700 | [diff] [blame] | 59 | err = slirp_tramp(pri->argw.argv, fds[1]); |
| 60 | if(err < 0){ |
| 61 | printk("slirp_tramp failed - errno = %d\n", -err); |
| 62 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | } |
Jeff Dike | a3c77c6 | 2005-06-13 15:52:18 -0700 | [diff] [blame] | 64 | pid = err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | |
Jeff Dike | a3c77c6 | 2005-06-13 15:52:18 -0700 | [diff] [blame] | 66 | pri->slave = fds[1]; |
| 67 | pri->slip.pos = 0; |
| 68 | pri->slip.esc = 0; |
| 69 | pri->pid = err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | |
Jeff Dike | a3c77c6 | 2005-06-13 15:52:18 -0700 | [diff] [blame] | 71 | return(fds[0]); |
| 72 | out: |
| 73 | os_close_file(fds[0]); |
| 74 | os_close_file(fds[1]); |
| 75 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | static void slirp_close(int fd, void *data) |
| 79 | { |
| 80 | struct slirp_data *pri = data; |
| 81 | int status,err; |
| 82 | |
| 83 | os_close_file(fd); |
| 84 | os_close_file(pri->slave); |
| 85 | |
| 86 | pri->slave = -1; |
| 87 | |
| 88 | if(pri->pid<1) { |
| 89 | printk("slirp_close: no child process to shut down\n"); |
| 90 | return; |
| 91 | } |
| 92 | |
| 93 | #if 0 |
| 94 | if(kill(pri->pid, SIGHUP)<0) { |
| 95 | printk("slirp_close: sending hangup to %d failed (%d)\n", |
| 96 | pri->pid, errno); |
| 97 | } |
| 98 | #endif |
| 99 | |
| 100 | CATCH_EINTR(err = waitpid(pri->pid, &status, WNOHANG)); |
| 101 | if(err < 0) { |
| 102 | printk("slirp_close: waitpid returned %d\n", errno); |
| 103 | return; |
| 104 | } |
| 105 | |
| 106 | if(err == 0) { |
Paolo 'Blaisorblade' Giarrusso | 802e307 | 2006-04-10 22:53:32 -0700 | [diff] [blame] | 107 | printk("slirp_close: process %d has not exited\n", pri->pid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | return; |
| 109 | } |
| 110 | |
| 111 | pri->pid = -1; |
| 112 | } |
| 113 | |
| 114 | int slirp_user_read(int fd, void *buf, int len, struct slirp_data *pri) |
| 115 | { |
Jeff Dike | a3c77c6 | 2005-06-13 15:52:18 -0700 | [diff] [blame] | 116 | return slip_proto_read(fd, buf, len, &pri->slip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | int slirp_user_write(int fd, void *buf, int len, struct slirp_data *pri) |
| 120 | { |
Jeff Dike | a3c77c6 | 2005-06-13 15:52:18 -0700 | [diff] [blame] | 121 | return slip_proto_write(fd, buf, len, &pri->slip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | static int slirp_set_mtu(int mtu, void *data) |
| 125 | { |
| 126 | return(mtu); |
| 127 | } |
| 128 | |
| 129 | struct net_user_info slirp_user_info = { |
| 130 | .init = slirp_user_init, |
| 131 | .open = slirp_open, |
| 132 | .close = slirp_close, |
| 133 | .remove = NULL, |
| 134 | .set_mtu = slirp_set_mtu, |
| 135 | .add_address = NULL, |
| 136 | .delete_address = NULL, |
| 137 | .max_packet = BUF_SIZE |
| 138 | }; |