t_fd.c revision 1.5 1 1.5 kre /* $NetBSD: t_fd.c,v 1.5 2016/08/10 21:10:18 kre 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/stat.h>
32 1.2 pooka #include <sys/socket.h>
33 1.2 pooka
34 1.2 pooka #include <netinet/in.h>
35 1.2 pooka #include <arpa/inet.h>
36 1.1 pooka
37 1.1 pooka #include <atf-c.h>
38 1.1 pooka #include <errno.h>
39 1.2 pooka #include <fcntl.h>
40 1.1 pooka #include <unistd.h>
41 1.1 pooka
42 1.1 pooka #include <rump/rumpclient.h>
43 1.1 pooka #include <rump/rump_syscalls.h>
44 1.1 pooka
45 1.1 pooka #include "../../h_macros.h"
46 1.1 pooka
47 1.1 pooka ATF_TC_WITH_CLEANUP(bigenough);
48 1.1 pooka ATF_TC_HEAD(bigenough, tc)
49 1.1 pooka {
50 1.1 pooka atf_tc_set_md_var(tc, "descr", "Check that rumpclient uses "
51 1.1 pooka "fd > 2");
52 1.1 pooka }
53 1.2 pooka ATF_TC_WITH_CLEANUP(sigio);
54 1.2 pooka ATF_TC_HEAD(sigio, tc)
55 1.2 pooka {
56 1.2 pooka atf_tc_set_md_var(tc, "descr", "Check that rump client receives "
57 1.2 pooka "SIGIO");
58 1.2 pooka }
59 1.1 pooka
60 1.1 pooka #define RUMPSERV "unix://sucket"
61 1.1 pooka
62 1.2 pooka ATF_TC_CLEANUP(bigenough, tc){system("env RUMP_SERVER=" RUMPSERV " rump.halt");}
63 1.2 pooka ATF_TC_CLEANUP(sigio, tc) { system("env RUMP_SERVER=" RUMPSERV " rump.halt"); }
64 1.2 pooka
65 1.1 pooka ATF_TC_BODY(bigenough, tc)
66 1.1 pooka {
67 1.1 pooka struct stat sb;
68 1.1 pooka
69 1.1 pooka RZ(system("rump_server " RUMPSERV));
70 1.1 pooka RL(setenv("RUMP_SERVER", RUMPSERV, 1));
71 1.1 pooka
72 1.1 pooka RL(dup2(0, 10));
73 1.1 pooka RL(dup2(1, 11));
74 1.1 pooka RL(dup2(2, 12));
75 1.1 pooka
76 1.1 pooka RL(close(0));
77 1.1 pooka RL(close(1));
78 1.1 pooka RL(close(2));
79 1.1 pooka
80 1.1 pooka RL(rumpclient_init());
81 1.1 pooka RL(rump_sys_getpid());
82 1.1 pooka
83 1.1 pooka ATF_REQUIRE_ERRNO(EBADF, fstat(0, &sb) == -1);
84 1.1 pooka ATF_REQUIRE_ERRNO(EBADF, fstat(1, &sb) == -1);
85 1.1 pooka ATF_REQUIRE_ERRNO(EBADF, fstat(2, &sb) == -1);
86 1.1 pooka
87 1.1 pooka RL(rump_sys_getpid());
88 1.1 pooka
89 1.1 pooka /* restore these. does it help? */
90 1.1 pooka dup2(10, 0);
91 1.1 pooka dup2(11, 1);
92 1.1 pooka dup2(12, 2);
93 1.1 pooka }
94 1.1 pooka
95 1.2 pooka static volatile sig_atomic_t sigcnt;
96 1.2 pooka static void
97 1.2 pooka gotsig(int sig)
98 1.1 pooka {
99 1.1 pooka
100 1.2 pooka sigcnt++;
101 1.2 pooka }
102 1.2 pooka
103 1.2 pooka ATF_TC_BODY(sigio, tc)
104 1.2 pooka {
105 1.2 pooka struct sockaddr_in sin;
106 1.2 pooka int ls;
107 1.2 pooka int cs;
108 1.2 pooka int fl;
109 1.3 gson int sc;
110 1.2 pooka
111 1.2 pooka signal(SIGIO, gotsig);
112 1.2 pooka RZ(system("rump_server -lrumpnet -lrumpnet_net -lrumpnet_netinet "
113 1.5 kre "-lrumpdev -lrumpvfs " RUMPSERV));
114 1.2 pooka RL(setenv("RUMP_SERVER", RUMPSERV, 1));
115 1.2 pooka
116 1.2 pooka RL(rumpclient_init());
117 1.2 pooka RL(ls = rump_sys_socket(PF_INET, SOCK_STREAM, 0));
118 1.2 pooka
119 1.2 pooka RL(rump_sys_fcntl(ls, F_SETOWN, rump_sys_getpid()));
120 1.2 pooka RL(fl = rump_sys_fcntl(ls, F_GETFL));
121 1.2 pooka RL(rump_sys_fcntl(ls, F_SETFL, fl | O_ASYNC));
122 1.2 pooka
123 1.2 pooka memset(&sin, 0, sizeof(sin));
124 1.2 pooka sin.sin_len = sizeof(sin);
125 1.2 pooka sin.sin_family = AF_INET;
126 1.2 pooka sin.sin_port = htons(12345);
127 1.2 pooka RL(rump_sys_bind(ls, (struct sockaddr *)&sin, sizeof(sin)));
128 1.2 pooka RL(rump_sys_listen(ls, 5));
129 1.2 pooka
130 1.2 pooka RL(cs = rump_sys_socket(PF_INET, SOCK_STREAM, 0));
131 1.2 pooka sin.sin_addr.s_addr = inet_addr("127.0.0.1");
132 1.2 pooka
133 1.2 pooka ATF_REQUIRE_EQ(sigcnt, 0);
134 1.2 pooka RL(rump_sys_connect(cs, (struct sockaddr *)&sin, sizeof(sin)));
135 1.3 gson sc = sigcnt;
136 1.3 gson printf("sigcnt after connect: %d\n", sc);
137 1.4 hannken ATF_REQUIRE(sc >= 1);
138 1.1 pooka }
139 1.1 pooka
140 1.1 pooka ATF_TP_ADD_TCS(tp)
141 1.1 pooka {
142 1.1 pooka ATF_TP_ADD_TC(tp, bigenough);
143 1.2 pooka ATF_TP_ADD_TC(tp, sigio);
144 1.1 pooka
145 1.1 pooka return atf_no_error();
146 1.1 pooka }
147