xref: /titanic_52/usr/src/cmd/locator/locator.c (revision bd0b44181edc734939be0794f7319af38ec1922c)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
506e46062Sjbeloro  * Common Development and Distribution License (the "License").
606e46062Sjbeloro  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
2106e46062Sjbeloro 
227c478bd9Sstevel@tonic-gate /*
23*bd0b4418Sjfrank  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
2406e46062Sjbeloro  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <stdio.h>
307c478bd9Sstevel@tonic-gate #include <stdlib.h>
317c478bd9Sstevel@tonic-gate #include <stdarg.h>
327c478bd9Sstevel@tonic-gate #include <string.h>
337c478bd9Sstevel@tonic-gate #include <libintl.h>
347c478bd9Sstevel@tonic-gate #include <locale.h>
357c478bd9Sstevel@tonic-gate #include <unistd.h>
367c478bd9Sstevel@tonic-gate #include <picl.h>
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #define	DEFAULT_NAME		"system"
397c478bd9Sstevel@tonic-gate 
4006e46062Sjbeloro typedef struct locator_info {
4106e46062Sjbeloro 	int		(*locator_func)(picl_nodehdl_t, struct locator_info *);
4206e46062Sjbeloro 	int		found; 		/* Nonzero if found during walk */
4306e46062Sjbeloro 	int		err;   		/* Last error from picl */
4406e46062Sjbeloro 	char		*name; 		/* Name/LocatorName of locator node */
4506e46062Sjbeloro 	int		new_state;	/* 0 = logical off, 1 = logical on */
4606e46062Sjbeloro 	char		*on;		/* Logical on value for State */
4706e46062Sjbeloro 	char		*off;		/* Logical off value for State */
487c478bd9Sstevel@tonic-gate } locator_info_t;
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate static void
517c478bd9Sstevel@tonic-gate usage(char *prog_name)
527c478bd9Sstevel@tonic-gate {
537c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("usage: %s [-n | -f]\n"), prog_name);
547c478bd9Sstevel@tonic-gate 	exit(1);
557c478bd9Sstevel@tonic-gate }
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate static int
5806e46062Sjbeloro change_locator_state(picl_nodehdl_t locator_node, locator_info_t *locator_info)
597c478bd9Sstevel@tonic-gate {
607c478bd9Sstevel@tonic-gate 	picl_prophdl_t	state_prop;
617c478bd9Sstevel@tonic-gate 	char		state[PICL_PROPNAMELEN_MAX];
627c478bd9Sstevel@tonic-gate 	int		err;
6306e46062Sjbeloro 	char		*new_state;
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate 	err = picl_get_prop_by_name(locator_node, "State", &state_prop);
667c478bd9Sstevel@tonic-gate 	if (err != PICL_SUCCESS) {
677c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
687c478bd9Sstevel@tonic-gate 			gettext("picl_get_prop_by_name failed: %s\n"),
697c478bd9Sstevel@tonic-gate 			picl_strerror(err));
707c478bd9Sstevel@tonic-gate 		return (err);
717c478bd9Sstevel@tonic-gate 	}
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate 	err = picl_get_propval(state_prop, state, sizeof (state));
747c478bd9Sstevel@tonic-gate 	if (err != PICL_SUCCESS) {
757c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
767c478bd9Sstevel@tonic-gate 			gettext("picl_get_propval failed: %s\n"),
777c478bd9Sstevel@tonic-gate 			picl_strerror(err));
787c478bd9Sstevel@tonic-gate 		return (err);
797c478bd9Sstevel@tonic-gate 	}
807c478bd9Sstevel@tonic-gate 
8106e46062Sjbeloro 	new_state = (locator_info->new_state) ? locator_info->on :
8206e46062Sjbeloro 	    locator_info->off;
8306e46062Sjbeloro 
847c478bd9Sstevel@tonic-gate 	if (strcmp(state, new_state) != 0) {
857c478bd9Sstevel@tonic-gate 		picl_propinfo_t prop_info;
867c478bd9Sstevel@tonic-gate 		err = picl_get_propinfo(state_prop, &prop_info);
877c478bd9Sstevel@tonic-gate 		if (err != PICL_SUCCESS) {
887c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
897c478bd9Sstevel@tonic-gate 				gettext("picl_get_propinfo failed: %s\n"),
907c478bd9Sstevel@tonic-gate 				picl_strerror(err));
917c478bd9Sstevel@tonic-gate 			return (err);
927c478bd9Sstevel@tonic-gate 		}
937c478bd9Sstevel@tonic-gate 		err = picl_set_propval(state_prop, new_state, prop_info.size);
947c478bd9Sstevel@tonic-gate 		if (err != PICL_SUCCESS) {
957c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
967c478bd9Sstevel@tonic-gate 				gettext("picl_set_propval failed: %s\n"),
977c478bd9Sstevel@tonic-gate 				picl_strerror(err));
987c478bd9Sstevel@tonic-gate 			return (err);
997c478bd9Sstevel@tonic-gate 		}
1007c478bd9Sstevel@tonic-gate 	}
1017c478bd9Sstevel@tonic-gate 	return (err);
1027c478bd9Sstevel@tonic-gate }
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate static int
10506e46062Sjbeloro display_locator_state(picl_nodehdl_t locator_node,
10606e46062Sjbeloro     locator_info_t *locator_info)
1077c478bd9Sstevel@tonic-gate {
1087c478bd9Sstevel@tonic-gate 	char		state[PICL_PROPNAMELEN_MAX];
10906e46062Sjbeloro 	char		*display_state;
1107c478bd9Sstevel@tonic-gate 	int		err;
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate 	err = picl_get_propval_by_name(locator_node, "State",
1137c478bd9Sstevel@tonic-gate 		state, sizeof (state));
1147c478bd9Sstevel@tonic-gate 	if (err != PICL_SUCCESS) {
1157c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
1167c478bd9Sstevel@tonic-gate 			gettext("picl_get_propval_by_name failed: %s\n"),
1177c478bd9Sstevel@tonic-gate 			picl_strerror(err));
1187c478bd9Sstevel@tonic-gate 		return (err);
1197c478bd9Sstevel@tonic-gate 	}
1207c478bd9Sstevel@tonic-gate 
12106e46062Sjbeloro 	if (strcmp(state, locator_info->on) == 0)
12206e46062Sjbeloro 		display_state = gettext("on");
12306e46062Sjbeloro 	else if (strcmp(state, locator_info->off) == 0)
12406e46062Sjbeloro 		display_state = gettext("off");
12506e46062Sjbeloro 	else
12606e46062Sjbeloro 		display_state = state;
12706e46062Sjbeloro 
1287c478bd9Sstevel@tonic-gate 	(void) printf(gettext("The '%s' locator is %s.\n"),
12906e46062Sjbeloro 		locator_info->name, display_state);
1307c478bd9Sstevel@tonic-gate 	return (err);
1317c478bd9Sstevel@tonic-gate }
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate static int
1347c478bd9Sstevel@tonic-gate locator_walker_func(picl_nodehdl_t nodeh, void *arg)
1357c478bd9Sstevel@tonic-gate {
1367c478bd9Sstevel@tonic-gate 	locator_info_t	*locator_info = (locator_info_t *)arg;
1377c478bd9Sstevel@tonic-gate 	int		err;
1387c478bd9Sstevel@tonic-gate 	char		is_locator[PICL_PROPNAMELEN_MAX];
1397c478bd9Sstevel@tonic-gate 	char		name[PICL_PROPNAMELEN_MAX];
14006e46062Sjbeloro 	char		locator_on[PICL_PROPNAMELEN_MAX];
14106e46062Sjbeloro 	char		locator_off[PICL_PROPNAMELEN_MAX];
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 	err = picl_get_propval_by_name(nodeh, "IsLocator", is_locator,
1447c478bd9Sstevel@tonic-gate 		sizeof (is_locator));
14506e46062Sjbeloro 
1467c478bd9Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
1477c478bd9Sstevel@tonic-gate 		return (PICL_WALK_CONTINUE);
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 	if (strcmp(is_locator, "true") != 0)
1507c478bd9Sstevel@tonic-gate 		return (PICL_WALK_CONTINUE);
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 	err = picl_get_propval_by_name(nodeh, "LocatorName", name,
1537c478bd9Sstevel@tonic-gate 		sizeof (name));
15406e46062Sjbeloro 
1557c478bd9Sstevel@tonic-gate 	if (err == PICL_PROPNOTFOUND)
1567c478bd9Sstevel@tonic-gate 		err = picl_get_propval_by_name(nodeh, PICL_PROP_NAME, name,
1577c478bd9Sstevel@tonic-gate 			sizeof (name));
15806e46062Sjbeloro 
1597c478bd9Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
1607c478bd9Sstevel@tonic-gate 		return (err);
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 	if (strcmp(name, locator_info->name) != 0)
1637c478bd9Sstevel@tonic-gate 		return (PICL_WALK_CONTINUE);
1647c478bd9Sstevel@tonic-gate 
16506e46062Sjbeloro 	err = picl_get_propval_by_name(nodeh, "LocatorOn", locator_on,
16606e46062Sjbeloro 		sizeof (locator_on));
1677c478bd9Sstevel@tonic-gate 
16806e46062Sjbeloro 	if (err == PICL_SUCCESS) {
16906e46062Sjbeloro 		locator_info->on = locator_on;
17006e46062Sjbeloro 	} else if (err == PICL_PROPNOTFOUND) {
171*bd0b4418Sjfrank 		locator_info->on = "ON";
17206e46062Sjbeloro 	} else {
17306e46062Sjbeloro 		return (err);
17406e46062Sjbeloro 	}
17506e46062Sjbeloro 
17606e46062Sjbeloro 	err = picl_get_propval_by_name(nodeh, "LocatorOff", locator_off,
17706e46062Sjbeloro 		sizeof (locator_off));
17806e46062Sjbeloro 
17906e46062Sjbeloro 	if (err == PICL_SUCCESS) {
18006e46062Sjbeloro 		locator_info->off = locator_off;
18106e46062Sjbeloro 	} else if (err == PICL_PROPNOTFOUND) {
182*bd0b4418Sjfrank 		locator_info->off = "OFF";
18306e46062Sjbeloro 	} else {
18406e46062Sjbeloro 		return (err);
18506e46062Sjbeloro 	}
18606e46062Sjbeloro 
18706e46062Sjbeloro 	locator_info->err = (locator_info->locator_func)(nodeh,
18806e46062Sjbeloro 		locator_info);
1897c478bd9Sstevel@tonic-gate 	locator_info->found = 1;
19006e46062Sjbeloro 
1917c478bd9Sstevel@tonic-gate 	return (PICL_WALK_TERMINATE);
1927c478bd9Sstevel@tonic-gate }
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate int
1957c478bd9Sstevel@tonic-gate main(int argc, char **argv)
1967c478bd9Sstevel@tonic-gate {
1977c478bd9Sstevel@tonic-gate 	locator_info_t	locator_info = {0, 0, 0, 0, 0};
1987c478bd9Sstevel@tonic-gate 	picl_nodehdl_t	rooth;
1997c478bd9Sstevel@tonic-gate 	int		err;
2007c478bd9Sstevel@tonic-gate 	int		c;
2017c478bd9Sstevel@tonic-gate 	int		on_flag = 0;
2027c478bd9Sstevel@tonic-gate 	int		off_flag = 0;
2037c478bd9Sstevel@tonic-gate 	char		*progname;
2047c478bd9Sstevel@tonic-gate 	char		*locator_name = DEFAULT_NAME;
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
2077c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 	if ((progname = strrchr(argv[0], '/')) == NULL)
2107c478bd9Sstevel@tonic-gate 		progname = argv[0];
2117c478bd9Sstevel@tonic-gate 	else
2127c478bd9Sstevel@tonic-gate 		progname++;
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "nf")) != EOF) {
2157c478bd9Sstevel@tonic-gate 		switch (c) {
2167c478bd9Sstevel@tonic-gate 		case 'n':
2177c478bd9Sstevel@tonic-gate 			on_flag++;
2187c478bd9Sstevel@tonic-gate 			break;
2197c478bd9Sstevel@tonic-gate 		case 'f':
2207c478bd9Sstevel@tonic-gate 			off_flag++;
2217c478bd9Sstevel@tonic-gate 			break;
2227c478bd9Sstevel@tonic-gate 		case '?':
2237c478bd9Sstevel@tonic-gate 			/*FALLTHROUGH*/
2247c478bd9Sstevel@tonic-gate 		default:
2257c478bd9Sstevel@tonic-gate 			usage(progname);
2267c478bd9Sstevel@tonic-gate 		}
2277c478bd9Sstevel@tonic-gate 	}
2287c478bd9Sstevel@tonic-gate 	if (argc != optind)
2297c478bd9Sstevel@tonic-gate 		usage(progname);
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 	/* We only take one option */
2327c478bd9Sstevel@tonic-gate 	if (on_flag && off_flag)
2337c478bd9Sstevel@tonic-gate 		usage(progname);
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 	err = picl_initialize();
2367c478bd9Sstevel@tonic-gate 	if (err != PICL_SUCCESS) {
2377c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("picl_initialize failed: %s\n"),
2387c478bd9Sstevel@tonic-gate 			picl_strerror(err));
2397c478bd9Sstevel@tonic-gate 		exit(2);
2407c478bd9Sstevel@tonic-gate 	}
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 	err = picl_get_root(&rooth);
2437c478bd9Sstevel@tonic-gate 	if (err != PICL_SUCCESS) {
2447c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("picl_get_root failed: %s\n"),
2457c478bd9Sstevel@tonic-gate 			picl_strerror(err));
2467c478bd9Sstevel@tonic-gate 		err = 2;
2477c478bd9Sstevel@tonic-gate 		goto OUT;
2487c478bd9Sstevel@tonic-gate 	}
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate 	if (on_flag) {
2517c478bd9Sstevel@tonic-gate 		locator_info.locator_func = change_locator_state;
25206e46062Sjbeloro 		locator_info.new_state = 1;
2537c478bd9Sstevel@tonic-gate 	} else if (off_flag) {
2547c478bd9Sstevel@tonic-gate 		locator_info.locator_func = change_locator_state;
25506e46062Sjbeloro 		locator_info.new_state = 0;
2567c478bd9Sstevel@tonic-gate 	} else {
2577c478bd9Sstevel@tonic-gate 		locator_info.locator_func = display_locator_state;
2587c478bd9Sstevel@tonic-gate 	}
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate 	locator_info.name = locator_name;
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 	err = picl_walk_tree_by_class(rooth, "led", &locator_info,
2637c478bd9Sstevel@tonic-gate 		locator_walker_func);
2647c478bd9Sstevel@tonic-gate 	if (err != PICL_SUCCESS) {
2657c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
2667c478bd9Sstevel@tonic-gate 			gettext("picl_walk_tree_by_class failed: %s\n"),
2677c478bd9Sstevel@tonic-gate 			picl_strerror(err));
2687c478bd9Sstevel@tonic-gate 		err = 2;
2697c478bd9Sstevel@tonic-gate 		goto OUT;
2707c478bd9Sstevel@tonic-gate 	}
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate 	if (locator_info.found == 0) {
2737c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("'%s' locator not found\n"),
2747c478bd9Sstevel@tonic-gate 			locator_name);
2757c478bd9Sstevel@tonic-gate 		err = 2;
2767c478bd9Sstevel@tonic-gate 	}
2777c478bd9Sstevel@tonic-gate 	if (locator_info.err != PICL_SUCCESS)
2787c478bd9Sstevel@tonic-gate 		err = 2;
2797c478bd9Sstevel@tonic-gate OUT:
2807c478bd9Sstevel@tonic-gate 	(void) picl_shutdown();
2817c478bd9Sstevel@tonic-gate 	return (err);
2827c478bd9Sstevel@tonic-gate }
283