blob: c999d187abb9d8a908eda42f342419963273dc35 [file] [log] [blame]
Jeff Dikecd1ae0e2007-10-16 01:27:29 -07001/*
2 * Copyright (C) 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
3 * Licensed under the GPL.
4 */
5
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#include <unistd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <errno.h>
Jeff Dikecd1ae0e2007-10-16 01:27:29 -07008#include <string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <sys/wait.h>
Al Viro37185b32012-10-08 03:27:32 +010010#include <net_user.h>
11#include <os.h>
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070012#include "slirp.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070013
Jeff Dikef34d9d22007-05-06 14:51:04 -070014static int slirp_user_init(void *data, void *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015{
16 struct slirp_data *pri = data;
17
18 pri->dev = dev;
Jeff Dikef34d9d22007-05-06 14:51:04 -070019 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070020}
21
22struct slirp_pre_exec_data {
23 int stdin;
24 int stdout;
25};
26
27static void slirp_pre_exec(void *arg)
28{
29 struct slirp_pre_exec_data *data = arg;
30
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070031 if (data->stdin != -1)
32 dup2(data->stdin, 0);
33 if (data->stdout != -1)
34 dup2(data->stdout, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070035}
36
37static int slirp_tramp(char **argv, int fd)
38{
39 struct slirp_pre_exec_data pe_data;
40 int pid;
41
42 pe_data.stdin = fd;
43 pe_data.stdout = fd;
Jeff Dikec4399012007-07-15 23:38:56 -070044 pid = run_helper(slirp_pre_exec, &pe_data, argv);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070046 return pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047}
48
Linus Torvalds1da177e2005-04-16 15:20:36 -070049static int slirp_open(void *data)
50{
51 struct slirp_data *pri = data;
Jeff Dikea3c77c62005-06-13 15:52:18 -070052 int fds[2], pid, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Jeff Dikea3c77c62005-06-13 15:52:18 -070054 err = os_pipe(fds, 1, 1);
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070055 if (err)
56 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Jeff Dikea3c77c62005-06-13 15:52:18 -070058 err = slirp_tramp(pri->argw.argv, fds[1]);
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070059 if (err < 0) {
60 printk(UM_KERN_ERR "slirp_tramp failed - errno = %d\n", -err);
Jeff Dikea3c77c62005-06-13 15:52:18 -070061 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 }
Jeff Dikea3c77c62005-06-13 15:52:18 -070063 pid = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Jeff Dikea3c77c62005-06-13 15:52:18 -070065 pri->slave = fds[1];
66 pri->slip.pos = 0;
67 pri->slip.esc = 0;
68 pri->pid = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070070 return fds[0];
Jeff Dikea3c77c62005-06-13 15:52:18 -070071out:
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070072 close(fds[0]);
73 close(fds[1]);
Jeff Dikea3c77c62005-06-13 15:52:18 -070074 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075}
76
77static void slirp_close(int fd, void *data)
78{
79 struct slirp_data *pri = data;
Stanislaw Gruszka4dbed852007-12-17 16:19:46 -080080 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070082 close(fd);
83 close(pri->slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85 pri->slave = -1;
86
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070087 if (pri->pid<1) {
88 printk(UM_KERN_ERR "slirp_close: no child process to shut "
89 "down\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 return;
91 }
92
93#if 0
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070094 if (kill(pri->pid, SIGHUP)<0) {
95 printk(UM_KERN_ERR "slirp_close: sending hangup to %d failed "
96 "(%d)\n", pri->pid, errno);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 }
98#endif
Jeff Dike1aa351a2008-02-04 22:31:10 -080099 err = helper_wait(pri->pid);
Stanislaw Gruszka4dbed852007-12-17 16:19:46 -0800100 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103 pri->pid = -1;
104}
105
106int slirp_user_read(int fd, void *buf, int len, struct slirp_data *pri)
107{
Jeff Dikea3c77c62005-06-13 15:52:18 -0700108 return slip_proto_read(fd, buf, len, &pri->slip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109}
110
111int slirp_user_write(int fd, void *buf, int len, struct slirp_data *pri)
112{
Jeff Dikea3c77c62005-06-13 15:52:18 -0700113 return slip_proto_write(fd, buf, len, &pri->slip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114}
115
Jeff Dike5e7672e2006-09-27 01:50:33 -0700116const struct net_user_info slirp_user_info = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 .init = slirp_user_init,
118 .open = slirp_open,
119 .close = slirp_close,
120 .remove = NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 .add_address = NULL,
122 .delete_address = NULL,
Jeff Dikeb53f35a2007-10-16 01:27:31 -0700123 .mtu = BUF_SIZE,
124 .max_packet = BUF_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125};