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