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