cmd: remove sparc-only virtinfo
[unleashed.git] / usr / src / cmd / picl / plugins / sun4u / snowbird / envmond / piclenvmond.c
blobd74a28f116245386d90c9b68159af4051948685e
1 /*
2 * CDDL HEADER START
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.
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.
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]
20 * CDDL HEADER END
23 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
29 #include <unistd.h>
30 #include <picl.h>
31 #include <picltree.h>
32 #include <picldefs.h>
33 #include <pthread.h>
34 #include <syslog.h>
35 #include <string.h>
36 #include <libnvpair.h>
37 #include <libintl.h>
38 #include "piclenvmond.h"
40 /* external funcs and varaibles */
41 extern void env_handle_event(const char *, const void *, size_t);
42 extern picl_errno_t env_init();
43 extern void env_platmod_fini();
44 extern int sensor_fd;
45 extern pthread_t env_temp_thr_tid;
47 /* local defines */
48 #define TIMEOUT (10)
50 #pragma init(piclenvmond_register)
53 * Plugin registration entry points
55 static void piclenvmond_register(void);
56 static void piclenvmond_init(void);
57 static void piclenvmond_fini(void);
58 static void piclenvmond_evhandler(const char *, const void *, size_t, void *);
60 int env_debug = 0x0;
62 static picld_plugin_reg_t envmond_reg_info = {
63 PICLD_PLUGIN_VERSION_1,
64 PICLD_PLUGIN_CRITICAL,
65 "SUNW_piclenvmond",
66 piclenvmond_init,
67 piclenvmond_fini
70 typedef struct {
71 picl_nodehdl_t nodehdl;
72 char node_name[PICL_PROPNAMELEN_MAX];
73 } env_callback_args_t;
76 * picld entry points
78 static void
79 piclenvmond_register(void)
81 (void) picld_plugin_register(&envmond_reg_info);
85 * picld entry point
86 * - do all the initialization
87 * - register for interested picl events
89 static void
90 piclenvmond_init(void)
92 picl_errno_t rc = PICL_SUCCESS;
94 if ((rc = env_init()) != PICL_SUCCESS) {
95 syslog(LOG_ERR, gettext("SUNW_envmond:envmond init failed, "
96 "error = %d"), rc);
97 return;
100 /* register handler for state change events */
101 (void) ptree_register_handler(PICLEVENT_STATE_CHANGE,
102 piclenvmond_evhandler, NULL);
103 /* register handler for condition change events */
104 (void) ptree_register_handler(PICLEVENT_CONDITION_CHANGE,
105 piclenvmond_evhandler, NULL);
109 static void
110 piclenvmond_fini(void)
112 void *exitval;
114 /* unregister event handler */
115 (void) ptree_unregister_handler(PICLEVENT_STATE_CHANGE,
116 piclenvmond_evhandler, NULL);
117 (void) ptree_unregister_handler(PICLEVENT_CONDITION_CHANGE,
118 piclenvmond_evhandler, NULL);
120 /* cancel all the threads */
121 (void) pthread_cancel(env_temp_thr_tid);
122 (void) pthread_join(env_temp_thr_tid, &exitval);
124 /* do any platform specific cleanups required */
125 env_platmod_fini();
126 (void) close(sensor_fd);
129 /*ARGSUSED*/
130 static void
131 piclenvmond_evhandler(const char *ename, const void *earg, size_t size,
132 void *cookie)
134 env_handle_event(ename, earg, size);
138 * Utility functions
142 * create_property -- Create a PICL property
144 picl_errno_t
145 env_create_property(int ptype, int pmode, size_t psize, char *pname,
146 int (*readfn)(ptree_rarg_t *, void *),
147 int (*writefn)(ptree_warg_t *, const void *),
148 picl_nodehdl_t nodeh, picl_prophdl_t *propp, void *vbuf)
150 picl_errno_t rc; /* return code */
151 ptree_propinfo_t propinfo; /* propinfo structure */
152 picl_prophdl_t proph;
154 rc = ptree_get_prop_by_name(nodeh, pname, &proph);
155 if (rc == PICL_SUCCESS) { /* prop. already exists */
156 return (rc);
159 rc = ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
160 ptype, pmode, psize, pname, readfn, writefn);
161 if (rc != PICL_SUCCESS) {
162 syslog(LOG_ERR, PTREE_INIT_PROPINFO_FAILED_MSG, rc);
163 return (rc);
166 rc = ptree_create_and_add_prop(nodeh, &propinfo, vbuf, propp);
167 if (rc != PICL_SUCCESS) {
168 syslog(LOG_ERR, PTREE_CREATE_AND_ADD_PROP_FAILED_MSG, rc);
169 return (rc);
171 return (PICL_SUCCESS);
175 * The picl event completion handler.
177 /* ARGSUSED */
178 static void
179 event_completion_handler(char *ename, void *earg, size_t size)
181 free(earg);
182 free(ename);
186 * utility routine to post PICL events
188 /*ARGSUSED*/
189 static int
190 post_picl_event(const char *ename, char *envl, size_t elen,
191 picl_nodehdl_t nodeh, int cond_wait)
193 nvlist_t *nvlp;
194 size_t nvl_size;
195 char *pack_buf = NULL;
196 char *evname;
198 if (nodeh == 0) {
199 return (PICL_FAILURE);
201 if ((evname = strdup(ename)) == NULL)
202 return (PICL_FAILURE);
203 if (envl) {
204 if (nvlist_unpack(envl, elen, &nvlp, 0) < 0) {
205 nvlist_free(nvlp);
206 free(evname);
207 return (PICL_FAILURE);
209 } else {
210 if (nvlist_alloc(&nvlp, NV_UNIQUE_NAME_TYPE, NULL)) {
211 free(evname);
212 return (PICL_FAILURE);
216 if (nvlist_add_uint64(nvlp, PICLEVENTARG_NODEHANDLE, nodeh) == -1) {
217 nvlist_free(nvlp);
218 free(evname);
219 return (PICL_FAILURE);
222 if (nvlist_pack(nvlp, &pack_buf, &nvl_size, NV_ENCODE_NATIVE, NULL)) {
223 nvlist_free(nvlp);
224 free(evname);
225 return (PICL_FAILURE);
227 nvlist_free(nvlp);
229 if (env_debug & EVENTS) {
230 char enodename[PICL_PROPNAMELEN_MAX];
231 if (ptree_get_propval_by_name(nodeh, PICL_PROP_NAME,
232 enodename, sizeof (enodename)) == PICL_SUCCESS)
233 syslog(LOG_INFO, "envmond:Posting %s on %s\n",
234 ename, enodename);
237 if (ptree_post_event(evname, pack_buf, nvl_size,
238 event_completion_handler) != 0) {
239 syslog(LOG_ERR, gettext("SUNW_envmond: Error posting %s PICL"
240 " event."), ename);
241 free(pack_buf);
242 free(evname);
243 return (PICL_FAILURE);
245 return (PICL_SUCCESS);
249 * post dr_req events
251 picl_errno_t
252 post_dr_req_event(picl_nodehdl_t fruh, char *dr_req_type, uint8_t wait)
254 nvlist_t *nvlp; /* nvlist of event specific args */
255 size_t nvl_size;
256 char *pack_buf = NULL;
257 char dr_ap_id[PICL_PROPNAMELEN_MAX];
258 int rc = PICL_SUCCESS;
260 if (env_debug & DEBUG)
261 syslog(LOG_DEBUG, "Post %s on %llx", dr_req_type, fruh);
262 if (fruh == 0) {
263 return (PICL_INVALIDARG);
265 if ((rc = ptree_get_propval_by_name(fruh, PICL_PROP_NAME,
266 dr_ap_id, sizeof (dr_ap_id))) != PICL_SUCCESS) {
267 return (rc);
270 if (nvlist_alloc(&nvlp, NV_UNIQUE_NAME_TYPE, NULL)) {
271 return (PICL_FAILURE);
274 if (nvlist_add_string(nvlp, PICLEVENTARG_AP_ID, dr_ap_id) == -1) {
275 nvlist_free(nvlp);
276 return (PICL_FAILURE);
278 if (nvlist_add_string(nvlp, PICLEVENTARG_DR_REQ_TYPE, dr_req_type)
279 == -1) {
280 nvlist_free(nvlp);
281 return (PICL_FAILURE);
283 if (nvlist_pack(nvlp, &pack_buf, &nvl_size, NV_ENCODE_NATIVE, NULL)) {
284 nvlist_free(nvlp);
285 return (PICL_FAILURE);
287 nvlist_free(nvlp);
289 if (env_debug & DEBUG)
290 syslog(LOG_DEBUG, "Posting %s on %s", dr_req_type, dr_ap_id);
291 rc = post_picl_event(PICLEVENT_DR_REQ, pack_buf, nvl_size, fruh,
292 wait);
294 free(pack_buf);
295 return (rc);
299 * routine to post dr_ap_state change events
301 picl_errno_t
302 post_dr_ap_state_change_event(picl_nodehdl_t nodehdl, char *dr_hint,
303 uint8_t wait)
305 nvlist_t *nvlp; /* nvlist of event specific args */
306 size_t nvl_size;
307 char *pack_buf = NULL;
308 char dr_ap_id[PICL_PROPNAMELEN_MAX];
309 int rc = PICL_SUCCESS;
311 if (nodehdl == 0) {
312 return (PICL_FAILURE);
314 if ((rc = ptree_get_propval_by_name(nodehdl, PICL_PROP_NAME,
315 dr_ap_id, sizeof (dr_ap_id))) != PICL_SUCCESS) {
316 return (rc);
318 if (nvlist_alloc(&nvlp, NV_UNIQUE_NAME_TYPE, NULL)) {
319 return (PICL_FAILURE);
322 if (nvlist_add_string(nvlp, PICLEVENTARG_AP_ID, dr_ap_id) == -1) {
323 nvlist_free(nvlp);
324 return (PICL_FAILURE);
326 if (nvlist_add_string(nvlp, PICLEVENTARG_HINT, dr_hint) == -1) {
327 nvlist_free(nvlp);
328 return (PICL_FAILURE);
330 if (nvlist_pack(nvlp, &pack_buf, &nvl_size, NV_ENCODE_NATIVE, NULL)) {
331 nvlist_free(nvlp);
332 return (PICL_FAILURE);
334 nvlist_free(nvlp);
335 rc = post_picl_event(PICLEVENT_DR_AP_STATE_CHANGE, pack_buf,
336 nvl_size, nodehdl, wait);
337 free(pack_buf);
338 return (rc);
341 picl_errno_t
342 post_cpu_state_change_event(picl_nodehdl_t fruh, char *event_type, uint8_t wait)
344 nvlist_t *nvlp; /* nvlist of event specific args */
345 size_t nvl_size;
346 char *pack_buf = NULL;
347 int rc = PICL_SUCCESS;
349 if (fruh == 0) {
350 return (PICL_FAILURE);
353 if (nvlist_alloc(&nvlp, NV_UNIQUE_NAME_TYPE, NULL))
354 return (PICL_FAILURE);
356 if (nvlist_add_int64(nvlp, PICLEVENTARG_NODEHANDLE, fruh)) {
357 nvlist_free(nvlp);
358 return (PICL_FAILURE);
361 if (nvlist_add_string(nvlp, PICLEVENTARG_CPU_EV_TYPE,
362 event_type) == -1) {
363 nvlist_free(nvlp);
364 return (PICL_FAILURE);
367 if (nvlist_pack(nvlp, &pack_buf, &nvl_size, NV_ENCODE_NATIVE, NULL)) {
368 nvlist_free(nvlp);
369 return (PICL_FAILURE);
371 nvlist_free(nvlp);
372 rc = post_picl_event(PICLEVENT_CPU_STATE_CHANGE, pack_buf,
373 nvl_size, fruh, wait);
374 free(pack_buf);
375 return (rc);
379 post_sensor_event(picl_nodehdl_t hdl, char *sensor_evalue, uint8_t wait)
381 nvlist_t *nvlp; /* nvlist of event specific args */
382 size_t nvl_size;
383 char *pack_buf = NULL;
384 char dr_ap_id[PICL_PROPNAMELEN_MAX];
385 int rc = PICL_SUCCESS;
387 if (env_debug & DEBUG)
388 syslog(LOG_DEBUG, "Post %s on %llx", sensor_evalue, hdl);
389 if (hdl == 0)
390 return (PICL_FAILURE);
392 if (nvlist_alloc(&nvlp, NV_UNIQUE_NAME_TYPE, NULL))
393 return (PICL_FAILURE);
395 if (nvlist_add_string(nvlp, PICLEVENTARG_CONDITION,
396 sensor_evalue) == -1) {
397 nvlist_free(nvlp);
398 return (PICL_FAILURE);
400 if (nvlist_pack(nvlp, &pack_buf, &nvl_size, NV_ENCODE_NATIVE, NULL)) {
401 nvlist_free(nvlp);
402 return (PICL_FAILURE);
404 nvlist_free(nvlp);
406 if (env_debug & DEBUG) {
407 if (ptree_get_propval_by_name(hdl, PICL_PROP_NAME, dr_ap_id,
408 sizeof (dr_ap_id)) == PICL_SUCCESS)
409 syslog(LOG_DEBUG, "Posting %s on %s", sensor_evalue,
410 dr_ap_id);
412 rc = post_picl_event(PICLEVENT_CONDITION_CHANGE, pack_buf, nvl_size,
413 hdl, wait);
414 free(pack_buf);
415 return (rc);
419 * return B_TRUE if admin lock is enabled
420 * return B_FALSE if admin lock is disabled
422 boolean_t
423 env_admin_lock_enabled(picl_nodehdl_t fruh)
425 char adminlock[PICL_PROPNAMELEN_MAX];
427 if (ptree_get_propval_by_name(fruh, PICL_PROP_ADMIN_LOCK,
428 adminlock, sizeof (adminlock))
429 != PICL_SUCCESS) {
430 return (B_FALSE);
432 if (strcmp(adminlock, PICL_ADMINLOCK_ENABLED) == 0) {
433 return (B_TRUE);
435 return (B_FALSE);