xref: /illumos-gate/usr/src/cmd/picl/plugins/common/frutree/piclfrutree.c (revision 2a8bcb4efb45d99ac41c94a75c396b362c414f7f)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1999-2000 by Sun Microsystems, Inc.
24*7c478bd9Sstevel@tonic-gate  * All rights reserved.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /*
28*7c478bd9Sstevel@tonic-gate  * This plugin creates the PICL nodes and properties specified in
29*7c478bd9Sstevel@tonic-gate  * configuration file.
30*7c478bd9Sstevel@tonic-gate  * It is used to create the FRU tree for a platform.
31*7c478bd9Sstevel@tonic-gate  * The configuration file for FRU tree is called "piclfrutree.conf".
32*7c478bd9Sstevel@tonic-gate  */
33*7c478bd9Sstevel@tonic-gate 
34*7c478bd9Sstevel@tonic-gate 
35*7c478bd9Sstevel@tonic-gate #include <stdio.h>
36*7c478bd9Sstevel@tonic-gate #include <fcntl.h>
37*7c478bd9Sstevel@tonic-gate #include <unistd.h>
38*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
39*7c478bd9Sstevel@tonic-gate #include <errno.h>
40*7c478bd9Sstevel@tonic-gate #include <string.h>
41*7c478bd9Sstevel@tonic-gate #include <syslog.h>
42*7c478bd9Sstevel@tonic-gate #include <alloca.h>
43*7c478bd9Sstevel@tonic-gate #include <limits.h>
44*7c478bd9Sstevel@tonic-gate #include <sys/utsname.h>
45*7c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h>
46*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
47*7c478bd9Sstevel@tonic-gate #include <libintl.h>
48*7c478bd9Sstevel@tonic-gate #include <picl.h>
49*7c478bd9Sstevel@tonic-gate #include <picltree.h>
50*7c478bd9Sstevel@tonic-gate #include "picld_pluginutil.h"
51*7c478bd9Sstevel@tonic-gate 
52*7c478bd9Sstevel@tonic-gate #define	EM_FAIL	gettext("SUNW_piclfrutree PICL plugin module failed")
53*7c478bd9Sstevel@tonic-gate 
54*7c478bd9Sstevel@tonic-gate static	void	piclfrutree_register(void);
55*7c478bd9Sstevel@tonic-gate static	void	piclfrutree_init(void);
56*7c478bd9Sstevel@tonic-gate static	void	piclfrutree_fini(void);
57*7c478bd9Sstevel@tonic-gate 
58*7c478bd9Sstevel@tonic-gate #define	FRUTREE_CONFFILE_NAME		"piclfrutree.conf"
59*7c478bd9Sstevel@tonic-gate 
60*7c478bd9Sstevel@tonic-gate #pragma	init(piclfrutree_register)
61*7c478bd9Sstevel@tonic-gate 
62*7c478bd9Sstevel@tonic-gate static picld_plugin_reg_t  my_reg_info = {
63*7c478bd9Sstevel@tonic-gate 	PICLD_PLUGIN_VERSION_1,
64*7c478bd9Sstevel@tonic-gate 	PICLD_PLUGIN_NON_CRITICAL,
65*7c478bd9Sstevel@tonic-gate 	"SUNW_piclfrutree",
66*7c478bd9Sstevel@tonic-gate 	piclfrutree_init,
67*7c478bd9Sstevel@tonic-gate 	piclfrutree_fini
68*7c478bd9Sstevel@tonic-gate };
69*7c478bd9Sstevel@tonic-gate 
70*7c478bd9Sstevel@tonic-gate static void
piclfrutree_register(void)71*7c478bd9Sstevel@tonic-gate piclfrutree_register(void)
72*7c478bd9Sstevel@tonic-gate {
73*7c478bd9Sstevel@tonic-gate 	(void) picld_plugin_register(&my_reg_info);
74*7c478bd9Sstevel@tonic-gate }
75*7c478bd9Sstevel@tonic-gate 
76*7c478bd9Sstevel@tonic-gate /*
77*7c478bd9Sstevel@tonic-gate  * Search for the frutree config file from the platform specific
78*7c478bd9Sstevel@tonic-gate  * directory to the common directory.
79*7c478bd9Sstevel@tonic-gate  *
80*7c478bd9Sstevel@tonic-gate  * The size of outfilename must be PATH_MAX
81*7c478bd9Sstevel@tonic-gate  */
82*7c478bd9Sstevel@tonic-gate static int
get_config_file(char * outfilename)83*7c478bd9Sstevel@tonic-gate get_config_file(char *outfilename)
84*7c478bd9Sstevel@tonic-gate {
85*7c478bd9Sstevel@tonic-gate 	char	nmbuf[SYS_NMLN];
86*7c478bd9Sstevel@tonic-gate 	char	pname[PATH_MAX];
87*7c478bd9Sstevel@tonic-gate 
88*7c478bd9Sstevel@tonic-gate 	if (sysinfo(SI_PLATFORM, nmbuf, sizeof (nmbuf)) != -1) {
89*7c478bd9Sstevel@tonic-gate 		(void) snprintf(pname, PATH_MAX, PICLD_PLAT_PLUGIN_DIRF, nmbuf);
90*7c478bd9Sstevel@tonic-gate 		(void) strlcat(pname, FRUTREE_CONFFILE_NAME, PATH_MAX);
91*7c478bd9Sstevel@tonic-gate 		if (access(pname, R_OK) == 0) {
92*7c478bd9Sstevel@tonic-gate 			(void) strlcpy(outfilename, pname, PATH_MAX);
93*7c478bd9Sstevel@tonic-gate 			return (0);
94*7c478bd9Sstevel@tonic-gate 		}
95*7c478bd9Sstevel@tonic-gate 	}
96*7c478bd9Sstevel@tonic-gate 
97*7c478bd9Sstevel@tonic-gate 	if (sysinfo(SI_MACHINE, nmbuf, sizeof (nmbuf)) != -1) {
98*7c478bd9Sstevel@tonic-gate 		(void) snprintf(pname, PATH_MAX, PICLD_PLAT_PLUGIN_DIRF, nmbuf);
99*7c478bd9Sstevel@tonic-gate 		(void) strlcat(pname, FRUTREE_CONFFILE_NAME, PATH_MAX);
100*7c478bd9Sstevel@tonic-gate 		if (access(pname, R_OK) == 0) {
101*7c478bd9Sstevel@tonic-gate 			(void) strlcpy(outfilename, pname, PATH_MAX);
102*7c478bd9Sstevel@tonic-gate 			return (0);
103*7c478bd9Sstevel@tonic-gate 		}
104*7c478bd9Sstevel@tonic-gate 	}
105*7c478bd9Sstevel@tonic-gate 
106*7c478bd9Sstevel@tonic-gate 	(void) snprintf(pname, PATH_MAX, "%s/%s", PICLD_COMMON_PLUGIN_DIR,
107*7c478bd9Sstevel@tonic-gate 	    FRUTREE_CONFFILE_NAME);
108*7c478bd9Sstevel@tonic-gate 
109*7c478bd9Sstevel@tonic-gate 	if (access(pname, R_OK) == 0) {
110*7c478bd9Sstevel@tonic-gate 		(void) strlcpy(outfilename, pname, PATH_MAX);
111*7c478bd9Sstevel@tonic-gate 		return (0);
112*7c478bd9Sstevel@tonic-gate 	}
113*7c478bd9Sstevel@tonic-gate 
114*7c478bd9Sstevel@tonic-gate 	return (-1);
115*7c478bd9Sstevel@tonic-gate }
116*7c478bd9Sstevel@tonic-gate 
117*7c478bd9Sstevel@tonic-gate static void
piclfrutree_init(void)118*7c478bd9Sstevel@tonic-gate piclfrutree_init(void)
119*7c478bd9Sstevel@tonic-gate {
120*7c478bd9Sstevel@tonic-gate 	char		fullfilename[PATH_MAX];
121*7c478bd9Sstevel@tonic-gate 	picl_nodehdl_t	rooth;
122*7c478bd9Sstevel@tonic-gate 
123*7c478bd9Sstevel@tonic-gate 	if (get_config_file(fullfilename) < 0)
124*7c478bd9Sstevel@tonic-gate 		return;
125*7c478bd9Sstevel@tonic-gate 
126*7c478bd9Sstevel@tonic-gate 	if (ptree_get_root(&rooth) != PICL_SUCCESS)
127*7c478bd9Sstevel@tonic-gate 		return;
128*7c478bd9Sstevel@tonic-gate 
129*7c478bd9Sstevel@tonic-gate 	if (picld_pluginutil_parse_config_file(rooth, fullfilename) !=
130*7c478bd9Sstevel@tonic-gate 	    PICL_SUCCESS)
131*7c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, EM_FAIL);
132*7c478bd9Sstevel@tonic-gate }
133*7c478bd9Sstevel@tonic-gate 
134*7c478bd9Sstevel@tonic-gate static void
piclfrutree_fini(void)135*7c478bd9Sstevel@tonic-gate piclfrutree_fini(void)
136*7c478bd9Sstevel@tonic-gate {
137*7c478bd9Sstevel@tonic-gate }
138