t_fd.c revision 314817
1/*	$NetBSD: t_fd.c,v 1.6 2017/01/13 21:30:41 christos Exp $	*/
2
3/*
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#include <sys/types.h>
31#include <sys/stat.h>
32#include <sys/socket.h>
33
34#include <netinet/in.h>
35#include <arpa/inet.h>
36
37#include <atf-c.h>
38#include <errno.h>
39#include <fcntl.h>
40#include <unistd.h>
41
42#include <rump/rumpclient.h>
43#include <rump/rump_syscalls.h>
44
45#include "h_macros.h"
46
47ATF_TC_WITH_CLEANUP(bigenough);
48ATF_TC_HEAD(bigenough, tc)
49{
50	atf_tc_set_md_var(tc, "descr", "Check that rumpclient uses "
51	    "fd > 2");
52}
53ATF_TC_WITH_CLEANUP(sigio);
54ATF_TC_HEAD(sigio, tc)
55{
56	atf_tc_set_md_var(tc, "descr", "Check that rump client receives "
57	    "SIGIO");
58}
59
60#define RUMPSERV "unix://sucket"
61
62ATF_TC_CLEANUP(bigenough, tc){system("env RUMP_SERVER=" RUMPSERV " rump.halt");}
63ATF_TC_CLEANUP(sigio, tc) { system("env RUMP_SERVER=" RUMPSERV " rump.halt"); }
64
65ATF_TC_BODY(bigenough, tc)
66{
67	struct stat sb;
68
69	RZ(system("rump_server " RUMPSERV));
70	RL(setenv("RUMP_SERVER", RUMPSERV, 1));
71
72	RL(dup2(0, 10));
73	RL(dup2(1, 11));
74	RL(dup2(2, 12));
75
76	RL(close(0));
77	RL(close(1));
78	RL(close(2));
79
80	RL(rumpclient_init());
81	RL(rump_sys_getpid());
82
83	ATF_REQUIRE_ERRNO(EBADF, fstat(0, &sb) == -1);
84	ATF_REQUIRE_ERRNO(EBADF, fstat(1, &sb) == -1);
85	ATF_REQUIRE_ERRNO(EBADF, fstat(2, &sb) == -1);
86
87	RL(rump_sys_getpid());
88
89	/* restore these.  does it help? */
90	dup2(10, 0);
91	dup2(11, 1);
92	dup2(12, 2);
93}
94
95static volatile sig_atomic_t sigcnt;
96static void
97gotsig(int sig)
98{
99
100	sigcnt++;
101}
102
103ATF_TC_BODY(sigio, tc)
104{
105	struct sockaddr_in sin;
106	int ls;
107	int cs;
108	int fl;
109	int sc;
110
111	signal(SIGIO, gotsig);
112	RZ(system("rump_server -lrumpnet -lrumpnet_net -lrumpnet_netinet "
113	    "-lrumpdev -lrumpvfs " RUMPSERV));
114	RL(setenv("RUMP_SERVER", RUMPSERV, 1));
115
116	RL(rumpclient_init());
117	RL(ls = rump_sys_socket(PF_INET, SOCK_STREAM, 0));
118
119	RL(rump_sys_fcntl(ls, F_SETOWN, rump_sys_getpid()));
120	RL(fl = rump_sys_fcntl(ls, F_GETFL));
121	RL(rump_sys_fcntl(ls, F_SETFL, fl | O_ASYNC));
122
123	memset(&sin, 0, sizeof(sin));
124	sin.sin_len = sizeof(sin);
125	sin.sin_family = AF_INET;
126	sin.sin_port = htons(12345);
127	RL(rump_sys_bind(ls, (struct sockaddr *)&sin, sizeof(sin)));
128	RL(rump_sys_listen(ls, 5));
129
130	RL(cs = rump_sys_socket(PF_INET, SOCK_STREAM, 0));
131	sin.sin_addr.s_addr = inet_addr("127.0.0.1");
132
133	ATF_REQUIRE_EQ(sigcnt, 0);
134	RL(rump_sys_connect(cs, (struct sockaddr *)&sin, sizeof(sin)));
135	sc = sigcnt;
136	printf("sigcnt after connect: %d\n", sc);
137	ATF_REQUIRE(sc >= 1);
138}
139
140ATF_TP_ADD_TCS(tp)
141{
142	ATF_TP_ADD_TC(tp, bigenough);
143	ATF_TP_ADD_TC(tp, sigio);
144
145	return atf_no_error();
146}
147