2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2012-2013 The FreeBSD Foundation
5 * Copyright (c) 2015-2017 Mariusz Zaborski <oshogbo@FreeBSD.org>
8 * This software was developed by Pawel Jakub Dawidek under sponsorship from
9 * the FreeBSD Foundation.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 #include <sys/types.h>
48 #define CASPER_NO_UNIQ 0x00000001
50 #ifndef _NVLIST_T_DECLARED
51 #define _NVLIST_T_DECLARED
54 typedef struct nvlist nvlist_t;
57 #ifndef _CAP_CHANNEL_T_DECLARED
58 #define _CAP_CHANNEL_T_DECLARED
62 typedef struct cap_channel cap_channel_t;
63 #define CASPER_SUPPORT (1)
69 typedef struct cap_channel cap_channel_t;
70 #define CASPER_SUPPORT (0)
71 #endif /* ! WITH_CASPER */
72 #endif /* ! _CAP_CHANNEL_T_DECLARED */
75 int cap_channel_flags(const cap_channel_t *chan);
78 cap_channel_flags(const cap_channel_t *chan)
81 return (chan->cch_flags);
86 channel_nvlist_flags(const cap_channel_t *chan)
91 if ((cap_channel_flags(chan) & CASPER_NO_UNIQ) != 0)
92 flags |= NV_FLAG_NO_UNIQUE;
98 * The functions opens unrestricted communication channel to Casper.
101 cap_channel_t *cap_init(void);
103 static inline cap_channel_t *
108 chan = malloc(sizeof(*chan));
117 * The functions to communicate with service.
120 cap_channel_t *cap_service_open(const cap_channel_t *chan, const char *name);
121 int cap_service_limit(const cap_channel_t *chan,
122 const char * const *names, size_t nnames);
124 #define cap_service_open(chan, name) (cap_init())
125 #define cap_service_limit(chan, names, nnames) (0)
129 * The function creates cap_channel_t based on the given socket.
132 cap_channel_t *cap_wrap(int sock, int flags);
134 static inline cap_channel_t *
135 cap_wrap(int sock, int flags)
142 chan->cch_flags = flags;
149 * The function returns communication socket and frees cap_channel_t.
152 int cap_unwrap(cap_channel_t *chan, int *flags);
155 cap_unwrap(cap_channel_t *chan)
166 * The function clones the given capability.
169 cap_channel_t *cap_clone(const cap_channel_t *chan);
171 static inline cap_channel_t *
172 cap_clone(const cap_channel_t *chan)
174 cap_channel_t *newchan;
176 newchan = cap_init();
177 if (newchan == NULL) {
181 if (chan->cch_fd == -1) {
182 newchan->cch_fd = -1;
184 newchan->cch_fd = dup(chan->cch_fd);
185 if (newchan->cch_fd < 0) {
190 newchan->cch_flags = chan->cch_flags;
197 * The function closes the given capability.
200 void cap_close(cap_channel_t *chan);
203 cap_close(cap_channel_t *chan)
206 if (chan->cch_fd >= 0) {
214 * The function returns socket descriptor associated with the given
215 * cap_channel_t for use with select(2)/kqueue(2)/etc.
218 int cap_sock(const cap_channel_t *chan);
220 #define cap_sock(chan) (chan->cch_fd)
224 * The function limits the given capability.
225 * It always destroys 'limits' on return.
228 int cap_limit_set(const cap_channel_t *chan, nvlist_t *limits);
230 #define cap_limit_set(chan, limits) (0)
234 * The function returns current limits of the given capability.
237 int cap_limit_get(const cap_channel_t *chan, nvlist_t **limitsp);
240 cap_limit_get(const cap_channel_t *chan __unused, nvlist_t **limitsp)
243 *limitsp = nvlist_create(channel_nvlist_flags(chan));
249 * Function sends nvlist over the given capability.
252 int cap_send_nvlist(const cap_channel_t *chan, const nvlist_t *nvl);
254 #define cap_send_nvlist(chan, nvl) (0)
258 * Function receives nvlist over the given capability.
261 nvlist_t *cap_recv_nvlist(const cap_channel_t *chan);
263 #define cap_recv_nvlist(chan) (nvlist_create(chan->cch_flags))
267 * Function sends the given nvlist, destroys it and receives new nvlist in
268 * response over the given capability.
271 nvlist_t *cap_xfer_nvlist(const cap_channel_t *chan, nvlist_t *nvl);
273 static inline nvlist_t *
274 cap_xfer_nvlist(const cap_channel_t *chan, nvlist_t *nvl)
278 return (nvlist_create(channel_nvlist_flags(chan)));
282 #endif /* !_LIBCASPER_H_ */