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