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 */
77 int cap_channel_flags(const cap_channel_t *chan);
80 cap_channel_flags(const cap_channel_t *chan)
83 return (chan->cch_flags);
88 channel_nvlist_flags(const cap_channel_t *chan)
93 if ((cap_channel_flags(chan) & CASPER_NO_UNIQ) != 0)
94 flags |= NV_FLAG_NO_UNIQUE;
100 * The functions opens unrestricted communication channel to Casper.
103 cap_channel_t *cap_init(void);
105 static inline cap_channel_t *
110 chan = (cap_channel_t *)malloc(sizeof(*chan));
119 * The functions to communicate with service.
122 cap_channel_t *cap_service_open(const cap_channel_t *chan, const char *name);
123 int cap_service_limit(const cap_channel_t *chan,
124 const char * const *names, size_t nnames);
126 #define cap_service_open(chan, name) (cap_init())
127 #define cap_service_limit(chan, names, nnames) (0)
131 * The function creates cap_channel_t based on the given socket.
134 cap_channel_t *cap_wrap(int sock, int flags);
136 static inline cap_channel_t *
137 cap_wrap(int sock, int flags)
144 chan->cch_flags = flags;
151 * The function returns communication socket and frees cap_channel_t.
154 int cap_unwrap(cap_channel_t *chan, int *flags);
157 cap_unwrap(cap_channel_t *chan)
168 * The function clones the given capability.
171 cap_channel_t *cap_clone(const cap_channel_t *chan);
173 static inline cap_channel_t *
174 cap_clone(const cap_channel_t *chan)
176 cap_channel_t *newchan;
178 newchan = cap_init();
179 if (newchan == NULL) {
183 if (chan->cch_fd == -1) {
184 newchan->cch_fd = -1;
186 newchan->cch_fd = dup(chan->cch_fd);
187 if (newchan->cch_fd < 0) {
192 newchan->cch_flags = chan->cch_flags;
199 * The function closes the given capability.
202 void cap_close(cap_channel_t *chan);
205 cap_close(cap_channel_t *chan)
208 if (chan->cch_fd >= 0) {
216 * The function returns socket descriptor associated with the given
217 * cap_channel_t for use with select(2)/kqueue(2)/etc.
220 int cap_sock(const cap_channel_t *chan);
222 #define cap_sock(chan) (chan->cch_fd)
226 * The function limits the given capability.
227 * It always destroys 'limits' on return.
230 int cap_limit_set(const cap_channel_t *chan, nvlist_t *limits);
232 #define cap_limit_set(chan, limits) (0)
236 * The function returns current limits of the given capability.
239 int cap_limit_get(const cap_channel_t *chan, nvlist_t **limitsp);
242 cap_limit_get(const cap_channel_t *chan __unused, nvlist_t **limitsp)
245 *limitsp = nvlist_create(channel_nvlist_flags(chan));
251 * Function sends nvlist over the given capability.
254 int cap_send_nvlist(const cap_channel_t *chan, const nvlist_t *nvl);
256 #define cap_send_nvlist(chan, nvl) (0)
260 * Function receives nvlist over the given capability.
263 nvlist_t *cap_recv_nvlist(const cap_channel_t *chan);
265 #define cap_recv_nvlist(chan) (nvlist_create(chan->cch_flags))
269 * Function sends the given nvlist, destroys it and receives new nvlist in
270 * response over the given capability.
273 nvlist_t *cap_xfer_nvlist(const cap_channel_t *chan, nvlist_t *nvl);
275 static inline nvlist_t *
276 cap_xfer_nvlist(const cap_channel_t *chan, nvlist_t *nvl)
280 return (nvlist_create(channel_nvlist_flags(chan)));
286 #endif /* !_LIBCASPER_H_ */