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