xref: /titanic_50/usr/src/cmd/zonename/zonename.c (revision 62a24de03df1f2399ceda704cb3874dabc98bbbd)
1 /*
2  * CDDL HEADER START
3  *
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.
7  *
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.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <locale.h>
31 #include <libintl.h>
32 #include <zone.h>
33 #include <libzonecfg.h>
34 #include <dlfcn.h>
35 #include <sys/zone.h>
36 
37 #if !defined(TEXT_DOMAIN)		/* should be defined by cc -D */
38 #define	TEXT_DOMAIN	"SYS_TEST"	/* Use this only if it wasn't */
39 #endif
40 
41 /*
42  * -t prints "shared" vs. "exclusive"
43  */
44 int
45 main(int argc, char *argv[])
46 {
47 	zoneid_t zoneid;
48 	char zonename[ZONENAME_MAX];
49 	FILE *fp;
50 	int arg;
51 	boolean_t stacktype = B_FALSE;
52 
53 	(void) setlocale(LC_ALL, "");
54 	(void) textdomain(TEXT_DOMAIN);
55 
56 	opterr = 0;
57 	while ((arg = getopt(argc, argv, "t")) != EOF) {
58 		switch (arg) {
59 		case 't':
60 			stacktype = B_TRUE;
61 			break;
62 		}
63 	}
64 
65 	zoneid = getzoneid();
66 
67 	if (stacktype) {
68 		ushort_t flags;
69 
70 		if (zone_getattr(zoneid, ZONE_ATTR_FLAGS, &flags,
71 		    sizeof (flags)) < 0) {
72 			perror("could not determine zone IP type");
73 			exit(1);
74 		}
75 		if (flags & ZF_NET_EXCL)
76 			(void) puts("exclusive");
77 		else
78 			(void) puts("shared");
79 		return (0);
80 	}
81 
82 	if (getzonenamebyid(zoneid, zonename, sizeof (zonename)) < 0) {
83 		(void) fputs(gettext("could not determine zone name\n"),
84 		    stderr);
85 		return (1);
86 	}
87 
88 	/*
89 	 * The use of dlopen here is a bit ugly, but it allows zonename to
90 	 * function properly before /usr is mounted.  On such a system, scratch
91 	 * zones don't exist, so no translation is necessary.
92 	 */
93 	if (dlopen("libzonecfg.so.1", RTLD_NOW | RTLD_GLOBAL) != NULL &&
94 	    zonecfg_is_scratch(zonename) &&
95 	    (fp = zonecfg_open_scratch("", B_FALSE)) != NULL) {
96 		(void) zonecfg_reverse_scratch(fp, zonename, zonename,
97 		    sizeof (zonename), NULL, 0);
98 		zonecfg_close_scratch(fp);
99 	}
100 	(void) puts(zonename);
101 	return (0);
102 }
103