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