1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
/* $NetBSD: h_forkcli.c,v 1.1 2011/01/05 17:19:09 pooka Exp $ */
#include <sys/types.h>
#include <sys/wait.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <rump/rump_syscalls.h>
#include <rump/rumpclient.h>
static void
simple(void)
{
struct rumpclient_fork *rf;
pid_t pid1, pid2;
int fd, status;
if ((pid1 = rump_sys_getpid()) < 2)
errx(1, "unexpected pid %d", pid1);
fd = rump_sys_open("/dev/null", O_CREAT | O_RDWR);
if (rump_sys_write(fd, &fd, sizeof(fd)) != sizeof(fd))
errx(1, "write newlyopened /dev/null");
if ((rf = rumpclient_prefork()) == NULL)
err(1, "prefork");
switch (fork()) {
case -1:
err(1, "fork");
break;
case 0:
if (rumpclient_fork_init(rf) == -1)
err(1, "postfork init failed");
if ((pid2 = rump_sys_getpid()) < 2)
errx(1, "unexpected pid %d", pid2);
if (pid1 == pid2)
errx(1, "child and parent pids are equal");
/* check that we can access the fd, the close it and exit */
if (rump_sys_write(fd, &fd, sizeof(fd)) != sizeof(fd))
errx(1, "write child /dev/null");
rump_sys_close(fd);
break;
default:
/*
* check that we can access the fd, wait for the child, and
* check we can still access the fd
*/
if (rump_sys_write(fd, &fd, sizeof(fd)) != sizeof(fd))
errx(1, "write parent /dev/null");
if (wait(&status) == -1)
err(1, "wait failed");
if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
errx(1, "child exited with status %d", status);
if (rump_sys_write(fd, &fd, sizeof(fd)) != sizeof(fd))
errx(1, "write parent /dev/null");
break;
}
}
static void
cancel(void)
{
/* XXX: not implemented in client / server !!! */
}
#define TESTSTR "i am your fatherrrrrrr"
#define TESTSLEN (sizeof(TESTSTR)-1)
static void
pipecomm(void)
{
struct rumpclient_fork *rf;
char buf[TESTSLEN+1];
int pipetti[2];
int status;
if (rump_sys_pipe(pipetti) == -1)
errx(1, "pipe");
if ((rf = rumpclient_prefork()) == NULL)
err(1, "prefork");
switch (fork()) {
case -1:
err(1, "fork");
break;
case 0:
if (rumpclient_fork_init(rf) == -1)
err(1, "postfork init failed");
memset(buf, 0, sizeof(buf));
if (rump_sys_read(pipetti[0], buf, TESTSLEN) != TESTSLEN)
err(1, "pipe read");
if (strcmp(TESTSTR, buf) != 0)
errx(1, "teststring doesn't match, got %s", buf);
break;
default:
if (rump_sys_write(pipetti[1], TESTSTR, TESTSLEN) != TESTSLEN)
err(1, "pipe write");
if (wait(&status) == -1)
err(1, "wait failed");
if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
errx(1, "child exited with status %d", status);
break;
}
}
static void
fakeauth(void)
{
struct rumpclient_fork *rf;
uint32_t *auth;
int rv;
if ((rf = rumpclient_prefork()) == NULL)
err(1, "prefork");
/* XXX: we know the internal structure of rf */
auth = (void *)rf;
*(auth+3) = *(auth+3) ^ 0x1;
rv = rumpclient_fork_init(rf);
if (!(rv == -1 && errno == ESRCH))
exit(1);
}
struct parsa {
const char *arg; /* sp arg, el */
void (*spring)(void); /* spring into action */
} paragus[] = {
{ "simple", simple },
{ "cancel", cancel },
{ "pipecomm", pipecomm },
{ "fakeauth", fakeauth },
};
int
main(int argc, char *argv[])
{
unsigned i;
if (argc != 2)
errx(1, "invalid usage");
if (rumpclient_init() == -1)
err(1, "rumpclient init");
for (i = 0; i < __arraycount(paragus); i++) {
if (strcmp(argv[1], paragus[i].arg) == 0) {
paragus[i].spring();
break;
}
}
if (i == __arraycount(paragus)) {
printf("invalid test %s\n", argv[1]);
exit(1);
}
exit(0);
}
|