1 /* $NetBSD: msg.h,v 1.1 2016/12/05 20:10:10 christos Exp $ */
4 * Copyright (c) 2016 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
37 #define CLOSEFD(fd) do { \
42 } while (/*CONSTCOND*/ 0)
45 msg_open(struct msg_fds *fds)
47 if (pipe(fds->pfd) == -1)
49 if (pipe(fds->cfd) == -1) {
58 msg_close(struct msg_fds *fds)
67 msg_write_child(const char *info, struct msg_fds *fds, void *msg, size_t len)
73 printf("Send %s\n", info);
74 rv = write(fds->pfd[1], msg, len);
75 if (rv != (ssize_t)len)
77 // printf("Wait %s\n", info);
78 rv = read(fds->cfd[0], msg, len);
79 if (rv != (ssize_t)len)
85 msg_write_parent(const char *info, struct msg_fds *fds, void *msg, size_t len)
91 printf("Send %s\n", info);
92 rv = write(fds->cfd[1], msg, len);
93 if (rv != (ssize_t)len)
95 // printf("Wait %s\n", info);
96 rv = read(fds->pfd[0], msg, len);
97 if (rv != (ssize_t)len)
103 msg_read_parent(const char *info, struct msg_fds *fds, void *msg, size_t len)
106 CLOSEFD(fds->pfd[1]);
107 CLOSEFD(fds->cfd[0]);
109 printf("Wait %s\n", info);
110 rv = read(fds->pfd[0], msg, len);
111 if (rv != (ssize_t)len)
113 // printf("Send %s\n", info);
114 rv = write(fds->cfd[1], msg, len);
115 if (rv != (ssize_t)len)
121 msg_read_child(const char *info, struct msg_fds *fds, void *msg, size_t len)
124 CLOSEFD(fds->cfd[1]);
125 CLOSEFD(fds->pfd[0]);
127 printf("Wait %s\n", info);
128 rv = read(fds->cfd[0], msg, len);
129 if (rv != (ssize_t)len)
131 // printf("Send %s\n", info);
132 rv = write(fds->pfd[1], msg, len);
133 if (rv != (ssize_t)len)