kill tsol ("Trusted Solaris") aka TX ("Trusted Extensions")
[unleashed.git] / usr / src / cmd / zoneadm / zoneadm.c
blob0165091ef8c6ca43071922a1fe4462c4fdad8f47
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright 2014 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2015 by Delphix. All rights reserved.
26 * Copyright (c) 2015 Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
30 * zoneadm is a command interpreter for zone administration. It is all in
31 * C (i.e., no lex/yacc), and all the argument passing is argc/argv based.
32 * main() calls parse_and_run() which calls cmd_match(), then invokes the
33 * appropriate command's handler function. The rest of the program is the
34 * handler functions and their helper functions.
36 * Some of the helper functions are used largely to simplify I18N: reducing
37 * the need for translation notes. This is particularly true of many of
38 * the zerror() calls: doing e.g. zerror(gettext("%s failed"), "foo") rather
39 * than zerror(gettext("foo failed")) with a translation note indicating
40 * that "foo" need not be translated.
43 #include <stdio.h>
44 #include <errno.h>
45 #include <unistd.h>
46 #include <signal.h>
47 #include <stdarg.h>
48 #include <ctype.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <wait.h>
52 #include <zone.h>
53 #include <priv.h>
54 #include <locale.h>
55 #include <libintl.h>
56 #include <libzonecfg.h>
57 #include <bsm/adt.h>
58 #include <sys/brand.h>
59 #include <sys/param.h>
60 #include <sys/types.h>
61 #include <sys/stat.h>
62 #include <sys/statvfs.h>
63 #include <assert.h>
64 #include <sys/sockio.h>
65 #include <sys/mntent.h>
66 #include <limits.h>
67 #include <dirent.h>
68 #include <uuid/uuid.h>
69 #include <fcntl.h>
70 #include <door.h>
71 #include <macros.h>
72 #include <libgen.h>
73 #include <fnmatch.h>
74 #include <sys/modctl.h>
75 #include <libbrand.h>
76 #include <libscf.h>
77 #include <procfs.h>
78 #include <strings.h>
79 #include <pool.h>
80 #include <sys/pool.h>
81 #include <sys/priocntl.h>
82 #include <sys/fsspriocntl.h>
83 #include <libdladm.h>
84 #include <libdllink.h>
85 #include <pwd.h>
86 #include <auth_list.h>
87 #include <auth_attr.h>
88 #include <secdb.h>
90 #include "zoneadm.h"
92 #define MAXARGS 8
93 #define SOURCE_ZONE (CMD_MAX + 1)
95 /* Reflects kernel zone entries */
96 typedef struct zone_entry {
97 zoneid_t zid;
98 char zname[ZONENAME_MAX];
99 char *zstate_str;
100 zone_state_t zstate_num;
101 char zbrand[MAXNAMELEN];
102 char zroot[MAXPATHLEN];
103 char zuuid[UUID_PRINTABLE_STRING_LENGTH];
104 zone_iptype_t ziptype;
105 } zone_entry_t;
107 #define CLUSTER_BRAND_NAME "cluster"
109 static zone_entry_t *zents;
110 static size_t nzents;
112 #define LOOPBACK_IF "lo0"
113 #define SOCKET_AF(af) (((af) == AF_UNSPEC) ? AF_INET : (af))
115 struct net_if {
116 char *name;
117 int af;
120 /* 0755 is the default directory mode. */
121 #define DEFAULT_DIR_MODE \
122 (S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)
124 struct cmd {
125 uint_t cmd_num; /* command number */
126 char *cmd_name; /* command name */
127 char *short_usage; /* short form help */
128 int (*handler)(int argc, char *argv[]); /* function to call */
132 #define SHELP_HELP "help"
133 #define SHELP_BOOT "boot [-- boot_arguments]"
134 #define SHELP_HALT "halt"
135 #define SHELP_READY "ready"
136 #define SHELP_SHUTDOWN "shutdown [-r [-- boot_arguments]]"
137 #define SHELP_REBOOT "reboot [-- boot_arguments]"
138 #define SHELP_LIST "list [-cipv]"
139 #define SHELP_VERIFY "verify"
140 #define SHELP_INSTALL "install [brand-specific args]"
141 #define SHELP_UNINSTALL "uninstall [-F] [brand-specific args]"
142 #define SHELP_CLONE "clone [-m method] [-s <ZFS snapshot>] "\
143 "[brand-specific args] zonename"
144 #define SHELP_MOVE "move zonepath"
145 #define SHELP_DETACH "detach [-n] [brand-specific args]"
146 #define SHELP_ATTACH "attach [-F] [-n <path>] [brand-specific args]"
147 #define SHELP_MARK "mark incomplete"
149 #define EXEC_PREFIX "exec "
150 #define EXEC_LEN (strlen(EXEC_PREFIX))
151 #define RMCOMMAND "/usr/bin/rm -rf"
153 static int cleanup_zonepath(char *, boolean_t);
156 static int help_func(int argc, char *argv[]);
157 static int ready_func(int argc, char *argv[]);
158 static int boot_func(int argc, char *argv[]);
159 static int shutdown_func(int argc, char *argv[]);
160 static int halt_func(int argc, char *argv[]);
161 static int reboot_func(int argc, char *argv[]);
162 static int list_func(int argc, char *argv[]);
163 static int verify_func(int argc, char *argv[]);
164 static int install_func(int argc, char *argv[]);
165 static int uninstall_func(int argc, char *argv[]);
166 static int mount_func(int argc, char *argv[]);
167 static int unmount_func(int argc, char *argv[]);
168 static int clone_func(int argc, char *argv[]);
169 static int move_func(int argc, char *argv[]);
170 static int detach_func(int argc, char *argv[]);
171 static int attach_func(int argc, char *argv[]);
172 static int mark_func(int argc, char *argv[]);
173 static int apply_func(int argc, char *argv[]);
174 static int sysboot_func(int argc, char *argv[]);
175 static int sanity_check(char *zone, int cmd_num, boolean_t running,
176 boolean_t unsafe_when_running, boolean_t force);
177 static int cmd_match(char *cmd);
178 static int verify_details(int, char *argv[]);
179 static int verify_brand(zone_dochandle_t, int, char *argv[]);
180 static int invoke_brand_handler(int, char *argv[]);
182 static struct cmd cmdtab[] = {
183 { CMD_HELP, "help", SHELP_HELP, help_func },
184 { CMD_BOOT, "boot", SHELP_BOOT, boot_func },
185 { CMD_HALT, "halt", SHELP_HALT, halt_func },
186 { CMD_READY, "ready", SHELP_READY, ready_func },
187 { CMD_SHUTDOWN, "shutdown", SHELP_SHUTDOWN, shutdown_func },
188 { CMD_REBOOT, "reboot", SHELP_REBOOT, reboot_func },
189 { CMD_LIST, "list", SHELP_LIST, list_func },
190 { CMD_VERIFY, "verify", SHELP_VERIFY, verify_func },
191 { CMD_INSTALL, "install", SHELP_INSTALL, install_func },
192 { CMD_UNINSTALL, "uninstall", SHELP_UNINSTALL,
193 uninstall_func },
194 /* mount and unmount are private commands for admin/install */
195 { CMD_MOUNT, "mount", NULL, mount_func },
196 { CMD_UNMOUNT, "umount", NULL, unmount_func },
197 { CMD_UNMOUNT, "unmount", NULL, unmount_func },
198 { CMD_CLONE, "clone", SHELP_CLONE, clone_func },
199 { CMD_MOVE, "move", SHELP_MOVE, move_func },
200 { CMD_DETACH, "detach", SHELP_DETACH, detach_func },
201 { CMD_ATTACH, "attach", SHELP_ATTACH, attach_func },
202 { CMD_MARK, "mark", SHELP_MARK, mark_func },
203 { CMD_APPLY, "apply", NULL, apply_func },
204 { CMD_SYSBOOT, "sysboot", NULL, sysboot_func }
207 /* global variables */
209 /* set early in main(), never modified thereafter, used all over the place */
210 static char *execname;
211 static char target_brand[MAXNAMELEN];
212 static char default_brand[MAXPATHLEN];
213 static char *locale;
214 char *target_zone;
215 static char *target_uuid;
216 char *username;
218 char *
219 cmd_to_str(int cmd_num)
221 assert(cmd_num >= CMD_MIN && cmd_num <= CMD_MAX);
222 return (cmdtab[cmd_num].cmd_name);
225 /* This is a separate function because of gettext() wrapping. */
226 static char *
227 long_help(int cmd_num)
229 assert(cmd_num >= CMD_MIN && cmd_num <= CMD_MAX);
230 switch (cmd_num) {
231 case CMD_HELP:
232 return (gettext("Print usage message."));
233 case CMD_BOOT:
234 return (gettext("Activates (boots) specified zone. See "
235 "zoneadm(1m) for valid boot\n\targuments."));
236 case CMD_HALT:
237 return (gettext("Halts specified zone, bypassing shutdown "
238 "scripts and removing runtime\n\tresources of the zone."));
239 case CMD_READY:
240 return (gettext("Prepares a zone for running applications but "
241 "does not start any user\n\tprocesses in the zone."));
242 case CMD_SHUTDOWN:
243 return (gettext("Gracefully shutdown the zone or reboot if "
244 "the '-r' option is specified.\n\t"
245 "See zoneadm(1m) for valid boot arguments."));
246 case CMD_REBOOT:
247 return (gettext("Restarts the zone (equivalent to a halt / "
248 "boot sequence).\n\tFails if the zone is not active. "
249 "See zoneadm(1m) for valid boot\n\targuments."));
250 case CMD_LIST:
251 return (gettext("Lists the current zones, or a "
252 "specific zone if indicated. By default,\n\tall "
253 "running zones are listed, though this can be "
254 "expanded to all\n\tinstalled zones with the -i "
255 "option or all configured zones with the\n\t-c "
256 "option. When used with the general -z <zone> and/or -u "
257 "<uuid-match>\n\toptions, lists only the specified "
258 "matching zone, but lists it\n\tregardless of its state, "
259 "and the -i and -c options are disallowed. The\n\t-v "
260 "option can be used to display verbose information: zone "
261 "name, id,\n\tcurrent state, root directory and options. "
262 "The -p option can be used\n\tto request machine-parsable "
263 "output. The -v and -p options are mutually\n\texclusive."
264 " If neither -v nor -p is used, just the zone name is "
265 "listed."));
266 case CMD_VERIFY:
267 return (gettext("Check to make sure the configuration "
268 "can safely be instantiated\n\ton the machine: "
269 "physical network interfaces exist, etc."));
270 case CMD_INSTALL:
271 return (gettext("Install the configuration on to the system. "
272 "All arguments are passed to the brand installation "
273 "function;\n\tsee brands(5) for more information."));
274 case CMD_UNINSTALL:
275 return (gettext("Uninstall the configuration from the system. "
276 "The -F flag can be used\n\tto force the action. All "
277 "other arguments are passed to the brand\n\tuninstall "
278 "function; see brands(5) for more information."));
279 case CMD_CLONE:
280 return (gettext("Clone the installation of another zone. "
281 "The -m option can be used to\n\tspecify 'copy' which "
282 "forces a copy of the source zone. The -s option\n\t"
283 "can be used to specify the name of a ZFS snapshot "
284 "that was taken from\n\ta previous clone command. The "
285 "snapshot will be used as the source\n\tinstead of "
286 "creating a new ZFS snapshot. All other arguments are "
287 "passed\n\tto the brand clone function; see "
288 "brands(5) for more information."));
289 case CMD_MOVE:
290 return (gettext("Move the zone to a new zonepath."));
291 case CMD_DETACH:
292 return (gettext("Detach the zone from the system. The zone "
293 "state is changed to\n\t'configured' (but the files under "
294 "the zonepath are untouched).\n\tThe zone can subsequently "
295 "be attached, or can be moved to another\n\tsystem and "
296 "attached there. The -n option can be used to specify\n\t"
297 "'no-execute' mode. When -n is used, the information "
298 "needed to attach\n\tthe zone is sent to standard output "
299 "but the zone is not actually\n\tdetached. All other "
300 "arguments are passed to the brand detach function;\n\tsee "
301 "brands(5) for more information."));
302 case CMD_ATTACH:
303 return (gettext("Attach the zone to the system. The zone "
304 "state must be 'configured'\n\tprior to attach; upon "
305 "successful completion, the zone state will be\n\t"
306 "'installed'. The system software on the current "
307 "system must be\n\tcompatible with the software on the "
308 "zone's original system.\n\tSpecify -F "
309 "to force the attach and skip software compatibility "
310 "tests.\n\tThe -n option can be used to specify "
311 "'no-execute' mode. When -n is\n\tused, the information "
312 "needed to attach the zone is read from the\n\tspecified "
313 "path and the configuration is only validated. The path "
314 "can\n\tbe '-' to specify standard input. The -F and -n "
315 "options are mutually\n\texclusive. All other arguments "
316 "are passed to the brand attach\n\tfunction; see "
317 "brands(5) for more information."));
318 case CMD_MARK:
319 return (gettext("Set the state of the zone. This can be used "
320 "to force the zone\n\tstate to 'incomplete' "
321 "administratively if some activity has rendered\n\tthe "
322 "zone permanently unusable. The only valid state that "
323 "may be\n\tspecified is 'incomplete'."));
324 default:
325 return ("");
327 /* NOTREACHED */
328 return (NULL);
332 * Called with explicit B_TRUE when help is explicitly requested, B_FALSE for
333 * unexpected errors.
336 static int
337 usage(boolean_t explicit)
339 int i;
340 FILE *fd = explicit ? stdout : stderr;
342 (void) fprintf(fd, "%s:\t%s help\n", gettext("usage"), execname);
343 (void) fprintf(fd, "\t%s [-z <zone>] [-u <uuid-match>] list\n",
344 execname);
345 (void) fprintf(fd, "\t%s {-z <zone>|-u <uuid-match>} <%s>\n", execname,
346 gettext("subcommand"));
347 (void) fprintf(fd, "\n%s:\n\n", gettext("Subcommands"));
348 for (i = CMD_MIN; i <= CMD_MAX; i++) {
349 if (cmdtab[i].short_usage == NULL)
350 continue;
351 (void) fprintf(fd, "%s\n", cmdtab[i].short_usage);
352 if (explicit)
353 (void) fprintf(fd, "\t%s\n\n", long_help(i));
355 if (!explicit)
356 (void) fputs("\n", fd);
357 return (Z_USAGE);
360 static void
361 sub_usage(char *short_usage, int cmd_num)
363 (void) fprintf(stderr, "%s:\t%s\n", gettext("usage"), short_usage);
364 (void) fprintf(stderr, "\t%s\n", long_help(cmd_num));
368 * zperror() is like perror(3c) except that this also prints the executable
369 * name at the start of the message, and takes a boolean indicating whether
370 * to call libc'c strerror() or that from libzonecfg.
373 void
374 zperror(const char *str, boolean_t zonecfg_error)
376 (void) fprintf(stderr, "%s: %s: %s\n", execname, str,
377 zonecfg_error ? zonecfg_strerror(errno) : strerror(errno));
381 * zperror2() is very similar to zperror() above, except it also prints a
382 * supplied zone name after the executable.
384 * All current consumers of this function want libzonecfg's strerror() rather
385 * than libc's; if this ever changes, this function can be made more generic
386 * like zperror() above.
389 void
390 zperror2(const char *zone, const char *str)
392 (void) fprintf(stderr, "%s: %s: %s: %s\n", execname, zone, str,
393 zonecfg_strerror(errno));
396 /* PRINTFLIKE1 */
397 void
398 zerror(const char *fmt, ...)
400 va_list alist;
402 va_start(alist, fmt);
403 (void) fprintf(stderr, "%s: ", execname);
404 if (target_zone != NULL)
405 (void) fprintf(stderr, "zone '%s': ", target_zone);
406 (void) vfprintf(stderr, fmt, alist);
407 (void) fprintf(stderr, "\n");
408 va_end(alist);
411 static void *
412 safe_calloc(size_t nelem, size_t elsize)
414 void *r = calloc(nelem, elsize);
416 if (r == NULL) {
417 zerror(gettext("failed to allocate %lu bytes: %s"),
418 (ulong_t)nelem * elsize, strerror(errno));
419 exit(Z_ERR);
421 return (r);
424 static void
425 zone_print(zone_entry_t *zent, boolean_t verbose, boolean_t parsable)
427 static boolean_t firsttime = B_TRUE;
428 char *ip_type_str;
430 /* Skip a zone that shutdown while we were collecting data. */
431 if (zent->zname[0] == '\0')
432 return;
434 if (zent->ziptype == ZS_EXCLUSIVE)
435 ip_type_str = "excl";
436 else
437 ip_type_str = "shared";
439 assert(!(verbose && parsable));
440 if (firsttime && verbose) {
441 firsttime = B_FALSE;
442 (void) printf("%*s %-16s %-10s %-30s %-8s %-6s\n",
443 ZONEID_WIDTH, "ID", "NAME", "STATUS", "PATH", "BRAND",
444 "IP");
446 if (!verbose) {
447 char *cp, *clim;
449 if (!parsable) {
450 (void) printf("%s\n", zent->zname);
451 return;
453 if (zent->zid == ZONE_ID_UNDEFINED)
454 (void) printf("-");
455 else
456 (void) printf("%lu", zent->zid);
457 (void) printf(":%s:%s:", zent->zname, zent->zstate_str);
458 cp = zent->zroot;
459 while ((clim = strchr(cp, ':')) != NULL) {
460 (void) printf("%.*s\\:", clim - cp, cp);
461 cp = clim + 1;
463 (void) printf("%s:%s:%s:%s\n", cp, zent->zuuid, zent->zbrand,
464 ip_type_str);
465 return;
467 if (zent->zstate_str != NULL) {
468 if (zent->zid == ZONE_ID_UNDEFINED)
469 (void) printf("%*s", ZONEID_WIDTH, "-");
470 else
471 (void) printf("%*lu", ZONEID_WIDTH, zent->zid);
472 (void) printf(" %-16s %-10s %-30s %-8s %-6s\n", zent->zname,
473 zent->zstate_str, zent->zroot, zent->zbrand, ip_type_str);
477 static int
478 lookup_zone_info(const char *zone_name, zoneid_t zid, zone_entry_t *zent)
480 char root[MAXPATHLEN];
481 int err;
482 uuid_t uuid;
483 zone_dochandle_t handle;
485 (void) strlcpy(zent->zname, zone_name, sizeof (zent->zname));
486 (void) strlcpy(zent->zroot, "???", sizeof (zent->zroot));
487 (void) strlcpy(zent->zbrand, "???", sizeof (zent->zbrand));
488 zent->zstate_str = "???";
490 zent->zid = zid;
492 if (zonecfg_get_uuid(zone_name, uuid) == Z_OK &&
493 !uuid_is_null(uuid))
494 uuid_unparse(uuid, zent->zuuid);
495 else
496 zent->zuuid[0] = '\0';
498 if ((err = zone_get_zonepath(zent->zname, root,
499 sizeof (root))) != Z_OK) {
500 errno = err;
501 zperror2(zent->zname,
502 gettext("could not get zone path."));
503 return (Z_ERR);
505 (void) strlcpy(zent->zroot, root, sizeof (zent->zroot));
507 if ((err = zone_get_state(zent->zname, &zent->zstate_num)) != Z_OK) {
508 errno = err;
509 zperror2(zent->zname, gettext("could not get state"));
510 return (Z_ERR);
512 zent->zstate_str = zone_state_str(zent->zstate_num);
514 if (zone_get_brand(zent->zname, zent->zbrand,
515 sizeof (zent->zbrand)) != Z_OK) {
516 zperror2(zent->zname, gettext("could not get brand name"));
517 return (Z_ERR);
521 * Get ip type of the zone.
522 * Note for global zone, ZS_SHARED is set always.
524 if (zid == GLOBAL_ZONEID) {
525 zent->ziptype = ZS_SHARED;
526 return (Z_OK);
530 * There is a race condition where the zone could boot while
531 * we're walking the index file. In this case the zone state
532 * could be seen as running from the call above, but the zoneid
533 * would be undefined.
535 * There is also a race condition where the zone could shutdown after
536 * we got its running state above. This is also not an error and
537 * we fall back to getting the ziptype from the zone configuration.
539 if (zent->zstate_num == ZONE_STATE_RUNNING &&
540 zid != ZONE_ID_UNDEFINED) {
541 ushort_t flags;
543 if (zone_getattr(zid, ZONE_ATTR_FLAGS, &flags,
544 sizeof (flags)) >= 0) {
545 if (flags & ZF_NET_EXCL)
546 zent->ziptype = ZS_EXCLUSIVE;
547 else
548 zent->ziptype = ZS_SHARED;
549 return (Z_OK);
553 if ((handle = zonecfg_init_handle()) == NULL) {
554 zperror2(zent->zname, gettext("could not init handle"));
555 return (Z_ERR);
557 if ((err = zonecfg_get_handle(zent->zname, handle)) != Z_OK) {
558 zperror2(zent->zname, gettext("could not get handle"));
559 zonecfg_fini_handle(handle);
560 return (Z_ERR);
563 if ((err = zonecfg_get_iptype(handle, &zent->ziptype)) != Z_OK) {
564 zperror2(zent->zname, gettext("could not get ip-type"));
565 zonecfg_fini_handle(handle);
566 return (Z_ERR);
568 zonecfg_fini_handle(handle);
570 return (Z_OK);
574 * fetch_zents() calls zone_list(2) to find out how many zones are running
575 * (which is stored in the global nzents), then calls zone_list(2) again
576 * to fetch the list of running zones (stored in the global zents). This
577 * function may be called multiple times, so if zents is already set, we
578 * return immediately to save work.
580 * Note that the data about running zones can change while this function
581 * is running, so its possible that the list of zones will have empty slots
582 * at the end.
585 static int
586 fetch_zents(void)
588 zoneid_t *zids = NULL;
589 uint_t nzents_saved;
590 int i, retv;
591 FILE *fp;
592 boolean_t inaltroot;
593 zone_entry_t *zentp;
594 const char *altroot;
596 if (nzents > 0)
597 return (Z_OK);
599 if (zone_list(NULL, &nzents) != 0) {
600 zperror(gettext("failed to get zoneid list"), B_FALSE);
601 return (Z_ERR);
604 again:
605 if (nzents == 0)
606 return (Z_OK);
608 zids = safe_calloc(nzents, sizeof (zoneid_t));
609 nzents_saved = nzents;
611 if (zone_list(zids, &nzents) != 0) {
612 zperror(gettext("failed to get zone list"), B_FALSE);
613 free(zids);
614 return (Z_ERR);
616 if (nzents != nzents_saved) {
617 /* list changed, try again */
618 free(zids);
619 goto again;
622 zents = safe_calloc(nzents, sizeof (zone_entry_t));
624 inaltroot = zonecfg_in_alt_root();
625 if (inaltroot) {
626 fp = zonecfg_open_scratch("", B_FALSE);
627 altroot = zonecfg_get_root();
628 } else {
629 fp = NULL;
631 zentp = zents;
632 retv = Z_OK;
633 for (i = 0; i < nzents; i++) {
634 char name[ZONENAME_MAX];
635 char altname[ZONENAME_MAX];
636 char rev_altroot[MAXPATHLEN];
638 if (getzonenamebyid(zids[i], name, sizeof (name)) < 0) {
640 * There is a race condition where the zone may have
641 * shutdown since we retrieved the number of running
642 * zones above. This is not an error, there will be
643 * an empty slot at the end of the list.
645 continue;
647 if (zonecfg_is_scratch(name)) {
648 /* Ignore scratch zones by default */
649 if (!inaltroot)
650 continue;
651 if (fp == NULL ||
652 zonecfg_reverse_scratch(fp, name, altname,
653 sizeof (altname), rev_altroot,
654 sizeof (rev_altroot)) == -1) {
655 zerror(gettext("could not resolve scratch "
656 "zone %s"), name);
657 retv = Z_ERR;
658 continue;
660 /* Ignore zones in other alternate roots */
661 if (strcmp(rev_altroot, altroot) != 0)
662 continue;
663 (void) strcpy(name, altname);
664 } else {
665 /* Ignore non-scratch when in an alternate root */
666 if (inaltroot && strcmp(name, GLOBAL_ZONENAME) != 0)
667 continue;
669 if (lookup_zone_info(name, zids[i], zentp) != Z_OK) {
671 * There is a race condition where the zone may have
672 * shutdown since we retrieved the number of running
673 * zones above. This is not an error, there will be
674 * an empty slot at the end of the list.
676 continue;
678 zentp++;
680 nzents = zentp - zents;
681 if (fp != NULL)
682 zonecfg_close_scratch(fp);
684 free(zids);
685 return (retv);
688 static int
689 zone_print_list(zone_state_t min_state, boolean_t verbose, boolean_t parsable)
691 int i;
692 zone_entry_t zent;
693 FILE *cookie;
694 char *name;
697 * First get the list of running zones from the kernel and print them.
698 * If that is all we need, then return.
700 if ((i = fetch_zents()) != Z_OK) {
702 * No need for error messages; fetch_zents() has already taken
703 * care of this.
705 return (i);
707 for (i = 0; i < nzents; i++)
708 zone_print(&zents[i], verbose, parsable);
709 if (min_state >= ZONE_STATE_RUNNING)
710 return (Z_OK);
712 * Next, get the full list of zones from the configuration, skipping
713 * any we have already printed.
715 cookie = setzoneent();
716 while ((name = getzoneent(cookie)) != NULL) {
717 for (i = 0; i < nzents; i++) {
718 if (strcmp(zents[i].zname, name) == 0)
719 break;
721 if (i < nzents) {
722 free(name);
723 continue;
725 if (lookup_zone_info(name, ZONE_ID_UNDEFINED, &zent) != Z_OK) {
726 free(name);
727 continue;
729 free(name);
730 if (zent.zstate_num >= min_state)
731 zone_print(&zent, verbose, parsable);
733 endzoneent(cookie);
734 return (Z_OK);
738 * Retrieve a zone entry by name. Returns NULL if no such zone exists.
740 static zone_entry_t *
741 lookup_running_zone(const char *str)
743 int i;
745 if (fetch_zents() != Z_OK)
746 return (NULL);
748 for (i = 0; i < nzents; i++) {
749 if (strcmp(str, zents[i].zname) == 0)
750 return (&zents[i]);
752 return (NULL);
756 * Check a bit in a mode_t: if on is B_TRUE, that bit should be on; if
757 * B_FALSE, it should be off. Return B_TRUE if the mode is bad (incorrect).
759 static boolean_t
760 bad_mode_bit(mode_t mode, mode_t bit, boolean_t on, char *file)
762 char *str;
764 assert(bit == S_IRUSR || bit == S_IWUSR || bit == S_IXUSR ||
765 bit == S_IRGRP || bit == S_IWGRP || bit == S_IXGRP ||
766 bit == S_IROTH || bit == S_IWOTH || bit == S_IXOTH);
768 * TRANSLATION_NOTE
769 * The strings below will be used as part of a larger message,
770 * either:
771 * (file name) must be (owner|group|world) (read|writ|execut)able
772 * or
773 * (file name) must not be (owner|group|world) (read|writ|execut)able
775 switch (bit) {
776 case S_IRUSR:
777 str = gettext("owner readable");
778 break;
779 case S_IWUSR:
780 str = gettext("owner writable");
781 break;
782 case S_IXUSR:
783 str = gettext("owner executable");
784 break;
785 case S_IRGRP:
786 str = gettext("group readable");
787 break;
788 case S_IWGRP:
789 str = gettext("group writable");
790 break;
791 case S_IXGRP:
792 str = gettext("group executable");
793 break;
794 case S_IROTH:
795 str = gettext("world readable");
796 break;
797 case S_IWOTH:
798 str = gettext("world writable");
799 break;
800 case S_IXOTH:
801 str = gettext("world executable");
802 break;
804 if ((mode & bit) == (on ? 0 : bit)) {
806 * TRANSLATION_NOTE
807 * The first parameter below is a file name; the second
808 * is one of the "(owner|group|world) (read|writ|execut)able"
809 * strings from above.
812 * The code below could be simplified but not in a way
813 * that would easily translate to non-English locales.
815 if (on) {
816 (void) fprintf(stderr, gettext("%s must be %s.\n"),
817 file, str);
818 } else {
819 (void) fprintf(stderr, gettext("%s must not be %s.\n"),
820 file, str);
822 return (B_TRUE);
824 return (B_FALSE);
828 * We want to make sure that no zone has its zone path as a child node
829 * (in the directory sense) of any other. We do that by comparing this
830 * zone's path to the path of all other (non-global) zones. The comparison
831 * in each case is simple: add '/' to the end of the path, then do a
832 * strncmp() of the two paths, using the length of the shorter one.
835 static int
836 crosscheck_zonepaths(char *path)
838 char rpath[MAXPATHLEN]; /* resolved path */
839 char path_copy[MAXPATHLEN]; /* copy of original path */
840 char rpath_copy[MAXPATHLEN]; /* copy of original rpath */
841 struct zoneent *ze;
842 int res, err;
843 FILE *cookie;
845 cookie = setzoneent();
846 while ((ze = getzoneent_private(cookie)) != NULL) {
847 /* Skip zones which are not installed. */
848 if (ze->zone_state < ZONE_STATE_INSTALLED) {
849 free(ze);
850 continue;
852 /* Skip the global zone and the current target zone. */
853 if (strcmp(ze->zone_name, GLOBAL_ZONENAME) == 0 ||
854 strcmp(ze->zone_name, target_zone) == 0) {
855 free(ze);
856 continue;
858 if (strlen(ze->zone_path) == 0) {
859 /* old index file without path, fall back */
860 if ((err = zone_get_zonepath(ze->zone_name,
861 ze->zone_path, sizeof (ze->zone_path))) != Z_OK) {
862 errno = err;
863 zperror2(ze->zone_name,
864 gettext("could not get zone path"));
865 free(ze);
866 continue;
869 (void) snprintf(path_copy, sizeof (path_copy), "%s%s",
870 zonecfg_get_root(), ze->zone_path);
871 res = resolvepath(path_copy, rpath, sizeof (rpath));
872 if (res == -1) {
873 if (errno != ENOENT) {
874 zperror(path_copy, B_FALSE);
875 free(ze);
876 return (Z_ERR);
878 (void) printf(gettext("WARNING: zone %s is installed, "
879 "but its %s %s does not exist.\n"), ze->zone_name,
880 "zonepath", path_copy);
881 free(ze);
882 continue;
884 rpath[res] = '\0';
885 (void) snprintf(path_copy, sizeof (path_copy), "%s/", path);
886 (void) snprintf(rpath_copy, sizeof (rpath_copy), "%s/", rpath);
887 if (strncmp(path_copy, rpath_copy,
888 min(strlen(path_copy), strlen(rpath_copy))) == 0) {
890 * TRANSLATION_NOTE
891 * zonepath is a literal that should not be translated.
893 (void) fprintf(stderr, gettext("%s zonepath (%s) and "
894 "%s zonepath (%s) overlap.\n"),
895 target_zone, path, ze->zone_name, rpath);
896 free(ze);
897 return (Z_ERR);
899 free(ze);
901 endzoneent(cookie);
902 return (Z_OK);
905 static int
906 validate_zonepath(char *path, int cmd_num)
908 int res; /* result of last library/system call */
909 boolean_t err = B_FALSE; /* have we run into an error? */
910 struct stat stbuf;
911 struct statvfs64 vfsbuf;
912 char rpath[MAXPATHLEN]; /* resolved path */
913 char ppath[MAXPATHLEN]; /* parent path */
914 char rppath[MAXPATHLEN]; /* resolved parent path */
915 char rootpath[MAXPATHLEN]; /* root path */
916 zone_state_t state;
918 if (path[0] != '/') {
919 (void) fprintf(stderr,
920 gettext("%s is not an absolute path.\n"), path);
921 return (Z_ERR);
923 if ((res = resolvepath(path, rpath, sizeof (rpath))) == -1) {
924 if ((errno != ENOENT) ||
925 (cmd_num != CMD_VERIFY && cmd_num != CMD_INSTALL &&
926 cmd_num != CMD_CLONE && cmd_num != CMD_MOVE)) {
927 zperror(path, B_FALSE);
928 return (Z_ERR);
930 if (cmd_num == CMD_VERIFY) {
932 * TRANSLATION_NOTE
933 * zoneadm is a literal that should not be translated.
935 (void) fprintf(stderr, gettext("WARNING: %s does not "
936 "exist, so it could not be verified.\nWhen "
937 "'zoneadm %s' is run, '%s' will try to create\n%s, "
938 "and '%s' will be tried again,\nbut the '%s' may "
939 "fail if:\nthe parent directory of %s is group- or "
940 "other-writable\nor\n%s overlaps with any other "
941 "installed zones.\n"), path,
942 cmd_to_str(CMD_INSTALL), cmd_to_str(CMD_INSTALL),
943 path, cmd_to_str(CMD_VERIFY),
944 cmd_to_str(CMD_VERIFY), path, path);
945 return (Z_OK);
948 * The zonepath is supposed to be mode 700 but its
949 * parent(s) 755. So use 755 on the mkdirp() then
950 * chmod() the zonepath itself to 700.
952 if (mkdirp(path, DEFAULT_DIR_MODE) < 0) {
953 zperror(path, B_FALSE);
954 return (Z_ERR);
957 * If the chmod() fails, report the error, but might
958 * as well continue the verify procedure.
960 if (chmod(path, S_IRWXU) != 0)
961 zperror(path, B_FALSE);
963 * Since the mkdir() succeeded, we should not have to
964 * worry about a subsequent ENOENT, thus this should
965 * only recurse once.
967 return (validate_zonepath(path, cmd_num));
969 rpath[res] = '\0';
970 if (strcmp(path, rpath) != 0) {
971 errno = Z_RESOLVED_PATH;
972 zperror(path, B_TRUE);
973 return (Z_ERR);
975 if ((res = stat(rpath, &stbuf)) != 0) {
976 zperror(rpath, B_FALSE);
977 return (Z_ERR);
979 if (!S_ISDIR(stbuf.st_mode)) {
980 (void) fprintf(stderr, gettext("%s is not a directory.\n"),
981 rpath);
982 return (Z_ERR);
984 if (strcmp(stbuf.st_fstype, MNTTYPE_TMPFS) == 0) {
985 (void) printf(gettext("WARNING: %s is on a temporary "
986 "file system.\n"), rpath);
988 if (crosscheck_zonepaths(rpath) != Z_OK)
989 return (Z_ERR);
991 * Try to collect and report as many minor errors as possible
992 * before returning, so the user can learn everything that needs
993 * to be fixed up front.
995 if (stbuf.st_uid != 0) {
996 (void) fprintf(stderr, gettext("%s is not owned by root.\n"),
997 rpath);
998 err = B_TRUE;
1000 /* Try to change owner */
1001 if (cmd_num != CMD_VERIFY) {
1002 (void) fprintf(stderr, gettext("%s: changing owner "
1003 "to root.\n"), rpath);
1004 if (chown(rpath, 0, -1) != 0) {
1005 zperror(rpath, B_FALSE);
1006 return (Z_ERR);
1007 } else {
1008 err = B_FALSE;
1012 err |= bad_mode_bit(stbuf.st_mode, S_IRUSR, B_TRUE, rpath);
1013 err |= bad_mode_bit(stbuf.st_mode, S_IWUSR, B_TRUE, rpath);
1014 err |= bad_mode_bit(stbuf.st_mode, S_IXUSR, B_TRUE, rpath);
1015 err |= bad_mode_bit(stbuf.st_mode, S_IRGRP, B_FALSE, rpath);
1016 err |= bad_mode_bit(stbuf.st_mode, S_IWGRP, B_FALSE, rpath);
1017 err |= bad_mode_bit(stbuf.st_mode, S_IXGRP, B_FALSE, rpath);
1018 err |= bad_mode_bit(stbuf.st_mode, S_IROTH, B_FALSE, rpath);
1019 err |= bad_mode_bit(stbuf.st_mode, S_IWOTH, B_FALSE, rpath);
1020 err |= bad_mode_bit(stbuf.st_mode, S_IXOTH, B_FALSE, rpath);
1022 /* If the group perms are wrong, fix them */
1023 if (err && (cmd_num != CMD_VERIFY)) {
1024 (void) fprintf(stderr, gettext("%s: changing permissions "
1025 "to 0700.\n"), rpath);
1026 if (chmod(rpath, S_IRWXU) != 0) {
1027 zperror(path, B_FALSE);
1028 } else {
1029 err = B_FALSE;
1033 (void) snprintf(ppath, sizeof (ppath), "%s/..", path);
1034 if ((res = resolvepath(ppath, rppath, sizeof (rppath))) == -1) {
1035 zperror(ppath, B_FALSE);
1036 return (Z_ERR);
1038 rppath[res] = '\0';
1039 if ((res = stat(rppath, &stbuf)) != 0) {
1040 zperror(rppath, B_FALSE);
1041 return (Z_ERR);
1043 /* theoretically impossible */
1044 if (!S_ISDIR(stbuf.st_mode)) {
1045 (void) fprintf(stderr, gettext("%s is not a directory.\n"),
1046 rppath);
1047 return (Z_ERR);
1049 if (stbuf.st_uid != 0) {
1050 (void) fprintf(stderr, gettext("%s is not owned by root.\n"),
1051 rppath);
1052 err = B_TRUE;
1054 err |= bad_mode_bit(stbuf.st_mode, S_IRUSR, B_TRUE, rppath);
1055 err |= bad_mode_bit(stbuf.st_mode, S_IWUSR, B_TRUE, rppath);
1056 err |= bad_mode_bit(stbuf.st_mode, S_IXUSR, B_TRUE, rppath);
1057 err |= bad_mode_bit(stbuf.st_mode, S_IWGRP, B_FALSE, rppath);
1058 err |= bad_mode_bit(stbuf.st_mode, S_IWOTH, B_FALSE, rppath);
1059 if (strcmp(rpath, rppath) == 0) {
1060 (void) fprintf(stderr, gettext("%s is its own parent.\n"),
1061 rppath);
1062 err = B_TRUE;
1065 if (statvfs64(rpath, &vfsbuf) != 0) {
1066 zperror(rpath, B_FALSE);
1067 return (Z_ERR);
1069 if (strcmp(vfsbuf.f_basetype, MNTTYPE_NFS) == 0) {
1071 * TRANSLATION_NOTE
1072 * Zonepath and NFS are literals that should not be translated.
1074 (void) fprintf(stderr, gettext("Zonepath %s is on an NFS "
1075 "mounted file system.\n"
1076 "\tA local file system must be used.\n"), rpath);
1077 return (Z_ERR);
1079 if (vfsbuf.f_flag & ST_NOSUID) {
1081 * TRANSLATION_NOTE
1082 * Zonepath and nosuid are literals that should not be
1083 * translated.
1085 (void) fprintf(stderr, gettext("Zonepath %s is on a nosuid "
1086 "file system.\n"), rpath);
1087 return (Z_ERR);
1090 if ((res = zone_get_state(target_zone, &state)) != Z_OK) {
1091 errno = res;
1092 zperror2(target_zone, gettext("could not get state"));
1093 return (Z_ERR);
1096 * The existence of the root path is only bad in the configured state,
1097 * as it is *supposed* to be there at the installed and later states.
1098 * However, the root path is expected to be there if the zone is
1099 * detached.
1100 * State/command mismatches are caught earlier in verify_details().
1102 if (state == ZONE_STATE_CONFIGURED && cmd_num != CMD_ATTACH) {
1103 if (snprintf(rootpath, sizeof (rootpath), "%s/root", rpath) >=
1104 sizeof (rootpath)) {
1106 * TRANSLATION_NOTE
1107 * Zonepath is a literal that should not be translated.
1109 (void) fprintf(stderr,
1110 gettext("Zonepath %s is too long.\n"), rpath);
1111 return (Z_ERR);
1113 if ((res = stat(rootpath, &stbuf)) == 0) {
1114 if (zonecfg_detached(rpath)) {
1115 (void) fprintf(stderr,
1116 gettext("Cannot %s detached "
1117 "zone.\nUse attach or remove %s "
1118 "directory.\n"), cmd_to_str(cmd_num),
1119 rpath);
1120 return (Z_ERR);
1123 /* Not detached, check if it really looks ok. */
1125 if (!S_ISDIR(stbuf.st_mode)) {
1126 (void) fprintf(stderr, gettext("%s is not a "
1127 "directory.\n"), rootpath);
1128 return (Z_ERR);
1131 if (stbuf.st_uid != 0) {
1132 (void) fprintf(stderr, gettext("%s is not "
1133 "owned by root.\n"), rootpath);
1134 return (Z_ERR);
1137 if ((stbuf.st_mode & 0777) != 0755) {
1138 (void) fprintf(stderr, gettext("%s mode is not "
1139 "0755.\n"), rootpath);
1140 return (Z_ERR);
1145 return (err ? Z_ERR : Z_OK);
1148 static int
1149 invoke_brand_handler(int cmd_num, char *argv[])
1151 zone_dochandle_t handle;
1152 int err;
1154 if ((handle = zonecfg_init_handle()) == NULL) {
1155 zperror(cmd_to_str(cmd_num), B_TRUE);
1156 return (Z_ERR);
1158 if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) {
1159 errno = err;
1160 zperror(cmd_to_str(cmd_num), B_TRUE);
1161 zonecfg_fini_handle(handle);
1162 return (Z_ERR);
1164 if (verify_brand(handle, cmd_num, argv) != Z_OK) {
1165 zonecfg_fini_handle(handle);
1166 return (Z_ERR);
1168 zonecfg_fini_handle(handle);
1169 return (Z_OK);
1172 static int
1173 ready_func(int argc, char *argv[])
1175 zone_cmd_arg_t zarg;
1176 int arg;
1178 if (zonecfg_in_alt_root()) {
1179 zerror(gettext("cannot ready zone in alternate root"));
1180 return (Z_ERR);
1183 optind = 0;
1184 if ((arg = getopt(argc, argv, "?")) != EOF) {
1185 switch (arg) {
1186 case '?':
1187 sub_usage(SHELP_READY, CMD_READY);
1188 return (optopt == '?' ? Z_OK : Z_USAGE);
1189 default:
1190 sub_usage(SHELP_READY, CMD_READY);
1191 return (Z_USAGE);
1194 if (argc > optind) {
1195 sub_usage(SHELP_READY, CMD_READY);
1196 return (Z_USAGE);
1198 if (sanity_check(target_zone, CMD_READY, B_FALSE, B_FALSE, B_FALSE)
1199 != Z_OK)
1200 return (Z_ERR);
1201 if (verify_details(CMD_READY, argv) != Z_OK)
1202 return (Z_ERR);
1204 zarg.cmd = Z_READY;
1205 if (zonecfg_call_zoneadmd(target_zone, &zarg, locale, B_TRUE) != 0) {
1206 zerror(gettext("call to %s failed"), "zoneadmd");
1207 return (Z_ERR);
1209 return (Z_OK);
1212 static int
1213 boot_func(int argc, char *argv[])
1215 zone_cmd_arg_t zarg;
1216 boolean_t force = B_FALSE;
1217 int arg;
1219 if (zonecfg_in_alt_root()) {
1220 zerror(gettext("cannot boot zone in alternate root"));
1221 return (Z_ERR);
1224 zarg.bootbuf[0] = '\0';
1227 * The following getopt processes arguments to zone boot; that
1228 * is to say, the [here] portion of the argument string:
1230 * zoneadm -z myzone boot [here] -- -v -m verbose
1232 * Where [here] can either be nothing, -? (in which case we bail
1233 * and print usage), -f (a private option to indicate that the
1234 * boot operation should be 'forced'), or -s. Support for -s is
1235 * vestigal and obsolete, but is retained because it was a
1236 * documented interface and there are known consumers including
1237 * admin/install; the proper way to specify boot arguments like -s
1238 * is:
1240 * zoneadm -z myzone boot -- -s -v -m verbose.
1242 optind = 0;
1243 while ((arg = getopt(argc, argv, "?fs")) != EOF) {
1244 switch (arg) {
1245 case '?':
1246 sub_usage(SHELP_BOOT, CMD_BOOT);
1247 return (optopt == '?' ? Z_OK : Z_USAGE);
1248 case 's':
1249 (void) strlcpy(zarg.bootbuf, "-s",
1250 sizeof (zarg.bootbuf));
1251 break;
1252 case 'f':
1253 force = B_TRUE;
1254 break;
1255 default:
1256 sub_usage(SHELP_BOOT, CMD_BOOT);
1257 return (Z_USAGE);
1261 for (; optind < argc; optind++) {
1262 if (strlcat(zarg.bootbuf, argv[optind],
1263 sizeof (zarg.bootbuf)) >= sizeof (zarg.bootbuf)) {
1264 zerror(gettext("Boot argument list too long"));
1265 return (Z_ERR);
1267 if (optind < argc - 1)
1268 if (strlcat(zarg.bootbuf, " ", sizeof (zarg.bootbuf)) >=
1269 sizeof (zarg.bootbuf)) {
1270 zerror(gettext("Boot argument list too long"));
1271 return (Z_ERR);
1274 if (sanity_check(target_zone, CMD_BOOT, B_FALSE, B_FALSE, force)
1275 != Z_OK)
1276 return (Z_ERR);
1277 if (verify_details(CMD_BOOT, argv) != Z_OK)
1278 return (Z_ERR);
1279 zarg.cmd = force ? Z_FORCEBOOT : Z_BOOT;
1280 if (zonecfg_call_zoneadmd(target_zone, &zarg, locale, B_TRUE) != 0) {
1281 zerror(gettext("call to %s failed"), "zoneadmd");
1282 return (Z_ERR);
1285 return (Z_OK);
1288 static void
1289 fake_up_local_zone(zoneid_t zid, zone_entry_t *zeptr)
1291 ssize_t result;
1292 uuid_t uuid;
1293 FILE *fp;
1294 ushort_t flags;
1296 (void) memset(zeptr, 0, sizeof (*zeptr));
1298 zeptr->zid = zid;
1301 * Since we're looking up our own (non-global) zone name,
1302 * we can be assured that it will succeed.
1304 result = getzonenamebyid(zid, zeptr->zname, sizeof (zeptr->zname));
1305 assert(result >= 0);
1306 if (zonecfg_is_scratch(zeptr->zname) &&
1307 (fp = zonecfg_open_scratch("", B_FALSE)) != NULL) {
1308 (void) zonecfg_reverse_scratch(fp, zeptr->zname, zeptr->zname,
1309 sizeof (zeptr->zname), NULL, 0);
1310 zonecfg_close_scratch(fp);
1313 (void) strlcpy(zeptr->zroot, "/", sizeof (zeptr->zroot));
1314 (void) zone_getattr(zid, ZONE_ATTR_BRAND, zeptr->zbrand,
1315 sizeof (zeptr->zbrand));
1317 zeptr->zstate_str = "running";
1318 if (zonecfg_get_uuid(zeptr->zname, uuid) == Z_OK &&
1319 !uuid_is_null(uuid))
1320 uuid_unparse(uuid, zeptr->zuuid);
1322 if (zone_getattr(zid, ZONE_ATTR_FLAGS, &flags, sizeof (flags)) < 0) {
1323 zperror2(zeptr->zname, gettext("could not get zone flags"));
1324 exit(Z_ERR);
1326 if (flags & ZF_NET_EXCL)
1327 zeptr->ziptype = ZS_EXCLUSIVE;
1328 else
1329 zeptr->ziptype = ZS_SHARED;
1332 static int
1333 list_func(int argc, char *argv[])
1335 zone_entry_t *zentp, zent;
1336 int arg, retv;
1337 boolean_t output = B_FALSE, verbose = B_FALSE, parsable = B_FALSE;
1338 zone_state_t min_state = ZONE_STATE_RUNNING;
1339 zoneid_t zone_id = getzoneid();
1341 if (target_zone == NULL) {
1342 /* all zones: default view to running but allow override */
1343 optind = 0;
1344 while ((arg = getopt(argc, argv, "?cipv")) != EOF) {
1345 switch (arg) {
1346 case '?':
1347 sub_usage(SHELP_LIST, CMD_LIST);
1348 return (optopt == '?' ? Z_OK : Z_USAGE);
1350 * The 'i' and 'c' options are not mutually
1351 * exclusive so if 'c' is given, then min_state
1352 * is set to 0 (ZONE_STATE_CONFIGURED) which is
1353 * the lowest possible state. If 'i' is given,
1354 * then min_state is set to be the lowest state
1355 * so far.
1357 case 'c':
1358 min_state = ZONE_STATE_CONFIGURED;
1359 break;
1360 case 'i':
1361 min_state = min(ZONE_STATE_INSTALLED,
1362 min_state);
1364 break;
1365 case 'p':
1366 parsable = B_TRUE;
1367 break;
1368 case 'v':
1369 verbose = B_TRUE;
1370 break;
1371 default:
1372 sub_usage(SHELP_LIST, CMD_LIST);
1373 return (Z_USAGE);
1376 if (parsable && verbose) {
1377 zerror(gettext("%s -p and -v are mutually exclusive."),
1378 cmd_to_str(CMD_LIST));
1379 return (Z_ERR);
1381 if (zone_id == GLOBAL_ZONEID) {
1382 retv = zone_print_list(min_state, verbose, parsable);
1383 } else {
1384 fake_up_local_zone(zone_id, &zent);
1385 retv = Z_OK;
1386 zone_print(&zent, verbose, parsable);
1388 return (retv);
1392 * Specific target zone: disallow -i/-c suboptions.
1394 optind = 0;
1395 while ((arg = getopt(argc, argv, "?pv")) != EOF) {
1396 switch (arg) {
1397 case '?':
1398 sub_usage(SHELP_LIST, CMD_LIST);
1399 return (optopt == '?' ? Z_OK : Z_USAGE);
1400 case 'p':
1401 parsable = B_TRUE;
1402 break;
1403 case 'v':
1404 verbose = B_TRUE;
1405 break;
1406 default:
1407 sub_usage(SHELP_LIST, CMD_LIST);
1408 return (Z_USAGE);
1411 if (parsable && verbose) {
1412 zerror(gettext("%s -p and -v are mutually exclusive."),
1413 cmd_to_str(CMD_LIST));
1414 return (Z_ERR);
1416 if (argc > optind) {
1417 sub_usage(SHELP_LIST, CMD_LIST);
1418 return (Z_USAGE);
1420 if (zone_id != GLOBAL_ZONEID) {
1421 fake_up_local_zone(zone_id, &zent);
1423 * main() will issue a Z_NO_ZONE error if it cannot get an
1424 * id for target_zone, which in a non-global zone should
1425 * happen for any zone name except `zonename`. Thus we
1426 * assert() that here but don't otherwise check.
1428 assert(strcmp(zent.zname, target_zone) == 0);
1429 zone_print(&zent, verbose, parsable);
1430 output = B_TRUE;
1431 } else if ((zentp = lookup_running_zone(target_zone)) != NULL) {
1432 zone_print(zentp, verbose, parsable);
1433 output = B_TRUE;
1434 } else if (lookup_zone_info(target_zone, ZONE_ID_UNDEFINED,
1435 &zent) == Z_OK) {
1436 zone_print(&zent, verbose, parsable);
1437 output = B_TRUE;
1441 * Invoke brand-specific handler. Note that we do this
1442 * only if we're in the global zone, and target_zone is specified
1443 * and it is not the global zone.
1445 if (zone_id == GLOBAL_ZONEID && target_zone != NULL &&
1446 strcmp(target_zone, GLOBAL_ZONENAME) != 0)
1447 if (invoke_brand_handler(CMD_LIST, argv) != Z_OK)
1448 return (Z_ERR);
1450 return (output ? Z_OK : Z_ERR);
1454 do_subproc(char *cmdbuf)
1456 void (*saveint)(int);
1457 void (*saveterm)(int);
1458 void (*savequit)(int);
1459 void (*savehup)(int);
1460 int pid, child, status;
1462 if ((child = vfork()) == 0) {
1463 (void) execl("/bin/sh", "sh", "-c", cmdbuf, (char *)NULL);
1466 if (child == -1)
1467 return (-1);
1469 saveint = sigset(SIGINT, SIG_IGN);
1470 saveterm = sigset(SIGTERM, SIG_IGN);
1471 savequit = sigset(SIGQUIT, SIG_IGN);
1472 savehup = sigset(SIGHUP, SIG_IGN);
1474 while ((pid = waitpid(child, &status, 0)) != child && pid != -1)
1477 (void) sigset(SIGINT, saveint);
1478 (void) sigset(SIGTERM, saveterm);
1479 (void) sigset(SIGQUIT, savequit);
1480 (void) sigset(SIGHUP, savehup);
1482 return (pid == -1 ? -1 : status);
1486 subproc_status(const char *cmd, int status, boolean_t verbose_failure)
1488 if (WIFEXITED(status)) {
1489 int exit_code = WEXITSTATUS(status);
1491 if ((verbose_failure) && (exit_code != ZONE_SUBPROC_OK))
1492 zerror(gettext("'%s' failed with exit code %d."), cmd,
1493 exit_code);
1495 return (exit_code);
1496 } else if (WIFSIGNALED(status)) {
1497 int signal = WTERMSIG(status);
1498 char sigstr[SIG2STR_MAX];
1500 if (sig2str(signal, sigstr) == 0) {
1501 zerror(gettext("'%s' terminated by signal SIG%s."), cmd,
1502 sigstr);
1503 } else {
1504 zerror(gettext("'%s' terminated by an unknown signal."),
1505 cmd);
1507 } else {
1508 zerror(gettext("'%s' failed for unknown reasons."), cmd);
1512 * Assume a subprocess that died due to a signal or an unknown error
1513 * should be considered an exit code of ZONE_SUBPROC_FATAL, as the
1514 * user will likely need to do some manual cleanup.
1516 return (ZONE_SUBPROC_FATAL);
1519 static int
1520 auth_check(char *user, char *zone, int cmd_num)
1522 char authname[MAXAUTHS];
1524 switch (cmd_num) {
1525 case CMD_LIST:
1526 case CMD_HELP:
1527 return (Z_OK);
1528 case SOURCE_ZONE:
1529 (void) strlcpy(authname, ZONE_CLONEFROM_AUTH, MAXAUTHS);
1530 break;
1531 case CMD_BOOT:
1532 case CMD_HALT:
1533 case CMD_READY:
1534 case CMD_SHUTDOWN:
1535 case CMD_REBOOT:
1536 case CMD_SYSBOOT:
1537 case CMD_VERIFY:
1538 case CMD_INSTALL:
1539 case CMD_UNINSTALL:
1540 case CMD_MOUNT:
1541 case CMD_UNMOUNT:
1542 case CMD_CLONE:
1543 case CMD_MOVE:
1544 case CMD_DETACH:
1545 case CMD_ATTACH:
1546 case CMD_MARK:
1547 case CMD_APPLY:
1548 default:
1549 (void) strlcpy(authname, ZONE_MANAGE_AUTH, MAXAUTHS);
1550 break;
1552 (void) strlcat(authname, KV_OBJECT, MAXAUTHS);
1553 (void) strlcat(authname, zone, MAXAUTHS);
1554 if (chkauthattr(authname, user) == 0) {
1555 return (Z_ERR);
1556 } else {
1558 * Some subcommands, e.g. install, run subcommands,
1559 * e.g. sysidcfg, that require a real uid of root,
1560 * so switch to root, here.
1562 if (setuid(0) == -1) {
1563 zperror(gettext("insufficient privilege"), B_TRUE);
1564 return (Z_ERR);
1566 return (Z_OK);
1571 * Various sanity checks; make sure:
1572 * 1. We're in the global zone.
1573 * 2. The calling user has sufficient privilege.
1574 * 3. The target zone is neither the global zone nor anything starting with
1575 * "SUNW".
1576 * 4a. If we're looking for a 'not running' (i.e., configured or installed)
1577 * zone, the name service knows about it.
1578 * 4b. For some operations which expect a zone not to be running, that it is
1579 * not already running (or ready).
1581 static int
1582 sanity_check(char *zone, int cmd_num, boolean_t running,
1583 boolean_t unsafe_when_running, boolean_t force)
1585 zone_entry_t *zent;
1586 priv_set_t *privset;
1587 zone_state_t state, min_state;
1588 char kernzone[ZONENAME_MAX];
1589 FILE *fp;
1591 if (getzoneid() != GLOBAL_ZONEID) {
1592 switch (cmd_num) {
1593 case CMD_HALT:
1594 zerror(gettext("use %s to %s this zone."), "halt(1M)",
1595 cmd_to_str(cmd_num));
1596 break;
1597 case CMD_SHUTDOWN:
1598 zerror(gettext("use %s to %s this zone."),
1599 "shutdown(1M)", cmd_to_str(cmd_num));
1600 break;
1601 case CMD_REBOOT:
1602 zerror(gettext("use %s to %s this zone."),
1603 "reboot(1M)", cmd_to_str(cmd_num));
1604 break;
1605 default:
1606 zerror(gettext("must be in the global zone to %s a "
1607 "zone."), cmd_to_str(cmd_num));
1608 break;
1610 return (Z_ERR);
1613 if ((privset = priv_allocset()) == NULL) {
1614 zerror(gettext("%s failed"), "priv_allocset");
1615 return (Z_ERR);
1618 if (getppriv(PRIV_EFFECTIVE, privset) != 0) {
1619 zerror(gettext("%s failed"), "getppriv");
1620 priv_freeset(privset);
1621 return (Z_ERR);
1624 if (priv_isfullset(privset) == B_FALSE) {
1625 zerror(gettext("only a privileged user may %s a zone."),
1626 cmd_to_str(cmd_num));
1627 priv_freeset(privset);
1628 return (Z_ERR);
1630 priv_freeset(privset);
1632 if (zone == NULL) {
1633 zerror(gettext("no zone specified"));
1634 return (Z_ERR);
1637 if (auth_check(username, zone, cmd_num) == Z_ERR) {
1638 zerror(gettext("User %s is not authorized to %s this zone."),
1639 username, cmd_to_str(cmd_num));
1640 return (Z_ERR);
1643 if (strcmp(zone, GLOBAL_ZONENAME) == 0) {
1644 zerror(gettext("%s operation is invalid for the global zone."),
1645 cmd_to_str(cmd_num));
1646 return (Z_ERR);
1649 if (strncmp(zone, "SUNW", 4) == 0) {
1650 zerror(gettext("%s operation is invalid for zones starting "
1651 "with SUNW."), cmd_to_str(cmd_num));
1652 return (Z_ERR);
1655 if (!zonecfg_in_alt_root()) {
1656 zent = lookup_running_zone(zone);
1657 } else if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) {
1658 zent = NULL;
1659 } else {
1660 if (zonecfg_find_scratch(fp, zone, zonecfg_get_root(),
1661 kernzone, sizeof (kernzone)) == 0)
1662 zent = lookup_running_zone(kernzone);
1663 else
1664 zent = NULL;
1665 zonecfg_close_scratch(fp);
1669 * Look up from the kernel for 'running' zones.
1671 if (running && !force) {
1672 if (zent == NULL) {
1673 zerror(gettext("not running"));
1674 return (Z_ERR);
1676 } else {
1677 int err;
1679 if (unsafe_when_running && zent != NULL) {
1680 /* check whether the zone is ready or running */
1681 if ((err = zone_get_state(zent->zname,
1682 &zent->zstate_num)) != Z_OK) {
1683 errno = err;
1684 zperror2(zent->zname,
1685 gettext("could not get state"));
1686 /* can't tell, so hedge */
1687 zent->zstate_str = "ready/running";
1688 } else {
1689 zent->zstate_str =
1690 zone_state_str(zent->zstate_num);
1692 zerror(gettext("%s operation is invalid for %s zones."),
1693 cmd_to_str(cmd_num), zent->zstate_str);
1694 return (Z_ERR);
1696 if ((err = zone_get_state(zone, &state)) != Z_OK) {
1697 errno = err;
1698 zperror2(zone, gettext("could not get state"));
1699 return (Z_ERR);
1701 switch (cmd_num) {
1702 case CMD_UNINSTALL:
1703 if (state == ZONE_STATE_CONFIGURED) {
1704 zerror(gettext("is already in state '%s'."),
1705 zone_state_str(ZONE_STATE_CONFIGURED));
1706 return (Z_ERR);
1708 break;
1709 case CMD_ATTACH:
1710 if (state == ZONE_STATE_INSTALLED) {
1711 zerror(gettext("is already %s."),
1712 zone_state_str(ZONE_STATE_INSTALLED));
1713 return (Z_ERR);
1714 } else if (state == ZONE_STATE_INCOMPLETE && !force) {
1715 zerror(gettext("zone is %s; %s required."),
1716 zone_state_str(ZONE_STATE_INCOMPLETE),
1717 cmd_to_str(CMD_UNINSTALL));
1718 return (Z_ERR);
1720 break;
1721 case CMD_CLONE:
1722 case CMD_INSTALL:
1723 if (state == ZONE_STATE_INSTALLED) {
1724 zerror(gettext("is already %s."),
1725 zone_state_str(ZONE_STATE_INSTALLED));
1726 return (Z_ERR);
1727 } else if (state == ZONE_STATE_INCOMPLETE) {
1728 zerror(gettext("zone is %s; %s required."),
1729 zone_state_str(ZONE_STATE_INCOMPLETE),
1730 cmd_to_str(CMD_UNINSTALL));
1731 return (Z_ERR);
1733 break;
1734 case CMD_DETACH:
1735 case CMD_MOVE:
1736 case CMD_READY:
1737 case CMD_BOOT:
1738 case CMD_MOUNT:
1739 case CMD_MARK:
1740 if ((cmd_num == CMD_BOOT || cmd_num == CMD_MOUNT) &&
1741 force)
1742 min_state = ZONE_STATE_INCOMPLETE;
1743 else if (cmd_num == CMD_MARK)
1744 min_state = ZONE_STATE_CONFIGURED;
1745 else
1746 min_state = ZONE_STATE_INSTALLED;
1748 if (state < min_state) {
1749 zerror(gettext("must be %s before %s."),
1750 zone_state_str(min_state),
1751 cmd_to_str(cmd_num));
1752 return (Z_ERR);
1754 break;
1755 case CMD_VERIFY:
1756 if (state == ZONE_STATE_INCOMPLETE) {
1757 zerror(gettext("zone is %s; %s required."),
1758 zone_state_str(ZONE_STATE_INCOMPLETE),
1759 cmd_to_str(CMD_UNINSTALL));
1760 return (Z_ERR);
1762 break;
1763 case CMD_UNMOUNT:
1764 if (state != ZONE_STATE_MOUNTED) {
1765 zerror(gettext("must be %s before %s."),
1766 zone_state_str(ZONE_STATE_MOUNTED),
1767 cmd_to_str(cmd_num));
1768 return (Z_ERR);
1770 break;
1771 case CMD_SYSBOOT:
1772 if (state != ZONE_STATE_INSTALLED) {
1773 zerror(gettext("%s operation is invalid for %s "
1774 "zones."), cmd_to_str(cmd_num),
1775 zone_state_str(state));
1776 return (Z_ERR);
1778 break;
1781 return (Z_OK);
1784 static int
1785 halt_func(int argc, char *argv[])
1787 zone_cmd_arg_t zarg;
1788 int arg;
1790 if (zonecfg_in_alt_root()) {
1791 zerror(gettext("cannot halt zone in alternate root"));
1792 return (Z_ERR);
1795 optind = 0;
1796 if ((arg = getopt(argc, argv, "?")) != EOF) {
1797 switch (arg) {
1798 case '?':
1799 sub_usage(SHELP_HALT, CMD_HALT);
1800 return (optopt == '?' ? Z_OK : Z_USAGE);
1801 default:
1802 sub_usage(SHELP_HALT, CMD_HALT);
1803 return (Z_USAGE);
1806 if (argc > optind) {
1807 sub_usage(SHELP_HALT, CMD_HALT);
1808 return (Z_USAGE);
1811 * zoneadmd should be the one to decide whether or not to proceed,
1812 * so even though it seems that the fourth parameter below should
1813 * perhaps be B_TRUE, it really shouldn't be.
1815 if (sanity_check(target_zone, CMD_HALT, B_FALSE, B_FALSE, B_FALSE)
1816 != Z_OK)
1817 return (Z_ERR);
1820 * Invoke brand-specific handler.
1822 if (invoke_brand_handler(CMD_HALT, argv) != Z_OK)
1823 return (Z_ERR);
1825 zarg.cmd = Z_HALT;
1826 return ((zonecfg_call_zoneadmd(target_zone, &zarg, locale,
1827 B_TRUE) == 0) ? Z_OK : Z_ERR);
1830 static int
1831 shutdown_func(int argc, char *argv[])
1833 zone_cmd_arg_t zarg;
1834 int arg;
1835 boolean_t reboot = B_FALSE;
1837 zarg.cmd = Z_SHUTDOWN;
1839 if (zonecfg_in_alt_root()) {
1840 zerror(gettext("cannot shut down zone in alternate root"));
1841 return (Z_ERR);
1844 optind = 0;
1845 while ((arg = getopt(argc, argv, "?r")) != EOF) {
1846 switch (arg) {
1847 case '?':
1848 sub_usage(SHELP_SHUTDOWN, CMD_SHUTDOWN);
1849 return (optopt == '?' ? Z_OK : Z_USAGE);
1850 case 'r':
1851 reboot = B_TRUE;
1852 break;
1853 default:
1854 sub_usage(SHELP_SHUTDOWN, CMD_SHUTDOWN);
1855 return (Z_USAGE);
1859 zarg.bootbuf[0] = '\0';
1860 for (; optind < argc; optind++) {
1861 if (strlcat(zarg.bootbuf, argv[optind],
1862 sizeof (zarg.bootbuf)) >= sizeof (zarg.bootbuf)) {
1863 zerror(gettext("Boot argument list too long"));
1864 return (Z_ERR);
1866 if (optind < argc - 1)
1867 if (strlcat(zarg.bootbuf, " ", sizeof (zarg.bootbuf)) >=
1868 sizeof (zarg.bootbuf)) {
1869 zerror(gettext("Boot argument list too long"));
1870 return (Z_ERR);
1875 * zoneadmd should be the one to decide whether or not to proceed,
1876 * so even though it seems that the third parameter below should
1877 * perhaps be B_TRUE, it really shouldn't be.
1879 if (sanity_check(target_zone, CMD_SHUTDOWN, B_TRUE, B_FALSE, B_FALSE)
1880 != Z_OK)
1881 return (Z_ERR);
1883 if (zonecfg_call_zoneadmd(target_zone, &zarg, locale, B_TRUE) != Z_OK)
1884 return (Z_ERR);
1886 if (reboot) {
1887 if (sanity_check(target_zone, CMD_BOOT, B_FALSE, B_FALSE,
1888 B_FALSE) != Z_OK)
1889 return (Z_ERR);
1891 zarg.cmd = Z_BOOT;
1892 if (zonecfg_call_zoneadmd(target_zone, &zarg, locale,
1893 B_TRUE) != Z_OK)
1894 return (Z_ERR);
1896 return (Z_OK);
1899 static int
1900 reboot_func(int argc, char *argv[])
1902 zone_cmd_arg_t zarg;
1903 int arg;
1905 if (zonecfg_in_alt_root()) {
1906 zerror(gettext("cannot reboot zone in alternate root"));
1907 return (Z_ERR);
1910 optind = 0;
1911 if ((arg = getopt(argc, argv, "?")) != EOF) {
1912 switch (arg) {
1913 case '?':
1914 sub_usage(SHELP_REBOOT, CMD_REBOOT);
1915 return (optopt == '?' ? Z_OK : Z_USAGE);
1916 default:
1917 sub_usage(SHELP_REBOOT, CMD_REBOOT);
1918 return (Z_USAGE);
1922 zarg.bootbuf[0] = '\0';
1923 for (; optind < argc; optind++) {
1924 if (strlcat(zarg.bootbuf, argv[optind],
1925 sizeof (zarg.bootbuf)) >= sizeof (zarg.bootbuf)) {
1926 zerror(gettext("Boot argument list too long"));
1927 return (Z_ERR);
1929 if (optind < argc - 1)
1930 if (strlcat(zarg.bootbuf, " ", sizeof (zarg.bootbuf)) >=
1931 sizeof (zarg.bootbuf)) {
1932 zerror(gettext("Boot argument list too long"));
1933 return (Z_ERR);
1939 * zoneadmd should be the one to decide whether or not to proceed,
1940 * so even though it seems that the fourth parameter below should
1941 * perhaps be B_TRUE, it really shouldn't be.
1943 if (sanity_check(target_zone, CMD_REBOOT, B_TRUE, B_FALSE, B_FALSE)
1944 != Z_OK)
1945 return (Z_ERR);
1946 if (verify_details(CMD_REBOOT, argv) != Z_OK)
1947 return (Z_ERR);
1949 zarg.cmd = Z_REBOOT;
1950 return ((zonecfg_call_zoneadmd(target_zone, &zarg, locale, B_TRUE) == 0)
1951 ? Z_OK : Z_ERR);
1954 static int
1955 get_hook(brand_handle_t bh, char *cmd, size_t len, int (*bp)(brand_handle_t,
1956 const char *, const char *, char *, size_t), char *zonename, char *zonepath)
1958 if (strlcpy(cmd, EXEC_PREFIX, len) >= len)
1959 return (Z_ERR);
1961 if (bp(bh, zonename, zonepath, cmd + EXEC_LEN, len - EXEC_LEN) != 0)
1962 return (Z_ERR);
1964 if (strlen(cmd) <= EXEC_LEN)
1965 cmd[0] = '\0';
1967 return (Z_OK);
1970 static int
1971 verify_brand(zone_dochandle_t handle, int cmd_num, char *argv[])
1973 char cmdbuf[MAXPATHLEN];
1974 int err;
1975 char zonepath[MAXPATHLEN];
1976 brand_handle_t bh = NULL;
1977 int status, i;
1980 * Fetch the verify command from the brand configuration.
1981 * "exec" the command so that the returned status is that of
1982 * the command and not the shell.
1984 if (handle == NULL) {
1985 (void) strlcpy(zonepath, "-", sizeof (zonepath));
1986 } else if ((err = zonecfg_get_zonepath(handle, zonepath,
1987 sizeof (zonepath))) != Z_OK) {
1988 errno = err;
1989 zperror(cmd_to_str(cmd_num), B_TRUE);
1990 return (Z_ERR);
1992 if ((bh = brand_open(target_brand)) == NULL) {
1993 zerror(gettext("missing or invalid brand"));
1994 return (Z_ERR);
1998 * If the brand has its own verification routine, execute it now.
1999 * The verification routine validates the intended zoneadm
2000 * operation for the specific brand. The zoneadm subcommand and
2001 * all its arguments are passed to the routine.
2003 err = get_hook(bh, cmdbuf, sizeof (cmdbuf), brand_get_verify_adm,
2004 target_zone, zonepath);
2005 brand_close(bh);
2006 if (err != Z_OK)
2007 return (Z_BRAND_ERROR);
2008 if (cmdbuf[0] == '\0')
2009 return (Z_OK);
2011 if (strlcat(cmdbuf, cmd_to_str(cmd_num),
2012 sizeof (cmdbuf)) >= sizeof (cmdbuf))
2013 return (Z_ERR);
2015 /* Build the argv string */
2016 i = 0;
2017 while (argv[i] != NULL) {
2018 if ((strlcat(cmdbuf, " ",
2019 sizeof (cmdbuf)) >= sizeof (cmdbuf)) ||
2020 (strlcat(cmdbuf, argv[i++],
2021 sizeof (cmdbuf)) >= sizeof (cmdbuf)))
2022 return (Z_ERR);
2025 status = do_subproc(cmdbuf);
2026 err = subproc_status(gettext("brand-specific verification"),
2027 status, B_FALSE);
2029 return ((err == ZONE_SUBPROC_OK) ? Z_OK : Z_BRAND_ERROR);
2032 static int
2033 verify_rctls(zone_dochandle_t handle)
2035 struct zone_rctltab rctltab;
2036 size_t rbs = rctlblk_size();
2037 rctlblk_t *rctlblk;
2038 int error = Z_INVAL;
2040 if ((rctlblk = malloc(rbs)) == NULL) {
2041 zerror(gettext("failed to allocate %lu bytes: %s"), rbs,
2042 strerror(errno));
2043 return (Z_NOMEM);
2046 if (zonecfg_setrctlent(handle) != Z_OK) {
2047 zerror(gettext("zonecfg_setrctlent failed"));
2048 free(rctlblk);
2049 return (error);
2052 rctltab.zone_rctl_valptr = NULL;
2053 while (zonecfg_getrctlent(handle, &rctltab) == Z_OK) {
2054 struct zone_rctlvaltab *rctlval;
2055 const char *name = rctltab.zone_rctl_name;
2057 if (!zonecfg_is_rctl(name)) {
2058 zerror(gettext("WARNING: Ignoring unrecognized rctl "
2059 "'%s'."), name);
2060 zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
2061 rctltab.zone_rctl_valptr = NULL;
2062 continue;
2065 for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL;
2066 rctlval = rctlval->zone_rctlval_next) {
2067 if (zonecfg_construct_rctlblk(rctlval, rctlblk)
2068 != Z_OK) {
2069 zerror(gettext("invalid rctl value: "
2070 "(priv=%s,limit=%s,action%s)"),
2071 rctlval->zone_rctlval_priv,
2072 rctlval->zone_rctlval_limit,
2073 rctlval->zone_rctlval_action);
2074 goto out;
2076 if (!zonecfg_valid_rctl(name, rctlblk)) {
2077 zerror(gettext("(priv=%s,limit=%s,action=%s) "
2078 "is not a valid value for rctl '%s'"),
2079 rctlval->zone_rctlval_priv,
2080 rctlval->zone_rctlval_limit,
2081 rctlval->zone_rctlval_action,
2082 name);
2083 goto out;
2086 zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
2088 rctltab.zone_rctl_valptr = NULL;
2089 error = Z_OK;
2090 out:
2091 zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
2092 (void) zonecfg_endrctlent(handle);
2093 free(rctlblk);
2094 return (error);
2097 static int
2098 verify_pool(zone_dochandle_t handle)
2100 char poolname[MAXPATHLEN];
2101 pool_conf_t *poolconf;
2102 pool_t *pool;
2103 int status;
2104 int error;
2107 * This ends up being very similar to the check done in zoneadmd.
2109 error = zonecfg_get_pool(handle, poolname, sizeof (poolname));
2110 if (error == Z_NO_ENTRY || (error == Z_OK && strlen(poolname) == 0)) {
2112 * No pool specified.
2114 return (0);
2116 if (error != Z_OK) {
2117 zperror(gettext("Unable to retrieve pool name from "
2118 "configuration"), B_TRUE);
2119 return (error);
2122 * Don't do anything if pools aren't enabled.
2124 if (pool_get_status(&status) != PO_SUCCESS || status != POOL_ENABLED) {
2125 zerror(gettext("WARNING: pools facility not active; "
2126 "zone will not be bound to pool '%s'."), poolname);
2127 return (Z_OK);
2130 * Try to provide a sane error message if the requested pool doesn't
2131 * exist. It isn't clear that pools-related failures should
2132 * necessarily translate to a failure to verify the zone configuration,
2133 * hence they are not considered errors.
2135 if ((poolconf = pool_conf_alloc()) == NULL) {
2136 zerror(gettext("WARNING: pool_conf_alloc failed; "
2137 "using default pool"));
2138 return (Z_OK);
2140 if (pool_conf_open(poolconf, pool_dynamic_location(), PO_RDONLY) !=
2141 PO_SUCCESS) {
2142 zerror(gettext("WARNING: pool_conf_open failed; "
2143 "using default pool"));
2144 pool_conf_free(poolconf);
2145 return (Z_OK);
2147 pool = pool_get_pool(poolconf, poolname);
2148 (void) pool_conf_close(poolconf);
2149 pool_conf_free(poolconf);
2150 if (pool == NULL) {
2151 zerror(gettext("WARNING: pool '%s' not found. "
2152 "using default pool"), poolname);
2155 return (Z_OK);
2159 * Verify that the special device/file system exists and is valid.
2161 static int
2162 verify_fs_special(struct zone_fstab *fstab)
2164 struct stat64 st;
2167 * This validation is really intended for standard zone administration.
2168 * If we are in a mini-root or some other upgrade situation where
2169 * we are using the scratch zone, just by-pass this.
2171 if (zonecfg_in_alt_root())
2172 return (Z_OK);
2174 if (strcmp(fstab->zone_fs_type, MNTTYPE_ZFS) == 0)
2175 return (verify_fs_zfs(fstab));
2177 if (stat64(fstab->zone_fs_special, &st) != 0) {
2178 (void) fprintf(stderr, gettext("could not verify fs "
2179 "%s: could not access %s: %s\n"), fstab->zone_fs_dir,
2180 fstab->zone_fs_special, strerror(errno));
2181 return (Z_ERR);
2184 if (strcmp(st.st_fstype, MNTTYPE_NFS) == 0) {
2186 * TRANSLATION_NOTE
2187 * fs and NFS are literals that should
2188 * not be translated.
2190 (void) fprintf(stderr, gettext("cannot verify "
2191 "fs %s: NFS mounted file system.\n"
2192 "\tA local file system must be used.\n"),
2193 fstab->zone_fs_special);
2194 return (Z_ERR);
2197 return (Z_OK);
2200 static int
2201 isregfile(const char *path)
2203 struct stat64 st;
2205 if (stat64(path, &st) == -1)
2206 return (-1);
2208 return (S_ISREG(st.st_mode));
2211 static int
2212 verify_filesystems(zone_dochandle_t handle)
2214 int return_code = Z_OK;
2215 struct zone_fstab fstab;
2216 char cmdbuf[MAXPATHLEN];
2217 struct stat st;
2220 * Since the actual mount point is not known until the dependent mounts
2221 * are performed, we don't attempt any path validation here: that will
2222 * happen later when zoneadmd actually does the mounts.
2224 if (zonecfg_setfsent(handle) != Z_OK) {
2225 (void) fprintf(stderr, gettext("could not verify file systems: "
2226 "unable to enumerate mounts\n"));
2227 return (Z_ERR);
2229 while (zonecfg_getfsent(handle, &fstab) == Z_OK) {
2230 if (!zonecfg_valid_fs_type(fstab.zone_fs_type)) {
2231 (void) fprintf(stderr, gettext("cannot verify fs %s: "
2232 "type %s is not allowed.\n"), fstab.zone_fs_dir,
2233 fstab.zone_fs_type);
2234 return_code = Z_ERR;
2235 goto next_fs;
2238 * Verify /usr/lib/fs/<fstype>/mount exists.
2240 if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/mount",
2241 fstab.zone_fs_type) > sizeof (cmdbuf)) {
2242 (void) fprintf(stderr, gettext("cannot verify fs %s: "
2243 "type %s is too long.\n"), fstab.zone_fs_dir,
2244 fstab.zone_fs_type);
2245 return_code = Z_ERR;
2246 goto next_fs;
2248 if (stat(cmdbuf, &st) != 0) {
2249 (void) fprintf(stderr, gettext("could not verify fs "
2250 "%s: could not access %s: %s\n"), fstab.zone_fs_dir,
2251 cmdbuf, strerror(errno));
2252 return_code = Z_ERR;
2253 goto next_fs;
2255 if (!S_ISREG(st.st_mode)) {
2256 (void) fprintf(stderr, gettext("could not verify fs "
2257 "%s: %s is not a regular file\n"),
2258 fstab.zone_fs_dir, cmdbuf);
2259 return_code = Z_ERR;
2260 goto next_fs;
2263 * If zone_fs_raw is set, verify that there's an fsck
2264 * binary for it. If zone_fs_raw is not set, and it's
2265 * not a regular file (lofi mount), and there's an fsck
2266 * binary for it, complain.
2268 if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/fsck",
2269 fstab.zone_fs_type) > sizeof (cmdbuf)) {
2270 (void) fprintf(stderr, gettext("cannot verify fs %s: "
2271 "type %s is too long.\n"), fstab.zone_fs_dir,
2272 fstab.zone_fs_type);
2273 return_code = Z_ERR;
2274 goto next_fs;
2276 if (fstab.zone_fs_raw[0] != '\0' &&
2277 (stat(cmdbuf, &st) != 0 || !S_ISREG(st.st_mode))) {
2278 (void) fprintf(stderr, gettext("cannot verify fs %s: "
2279 "'raw' device specified but "
2280 "no fsck executable exists for %s\n"),
2281 fstab.zone_fs_dir, fstab.zone_fs_type);
2282 return_code = Z_ERR;
2283 goto next_fs;
2284 } else if (fstab.zone_fs_raw[0] == '\0' &&
2285 stat(cmdbuf, &st) == 0 &&
2286 isregfile(fstab.zone_fs_special) != 1) {
2287 (void) fprintf(stderr, gettext("could not verify fs "
2288 "%s: must specify 'raw' device for %s "
2289 "file systems\n"),
2290 fstab.zone_fs_dir, fstab.zone_fs_type);
2291 return_code = Z_ERR;
2292 goto next_fs;
2295 /* Verify fs_special. */
2296 if ((return_code = verify_fs_special(&fstab)) != Z_OK)
2297 goto next_fs;
2299 /* Verify fs_raw. */
2300 if (fstab.zone_fs_raw[0] != '\0' &&
2301 stat(fstab.zone_fs_raw, &st) != 0) {
2303 * TRANSLATION_NOTE
2304 * fs is a literal that should not be translated.
2306 (void) fprintf(stderr, gettext("could not verify fs "
2307 "%s: could not access %s: %s\n"), fstab.zone_fs_dir,
2308 fstab.zone_fs_raw, strerror(errno));
2309 return_code = Z_ERR;
2310 goto next_fs;
2312 next_fs:
2313 zonecfg_free_fs_option_list(fstab.zone_fs_options);
2315 (void) zonecfg_endfsent(handle);
2317 return (return_code);
2320 static int
2321 verify_limitpriv(zone_dochandle_t handle)
2323 char *privname = NULL;
2324 int err;
2325 priv_set_t *privs;
2327 if ((privs = priv_allocset()) == NULL) {
2328 zperror(gettext("failed to allocate privilege set"), B_FALSE);
2329 return (Z_NOMEM);
2331 err = zonecfg_get_privset(handle, privs, &privname);
2332 switch (err) {
2333 case Z_OK:
2334 break;
2335 case Z_PRIV_PROHIBITED:
2336 (void) fprintf(stderr, gettext("privilege \"%s\" is not "
2337 "permitted within the zone's privilege set\n"), privname);
2338 break;
2339 case Z_PRIV_REQUIRED:
2340 (void) fprintf(stderr, gettext("required privilege \"%s\" is "
2341 "missing from the zone's privilege set\n"), privname);
2342 break;
2343 case Z_PRIV_UNKNOWN:
2344 (void) fprintf(stderr, gettext("unknown privilege \"%s\" "
2345 "specified in the zone's privilege set\n"), privname);
2346 break;
2347 default:
2348 zperror(
2349 gettext("failed to determine the zone's privilege set"),
2350 B_TRUE);
2351 break;
2353 free(privname);
2354 priv_freeset(privs);
2355 return (err);
2358 static void
2359 free_local_netifs(int if_cnt, struct net_if **if_list)
2361 int i;
2363 for (i = 0; i < if_cnt; i++) {
2364 free(if_list[i]->name);
2365 free(if_list[i]);
2367 free(if_list);
2371 * Get a list of the network interfaces, along with their address families,
2372 * that are plumbed in the global zone. See if_tcp(7p) for a description
2373 * of the ioctls used here.
2375 static int
2376 get_local_netifs(int *if_cnt, struct net_if ***if_list)
2378 int s;
2379 int i;
2380 int res = Z_OK;
2381 int space_needed;
2382 int cnt = 0;
2383 struct lifnum if_num;
2384 struct lifconf if_conf;
2385 struct lifreq *if_reqp;
2386 char *if_buf;
2387 struct net_if **local_ifs = NULL;
2389 *if_cnt = 0;
2390 *if_list = NULL;
2392 if ((s = socket(SOCKET_AF(AF_INET), SOCK_DGRAM, 0)) < 0)
2393 return (Z_ERR);
2396 * Come back here in the unlikely event that the number of interfaces
2397 * increases between the time we get the count and the time we do the
2398 * SIOCGLIFCONF ioctl.
2400 retry:
2401 /* Get the number of interfaces. */
2402 if_num.lifn_family = AF_UNSPEC;
2403 if_num.lifn_flags = LIFC_NOXMIT;
2404 if (ioctl(s, SIOCGLIFNUM, &if_num) < 0) {
2405 (void) close(s);
2406 return (Z_ERR);
2409 /* Get the interface configuration list. */
2410 space_needed = if_num.lifn_count * sizeof (struct lifreq);
2411 if ((if_buf = malloc(space_needed)) == NULL) {
2412 (void) close(s);
2413 return (Z_ERR);
2415 if_conf.lifc_family = AF_UNSPEC;
2416 if_conf.lifc_flags = LIFC_NOXMIT;
2417 if_conf.lifc_len = space_needed;
2418 if_conf.lifc_buf = if_buf;
2419 if (ioctl(s, SIOCGLIFCONF, &if_conf) < 0) {
2420 free(if_buf);
2422 * SIOCGLIFCONF returns EINVAL if the buffer we passed in is
2423 * too small. In this case go back and get the new if cnt.
2425 if (errno == EINVAL)
2426 goto retry;
2428 (void) close(s);
2429 return (Z_ERR);
2431 (void) close(s);
2433 /* Get the name and address family for each interface. */
2434 if_reqp = if_conf.lifc_req;
2435 for (i = 0; i < (if_conf.lifc_len / sizeof (struct lifreq)); i++) {
2436 struct net_if **p;
2437 struct lifreq req;
2439 if (strcmp(LOOPBACK_IF, if_reqp->lifr_name) == 0) {
2440 if_reqp++;
2441 continue;
2444 if ((s = socket(SOCKET_AF(if_reqp->lifr_addr.ss_family),
2445 SOCK_DGRAM, 0)) == -1) {
2446 res = Z_ERR;
2447 break;
2450 (void) strncpy(req.lifr_name, if_reqp->lifr_name,
2451 sizeof (req.lifr_name));
2452 if (ioctl(s, SIOCGLIFADDR, &req) < 0) {
2453 (void) close(s);
2454 if_reqp++;
2455 continue;
2458 if ((p = (struct net_if **)realloc(local_ifs,
2459 sizeof (struct net_if *) * (cnt + 1))) == NULL) {
2460 res = Z_ERR;
2461 break;
2463 local_ifs = p;
2465 if ((local_ifs[cnt] = malloc(sizeof (struct net_if))) == NULL) {
2466 res = Z_ERR;
2467 break;
2470 if ((local_ifs[cnt]->name = strdup(if_reqp->lifr_name))
2471 == NULL) {
2472 free(local_ifs[cnt]);
2473 res = Z_ERR;
2474 break;
2476 local_ifs[cnt]->af = req.lifr_addr.ss_family;
2477 cnt++;
2479 (void) close(s);
2480 if_reqp++;
2483 free(if_buf);
2485 if (res != Z_OK) {
2486 free_local_netifs(cnt, local_ifs);
2487 } else {
2488 *if_cnt = cnt;
2489 *if_list = local_ifs;
2492 return (res);
2495 static char *
2496 af2str(int af)
2498 switch (af) {
2499 case AF_INET:
2500 return ("IPv4");
2501 case AF_INET6:
2502 return ("IPv6");
2503 default:
2504 return ("Unknown");
2509 * Cross check the network interface name and address family with the
2510 * interfaces that are set up in the global zone so that we can print the
2511 * appropriate error message.
2513 static void
2514 print_net_err(char *phys, char *addr, int af, char *msg)
2516 int i;
2517 int local_if_cnt = 0;
2518 struct net_if **local_ifs = NULL;
2519 boolean_t found_if = B_FALSE;
2520 boolean_t found_af = B_FALSE;
2522 if (get_local_netifs(&local_if_cnt, &local_ifs) != Z_OK) {
2523 (void) fprintf(stderr,
2524 gettext("could not verify %s %s=%s %s=%s\n\t%s\n"),
2525 "net", "address", addr, "physical", phys, msg);
2526 return;
2529 for (i = 0; i < local_if_cnt; i++) {
2530 if (strcmp(phys, local_ifs[i]->name) == 0) {
2531 found_if = B_TRUE;
2532 if (af == local_ifs[i]->af) {
2533 found_af = B_TRUE;
2534 break;
2539 free_local_netifs(local_if_cnt, local_ifs);
2541 if (!found_if) {
2542 (void) fprintf(stderr,
2543 gettext("could not verify %s %s=%s\n\t"
2544 "network interface %s is not plumbed in the global zone\n"),
2545 "net", "physical", phys, phys);
2546 return;
2550 * Print this error if we were unable to find the address family
2551 * for this interface. If the af variable is not initialized to
2552 * to something meaningful by the caller (not AF_UNSPEC) then we
2553 * also skip this message since it wouldn't be informative.
2555 if (!found_af && af != AF_UNSPEC) {
2556 (void) fprintf(stderr,
2557 gettext("could not verify %s %s=%s %s=%s\n\tthe %s address "
2558 "family is not configured on this network interface in "
2559 "the\n\tglobal zone\n"),
2560 "net", "address", addr, "physical", phys, af2str(af));
2561 return;
2564 (void) fprintf(stderr,
2565 gettext("could not verify %s %s=%s %s=%s\n\t%s\n"),
2566 "net", "address", addr, "physical", phys, msg);
2569 static int
2570 verify_handle(int cmd_num, zone_dochandle_t handle, char *argv[])
2572 struct zone_nwiftab nwiftab;
2573 int return_code = Z_OK;
2574 int err;
2575 boolean_t in_alt_root;
2576 zone_iptype_t iptype;
2577 dladm_handle_t dh;
2578 dladm_status_t status;
2579 datalink_id_t linkid;
2580 char errmsg[DLADM_STRSIZE];
2582 in_alt_root = zonecfg_in_alt_root();
2583 if (in_alt_root)
2584 goto no_net;
2586 if ((err = zonecfg_get_iptype(handle, &iptype)) != Z_OK) {
2587 errno = err;
2588 zperror(cmd_to_str(cmd_num), B_TRUE);
2589 zonecfg_fini_handle(handle);
2590 return (Z_ERR);
2592 if ((err = zonecfg_setnwifent(handle)) != Z_OK) {
2593 errno = err;
2594 zperror(cmd_to_str(cmd_num), B_TRUE);
2595 zonecfg_fini_handle(handle);
2596 return (Z_ERR);
2598 while (zonecfg_getnwifent(handle, &nwiftab) == Z_OK) {
2599 struct lifreq lifr;
2600 sa_family_t af = AF_UNSPEC;
2601 char dl_owner_zname[ZONENAME_MAX];
2602 zoneid_t dl_owner_zid;
2603 zoneid_t target_zid;
2604 int res;
2606 /* skip any loopback interfaces */
2607 if (strcmp(nwiftab.zone_nwif_physical, "lo0") == 0)
2608 continue;
2609 switch (iptype) {
2610 case ZS_SHARED:
2611 if ((res = zonecfg_valid_net_address(
2612 nwiftab.zone_nwif_address, &lifr)) != Z_OK) {
2613 print_net_err(nwiftab.zone_nwif_physical,
2614 nwiftab.zone_nwif_address, af,
2615 zonecfg_strerror(res));
2616 return_code = Z_ERR;
2617 continue;
2619 af = lifr.lifr_addr.ss_family;
2620 if (!zonecfg_ifname_exists(af,
2621 nwiftab.zone_nwif_physical)) {
2623 * The interface failed to come up. We continue
2624 * on anyway for the sake of consistency: a
2625 * zone is not shut down if the interface fails
2626 * any time after boot, nor does the global zone
2627 * fail to boot if an interface fails.
2629 (void) fprintf(stderr,
2630 gettext("WARNING: skipping network "
2631 "interface '%s' which may not be "
2632 "present/plumbed in the global "
2633 "zone.\n"),
2634 nwiftab.zone_nwif_physical);
2636 break;
2637 case ZS_EXCLUSIVE:
2638 /* Warning if it exists for either IPv4 or IPv6 */
2640 if (zonecfg_ifname_exists(AF_INET,
2641 nwiftab.zone_nwif_physical) ||
2642 zonecfg_ifname_exists(AF_INET6,
2643 nwiftab.zone_nwif_physical)) {
2644 (void) fprintf(stderr,
2645 gettext("WARNING: skipping network "
2646 "interface '%s' which is used in the "
2647 "global zone.\n"),
2648 nwiftab.zone_nwif_physical);
2649 break;
2653 * Verify that the datalink exists and that it isn't
2654 * already assigned to a zone.
2656 if ((status = dladm_open(&dh)) == DLADM_STATUS_OK) {
2657 status = dladm_name2info(dh,
2658 nwiftab.zone_nwif_physical, &linkid, NULL,
2659 NULL, NULL);
2660 dladm_close(dh);
2662 if (status != DLADM_STATUS_OK) {
2663 (void) fprintf(stderr,
2664 gettext("WARNING: skipping network "
2665 "interface '%s': %s\n"),
2666 nwiftab.zone_nwif_physical,
2667 dladm_status2str(status, errmsg));
2668 break;
2670 dl_owner_zid = ALL_ZONES;
2671 if (zone_check_datalink(&dl_owner_zid, linkid) != 0)
2672 break;
2675 * If the zone being verified is
2676 * running and owns the interface
2678 target_zid = getzoneidbyname(target_zone);
2679 if (target_zid == dl_owner_zid)
2680 break;
2682 /* Zone id match failed, use name to check */
2683 if (getzonenamebyid(dl_owner_zid, dl_owner_zname,
2684 ZONENAME_MAX) < 0) {
2685 /* No name, show ID instead */
2686 (void) snprintf(dl_owner_zname, ZONENAME_MAX,
2687 "<%d>", dl_owner_zid);
2688 } else if (strcmp(dl_owner_zname, target_zone) == 0)
2689 break;
2692 * Note here we only report a warning that
2693 * the interface is already in use by another
2694 * running zone, and the verify process just
2695 * goes on, if the interface is still in use
2696 * when this zone really boots up, zoneadmd
2697 * will find it. If the name of the zone which
2698 * owns this interface cannot be determined,
2699 * then it is not possible to determine if there
2700 * is a conflict so just report it as a warning.
2702 (void) fprintf(stderr,
2703 gettext("WARNING: skipping network interface "
2704 "'%s' which is used by the non-global zone "
2705 "'%s'.\n"), nwiftab.zone_nwif_physical,
2706 dl_owner_zname);
2707 break;
2710 (void) zonecfg_endnwifent(handle);
2711 no_net:
2713 /* verify that lofs has not been excluded from the kernel */
2714 if (!(cmd_num == CMD_DETACH || cmd_num == CMD_ATTACH ||
2715 cmd_num == CMD_MOVE || cmd_num == CMD_CLONE) &&
2716 modctl(MODLOAD, 1, "fs/lofs", NULL) != 0) {
2717 if (errno == ENXIO)
2718 (void) fprintf(stderr, gettext("could not verify "
2719 "lofs(7FS): possibly excluded in /etc/system\n"));
2720 else
2721 (void) fprintf(stderr, gettext("could not verify "
2722 "lofs(7FS): %s\n"), strerror(errno));
2723 return_code = Z_ERR;
2726 if (verify_filesystems(handle) != Z_OK)
2727 return_code = Z_ERR;
2728 if (!in_alt_root && verify_rctls(handle) != Z_OK)
2729 return_code = Z_ERR;
2730 if (!in_alt_root && verify_pool(handle) != Z_OK)
2731 return_code = Z_ERR;
2732 if (!in_alt_root && verify_brand(handle, cmd_num, argv) != Z_OK)
2733 return_code = Z_ERR;
2734 if (!in_alt_root && verify_datasets(handle) != Z_OK)
2735 return_code = Z_ERR;
2738 * As the "mount" command is used for patching/upgrading of zones
2739 * or other maintenance processes, the zone's privilege set is not
2740 * checked in this case. Instead, the default, safe set of
2741 * privileges will be used when this zone is created in the
2742 * kernel.
2744 if (!in_alt_root && cmd_num != CMD_MOUNT &&
2745 verify_limitpriv(handle) != Z_OK)
2746 return_code = Z_ERR;
2748 return (return_code);
2751 static int
2752 verify_details(int cmd_num, char *argv[])
2754 zone_dochandle_t handle;
2755 char zonepath[MAXPATHLEN], checkpath[MAXPATHLEN];
2756 int return_code = Z_OK;
2757 int err;
2759 if ((handle = zonecfg_init_handle()) == NULL) {
2760 zperror(cmd_to_str(cmd_num), B_TRUE);
2761 return (Z_ERR);
2763 if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) {
2764 errno = err;
2765 zperror(cmd_to_str(cmd_num), B_TRUE);
2766 zonecfg_fini_handle(handle);
2767 return (Z_ERR);
2769 if ((err = zonecfg_get_zonepath(handle, zonepath, sizeof (zonepath))) !=
2770 Z_OK) {
2771 errno = err;
2772 zperror(cmd_to_str(cmd_num), B_TRUE);
2773 zonecfg_fini_handle(handle);
2774 return (Z_ERR);
2777 * zonecfg_get_zonepath() gets its data from the XML repository.
2778 * Verify this against the index file, which is checked first by
2779 * zone_get_zonepath(). If they don't match, bail out.
2781 if ((err = zone_get_zonepath(target_zone, checkpath,
2782 sizeof (checkpath))) != Z_OK) {
2783 errno = err;
2784 zperror2(target_zone, gettext("could not get zone path"));
2785 zonecfg_fini_handle(handle);
2786 return (Z_ERR);
2788 if (strcmp(zonepath, checkpath) != 0) {
2790 * TRANSLATION_NOTE
2791 * XML and zonepath are literals that should not be translated.
2793 (void) fprintf(stderr, gettext("The XML repository has "
2794 "zonepath '%s',\nbut the index file has zonepath '%s'.\n"
2795 "These must match, so fix the incorrect entry.\n"),
2796 zonepath, checkpath);
2797 zonecfg_fini_handle(handle);
2798 return (Z_ERR);
2800 if (cmd_num != CMD_ATTACH &&
2801 validate_zonepath(zonepath, cmd_num) != Z_OK) {
2802 (void) fprintf(stderr, gettext("could not verify zonepath %s "
2803 "because of the above errors.\n"), zonepath);
2804 return_code = Z_ERR;
2807 if (verify_handle(cmd_num, handle, argv) != Z_OK)
2808 return_code = Z_ERR;
2810 zonecfg_fini_handle(handle);
2811 if (return_code == Z_ERR)
2812 (void) fprintf(stderr,
2813 gettext("%s: zone %s failed to verify\n"),
2814 execname, target_zone);
2815 return (return_code);
2818 static int
2819 verify_func(int argc, char *argv[])
2821 int arg;
2823 optind = 0;
2824 if ((arg = getopt(argc, argv, "?")) != EOF) {
2825 switch (arg) {
2826 case '?':
2827 sub_usage(SHELP_VERIFY, CMD_VERIFY);
2828 return (optopt == '?' ? Z_OK : Z_USAGE);
2829 default:
2830 sub_usage(SHELP_VERIFY, CMD_VERIFY);
2831 return (Z_USAGE);
2834 if (argc > optind) {
2835 sub_usage(SHELP_VERIFY, CMD_VERIFY);
2836 return (Z_USAGE);
2838 if (sanity_check(target_zone, CMD_VERIFY, B_FALSE, B_FALSE, B_FALSE)
2839 != Z_OK)
2840 return (Z_ERR);
2841 return (verify_details(CMD_VERIFY, argv));
2844 static int
2845 addoptions(char *buf, char *argv[], size_t len)
2847 int i = 0;
2849 if (buf[0] == '\0')
2850 return (Z_OK);
2852 while (argv[i] != NULL) {
2853 if (strlcat(buf, " ", len) >= len ||
2854 strlcat(buf, argv[i++], len) >= len) {
2855 zerror("Command line too long");
2856 return (Z_ERR);
2860 return (Z_OK);
2863 static int
2864 addopt(char *buf, int opt, char *optarg, size_t bufsize)
2866 char optstring[4];
2868 if (opt > 0)
2869 (void) sprintf(optstring, " -%c", opt);
2870 else
2871 (void) strcpy(optstring, " ");
2873 if ((strlcat(buf, optstring, bufsize) > bufsize))
2874 return (Z_ERR);
2876 if ((optarg != NULL) && (strlcat(buf, optarg, bufsize) > bufsize))
2877 return (Z_ERR);
2879 return (Z_OK);
2882 /* ARGSUSED */
2883 static int
2884 install_func(int argc, char *argv[])
2886 char cmdbuf[MAXPATHLEN];
2887 char postcmdbuf[MAXPATHLEN];
2888 int lockfd;
2889 int arg, err, subproc_err;
2890 char zonepath[MAXPATHLEN];
2891 brand_handle_t bh = NULL;
2892 int status;
2893 boolean_t do_postinstall = B_FALSE;
2894 boolean_t brand_help = B_FALSE;
2895 char opts[128];
2897 if (target_zone == NULL) {
2898 sub_usage(SHELP_INSTALL, CMD_INSTALL);
2899 return (Z_USAGE);
2902 if (zonecfg_in_alt_root()) {
2903 zerror(gettext("cannot install zone in alternate root"));
2904 return (Z_ERR);
2907 if ((err = zone_get_zonepath(target_zone, zonepath,
2908 sizeof (zonepath))) != Z_OK) {
2909 errno = err;
2910 zperror2(target_zone, gettext("could not get zone path"));
2911 return (Z_ERR);
2914 /* Fetch the install command from the brand configuration. */
2915 if ((bh = brand_open(target_brand)) == NULL) {
2916 zerror(gettext("missing or invalid brand"));
2917 return (Z_ERR);
2920 if (get_hook(bh, cmdbuf, sizeof (cmdbuf), brand_get_install,
2921 target_zone, zonepath) != Z_OK) {
2922 zerror("invalid brand configuration: missing install resource");
2923 brand_close(bh);
2924 return (Z_ERR);
2927 if (get_hook(bh, postcmdbuf, sizeof (postcmdbuf), brand_get_postinstall,
2928 target_zone, zonepath) != Z_OK) {
2929 zerror("invalid brand configuration: missing postinstall "
2930 "resource");
2931 brand_close(bh);
2932 return (Z_ERR);
2935 if (postcmdbuf[0] != '\0')
2936 do_postinstall = B_TRUE;
2938 (void) strcpy(opts, "?x:");
2940 * Fetch the list of recognized command-line options from
2941 * the brand configuration file.
2943 if (brand_get_installopts(bh, opts + strlen(opts),
2944 sizeof (opts) - strlen(opts)) != 0) {
2945 zerror("invalid brand configuration: missing "
2946 "install options resource");
2947 brand_close(bh);
2948 return (Z_ERR);
2951 brand_close(bh);
2953 if (cmdbuf[0] == '\0') {
2954 zerror("Missing brand install command");
2955 return (Z_ERR);
2958 /* Check the argv string for args we handle internally */
2959 optind = 0;
2960 opterr = 0;
2961 while ((arg = getopt(argc, argv, opts)) != EOF) {
2962 switch (arg) {
2963 case '?':
2964 if (optopt == '?') {
2965 sub_usage(SHELP_INSTALL, CMD_INSTALL);
2966 brand_help = B_TRUE;
2968 /* Ignore unknown options - may be brand specific. */
2969 break;
2970 default:
2971 /* Ignore unknown options - may be brand specific. */
2972 break;
2976 * Append the option to the command line passed to the
2977 * brand-specific install and postinstall routines.
2979 if (addopt(cmdbuf, optopt, optarg, sizeof (cmdbuf)) != Z_OK) {
2980 zerror("Install command line too long");
2981 return (Z_ERR);
2983 if (addopt(postcmdbuf, optopt, optarg, sizeof (postcmdbuf))
2984 != Z_OK) {
2985 zerror("Post-Install command line too long");
2986 return (Z_ERR);
2990 for (; optind < argc; optind++) {
2991 if (addopt(cmdbuf, 0, argv[optind], sizeof (cmdbuf)) != Z_OK) {
2992 zerror("Install command line too long");
2993 return (Z_ERR);
2996 if (addopt(postcmdbuf, 0, argv[optind], sizeof (postcmdbuf))
2997 != Z_OK) {
2998 zerror("Post-Install command line too long");
2999 return (Z_ERR);
3003 if (!brand_help) {
3004 if (sanity_check(target_zone, CMD_INSTALL, B_FALSE, B_TRUE,
3005 B_FALSE) != Z_OK)
3006 return (Z_ERR);
3007 if (verify_details(CMD_INSTALL, argv) != Z_OK)
3008 return (Z_ERR);
3010 if (zonecfg_grab_lock_file(target_zone, &lockfd) != Z_OK) {
3011 zerror(gettext("another %s may have an operation in "
3012 "progress."), "zoneadm");
3013 return (Z_ERR);
3015 err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE);
3016 if (err != Z_OK) {
3017 errno = err;
3018 zperror2(target_zone, gettext("could not set state"));
3019 goto done;
3022 create_zfs_zonepath(zonepath);
3025 status = do_subproc(cmdbuf);
3026 if ((subproc_err =
3027 subproc_status(gettext("brand-specific installation"), status,
3028 B_FALSE)) != ZONE_SUBPROC_OK) {
3029 if (subproc_err == ZONE_SUBPROC_USAGE && !brand_help) {
3030 sub_usage(SHELP_INSTALL, CMD_INSTALL);
3031 zonecfg_release_lock_file(target_zone, lockfd);
3032 return (Z_ERR);
3034 errno = subproc_err;
3035 err = Z_ERR;
3036 goto done;
3039 if (brand_help)
3040 return (Z_OK);
3042 if ((err = zone_set_state(target_zone, ZONE_STATE_INSTALLED)) != Z_OK) {
3043 errno = err;
3044 zperror2(target_zone, gettext("could not set state"));
3045 goto done;
3048 if (do_postinstall) {
3049 status = do_subproc(postcmdbuf);
3051 if ((subproc_err =
3052 subproc_status(gettext("brand-specific post-install"),
3053 status, B_FALSE)) != ZONE_SUBPROC_OK) {
3054 errno = subproc_err;
3055 err = Z_ERR;
3056 (void) zone_set_state(target_zone,
3057 ZONE_STATE_INCOMPLETE);
3061 done:
3063 * If the install script exited with ZONE_SUBPROC_NOTCOMPLETE, try to
3064 * clean up the zone and leave the zone in the CONFIGURED state so that
3065 * another install can be attempted without requiring an uninstall
3066 * first.
3068 if (subproc_err == ZONE_SUBPROC_NOTCOMPLETE) {
3069 int temp_err;
3071 if ((temp_err = cleanup_zonepath(zonepath, B_FALSE)) != Z_OK) {
3072 errno = err = temp_err;
3073 zperror2(target_zone,
3074 gettext("cleaning up zonepath failed"));
3075 } else if ((temp_err = zone_set_state(target_zone,
3076 ZONE_STATE_CONFIGURED)) != Z_OK) {
3077 errno = err = temp_err;
3078 zperror2(target_zone, gettext("could not set state"));
3082 if (!brand_help)
3083 zonecfg_release_lock_file(target_zone, lockfd);
3084 return ((err == Z_OK) ? Z_OK : Z_ERR);
3087 static void
3088 warn_dev_match(zone_dochandle_t s_handle, char *source_zone,
3089 zone_dochandle_t t_handle, char *target_zone)
3091 int err;
3092 struct zone_devtab s_devtab;
3093 struct zone_devtab t_devtab;
3095 if ((err = zonecfg_setdevent(t_handle)) != Z_OK) {
3096 errno = err;
3097 zperror2(target_zone, gettext("could not enumerate devices"));
3098 return;
3101 while (zonecfg_getdevent(t_handle, &t_devtab) == Z_OK) {
3102 if ((err = zonecfg_setdevent(s_handle)) != Z_OK) {
3103 errno = err;
3104 zperror2(source_zone,
3105 gettext("could not enumerate devices"));
3106 (void) zonecfg_enddevent(t_handle);
3107 return;
3110 while (zonecfg_getdevent(s_handle, &s_devtab) == Z_OK) {
3112 * Use fnmatch to catch the case where wildcards
3113 * were used in one zone and the other has an
3114 * explicit entry (e.g. /dev/dsk/c0t0d0s6 vs.
3115 * /dev/\*dsk/c0t0d0s6).
3117 if (fnmatch(t_devtab.zone_dev_match,
3118 s_devtab.zone_dev_match, FNM_PATHNAME) == 0 ||
3119 fnmatch(s_devtab.zone_dev_match,
3120 t_devtab.zone_dev_match, FNM_PATHNAME) == 0) {
3121 (void) fprintf(stderr,
3122 gettext("WARNING: device '%s' "
3123 "is configured in both zones.\n"),
3124 t_devtab.zone_dev_match);
3125 break;
3128 (void) zonecfg_enddevent(s_handle);
3131 (void) zonecfg_enddevent(t_handle);
3135 * Check if the specified mount option (opt) is contained within the
3136 * options string.
3138 static boolean_t
3139 opt_match(char *opt, char *options)
3141 char *p;
3142 char *lastp;
3144 if ((p = strtok_r(options, ",", &lastp)) != NULL) {
3145 if (strcmp(p, opt) == 0)
3146 return (B_TRUE);
3147 while ((p = strtok_r(NULL, ",", &lastp)) != NULL) {
3148 if (strcmp(p, opt) == 0)
3149 return (B_TRUE);
3153 return (B_FALSE);
3156 #define RW_LOFS "WARNING: read-write lofs file system on '%s' is configured " \
3157 "in both zones.\n"
3159 static void
3160 print_fs_warnings(struct zone_fstab *s_fstab, struct zone_fstab *t_fstab)
3163 * It is ok to have shared lofs mounted fs but we want to warn if
3164 * either is rw since this will effect the other zone.
3166 if (strcmp(t_fstab->zone_fs_type, "lofs") == 0) {
3167 zone_fsopt_t *optp;
3169 /* The default is rw so no options means rw */
3170 if (t_fstab->zone_fs_options == NULL ||
3171 s_fstab->zone_fs_options == NULL) {
3172 (void) fprintf(stderr, gettext(RW_LOFS),
3173 t_fstab->zone_fs_special);
3174 return;
3177 for (optp = s_fstab->zone_fs_options; optp != NULL;
3178 optp = optp->zone_fsopt_next) {
3179 if (opt_match("rw", optp->zone_fsopt_opt)) {
3180 (void) fprintf(stderr, gettext(RW_LOFS),
3181 s_fstab->zone_fs_special);
3182 return;
3186 for (optp = t_fstab->zone_fs_options; optp != NULL;
3187 optp = optp->zone_fsopt_next) {
3188 if (opt_match("rw", optp->zone_fsopt_opt)) {
3189 (void) fprintf(stderr, gettext(RW_LOFS),
3190 t_fstab->zone_fs_special);
3191 return;
3195 return;
3199 * TRANSLATION_NOTE
3200 * The first variable is the file system type and the second is
3201 * the file system special device. For example,
3202 * WARNING: ufs file system on '/dev/dsk/c0t0d0s0' ...
3204 (void) fprintf(stderr, gettext("WARNING: %s file system on '%s' "
3205 "is configured in both zones.\n"), t_fstab->zone_fs_type,
3206 t_fstab->zone_fs_special);
3209 static void
3210 warn_fs_match(zone_dochandle_t s_handle, char *source_zone,
3211 zone_dochandle_t t_handle, char *target_zone)
3213 int err;
3214 struct zone_fstab s_fstab;
3215 struct zone_fstab t_fstab;
3217 if ((err = zonecfg_setfsent(t_handle)) != Z_OK) {
3218 errno = err;
3219 zperror2(target_zone,
3220 gettext("could not enumerate file systems"));
3221 return;
3224 while (zonecfg_getfsent(t_handle, &t_fstab) == Z_OK) {
3225 if ((err = zonecfg_setfsent(s_handle)) != Z_OK) {
3226 errno = err;
3227 zperror2(source_zone,
3228 gettext("could not enumerate file systems"));
3229 (void) zonecfg_endfsent(t_handle);
3230 return;
3233 while (zonecfg_getfsent(s_handle, &s_fstab) == Z_OK) {
3234 if (strcmp(t_fstab.zone_fs_special,
3235 s_fstab.zone_fs_special) == 0) {
3236 print_fs_warnings(&s_fstab, &t_fstab);
3237 break;
3240 (void) zonecfg_endfsent(s_handle);
3243 (void) zonecfg_endfsent(t_handle);
3247 * We don't catch the case where you used the same IP address but
3248 * it is not an exact string match. For example, 192.9.0.128 vs. 192.09.0.128.
3249 * However, we're not going to worry about that but we will check for
3250 * a possible netmask on one of the addresses (e.g. 10.0.0.1 and 10.0.0.1/24)
3251 * and handle that case as a match.
3253 static void
3254 warn_ip_match(zone_dochandle_t s_handle, char *source_zone,
3255 zone_dochandle_t t_handle, char *target_zone)
3257 int err;
3258 struct zone_nwiftab s_nwiftab;
3259 struct zone_nwiftab t_nwiftab;
3261 if ((err = zonecfg_setnwifent(t_handle)) != Z_OK) {
3262 errno = err;
3263 zperror2(target_zone,
3264 gettext("could not enumerate network interfaces"));
3265 return;
3268 while (zonecfg_getnwifent(t_handle, &t_nwiftab) == Z_OK) {
3269 char *p;
3271 /* remove an (optional) netmask from the address */
3272 if ((p = strchr(t_nwiftab.zone_nwif_address, '/')) != NULL)
3273 *p = '\0';
3275 if ((err = zonecfg_setnwifent(s_handle)) != Z_OK) {
3276 errno = err;
3277 zperror2(source_zone,
3278 gettext("could not enumerate network interfaces"));
3279 (void) zonecfg_endnwifent(t_handle);
3280 return;
3283 while (zonecfg_getnwifent(s_handle, &s_nwiftab) == Z_OK) {
3284 /* remove an (optional) netmask from the address */
3285 if ((p = strchr(s_nwiftab.zone_nwif_address, '/'))
3286 != NULL)
3287 *p = '\0';
3289 /* For exclusive-IP zones, address is not specified. */
3290 if (strlen(s_nwiftab.zone_nwif_address) == 0)
3291 continue;
3293 if (strcmp(t_nwiftab.zone_nwif_address,
3294 s_nwiftab.zone_nwif_address) == 0) {
3295 (void) fprintf(stderr,
3296 gettext("WARNING: network address '%s' "
3297 "is configured in both zones.\n"),
3298 t_nwiftab.zone_nwif_address);
3299 break;
3302 (void) zonecfg_endnwifent(s_handle);
3305 (void) zonecfg_endnwifent(t_handle);
3308 static void
3309 warn_dataset_match(zone_dochandle_t s_handle, char *source,
3310 zone_dochandle_t t_handle, char *target)
3312 int err;
3313 struct zone_dstab s_dstab;
3314 struct zone_dstab t_dstab;
3316 if ((err = zonecfg_setdsent(t_handle)) != Z_OK) {
3317 errno = err;
3318 zperror2(target, gettext("could not enumerate datasets"));
3319 return;
3322 while (zonecfg_getdsent(t_handle, &t_dstab) == Z_OK) {
3323 if ((err = zonecfg_setdsent(s_handle)) != Z_OK) {
3324 errno = err;
3325 zperror2(source,
3326 gettext("could not enumerate datasets"));
3327 (void) zonecfg_enddsent(t_handle);
3328 return;
3331 while (zonecfg_getdsent(s_handle, &s_dstab) == Z_OK) {
3332 if (strcmp(t_dstab.zone_dataset_name,
3333 s_dstab.zone_dataset_name) == 0) {
3334 target_zone = source;
3335 zerror(gettext("WARNING: dataset '%s' "
3336 "is configured in both zones.\n"),
3337 t_dstab.zone_dataset_name);
3338 break;
3341 (void) zonecfg_enddsent(s_handle);
3344 (void) zonecfg_enddsent(t_handle);
3348 * Check that the clone and its source have the same brand type.
3350 static int
3351 valid_brand_clone(char *source_zone, char *target_zone)
3353 brand_handle_t bh;
3354 char source_brand[MAXNAMELEN];
3356 if ((zone_get_brand(source_zone, source_brand,
3357 sizeof (source_brand))) != Z_OK) {
3358 (void) fprintf(stderr, "%s: zone '%s': %s\n",
3359 execname, source_zone, gettext("missing or invalid brand"));
3360 return (Z_ERR);
3363 if (strcmp(source_brand, target_brand) != 0) {
3364 (void) fprintf(stderr,
3365 gettext("%s: Zones '%s' and '%s' have different brand "
3366 "types.\n"), execname, source_zone, target_zone);
3367 return (Z_ERR);
3370 if ((bh = brand_open(target_brand)) == NULL) {
3371 zerror(gettext("missing or invalid brand"));
3372 return (Z_ERR);
3374 brand_close(bh);
3375 return (Z_OK);
3378 static int
3379 validate_clone(char *source_zone, char *target_zone)
3381 int err = Z_OK;
3382 zone_dochandle_t s_handle;
3383 zone_dochandle_t t_handle;
3385 if ((t_handle = zonecfg_init_handle()) == NULL) {
3386 zperror(cmd_to_str(CMD_CLONE), B_TRUE);
3387 return (Z_ERR);
3389 if ((err = zonecfg_get_handle(target_zone, t_handle)) != Z_OK) {
3390 errno = err;
3391 zperror(cmd_to_str(CMD_CLONE), B_TRUE);
3392 zonecfg_fini_handle(t_handle);
3393 return (Z_ERR);
3396 if ((s_handle = zonecfg_init_handle()) == NULL) {
3397 zperror(cmd_to_str(CMD_CLONE), B_TRUE);
3398 zonecfg_fini_handle(t_handle);
3399 return (Z_ERR);
3401 if ((err = zonecfg_get_handle(source_zone, s_handle)) != Z_OK) {
3402 errno = err;
3403 zperror(cmd_to_str(CMD_CLONE), B_TRUE);
3404 goto done;
3407 /* verify new zone has same brand type */
3408 err = valid_brand_clone(source_zone, target_zone);
3409 if (err != Z_OK)
3410 goto done;
3412 /* warn about imported fs's which are the same */
3413 warn_fs_match(s_handle, source_zone, t_handle, target_zone);
3415 /* warn about imported IP addresses which are the same */
3416 warn_ip_match(s_handle, source_zone, t_handle, target_zone);
3418 /* warn about imported devices which are the same */
3419 warn_dev_match(s_handle, source_zone, t_handle, target_zone);
3421 /* warn about imported datasets which are the same */
3422 warn_dataset_match(s_handle, source_zone, t_handle, target_zone);
3424 done:
3425 zonecfg_fini_handle(t_handle);
3426 zonecfg_fini_handle(s_handle);
3428 return ((err == Z_OK) ? Z_OK : Z_ERR);
3431 static int
3432 copy_zone(char *src, char *dst)
3434 boolean_t out_null = B_FALSE;
3435 int status;
3436 char *outfile;
3437 char cmdbuf[MAXPATHLEN * 2 + 128];
3439 if ((outfile = tempnam("/var/log", "zone")) == NULL) {
3440 outfile = "/dev/null";
3441 out_null = B_TRUE;
3445 * Use find to get the list of files to copy. We need to skip
3446 * files of type "socket" since cpio can't handle those but that
3447 * should be ok since the app will recreate the socket when it runs.
3448 * We also need to filter out anything under the .zfs subdir. Since
3449 * find is running depth-first, we need the extra egrep to filter .zfs.
3451 (void) snprintf(cmdbuf, sizeof (cmdbuf),
3452 "cd %s && /usr/bin/find . -type s -prune -o -depth -print | "
3453 "/usr/bin/egrep -v '^\\./\\.zfs$|^\\./\\.zfs/' | "
3454 "/usr/bin/cpio -pdmuP@ %s > %s 2>&1",
3455 src, dst, outfile);
3457 status = do_subproc(cmdbuf);
3459 if (subproc_status("copy", status, B_TRUE) != ZONE_SUBPROC_OK) {
3460 if (!out_null)
3461 (void) fprintf(stderr, gettext("\nThe copy failed.\n"
3462 "More information can be found in %s\n"), outfile);
3463 return (Z_ERR);
3466 if (!out_null)
3467 (void) unlink(outfile);
3469 return (Z_OK);
3472 /* ARGSUSED */
3474 zfm_print(const struct mnttab *p, void *r)
3476 zerror(" %s\n", p->mnt_mountp);
3477 return (0);
3481 clone_copy(char *source_zonepath, char *zonepath)
3483 int err;
3485 /* Don't clone the zone if anything is still mounted there */
3486 if (zonecfg_find_mounts(source_zonepath, NULL, NULL)) {
3487 zerror(gettext("These file systems are mounted on "
3488 "subdirectories of %s.\n"), source_zonepath);
3489 (void) zonecfg_find_mounts(source_zonepath, zfm_print, NULL);
3490 return (Z_ERR);
3494 * Attempt to create a ZFS fs for the zonepath. As usual, we don't
3495 * care if this works or not since we always have the default behavior
3496 * of a simple directory for the zonepath.
3498 create_zfs_zonepath(zonepath);
3500 (void) printf(gettext("Copying %s..."), source_zonepath);
3501 (void) fflush(stdout);
3503 err = copy_zone(source_zonepath, zonepath);
3505 (void) printf("\n");
3507 return (err);
3510 static int
3511 clone_func(int argc, char *argv[])
3513 char *source_zone = NULL;
3514 int lockfd;
3515 int err, arg;
3516 char zonepath[MAXPATHLEN];
3517 char source_zonepath[MAXPATHLEN];
3518 zone_state_t state;
3519 zone_entry_t *zent;
3520 char *method = NULL;
3521 char *snapshot = NULL;
3522 char cmdbuf[MAXPATHLEN];
3523 char postcmdbuf[MAXPATHLEN];
3524 char presnapbuf[MAXPATHLEN];
3525 char postsnapbuf[MAXPATHLEN];
3526 char validsnapbuf[MAXPATHLEN];
3527 brand_handle_t bh = NULL;
3528 int status;
3529 boolean_t brand_help = B_FALSE;
3531 if (zonecfg_in_alt_root()) {
3532 zerror(gettext("cannot clone zone in alternate root"));
3533 return (Z_ERR);
3536 /* Check the argv string for args we handle internally */
3537 optind = 0;
3538 opterr = 0;
3539 while ((arg = getopt(argc, argv, "?m:s:")) != EOF) {
3540 switch (arg) {
3541 case '?':
3542 if (optopt == '?') {
3543 sub_usage(SHELP_CLONE, CMD_CLONE);
3544 brand_help = B_TRUE;
3546 /* Ignore unknown options - may be brand specific. */
3547 break;
3548 case 'm':
3549 method = optarg;
3550 break;
3551 case 's':
3552 snapshot = optarg;
3553 break;
3554 default:
3555 /* Ignore unknown options - may be brand specific. */
3556 break;
3560 if (argc != (optind + 1)) {
3561 sub_usage(SHELP_CLONE, CMD_CLONE);
3562 return (Z_USAGE);
3565 source_zone = argv[optind];
3567 if (!brand_help) {
3568 if (sanity_check(target_zone, CMD_CLONE, B_FALSE, B_TRUE,
3569 B_FALSE) != Z_OK)
3570 return (Z_ERR);
3571 if (verify_details(CMD_CLONE, argv) != Z_OK)
3572 return (Z_ERR);
3575 * We also need to do some extra validation on the source zone.
3577 if (strcmp(source_zone, GLOBAL_ZONENAME) == 0) {
3578 zerror(gettext("%s operation is invalid for the "
3579 "global zone."), cmd_to_str(CMD_CLONE));
3580 return (Z_ERR);
3583 if (strncmp(source_zone, "SUNW", 4) == 0) {
3584 zerror(gettext("%s operation is invalid for zones "
3585 "starting with SUNW."), cmd_to_str(CMD_CLONE));
3586 return (Z_ERR);
3589 if (auth_check(username, source_zone, SOURCE_ZONE) == Z_ERR) {
3590 zerror(gettext("%s operation is invalid because "
3591 "user is not authorized to read source zone."),
3592 cmd_to_str(CMD_CLONE));
3593 return (Z_ERR);
3596 zent = lookup_running_zone(source_zone);
3597 if (zent != NULL) {
3598 /* check whether the zone is ready or running */
3599 if ((err = zone_get_state(zent->zname,
3600 &zent->zstate_num)) != Z_OK) {
3601 errno = err;
3602 zperror2(zent->zname, gettext("could not get "
3603 "state"));
3604 /* can't tell, so hedge */
3605 zent->zstate_str = "ready/running";
3606 } else {
3607 zent->zstate_str =
3608 zone_state_str(zent->zstate_num);
3610 zerror(gettext("%s operation is invalid for %s zones."),
3611 cmd_to_str(CMD_CLONE), zent->zstate_str);
3612 return (Z_ERR);
3615 if ((err = zone_get_state(source_zone, &state)) != Z_OK) {
3616 errno = err;
3617 zperror2(source_zone, gettext("could not get state"));
3618 return (Z_ERR);
3620 if (state != ZONE_STATE_INSTALLED) {
3621 (void) fprintf(stderr,
3622 gettext("%s: zone %s is %s; %s is required.\n"),
3623 execname, source_zone, zone_state_str(state),
3624 zone_state_str(ZONE_STATE_INSTALLED));
3625 return (Z_ERR);
3629 * The source zone checks out ok, continue with the clone.
3632 if (validate_clone(source_zone, target_zone) != Z_OK)
3633 return (Z_ERR);
3635 if (zonecfg_grab_lock_file(target_zone, &lockfd) != Z_OK) {
3636 zerror(gettext("another %s may have an operation in "
3637 "progress."), "zoneadm");
3638 return (Z_ERR);
3642 if ((err = zone_get_zonepath(source_zone, source_zonepath,
3643 sizeof (source_zonepath))) != Z_OK) {
3644 errno = err;
3645 zperror2(source_zone, gettext("could not get zone path"));
3646 goto done;
3649 if ((err = zone_get_zonepath(target_zone, zonepath, sizeof (zonepath)))
3650 != Z_OK) {
3651 errno = err;
3652 zperror2(target_zone, gettext("could not get zone path"));
3653 goto done;
3657 * Fetch the clone and postclone hooks from the brand configuration.
3659 if ((bh = brand_open(target_brand)) == NULL) {
3660 zerror(gettext("missing or invalid brand"));
3661 err = Z_ERR;
3662 goto done;
3665 if (get_hook(bh, cmdbuf, sizeof (cmdbuf), brand_get_clone, target_zone,
3666 zonepath) != Z_OK) {
3667 zerror("invalid brand configuration: missing clone resource");
3668 brand_close(bh);
3669 err = Z_ERR;
3670 goto done;
3673 if (get_hook(bh, postcmdbuf, sizeof (postcmdbuf), brand_get_postclone,
3674 target_zone, zonepath) != Z_OK) {
3675 zerror("invalid brand configuration: missing postclone "
3676 "resource");
3677 brand_close(bh);
3678 err = Z_ERR;
3679 goto done;
3682 if (get_hook(bh, presnapbuf, sizeof (presnapbuf), brand_get_presnap,
3683 source_zone, source_zonepath) != Z_OK) {
3684 zerror("invalid brand configuration: missing presnap "
3685 "resource");
3686 brand_close(bh);
3687 err = Z_ERR;
3688 goto done;
3691 if (get_hook(bh, postsnapbuf, sizeof (postsnapbuf), brand_get_postsnap,
3692 source_zone, source_zonepath) != Z_OK) {
3693 zerror("invalid brand configuration: missing postsnap "
3694 "resource");
3695 brand_close(bh);
3696 err = Z_ERR;
3697 goto done;
3700 if (get_hook(bh, validsnapbuf, sizeof (validsnapbuf),
3701 brand_get_validatesnap, target_zone, zonepath) != Z_OK) {
3702 zerror("invalid brand configuration: missing validatesnap "
3703 "resource");
3704 brand_close(bh);
3705 err = Z_ERR;
3706 goto done;
3708 brand_close(bh);
3710 /* Append all options to clone hook. */
3711 if (addoptions(cmdbuf, argv, sizeof (cmdbuf)) != Z_OK) {
3712 err = Z_ERR;
3713 goto done;
3716 /* Append all options to postclone hook. */
3717 if (addoptions(postcmdbuf, argv, sizeof (postcmdbuf)) != Z_OK) {
3718 err = Z_ERR;
3719 goto done;
3722 if (!brand_help) {
3723 if ((err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE))
3724 != Z_OK) {
3725 errno = err;
3726 zperror2(target_zone, gettext("could not set state"));
3727 goto done;
3732 * The clone hook is optional. If it exists, use the hook for
3733 * cloning, otherwise use the built-in clone support
3735 if (cmdbuf[0] != '\0') {
3736 /* Run the clone hook */
3737 status = do_subproc(cmdbuf);
3738 if ((status = subproc_status(gettext("brand-specific clone"),
3739 status, B_FALSE)) != ZONE_SUBPROC_OK) {
3740 if (status == ZONE_SUBPROC_USAGE && !brand_help)
3741 sub_usage(SHELP_CLONE, CMD_CLONE);
3742 err = Z_ERR;
3743 goto done;
3746 if (brand_help)
3747 return (Z_OK);
3749 } else {
3750 /* If just help, we're done since there is no brand help. */
3751 if (brand_help)
3752 return (Z_OK);
3754 /* Run the built-in clone support. */
3756 /* The only explicit built-in method is "copy". */
3757 if (method != NULL && strcmp(method, "copy") != 0) {
3758 sub_usage(SHELP_CLONE, CMD_CLONE);
3759 err = Z_USAGE;
3760 goto done;
3763 if (snapshot != NULL) {
3764 err = clone_snapshot_zfs(snapshot, zonepath,
3765 validsnapbuf);
3766 } else {
3768 * We always copy the clone unless the source is ZFS
3769 * and a ZFS clone worked. We fallback to copying if
3770 * the ZFS clone fails for some reason.
3772 err = Z_ERR;
3773 if (method == NULL && is_zonepath_zfs(source_zonepath))
3774 err = clone_zfs(source_zonepath, zonepath,
3775 presnapbuf, postsnapbuf);
3777 if (err != Z_OK)
3778 err = clone_copy(source_zonepath, zonepath);
3782 if (err == Z_OK && postcmdbuf[0] != '\0') {
3783 status = do_subproc(postcmdbuf);
3784 if ((err = subproc_status("postclone", status, B_FALSE))
3785 != ZONE_SUBPROC_OK) {
3786 zerror(gettext("post-clone configuration failed."));
3787 err = Z_ERR;
3791 done:
3793 * If everything went well, we mark the zone as installed.
3795 if (err == Z_OK) {
3796 err = zone_set_state(target_zone, ZONE_STATE_INSTALLED);
3797 if (err != Z_OK) {
3798 errno = err;
3799 zperror2(target_zone, gettext("could not set state"));
3802 if (!brand_help)
3803 zonecfg_release_lock_file(target_zone, lockfd);
3804 return ((err == Z_OK) ? Z_OK : Z_ERR);
3808 * Used when removing a zonepath after uninstalling or cleaning up after
3809 * the move subcommand. This handles a zonepath that has non-standard
3810 * contents so that we will only cleanup the stuff we know about and leave
3811 * any user data alone.
3813 * If the "all" parameter is true then we should remove the whole zonepath
3814 * even if it has non-standard files/directories in it. This can be used when
3815 * we need to cleanup after moving the zonepath across file systems.
3817 * We "exec" the RMCOMMAND so that the returned status is that of RMCOMMAND
3818 * and not the shell.
3820 static int
3821 cleanup_zonepath(char *zonepath, boolean_t all)
3823 int status;
3824 int i;
3825 boolean_t non_std = B_FALSE;
3826 struct dirent *dp;
3827 DIR *dirp;
3829 * The SUNWattached.xml file is expected since it might
3830 * exist if the zone was force-attached after a
3831 * migration.
3833 char *std_entries[] = {"dev", "lu", "root",
3834 "SUNWattached.xml", NULL};
3835 /* (MAXPATHLEN * 3) is for the 3 std_entries dirs */
3836 char cmdbuf[sizeof (RMCOMMAND) + (MAXPATHLEN * 3) + 64];
3839 * We shouldn't need these checks but lets be paranoid since we
3840 * could blow away the whole system here if we got the wrong zonepath.
3842 if (*zonepath == NULL || strcmp(zonepath, "/") == 0) {
3843 (void) fprintf(stderr, "invalid zonepath '%s'\n", zonepath);
3844 return (Z_INVAL);
3848 * If the dirpath is already gone (maybe it was manually removed) then
3849 * we just return Z_OK so that the cleanup is successful.
3851 if ((dirp = opendir(zonepath)) == NULL)
3852 return (Z_OK);
3855 * Look through the zonepath directory to see if there are any
3856 * non-standard files/dirs. Also skip .zfs since that might be
3857 * there but we'll handle ZFS file systems as a special case.
3859 while ((dp = readdir(dirp)) != NULL) {
3860 if (strcmp(dp->d_name, ".") == 0 ||
3861 strcmp(dp->d_name, "..") == 0 ||
3862 strcmp(dp->d_name, ".zfs") == 0)
3863 continue;
3865 for (i = 0; std_entries[i] != NULL; i++)
3866 if (strcmp(dp->d_name, std_entries[i]) == 0)
3867 break;
3869 if (std_entries[i] == NULL)
3870 non_std = B_TRUE;
3872 (void) closedir(dirp);
3874 if (!all && non_std) {
3876 * There are extra, non-standard directories/files in the
3877 * zonepath so we don't want to remove the zonepath. We
3878 * just want to remove the standard directories and leave
3879 * the user data alone.
3881 (void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND);
3883 for (i = 0; std_entries[i] != NULL; i++) {
3884 char tmpbuf[MAXPATHLEN];
3886 if (snprintf(tmpbuf, sizeof (tmpbuf), " %s/%s",
3887 zonepath, std_entries[i]) >= sizeof (tmpbuf) ||
3888 strlcat(cmdbuf, tmpbuf, sizeof (cmdbuf)) >=
3889 sizeof (cmdbuf)) {
3890 (void) fprintf(stderr,
3891 gettext("path is too long\n"));
3892 return (Z_INVAL);
3896 status = do_subproc(cmdbuf);
3898 (void) fprintf(stderr, gettext("WARNING: Unable to completely "
3899 "remove %s\nbecause it contains additional user data. "
3900 "Only the standard directory\nentries have been "
3901 "removed.\n"),
3902 zonepath);
3904 return ((subproc_status(RMCOMMAND, status, B_TRUE) ==
3905 ZONE_SUBPROC_OK) ? Z_OK : Z_ERR);
3909 * There is nothing unexpected in the zonepath, try to get rid of the
3910 * whole zonepath directory.
3912 * If the zonepath is its own zfs file system, try to destroy the
3913 * file system. If that fails for some reason (e.g. it has clones)
3914 * then we'll just remove the contents of the zonepath.
3916 if (is_zonepath_zfs(zonepath)) {
3917 if (destroy_zfs(zonepath) == Z_OK)
3918 return (Z_OK);
3919 (void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND
3920 " %s/*", zonepath);
3921 status = do_subproc(cmdbuf);
3922 return ((subproc_status(RMCOMMAND, status, B_TRUE) ==
3923 ZONE_SUBPROC_OK) ? Z_OK : Z_ERR);
3926 (void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND " %s",
3927 zonepath);
3928 status = do_subproc(cmdbuf);
3930 return ((subproc_status(RMCOMMAND, status, B_TRUE) == ZONE_SUBPROC_OK)
3931 ? Z_OK : Z_ERR);
3934 static int
3935 move_func(int argc, char *argv[])
3937 char *new_zonepath = NULL;
3938 int lockfd;
3939 int err, arg;
3940 char zonepath[MAXPATHLEN];
3941 zone_dochandle_t handle;
3942 boolean_t fast;
3943 boolean_t is_zfs = B_FALSE;
3944 boolean_t root_fs_mounted = B_FALSE;
3945 struct dirent *dp;
3946 DIR *dirp;
3947 boolean_t empty = B_TRUE;
3948 boolean_t revert;
3949 struct stat zonepath_buf;
3950 struct stat new_zonepath_buf;
3951 zone_mounts_t mounts;
3953 if (zonecfg_in_alt_root()) {
3954 zerror(gettext("cannot move zone in alternate root"));
3955 return (Z_ERR);
3958 optind = 0;
3959 if ((arg = getopt(argc, argv, "?")) != EOF) {
3960 switch (arg) {
3961 case '?':
3962 sub_usage(SHELP_MOVE, CMD_MOVE);
3963 return (optopt == '?' ? Z_OK : Z_USAGE);
3964 default:
3965 sub_usage(SHELP_MOVE, CMD_MOVE);
3966 return (Z_USAGE);
3969 if (argc != (optind + 1)) {
3970 sub_usage(SHELP_MOVE, CMD_MOVE);
3971 return (Z_USAGE);
3973 new_zonepath = argv[optind];
3974 if (sanity_check(target_zone, CMD_MOVE, B_FALSE, B_TRUE, B_FALSE)
3975 != Z_OK)
3976 return (Z_ERR);
3977 if (verify_details(CMD_MOVE, argv) != Z_OK)
3978 return (Z_ERR);
3981 * Check out the new zonepath. This has the side effect of creating
3982 * a directory for the new zonepath. We depend on this later when we
3983 * stat to see if we are doing a cross file system move or not.
3985 if (validate_zonepath(new_zonepath, CMD_MOVE) != Z_OK)
3986 return (Z_ERR);
3988 if ((err = zone_get_zonepath(target_zone, zonepath, sizeof (zonepath)))
3989 != Z_OK) {
3990 errno = err;
3991 zperror2(target_zone, gettext("could not get zone path"));
3992 return (Z_ERR);
3995 if (stat(zonepath, &zonepath_buf) == -1) {
3996 zperror(gettext("could not stat zone path"), B_FALSE);
3997 return (Z_ERR);
4000 if (stat(new_zonepath, &new_zonepath_buf) == -1) {
4001 zperror(gettext("could not stat new zone path"), B_FALSE);
4002 return (Z_ERR);
4006 * Check if the destination directory is empty.
4008 if ((dirp = opendir(new_zonepath)) == NULL) {
4009 zperror(gettext("could not open new zone path"), B_FALSE);
4010 return (Z_ERR);
4012 while ((dp = readdir(dirp)) != (struct dirent *)0) {
4013 if (strcmp(dp->d_name, ".") == 0 ||
4014 strcmp(dp->d_name, "..") == 0)
4015 continue;
4016 empty = B_FALSE;
4017 break;
4019 (void) closedir(dirp);
4021 /* Error if there is anything in the destination directory. */
4022 if (!empty) {
4023 (void) fprintf(stderr, gettext("could not move zone to %s: "
4024 "directory not empty\n"), new_zonepath);
4025 return (Z_ERR);
4029 * Collect information about mounts within the zone's zonepath.
4030 * Overlay mounts on the zone's root directory are erroneous.
4031 * Bail if we encounter any unexpected mounts.
4033 if (zone_mounts_init(&mounts, zonepath) != 0)
4034 return (Z_ERR);
4035 if (mounts.num_root_overlay_mounts != 0) {
4036 zerror(gettext("%d overlay mount(s) detected on %s/root."),
4037 mounts.num_root_overlay_mounts, zonepath);
4038 goto err_and_mounts_destroy;
4040 if (mounts.num_unexpected_mounts != 0)
4041 goto err_and_mounts_destroy;
4044 * Check if we are moving in the same file system and can do a fast
4045 * move or if we are crossing file systems and have to copy the data.
4047 fast = (zonepath_buf.st_dev == new_zonepath_buf.st_dev);
4049 if ((handle = zonecfg_init_handle()) == NULL) {
4050 zperror(cmd_to_str(CMD_MOVE), B_TRUE);
4051 goto err_and_mounts_destroy;
4054 if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) {
4055 errno = err;
4056 zperror(cmd_to_str(CMD_MOVE), B_TRUE);
4057 goto err_and_fini_handle;
4060 if (zonecfg_grab_lock_file(target_zone, &lockfd) != Z_OK) {
4061 zerror(gettext("another %s may have an operation in progress."),
4062 "zoneadm");
4063 goto err_and_fini_handle;
4067 * Unmount the zone's root filesystem before we move the zone's
4068 * zonepath.
4070 if (zone_unmount_rootfs(&mounts, zonepath, B_FALSE) != 0)
4071 goto err_and_rele_lockfile;
4074 * We're making some file system changes now so we have to clean up
4075 * the file system before we are done. This will either clean up the
4076 * new zonepath if the zonecfg update failed or it will clean up the
4077 * old zonepath if everything is ok.
4079 revert = B_TRUE;
4081 if (is_zonepath_zfs(zonepath) &&
4082 move_zfs(zonepath, new_zonepath) != Z_ERR) {
4083 is_zfs = B_TRUE;
4085 } else if (fast) {
4086 /* same file system, use rename for a quick move */
4089 * Remove the new_zonepath directory that got created above
4090 * during the validation. It gets in the way of the rename.
4092 if (rmdir(new_zonepath) != 0) {
4093 zperror(gettext("could not rmdir new zone path"),
4094 B_FALSE);
4095 (void) zone_mount_rootfs(&mounts, zonepath);
4096 goto err_and_rele_lockfile;
4099 if (rename(zonepath, new_zonepath) != 0) {
4101 * If this fails we don't need to do all of the
4102 * cleanup that happens for the rest of the code
4103 * so just return from this error.
4105 zperror(gettext("could not move zone"), B_FALSE);
4106 (void) zone_mount_rootfs(&mounts, zonepath);
4107 goto err_and_rele_lockfile;
4110 } else {
4112 * Attempt to create a ZFS fs for the new zonepath. As usual,
4113 * we don't care if this works or not since we always have the
4114 * default behavior of a simple directory for the zonepath.
4116 create_zfs_zonepath(new_zonepath);
4118 (void) printf(gettext(
4119 "Moving across file systems; copying zonepath %s..."),
4120 zonepath);
4121 (void) fflush(stdout);
4123 err = copy_zone(zonepath, new_zonepath);
4125 (void) printf("\n");
4126 if (err != Z_OK)
4127 goto done;
4131 * Mount the zone's root filesystem in the new zonepath if there was
4132 * a root mount prior to the move.
4134 if (zone_mount_rootfs(&mounts, new_zonepath) != 0) {
4135 err = Z_ERR;
4136 goto done;
4138 root_fs_mounted = B_TRUE;
4140 if ((err = zonecfg_set_zonepath(handle, new_zonepath)) != Z_OK) {
4141 errno = err;
4142 zperror(gettext("could not set new zonepath"), B_TRUE);
4143 goto done;
4146 if ((err = zonecfg_save(handle)) != Z_OK) {
4147 errno = err;
4148 zperror(gettext("zonecfg save failed"), B_TRUE);
4149 goto done;
4152 revert = B_FALSE;
4154 done:
4155 zonecfg_fini_handle(handle);
4156 zonecfg_release_lock_file(target_zone, lockfd);
4159 * Clean up the file system based on how things went. We either
4160 * clean up the new zonepath if the operation failed for some reason
4161 * or we clean up the old zonepath if everything is ok.
4163 if (revert) {
4165 * Check for the unlikely scenario in which the zone's
4166 * zonepath and its root file system moved but libzonecfg
4167 * couldn't save the new zonepath to the zone's configuration
4168 * file. The mounted root filesystem must be unmounted before
4169 * zoneadm restores the zone's zonepath.
4171 if (root_fs_mounted && zone_unmount_rootfs(&mounts,
4172 new_zonepath, B_TRUE) != 0) {
4174 * We can't forcibly unmount the zone's root file system
4175 * from the new zonepath. Bail!
4177 zerror(gettext("fatal error: cannot unmount %s/root\n"),
4178 new_zonepath);
4179 goto err_and_mounts_destroy;
4182 /* The zonecfg update failed, cleanup the new zonepath. */
4183 if (is_zfs) {
4184 if (move_zfs(new_zonepath, zonepath) == Z_ERR) {
4185 (void) fprintf(stderr, gettext("could not "
4186 "restore zonepath, the zfs mountpoint is "
4187 "set as:\n%s\n"), new_zonepath);
4189 * err is already != Z_OK since we're reverting
4191 } else {
4192 (void) zone_mount_rootfs(&mounts, zonepath);
4194 } else if (fast) {
4195 if (rename(new_zonepath, zonepath) != 0) {
4196 zperror(gettext("could not restore zonepath"),
4197 B_FALSE);
4199 * err is already != Z_OK since we're reverting
4201 } else {
4202 (void) zone_mount_rootfs(&mounts, zonepath);
4204 } else {
4205 (void) printf(gettext("Cleaning up zonepath %s..."),
4206 new_zonepath);
4207 (void) fflush(stdout);
4208 err = cleanup_zonepath(new_zonepath, B_TRUE);
4209 (void) printf("\n");
4211 if (err != Z_OK) {
4212 errno = err;
4213 zperror(gettext("could not remove new "
4214 "zonepath"), B_TRUE);
4215 } else {
4217 * Because we're reverting we know the mainline
4218 * code failed but we just reused the err
4219 * variable so we reset it back to Z_ERR.
4221 err = Z_ERR;
4224 (void) zone_mount_rootfs(&mounts, zonepath);
4226 } else {
4227 /* The move was successful, cleanup the old zonepath. */
4228 if (!is_zfs && !fast) {
4229 (void) printf(
4230 gettext("Cleaning up zonepath %s..."), zonepath);
4231 (void) fflush(stdout);
4232 err = cleanup_zonepath(zonepath, B_TRUE);
4233 (void) printf("\n");
4235 if (err != Z_OK) {
4236 errno = err;
4237 zperror(gettext("could not remove zonepath"),
4238 B_TRUE);
4243 zone_mounts_destroy(&mounts);
4244 return ((err == Z_OK) ? Z_OK : Z_ERR);
4246 err_and_rele_lockfile:
4247 zonecfg_release_lock_file(target_zone, lockfd);
4248 err_and_fini_handle:
4249 zonecfg_fini_handle(handle);
4250 err_and_mounts_destroy:
4251 zone_mounts_destroy(&mounts);
4252 return (Z_ERR);
4255 /* ARGSUSED */
4256 static int
4257 detach_func(int argc, char *argv[])
4259 int lockfd = -1;
4260 int err, arg;
4261 char zonepath[MAXPATHLEN];
4262 char cmdbuf[MAXPATHLEN];
4263 char precmdbuf[MAXPATHLEN];
4264 boolean_t execute = B_TRUE;
4265 boolean_t brand_help = B_FALSE;
4266 brand_handle_t bh = NULL;
4267 int status;
4269 if (zonecfg_in_alt_root()) {
4270 zerror(gettext("cannot detach zone in alternate root"));
4271 return (Z_ERR);
4274 /* Check the argv string for args we handle internally */
4275 optind = 0;
4276 opterr = 0;
4277 while ((arg = getopt(argc, argv, "?n")) != EOF) {
4278 switch (arg) {
4279 case '?':
4280 if (optopt == '?') {
4281 sub_usage(SHELP_DETACH, CMD_DETACH);
4282 brand_help = B_TRUE;
4284 /* Ignore unknown options - may be brand specific. */
4285 break;
4286 case 'n':
4287 execute = B_FALSE;
4288 break;
4289 default:
4290 /* Ignore unknown options - may be brand specific. */
4291 break;
4295 if (brand_help)
4296 execute = B_FALSE;
4298 if (execute) {
4299 if (sanity_check(target_zone, CMD_DETACH, B_FALSE, B_TRUE,
4300 B_FALSE) != Z_OK)
4301 return (Z_ERR);
4302 if (verify_details(CMD_DETACH, argv) != Z_OK)
4303 return (Z_ERR);
4304 } else {
4306 * We want a dry-run to work for a non-privileged user so we
4307 * only do minimal validation.
4309 if (target_zone == NULL) {
4310 zerror(gettext("no zone specified"));
4311 return (Z_ERR);
4314 if (strcmp(target_zone, GLOBAL_ZONENAME) == 0) {
4315 zerror(gettext("%s operation is invalid for the "
4316 "global zone."), cmd_to_str(CMD_DETACH));
4317 return (Z_ERR);
4321 if ((err = zone_get_zonepath(target_zone, zonepath, sizeof (zonepath)))
4322 != Z_OK) {
4323 errno = err;
4324 zperror2(target_zone, gettext("could not get zone path"));
4325 return (Z_ERR);
4328 /* Fetch the detach and predetach hooks from the brand configuration. */
4329 if ((bh = brand_open(target_brand)) == NULL) {
4330 zerror(gettext("missing or invalid brand"));
4331 return (Z_ERR);
4334 if (get_hook(bh, cmdbuf, sizeof (cmdbuf), brand_get_detach, target_zone,
4335 zonepath) != Z_OK) {
4336 zerror("invalid brand configuration: missing detach resource");
4337 brand_close(bh);
4338 return (Z_ERR);
4341 if (get_hook(bh, precmdbuf, sizeof (precmdbuf), brand_get_predetach,
4342 target_zone, zonepath) != Z_OK) {
4343 zerror("invalid brand configuration: missing predetach "
4344 "resource");
4345 brand_close(bh);
4346 return (Z_ERR);
4348 brand_close(bh);
4350 /* Append all options to predetach hook. */
4351 if (addoptions(precmdbuf, argv, sizeof (precmdbuf)) != Z_OK)
4352 return (Z_ERR);
4354 /* Append all options to detach hook. */
4355 if (addoptions(cmdbuf, argv, sizeof (cmdbuf)) != Z_OK)
4356 return (Z_ERR);
4358 if (execute && zonecfg_grab_lock_file(target_zone, &lockfd) != Z_OK) {
4359 zerror(gettext("another %s may have an operation in progress."),
4360 "zoneadm");
4361 return (Z_ERR);
4364 /* If we have a brand predetach hook, run it. */
4365 if (!brand_help && precmdbuf[0] != '\0') {
4366 status = do_subproc(precmdbuf);
4367 if (subproc_status(gettext("brand-specific predetach"),
4368 status, B_FALSE) != ZONE_SUBPROC_OK) {
4370 if (execute) {
4371 assert(lockfd >= 0);
4372 zonecfg_release_lock_file(target_zone, lockfd);
4373 lockfd = -1;
4376 assert(lockfd == -1);
4377 return (Z_ERR);
4381 if (cmdbuf[0] != '\0') {
4382 /* Run the detach hook */
4383 status = do_subproc(cmdbuf);
4384 if ((status = subproc_status(gettext("brand-specific detach"),
4385 status, B_FALSE)) != ZONE_SUBPROC_OK) {
4386 if (status == ZONE_SUBPROC_USAGE && !brand_help)
4387 sub_usage(SHELP_DETACH, CMD_DETACH);
4389 if (execute) {
4390 assert(lockfd >= 0);
4391 zonecfg_release_lock_file(target_zone, lockfd);
4392 lockfd = -1;
4395 assert(lockfd == -1);
4396 return (Z_ERR);
4399 } else {
4400 zone_dochandle_t handle;
4402 /* If just help, we're done since there is no brand help. */
4403 if (brand_help) {
4404 assert(lockfd == -1);
4405 return (Z_OK);
4409 * Run the built-in detach support. Just generate a simple
4410 * zone definition XML file and detach.
4413 /* Don't detach the zone if anything is still mounted there */
4414 if (execute && zonecfg_find_mounts(zonepath, NULL, NULL)) {
4415 (void) fprintf(stderr, gettext("These file systems are "
4416 "mounted on subdirectories of %s.\n"), zonepath);
4417 (void) zonecfg_find_mounts(zonepath, zfm_print, NULL);
4418 err = ZONE_SUBPROC_NOTCOMPLETE;
4419 goto done;
4422 if ((handle = zonecfg_init_handle()) == NULL) {
4423 zperror(cmd_to_str(CMD_DETACH), B_TRUE);
4424 err = ZONE_SUBPROC_NOTCOMPLETE;
4425 goto done;
4428 if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) {
4429 errno = err;
4430 zperror(cmd_to_str(CMD_DETACH), B_TRUE);
4432 } else if ((err = zonecfg_detach_save(handle,
4433 (execute ? 0 : ZONE_DRY_RUN))) != Z_OK) {
4434 errno = err;
4435 zperror(gettext("saving the detach manifest failed"),
4436 B_TRUE);
4439 zonecfg_fini_handle(handle);
4440 if (err != Z_OK)
4441 goto done;
4445 * Set the zone state back to configured unless we are running with the
4446 * no-execute option.
4448 if (execute && (err = zone_set_state(target_zone,
4449 ZONE_STATE_CONFIGURED)) != Z_OK) {
4450 errno = err;
4451 zperror(gettext("could not reset state"), B_TRUE);
4454 done:
4455 if (execute) {
4456 assert(lockfd >= 0);
4457 zonecfg_release_lock_file(target_zone, lockfd);
4458 lockfd = -1;
4461 assert(lockfd == -1);
4462 return ((err == Z_OK) ? Z_OK : Z_ERR);
4466 * Determine the brand when doing a dry-run attach. The zone does not have to
4467 * exist, so we have to read the incoming manifest to determine the zone's
4468 * brand.
4470 * Because the manifest has to be processed twice; once to determine the brand
4471 * and once to do the brand-specific attach logic, we always read it into a tmp
4472 * file. This handles the manifest coming from stdin or a regular file. The
4473 * tmpname parameter returns the name of the temporary file that the manifest
4474 * was read into.
4476 static int
4477 dryrun_get_brand(char *manifest_path, char *tmpname, int size)
4479 int fd;
4480 int err;
4481 int res = Z_OK;
4482 zone_dochandle_t local_handle;
4483 zone_dochandle_t rem_handle = NULL;
4484 int len;
4485 int ofd;
4486 char buf[512];
4488 if (strcmp(manifest_path, "-") == 0) {
4489 fd = STDIN_FILENO;
4490 } else {
4491 if ((fd = open(manifest_path, O_RDONLY)) < 0) {
4492 if (getcwd(buf, sizeof (buf)) == NULL)
4493 (void) strlcpy(buf, "/", sizeof (buf));
4494 zerror(gettext("could not open manifest path %s%s: %s"),
4495 (*manifest_path == '/' ? "" : buf), manifest_path,
4496 strerror(errno));
4497 return (Z_ERR);
4501 (void) snprintf(tmpname, size, "/var/run/zone.%d", getpid());
4503 if ((ofd = open(tmpname, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR)) < 0) {
4504 zperror(gettext("could not save manifest"), B_FALSE);
4505 (void) close(fd);
4506 return (Z_ERR);
4509 while ((len = read(fd, buf, sizeof (buf))) > 0) {
4510 if (write(ofd, buf, len) == -1) {
4511 zperror(gettext("could not save manifest"), B_FALSE);
4512 (void) close(ofd);
4513 (void) close(fd);
4514 return (Z_ERR);
4518 if (close(ofd) != 0) {
4519 zperror(gettext("could not save manifest"), B_FALSE);
4520 (void) close(fd);
4521 return (Z_ERR);
4524 (void) close(fd);
4526 if ((fd = open(tmpname, O_RDONLY)) < 0) {
4527 zperror(gettext("could not open manifest path"), B_FALSE);
4528 return (Z_ERR);
4531 if ((local_handle = zonecfg_init_handle()) == NULL) {
4532 zperror(cmd_to_str(CMD_ATTACH), B_TRUE);
4533 res = Z_ERR;
4534 goto done;
4537 if ((rem_handle = zonecfg_init_handle()) == NULL) {
4538 zperror(cmd_to_str(CMD_ATTACH), B_TRUE);
4539 res = Z_ERR;
4540 goto done;
4543 if ((err = zonecfg_attach_manifest(fd, local_handle, rem_handle))
4544 != Z_OK) {
4545 res = Z_ERR;
4547 if (err == Z_INVALID_DOCUMENT) {
4548 struct stat st;
4549 char buf[6];
4551 if (strcmp(manifest_path, "-") == 0) {
4552 zerror(gettext("Input is not a valid XML "
4553 "file"));
4554 goto done;
4557 if (fstat(fd, &st) == -1 || !S_ISREG(st.st_mode)) {
4558 zerror(gettext("%s is not an XML file"),
4559 manifest_path);
4560 goto done;
4563 bzero(buf, sizeof (buf));
4564 (void) lseek(fd, 0L, SEEK_SET);
4565 if (read(fd, buf, sizeof (buf) - 1) < 0 ||
4566 strncmp(buf, "<?xml", 5) != 0)
4567 zerror(gettext("%s is not an XML file"),
4568 manifest_path);
4569 else
4570 zerror(gettext("Cannot attach to an earlier "
4571 "release of the operating system"));
4572 } else {
4573 zperror(cmd_to_str(CMD_ATTACH), B_TRUE);
4575 goto done;
4578 /* Retrieve remote handle brand type. */
4579 if (zonecfg_get_brand(rem_handle, target_brand, sizeof (target_brand))
4580 != Z_OK) {
4581 zerror(gettext("missing or invalid brand"));
4582 exit(Z_ERR);
4585 done:
4586 zonecfg_fini_handle(local_handle);
4587 zonecfg_fini_handle(rem_handle);
4588 (void) close(fd);
4590 return ((res == Z_OK) ? Z_OK : Z_ERR);
4593 /* ARGSUSED */
4594 static int
4595 attach_func(int argc, char *argv[])
4597 int lockfd = -1;
4598 int err, arg;
4599 boolean_t force = B_FALSE;
4600 zone_dochandle_t handle;
4601 char zonepath[MAXPATHLEN];
4602 char cmdbuf[MAXPATHLEN];
4603 char postcmdbuf[MAXPATHLEN];
4604 boolean_t execute = B_TRUE;
4605 boolean_t brand_help = B_FALSE;
4606 char *manifest_path;
4607 char tmpmanifest[80];
4608 int manifest_pos;
4609 brand_handle_t bh = NULL;
4610 int status;
4611 int last_index = 0;
4612 int offset;
4613 char *up;
4614 boolean_t forced_update = B_FALSE;
4616 if (zonecfg_in_alt_root()) {
4617 zerror(gettext("cannot attach zone in alternate root"));
4618 return (Z_ERR);
4621 /* Check the argv string for args we handle internally */
4622 optind = 0;
4623 opterr = 0;
4624 while ((arg = getopt(argc, argv, "?Fn:U")) != EOF) {
4625 switch (arg) {
4626 case '?':
4627 if (optopt == '?') {
4628 sub_usage(SHELP_ATTACH, CMD_ATTACH);
4629 brand_help = B_TRUE;
4631 /* Ignore unknown options - may be brand specific. */
4632 break;
4633 case 'F':
4634 force = B_TRUE;
4635 break;
4636 case 'n':
4637 execute = B_FALSE;
4638 manifest_path = optarg;
4639 manifest_pos = optind - 1;
4640 break;
4641 case 'U':
4643 * Undocumented 'force update' option for p2v update on
4644 * attach when zone is in the incomplete state. Change
4645 * the option back to 'u' and set forced_update flag.
4647 if (optind == last_index)
4648 offset = optind;
4649 else
4650 offset = optind - 1;
4651 if ((up = index(argv[offset], 'U')) != NULL)
4652 *up = 'u';
4653 forced_update = B_TRUE;
4654 break;
4655 default:
4656 /* Ignore unknown options - may be brand specific. */
4657 break;
4659 last_index = optind;
4662 if (brand_help) {
4663 force = B_FALSE;
4664 execute = B_TRUE;
4667 /* dry-run and force flags are mutually exclusive */
4668 if (!execute && force) {
4669 zerror(gettext("-F and -n flags are mutually exclusive"));
4670 return (Z_ERR);
4674 * If the no-execute option was specified, we don't do validation and
4675 * need to figure out the brand, since there is no zone required to be
4676 * configured for this option.
4678 if (execute) {
4679 if (!brand_help) {
4680 if (sanity_check(target_zone, CMD_ATTACH, B_FALSE,
4681 B_TRUE, forced_update) != Z_OK)
4682 return (Z_ERR);
4683 if (verify_details(CMD_ATTACH, argv) != Z_OK)
4684 return (Z_ERR);
4687 if ((err = zone_get_zonepath(target_zone, zonepath,
4688 sizeof (zonepath))) != Z_OK) {
4689 errno = err;
4690 zperror2(target_zone,
4691 gettext("could not get zone path"));
4692 return (Z_ERR);
4694 } else {
4695 if (dryrun_get_brand(manifest_path, tmpmanifest,
4696 sizeof (tmpmanifest)) != Z_OK)
4697 return (Z_ERR);
4699 argv[manifest_pos] = tmpmanifest;
4700 target_zone = "-";
4701 (void) strlcpy(zonepath, "-", sizeof (zonepath));
4703 /* Run the brand's verify_adm hook. */
4704 if (verify_brand(NULL, CMD_ATTACH, argv) != Z_OK)
4705 return (Z_ERR);
4709 * Fetch the attach and postattach hooks from the brand configuration.
4711 if ((bh = brand_open(target_brand)) == NULL) {
4712 zerror(gettext("missing or invalid brand"));
4713 return (Z_ERR);
4716 if (get_hook(bh, cmdbuf, sizeof (cmdbuf), brand_get_attach, target_zone,
4717 zonepath) != Z_OK) {
4718 zerror("invalid brand configuration: missing attach resource");
4719 brand_close(bh);
4720 return (Z_ERR);
4723 if (get_hook(bh, postcmdbuf, sizeof (postcmdbuf), brand_get_postattach,
4724 target_zone, zonepath) != Z_OK) {
4725 zerror("invalid brand configuration: missing postattach "
4726 "resource");
4727 brand_close(bh);
4728 return (Z_ERR);
4730 brand_close(bh);
4732 /* Append all options to attach hook. */
4733 if (addoptions(cmdbuf, argv, sizeof (cmdbuf)) != Z_OK)
4734 return (Z_ERR);
4736 /* Append all options to postattach hook. */
4737 if (addoptions(postcmdbuf, argv, sizeof (postcmdbuf)) != Z_OK)
4738 return (Z_ERR);
4740 if (execute && !brand_help) {
4741 if (zonecfg_grab_lock_file(target_zone, &lockfd) != Z_OK) {
4742 zerror(gettext("another %s may have an operation in "
4743 "progress."), "zoneadm");
4744 return (Z_ERR);
4748 if (!force) {
4750 * Not a force-attach, so we need to actually do the work.
4752 if (cmdbuf[0] != '\0') {
4753 /* Run the attach hook */
4754 status = do_subproc(cmdbuf);
4755 if ((status = subproc_status(gettext("brand-specific "
4756 "attach"), status, B_FALSE)) != ZONE_SUBPROC_OK) {
4757 if (status == ZONE_SUBPROC_USAGE && !brand_help)
4758 sub_usage(SHELP_ATTACH, CMD_ATTACH);
4760 if (execute && !brand_help) {
4761 assert(zonecfg_lock_file_held(&lockfd));
4762 zonecfg_release_lock_file(target_zone,
4763 lockfd);
4764 lockfd = -1;
4767 assert(lockfd == -1);
4768 return (Z_ERR);
4773 * Else run the built-in attach support.
4774 * This is a no-op since there is nothing to validate.
4777 /* If dry-run or help, then we're done. */
4778 if (!execute || brand_help) {
4779 if (!execute)
4780 (void) unlink(tmpmanifest);
4781 assert(lockfd == -1);
4782 return (Z_OK);
4786 /* Now we can validate that the zonepath exists. */
4787 if (validate_zonepath(zonepath, CMD_ATTACH) != Z_OK) {
4788 (void) fprintf(stderr, gettext("could not verify zonepath %s "
4789 "because of the above errors.\n"), zonepath);
4791 assert(zonecfg_lock_file_held(&lockfd));
4792 zonecfg_release_lock_file(target_zone, lockfd);
4793 return (Z_ERR);
4796 if ((handle = zonecfg_init_handle()) == NULL) {
4797 zperror(cmd_to_str(CMD_ATTACH), B_TRUE);
4798 err = Z_ERR;
4799 } else if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) {
4800 errno = err;
4801 zperror(cmd_to_str(CMD_ATTACH), B_TRUE);
4802 zonecfg_fini_handle(handle);
4803 } else {
4804 zonecfg_rm_detached(handle, force);
4805 zonecfg_fini_handle(handle);
4808 if (err == Z_OK &&
4809 (err = zone_set_state(target_zone, ZONE_STATE_INSTALLED)) != Z_OK) {
4810 errno = err;
4811 zperror(gettext("could not reset state"), B_TRUE);
4814 assert(zonecfg_lock_file_held(&lockfd));
4815 zonecfg_release_lock_file(target_zone, lockfd);
4816 lockfd = -1;
4818 /* If we have a brand postattach hook, run it. */
4819 if (err == Z_OK && !force && postcmdbuf[0] != '\0') {
4820 status = do_subproc(postcmdbuf);
4821 if (subproc_status(gettext("brand-specific postattach"),
4822 status, B_FALSE) != ZONE_SUBPROC_OK) {
4823 if ((err = zone_set_state(target_zone,
4824 ZONE_STATE_CONFIGURED)) != Z_OK) {
4825 errno = err;
4826 zperror(gettext("could not reset state"),
4827 B_TRUE);
4832 assert(lockfd == -1);
4833 return ((err == Z_OK) ? Z_OK : Z_ERR);
4837 * On input, TRUE => yes, FALSE => no.
4838 * On return, TRUE => 1, FALSE => 0, could not ask => -1.
4841 static int
4842 ask_yesno(boolean_t default_answer, const char *question)
4844 char line[64]; /* should be large enough to answer yes or no */
4846 if (!isatty(STDIN_FILENO))
4847 return (-1);
4848 for (;;) {
4849 (void) printf("%s (%s)? ", question,
4850 default_answer ? "[y]/n" : "y/[n]");
4851 if (fgets(line, sizeof (line), stdin) == NULL ||
4852 line[0] == '\n')
4853 return (default_answer ? 1 : 0);
4854 if (tolower(line[0]) == 'y')
4855 return (1);
4856 if (tolower(line[0]) == 'n')
4857 return (0);
4861 /* ARGSUSED */
4862 static int
4863 uninstall_func(int argc, char *argv[])
4865 char line[ZONENAME_MAX + 128]; /* Enough for "Are you sure ..." */
4866 char rootpath[MAXPATHLEN], zonepath[MAXPATHLEN];
4867 char cmdbuf[MAXPATHLEN];
4868 char precmdbuf[MAXPATHLEN];
4869 boolean_t force = B_FALSE;
4870 int lockfd, answer;
4871 int err, arg;
4872 boolean_t brand_help = B_FALSE;
4873 brand_handle_t bh = NULL;
4874 int status;
4876 if (zonecfg_in_alt_root()) {
4877 zerror(gettext("cannot uninstall zone in alternate root"));
4878 return (Z_ERR);
4881 /* Check the argv string for args we handle internally */
4882 optind = 0;
4883 opterr = 0;
4884 while ((arg = getopt(argc, argv, "?F")) != EOF) {
4885 switch (arg) {
4886 case '?':
4887 if (optopt == '?') {
4888 sub_usage(SHELP_UNINSTALL, CMD_UNINSTALL);
4889 brand_help = B_TRUE;
4891 /* Ignore unknown options - may be brand specific. */
4892 break;
4893 case 'F':
4894 force = B_TRUE;
4895 break;
4896 default:
4897 /* Ignore unknown options - may be brand specific. */
4898 break;
4902 if (!brand_help) {
4903 if (sanity_check(target_zone, CMD_UNINSTALL, B_FALSE, B_TRUE,
4904 B_FALSE) != Z_OK)
4905 return (Z_ERR);
4908 * Invoke brand-specific handler.
4910 if (invoke_brand_handler(CMD_UNINSTALL, argv) != Z_OK)
4911 return (Z_ERR);
4913 if (!force) {
4914 (void) snprintf(line, sizeof (line),
4915 gettext("Are you sure you want to %s zone %s"),
4916 cmd_to_str(CMD_UNINSTALL), target_zone);
4917 if ((answer = ask_yesno(B_FALSE, line)) == 0) {
4918 return (Z_OK);
4919 } else if (answer == -1) {
4920 zerror(gettext("Input not from terminal and -F "
4921 "not specified: %s not done."),
4922 cmd_to_str(CMD_UNINSTALL));
4923 return (Z_ERR);
4928 if ((err = zone_get_zonepath(target_zone, zonepath,
4929 sizeof (zonepath))) != Z_OK) {
4930 errno = err;
4931 zperror2(target_zone, gettext("could not get zone path"));
4932 return (Z_ERR);
4936 * Fetch the uninstall and preuninstall hooks from the brand
4937 * configuration.
4939 if ((bh = brand_open(target_brand)) == NULL) {
4940 zerror(gettext("missing or invalid brand"));
4941 return (Z_ERR);
4944 if (get_hook(bh, cmdbuf, sizeof (cmdbuf), brand_get_uninstall,
4945 target_zone, zonepath) != Z_OK) {
4946 zerror("invalid brand configuration: missing uninstall "
4947 "resource");
4948 brand_close(bh);
4949 return (Z_ERR);
4952 if (get_hook(bh, precmdbuf, sizeof (precmdbuf), brand_get_preuninstall,
4953 target_zone, zonepath) != Z_OK) {
4954 zerror("invalid brand configuration: missing preuninstall "
4955 "resource");
4956 brand_close(bh);
4957 return (Z_ERR);
4959 brand_close(bh);
4961 /* Append all options to preuninstall hook. */
4962 if (addoptions(precmdbuf, argv, sizeof (precmdbuf)) != Z_OK)
4963 return (Z_ERR);
4965 /* Append all options to uninstall hook. */
4966 if (addoptions(cmdbuf, argv, sizeof (cmdbuf)) != Z_OK)
4967 return (Z_ERR);
4969 if (!brand_help) {
4970 if ((err = zone_get_rootpath(target_zone, rootpath,
4971 sizeof (rootpath))) != Z_OK) {
4972 errno = err;
4973 zperror2(target_zone, gettext("could not get root "
4974 "path"));
4975 return (Z_ERR);
4979 * If there seems to be a zoneadmd running for this zone, call
4980 * it to tell it that an uninstall is happening; if all goes
4981 * well it will then shut itself down.
4983 if (zonecfg_ping_zoneadmd(target_zone) == Z_OK) {
4984 zone_cmd_arg_t zarg;
4985 zarg.cmd = Z_NOTE_UNINSTALLING;
4986 /* we don't care too much if this fails, just plow on */
4987 (void) zonecfg_call_zoneadmd(target_zone, &zarg, locale,
4988 B_TRUE);
4991 if (zonecfg_grab_lock_file(target_zone, &lockfd) != Z_OK) {
4992 zerror(gettext("another %s may have an operation in "
4993 "progress."), "zoneadm");
4994 return (Z_ERR);
4997 /* Don't uninstall the zone if anything is mounted there */
4998 err = zonecfg_find_mounts(rootpath, NULL, NULL);
4999 if (err) {
5000 zerror(gettext("These file systems are mounted on "
5001 "subdirectories of %s.\n"), rootpath);
5002 (void) zonecfg_find_mounts(rootpath, zfm_print, NULL);
5003 zonecfg_release_lock_file(target_zone, lockfd);
5004 return (Z_ERR);
5008 /* If we have a brand preuninstall hook, run it. */
5009 if (!brand_help && precmdbuf[0] != '\0') {
5010 status = do_subproc(precmdbuf);
5011 if (subproc_status(gettext("brand-specific preuninstall"),
5012 status, B_FALSE) != ZONE_SUBPROC_OK) {
5013 zonecfg_release_lock_file(target_zone, lockfd);
5014 return (Z_ERR);
5018 if (!brand_help) {
5019 err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE);
5020 if (err != Z_OK) {
5021 errno = err;
5022 zperror2(target_zone, gettext("could not set state"));
5023 goto bad;
5028 * If there is a brand uninstall hook, use it, otherwise use the
5029 * built-in uninstall code.
5031 if (cmdbuf[0] != '\0') {
5032 /* Run the uninstall hook */
5033 status = do_subproc(cmdbuf);
5034 if ((status = subproc_status(gettext("brand-specific "
5035 "uninstall"), status, B_FALSE)) != ZONE_SUBPROC_OK) {
5036 if (status == ZONE_SUBPROC_USAGE && !brand_help)
5037 sub_usage(SHELP_UNINSTALL, CMD_UNINSTALL);
5038 if (!brand_help)
5039 zonecfg_release_lock_file(target_zone, lockfd);
5040 return (Z_ERR);
5043 if (brand_help)
5044 return (Z_OK);
5045 } else {
5046 /* If just help, we're done since there is no brand help. */
5047 if (brand_help)
5048 return (Z_OK);
5050 /* Run the built-in uninstall support. */
5051 if ((err = cleanup_zonepath(zonepath, B_FALSE)) != Z_OK) {
5052 errno = err;
5053 zperror2(target_zone, gettext("cleaning up zonepath "
5054 "failed"));
5055 goto bad;
5059 err = zone_set_state(target_zone, ZONE_STATE_CONFIGURED);
5060 if (err != Z_OK) {
5061 errno = err;
5062 zperror2(target_zone, gettext("could not reset state"));
5064 bad:
5065 zonecfg_release_lock_file(target_zone, lockfd);
5066 return (err);
5069 /* ARGSUSED */
5070 static int
5071 mount_func(int argc, char *argv[])
5073 zone_cmd_arg_t zarg;
5074 boolean_t force = B_FALSE;
5075 int arg;
5078 * The only supported subargument to the "mount" subcommand is
5079 * "-f", which forces us to mount a zone in the INCOMPLETE state.
5081 optind = 0;
5082 if ((arg = getopt(argc, argv, "f")) != EOF) {
5083 switch (arg) {
5084 case 'f':
5085 force = B_TRUE;
5086 break;
5087 default:
5088 return (Z_USAGE);
5091 if (argc > optind)
5092 return (Z_USAGE);
5094 if (sanity_check(target_zone, CMD_MOUNT, B_FALSE, B_FALSE, force)
5095 != Z_OK)
5096 return (Z_ERR);
5097 if (verify_details(CMD_MOUNT, argv) != Z_OK)
5098 return (Z_ERR);
5100 zarg.cmd = force ? Z_FORCEMOUNT : Z_MOUNT;
5101 zarg.bootbuf[0] = '\0';
5102 if (zonecfg_call_zoneadmd(target_zone, &zarg, locale, B_TRUE) != 0) {
5103 zerror(gettext("call to %s failed"), "zoneadmd");
5104 return (Z_ERR);
5106 return (Z_OK);
5109 /* ARGSUSED */
5110 static int
5111 unmount_func(int argc, char *argv[])
5113 zone_cmd_arg_t zarg;
5115 if (argc > 0)
5116 return (Z_USAGE);
5117 if (sanity_check(target_zone, CMD_UNMOUNT, B_FALSE, B_FALSE, B_FALSE)
5118 != Z_OK)
5119 return (Z_ERR);
5121 zarg.cmd = Z_UNMOUNT;
5122 if (zonecfg_call_zoneadmd(target_zone, &zarg, locale, B_TRUE) != 0) {
5123 zerror(gettext("call to %s failed"), "zoneadmd");
5124 return (Z_ERR);
5126 return (Z_OK);
5129 static int
5130 mark_func(int argc, char *argv[])
5132 int err, lockfd;
5133 int arg;
5134 boolean_t force = B_FALSE;
5135 int state;
5137 optind = 0;
5138 opterr = 0;
5139 while ((arg = getopt(argc, argv, "F")) != EOF) {
5140 switch (arg) {
5141 case 'F':
5142 force = B_TRUE;
5143 break;
5144 default:
5145 return (Z_USAGE);
5149 if (argc != (optind + 1))
5150 return (Z_USAGE);
5152 if (strcmp(argv[optind], "configured") == 0)
5153 state = ZONE_STATE_CONFIGURED;
5154 else if (strcmp(argv[optind], "incomplete") == 0)
5155 state = ZONE_STATE_INCOMPLETE;
5156 else if (strcmp(argv[optind], "installed") == 0)
5157 state = ZONE_STATE_INSTALLED;
5158 else
5159 return (Z_USAGE);
5161 if (state != ZONE_STATE_INCOMPLETE && !force)
5162 return (Z_USAGE);
5164 if (sanity_check(target_zone, CMD_MARK, B_FALSE, B_TRUE, B_FALSE)
5165 != Z_OK)
5166 return (Z_ERR);
5169 * Invoke brand-specific handler.
5171 if (invoke_brand_handler(CMD_MARK, argv) != Z_OK)
5172 return (Z_ERR);
5174 if (zonecfg_grab_lock_file(target_zone, &lockfd) != Z_OK) {
5175 zerror(gettext("another %s may have an operation in progress."),
5176 "zoneadm");
5177 return (Z_ERR);
5180 err = zone_set_state(target_zone, state);
5181 if (err != Z_OK) {
5182 errno = err;
5183 zperror2(target_zone, gettext("could not set state"));
5185 zonecfg_release_lock_file(target_zone, lockfd);
5187 return (err);
5191 * Check what scheduling class we're running under and print a warning if
5192 * we're not using FSS.
5194 static int
5195 check_sched_fss(zone_dochandle_t handle)
5197 char class_name[PC_CLNMSZ];
5199 if (zonecfg_get_dflt_sched_class(handle, class_name,
5200 sizeof (class_name)) != Z_OK) {
5201 zerror(gettext("WARNING: unable to determine the zone's "
5202 "scheduling class"));
5203 } else if (strcmp("FSS", class_name) != 0) {
5204 zerror(gettext("WARNING: The zone.cpu-shares rctl is set but\n"
5205 "FSS is not the default scheduling class for this zone. "
5206 "FSS will be\nused for processes in the zone but to get "
5207 "the full benefit of FSS,\nit should be the default "
5208 "scheduling class. See dispadmin(1M) for\nmore details."));
5209 return (Z_SYSTEM);
5212 return (Z_OK);
5215 static int
5216 check_cpu_shares_sched(zone_dochandle_t handle)
5218 int err;
5219 int res = Z_OK;
5220 struct zone_rctltab rctl;
5222 if ((err = zonecfg_setrctlent(handle)) != Z_OK) {
5223 errno = err;
5224 zperror(cmd_to_str(CMD_APPLY), B_TRUE);
5225 return (err);
5228 while (zonecfg_getrctlent(handle, &rctl) == Z_OK) {
5229 if (strcmp(rctl.zone_rctl_name, "zone.cpu-shares") == 0) {
5230 if (check_sched_fss(handle) != Z_OK)
5231 res = Z_SYSTEM;
5232 break;
5236 (void) zonecfg_endrctlent(handle);
5238 return (res);
5242 * Check if there is a mix of processes running in different pools within the
5243 * zone. This is currently only going to be called for the global zone from
5244 * apply_func but that could be generalized in the future.
5246 static boolean_t
5247 mixed_pools(zoneid_t zoneid)
5249 DIR *dirp;
5250 dirent_t *dent;
5251 boolean_t mixed = B_FALSE;
5252 boolean_t poolid_set = B_FALSE;
5253 poolid_t last_poolid = 0;
5255 if ((dirp = opendir("/proc")) == NULL) {
5256 zerror(gettext("could not open /proc"));
5257 return (B_FALSE);
5260 while ((dent = readdir(dirp)) != NULL) {
5261 int procfd;
5262 psinfo_t ps;
5263 char procpath[MAXPATHLEN];
5265 if (dent->d_name[0] == '.')
5266 continue;
5268 (void) snprintf(procpath, sizeof (procpath), "/proc/%s/psinfo",
5269 dent->d_name);
5271 if ((procfd = open(procpath, O_RDONLY)) == -1)
5272 continue;
5274 if (read(procfd, &ps, sizeof (ps)) == sizeof (psinfo_t)) {
5275 /* skip processes in other zones and system processes */
5276 if (zoneid != ps.pr_zoneid || ps.pr_flag & SSYS) {
5277 (void) close(procfd);
5278 continue;
5281 if (poolid_set) {
5282 if (ps.pr_poolid != last_poolid)
5283 mixed = B_TRUE;
5284 } else {
5285 last_poolid = ps.pr_poolid;
5286 poolid_set = B_TRUE;
5290 (void) close(procfd);
5292 if (mixed)
5293 break;
5296 (void) closedir(dirp);
5298 return (mixed);
5302 * Check if a persistent or temporary pool is configured for the zone.
5303 * This is currently only going to be called for the global zone from
5304 * apply_func but that could be generalized in the future.
5306 static boolean_t
5307 pool_configured(zone_dochandle_t handle)
5309 int err1, err2;
5310 struct zone_psettab pset_tab;
5311 char poolname[MAXPATHLEN];
5313 err1 = zonecfg_lookup_pset(handle, &pset_tab);
5314 err2 = zonecfg_get_pool(handle, poolname, sizeof (poolname));
5316 if (err1 == Z_NO_ENTRY &&
5317 (err2 == Z_NO_ENTRY || (err2 == Z_OK && strlen(poolname) == 0)))
5318 return (B_FALSE);
5320 return (B_TRUE);
5324 * This is an undocumented interface which is currently only used to apply
5325 * the global zone resource management settings when the system boots.
5326 * This function does not yet properly handle updating a running system so
5327 * any projects running in the zone would be trashed if this function
5328 * were to run after the zone had booted. It also does not reset any
5329 * rctl settings that were removed from zonecfg. There is still work to be
5330 * done before we can properly support dynamically updating the resource
5331 * management settings for a running zone (global or non-global). Thus, this
5332 * functionality is undocumented for now.
5334 /* ARGSUSED */
5335 static int
5336 apply_func(int argc, char *argv[])
5338 int err;
5339 int res = Z_OK;
5340 priv_set_t *privset;
5341 zoneid_t zoneid;
5342 zone_dochandle_t handle;
5343 struct zone_mcaptab mcap;
5344 char pool_err[128];
5346 zoneid = getzoneid();
5348 if (zonecfg_in_alt_root() || zoneid != GLOBAL_ZONEID ||
5349 target_zone == NULL || strcmp(target_zone, GLOBAL_ZONENAME) != 0)
5350 return (usage(B_FALSE));
5352 if ((privset = priv_allocset()) == NULL) {
5353 zerror(gettext("%s failed"), "priv_allocset");
5354 return (Z_ERR);
5357 if (getppriv(PRIV_EFFECTIVE, privset) != 0) {
5358 zerror(gettext("%s failed"), "getppriv");
5359 priv_freeset(privset);
5360 return (Z_ERR);
5363 if (priv_isfullset(privset) == B_FALSE) {
5364 (void) usage(B_FALSE);
5365 priv_freeset(privset);
5366 return (Z_ERR);
5368 priv_freeset(privset);
5370 if ((handle = zonecfg_init_handle()) == NULL) {
5371 zperror(cmd_to_str(CMD_APPLY), B_TRUE);
5372 return (Z_ERR);
5375 if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) {
5376 errno = err;
5377 zperror(cmd_to_str(CMD_APPLY), B_TRUE);
5378 zonecfg_fini_handle(handle);
5379 return (Z_ERR);
5382 /* specific error msgs are printed within apply_rctls */
5383 if ((err = zonecfg_apply_rctls(target_zone, handle)) != Z_OK) {
5384 errno = err;
5385 zperror(cmd_to_str(CMD_APPLY), B_TRUE);
5386 res = Z_ERR;
5389 if ((err = check_cpu_shares_sched(handle)) != Z_OK)
5390 res = Z_ERR;
5392 if (pool_configured(handle)) {
5393 if (mixed_pools(zoneid)) {
5394 zerror(gettext("Zone is using multiple resource "
5395 "pools. The pool\nconfiguration cannot be "
5396 "applied without rebooting."));
5397 res = Z_ERR;
5398 } else {
5401 * The next two blocks of code attempt to set up
5402 * temporary pools as well as persistent pools. In
5403 * both cases we call the functions unconditionally.
5404 * Within each funtion the code will check if the zone
5405 * is actually configured for a temporary pool or
5406 * persistent pool and just return if there is nothing
5407 * to do.
5409 if ((err = zonecfg_bind_tmp_pool(handle, zoneid,
5410 pool_err, sizeof (pool_err))) != Z_OK) {
5411 if (err == Z_POOL || err == Z_POOL_CREATE ||
5412 err == Z_POOL_BIND)
5413 zerror("%s: %s", zonecfg_strerror(err),
5414 pool_err);
5415 else
5416 zerror(gettext("could not bind zone to "
5417 "temporary pool: %s"),
5418 zonecfg_strerror(err));
5419 res = Z_ERR;
5422 if ((err = zonecfg_bind_pool(handle, zoneid, pool_err,
5423 sizeof (pool_err))) != Z_OK) {
5424 if (err == Z_POOL || err == Z_POOL_BIND)
5425 zerror("%s: %s", zonecfg_strerror(err),
5426 pool_err);
5427 else
5428 zerror("%s", zonecfg_strerror(err));
5434 * If a memory cap is configured, set the cap in the kernel using
5435 * zone_setattr() and make sure the rcapd SMF service is enabled.
5437 if (zonecfg_getmcapent(handle, &mcap) == Z_OK) {
5438 uint64_t num;
5439 char smf_err[128];
5441 num = (uint64_t)strtoll(mcap.zone_physmem_cap, NULL, 10);
5442 if (zone_setattr(zoneid, ZONE_ATTR_PHYS_MCAP, &num, 0) == -1) {
5443 zerror(gettext("could not set zone memory cap"));
5444 res = Z_ERR;
5447 if (zonecfg_enable_rcapd(smf_err, sizeof (smf_err)) != Z_OK) {
5448 zerror(gettext("enabling system/rcap service failed: "
5449 "%s"), smf_err);
5450 res = Z_ERR;
5454 zonecfg_fini_handle(handle);
5456 return (res);
5460 * This is an undocumented interface that is invoked by the zones SMF service
5461 * for installed zones that won't automatically boot.
5463 /* ARGSUSED */
5464 static int
5465 sysboot_func(int argc, char *argv[])
5467 int err;
5468 zone_dochandle_t zone_handle;
5469 brand_handle_t brand_handle;
5470 char cmdbuf[MAXPATHLEN];
5471 char zonepath[MAXPATHLEN];
5474 * This subcommand can only be executed in the global zone on non-global
5475 * zones.
5477 if (zonecfg_in_alt_root())
5478 return (usage(B_FALSE));
5479 if (sanity_check(target_zone, CMD_SYSBOOT, B_FALSE, B_TRUE, B_FALSE) !=
5480 Z_OK)
5481 return (Z_ERR);
5484 * Fetch the sysboot hook from the target zone's brand.
5486 if ((zone_handle = zonecfg_init_handle()) == NULL) {
5487 zperror(cmd_to_str(CMD_SYSBOOT), B_TRUE);
5488 return (Z_ERR);
5490 if ((err = zonecfg_get_handle(target_zone, zone_handle)) != Z_OK) {
5491 errno = err;
5492 zperror(cmd_to_str(CMD_SYSBOOT), B_TRUE);
5493 zonecfg_fini_handle(zone_handle);
5494 return (Z_ERR);
5496 if ((err = zonecfg_get_zonepath(zone_handle, zonepath,
5497 sizeof (zonepath))) != Z_OK) {
5498 errno = err;
5499 zperror(cmd_to_str(CMD_SYSBOOT), B_TRUE);
5500 zonecfg_fini_handle(zone_handle);
5501 return (Z_ERR);
5503 if ((brand_handle = brand_open(target_brand)) == NULL) {
5504 zerror(gettext("missing or invalid brand during %s operation: "
5505 "%s"), cmd_to_str(CMD_SYSBOOT), target_brand);
5506 zonecfg_fini_handle(zone_handle);
5507 return (Z_ERR);
5509 err = get_hook(brand_handle, cmdbuf, sizeof (cmdbuf), brand_get_sysboot,
5510 target_zone, zonepath);
5511 brand_close(brand_handle);
5512 zonecfg_fini_handle(zone_handle);
5513 if (err != Z_OK) {
5514 zerror(gettext("unable to get brand hook from brand %s for %s "
5515 "operation"), target_brand, cmd_to_str(CMD_SYSBOOT));
5516 return (Z_ERR);
5520 * If the hook wasn't defined (which is OK), then indicate success and
5521 * return. Otherwise, execute the hook.
5523 if (cmdbuf[0] != '\0')
5524 return ((subproc_status(gettext("brand sysboot operation"),
5525 do_subproc(cmdbuf), B_FALSE) == ZONE_SUBPROC_OK) ? Z_OK :
5526 Z_BRAND_ERROR);
5527 return (Z_OK);
5530 static int
5531 help_func(int argc, char *argv[])
5533 int arg, cmd_num;
5535 if (argc == 0) {
5536 (void) usage(B_TRUE);
5537 return (Z_OK);
5539 optind = 0;
5540 if ((arg = getopt(argc, argv, "?")) != EOF) {
5541 switch (arg) {
5542 case '?':
5543 sub_usage(SHELP_HELP, CMD_HELP);
5544 return (optopt == '?' ? Z_OK : Z_USAGE);
5545 default:
5546 sub_usage(SHELP_HELP, CMD_HELP);
5547 return (Z_USAGE);
5550 while (optind < argc) {
5551 /* Private commands have NULL short_usage; omit them */
5552 if ((cmd_num = cmd_match(argv[optind])) < 0 ||
5553 cmdtab[cmd_num].short_usage == NULL) {
5554 sub_usage(SHELP_HELP, CMD_HELP);
5555 return (Z_USAGE);
5557 sub_usage(cmdtab[cmd_num].short_usage, cmd_num);
5558 optind++;
5560 return (Z_OK);
5564 * Returns: CMD_MIN thru CMD_MAX on success, -1 on error
5567 static int
5568 cmd_match(char *cmd)
5570 int i;
5572 for (i = CMD_MIN; i <= CMD_MAX; i++) {
5573 /* return only if there is an exact match */
5574 if (strcmp(cmd, cmdtab[i].cmd_name) == 0)
5575 return (cmdtab[i].cmd_num);
5577 return (-1);
5580 static int
5581 parse_and_run(int argc, char *argv[])
5583 int i = cmd_match(argv[0]);
5585 if (i < 0)
5586 return (usage(B_FALSE));
5587 return (cmdtab[i].handler(argc - 1, &(argv[1])));
5590 static char *
5591 get_execbasename(char *execfullname)
5593 char *last_slash, *execbasename;
5595 /* guard against '/' at end of command invocation */
5596 for (;;) {
5597 last_slash = strrchr(execfullname, '/');
5598 if (last_slash == NULL) {
5599 execbasename = execfullname;
5600 break;
5601 } else {
5602 execbasename = last_slash + 1;
5603 if (*execbasename == '\0') {
5604 *last_slash = '\0';
5605 continue;
5607 break;
5610 return (execbasename);
5613 static char *
5614 get_username()
5616 uid_t uid;
5617 struct passwd *nptr;
5621 * Authorizations are checked to restrict access based on the
5622 * requested operation and zone name, It is assumed that the
5623 * program is running with all privileges, but that the real
5624 * user ID is that of the user or role on whose behalf we are
5625 * operating. So we start by getting the username that will be
5626 * used for subsequent authorization checks.
5629 uid = getuid();
5630 if ((nptr = getpwuid(uid)) == NULL) {
5631 zerror(gettext("could not get user name."));
5632 exit(Z_ERR);
5634 return (nptr->pw_name);
5638 main(int argc, char **argv)
5640 int arg;
5641 zoneid_t zid;
5642 struct stat st;
5643 char *zone_lock_env;
5644 int err;
5646 if ((locale = setlocale(LC_ALL, "")) == NULL)
5647 locale = "C";
5648 (void) textdomain(TEXT_DOMAIN);
5649 setbuf(stdout, NULL);
5650 (void) sigset(SIGHUP, SIG_IGN);
5651 execname = get_execbasename(argv[0]);
5652 username = get_username();
5653 target_zone = NULL;
5654 if (chdir("/") != 0) {
5655 zerror(gettext("could not change directory to /."));
5656 exit(Z_ERR);
5660 * Use the default system mask rather than anything that may have been
5661 * set by the caller.
5663 (void) umask(CMASK);
5665 if (init_zfs() != Z_OK)
5666 exit(Z_ERR);
5668 while ((arg = getopt(argc, argv, "?u:z:R:")) != EOF) {
5669 switch (arg) {
5670 case '?':
5671 return (usage(B_TRUE));
5672 case 'u':
5673 target_uuid = optarg;
5674 break;
5675 case 'z':
5676 target_zone = optarg;
5677 break;
5678 case 'R': /* private option for admin/install use */
5679 if (*optarg != '/') {
5680 zerror(gettext("root path must be absolute."));
5681 exit(Z_ERR);
5683 if (stat(optarg, &st) == -1 || !S_ISDIR(st.st_mode)) {
5684 zerror(
5685 gettext("root path must be a directory."));
5686 exit(Z_ERR);
5688 zonecfg_set_root(optarg);
5689 break;
5690 default:
5691 return (usage(B_FALSE));
5695 if (optind >= argc)
5696 return (usage(B_FALSE));
5698 if (target_uuid != NULL && *target_uuid != '\0') {
5699 uuid_t uuid;
5700 static char newtarget[ZONENAME_MAX];
5702 if (uuid_parse(target_uuid, uuid) == -1) {
5703 zerror(gettext("illegal UUID value specified"));
5704 exit(Z_ERR);
5706 if (zonecfg_get_name_by_uuid(uuid, newtarget,
5707 sizeof (newtarget)) == Z_OK)
5708 target_zone = newtarget;
5711 if (target_zone != NULL && zone_get_id(target_zone, &zid) != 0) {
5712 errno = Z_NO_ZONE;
5713 zperror(target_zone, B_TRUE);
5714 exit(Z_ERR);
5718 * See if we have inherited the right to manipulate this zone from
5719 * a zoneadm instance in our ancestry. If so, set zone_lock_cnt to
5720 * indicate it. If not, make that explicit in our environment.
5722 zonecfg_init_lock_file(target_zone, &zone_lock_env);
5724 /* Figure out what the system's default brand is */
5725 if (zonecfg_default_brand(default_brand,
5726 sizeof (default_brand)) != Z_OK) {
5727 zerror(gettext("unable to determine default brand"));
5728 return (Z_ERR);
5732 * If we are going to be operating on a single zone, retrieve its
5733 * brand type and determine whether it is native or not.
5735 if ((target_zone != NULL) &&
5736 (strcmp(target_zone, GLOBAL_ZONENAME) != 0)) {
5737 if (zone_get_brand(target_zone, target_brand,
5738 sizeof (target_brand)) != Z_OK) {
5739 zerror(gettext("missing or invalid brand"));
5740 exit(Z_ERR);
5743 * In the alternate root environment, the only supported
5744 * operations are mount and unmount. In this case, just treat
5745 * the zone as native if it is cluster. Cluster zones can be
5746 * native for the purpose of LU or upgrade, and the cluster
5747 * brand may not exist in the miniroot (such as in net install
5748 * upgrade).
5750 if (strcmp(target_brand, CLUSTER_BRAND_NAME) == 0) {
5751 if (zonecfg_in_alt_root()) {
5752 (void) strlcpy(target_brand, default_brand,
5753 sizeof (target_brand));
5758 err = parse_and_run(argc - optind, &argv[optind]);
5760 return (err);