]> CyberLeo.Net >> Repos - FreeBSD/releng/8.0.git/blob - sbin/geom/class/part/geom_part.c
Adjust to reflect 8.0-RELEASE.
[FreeBSD/releng/8.0.git] / sbin / geom / class / part / geom_part.c
1 /*-
2  * Copyright (c) 2007, 2008 Marcel Moolenaar
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include <sys/stat.h>
31
32 #include <assert.h>
33 #include <err.h>
34 #include <errno.h>
35 #include <fcntl.h>
36 #include <libgeom.h>
37 #include <libutil.h>
38 #include <paths.h>
39 #include <stdint.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <strings.h>
44 #include <unistd.h>
45
46 #include "core/geom.h"
47 #include "misc/subr.h"
48
49 #ifdef STATIC_GEOM_CLASSES
50 #define PUBSYM(x)       gpart_##x
51 #else
52 #define PUBSYM(x)       x
53 #endif
54
55 uint32_t PUBSYM(lib_version) = G_LIB_VERSION;
56 uint32_t PUBSYM(version) = 0;
57
58 static char autofill[] = "*";
59 static char optional[] = "";
60 static char flags[] = "C";
61
62 static char bootcode_param[] = "bootcode";
63 static char index_param[] = "index";
64 static char partcode_param[] = "partcode";
65
66 static void gpart_bootcode(struct gctl_req *, unsigned int);
67 static void gpart_issue(struct gctl_req *, unsigned int);
68 static void gpart_show(struct gctl_req *, unsigned int);
69
70 struct g_command PUBSYM(class_commands)[] = {
71         { "add", 0, gpart_issue, {
72                 { 'b', "start", autofill, G_TYPE_ASCLBA },
73                 { 's', "size", autofill, G_TYPE_ASCLBA },
74                 { 't', "type", NULL, G_TYPE_STRING },
75                 { 'i', index_param, optional, G_TYPE_ASCNUM },
76                 { 'l', "label", optional, G_TYPE_STRING },
77                 { 'f', "flags", flags, G_TYPE_STRING },
78                 G_OPT_SENTINEL },
79           "geom", NULL
80         },
81         { "bootcode", 0, gpart_bootcode, {
82                 { 'b', bootcode_param, optional, G_TYPE_STRING },
83                 { 'p', partcode_param, optional, G_TYPE_STRING },
84                 { 'i', index_param, optional, G_TYPE_ASCNUM },
85                 { 'f', "flags", flags, G_TYPE_STRING },
86                 G_OPT_SENTINEL },
87           "geom", NULL
88         },
89         { "commit", 0, gpart_issue, G_NULL_OPTS, "geom", NULL },
90         { "create", 0, gpart_issue, {
91                 { 's', "scheme", NULL, G_TYPE_STRING },
92                 { 'n', "entries", optional, G_TYPE_ASCNUM },
93                 { 'f', "flags", flags, G_TYPE_STRING },
94                 G_OPT_SENTINEL },
95           "provider", NULL
96         },
97         { "delete", 0, gpart_issue, {
98                 { 'i', index_param, NULL, G_TYPE_ASCNUM },
99                 { 'f', "flags", flags, G_TYPE_STRING },
100                 G_OPT_SENTINEL },
101           "geom", NULL
102         },
103         { "destroy", 0, gpart_issue, {
104                 { 'f', "flags", flags, G_TYPE_STRING },
105                 G_OPT_SENTINEL },
106           "geom", NULL },
107         { "modify", 0, gpart_issue, {
108                 { 'i', index_param, NULL, G_TYPE_ASCNUM },
109                 { 'l', "label", optional, G_TYPE_STRING },
110                 { 't', "type", optional, G_TYPE_STRING },
111                 { 'f', "flags", flags, G_TYPE_STRING },
112                 G_OPT_SENTINEL },
113           "geom", NULL
114         },
115         { "set", 0, gpart_issue, {
116                 { 'a', "attrib", NULL, G_TYPE_STRING },
117                 { 'i', index_param, NULL, G_TYPE_ASCNUM },
118                 { 'f', "flags", flags, G_TYPE_STRING },
119                 G_OPT_SENTINEL },
120           "geom", NULL
121         },
122         { "show", 0, gpart_show, {
123                 { 'l', "show_label", NULL, G_TYPE_BOOL },
124                 { 'r', "show_rawtype", NULL, G_TYPE_BOOL },
125                 G_OPT_SENTINEL },
126           NULL, "[-lr] [geom ...]"
127         },
128         { "undo", 0, gpart_issue, G_NULL_OPTS, "geom", NULL },
129         { "unset", 0, gpart_issue, {
130                 { 'a', "attrib", NULL, G_TYPE_STRING },
131                 { 'i', index_param, NULL, G_TYPE_ASCNUM },
132                 { 'f', "flags", flags, G_TYPE_STRING },
133                 G_OPT_SENTINEL },
134           "geom", NULL
135         },
136         G_CMD_SENTINEL
137 };
138
139 static struct gclass *
140 find_class(struct gmesh *mesh, const char *name)
141 {
142         struct gclass *classp;
143
144         LIST_FOREACH(classp, &mesh->lg_class, lg_class) {
145                 if (strcmp(classp->lg_name, name) == 0)
146                         return (classp);
147         }
148         return (NULL);
149 }
150
151 static struct ggeom *
152 find_geom(struct gclass *classp, const char *name)
153 {
154         struct ggeom *gp;
155
156         LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
157                 if (strcmp(gp->lg_name, name) == 0)
158                         return (gp);
159         }
160         return (NULL);
161 }
162
163 static const char *
164 find_geomcfg(struct ggeom *gp, const char *cfg)
165 {
166         struct gconfig *gc;
167
168         LIST_FOREACH(gc, &gp->lg_config, lg_config) {
169                 if (!strcmp(gc->lg_name, cfg))
170                         return (gc->lg_val);
171         }
172         return (NULL);
173 }
174
175 static const char *
176 find_provcfg(struct gprovider *pp, const char *cfg)
177 {
178         struct gconfig *gc;
179
180         LIST_FOREACH(gc, &pp->lg_config, lg_config) {
181                 if (!strcmp(gc->lg_name, cfg))
182                         return (gc->lg_val);
183         }
184         return (NULL);
185 }
186
187 static struct gprovider *
188 find_provider(struct ggeom *gp, unsigned long long minsector)
189 {
190         struct gprovider *pp, *bestpp;
191         const char *s;
192         unsigned long long sector, bestsector;
193
194         bestpp = NULL;
195         LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
196                 s = find_provcfg(pp, "start");
197                 if (s == NULL) {
198                         s = find_provcfg(pp, "offset");
199                         sector = atoll(s) / pp->lg_sectorsize;
200                 } else
201                         sector = atoll(s);
202
203                 if (sector < minsector)
204                         continue;
205                 if (bestpp != NULL && sector >= bestsector)
206                         continue;
207
208                 bestpp = pp;
209                 bestsector = sector;
210         }
211         return (bestpp);
212 }
213
214 static const char *
215 fmtsize(int64_t rawsz)
216 {
217         static char buf[5];
218
219         humanize_number(buf, sizeof(buf), rawsz, "", HN_AUTOSCALE,
220             HN_B | HN_NOSPACE | HN_DECIMAL);
221         return (buf);
222 }
223
224 static const char *
225 fmtattrib(struct gprovider *pp)
226 {
227         static char buf[128];
228         struct gconfig *gc;
229         u_int idx;
230
231         buf[0] = '\0';
232         idx = 0;
233         LIST_FOREACH(gc, &pp->lg_config, lg_config) {
234                 if (strcmp(gc->lg_name, "attrib") != 0)
235                         continue;
236                 idx += snprintf(buf + idx, sizeof(buf) - idx, "%s%s",
237                     (idx == 0) ? " [" : ",", gc->lg_val);
238         }
239         if (idx > 0)
240                 snprintf(buf + idx, sizeof(buf) - idx, "] ");
241         return (buf);
242 }
243
244 static int
245 gpart_autofill(struct gctl_req *req)
246 {
247         struct gmesh mesh;
248         struct gclass *cp;
249         struct ggeom *gp;
250         struct gprovider *pp;
251         unsigned long long first, last;
252         unsigned long long size, start;
253         unsigned long long lba, len, grade;
254         const char *s;
255         char *val;
256         int error, has_size, has_start;
257
258         s = gctl_get_ascii(req, "verb");
259         if (strcmp(s, "add") != 0)
260                 return (0);
261
262         s = gctl_get_ascii(req, "size");
263         has_size = (*s == '*') ? 0 : 1;
264         size = (has_size) ? (unsigned long long)atoll(s) : 0ULL;
265
266         s = gctl_get_ascii(req, "start");
267         has_start = (*s == '*') ? 0 : 1;
268         start = (has_start) ? (unsigned long long)atoll(s) : ~0ULL;
269
270         /* No autofill necessary. */
271         if (has_size && has_start)
272                 return (0);
273
274         error = geom_gettree(&mesh);
275         if (error)
276                 return (error);
277         s = gctl_get_ascii(req, "class");
278         if (s == NULL)
279                 abort();
280         cp = find_class(&mesh, s);
281         if (cp == NULL)
282                 errx(EXIT_FAILURE, "Class %s not found.", s);
283         s = gctl_get_ascii(req, "geom");
284         if (s == NULL)
285                 abort();
286         gp = find_geom(cp, s);
287         if (gp == NULL)
288                 errx(EXIT_FAILURE, "No such geom: %s.", s);
289         first = atoll(find_geomcfg(gp, "first"));
290         last = atoll(find_geomcfg(gp, "last"));
291         grade = ~0ULL;
292         while ((pp = find_provider(gp, first)) != NULL) {
293                 s = find_provcfg(pp, "start");
294                 if (s == NULL) {
295                         s = find_provcfg(pp, "offset");
296                         lba = atoll(s) / pp->lg_sectorsize;
297                 } else
298                         lba = atoll(s);
299
300                 if (first < lba) {
301                         /* Free space [first, lba> */
302                         len = lba - first;
303                         if (has_size) {
304                                 if (len >= size && len - size < grade) {
305                                         start = first;
306                                         grade = len - size;
307                                 }
308                         } else if (has_start) {
309                                 if (start >= first && start < lba) {
310                                         size = lba - start;
311                                         grade = start - first;
312                                 }
313                         } else {
314                                 if (grade == ~0ULL || len > size) {
315                                         start = first;
316                                         size = len;
317                                         grade = 0;
318                                 }
319                         }
320                 }
321
322                 s = find_provcfg(pp, "end");
323                 if (s == NULL) {
324                         s = find_provcfg(pp, "length");
325                         first = lba + atoll(s) / pp->lg_sectorsize;
326                 } else
327                         first = atoll(s) + 1;
328         }
329         if (first <= last) {
330                 /* Free space [first-last] */
331                 len = last - first + 1;
332                 if (has_size) {
333                         if (len >= size && len - size < grade) {
334                                 start = first;
335                                 grade = len - size;
336                         }
337                 } else if (has_start) {
338                         if (start >= first && start <= last) {
339                                 size = last - start + 1;
340                                 grade = start - first;
341                         }
342                 } else {
343                         if (grade == ~0ULL || len > size) {
344                                 start = first;
345                                 size = len;
346                                 grade = 0;
347                         }
348                 }
349         }
350
351         if (grade == ~0ULL)
352                 return (ENOSPC);
353
354         if (!has_size) {
355                 asprintf(&val, "%llu", size);
356                 if (val == NULL)
357                         return (ENOMEM);
358                 gctl_change_param(req, "size", -1, val);
359         }
360         if (!has_start) {
361                 asprintf(&val, "%llu", start);
362                 if (val == NULL)
363                         return (ENOMEM);
364                 gctl_change_param(req, "start", -1, val);
365         }
366         return (0);
367 }
368
369 static void
370 gpart_show_geom(struct ggeom *gp, const char *element)
371 {
372         struct gprovider *pp;
373         const char *s, *scheme;
374         unsigned long long first, last, sector, end;
375         unsigned long long length, secsz;
376         int idx, wblocks, wname;
377
378         scheme = find_geomcfg(gp, "scheme");
379         s = find_geomcfg(gp, "first");
380         first = atoll(s);
381         s = find_geomcfg(gp, "last");
382         last = atoll(s);
383         wblocks = strlen(s);
384         wname = strlen(gp->lg_name);
385         pp = LIST_FIRST(&gp->lg_consumer)->lg_provider;
386         secsz = pp->lg_sectorsize;
387         printf("=>%*llu  %*llu  %*s  %s  (%s)\n",
388             wblocks, first, wblocks, (last - first + 1),
389             wname, gp->lg_name,
390             scheme, fmtsize(pp->lg_mediasize));
391
392         while ((pp = find_provider(gp, first)) != NULL) {
393                 s = find_provcfg(pp, "start");
394                 if (s == NULL) {
395                         s = find_provcfg(pp, "offset");
396                         sector = atoll(s) / secsz;
397                 } else
398                         sector = atoll(s);
399
400                 s = find_provcfg(pp, "end");
401                 if (s == NULL) {
402                         s = find_provcfg(pp, "length");
403                         length = atoll(s) / secsz;
404                         end = sector + length - 1;
405                 } else {
406                         end = atoll(s);
407                         length = end - sector + 1;
408                 }
409                 s = find_provcfg(pp, "index");
410                 idx = atoi(s);
411                 if (first < sector) {
412                         printf("  %*llu  %*llu  %*s  - free -  (%s)\n",
413                             wblocks, first, wblocks, sector - first,
414                             wname, "",
415                             fmtsize((sector - first) * secsz));
416                 }
417                 printf("  %*llu  %*llu  %*d  %s %s (%s)\n",
418                     wblocks, sector, wblocks, length,
419                     wname, idx, find_provcfg(pp, element),
420                     fmtattrib(pp), fmtsize(pp->lg_mediasize));
421                 first = end + 1;
422         }
423         if (first <= last) {
424                 length = last - first + 1;
425                 printf("  %*llu  %*llu  %*s  - free -  (%s)\n",
426                     wblocks, first, wblocks, length,
427                     wname, "",
428                     fmtsize(length * secsz));
429         }
430         printf("\n");
431 }
432
433 static int
434 gpart_show_hasopt(struct gctl_req *req, const char *opt, const char *elt)
435 {
436
437         if (!gctl_get_int(req, opt))
438                 return (0);
439
440         if (elt != NULL)
441                 errx(EXIT_FAILURE, "-l and -r are mutually exclusive");
442
443         return (1);
444 }
445
446 static void
447 gpart_show(struct gctl_req *req, unsigned int fl __unused)
448 {
449         struct gmesh mesh;
450         struct gclass *classp;
451         struct ggeom *gp;
452         const char *element, *name;
453         int error, i, nargs;
454
455         element = NULL;
456         if (gpart_show_hasopt(req, "show_label", element))
457                 element = "label";
458         if (gpart_show_hasopt(req, "show_rawtype", element))
459                 element = "rawtype";
460         if (element == NULL)
461                 element = "type";
462
463         name = gctl_get_ascii(req, "class");
464         if (name == NULL)
465                 abort();
466         error = geom_gettree(&mesh);
467         if (error != 0)
468                 errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
469         classp = find_class(&mesh, name);
470         if (classp == NULL) {
471                 geom_deletetree(&mesh);
472                 errx(EXIT_FAILURE, "Class %s not found.", name);
473         }
474         nargs = gctl_get_int(req, "nargs");
475         if (nargs > 0) {
476                 for (i = 0; i < nargs; i++) {
477                         name = gctl_get_ascii(req, "arg%d", i);
478                         gp = find_geom(classp, name);
479                         if (gp != NULL)
480                                 gpart_show_geom(gp, element);
481                         else
482                                 errx(EXIT_FAILURE, "No such geom: %s.", name);
483                 }
484         } else {
485                 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
486                         gpart_show_geom(gp, element);
487                 }
488         }
489         geom_deletetree(&mesh);
490 }
491
492 static void *
493 gpart_bootfile_read(const char *bootfile, ssize_t *size)
494 {
495         struct stat sb;
496         void *code;
497         int fd;
498
499         if (stat(bootfile, &sb) == -1)
500                 err(EXIT_FAILURE, "%s", bootfile);
501         if (!S_ISREG(sb.st_mode))
502                 errx(EXIT_FAILURE, "%s: not a regular file", bootfile);
503         if (sb.st_size == 0)
504                 errx(EXIT_FAILURE, "%s: empty file", bootfile);
505         if (*size > 0 && sb.st_size >= *size)
506                 errx(EXIT_FAILURE, "%s: file too big (%zu limit)", bootfile,
507                     *size);
508
509         *size = sb.st_size;
510
511         fd = open(bootfile, O_RDONLY);
512         if (fd == -1)
513                 err(EXIT_FAILURE, "%s", bootfile);
514         code = malloc(*size);
515         if (code == NULL)
516                 err(EXIT_FAILURE, NULL);
517         if (read(fd, code, *size) != *size)
518                 err(EXIT_FAILURE, "%s", bootfile);
519         close(fd);
520
521         return (code);
522 }
523
524 static void
525 gpart_write_partcode(struct gctl_req *req, int idx, void *code, ssize_t size)
526 {
527         char dsf[128];
528         struct gmesh mesh;
529         struct gclass *classp;
530         struct ggeom *gp;
531         struct gprovider *pp;
532         const char *s;
533         char *buf;
534         off_t bsize;
535         int error, fd;
536
537         s = gctl_get_ascii(req, "class");
538         if (s == NULL)
539                 abort();
540         error = geom_gettree(&mesh);
541         if (error != 0)
542                 errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
543         classp = find_class(&mesh, s);
544         if (classp == NULL) {
545                 geom_deletetree(&mesh);
546                 errx(EXIT_FAILURE, "Class %s not found.", s);
547         }
548         s = gctl_get_ascii(req, "geom");
549         if (s == NULL)
550                 abort();
551         gp = find_geom(classp, s);
552         if (gp == NULL)
553                 errx(EXIT_FAILURE, "No such geom: %s.", s);
554
555         LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
556                 s = find_provcfg(pp, "index");
557                 if (s == NULL)
558                         continue;
559                 if (atoi(s) == idx)
560                         break;
561         }
562
563         if (pp != NULL) {
564                 snprintf(dsf, sizeof(dsf), "/dev/%s", pp->lg_name);
565                 fd = open(dsf, O_WRONLY);
566                 if (fd == -1)
567                         err(EXIT_FAILURE, "%s", dsf);
568                 if (lseek(fd, size, SEEK_SET) != size)
569                         errx(EXIT_FAILURE, "%s: not enough space", dsf);
570                 if (lseek(fd, 0, SEEK_SET) != 0)
571                         err(EXIT_FAILURE, "%s", dsf);
572
573                 /*
574                  * When writing to a disk device, the write must be
575                  * sector aligned and not write to any partial sectors,
576                  * so round up the buffer size to the next sector and zero it.
577                  */
578                 bsize = (size + pp->lg_sectorsize - 1) /
579                     pp->lg_sectorsize * pp->lg_sectorsize;
580                 buf = calloc(1, bsize);
581                 if (buf == NULL)
582                         err(EXIT_FAILURE, "%s", dsf);
583                 bcopy(code, buf, size);
584                 if (write(fd, buf, bsize) != bsize)
585                         err(EXIT_FAILURE, "%s", dsf);
586                 free(buf);
587                 close(fd);
588         } else
589                 errx(EXIT_FAILURE, "invalid partition index");
590
591         geom_deletetree(&mesh);
592 }
593
594 static void
595 gpart_bootcode(struct gctl_req *req, unsigned int fl)
596 {
597         const char *s;
598         char *sp;
599         void *bootcode, *partcode;
600         size_t bootsize, partsize;
601         int error, idx;
602
603         if (gctl_has_param(req, bootcode_param)) {
604                 s = gctl_get_ascii(req, bootcode_param);
605                 bootsize = 64 * 1024;           /* Arbitrary limit. */
606                 bootcode = gpart_bootfile_read(s, &bootsize);
607                 error = gctl_change_param(req, bootcode_param, bootsize,
608                     bootcode);
609                 if (error)
610                         errc(EXIT_FAILURE, error, "internal error");
611         } else {
612                 bootcode = NULL;
613                 bootsize = 0;
614         }
615
616         if (gctl_has_param(req, partcode_param)) {
617                 s = gctl_get_ascii(req, partcode_param);
618                 partsize = bootsize * 1024;
619                 partcode = gpart_bootfile_read(s, &partsize);
620                 error = gctl_delete_param(req, partcode_param);
621                 if (error)
622                         errc(EXIT_FAILURE, error, "internal error");
623         } else {
624                 partcode = NULL;
625                 partsize = 0;
626         }
627
628         if (gctl_has_param(req, index_param)) {
629                 if (partcode == NULL)
630                         errx(EXIT_FAILURE, "-i is only valid with -p");
631                 s = gctl_get_ascii(req, index_param);
632                 idx = strtol(s, &sp, 10);
633                 if (idx < 1 || *s == '\0' || *sp != '\0')
634                         errx(EXIT_FAILURE, "invalid partition index");
635                 error = gctl_delete_param(req, index_param);
636                 if (error)
637                         errc(EXIT_FAILURE, error, "internal error");
638         } else
639                 idx = 0;
640
641         if (partcode != NULL) {
642                 if (idx == 0)
643                         errx(EXIT_FAILURE, "missing -i option");
644                 gpart_write_partcode(req, idx, partcode, partsize);
645         } else {
646                 if (bootcode == NULL)
647                         errx(EXIT_FAILURE, "no -b nor -p");
648         }
649
650         if (bootcode != NULL)
651                 gpart_issue(req, fl);
652 }
653
654 static void
655 gpart_issue(struct gctl_req *req, unsigned int fl __unused)
656 {
657         char buf[4096];
658         char *errmsg;
659         const char *errstr;
660         int error, status;
661
662         /* autofill parameters (if applicable). */
663         error = gpart_autofill(req);
664         if (error) {
665                 warnc(error, "autofill");
666                 status = EXIT_FAILURE;
667                 goto done;
668         }
669
670         bzero(buf, sizeof(buf));
671         gctl_rw_param(req, "output", sizeof(buf), buf);
672         errstr = gctl_issue(req);
673         if (errstr == NULL || errstr[0] == '\0') {
674                 if (buf[0] != '\0')
675                         printf("%s", buf);
676                 status = EXIT_SUCCESS;
677                 goto done;
678         }
679
680         error = strtol(errstr, &errmsg, 0);
681         if (errmsg != errstr) {
682                 while (errmsg[0] == ' ')
683                         errmsg++;
684                 if (errmsg[0] != '\0')
685                         warnc(error, "%s", errmsg);
686                 else
687                         warnc(error, NULL);
688         } else
689                 warnx("%s", errmsg);
690
691         status = EXIT_FAILURE;
692
693  done:
694         gctl_free(req);
695         exit(status);
696 }