h_exec.c revision 1.6.2.2 1 1.6.2.2 bouyer /* $NetBSD: h_exec.c,v 1.6.2.2 2011/02/17 12:00:54 bouyer Exp $ */
2 1.6.2.2 bouyer
3 1.6.2.2 bouyer /*
4 1.6.2.2 bouyer * Copyright (c) 2011 The NetBSD Foundation, Inc.
5 1.6.2.2 bouyer * All rights reserved.
6 1.6.2.2 bouyer *
7 1.6.2.2 bouyer * Redistribution and use in source and binary forms, with or without
8 1.6.2.2 bouyer * modification, are permitted provided that the following conditions
9 1.6.2.2 bouyer * are met:
10 1.6.2.2 bouyer * 1. Redistributions of source code must retain the above copyright
11 1.6.2.2 bouyer * notice, this list of conditions and the following disclaimer.
12 1.6.2.2 bouyer * 2. Redistributions in binary form must reproduce the above copyright
13 1.6.2.2 bouyer * notice, this list of conditions and the following disclaimer in the
14 1.6.2.2 bouyer * documentation and/or other materials provided with the distribution.
15 1.6.2.2 bouyer *
16 1.6.2.2 bouyer * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17 1.6.2.2 bouyer * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18 1.6.2.2 bouyer * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 1.6.2.2 bouyer * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 1.6.2.2 bouyer * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21 1.6.2.2 bouyer * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 1.6.2.2 bouyer * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 1.6.2.2 bouyer * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 1.6.2.2 bouyer * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 1.6.2.2 bouyer * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 1.6.2.2 bouyer * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 1.6.2.2 bouyer * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 1.6.2.2 bouyer */
29 1.6.2.2 bouyer
30 1.6.2.2 bouyer #include <sys/types.h>
31 1.6.2.2 bouyer #include <sys/socket.h>
32 1.6.2.2 bouyer
33 1.6.2.2 bouyer #include <netinet/in.h>
34 1.6.2.2 bouyer
35 1.6.2.2 bouyer #include <err.h>
36 1.6.2.2 bouyer #include <errno.h>
37 1.6.2.2 bouyer #include <fcntl.h>
38 1.6.2.2 bouyer #include <stdio.h>
39 1.6.2.2 bouyer #include <stdlib.h>
40 1.6.2.2 bouyer #include <string.h>
41 1.6.2.2 bouyer #include <unistd.h>
42 1.6.2.2 bouyer
43 1.6.2.2 bouyer #include <rump/rumpclient.h>
44 1.6.2.2 bouyer #include <rump/rump_syscalls.h>
45 1.6.2.2 bouyer
46 1.6.2.2 bouyer int
47 1.6.2.2 bouyer main(int argc, char *argv[])
48 1.6.2.2 bouyer {
49 1.6.2.2 bouyer struct sockaddr_in sin;
50 1.6.2.2 bouyer socklen_t slen;
51 1.6.2.2 bouyer int s1, s2;
52 1.6.2.2 bouyer char buf[12];
53 1.6.2.2 bouyer char *eargv[4];
54 1.6.2.2 bouyer char *ename;
55 1.6.2.2 bouyer extern char **environ;
56 1.6.2.2 bouyer
57 1.6.2.2 bouyer if (rumpclient_init() == -1)
58 1.6.2.2 bouyer err(1, "init");
59 1.6.2.2 bouyer
60 1.6.2.2 bouyer if (argc > 1) {
61 1.6.2.2 bouyer if (strcmp(argv[1], "_didexec") == 0) {
62 1.6.2.2 bouyer rumpclient_daemon(0, 0); /* detach-me-notnot */
63 1.6.2.2 bouyer s2 = atoi(argv[2]);
64 1.6.2.2 bouyer slen = sizeof(sin);
65 1.6.2.2 bouyer /* see below */
66 1.6.2.2 bouyer rump_sys_accept(s2, (struct sockaddr *)&sin, &slen);
67 1.6.2.2 bouyer }
68 1.6.2.2 bouyer }
69 1.6.2.2 bouyer
70 1.6.2.2 bouyer /* open and listenize two TCP4 suckets */
71 1.6.2.2 bouyer if ((s1 = rump_sys_socket(PF_INET, SOCK_STREAM, 0)) == -1)
72 1.6.2.2 bouyer err(1, "socket 1");
73 1.6.2.2 bouyer if ((s2 = rump_sys_socket(PF_INET, SOCK_STREAM, 0)) == -1)
74 1.6.2.2 bouyer err(1, "socket 2");
75 1.6.2.2 bouyer
76 1.6.2.2 bouyer memset(&sin, 0, sizeof(sin));
77 1.6.2.2 bouyer sin.sin_len = sizeof(sin);
78 1.6.2.2 bouyer sin.sin_family = AF_INET;
79 1.6.2.2 bouyer sin.sin_port = htons(1234);
80 1.6.2.2 bouyer
81 1.6.2.2 bouyer if (rump_sys_bind(s1, (struct sockaddr *)&sin, sizeof(sin)) == -1)
82 1.6.2.2 bouyer err(1, "bind1");
83 1.6.2.2 bouyer sin.sin_port = htons(2345);
84 1.6.2.2 bouyer if (rump_sys_bind(s2, (struct sockaddr *)&sin, sizeof(sin)) == -1)
85 1.6.2.2 bouyer err(1, "bind2");
86 1.6.2.2 bouyer
87 1.6.2.2 bouyer if (rump_sys_listen(s1, 1) == -1)
88 1.6.2.2 bouyer err(1, "listen1");
89 1.6.2.2 bouyer if (rump_sys_listen(s2, 1) == -1)
90 1.6.2.2 bouyer err(1, "listen2");
91 1.6.2.2 bouyer
92 1.6.2.2 bouyer if (argc == 1) {
93 1.6.2.2 bouyer rumpclient_daemon(0, 0);
94 1.6.2.2 bouyer slen = sizeof(sin);
95 1.6.2.2 bouyer /*
96 1.6.2.2 bouyer * "pause()", but conveniently gets rid of this helper
97 1.6.2.2 bouyer * since we were called with RUMPCLIENT_RETRYCONN_DIE set
98 1.6.2.2 bouyer */
99 1.6.2.2 bouyer rump_sys_accept(s2, (struct sockaddr *)&sin, &slen);
100 1.6.2.2 bouyer }
101 1.6.2.2 bouyer
102 1.6.2.2 bouyer if (argc == 3 && strcmp(argv[2], "cloexec1") == 0) {
103 1.6.2.2 bouyer if (rump_sys_fcntl(s1, F_SETFD, FD_CLOEXEC) == -1) {
104 1.6.2.2 bouyer err(1, "cloexec failed");
105 1.6.2.2 bouyer }
106 1.6.2.2 bouyer }
107 1.6.2.2 bouyer
108 1.6.2.2 bouyer sprintf(buf, "%d", s2);
109 1.6.2.2 bouyer
110 1.6.2.2 bouyer if (argc == 3 && strcmp(argv[2], "vfork_please") == 0) {
111 1.6.2.2 bouyer switch (rumpclient_vfork()) {
112 1.6.2.2 bouyer case 0:
113 1.6.2.2 bouyer ename = __UNCONST("fourchette");
114 1.6.2.2 bouyer break;
115 1.6.2.2 bouyer case -1:
116 1.6.2.2 bouyer err(1, "vfork");
117 1.6.2.2 bouyer default:
118 1.6.2.2 bouyer ename = __UNCONST("h_ution");
119 1.6.2.2 bouyer break;
120 1.6.2.2 bouyer }
121 1.6.2.2 bouyer } else {
122 1.6.2.2 bouyer ename = __UNCONST("h_ution");
123 1.6.2.2 bouyer }
124 1.6.2.2 bouyer
125 1.6.2.2 bouyer /* omstart! */
126 1.6.2.2 bouyer eargv[0] = ename;
127 1.6.2.2 bouyer eargv[1] = __UNCONST("_didexec");
128 1.6.2.2 bouyer eargv[2] = buf;
129 1.6.2.2 bouyer eargv[3] = NULL;
130 1.6.2.2 bouyer if (rumpclient_exec(argv[1], __UNCONST(eargv), environ) == -1)
131 1.6.2.2 bouyer err(1, "exec");
132 1.6.2.2 bouyer }
133