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