]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libcasper/libcasper/libcasper.h
MFhead@r325119
[FreeBSD/FreeBSD.git] / lib / libcasper / libcasper / libcasper.h
1 /*-
2  * Copyright (c) 2012-2013 The FreeBSD Foundation
3  * Copyright (c) 2015-2017 Mariusz Zaborski <oshogbo@FreeBSD.org>
4  * All rights reserved.
5  *
6  * This software was developed by Pawel Jakub Dawidek under sponsorship from
7  * the FreeBSD Foundation.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  * $FreeBSD$
31  */
32
33 #ifndef _LIBCASPER_H_
34 #define _LIBCASPER_H_
35
36 #ifdef HAVE_CASPER
37 #define WITH_CASPER
38 #endif
39
40 #include <sys/types.h>
41 #include <sys/nv.h>
42
43 #include <stdlib.h>
44 #include <unistd.h>
45
46 #ifndef _NVLIST_T_DECLARED
47 #define _NVLIST_T_DECLARED
48 struct nvlist;
49
50 typedef struct nvlist nvlist_t;
51 #endif
52
53 #ifndef _CAP_CHANNEL_T_DECLARED
54 #define _CAP_CHANNEL_T_DECLARED
55 #ifdef WITH_CASPER
56 struct cap_channel;
57
58 typedef struct cap_channel cap_channel_t;
59 #else
60 struct cap_channel {
61         int cch_fd;
62 };
63 typedef struct cap_channel cap_channel_t;
64 #endif /* ! WITH_CASPER */
65 #endif /* ! _CAP_CHANNEL_T_DECLARED */
66
67 /*
68  * The functions opens unrestricted communication channel to Casper.
69  */
70 #ifdef WITH_CASPER
71 cap_channel_t *cap_init(void);
72 #else
73 static inline cap_channel_t *
74 cap_init(void)
75 {
76         cap_channel_t *chan;
77
78         chan = malloc(sizeof(*chan));
79         if (chan != NULL) {
80                 chan->cch_fd = -1;
81         }
82         return (chan);
83 }
84 #endif
85
86 /*
87  * The functions to communicate with service.
88  */
89 #ifdef WITH_CASPER
90 cap_channel_t   *cap_service_open(const cap_channel_t *chan, const char *name);
91 int              cap_service_limit(const cap_channel_t *chan,
92                     const char * const *names, size_t nnames);
93 #else
94 #define cap_service_open(chan, name)            (cap_init())
95 #define cap_service_limit(chan, names, nnames)  (0)
96 #endif
97
98 /*
99  * The function creates cap_channel_t based on the given socket.
100  */
101 #ifdef WITH_CASPER
102 cap_channel_t *cap_wrap(int sock);
103 #else
104 static inline cap_channel_t *
105 cap_wrap(int sock)
106 {
107         cap_channel_t *chan;
108
109         chan = cap_init();
110         if (chan != NULL) {
111                 chan->cch_fd = sock;
112         }
113         return (chan);
114 }
115 #endif
116
117 /*
118  * The function returns communication socket and frees cap_channel_t.
119  */
120 #ifdef WITH_CASPER
121 int     cap_unwrap(cap_channel_t *chan);
122 #else
123 #define cap_unwrap(chan)        (chan->cch_fd)
124 #endif
125
126 /*
127  * The function clones the given capability.
128  */
129 #ifdef WITH_CASPER
130 cap_channel_t *cap_clone(const cap_channel_t *chan);
131 #else
132 static inline cap_channel_t *
133 cap_clone(const cap_channel_t *chan)
134 {
135         cap_channel_t *newchan;
136
137         newchan = cap_init();
138         if (newchan == NULL) {
139                 return (NULL);
140         }
141
142         if (chan->cch_fd == -1) {
143                 newchan->cch_fd = -1;
144         } else {
145                 newchan->cch_fd = dup(chan->cch_fd);
146                 if (newchan->cch_fd < 0) {
147                         free(newchan);
148                         newchan = NULL;
149                 }
150         }
151
152         return (newchan);
153 }
154 #endif
155
156 /*
157  * The function closes the given capability.
158  */
159 #ifdef WITH_CASPER
160 void    cap_close(cap_channel_t *chan);
161 #else
162 static inline void
163 cap_close(cap_channel_t *chan)
164 {
165
166         if (chan->cch_fd >= 0) {
167                 close(chan->cch_fd);
168         }
169         free(chan);
170 }
171 #endif
172
173 /*
174  * The function returns socket descriptor associated with the given
175  * cap_channel_t for use with select(2)/kqueue(2)/etc.
176  */
177 #ifdef WITH_CASPER
178 int     cap_sock(const cap_channel_t *chan);
179 #else
180 #define cap_sock(chan)  (chan->cch_fd)
181 #endif
182
183 /*
184  * The function limits the given capability.
185  * It always destroys 'limits' on return.
186  */
187 #ifdef WITH_CASPER
188 int     cap_limit_set(const cap_channel_t *chan, nvlist_t *limits);
189 #else
190 #define cap_limit_set(chan, limits)     (0)
191 #endif
192
193 /*
194  * The function returns current limits of the given capability.
195  */
196 #ifdef WITH_CASPER
197 int     cap_limit_get(const cap_channel_t *chan, nvlist_t **limitsp);
198 #else
199 static inline int
200 cap_limit_get(const cap_channel_t *chan __unused, nvlist_t **limitsp)
201 {
202
203         *limitsp = nvlist_create(0);
204         return (0);
205 }
206 #endif
207
208 /*
209  * Function sends nvlist over the given capability.
210  */
211 #ifdef WITH_CASPER
212 int     cap_send_nvlist(const cap_channel_t *chan, const nvlist_t *nvl);
213 #else
214 #define cap_send_nvlist(chan, nvl)      (0)
215 #endif
216
217 /*
218  * Function receives nvlist over the given capability.
219  */
220 #ifdef WITH_CASPER
221 nvlist_t *cap_recv_nvlist(const cap_channel_t *chan, int flags);
222 #else
223 #define cap_recv_nvlist(chan, flags)    (0)
224 #endif
225
226 /*
227  * Function sends the given nvlist, destroys it and receives new nvlist in
228  * response over the given capability.
229  */
230 #ifdef WITH_CASPER
231 nvlist_t *cap_xfer_nvlist(const cap_channel_t *chan, nvlist_t *nvl, int flags);
232 #else
233 static inline nvlist_t *
234 cap_xfer_nvlist(const cap_channel_t *chan __unused, nvlist_t *nvl, int flags)
235 {
236
237         nvlist_destroy(nvl);
238         return (nvlist_create(flags));
239 }
240 #endif
241
242 #endif  /* !_LIBCASPER_H_ */