xref: /onnv-gate/usr/src/cmd/zonename/zonename.c (revision 3448:aaf16568054b)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*3448Sdh155122  * Common Development and Distribution License (the "License").
6*3448Sdh155122  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*3448Sdh155122  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <stdio.h>
290Sstevel@tonic-gate #include <stdlib.h>
300Sstevel@tonic-gate #include <locale.h>
310Sstevel@tonic-gate #include <libintl.h>
320Sstevel@tonic-gate #include <zone.h>
33766Scarlsonj #include <libzonecfg.h>
34766Scarlsonj #include <dlfcn.h>
35*3448Sdh155122 #include <sys/zone.h>
360Sstevel@tonic-gate 
370Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)		/* should be defined by cc -D */
380Sstevel@tonic-gate #define	TEXT_DOMAIN	"SYS_TEST"	/* Use this only if it wasn't */
390Sstevel@tonic-gate #endif
400Sstevel@tonic-gate 
41*3448Sdh155122 /*
42*3448Sdh155122  * -t prints "shared" vs. "exclusive"
43*3448Sdh155122  */
440Sstevel@tonic-gate int
main(int argc,char * argv[])45*3448Sdh155122 main(int argc, char *argv[])
460Sstevel@tonic-gate {
47*3448Sdh155122 	zoneid_t zoneid;
480Sstevel@tonic-gate 	char zonename[ZONENAME_MAX];
49766Scarlsonj 	FILE *fp;
50*3448Sdh155122 	int arg;
51*3448Sdh155122 	boolean_t stacktype = B_FALSE;
520Sstevel@tonic-gate 
530Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
540Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
550Sstevel@tonic-gate 
56*3448Sdh155122 	opterr = 0;
57*3448Sdh155122 	while ((arg = getopt(argc, argv, "t")) != EOF) {
58*3448Sdh155122 		switch (arg) {
59*3448Sdh155122 		case 't':
60*3448Sdh155122 			stacktype = B_TRUE;
61*3448Sdh155122 			break;
62*3448Sdh155122 		}
63*3448Sdh155122 	}
64*3448Sdh155122 
65*3448Sdh155122 	zoneid = getzoneid();
66*3448Sdh155122 
67*3448Sdh155122 	if (stacktype) {
68*3448Sdh155122 		ushort_t flags;
69*3448Sdh155122 
70*3448Sdh155122 		if (zone_getattr(zoneid, ZONE_ATTR_FLAGS, &flags,
71*3448Sdh155122 		    sizeof (flags)) < 0) {
72*3448Sdh155122 			perror("could not determine zone IP type");
73*3448Sdh155122 			exit(1);
74*3448Sdh155122 		}
75*3448Sdh155122 		if (flags & ZF_NET_EXCL)
76*3448Sdh155122 			(void) puts("exclusive");
77*3448Sdh155122 		else
78*3448Sdh155122 			(void) puts("shared");
79*3448Sdh155122 		return (0);
80*3448Sdh155122 	}
81*3448Sdh155122 
82*3448Sdh155122 	if (getzonenamebyid(zoneid, zonename, sizeof (zonename)) < 0) {
830Sstevel@tonic-gate 		(void) fputs(gettext("could not determine zone name\n"),
840Sstevel@tonic-gate 		    stderr);
850Sstevel@tonic-gate 		return (1);
860Sstevel@tonic-gate 	}
87766Scarlsonj 
88766Scarlsonj 	/*
89766Scarlsonj 	 * The use of dlopen here is a bit ugly, but it allows zonename to
90766Scarlsonj 	 * function properly before /usr is mounted.  On such a system, scratch
91766Scarlsonj 	 * zones don't exist, so no translation is necessary.
92766Scarlsonj 	 */
93766Scarlsonj 	if (dlopen("libzonecfg.so.1", RTLD_NOW | RTLD_GLOBAL) != NULL &&
94766Scarlsonj 	    zonecfg_is_scratch(zonename) &&
95766Scarlsonj 	    (fp = zonecfg_open_scratch("", B_FALSE)) != NULL) {
96766Scarlsonj 		(void) zonecfg_reverse_scratch(fp, zonename, zonename,
97766Scarlsonj 		    sizeof (zonename), NULL, 0);
98766Scarlsonj 		zonecfg_close_scratch(fp);
99766Scarlsonj 	}
1000Sstevel@tonic-gate 	(void) puts(zonename);
1010Sstevel@tonic-gate 	return (0);
1020Sstevel@tonic-gate }
103