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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2002 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*
28  * show.c: support for scadm show <variable> option (to show the value of
29  * a service processor NV variable)
30  */
31 
32 #include <libintl.h>
33 #include <stdio.h>
34 #include <string.h>
35 #include <time.h>  /* required by librsc.h */
36 
37 #include "librsc.h"
38 #include "adm.h"
39 
40 
41 
42 char *ADM_Get_Var(char *Variable);
43 
44 static void ADM_Show_Var(char *Variable);
45 static int ADM_Get_Next_Var(char *oldVar, char *newVar, int maxSize);
46 static void command_line();
47 
48 
49 void
ADM_Process_show(int argc,char * argv[])50 ADM_Process_show(int argc, char *argv[])
51 {
52 	char		*oldVar;
53 	static char	newVar[128];
54 
55 
56 	if ((argc != 2) && (argc != 3)) {
57 		(void) fprintf(stderr, "\n%s\n\n",
58 		    gettext("USAGE: scadm show [variable]"));
59 		exit(-1);
60 	}
61 
62 	ADM_Start();
63 
64 	if (argc == 2) {
65 		oldVar = NULL;
66 		newVar[0] = 0x0;
67 		while (ADM_Get_Next_Var(oldVar, newVar, 128) == 0) {
68 			ADM_Show_Var(newVar);
69 			oldVar = newVar;
70 		}
71 	} else {
72 		ADM_Show_Var(argv[2]);
73 	}
74 }
75 
76 void
ADM_Process_show_network()77 ADM_Process_show_network()
78 {
79 	rscp_msg_t		Message;
80 	struct timespec		Timeout;
81 	dp_get_network_cfg_r_t	*netParams;
82 
83 	ADM_Start();
84 
85 	Message.type = DP_GET_NETWORK_CFG;
86 	Message.len = 0;
87 	Message.data = NULL;
88 
89 	ADM_Send(&Message);
90 
91 	Timeout.tv_nsec = 0;
92 	Timeout.tv_sec = ADM_TIMEOUT;
93 	ADM_Recv(&Message, &Timeout,
94 	    DP_GET_NETWORK_CFG_R, sizeof (dp_get_network_cfg_r_t));
95 
96 	netParams = (dp_get_network_cfg_r_t *)Message.data;
97 
98 	/* Print the network configuration */
99 	if (netParams->status != 0) {
100 		(void) printf("%s \r\n", gettext("SC ethernet is disabled."));
101 	} else {
102 #if 0
103 		/* Include this if we want to display the IP mode */
104 		(void) printf("%s %s\r\n",
105 		    gettext("SC network configuration is:"),
106 		    netParams->ipMode);
107 #endif
108 		if (strcmp(netParams->ipMode, "dhcp") == 0) {
109 			(void) printf("%s %s\r\n", gettext("DHCP server:"),
110 			    netParams->ipDHCPServer);
111 		}
112 		(void) printf("%s %s\r\n", gettext("IP Address:"),
113 		    netParams->ipAddr);
114 		(void) printf("%s %s\r\n", gettext("Gateway address:"),
115 		    netParams->ipGateway);
116 		(void) printf("%s %s\r\n", gettext("Netmask:"),
117 		    netParams->ipMask);
118 		(void) printf("%s %s\r\n", gettext("Ethernet address:"),
119 		    netParams->ethAddr);
120 	}
121 
122 	ADM_Free(&Message);
123 
124 }
125 
126 char
ADM_Get_Var(char * Variable)127 *ADM_Get_Var(char *Variable)
128 {
129 	rscp_msg_t	Message;
130 	struct timespec	Timeout;
131 	char		*varValue;
132 
133 	varValue = NULL;
134 
135 	Message.type = DP_GET_CFGVAR;
136 	Message.len = strlen(Variable) + 1; /* + 1 for string termination */
137 	if (Message.len > DP_MAX_MSGLEN-4) {
138 		command_line();
139 		exit(-1);
140 	}
141 
142 	Message.data = Variable;
143 	ADM_Send(&Message);
144 
145 	Timeout.tv_nsec = 0;
146 	Timeout.tv_sec = ADM_TIMEOUT;
147 	ADM_Recv(&Message, &Timeout,
148 	    DP_GET_CFGVAR_R, sizeof (dp_get_cfgvar_r_t));
149 
150 	if (*(int *)Message.data != 0) {
151 		(void) fprintf(stderr, "\n%s - \"%s\"\n\n",
152 		    gettext("scadm: invalid variable"), Variable);
153 		exit(-1);
154 	}
155 
156 	/* show variable setting */
157 	/* The variable setting is right after the Status of the message */
158 	varValue = (char *)(&((char *)Message.data)[
159 	    sizeof (dp_get_cfgvar_r_t)]);
160 
161 	ADM_Free(&Message);
162 
163 	return (varValue);
164 }
165 
166 static void
ADM_Show_Var(char * Variable)167 ADM_Show_Var(char *Variable)
168 {
169 	char *varValue;
170 
171 	varValue = ADM_Get_Var(Variable);
172 	(void) printf("%s=\"%s\"\n", Variable, varValue);
173 	(void) fflush(stdout);
174 }
175 
176 static int
ADM_Get_Next_Var(char * oldVar,char * newVar,int maxSize)177 ADM_Get_Next_Var(char *oldVar, char *newVar, int maxSize)
178 {
179 	rscp_msg_t	Message;
180 	struct timespec	Timeout;
181 	char		*var;
182 
183 
184 	Message.type = DP_GET_CFGVAR_NAME;
185 	if (oldVar == NULL)
186 		Message.len = 0;
187 	else
188 		Message.len  = strlen(oldVar) + 1;	/* + 1 for string */
189 							/* termination */
190 
191 	if (Message.len > DP_MAX_MSGLEN-4) {
192 		command_line();
193 		exit(-1);
194 	}
195 
196 	Message.data = oldVar;
197 	ADM_Send(&Message);
198 
199 	Timeout.tv_nsec = 0;
200 	Timeout.tv_sec  = ADM_TIMEOUT;
201 	ADM_Recv(&Message, &Timeout,
202 	    DP_GET_CFGVAR_NAME_R, sizeof (dp_get_cfgvar_name_r_t));
203 	if (*(int *)Message.data != 0) {
204 		/* Last variable read */
205 		return (-1);
206 	}
207 
208 	/* The variable is right after the Status of the message */
209 	var = (char *)(&((char *)Message.data)[
210 	    sizeof (dp_get_cfgvar_name_r_t)]);
211 	(void) strncpy(newVar, var, maxSize);
212 
213 	ADM_Free(&Message);
214 
215 	return (0);
216 }
217 
218 
219 static void
command_line()220 command_line()
221 {
222 	(void) fprintf(stderr, "\n%s\n\n",
223 	    gettext("scadm: command line too long"));
224 }
225