]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libcasper/libcasper/libcasper.h
MFV r328490: Update libfdt to github:f1879e1
[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 #ifndef _NVLIST_T_DECLARED
49 #define _NVLIST_T_DECLARED
50 struct nvlist;
51
52 typedef struct nvlist nvlist_t;
53 #endif
54
55 #ifndef _CAP_CHANNEL_T_DECLARED
56 #define _CAP_CHANNEL_T_DECLARED
57 #ifdef WITH_CASPER
58 struct cap_channel;
59
60 typedef struct cap_channel cap_channel_t;
61 #define CASPER_SUPPORT  (1)
62 #else
63 struct cap_channel {
64         int cch_fd;
65 };
66 typedef struct cap_channel cap_channel_t;
67 #define CASPER_SUPPORT  (0)
68 #endif /* ! WITH_CASPER */
69 #endif /* ! _CAP_CHANNEL_T_DECLARED */
70
71 /*
72  * The functions opens unrestricted communication channel to Casper.
73  */
74 #ifdef WITH_CASPER
75 cap_channel_t *cap_init(void);
76 #else
77 static inline cap_channel_t *
78 cap_init(void)
79 {
80         cap_channel_t *chan;
81
82         chan = malloc(sizeof(*chan));
83         if (chan != NULL) {
84                 chan->cch_fd = -1;
85         }
86         return (chan);
87 }
88 #endif
89
90 /*
91  * The functions to communicate with service.
92  */
93 #ifdef WITH_CASPER
94 cap_channel_t   *cap_service_open(const cap_channel_t *chan, const char *name);
95 int              cap_service_limit(const cap_channel_t *chan,
96                     const char * const *names, size_t nnames);
97 #else
98 #define cap_service_open(chan, name)            (cap_init())
99 #define cap_service_limit(chan, names, nnames)  (0)
100 #endif
101
102 /*
103  * The function creates cap_channel_t based on the given socket.
104  */
105 #ifdef WITH_CASPER
106 cap_channel_t *cap_wrap(int sock);
107 #else
108 static inline cap_channel_t *
109 cap_wrap(int sock)
110 {
111         cap_channel_t *chan;
112
113         chan = cap_init();
114         if (chan != NULL) {
115                 chan->cch_fd = sock;
116         }
117         return (chan);
118 }
119 #endif
120
121 /*
122  * The function returns communication socket and frees cap_channel_t.
123  */
124 #ifdef WITH_CASPER
125 int     cap_unwrap(cap_channel_t *chan);
126 #else
127 static inline int
128 cap_unwrap(cap_channel_t *chan)
129 {
130         int fd;
131
132         fd = chan->cch_fd;
133         free(chan);
134         return (fd);
135 }
136 #endif
137
138 /*
139  * The function clones the given capability.
140  */
141 #ifdef WITH_CASPER
142 cap_channel_t *cap_clone(const cap_channel_t *chan);
143 #else
144 static inline cap_channel_t *
145 cap_clone(const cap_channel_t *chan)
146 {
147         cap_channel_t *newchan;
148
149         newchan = cap_init();
150         if (newchan == NULL) {
151                 return (NULL);
152         }
153
154         if (chan->cch_fd == -1) {
155                 newchan->cch_fd = -1;
156         } else {
157                 newchan->cch_fd = dup(chan->cch_fd);
158                 if (newchan->cch_fd < 0) {
159                         free(newchan);
160                         newchan = NULL;
161                 }
162         }
163
164         return (newchan);
165 }
166 #endif
167
168 /*
169  * The function closes the given capability.
170  */
171 #ifdef WITH_CASPER
172 void    cap_close(cap_channel_t *chan);
173 #else
174 static inline void
175 cap_close(cap_channel_t *chan)
176 {
177
178         if (chan->cch_fd >= 0) {
179                 close(chan->cch_fd);
180         }
181         free(chan);
182 }
183 #endif
184
185 /*
186  * The function returns socket descriptor associated with the given
187  * cap_channel_t for use with select(2)/kqueue(2)/etc.
188  */
189 #ifdef WITH_CASPER
190 int     cap_sock(const cap_channel_t *chan);
191 #else
192 #define cap_sock(chan)  (chan->cch_fd)
193 #endif
194
195 /*
196  * The function limits the given capability.
197  * It always destroys 'limits' on return.
198  */
199 #ifdef WITH_CASPER
200 int     cap_limit_set(const cap_channel_t *chan, nvlist_t *limits);
201 #else
202 #define cap_limit_set(chan, limits)     (0)
203 #endif
204
205 /*
206  * The function returns current limits of the given capability.
207  */
208 #ifdef WITH_CASPER
209 int     cap_limit_get(const cap_channel_t *chan, nvlist_t **limitsp);
210 #else
211 static inline int
212 cap_limit_get(const cap_channel_t *chan __unused, nvlist_t **limitsp)
213 {
214
215         *limitsp = nvlist_create(0);
216         return (0);
217 }
218 #endif
219
220 /*
221  * Function sends nvlist over the given capability.
222  */
223 #ifdef WITH_CASPER
224 int     cap_send_nvlist(const cap_channel_t *chan, const nvlist_t *nvl);
225 #else
226 #define cap_send_nvlist(chan, nvl)      (0)
227 #endif
228
229 /*
230  * Function receives nvlist over the given capability.
231  */
232 #ifdef WITH_CASPER
233 nvlist_t *cap_recv_nvlist(const cap_channel_t *chan, int flags);
234 #else
235 #define cap_recv_nvlist(chan, flags)    (0)
236 #endif
237
238 /*
239  * Function sends the given nvlist, destroys it and receives new nvlist in
240  * response over the given capability.
241  */
242 #ifdef WITH_CASPER
243 nvlist_t *cap_xfer_nvlist(const cap_channel_t *chan, nvlist_t *nvl, int flags);
244 #else
245 static inline nvlist_t *
246 cap_xfer_nvlist(const cap_channel_t *chan __unused, nvlist_t *nvl, int flags)
247 {
248
249         nvlist_destroy(nvl);
250         return (nvlist_create(flags));
251 }
252 #endif
253
254 #endif  /* !_LIBCASPER_H_ */