xref: /freebsd/contrib/netbsd-tests/kernel/msg.h (revision d370fd1cd8fc69e87dc63f4f4a82e5a8b4956c93)
1*cdebaff8SEnji Cooper /*	$NetBSD: msg.h,v 1.1 2016/12/05 20:10:10 christos Exp $	*/
2*cdebaff8SEnji Cooper 
3*cdebaff8SEnji Cooper /*-
4*cdebaff8SEnji Cooper  * Copyright (c) 2016 The NetBSD Foundation, Inc.
5*cdebaff8SEnji Cooper  * All rights reserved.
6*cdebaff8SEnji Cooper  *
7*cdebaff8SEnji Cooper  * This code is derived from software contributed to The NetBSD Foundation
8*cdebaff8SEnji Cooper  * by Christos Zoulas.
9*cdebaff8SEnji Cooper  *
10*cdebaff8SEnji Cooper  * Redistribution and use in source and binary forms, with or without
11*cdebaff8SEnji Cooper  * modification, are permitted provided that the following conditions
12*cdebaff8SEnji Cooper  * are met:
13*cdebaff8SEnji Cooper  * 1. Redistributions of source code must retain the above copyright
14*cdebaff8SEnji Cooper  *    notice, this list of conditions and the following disclaimer.
15*cdebaff8SEnji Cooper  * 2. Redistributions in binary form must reproduce the above copyright
16*cdebaff8SEnji Cooper  *    notice, this list of conditions and the following disclaimer in the
17*cdebaff8SEnji Cooper  *    documentation and/or other materials provided with the distribution.
18*cdebaff8SEnji Cooper  *
19*cdebaff8SEnji Cooper  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20*cdebaff8SEnji Cooper  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21*cdebaff8SEnji Cooper  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22*cdebaff8SEnji Cooper  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23*cdebaff8SEnji Cooper  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24*cdebaff8SEnji Cooper  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25*cdebaff8SEnji Cooper  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26*cdebaff8SEnji Cooper  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27*cdebaff8SEnji Cooper  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28*cdebaff8SEnji Cooper  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29*cdebaff8SEnji Cooper  * POSSIBILITY OF SUCH DAMAGE.
30*cdebaff8SEnji Cooper  */
31*cdebaff8SEnji Cooper 
32*cdebaff8SEnji Cooper struct msg_fds {
33*cdebaff8SEnji Cooper 	int pfd[2];
34*cdebaff8SEnji Cooper 	int cfd[2];
35*cdebaff8SEnji Cooper };
36*cdebaff8SEnji Cooper 
37*cdebaff8SEnji Cooper #define CLOSEFD(fd) do { \
38*cdebaff8SEnji Cooper 	if (fd != -1) { \
39*cdebaff8SEnji Cooper 		close(fd); \
40*cdebaff8SEnji Cooper 		fd = -1; \
41*cdebaff8SEnji Cooper 	} \
42*cdebaff8SEnji Cooper } while (/*CONSTCOND*/ 0)
43*cdebaff8SEnji Cooper 
44*cdebaff8SEnji Cooper static int
msg_open(struct msg_fds * fds)45*cdebaff8SEnji Cooper msg_open(struct msg_fds *fds)
46*cdebaff8SEnji Cooper {
47*cdebaff8SEnji Cooper 	if (pipe(fds->pfd) == -1)
48*cdebaff8SEnji Cooper 		return -1;
49*cdebaff8SEnji Cooper 	if (pipe(fds->cfd) == -1) {
50*cdebaff8SEnji Cooper 		close(fds->pfd[0]);
51*cdebaff8SEnji Cooper 		close(fds->pfd[1]);
52*cdebaff8SEnji Cooper 		return -1;
53*cdebaff8SEnji Cooper 	}
54*cdebaff8SEnji Cooper 	return 0;
55*cdebaff8SEnji Cooper }
56*cdebaff8SEnji Cooper 
57*cdebaff8SEnji Cooper static void
msg_close(struct msg_fds * fds)58*cdebaff8SEnji Cooper msg_close(struct msg_fds *fds)
59*cdebaff8SEnji Cooper {
60*cdebaff8SEnji Cooper 	CLOSEFD(fds->pfd[0]);
61*cdebaff8SEnji Cooper 	CLOSEFD(fds->pfd[1]);
62*cdebaff8SEnji Cooper 	CLOSEFD(fds->cfd[0]);
63*cdebaff8SEnji Cooper 	CLOSEFD(fds->cfd[1]);
64*cdebaff8SEnji Cooper }
65*cdebaff8SEnji Cooper 
66*cdebaff8SEnji Cooper static int
msg_write_child(const char * info,struct msg_fds * fds,void * msg,size_t len)67*cdebaff8SEnji Cooper msg_write_child(const char *info, struct msg_fds *fds, void *msg, size_t len)
68*cdebaff8SEnji Cooper {
69*cdebaff8SEnji Cooper 	ssize_t rv;
70*cdebaff8SEnji Cooper 	CLOSEFD(fds->cfd[1]);
71*cdebaff8SEnji Cooper 	CLOSEFD(fds->pfd[0]);
72*cdebaff8SEnji Cooper 
73*cdebaff8SEnji Cooper 	printf("Send %s\n", info);
74*cdebaff8SEnji Cooper 	rv = write(fds->pfd[1], msg, len);
75*cdebaff8SEnji Cooper 	if (rv != (ssize_t)len)
76*cdebaff8SEnji Cooper 		return 1;
77*cdebaff8SEnji Cooper //	printf("Wait %s\n", info);
78*cdebaff8SEnji Cooper 	rv = read(fds->cfd[0], msg, len);
79*cdebaff8SEnji Cooper 	if (rv != (ssize_t)len)
80*cdebaff8SEnji Cooper 		return 1;
81*cdebaff8SEnji Cooper 	return 0;
82*cdebaff8SEnji Cooper }
83*cdebaff8SEnji Cooper 
84*cdebaff8SEnji Cooper static int
msg_write_parent(const char * info,struct msg_fds * fds,void * msg,size_t len)85*cdebaff8SEnji Cooper msg_write_parent(const char *info, struct msg_fds *fds, void *msg, size_t len)
86*cdebaff8SEnji Cooper {
87*cdebaff8SEnji Cooper 	ssize_t rv;
88*cdebaff8SEnji Cooper 	CLOSEFD(fds->pfd[1]);
89*cdebaff8SEnji Cooper 	CLOSEFD(fds->cfd[0]);
90*cdebaff8SEnji Cooper 
91*cdebaff8SEnji Cooper 	printf("Send %s\n", info);
92*cdebaff8SEnji Cooper 	rv = write(fds->cfd[1], msg, len);
93*cdebaff8SEnji Cooper 	if (rv != (ssize_t)len)
94*cdebaff8SEnji Cooper 		return 1;
95*cdebaff8SEnji Cooper //	printf("Wait %s\n", info);
96*cdebaff8SEnji Cooper 	rv = read(fds->pfd[0], msg, len);
97*cdebaff8SEnji Cooper 	if (rv != (ssize_t)len)
98*cdebaff8SEnji Cooper 		return 1;
99*cdebaff8SEnji Cooper 	return 0;
100*cdebaff8SEnji Cooper }
101*cdebaff8SEnji Cooper 
102*cdebaff8SEnji Cooper static int
msg_read_parent(const char * info,struct msg_fds * fds,void * msg,size_t len)103*cdebaff8SEnji Cooper msg_read_parent(const char *info, struct msg_fds *fds, void *msg, size_t len)
104*cdebaff8SEnji Cooper {
105*cdebaff8SEnji Cooper 	ssize_t rv;
106*cdebaff8SEnji Cooper 	CLOSEFD(fds->pfd[1]);
107*cdebaff8SEnji Cooper 	CLOSEFD(fds->cfd[0]);
108*cdebaff8SEnji Cooper 
109*cdebaff8SEnji Cooper 	printf("Wait %s\n", info);
110*cdebaff8SEnji Cooper 	rv = read(fds->pfd[0], msg, len);
111*cdebaff8SEnji Cooper 	if (rv != (ssize_t)len)
112*cdebaff8SEnji Cooper 		return 1;
113*cdebaff8SEnji Cooper //	printf("Send %s\n", info);
114*cdebaff8SEnji Cooper 	rv = write(fds->cfd[1], msg, len);
115*cdebaff8SEnji Cooper 	if (rv != (ssize_t)len)
116*cdebaff8SEnji Cooper 		return 1;
117*cdebaff8SEnji Cooper 	return 0;
118*cdebaff8SEnji Cooper }
119*cdebaff8SEnji Cooper 
120*cdebaff8SEnji Cooper static int
msg_read_child(const char * info,struct msg_fds * fds,void * msg,size_t len)121*cdebaff8SEnji Cooper msg_read_child(const char *info, struct msg_fds *fds, void *msg, size_t len)
122*cdebaff8SEnji Cooper {
123*cdebaff8SEnji Cooper 	ssize_t rv;
124*cdebaff8SEnji Cooper 	CLOSEFD(fds->cfd[1]);
125*cdebaff8SEnji Cooper 	CLOSEFD(fds->pfd[0]);
126*cdebaff8SEnji Cooper 
127*cdebaff8SEnji Cooper 	printf("Wait %s\n", info);
128*cdebaff8SEnji Cooper 	rv = read(fds->cfd[0], msg, len);
129*cdebaff8SEnji Cooper 	if (rv != (ssize_t)len)
130*cdebaff8SEnji Cooper 		return 1;
131*cdebaff8SEnji Cooper //	printf("Send %s\n", info);
132*cdebaff8SEnji Cooper 	rv = write(fds->pfd[1], msg, len);
133*cdebaff8SEnji Cooper 	if (rv != (ssize_t)len)
134*cdebaff8SEnji Cooper 		return 1;
135*cdebaff8SEnji Cooper 	return 0;
136*cdebaff8SEnji Cooper }
137