xref: /titanic_52/usr/src/cmd/picl/plugins/common/piclevent/piclevent.c (revision f841f6ad96ea6675d6c6b35c749eaac601799fdf)
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 2006 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * PICL plug-in that listens to sysevent and posts picl events
31  */
32 
33 #include <stdio.h>
34 #include <string.h>
35 #include <ctype.h>
36 #include <limits.h>
37 #include <stdlib.h>
38 #include <assert.h>
39 #include <alloca.h>
40 #include <unistd.h>
41 #include <stropts.h>
42 #include <syslog.h>
43 #include <libdevinfo.h>
44 #include <sys/time.h>
45 #include <fcntl.h>
46 #include <picl.h>
47 #include <picltree.h>
48 #include <sys/types.h>
49 #include <sys/sysinfo.h>
50 #include <dirent.h>
51 #include <libintl.h>
52 #include <sys/sunddi.h>
53 #include <sys/stat.h>
54 #include <libsysevent.h>
55 #include <libnvpair.h>
56 #include "piclevent.h"
57 
58 /*
59  * Plugin registration entry points
60  */
61 static void	eventplugin_register(void);
62 static void	eventplugin_init(void);
63 static void	eventplugin_fini(void);
64 
65 #pragma	init(eventplugin_register)
66 
67 static picld_plugin_reg_t  my_reg_info = {
68 	PICLD_PLUGIN_VERSION_1,
69 	PICLD_PLUGIN_CRITICAL,
70 	"SUNW_piclevent plugin for sysevents",
71 	eventplugin_init,
72 	eventplugin_fini
73 };
74 
75 /*
76  * Log message texts
77  */
78 #define	EVT_THR_FAILED		gettext("Event thread create failed!\n")
79 #define	EVT_OPEN_FAILED		gettext("PICL SLM door create failed\n")
80 
81 static	int		door_id = -1;
82 #define	SUNW_PICLEVENT_PLUGIN_DEBUG	"SUNW_PICLEVENT_PLUGIN_DEBUG"
83 static	int		piclevent_debug = 0;
84 
85 
86 /*
87  * completion handler for the posted picl event
88  */
89 /*ARGSUSED*/
90 static void
91 piclevent_completion_handler(char *ename, void *earg, size_t size)
92 {
93 	free(earg);
94 	free(ename);
95 }
96 
97 /*
98  * This function posts the incoming piclevent
99  * It packs the nvlist and posts it to PICL
100  */
101 static void
102 parse_piclevent(nvlist_t *nvlp)
103 {
104 	char		*enval;
105 	char		*ename;
106 	size_t		nvl_size;
107 	char		*packed_nvl;
108 	int		err;
109 
110 	if (nvlist_lookup_string(nvlp, PICLEVENTARG_EVENT_NAME, &enval))
111 		return;
112 
113 	packed_nvl = NULL;
114 	if (nvlist_pack(nvlp, &packed_nvl, &nvl_size, NV_ENCODE_NATIVE, NULL))
115 		return;
116 
117 	ename = strdup(enval);
118 	if (ename == NULL) {
119 		free(packed_nvl);
120 		return;
121 	}
122 
123 	if (piclevent_debug) {
124 		syslog(LOG_INFO, "piclevent: posting ename:%s packed_nvl:%p "
125 		    "nvl_size:0x%x\n", ename, packed_nvl, nvl_size);
126 	}
127 	err = ptree_post_event(ename, packed_nvl, nvl_size,
128 	    piclevent_completion_handler);
129 
130 	if (err != PICL_SUCCESS) {
131 		if (piclevent_debug)
132 			syslog(LOG_INFO,
133 			    "piclevent: posting ename:%s failed err:%d\n",
134 			    ename, err);
135 		free(ename);
136 		free(packed_nvl);
137 	}
138 }
139 
140 /*
141  * This is the PICL SLM door handler. It parses the event tuple received
142  * and posts an event to refresh the PICL tree.
143  */
144 /*ARGSUSED*/
145 static void
146 event_handler(void *cookie, char *argp, size_t asize,
147     door_desc_t *dp, uint_t n_desc)
148 {
149 	door_cred_t		cred;
150 	nvlist_t		*nvlp;
151 	char			*dtype;
152 
153 	if (piclevent_debug)
154 		syslog(LOG_INFO,
155 		    "piclevent: got SLM event cookie:%p evarg:%p size:0x%x\n",
156 		    cookie, argp, asize);
157 	if ((door_id < 0) || (argp == NULL) || (door_cred(&cred) < 0) ||
158 	    (cred.dc_euid != 0))
159 		(void) door_return(argp, 0, NULL, 0);
160 
161 	if (nvlist_unpack(argp, asize, &nvlp, NULL))
162 		(void) door_return(argp, 0, NULL, 0);
163 
164 	if (nvlist_lookup_string(nvlp, PICLEVENTARG_DATA_TYPE, &dtype)) {
165 		nvlist_free(nvlp);
166 		(void) door_return(argp, 0, NULL, 0);
167 	}
168 
169 	if (strcmp(dtype, PICLEVENTARG_PICLEVENT_DATA) == 0)
170 		parse_piclevent(nvlp);
171 	/*
172 	 * ignore other event data types
173 	 */
174 	nvlist_free(nvlp);
175 	(void) door_return(argp, 0, NULL, 0);
176 }
177 
178 /*
179  * Create the slm to picl plugin door
180  */
181 static int
182 setup_door(void)
183 {
184 	struct stat	stbuf;
185 
186 	/*
187 	 * Create the door
188 	 */
189 	door_id = door_create(event_handler, PICLEVENT_DOOR_COOKIE,
190 	    DOOR_REFUSE_DESC | DOOR_NO_CANCEL);
191 
192 	if (door_id < 0)
193 		return (-1);
194 
195 	if (stat(PICLEVENT_DOOR, &stbuf) < 0) {
196 		int newfd;
197 		if ((newfd = creat(PICLEVENT_DOOR, 0444)) < 0) {
198 			(void) door_revoke(door_id);
199 			door_id = -1;
200 			return (-1);
201 		}
202 		(void) close(newfd);
203 	}
204 
205 	if (fattach(door_id, PICLEVENT_DOOR) < 0) {
206 		if ((errno != EBUSY) || (fdetach(PICLEVENT_DOOR) < 0) ||
207 		    (fattach(door_id, PICLEVENT_DOOR) < 0)) {
208 			(void) door_revoke(door_id);
209 			door_id = -1;
210 			return (-1);
211 		}
212 	}
213 
214 	return (0);
215 }
216 
217 
218 /*
219  * This function is executed as part of .init when the plugin is
220  * dlopen()ed
221  */
222 static void
223 eventplugin_register(void)
224 {
225 	if (getenv(SUNW_PICLEVENT_PLUGIN_DEBUG))
226 		piclevent_debug = 1;
227 	(void) picld_plugin_register(&my_reg_info);
228 }
229 
230 /*
231  * This function is the init entry point of the plugin.
232  * It creates the slm to picl plugin door.
233  */
234 static void
235 eventplugin_init(void)
236 {
237 	if (setup_door() < 0) {
238 		syslog(LOG_ERR, EVT_OPEN_FAILED);
239 	}
240 }
241 
242 /*
243  * This function is the fini entry point of the plugin
244  */
245 static void
246 eventplugin_fini(void)
247 {
248 	if (door_id >= 0) {
249 		(void) door_revoke(door_id);
250 		door_id = -1;
251 	}
252 }
253