]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libcasper/libcasper/libcasper.h
MFV r333668:
[FreeBSD/FreeBSD.git] / lib / libcasper / libcasper / libcasper.h
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2012-2013 The FreeBSD Foundation
5  * Copyright (c) 2015-2017 Mariusz Zaborski <oshogbo@FreeBSD.org>
6  * All rights reserved.
7  *
8  * This software was developed by Pawel Jakub Dawidek under sponsorship from
9  * the FreeBSD Foundation.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
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.
19  *
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
30  * SUCH DAMAGE.
31  *
32  * $FreeBSD$
33  */
34
35 #ifndef _LIBCASPER_H_
36 #define _LIBCASPER_H_
37
38 #ifdef HAVE_CASPER
39 #define WITH_CASPER
40 #endif
41
42 #include <sys/types.h>
43 #include <sys/nv.h>
44
45 #include <stdlib.h>
46 #include <unistd.h>
47
48 #define CASPER_NO_UNIQ  0x00000001
49
50 #ifndef _NVLIST_T_DECLARED
51 #define _NVLIST_T_DECLARED
52 struct nvlist;
53
54 typedef struct nvlist nvlist_t;
55 #endif
56
57 #ifndef _CAP_CHANNEL_T_DECLARED
58 #define _CAP_CHANNEL_T_DECLARED
59 #ifdef WITH_CASPER
60 struct cap_channel;
61
62 typedef struct cap_channel cap_channel_t;
63 #define CASPER_SUPPORT  (1)
64 #else
65 struct cap_channel {
66         int cch_fd;
67         int cch_flags;
68 };
69 typedef struct cap_channel cap_channel_t;
70 #define CASPER_SUPPORT  (0)
71 #endif /* ! WITH_CASPER */
72 #endif /* ! _CAP_CHANNEL_T_DECLARED */
73
74 #ifdef WITH_CASPER
75 int cap_channel_flags(const cap_channel_t *chan);
76 #else
77 static inline int
78 cap_channel_flags(const cap_channel_t *chan)
79 {
80
81         return (chan->cch_flags);
82 }
83 #endif
84
85 static inline int
86 channel_nvlist_flags(const cap_channel_t *chan)
87 {
88         int flags;
89
90         flags = 0;
91         if ((cap_channel_flags(chan) & CASPER_NO_UNIQ) != 0)
92                 flags |= NV_FLAG_NO_UNIQUE;
93
94         return (flags);
95 }
96
97 /*
98  * The functions opens unrestricted communication channel to Casper.
99  */
100 #ifdef WITH_CASPER
101 cap_channel_t *cap_init(void);
102 #else
103 static inline cap_channel_t *
104 cap_init(void)
105 {
106         cap_channel_t *chan;
107
108         chan = malloc(sizeof(*chan));
109         if (chan != NULL) {
110                 chan->cch_fd = -1;
111         }
112         return (chan);
113 }
114 #endif
115
116 /*
117  * The functions to communicate with service.
118  */
119 #ifdef WITH_CASPER
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);
123 #else
124 #define cap_service_open(chan, name)            (cap_init())
125 #define cap_service_limit(chan, names, nnames)  (0)
126 #endif
127
128 /*
129  * The function creates cap_channel_t based on the given socket.
130  */
131 #ifdef WITH_CASPER
132 cap_channel_t *cap_wrap(int sock, int flags);
133 #else
134 static inline cap_channel_t *
135 cap_wrap(int sock, int flags)
136 {
137         cap_channel_t *chan;
138
139         chan = cap_init();
140         if (chan != NULL) {
141                 chan->cch_fd = sock;
142                 chan->cch_flags = flags;
143         }
144         return (chan);
145 }
146 #endif
147
148 /*
149  * The function returns communication socket and frees cap_channel_t.
150  */
151 #ifdef WITH_CASPER
152 int     cap_unwrap(cap_channel_t *chan, int *flags);
153 #else
154 static inline int
155 cap_unwrap(cap_channel_t *chan)
156 {
157         int fd;
158
159         fd = chan->cch_fd;
160         free(chan);
161         return (fd);
162 }
163 #endif
164
165 /*
166  * The function clones the given capability.
167  */
168 #ifdef WITH_CASPER
169 cap_channel_t *cap_clone(const cap_channel_t *chan);
170 #else
171 static inline cap_channel_t *
172 cap_clone(const cap_channel_t *chan)
173 {
174         cap_channel_t *newchan;
175
176         newchan = cap_init();
177         if (newchan == NULL) {
178                 return (NULL);
179         }
180
181         if (chan->cch_fd == -1) {
182                 newchan->cch_fd = -1;
183         } else {
184                 newchan->cch_fd = dup(chan->cch_fd);
185                 if (newchan->cch_fd < 0) {
186                         free(newchan);
187                         newchan = NULL;
188                 }
189         }
190         newchan->cch_flags = chan->cch_flags;
191
192         return (newchan);
193 }
194 #endif
195
196 /*
197  * The function closes the given capability.
198  */
199 #ifdef WITH_CASPER
200 void    cap_close(cap_channel_t *chan);
201 #else
202 static inline void
203 cap_close(cap_channel_t *chan)
204 {
205
206         if (chan->cch_fd >= 0) {
207                 close(chan->cch_fd);
208         }
209         free(chan);
210 }
211 #endif
212
213 /*
214  * The function returns socket descriptor associated with the given
215  * cap_channel_t for use with select(2)/kqueue(2)/etc.
216  */
217 #ifdef WITH_CASPER
218 int     cap_sock(const cap_channel_t *chan);
219 #else
220 #define cap_sock(chan)  (chan->cch_fd)
221 #endif
222
223 /*
224  * The function limits the given capability.
225  * It always destroys 'limits' on return.
226  */
227 #ifdef WITH_CASPER
228 int     cap_limit_set(const cap_channel_t *chan, nvlist_t *limits);
229 #else
230 #define cap_limit_set(chan, limits)     (0)
231 #endif
232
233 /*
234  * The function returns current limits of the given capability.
235  */
236 #ifdef WITH_CASPER
237 int     cap_limit_get(const cap_channel_t *chan, nvlist_t **limitsp);
238 #else
239 static inline int
240 cap_limit_get(const cap_channel_t *chan __unused, nvlist_t **limitsp)
241 {
242
243         *limitsp = nvlist_create(channel_nvlist_flags(chan));
244         return (0);
245 }
246 #endif
247
248 /*
249  * Function sends nvlist over the given capability.
250  */
251 #ifdef WITH_CASPER
252 int     cap_send_nvlist(const cap_channel_t *chan, const nvlist_t *nvl);
253 #else
254 #define cap_send_nvlist(chan, nvl)      (0)
255 #endif
256
257 /*
258  * Function receives nvlist over the given capability.
259  */
260 #ifdef WITH_CASPER
261 nvlist_t *cap_recv_nvlist(const cap_channel_t *chan);
262 #else
263 #define cap_recv_nvlist(chan)           (nvlist_create(chan->cch_flags))
264 #endif
265
266 /*
267  * Function sends the given nvlist, destroys it and receives new nvlist in
268  * response over the given capability.
269  */
270 #ifdef WITH_CASPER
271 nvlist_t *cap_xfer_nvlist(const cap_channel_t *chan, nvlist_t *nvl);
272 #else
273 static inline nvlist_t *
274 cap_xfer_nvlist(const cap_channel_t *chan, nvlist_t *nvl)
275 {
276
277         nvlist_destroy(nvl);
278         return (nvlist_create(channel_nvlist_flags(chan)));
279 }
280 #endif
281
282 #endif  /* !_LIBCASPER_H_ */