]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/ctld/kernel.c
Import tzdata 2017c
[FreeBSD/FreeBSD.git] / usr.sbin / ctld / kernel.c
1 /*-
2  * Copyright (c) 2003, 2004 Silicon Graphics International Corp.
3  * Copyright (c) 1997-2007 Kenneth D. Merry
4  * Copyright (c) 2012 The FreeBSD Foundation
5  * All rights reserved.
6  *
7  * Portions of this software were developed by Edward Tomasz Napierala
8  * under sponsorship from the FreeBSD Foundation.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions, and the following disclaimer,
15  *    without modification.
16  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
17  *    substantially similar to the "NO WARRANTY" disclaimer below
18  *    ("Disclaimer") and any redistribution must be conditioned upon
19  *    including a substantially similar Disclaimer requirement for further
20  *    binary redistribution.
21  *
22  * NO WARRANTY
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
26  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
32  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGES.
34  *
35  */
36
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
39
40 #include <sys/param.h>
41 #include <sys/capsicum.h>
42 #include <sys/callout.h>
43 #include <sys/ioctl.h>
44 #include <sys/linker.h>
45 #include <sys/module.h>
46 #include <sys/queue.h>
47 #include <sys/sbuf.h>
48 #include <sys/stat.h>
49 #include <assert.h>
50 #include <bsdxml.h>
51 #include <ctype.h>
52 #include <errno.h>
53 #include <fcntl.h>
54 #include <stdint.h>
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include <strings.h>
59 #include <cam/scsi/scsi_all.h>
60 #include <cam/scsi/scsi_message.h>
61 #include <cam/ctl/ctl.h>
62 #include <cam/ctl/ctl_io.h>
63 #include <cam/ctl/ctl_backend.h>
64 #include <cam/ctl/ctl_ioctl.h>
65 #include <cam/ctl/ctl_util.h>
66 #include <cam/ctl/ctl_scsi_all.h>
67
68 #include "ctld.h"
69
70 #ifdef ICL_KERNEL_PROXY
71 #include <netdb.h>
72 #endif
73
74 extern bool proxy_mode;
75
76 static int      ctl_fd = 0;
77
78 void
79 kernel_init(void)
80 {
81         int retval, saved_errno;
82
83         ctl_fd = open(CTL_DEFAULT_DEV, O_RDWR);
84         if (ctl_fd < 0 && errno == ENOENT) {
85                 saved_errno = errno;
86                 retval = kldload("ctl");
87                 if (retval != -1)
88                         ctl_fd = open(CTL_DEFAULT_DEV, O_RDWR);
89                 else
90                         errno = saved_errno;
91         }
92         if (ctl_fd < 0)
93                 log_err(1, "failed to open %s", CTL_DEFAULT_DEV);
94 #ifdef  WANT_ISCSI
95         else {
96                 saved_errno = errno;
97                 if (modfind("cfiscsi") == -1 && kldload("cfiscsi") == -1)
98                         log_warn("couldn't load cfiscsi");
99                 errno = saved_errno;
100         }
101 #endif
102 }
103
104 /*
105  * Name/value pair used for per-LUN attributes.
106  */
107 struct cctl_lun_nv {
108         char *name;
109         char *value;
110         STAILQ_ENTRY(cctl_lun_nv) links;
111 };
112
113 /*
114  * Backend LUN information.
115  */
116 struct cctl_lun {
117         uint64_t lun_id;
118         char *backend_type;
119         uint8_t device_type;
120         uint64_t size_blocks;
121         uint32_t blocksize;
122         char *serial_number;
123         char *device_id;
124         char *ctld_name;
125         STAILQ_HEAD(,cctl_lun_nv) attr_list;
126         STAILQ_ENTRY(cctl_lun) links;
127 };
128
129 struct cctl_port {
130         uint32_t port_id;
131         char *port_frontend;
132         char *port_name;
133         int pp;
134         int vp;
135         int cfiscsi_state;
136         char *cfiscsi_target;
137         uint16_t cfiscsi_portal_group_tag;
138         char *ctld_portal_group_name;
139         STAILQ_HEAD(,cctl_lun_nv) attr_list;
140         STAILQ_ENTRY(cctl_port) links;
141 };
142
143 struct cctl_devlist_data {
144         int num_luns;
145         STAILQ_HEAD(,cctl_lun) lun_list;
146         struct cctl_lun *cur_lun;
147         int num_ports;
148         STAILQ_HEAD(,cctl_port) port_list;
149         struct cctl_port *cur_port;
150         int level;
151         struct sbuf *cur_sb[32];
152 };
153
154 static void
155 cctl_start_element(void *user_data, const char *name, const char **attr)
156 {
157         int i;
158         struct cctl_devlist_data *devlist;
159         struct cctl_lun *cur_lun;
160
161         devlist = (struct cctl_devlist_data *)user_data;
162         cur_lun = devlist->cur_lun;
163         devlist->level++;
164         if ((u_int)devlist->level >= (sizeof(devlist->cur_sb) /
165             sizeof(devlist->cur_sb[0])))
166                 log_errx(1, "%s: too many nesting levels, %zd max", __func__,
167                      sizeof(devlist->cur_sb) / sizeof(devlist->cur_sb[0]));
168
169         devlist->cur_sb[devlist->level] = sbuf_new_auto();
170         if (devlist->cur_sb[devlist->level] == NULL)
171                 log_err(1, "%s: unable to allocate sbuf", __func__);
172
173         if (strcmp(name, "lun") == 0) {
174                 if (cur_lun != NULL)
175                         log_errx(1, "%s: improper lun element nesting",
176                             __func__);
177
178                 cur_lun = calloc(1, sizeof(*cur_lun));
179                 if (cur_lun == NULL)
180                         log_err(1, "%s: cannot allocate %zd bytes", __func__,
181                             sizeof(*cur_lun));
182
183                 devlist->num_luns++;
184                 devlist->cur_lun = cur_lun;
185
186                 STAILQ_INIT(&cur_lun->attr_list);
187                 STAILQ_INSERT_TAIL(&devlist->lun_list, cur_lun, links);
188
189                 for (i = 0; attr[i] != NULL; i += 2) {
190                         if (strcmp(attr[i], "id") == 0) {
191                                 cur_lun->lun_id = strtoull(attr[i+1], NULL, 0);
192                         } else {
193                                 log_errx(1, "%s: invalid LUN attribute %s = %s",
194                                      __func__, attr[i], attr[i+1]);
195                         }
196                 }
197         }
198 }
199
200 static void
201 cctl_end_element(void *user_data, const char *name)
202 {
203         struct cctl_devlist_data *devlist;
204         struct cctl_lun *cur_lun;
205         char *str;
206
207         devlist = (struct cctl_devlist_data *)user_data;
208         cur_lun = devlist->cur_lun;
209
210         if ((cur_lun == NULL)
211          && (strcmp(name, "ctllunlist") != 0))
212                 log_errx(1, "%s: cur_lun == NULL! (name = %s)", __func__, name);
213
214         if (devlist->cur_sb[devlist->level] == NULL)
215                 log_errx(1, "%s: no valid sbuf at level %d (name %s)", __func__,
216                      devlist->level, name);
217
218         sbuf_finish(devlist->cur_sb[devlist->level]);
219         str = checked_strdup(sbuf_data(devlist->cur_sb[devlist->level]));
220
221         if (strlen(str) == 0) {
222                 free(str);
223                 str = NULL;
224         }
225
226         sbuf_delete(devlist->cur_sb[devlist->level]);
227         devlist->cur_sb[devlist->level] = NULL;
228         devlist->level--;
229
230         if (strcmp(name, "backend_type") == 0) {
231                 cur_lun->backend_type = str;
232                 str = NULL;
233         } else if (strcmp(name, "lun_type") == 0) {
234                 cur_lun->device_type = strtoull(str, NULL, 0);
235         } else if (strcmp(name, "size") == 0) {
236                 cur_lun->size_blocks = strtoull(str, NULL, 0);
237         } else if (strcmp(name, "blocksize") == 0) {
238                 cur_lun->blocksize = strtoul(str, NULL, 0);
239         } else if (strcmp(name, "serial_number") == 0) {
240                 cur_lun->serial_number = str;
241                 str = NULL;
242         } else if (strcmp(name, "device_id") == 0) {
243                 cur_lun->device_id = str;
244                 str = NULL;
245         } else if (strcmp(name, "ctld_name") == 0) {
246                 cur_lun->ctld_name = str;
247                 str = NULL;
248         } else if (strcmp(name, "lun") == 0) {
249                 devlist->cur_lun = NULL;
250         } else if (strcmp(name, "ctllunlist") == 0) {
251                 /* Nothing. */
252         } else {
253                 struct cctl_lun_nv *nv;
254
255                 nv = calloc(1, sizeof(*nv));
256                 if (nv == NULL)
257                         log_err(1, "%s: can't allocate %zd bytes for nv pair",
258                             __func__, sizeof(*nv));
259
260                 nv->name = checked_strdup(name);
261
262                 nv->value = str;
263                 str = NULL;
264                 STAILQ_INSERT_TAIL(&cur_lun->attr_list, nv, links);
265         }
266
267         free(str);
268 }
269
270 static void
271 cctl_start_pelement(void *user_data, const char *name, const char **attr)
272 {
273         int i;
274         struct cctl_devlist_data *devlist;
275         struct cctl_port *cur_port;
276
277         devlist = (struct cctl_devlist_data *)user_data;
278         cur_port = devlist->cur_port;
279         devlist->level++;
280         if ((u_int)devlist->level >= (sizeof(devlist->cur_sb) /
281             sizeof(devlist->cur_sb[0])))
282                 log_errx(1, "%s: too many nesting levels, %zd max", __func__,
283                      sizeof(devlist->cur_sb) / sizeof(devlist->cur_sb[0]));
284
285         devlist->cur_sb[devlist->level] = sbuf_new_auto();
286         if (devlist->cur_sb[devlist->level] == NULL)
287                 log_err(1, "%s: unable to allocate sbuf", __func__);
288
289         if (strcmp(name, "targ_port") == 0) {
290                 if (cur_port != NULL)
291                         log_errx(1, "%s: improper port element nesting (%s)",
292                             __func__, name);
293
294                 cur_port = calloc(1, sizeof(*cur_port));
295                 if (cur_port == NULL)
296                         log_err(1, "%s: cannot allocate %zd bytes", __func__,
297                             sizeof(*cur_port));
298
299                 devlist->num_ports++;
300                 devlist->cur_port = cur_port;
301
302                 STAILQ_INIT(&cur_port->attr_list);
303                 STAILQ_INSERT_TAIL(&devlist->port_list, cur_port, links);
304
305                 for (i = 0; attr[i] != NULL; i += 2) {
306                         if (strcmp(attr[i], "id") == 0) {
307                                 cur_port->port_id = strtoul(attr[i+1], NULL, 0);
308                         } else {
309                                 log_errx(1, "%s: invalid LUN attribute %s = %s",
310                                      __func__, attr[i], attr[i+1]);
311                         }
312                 }
313         }
314 }
315
316 static void
317 cctl_end_pelement(void *user_data, const char *name)
318 {
319         struct cctl_devlist_data *devlist;
320         struct cctl_port *cur_port;
321         char *str;
322
323         devlist = (struct cctl_devlist_data *)user_data;
324         cur_port = devlist->cur_port;
325
326         if ((cur_port == NULL)
327          && (strcmp(name, "ctlportlist") != 0))
328                 log_errx(1, "%s: cur_port == NULL! (name = %s)", __func__, name);
329
330         if (devlist->cur_sb[devlist->level] == NULL)
331                 log_errx(1, "%s: no valid sbuf at level %d (name %s)", __func__,
332                      devlist->level, name);
333
334         sbuf_finish(devlist->cur_sb[devlist->level]);
335         str = checked_strdup(sbuf_data(devlist->cur_sb[devlist->level]));
336
337         if (strlen(str) == 0) {
338                 free(str);
339                 str = NULL;
340         }
341
342         sbuf_delete(devlist->cur_sb[devlist->level]);
343         devlist->cur_sb[devlist->level] = NULL;
344         devlist->level--;
345
346         if (strcmp(name, "frontend_type") == 0) {
347                 cur_port->port_frontend = str;
348                 str = NULL;
349         } else if (strcmp(name, "port_name") == 0) {
350                 cur_port->port_name = str;
351                 str = NULL;
352         } else if (strcmp(name, "physical_port") == 0) {
353                 cur_port->pp = strtoul(str, NULL, 0);
354         } else if (strcmp(name, "virtual_port") == 0) {
355                 cur_port->vp = strtoul(str, NULL, 0);
356         } else if (strcmp(name, "cfiscsi_target") == 0) {
357                 cur_port->cfiscsi_target = str;
358                 str = NULL;
359         } else if (strcmp(name, "cfiscsi_state") == 0) {
360                 cur_port->cfiscsi_state = strtoul(str, NULL, 0);
361         } else if (strcmp(name, "cfiscsi_portal_group_tag") == 0) {
362                 cur_port->cfiscsi_portal_group_tag = strtoul(str, NULL, 0);
363         } else if (strcmp(name, "ctld_portal_group_name") == 0) {
364                 cur_port->ctld_portal_group_name = str;
365                 str = NULL;
366         } else if (strcmp(name, "targ_port") == 0) {
367                 devlist->cur_port = NULL;
368         } else if (strcmp(name, "ctlportlist") == 0) {
369                 /* Nothing. */
370         } else {
371                 struct cctl_lun_nv *nv;
372
373                 nv = calloc(1, sizeof(*nv));
374                 if (nv == NULL)
375                         log_err(1, "%s: can't allocate %zd bytes for nv pair",
376                             __func__, sizeof(*nv));
377
378                 nv->name = checked_strdup(name);
379
380                 nv->value = str;
381                 str = NULL;
382                 STAILQ_INSERT_TAIL(&cur_port->attr_list, nv, links);
383         }
384
385         free(str);
386 }
387
388 static void
389 cctl_char_handler(void *user_data, const XML_Char *str, int len)
390 {
391         struct cctl_devlist_data *devlist;
392
393         devlist = (struct cctl_devlist_data *)user_data;
394
395         sbuf_bcat(devlist->cur_sb[devlist->level], str, len);
396 }
397
398 struct conf *
399 conf_new_from_kernel(void)
400 {
401         struct conf *conf = NULL;
402         struct target *targ;
403         struct portal_group *pg;
404         struct pport *pp;
405         struct port *cp;
406         struct lun *cl;
407         struct option *o;
408         struct ctl_lun_list list;
409         struct cctl_devlist_data devlist;
410         struct cctl_lun *lun;
411         struct cctl_port *port;
412         XML_Parser parser;
413         char *str, *name;
414         int len, retval;
415
416         bzero(&devlist, sizeof(devlist));
417         STAILQ_INIT(&devlist.lun_list);
418         STAILQ_INIT(&devlist.port_list);
419
420         log_debugx("obtaining previously configured CTL luns from the kernel");
421
422         str = NULL;
423         len = 4096;
424 retry:
425         str = realloc(str, len);
426         if (str == NULL)
427                 log_err(1, "realloc");
428
429         bzero(&list, sizeof(list));
430         list.alloc_len = len;
431         list.status = CTL_LUN_LIST_NONE;
432         list.lun_xml = str;
433
434         if (ioctl(ctl_fd, CTL_LUN_LIST, &list) == -1) {
435                 log_warn("error issuing CTL_LUN_LIST ioctl");
436                 free(str);
437                 return (NULL);
438         }
439
440         if (list.status == CTL_LUN_LIST_ERROR) {
441                 log_warnx("error returned from CTL_LUN_LIST ioctl: %s",
442                     list.error_str);
443                 free(str);
444                 return (NULL);
445         }
446
447         if (list.status == CTL_LUN_LIST_NEED_MORE_SPACE) {
448                 len = len << 1;
449                 goto retry;
450         }
451
452         parser = XML_ParserCreate(NULL);
453         if (parser == NULL) {
454                 log_warnx("unable to create XML parser");
455                 free(str);
456                 return (NULL);
457         }
458
459         XML_SetUserData(parser, &devlist);
460         XML_SetElementHandler(parser, cctl_start_element, cctl_end_element);
461         XML_SetCharacterDataHandler(parser, cctl_char_handler);
462
463         retval = XML_Parse(parser, str, strlen(str), 1);
464         XML_ParserFree(parser);
465         free(str);
466         if (retval != 1) {
467                 log_warnx("XML_Parse failed");
468                 return (NULL);
469         }
470
471         str = NULL;
472         len = 4096;
473 retry_port:
474         str = realloc(str, len);
475         if (str == NULL)
476                 log_err(1, "realloc");
477
478         bzero(&list, sizeof(list));
479         list.alloc_len = len;
480         list.status = CTL_LUN_LIST_NONE;
481         list.lun_xml = str;
482
483         if (ioctl(ctl_fd, CTL_PORT_LIST, &list) == -1) {
484                 log_warn("error issuing CTL_PORT_LIST ioctl");
485                 free(str);
486                 return (NULL);
487         }
488
489         if (list.status == CTL_LUN_LIST_ERROR) {
490                 log_warnx("error returned from CTL_PORT_LIST ioctl: %s",
491                     list.error_str);
492                 free(str);
493                 return (NULL);
494         }
495
496         if (list.status == CTL_LUN_LIST_NEED_MORE_SPACE) {
497                 len = len << 1;
498                 goto retry_port;
499         }
500
501         parser = XML_ParserCreate(NULL);
502         if (parser == NULL) {
503                 log_warnx("unable to create XML parser");
504                 free(str);
505                 return (NULL);
506         }
507
508         XML_SetUserData(parser, &devlist);
509         XML_SetElementHandler(parser, cctl_start_pelement, cctl_end_pelement);
510         XML_SetCharacterDataHandler(parser, cctl_char_handler);
511
512         retval = XML_Parse(parser, str, strlen(str), 1);
513         XML_ParserFree(parser);
514         free(str);
515         if (retval != 1) {
516                 log_warnx("XML_Parse failed");
517                 return (NULL);
518         }
519
520         conf = conf_new();
521
522         name = NULL;
523         STAILQ_FOREACH(port, &devlist.port_list, links) {
524                 if (strcmp(port->port_frontend, "ha") == 0)
525                         continue;
526                 free(name);
527                 if (port->pp == 0 && port->vp == 0) {
528                         name = checked_strdup(port->port_name);
529                 } else if (port->vp == 0) {
530                         retval = asprintf(&name, "%s/%d",
531                             port->port_name, port->pp);
532                         if (retval <= 0)
533                                 log_err(1, "asprintf");
534                 } else {
535                         retval = asprintf(&name, "%s/%d/%d",
536                             port->port_name, port->pp, port->vp);
537                         if (retval <= 0)
538                                 log_err(1, "asprintf");
539                 }
540
541                 if (port->cfiscsi_target == NULL) {
542                         log_debugx("CTL port %u \"%s\" wasn't managed by ctld; ",
543                             port->port_id, name);
544                         pp = pport_find(conf, name);
545                         if (pp == NULL) {
546 #if 0
547                                 log_debugx("found new kernel port %u \"%s\"",
548                                     port->port_id, name);
549 #endif
550                                 pp = pport_new(conf, name, port->port_id);
551                                 if (pp == NULL) {
552                                         log_warnx("pport_new failed");
553                                         continue;
554                                 }
555                         }
556                         continue;
557                 }
558                 if (port->cfiscsi_state != 1) {
559                         log_debugx("CTL port %ju is not active (%d); ignoring",
560                             (uintmax_t)port->port_id, port->cfiscsi_state);
561                         continue;
562                 }
563
564                 targ = target_find(conf, port->cfiscsi_target);
565                 if (targ == NULL) {
566 #if 0
567                         log_debugx("found new kernel target %s for CTL port %ld",
568                             port->cfiscsi_target, port->port_id);
569 #endif
570                         targ = target_new(conf, port->cfiscsi_target);
571                         if (targ == NULL) {
572                                 log_warnx("target_new failed");
573                                 continue;
574                         }
575                 }
576
577                 if (port->ctld_portal_group_name == NULL)
578                         continue;
579                 pg = portal_group_find(conf, port->ctld_portal_group_name);
580                 if (pg == NULL) {
581 #if 0
582                         log_debugx("found new kernel portal group %s for CTL port %ld",
583                             port->ctld_portal_group_name, port->port_id);
584 #endif
585                         pg = portal_group_new(conf, port->ctld_portal_group_name);
586                         if (pg == NULL) {
587                                 log_warnx("portal_group_new failed");
588                                 continue;
589                         }
590                 }
591                 pg->pg_tag = port->cfiscsi_portal_group_tag;
592                 cp = port_new(conf, targ, pg);
593                 if (cp == NULL) {
594                         log_warnx("port_new failed");
595                         continue;
596                 }
597                 cp->p_ctl_port = port->port_id;
598         }
599         free(name);
600
601         STAILQ_FOREACH(lun, &devlist.lun_list, links) {
602                 struct cctl_lun_nv *nv;
603
604                 if (lun->ctld_name == NULL) {
605                         log_debugx("CTL lun %ju wasn't managed by ctld; "
606                             "ignoring", (uintmax_t)lun->lun_id);
607                         continue;
608                 }
609
610                 cl = lun_find(conf, lun->ctld_name);
611                 if (cl != NULL) {
612                         log_warnx("found CTL lun %ju \"%s\", "
613                             "also backed by CTL lun %d; ignoring",
614                             (uintmax_t)lun->lun_id, lun->ctld_name,
615                             cl->l_ctl_lun);
616                         continue;
617                 }
618
619                 log_debugx("found CTL lun %ju \"%s\"",
620                     (uintmax_t)lun->lun_id, lun->ctld_name);
621
622                 cl = lun_new(conf, lun->ctld_name);
623                 if (cl == NULL) {
624                         log_warnx("lun_new failed");
625                         continue;
626                 }
627                 lun_set_backend(cl, lun->backend_type);
628                 lun_set_device_type(cl, lun->device_type);
629                 lun_set_blocksize(cl, lun->blocksize);
630                 lun_set_device_id(cl, lun->device_id);
631                 lun_set_serial(cl, lun->serial_number);
632                 lun_set_size(cl, lun->size_blocks * cl->l_blocksize);
633                 lun_set_ctl_lun(cl, lun->lun_id);
634
635                 STAILQ_FOREACH(nv, &lun->attr_list, links) {
636                         if (strcmp(nv->name, "file") == 0 ||
637                             strcmp(nv->name, "dev") == 0) {
638                                 lun_set_path(cl, nv->value);
639                                 continue;
640                         }
641                         o = option_new(&cl->l_options, nv->name, nv->value);
642                         if (o == NULL)
643                                 log_warnx("unable to add CTL lun option %s "
644                                     "for CTL lun %ju \"%s\"",
645                                     nv->name, (uintmax_t) lun->lun_id,
646                                     cl->l_name);
647                 }
648         }
649
650         return (conf);
651 }
652
653 static void
654 str_arg(struct ctl_be_arg *arg, const char *name, const char *value)
655 {
656
657         arg->namelen = strlen(name) + 1;
658         arg->name = __DECONST(char *, name);
659         arg->vallen = strlen(value) + 1;
660         arg->value = __DECONST(char *, value);
661         arg->flags = CTL_BEARG_ASCII | CTL_BEARG_RD;
662 }
663
664 int
665 kernel_lun_add(struct lun *lun)
666 {
667         struct option *o;
668         struct ctl_lun_req req;
669         int error, i, num_options;
670
671         bzero(&req, sizeof(req));
672
673         strlcpy(req.backend, lun->l_backend, sizeof(req.backend));
674         req.reqtype = CTL_LUNREQ_CREATE;
675
676         req.reqdata.create.blocksize_bytes = lun->l_blocksize;
677
678         if (lun->l_size != 0)
679                 req.reqdata.create.lun_size_bytes = lun->l_size;
680
681         if (lun->l_ctl_lun >= 0) {
682                 req.reqdata.create.req_lun_id = lun->l_ctl_lun;
683                 req.reqdata.create.flags |= CTL_LUN_FLAG_ID_REQ;
684         }
685
686         req.reqdata.create.flags |= CTL_LUN_FLAG_DEV_TYPE;
687         req.reqdata.create.device_type = lun->l_device_type;
688
689         if (lun->l_serial != NULL) {
690                 strncpy(req.reqdata.create.serial_num, lun->l_serial,
691                         sizeof(req.reqdata.create.serial_num));
692                 req.reqdata.create.flags |= CTL_LUN_FLAG_SERIAL_NUM;
693         }
694
695         if (lun->l_device_id != NULL) {
696                 strncpy(req.reqdata.create.device_id, lun->l_device_id,
697                         sizeof(req.reqdata.create.device_id));
698                 req.reqdata.create.flags |= CTL_LUN_FLAG_DEVID;
699         }
700
701         if (lun->l_path != NULL) {
702                 o = option_find(&lun->l_options, "file");
703                 if (o != NULL) {
704                         option_set(o, lun->l_path);
705                 } else {
706                         o = option_new(&lun->l_options, "file", lun->l_path);
707                         assert(o != NULL);
708                 }
709         }
710
711         o = option_find(&lun->l_options, "ctld_name");
712         if (o != NULL) {
713                 option_set(o, lun->l_name);
714         } else {
715                 o = option_new(&lun->l_options, "ctld_name", lun->l_name);
716                 assert(o != NULL);
717         }
718
719         o = option_find(&lun->l_options, "scsiname");
720         if (o == NULL && lun->l_scsiname != NULL) {
721                 o = option_new(&lun->l_options, "scsiname", lun->l_scsiname);
722                 assert(o != NULL);
723         }
724
725         num_options = 0;
726         TAILQ_FOREACH(o, &lun->l_options, o_next)
727                 num_options++;
728
729         req.num_be_args = num_options;
730         if (num_options > 0) {
731                 req.be_args = malloc(num_options * sizeof(*req.be_args));
732                 if (req.be_args == NULL) {
733                         log_warn("error allocating %zd bytes",
734                             num_options * sizeof(*req.be_args));
735                         return (1);
736                 }
737
738                 i = 0;
739                 TAILQ_FOREACH(o, &lun->l_options, o_next) {
740                         str_arg(&req.be_args[i], o->o_name, o->o_value);
741                         i++;
742                 }
743                 assert(i == num_options);
744         }
745
746         error = ioctl(ctl_fd, CTL_LUN_REQ, &req);
747         free(req.be_args);
748         if (error != 0) {
749                 log_warn("error issuing CTL_LUN_REQ ioctl");
750                 return (1);
751         }
752
753         switch (req.status) {
754         case CTL_LUN_ERROR:
755                 log_warnx("LUN creation error: %s", req.error_str);
756                 return (1);
757         case CTL_LUN_WARNING:
758                 log_warnx("LUN creation warning: %s", req.error_str);
759                 break;
760         case CTL_LUN_OK:
761                 break;
762         default:
763                 log_warnx("unknown LUN creation status: %d",
764                     req.status);
765                 return (1);
766         }
767
768         lun_set_ctl_lun(lun, req.reqdata.create.req_lun_id);
769         return (0);
770 }
771
772 int
773 kernel_lun_modify(struct lun *lun)
774 {
775         struct option *o;
776         struct ctl_lun_req req;
777         int error, i, num_options;
778
779         bzero(&req, sizeof(req));
780
781         strlcpy(req.backend, lun->l_backend, sizeof(req.backend));
782         req.reqtype = CTL_LUNREQ_MODIFY;
783
784         req.reqdata.modify.lun_id = lun->l_ctl_lun;
785         req.reqdata.modify.lun_size_bytes = lun->l_size;
786
787         num_options = 0;
788         TAILQ_FOREACH(o, &lun->l_options, o_next)
789                 num_options++;
790
791         req.num_be_args = num_options;
792         if (num_options > 0) {
793                 req.be_args = malloc(num_options * sizeof(*req.be_args));
794                 if (req.be_args == NULL) {
795                         log_warn("error allocating %zd bytes",
796                             num_options * sizeof(*req.be_args));
797                         return (1);
798                 }
799
800                 i = 0;
801                 TAILQ_FOREACH(o, &lun->l_options, o_next) {
802                         str_arg(&req.be_args[i], o->o_name, o->o_value);
803                         i++;
804                 }
805                 assert(i == num_options);
806         }
807
808         error = ioctl(ctl_fd, CTL_LUN_REQ, &req);
809         free(req.be_args);
810         if (error != 0) {
811                 log_warn("error issuing CTL_LUN_REQ ioctl");
812                 return (1);
813         }
814
815         switch (req.status) {
816         case CTL_LUN_ERROR:
817                 log_warnx("LUN modification error: %s", req.error_str);
818                 return (1);
819         case CTL_LUN_WARNING:
820                 log_warnx("LUN modification warning: %s", req.error_str);
821                 break;
822         case CTL_LUN_OK:
823                 break;
824         default:
825                 log_warnx("unknown LUN modification status: %d",
826                     req.status);
827                 return (1);
828         }
829
830         return (0);
831 }
832
833 int
834 kernel_lun_remove(struct lun *lun)
835 {
836         struct ctl_lun_req req;
837
838         bzero(&req, sizeof(req));
839
840         strlcpy(req.backend, lun->l_backend, sizeof(req.backend));
841         req.reqtype = CTL_LUNREQ_RM;
842
843         req.reqdata.rm.lun_id = lun->l_ctl_lun;
844
845         if (ioctl(ctl_fd, CTL_LUN_REQ, &req) == -1) {
846                 log_warn("error issuing CTL_LUN_REQ ioctl");
847                 return (1);
848         }
849
850         switch (req.status) {
851         case CTL_LUN_ERROR:
852                 log_warnx("LUN removal error: %s", req.error_str);
853                 return (1);
854         case CTL_LUN_WARNING:
855                 log_warnx("LUN removal warning: %s", req.error_str);
856                 break;
857         case CTL_LUN_OK:
858                 break;
859         default:
860                 log_warnx("unknown LUN removal status: %d", req.status);
861                 return (1);
862         }
863
864         return (0);
865 }
866
867 void
868 kernel_handoff(struct connection *conn)
869 {
870         struct ctl_iscsi req;
871
872         bzero(&req, sizeof(req));
873
874         req.type = CTL_ISCSI_HANDOFF;
875         strlcpy(req.data.handoff.initiator_name,
876             conn->conn_initiator_name, sizeof(req.data.handoff.initiator_name));
877         strlcpy(req.data.handoff.initiator_addr,
878             conn->conn_initiator_addr, sizeof(req.data.handoff.initiator_addr));
879         if (conn->conn_initiator_alias != NULL) {
880                 strlcpy(req.data.handoff.initiator_alias,
881                     conn->conn_initiator_alias, sizeof(req.data.handoff.initiator_alias));
882         }
883         memcpy(req.data.handoff.initiator_isid, conn->conn_initiator_isid,
884             sizeof(req.data.handoff.initiator_isid));
885         strlcpy(req.data.handoff.target_name,
886             conn->conn_target->t_name, sizeof(req.data.handoff.target_name));
887         if (conn->conn_portal->p_portal_group->pg_offload != NULL) {
888                 strlcpy(req.data.handoff.offload,
889                     conn->conn_portal->p_portal_group->pg_offload,
890                     sizeof(req.data.handoff.offload));
891         }
892 #ifdef ICL_KERNEL_PROXY
893         if (proxy_mode)
894                 req.data.handoff.connection_id = conn->conn_socket;
895         else
896                 req.data.handoff.socket = conn->conn_socket;
897 #else
898         req.data.handoff.socket = conn->conn_socket;
899 #endif
900         req.data.handoff.portal_group_tag =
901             conn->conn_portal->p_portal_group->pg_tag;
902         if (conn->conn_header_digest == CONN_DIGEST_CRC32C)
903                 req.data.handoff.header_digest = CTL_ISCSI_DIGEST_CRC32C;
904         if (conn->conn_data_digest == CONN_DIGEST_CRC32C)
905                 req.data.handoff.data_digest = CTL_ISCSI_DIGEST_CRC32C;
906         req.data.handoff.cmdsn = conn->conn_cmdsn;
907         req.data.handoff.statsn = conn->conn_statsn;
908         req.data.handoff.max_recv_data_segment_length =
909             conn->conn_max_recv_data_segment_length;
910         req.data.handoff.max_send_data_segment_length =
911             conn->conn_max_send_data_segment_length;
912         req.data.handoff.max_burst_length = conn->conn_max_burst_length;
913         req.data.handoff.first_burst_length = conn->conn_first_burst_length;
914         req.data.handoff.immediate_data = conn->conn_immediate_data;
915
916         if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) {
917                 log_err(1, "error issuing CTL_ISCSI ioctl; "
918                     "dropping connection");
919         }
920
921         if (req.status != CTL_ISCSI_OK) {
922                 log_errx(1, "error returned from CTL iSCSI handoff request: "
923                     "%s; dropping connection", req.error_str);
924         }
925 }
926
927 void
928 kernel_limits(const char *offload, int *max_recv_dsl, int *max_send_dsl,
929     int *max_burst_length, int *first_burst_length)
930 {
931         struct ctl_iscsi req;
932         struct ctl_iscsi_limits_params *cilp;
933
934         bzero(&req, sizeof(req));
935
936         req.type = CTL_ISCSI_LIMITS;
937         cilp = (struct ctl_iscsi_limits_params *)&(req.data.limits);
938         if (offload != NULL) {
939                 strlcpy(cilp->offload, offload, sizeof(cilp->offload));
940         }
941
942         if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) {
943                 log_err(1, "error issuing CTL_ISCSI ioctl; "
944                     "dropping connection");
945         }
946
947         if (req.status != CTL_ISCSI_OK) {
948                 log_errx(1, "error returned from CTL iSCSI limits request: "
949                     "%s; dropping connection", req.error_str);
950         }
951
952         if (cilp->max_recv_data_segment_length != 0) {
953                 *max_recv_dsl = cilp->max_recv_data_segment_length;
954                 *max_send_dsl = cilp->max_recv_data_segment_length;
955         }
956         if (cilp->max_send_data_segment_length != 0)
957                 *max_send_dsl = cilp->max_send_data_segment_length;
958         if (cilp->max_burst_length != 0)
959                 *max_burst_length = cilp->max_burst_length;
960         if (cilp->first_burst_length != 0)
961                 *first_burst_length = cilp->first_burst_length;
962         if (*max_burst_length < *first_burst_length)
963                 *first_burst_length = *max_burst_length;
964
965         if (offload != NULL) {
966                 log_debugx("Kernel limits for offload \"%s\" are "
967                     "MaxRecvDataSegment=%d, max_send_dsl=%d, "
968                     "MaxBurstLength=%d, FirstBurstLength=%d",
969                     offload, *max_recv_dsl, *max_send_dsl, *max_burst_length,
970                     *first_burst_length);
971         } else {
972                 log_debugx("Kernel limits are "
973                     "MaxRecvDataSegment=%d, max_send_dsl=%d, "
974                     "MaxBurstLength=%d, FirstBurstLength=%d",
975                     *max_recv_dsl, *max_send_dsl, *max_burst_length,
976                     *first_burst_length);
977         }
978 }
979
980 int
981 kernel_port_add(struct port *port)
982 {
983         struct option *o;
984         struct ctl_port_entry entry;
985         struct ctl_req req;
986         struct ctl_lun_map lm;
987         struct target *targ = port->p_target;
988         struct portal_group *pg = port->p_portal_group;
989         char tagstr[16];
990         int error, i, n;
991
992         /* Create iSCSI port. */
993         if (port->p_portal_group) {
994                 bzero(&req, sizeof(req));
995                 strlcpy(req.driver, "iscsi", sizeof(req.driver));
996                 req.reqtype = CTL_REQ_CREATE;
997                 req.num_args = 5;
998                 TAILQ_FOREACH(o, &pg->pg_options, o_next)
999                         req.num_args++;
1000                 req.args = malloc(req.num_args * sizeof(*req.args));
1001                 if (req.args == NULL)
1002                         log_err(1, "malloc");
1003                 n = 0;
1004                 req.args[n].namelen = sizeof("port_id");
1005                 req.args[n].name = __DECONST(char *, "port_id");
1006                 req.args[n].vallen = sizeof(port->p_ctl_port);
1007                 req.args[n].value = &port->p_ctl_port;
1008                 req.args[n++].flags = CTL_BEARG_WR;
1009                 str_arg(&req.args[n++], "cfiscsi_target", targ->t_name);
1010                 snprintf(tagstr, sizeof(tagstr), "%d", pg->pg_tag);
1011                 str_arg(&req.args[n++], "cfiscsi_portal_group_tag", tagstr);
1012                 if (targ->t_alias)
1013                         str_arg(&req.args[n++], "cfiscsi_target_alias", targ->t_alias);
1014                 str_arg(&req.args[n++], "ctld_portal_group_name", pg->pg_name);
1015                 TAILQ_FOREACH(o, &pg->pg_options, o_next)
1016                         str_arg(&req.args[n++], o->o_name, o->o_value);
1017                 req.num_args = n;
1018                 error = ioctl(ctl_fd, CTL_PORT_REQ, &req);
1019                 free(req.args);
1020                 if (error != 0) {
1021                         log_warn("error issuing CTL_PORT_REQ ioctl");
1022                         return (1);
1023                 }
1024                 if (req.status == CTL_LUN_ERROR) {
1025                         log_warnx("error returned from port creation request: %s",
1026                             req.error_str);
1027                         return (1);
1028                 }
1029                 if (req.status != CTL_LUN_OK) {
1030                         log_warnx("unknown port creation request status %d",
1031                             req.status);
1032                         return (1);
1033                 }
1034         } else if (port->p_pport) {
1035                 port->p_ctl_port = port->p_pport->pp_ctl_port;
1036
1037                 if (strncmp(targ->t_name, "naa.", 4) == 0 &&
1038                     strlen(targ->t_name) == 20) {
1039                         bzero(&entry, sizeof(entry));
1040                         entry.port_type = CTL_PORT_NONE;
1041                         entry.targ_port = port->p_ctl_port;
1042                         entry.flags |= CTL_PORT_WWNN_VALID;
1043                         entry.wwnn = strtoull(targ->t_name + 4, NULL, 16);
1044                         if (ioctl(ctl_fd, CTL_SET_PORT_WWNS, &entry) == -1)
1045                                 log_warn("CTL_SET_PORT_WWNS ioctl failed");
1046                 }
1047         }
1048
1049         /* Explicitly enable mapping to block any access except allowed. */
1050         lm.port = port->p_ctl_port;
1051         lm.plun = UINT32_MAX;
1052         lm.lun = 0;
1053         error = ioctl(ctl_fd, CTL_LUN_MAP, &lm);
1054         if (error != 0)
1055                 log_warn("CTL_LUN_MAP ioctl failed");
1056
1057         /* Map configured LUNs */
1058         for (i = 0; i < MAX_LUNS; i++) {
1059                 if (targ->t_luns[i] == NULL)
1060                         continue;
1061                 lm.port = port->p_ctl_port;
1062                 lm.plun = i;
1063                 lm.lun = targ->t_luns[i]->l_ctl_lun;
1064                 error = ioctl(ctl_fd, CTL_LUN_MAP, &lm);
1065                 if (error != 0)
1066                         log_warn("CTL_LUN_MAP ioctl failed");
1067         }
1068
1069         /* Enable port */
1070         bzero(&entry, sizeof(entry));
1071         entry.targ_port = port->p_ctl_port;
1072         error = ioctl(ctl_fd, CTL_ENABLE_PORT, &entry);
1073         if (error != 0) {
1074                 log_warn("CTL_ENABLE_PORT ioctl failed");
1075                 return (-1);
1076         }
1077
1078         return (0);
1079 }
1080
1081 int
1082 kernel_port_update(struct port *port, struct port *oport)
1083 {
1084         struct ctl_lun_map lm;
1085         struct target *targ = port->p_target;
1086         struct target *otarg = oport->p_target;
1087         int error, i;
1088         uint32_t olun;
1089
1090         /* Map configured LUNs and unmap others */
1091         for (i = 0; i < MAX_LUNS; i++) {
1092                 lm.port = port->p_ctl_port;
1093                 lm.plun = i;
1094                 if (targ->t_luns[i] == NULL)
1095                         lm.lun = UINT32_MAX;
1096                 else
1097                         lm.lun = targ->t_luns[i]->l_ctl_lun;
1098                 if (otarg->t_luns[i] == NULL)
1099                         olun = UINT32_MAX;
1100                 else
1101                         olun = otarg->t_luns[i]->l_ctl_lun;
1102                 if (lm.lun == olun)
1103                         continue;
1104                 error = ioctl(ctl_fd, CTL_LUN_MAP, &lm);
1105                 if (error != 0)
1106                         log_warn("CTL_LUN_MAP ioctl failed");
1107         }
1108         return (0);
1109 }
1110
1111 int
1112 kernel_port_remove(struct port *port)
1113 {
1114         struct ctl_port_entry entry;
1115         struct ctl_lun_map lm;
1116         struct ctl_req req;
1117         char tagstr[16];
1118         struct target *targ = port->p_target;
1119         struct portal_group *pg = port->p_portal_group;
1120         int error;
1121
1122         /* Disable port */
1123         bzero(&entry, sizeof(entry));
1124         entry.targ_port = port->p_ctl_port;
1125         error = ioctl(ctl_fd, CTL_DISABLE_PORT, &entry);
1126         if (error != 0) {
1127                 log_warn("CTL_DISABLE_PORT ioctl failed");
1128                 return (-1);
1129         }
1130
1131         /* Remove iSCSI port. */
1132         if (port->p_portal_group) {
1133                 bzero(&req, sizeof(req));
1134                 strlcpy(req.driver, "iscsi", sizeof(req.driver));
1135                 req.reqtype = CTL_REQ_REMOVE;
1136                 req.num_args = 2;
1137                 req.args = malloc(req.num_args * sizeof(*req.args));
1138                 if (req.args == NULL)
1139                         log_err(1, "malloc");
1140                 str_arg(&req.args[0], "cfiscsi_target", targ->t_name);
1141                 snprintf(tagstr, sizeof(tagstr), "%d", pg->pg_tag);
1142                 str_arg(&req.args[1], "cfiscsi_portal_group_tag", tagstr);
1143                 error = ioctl(ctl_fd, CTL_PORT_REQ, &req);
1144                 free(req.args);
1145                 if (error != 0) {
1146                         log_warn("error issuing CTL_PORT_REQ ioctl");
1147                         return (1);
1148                 }
1149                 if (req.status == CTL_LUN_ERROR) {
1150                         log_warnx("error returned from port removal request: %s",
1151                             req.error_str);
1152                         return (1);
1153                 }
1154                 if (req.status != CTL_LUN_OK) {
1155                         log_warnx("unknown port removal request status %d",
1156                             req.status);
1157                         return (1);
1158                 }
1159         } else {
1160                 /* Disable LUN mapping. */
1161                 lm.port = port->p_ctl_port;
1162                 lm.plun = UINT32_MAX;
1163                 lm.lun = UINT32_MAX;
1164                 error = ioctl(ctl_fd, CTL_LUN_MAP, &lm);
1165                 if (error != 0)
1166                         log_warn("CTL_LUN_MAP ioctl failed");
1167         }
1168         return (0);
1169 }
1170
1171 #ifdef ICL_KERNEL_PROXY
1172 void
1173 kernel_listen(struct addrinfo *ai, bool iser, int portal_id)
1174 {
1175         struct ctl_iscsi req;
1176
1177         bzero(&req, sizeof(req));
1178
1179         req.type = CTL_ISCSI_LISTEN;
1180         req.data.listen.iser = iser;
1181         req.data.listen.domain = ai->ai_family;
1182         req.data.listen.socktype = ai->ai_socktype;
1183         req.data.listen.protocol = ai->ai_protocol;
1184         req.data.listen.addr = ai->ai_addr;
1185         req.data.listen.addrlen = ai->ai_addrlen;
1186         req.data.listen.portal_id = portal_id;
1187
1188         if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1)
1189                 log_err(1, "error issuing CTL_ISCSI ioctl");
1190
1191         if (req.status != CTL_ISCSI_OK) {
1192                 log_errx(1, "error returned from CTL iSCSI listen: %s",
1193                     req.error_str);
1194         }
1195 }
1196
1197 void
1198 kernel_accept(int *connection_id, int *portal_id,
1199     struct sockaddr *client_sa, socklen_t *client_salen)
1200 {
1201         struct ctl_iscsi req;
1202         struct sockaddr_storage ss;
1203
1204         bzero(&req, sizeof(req));
1205
1206         req.type = CTL_ISCSI_ACCEPT;
1207         req.data.accept.initiator_addr = (struct sockaddr *)&ss;
1208
1209         if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1)
1210                 log_err(1, "error issuing CTL_ISCSI ioctl");
1211
1212         if (req.status != CTL_ISCSI_OK) {
1213                 log_errx(1, "error returned from CTL iSCSI accept: %s",
1214                     req.error_str);
1215         }
1216
1217         *connection_id = req.data.accept.connection_id;
1218         *portal_id = req.data.accept.portal_id;
1219         *client_salen = req.data.accept.initiator_addrlen;
1220         memcpy(client_sa, &ss, *client_salen);
1221 }
1222
1223 void
1224 kernel_send(struct pdu *pdu)
1225 {
1226         struct ctl_iscsi req;
1227
1228         bzero(&req, sizeof(req));
1229
1230         req.type = CTL_ISCSI_SEND;
1231         req.data.send.connection_id = pdu->pdu_connection->conn_socket;
1232         req.data.send.bhs = pdu->pdu_bhs;
1233         req.data.send.data_segment_len = pdu->pdu_data_len;
1234         req.data.send.data_segment = pdu->pdu_data;
1235
1236         if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) {
1237                 log_err(1, "error issuing CTL_ISCSI ioctl; "
1238                     "dropping connection");
1239         }
1240
1241         if (req.status != CTL_ISCSI_OK) {
1242                 log_errx(1, "error returned from CTL iSCSI send: "
1243                     "%s; dropping connection", req.error_str);
1244         }
1245 }
1246
1247 void
1248 kernel_receive(struct pdu *pdu)
1249 {
1250         struct connection *conn;
1251         struct ctl_iscsi req;
1252
1253         conn = pdu->pdu_connection;
1254         pdu->pdu_data = malloc(conn->conn_max_recv_data_segment_length);
1255         if (pdu->pdu_data == NULL)
1256                 log_err(1, "malloc");
1257
1258         bzero(&req, sizeof(req));
1259
1260         req.type = CTL_ISCSI_RECEIVE;
1261         req.data.receive.connection_id = conn->conn_socket;
1262         req.data.receive.bhs = pdu->pdu_bhs;
1263         req.data.receive.data_segment_len =
1264             conn->conn_max_recv_data_segment_length;
1265         req.data.receive.data_segment = pdu->pdu_data;
1266
1267         if (ioctl(ctl_fd, CTL_ISCSI, &req) == -1) {
1268                 log_err(1, "error issuing CTL_ISCSI ioctl; "
1269                     "dropping connection");
1270         }
1271
1272         if (req.status != CTL_ISCSI_OK) {
1273                 log_errx(1, "error returned from CTL iSCSI receive: "
1274                     "%s; dropping connection", req.error_str);
1275         }
1276
1277 }
1278
1279 #endif /* ICL_KERNEL_PROXY */
1280
1281 /*
1282  * XXX: I CANT INTO LATIN
1283  */
1284 void
1285 kernel_capsicate(void)
1286 {
1287         int error;
1288         cap_rights_t rights;
1289         const unsigned long cmds[] = { CTL_ISCSI };
1290
1291         cap_rights_init(&rights, CAP_IOCTL);
1292         error = cap_rights_limit(ctl_fd, &rights);
1293         if (error != 0 && errno != ENOSYS)
1294                 log_err(1, "cap_rights_limit");
1295
1296         error = cap_ioctls_limit(ctl_fd, cmds, nitems(cmds));
1297
1298         if (error != 0 && errno != ENOSYS)
1299                 log_err(1, "cap_ioctls_limit");
1300
1301         error = cap_enter();
1302         if (error != 0 && errno != ENOSYS)
1303                 log_err(1, "cap_enter");
1304
1305         if (cap_sandboxed())
1306                 log_debugx("Capsicum capability mode enabled");
1307         else
1308                 log_warnx("Capsicum capability mode not supported");
1309 }
1310