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