2 * Copyright (c) 2012-2013 The FreeBSD Foundation
5 * This software was developed by Pawel Jakub Dawidek under sponsorship from
6 * the FreeBSD Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/types.h>
34 #include <sys/socket.h>
45 #include "libcapsicum.h"
46 #include "libcapsicum_impl.h"
50 * Structure describing communication channel between two separated processes.
52 #define CAP_CHANNEL_MAGIC 0xcac8a31
55 * Magic value helps to ensure that a pointer to the right structure is
56 * passed to our functions.
59 /* Socket descriptor for IPC. */
67 return (fcntl(fd, F_GETFL) != -1 || errno != EBADF);
74 struct sockaddr_un sun;
77 bzero(&sun, sizeof(sun));
78 sun.sun_family = AF_UNIX;
79 strlcpy(sun.sun_path, CASPER_SOCKPATH, sizeof(sun.sun_path));
80 sun.sun_len = SUN_LEN(&sun);
82 sock = socket(AF_UNIX, SOCK_STREAM, 0);
85 if (connect(sock, (struct sockaddr *)&sun, sizeof(sun)) < 0) {
91 chan = cap_wrap(sock);
106 if (!fd_is_valid(sock))
109 chan = malloc(sizeof(*chan));
111 chan->cch_sock = sock;
112 chan->cch_magic = CAP_CHANNEL_MAGIC;
119 cap_unwrap(cap_channel_t *chan)
123 assert(chan != NULL);
124 assert(chan->cch_magic == CAP_CHANNEL_MAGIC);
126 sock = chan->cch_sock;
134 cap_clone(const cap_channel_t *chan)
136 cap_channel_t *newchan;
140 assert(chan != NULL);
141 assert(chan->cch_magic == CAP_CHANNEL_MAGIC);
143 nvl = nvlist_create(0);
144 nvlist_add_string(nvl, "cmd", "clone");
145 nvl = cap_xfer_nvlist(chan, nvl, 0);
148 if (nvlist_get_number(nvl, "error") != 0) {
149 errno = (int)nvlist_get_number(nvl, "error");
153 newsock = nvlist_take_descriptor(nvl, "sock");
155 newchan = cap_wrap(newsock);
156 if (newchan == NULL) {
168 cap_close(cap_channel_t *chan)
171 assert(chan != NULL);
172 assert(chan->cch_magic == CAP_CHANNEL_MAGIC);
175 close(chan->cch_sock);
180 cap_sock(const cap_channel_t *chan)
183 assert(chan != NULL);
184 assert(chan->cch_magic == CAP_CHANNEL_MAGIC);
186 return (chan->cch_sock);
190 cap_limit_set(const cap_channel_t *chan, nvlist_t *limits)
195 nvlmsg = nvlist_create(0);
196 nvlist_add_string(nvlmsg, "cmd", "limit_set");
197 nvlist_add_nvlist(nvlmsg, "limits", limits);
198 nvlmsg = cap_xfer_nvlist(chan, nvlmsg, 0);
199 if (nvlmsg == NULL) {
200 nvlist_destroy(limits);
203 error = (int)nvlist_get_number(nvlmsg, "error");
204 nvlist_destroy(nvlmsg);
205 nvlist_destroy(limits);
214 cap_limit_get(const cap_channel_t *chan, nvlist_t **limitsp)
219 nvlmsg = nvlist_create(0);
220 nvlist_add_string(nvlmsg, "cmd", "limit_get");
221 nvlmsg = cap_xfer_nvlist(chan, nvlmsg, 0);
224 error = (int)nvlist_get_number(nvlmsg, "error");
226 nvlist_destroy(nvlmsg);
230 if (nvlist_exists_null(nvlmsg, "limits"))
233 *limitsp = nvlist_take_nvlist(nvlmsg, "limits");
234 nvlist_destroy(nvlmsg);
239 cap_send_nvlist(const cap_channel_t *chan, const nvlist_t *nvl)
242 assert(chan != NULL);
243 assert(chan->cch_magic == CAP_CHANNEL_MAGIC);
245 return (nvlist_send(chan->cch_sock, nvl));
249 cap_recv_nvlist(const cap_channel_t *chan, int flags)
252 assert(chan != NULL);
253 assert(chan->cch_magic == CAP_CHANNEL_MAGIC);
255 return (nvlist_recv(chan->cch_sock, flags));
259 cap_xfer_nvlist(const cap_channel_t *chan, nvlist_t *nvl, int flags)
262 assert(chan != NULL);
263 assert(chan->cch_magic == CAP_CHANNEL_MAGIC);
265 return (nvlist_xfer(chan->cch_sock, nvl, flags));