xref: /titanic_52/usr/src/cmd/picl/plugins/common/frutree/piclfrutree.c (revision c2580b931007758eab8cb5ae8726ebe1588e259b)
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) 1999-2000 by Sun Microsystems, Inc.
24  * All rights reserved.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * This plugin creates the PICL nodes and properties specified in
31  * configuration file.
32  * It is used to create the FRU tree for a platform.
33  * The configuration file for FRU tree is called "piclfrutree.conf".
34  */
35 
36 
37 #include <stdio.h>
38 #include <fcntl.h>
39 #include <unistd.h>
40 #include <stdlib.h>
41 #include <errno.h>
42 #include <string.h>
43 #include <syslog.h>
44 #include <alloca.h>
45 #include <limits.h>
46 #include <sys/utsname.h>
47 #include <sys/systeminfo.h>
48 #include <sys/stat.h>
49 #include <libintl.h>
50 #include <picl.h>
51 #include <picltree.h>
52 #include "picld_pluginutil.h"
53 
54 #define	EM_FAIL	gettext("SUNW_piclfrutree PICL plugin module failed")
55 
56 static	void	piclfrutree_register(void);
57 static	void	piclfrutree_init(void);
58 static	void	piclfrutree_fini(void);
59 
60 #define	FRUTREE_CONFFILE_NAME		"piclfrutree.conf"
61 
62 #pragma	init(piclfrutree_register)
63 
64 static picld_plugin_reg_t  my_reg_info = {
65 	PICLD_PLUGIN_VERSION_1,
66 	PICLD_PLUGIN_NON_CRITICAL,
67 	"SUNW_piclfrutree",
68 	piclfrutree_init,
69 	piclfrutree_fini
70 };
71 
72 static void
73 piclfrutree_register(void)
74 {
75 	(void) picld_plugin_register(&my_reg_info);
76 }
77 
78 /*
79  * Search for the frutree config file from the platform specific
80  * directory to the common directory.
81  *
82  * The size of outfilename must be PATH_MAX
83  */
84 static int
85 get_config_file(char *outfilename)
86 {
87 	char	nmbuf[SYS_NMLN];
88 	char	pname[PATH_MAX];
89 
90 	if (sysinfo(SI_PLATFORM, nmbuf, sizeof (nmbuf)) != -1) {
91 		(void) snprintf(pname, PATH_MAX, PICLD_PLAT_PLUGIN_DIRF, nmbuf);
92 		(void) strlcat(pname, FRUTREE_CONFFILE_NAME, PATH_MAX);
93 		if (access(pname, R_OK) == 0) {
94 			(void) strlcpy(outfilename, pname, PATH_MAX);
95 			return (0);
96 		}
97 	}
98 
99 	if (sysinfo(SI_MACHINE, nmbuf, sizeof (nmbuf)) != -1) {
100 		(void) snprintf(pname, PATH_MAX, PICLD_PLAT_PLUGIN_DIRF, nmbuf);
101 		(void) strlcat(pname, FRUTREE_CONFFILE_NAME, PATH_MAX);
102 		if (access(pname, R_OK) == 0) {
103 			(void) strlcpy(outfilename, pname, PATH_MAX);
104 			return (0);
105 		}
106 	}
107 
108 	(void) snprintf(pname, PATH_MAX, "%s/%s", PICLD_COMMON_PLUGIN_DIR,
109 	    FRUTREE_CONFFILE_NAME);
110 
111 	if (access(pname, R_OK) == 0) {
112 		(void) strlcpy(outfilename, pname, PATH_MAX);
113 		return (0);
114 	}
115 
116 	return (-1);
117 }
118 
119 static void
120 piclfrutree_init(void)
121 {
122 	char		fullfilename[PATH_MAX];
123 	picl_nodehdl_t	rooth;
124 
125 	if (get_config_file(fullfilename) < 0)
126 		return;
127 
128 	if (ptree_get_root(&rooth) != PICL_SUCCESS)
129 		return;
130 
131 	if (picld_pluginutil_parse_config_file(rooth, fullfilename) !=
132 	    PICL_SUCCESS)
133 		syslog(LOG_ERR, EM_FAIL);
134 }
135 
136 static void
137 piclfrutree_fini(void)
138 {
139 }
140