xref: /titanic_52/usr/src/cmd/locator/locator.c (revision bdfc6d18da790deeec2e0eb09c625902defe2498)
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 (c) 2001 by Sun Microsystems, Inc.
24  * All rights reserved.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <stdarg.h>
32 #include <string.h>
33 #include <libintl.h>
34 #include <locale.h>
35 #include <unistd.h>
36 #include <picl.h>
37 
38 #define	DEFAULT_NAME		"system"
39 
40 typedef struct {
41 	int		(*locator_func)(picl_nodehdl_t, char *);
42 	int		found;
43 	int		err;
44 	char		*name;
45 	char		*str;
46 } locator_info_t;
47 
48 static void
49 usage(char *prog_name)
50 {
51 	(void) fprintf(stderr, gettext("usage: %s [-n | -f]\n"), prog_name);
52 	exit(1);
53 }
54 
55 static int
56 change_locator_state(picl_nodehdl_t locator_node, char *new_state)
57 {
58 	picl_prophdl_t	state_prop;
59 	char		state[PICL_PROPNAMELEN_MAX];
60 	int		err;
61 
62 	err = picl_get_prop_by_name(locator_node, "State", &state_prop);
63 	if (err != PICL_SUCCESS) {
64 		(void) fprintf(stderr,
65 			gettext("picl_get_prop_by_name failed: %s\n"),
66 			picl_strerror(err));
67 		return (err);
68 	}
69 
70 	err = picl_get_propval(state_prop, state, sizeof (state));
71 	if (err != PICL_SUCCESS) {
72 		(void) fprintf(stderr,
73 			gettext("picl_get_propval failed: %s\n"),
74 			picl_strerror(err));
75 		return (err);
76 	}
77 
78 	if (strcmp(state, new_state) != 0) {
79 		picl_propinfo_t prop_info;
80 		err = picl_get_propinfo(state_prop, &prop_info);
81 		if (err != PICL_SUCCESS) {
82 			(void) fprintf(stderr,
83 				gettext("picl_get_propinfo failed: %s\n"),
84 				picl_strerror(err));
85 			return (err);
86 		}
87 		err = picl_set_propval(state_prop, new_state, prop_info.size);
88 		if (err != PICL_SUCCESS) {
89 			(void) fprintf(stderr,
90 				gettext("picl_set_propval failed: %s\n"),
91 				picl_strerror(err));
92 			return (err);
93 		}
94 	}
95 	return (err);
96 }
97 
98 static int
99 display_locator_state(picl_nodehdl_t locator_node, char *locator_name)
100 {
101 	char		state[PICL_PROPNAMELEN_MAX];
102 	int		err;
103 
104 	err = picl_get_propval_by_name(locator_node, "State",
105 		state, sizeof (state));
106 	if (err != PICL_SUCCESS) {
107 		(void) fprintf(stderr,
108 			gettext("picl_get_propval_by_name failed: %s\n"),
109 			picl_strerror(err));
110 		return (err);
111 	}
112 
113 	(void) printf(gettext("The '%s' locator is %s.\n"),
114 		locator_name, state);
115 	return (err);
116 }
117 
118 static int
119 locator_walker_func(picl_nodehdl_t nodeh, void *arg)
120 {
121 	locator_info_t	*locator_info = (locator_info_t *)arg;
122 	int		err;
123 	char		is_locator[PICL_PROPNAMELEN_MAX];
124 	char		name[PICL_PROPNAMELEN_MAX];
125 
126 	err = picl_get_propval_by_name(nodeh, "IsLocator", is_locator,
127 		sizeof (is_locator));
128 	if (err != PICL_SUCCESS)
129 		return (PICL_WALK_CONTINUE);
130 
131 	if (strcmp(is_locator, "true") != 0)
132 		return (PICL_WALK_CONTINUE);
133 
134 	err = picl_get_propval_by_name(nodeh, "LocatorName", name,
135 		sizeof (name));
136 	if (err == PICL_PROPNOTFOUND)
137 		err = picl_get_propval_by_name(nodeh, PICL_PROP_NAME, name,
138 			sizeof (name));
139 	if (err != PICL_SUCCESS)
140 		return (err);
141 
142 	if (strcmp(name, locator_info->name) != 0)
143 		return (PICL_WALK_CONTINUE);
144 
145 	locator_info->err =
146 		(locator_info->locator_func)(nodeh, locator_info->str);
147 
148 	locator_info->found = 1;
149 	return (PICL_WALK_TERMINATE);
150 }
151 
152 int
153 main(int argc, char **argv)
154 {
155 	locator_info_t	locator_info = {0, 0, 0, 0, 0};
156 	picl_nodehdl_t	rooth;
157 	int		err;
158 	int		c;
159 	int		on_flag = 0;
160 	int		off_flag = 0;
161 	char		*progname;
162 	char		*locator_name = DEFAULT_NAME;
163 
164 	(void) setlocale(LC_ALL, "");
165 	(void) textdomain(TEXT_DOMAIN);
166 
167 	if ((progname = strrchr(argv[0], '/')) == NULL)
168 		progname = argv[0];
169 	else
170 		progname++;
171 
172 	while ((c = getopt(argc, argv, "nf")) != EOF) {
173 		switch (c) {
174 		case 'n':
175 			on_flag++;
176 			break;
177 		case 'f':
178 			off_flag++;
179 			break;
180 		case '?':
181 			/*FALLTHROUGH*/
182 		default:
183 			usage(progname);
184 		}
185 	}
186 	if (argc != optind)
187 		usage(progname);
188 
189 	/* We only take one option */
190 	if (on_flag && off_flag)
191 		usage(progname);
192 
193 	err = picl_initialize();
194 	if (err != PICL_SUCCESS) {
195 		(void) fprintf(stderr, gettext("picl_initialize failed: %s\n"),
196 			picl_strerror(err));
197 		exit(2);
198 	}
199 
200 	err = picl_get_root(&rooth);
201 	if (err != PICL_SUCCESS) {
202 		(void) fprintf(stderr, gettext("picl_get_root failed: %s\n"),
203 			picl_strerror(err));
204 		err = 2;
205 		goto OUT;
206 	}
207 
208 	if (on_flag) {
209 		locator_info.locator_func = change_locator_state;
210 		locator_info.str = "ON";
211 	} else if (off_flag) {
212 		locator_info.locator_func = change_locator_state;
213 		locator_info.str = "OFF";
214 	} else {
215 		locator_info.locator_func = display_locator_state;
216 		locator_info.str = locator_name;
217 	}
218 
219 	locator_info.name = locator_name;
220 
221 	err = picl_walk_tree_by_class(rooth, "led", &locator_info,
222 		locator_walker_func);
223 	if (err != PICL_SUCCESS) {
224 		(void) fprintf(stderr,
225 			gettext("picl_walk_tree_by_class failed: %s\n"),
226 			picl_strerror(err));
227 		err = 2;
228 		goto OUT;
229 	}
230 
231 	if (locator_info.found == 0) {
232 		(void) fprintf(stderr, gettext("'%s' locator not found\n"),
233 			locator_name);
234 		err = 2;
235 	}
236 	if (locator_info.err != PICL_SUCCESS)
237 		err = 2;
238 OUT:
239 	(void) picl_shutdown();
240 	return (err);
241 }
242