8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / virtinfo / virtinfo.c
blobf00db2db35b120f6b09230d1dab74b40102d15bb
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 2010 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #include <errno.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <strings.h>
31 #include <unistd.h>
32 #include <libgen.h>
33 #include <libintl.h>
34 #include <libv12n.h>
35 #include <locale.h>
36 #include <zone.h>
37 #include <sys/types.h>
38 #include <sys/param.h>
39 #include <uuid/uuid.h>
41 #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
42 #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it wasn't */
43 #endif
45 static char *cmdname;
47 char *options = "acdpstu";
49 static void
50 virtinfo_usage()
52 (void) fprintf(stderr, gettext("usage: %s [-%s]\n"), cmdname, options);
53 exit(1);
56 static char *
57 virtinfo_cap_to_impl(int cap)
59 if (cap & V12N_CAP_IMPL_LDOMS)
60 return ("LDoms");
61 return ("Unknown");
65 int
66 main(int argc, char *argv[])
68 int cap;
69 int roles;
70 size_t rv;
71 int opt;
72 int errflg = 0;
73 int aflg = 0, cflg = 0, dflg = 0, pflg = 0, sflg = 0, tflg = 0,
74 uflg = 0;
76 /* Set locale environment variables local definitions */
77 (void) setlocale(LC_ALL, "");
78 (void) textdomain(TEXT_DOMAIN);
80 cmdname = basename(argv[0]);
82 /* disable getopt error messages */
83 opterr = 0;
85 while ((opt = getopt(argc, argv, options)) != EOF) {
87 switch (opt) {
88 case 'a':
89 aflg = 1;
90 break;
91 case 'c':
92 cflg = 1;
93 break;
94 case 'd':
95 dflg = 1;
96 break;
97 case 'p':
98 pflg = 1;
99 break;
100 case 's':
101 sflg = 1;
102 break;
103 case 't':
104 tflg = 1;
105 break;
106 case 'u':
107 uflg = 1;
108 break;
109 case '?':
110 default:
111 errflg = 1;
112 break;
116 if (errflg || optind != argc)
117 virtinfo_usage();
119 if (aflg) {
120 /* aflg -> set all flags except -p */
121 cflg = dflg = sflg = tflg = uflg = 1;
122 } else if (cflg == 0 && dflg == 0 && sflg == 0 && tflg == 0 &&
123 uflg == 0) {
124 /* no flag set, default to '-t' */
125 tflg = 1;
128 if (getzoneid() != GLOBAL_ZONEID) {
129 (void) printf(gettext(
130 "%s can only be run from the global zone\n"), cmdname);
131 exit(0);
134 cap = v12n_capabilities();
135 if ((cap & V12N_CAP_SUPPORTED) == 0) {
136 (void) printf(gettext("Virtual machines are not supported\n"));
137 exit(0);
138 } else if ((cap & V12N_CAP_ENABLED) == 0) {
139 (void) printf(gettext(
140 "Virtual machines (%s) are supported but not enabled\n"),
141 virtinfo_cap_to_impl(cap));
142 exit(0);
145 if (pflg) {
146 (void) printf("VERSION 1.0\n");
149 if (tflg) {
150 char *impl = "", *role = "", *io = "", *service = "",
151 *root = "";
153 roles = v12n_domain_roles();
155 if (roles == -1 || (cap & V12N_CAP_IMPL_LDOMS) == 0) {
156 if (pflg)
157 impl = "impl=Unknown";
158 else
159 impl = "Unknown";
160 } else if (pflg) {
161 impl = "impl=LDoms";
162 role = (roles & V12N_ROLE_CONTROL) ?
163 "|control=true" : "|control=false";
164 io = (roles & V12N_ROLE_IO) ?
165 "|io=true" : "|io=false";
166 service = (roles & V12N_ROLE_SERVICE) ?
167 "|service=true" : "|service=false";
168 root = (roles & V12N_ROLE_ROOT) ?
169 "|root=true" : "|root=false";
170 } else {
171 impl = "LDoms";
172 role = (roles & V12N_ROLE_CONTROL) ?
173 " control" : " guest";
174 io = (roles & V12N_ROLE_IO) ?
175 " I/O" : "";
176 service = (roles & V12N_ROLE_SERVICE) ?
177 " service" : "";
178 root = (roles & V12N_ROLE_ROOT) ?
179 " root" : "";
181 (void) printf("%s%s%s%s%s%s\n", pflg ? "DOMAINROLE|" :
182 gettext("Domain role: "), impl, role, io, service, root);
185 if (dflg) {
186 char domain_name[V12N_NAME_MAX];
188 rv = v12n_domain_name(domain_name, sizeof (domain_name));
189 if (rv == (size_t)(-1)) {
190 (void) strcpy(domain_name, "Unknown");
192 (void) printf("%s%s\n", pflg ? "DOMAINNAME|name=" :
193 gettext("Domain name: "), domain_name);
196 if (uflg) {
197 uuid_t uuid;
198 char uuid_str[UUID_PRINTABLE_STRING_LENGTH];
200 rv = v12n_domain_uuid(uuid);
202 if (rv == (size_t)(-1)) {
203 (void) strcpy(uuid_str, "Unknown");
204 } else {
205 uuid_unparse(uuid, uuid_str);
207 (void) printf("%s%s\n", pflg ? "DOMAINUUID|uuid=" :
208 gettext("Domain UUID: "), uuid_str);
211 if (cflg) {
212 char ctrl_name[V12N_NAME_MAX];
214 rv = v12n_ctrl_domain(ctrl_name, sizeof (ctrl_name));
216 if (rv == (size_t)(-1)) {
217 (void) strcpy(ctrl_name, "Unknown");
219 (void) printf("%s%s\n", pflg ? "DOMAINCONTROL|name=" :
220 gettext("Control domain: "), ctrl_name);
223 if (sflg) {
224 char serial_no[V12N_NAME_MAX];
226 rv = v12n_chassis_serialno(serial_no, sizeof (serial_no));
228 if (rv == (size_t)(-1)) {
229 (void) strcpy(serial_no, "Unknown");
231 (void) printf("%s%s\n", pflg ? "DOMAINCHASSIS|serialno=" :
232 gettext("Chassis serial#: "), serial_no);
234 return (0);