2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright 2021 Lutz Donnerhacke
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials provided
15 * with the distribution.
16 * 3. Neither the name of the copyright holder nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
21 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
22 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
27 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
29 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
30 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
31 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 #include <sys/select.h>
40 #include <sys/queue.h>
45 static int cs = -1, ds = -1;
46 static ng_error_t error_handling = FAIL;
48 #define CHECK(r, x) do { \
50 if (error_handling == PASS) \
52 atf_tc_fail_requirement(file, line, "%s (%s)", \
53 #x " not met", strerror(errno));\
60 ng_data_handler_t handler;
61 SLIST_ENTRY(data_handler) next;
63 static SLIST_HEAD(, data_handler) data_head = SLIST_HEAD_INITIALIZER(data_head);
64 static ng_msg_handler_t msg_handler = NULL;
66 static void handle_data(void *ctx);
67 static void handle_msg(void *ctx);
70 _ng_connect(char const *path1, char const *hook1,
71 char const *path2, char const *hook2,
72 char const *file, size_t line)
76 strncpy(c.ourhook, hook1, sizeof(c.ourhook));
77 strncpy(c.peerhook, hook2, sizeof(c.peerhook));
78 strncpy(c.path, path2, sizeof(c.path));
80 CHECK(, -1 != NgSendMsg(cs, path1,
81 NGM_GENERIC_COOKIE, NGM_CONNECT,
86 _ng_mkpeer(char const *path1, char const *hook1,
87 char const *type, char const *hook2,
88 char const *file, size_t line)
92 strncpy(p.ourhook, hook1, sizeof(p.ourhook));
93 strncpy(p.peerhook, hook2, sizeof(p.peerhook));
94 strncpy(p.type, type, sizeof(p.type));
96 CHECK(, -1 != NgSendMsg(cs, path1,
97 NGM_GENERIC_COOKIE, NGM_MKPEER,
102 _ng_rmhook(char const *path, char const *hook,
103 char const *file, size_t line)
107 strncpy(h.ourhook, hook, sizeof(h.ourhook));
109 CHECK(, -1 != NgSendMsg(cs, path,
110 NGM_GENERIC_COOKIE, NGM_RMHOOK,
115 _ng_name(char const *path, char const *name,
116 char const *file, size_t line)
120 strncpy(n.name, name, sizeof(n.name));
122 CHECK(, -1 != NgSendMsg(cs, path,
123 NGM_GENERIC_COOKIE, NGM_NAME,
128 _ng_shutdown(char const *path,
129 char const *file, size_t line)
131 CHECK(, -1 != NgSendMsg(cs, path,
132 NGM_GENERIC_COOKIE, NGM_SHUTDOWN,
137 ng_register_data(char const *hook, ng_data_handler_t proc)
139 struct data_handler *p;
141 ATF_REQUIRE(NULL != (p = calloc(1, sizeof(struct data_handler))));
142 ATF_REQUIRE(NULL != (p->hook = strdup(hook)));
143 ATF_REQUIRE(NULL != (p->handler = proc));
144 SLIST_INSERT_HEAD(&data_head, p, next);
148 _ng_send_data(char const *hook,
149 void const *data, size_t len,
150 char const *file, size_t line)
152 CHECK(, -1 != NgSendData(ds, hook, data, len));
156 ng_register_msg(ng_msg_handler_t proc)
162 handle_msg(void *ctx)
165 char path[NG_PATHSIZ];
167 ATF_REQUIRE(-1 != NgAllocRecvMsg(cs, &m, path));
169 if (msg_handler != NULL)
170 (*msg_handler) (path, m, ctx);
176 handle_data(void *ctx)
178 char hook[NG_HOOKSIZ];
179 struct data_handler *hnd;
183 ATF_REQUIRE(0 < (len = NgAllocRecvData(ds, &data, hook)));
184 SLIST_FOREACH(hnd, &data_head, next)
186 if (0 == strcmp(hnd->hook, hook))
191 (*(hnd->handler)) (data, len, ctx);
197 ng_handle_event(unsigned int ms, void *context)
200 int maxfd = (ds < cs) ? cs : ds;
201 struct timeval timeout = {0, ms * 1000lu};
207 switch (select(maxfd + 1, &fds, NULL, NULL, &timeout))
210 ATF_REQUIRE_ERRNO(EINTR, 1);
212 case 0: /* timeout */
214 default: /* something to do */
215 if (FD_ISSET(cs, &fds))
217 if (FD_ISSET(ds, &fds))
218 handle_data(context);
224 ng_handle_events(unsigned int ms, void *context)
226 while (ng_handle_event(ms, context))
231 _ng_send_msg(char const *path, char const *msg,
232 char const *file, size_t line)
236 CHECK(-1, -1 != (res = NgSendAsciiMsg(cs, path, "%s", msg)));
241 ng_errors(ng_error_t n)
243 ng_error_t o = error_handling;
250 _ng_init(char const *file, size_t line)
252 if (cs >= 0) /* prevent reinit */
255 CHECK(, 0 == NgMkSockNode(NULL, &cs, &ds));
260 get_data##x(void *data, size_t len, void *ctx) {\