4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #include "librcm_impl.h"
27 #include "librcm_event.h"
30 static int rcm_debug
= 1;
31 #define dprintf(args) if (rcm_debug) (void) fprintf args
33 #define dprintf(args) /* nothing */
36 static int extract_info(nvlist_t
*, rcm_info_t
**);
37 static int rcm_daemon_is_alive();
38 static int rcm_common(int, rcm_handle_t
*, char **, uint_t
, void *,
40 static int rcm_direct_call(int, rcm_handle_t
*, char **, uint_t
, void *,
42 static int rcm_daemon_call(int, rcm_handle_t
*, char **, uint_t
, void *,
44 static int rcm_generate_nvlist(int, rcm_handle_t
*, char **, uint_t
, void *,
46 static int rcm_check_permission(void);
49 * Allocate a handle structure
53 rcm_alloc_handle(char *modname
, uint_t flag
, void *arg
, rcm_handle_t
**hdp
)
57 char namebuf
[MAXPATHLEN
];
59 if ((hdp
== NULL
) || (flag
& ~RCM_ALLOC_HDL_MASK
)) {
64 if (rcm_check_permission() == 0) {
69 if ((hd
= calloc(1, sizeof (*hd
))) == NULL
) {
74 (void) snprintf(namebuf
, MAXPATHLEN
, "%s%s", modname
,
77 if ((hd
->modname
= strdup(namebuf
)) == NULL
) {
82 if ((temp
= rcm_module_open(namebuf
)) == NULL
) {
89 rcm_module_close(temp
);
92 if (flag
& RCM_NOPID
) {
95 hd
->pid
= (pid_t
)getpid();
102 /* free handle structure */
104 rcm_free_handle(rcm_handle_t
*hd
)
108 return (RCM_FAILURE
);
116 return (RCM_SUCCESS
);
121 * Operations which require daemon processing
124 /* get registration and DR information from rcm_daemon */
126 rcm_get_info(rcm_handle_t
*hd
, char *rsrcname
, uint_t flag
, rcm_info_t
**infop
)
130 if ((flag
& ~RCM_GET_INFO_MASK
) || (infop
== NULL
)) {
132 return (RCM_FAILURE
);
136 * rsrcname may be NULL if requesting dr operations or modinfo
138 if (rsrcname
== NULL
&&
139 (flag
& (RCM_DR_OPERATION
| RCM_MOD_INFO
)) == 0) {
141 return (RCM_FAILURE
);
144 rsrcnames
[0] = rsrcname
;
147 return (rcm_common(CMD_GETINFO
, hd
, rsrcnames
, flag
, NULL
, infop
));
150 /* get registration and DR information from rcm_daemon (list version) */
152 rcm_get_info_list(rcm_handle_t
*hd
, char **rsrcnames
, uint_t flag
,
155 /* Requesting the current DR operations with a *list() is invalid */
156 if ((flag
& RCM_DR_OPERATION
) || (flag
& RCM_MOD_INFO
)) {
158 return (RCM_FAILURE
);
161 return (rcm_common(CMD_GETINFO
, hd
, rsrcnames
, flag
, NULL
, infop
));
164 /* request to offline a resource before DR removal */
166 rcm_request_offline(rcm_handle_t
*hd
, char *rsrcname
, uint_t flag
,
171 rsrcnames
[0] = rsrcname
;
174 return (rcm_request_offline_list(hd
, rsrcnames
, flag
, infop
));
177 /* request to offline a resource before DR removal (list version) */
179 rcm_request_offline_list(rcm_handle_t
*hd
, char **rsrcnames
, uint_t flag
,
182 if (flag
& ~RCM_REQUEST_MASK
) {
184 return (RCM_FAILURE
);
187 return (rcm_common(CMD_OFFLINE
, hd
, rsrcnames
, flag
, NULL
, infop
));
190 /* cancel offline request and allow apps to use rsrcname */
192 rcm_notify_online(rcm_handle_t
*hd
, char *rsrcname
, uint_t flag
,
197 rsrcnames
[0] = rsrcname
;
200 return (rcm_notify_online_list(hd
, rsrcnames
, flag
, infop
));
203 /* cancel offline and allow apps to use resources (list version) */
205 rcm_notify_online_list(rcm_handle_t
*hd
, char **rsrcnames
, uint_t flag
,
208 if (flag
& ~RCM_NOTIFY_MASK
) {
210 return (RCM_FAILURE
);
213 return (rcm_common(CMD_ONLINE
, hd
, rsrcnames
, flag
, NULL
, infop
));
216 /* notify that rsrcname has been removed */
218 rcm_notify_remove(rcm_handle_t
*hd
, char *rsrcname
, uint_t flag
,
223 rsrcnames
[0] = rsrcname
;
226 return (rcm_notify_remove_list(hd
, rsrcnames
, flag
, infop
));
229 /* notify that resrouces have been removed (list form) */
231 rcm_notify_remove_list(rcm_handle_t
*hd
, char **rsrcnames
, uint_t flag
,
234 if (flag
& ~RCM_NOTIFY_MASK
) {
236 return (RCM_FAILURE
);
239 return (rcm_common(CMD_REMOVE
, hd
, rsrcnames
, flag
, NULL
, infop
));
242 /* request for permission to suspend resource of interval time */
244 rcm_request_suspend(rcm_handle_t
*hd
, char *rsrcname
, uint_t flag
,
245 timespec_t
*interval
, rcm_info_t
**infop
)
249 rsrcnames
[0] = rsrcname
;
252 return (rcm_request_suspend_list(hd
, rsrcnames
, flag
, interval
, infop
));
255 /* request for permission to suspend resource of interval time (list form) */
257 rcm_request_suspend_list(rcm_handle_t
*hd
, char **rsrcnames
, uint_t flag
,
258 timespec_t
*interval
, rcm_info_t
**infop
)
260 if ((flag
& ~RCM_REQUEST_MASK
) || (interval
== NULL
) ||
261 (interval
->tv_sec
< 0) || (interval
->tv_nsec
< 0)) {
263 return (RCM_FAILURE
);
266 return (rcm_common(CMD_SUSPEND
, hd
, rsrcnames
, flag
, (void *)interval
,
270 /* notify apps of the completion of resource suspension */
272 rcm_notify_resume(rcm_handle_t
*hd
, char *rsrcname
, uint_t flag
,
277 rsrcnames
[0] = rsrcname
;
280 return (rcm_notify_resume_list(hd
, rsrcnames
, flag
, infop
));
283 /* notify apps of the completion of resource suspension (list form) */
285 rcm_notify_resume_list(rcm_handle_t
*hd
, char **rsrcnames
, uint_t flag
,
288 if (flag
& ~(RCM_NOTIFY_MASK
| RCM_SUSPENDED
)) {
290 return (RCM_FAILURE
);
293 return (rcm_common(CMD_RESUME
, hd
, rsrcnames
, flag
, NULL
, infop
));
296 /* request a capacity change from apps */
298 rcm_request_capacity_change(rcm_handle_t
*hd
, char *rsrcname
, uint_t flag
,
299 nvlist_t
*nvl
, rcm_info_t
**infop
)
304 if ((nvl
== NULL
) || (flag
& ~RCM_REQUEST_MASK
)) {
306 return (RCM_FAILURE
);
309 rsrcnames
[0] = rsrcname
;
312 rv
= rcm_common(CMD_REQUEST_CHANGE
, hd
, rsrcnames
, flag
, (void *)nvl
,
318 /* notify apps of a capacity change */
320 rcm_notify_capacity_change(rcm_handle_t
*hd
, char *rsrcname
, uint_t flag
,
321 nvlist_t
*nvl
, rcm_info_t
**infop
)
326 if ((nvl
== NULL
) || (flag
& ~RCM_REQUEST_MASK
)) {
328 return (RCM_FAILURE
);
331 rsrcnames
[0] = rsrcname
;
334 rv
= rcm_common(CMD_NOTIFY_CHANGE
, hd
, rsrcnames
, flag
, (void *)nvl
,
340 /* notify apps of an event */
342 rcm_notify_event(rcm_handle_t
*hd
, char *rsrcname
, uint_t flag
, nvlist_t
*nvl
,
348 /* No flags are defined yet for rcm_notify_event() */
349 if ((nvl
== NULL
) || (flag
!= 0)) {
351 return (RCM_FAILURE
);
354 rsrcnames
[0] = rsrcname
;
357 rv
= rcm_common(CMD_EVENT
, hd
, rsrcnames
, 0, (void *)nvl
, infop
);
363 * Register to receive capacity changes. This requires a module to exist in
364 * module directory. It should be called prior to using a new resource.
368 rcm_register_capacity(rcm_handle_t
*hd
, char *rsrcname
, uint_t flag
,
373 if (flag
& ~RCM_REGISTER_MASK
) {
375 return (RCM_FAILURE
);
378 flag
|= RCM_REGISTER_CAPACITY
;
380 rsrcnames
[0] = rsrcname
;
383 return (rcm_common(CMD_REGISTER
, hd
, rsrcnames
, flag
, NULL
, NULL
));
386 /* unregister interest in capacity changes */
388 rcm_unregister_capacity(rcm_handle_t
*hd
, char *rsrcname
, uint_t flag
)
392 if (flag
& ~RCM_REGISTER_MASK
) {
394 return (RCM_FAILURE
);
397 flag
|= RCM_REGISTER_CAPACITY
;
399 rsrcnames
[0] = rsrcname
;
402 return (rcm_common(CMD_UNREGISTER
, hd
, rsrcnames
, flag
, NULL
, NULL
));
406 * Register to receive events. This requires a module to exist in module
407 * directory. It should be called prior to using a new resource.
411 rcm_register_event(rcm_handle_t
*hd
, char *rsrcname
, uint_t flag
,
416 if (flag
& ~RCM_REGISTER_MASK
) {
418 return (RCM_FAILURE
);
421 flag
|= RCM_REGISTER_EVENT
;
423 rsrcnames
[0] = rsrcname
;
426 return (rcm_common(CMD_REGISTER
, hd
, rsrcnames
, flag
, NULL
, NULL
));
429 /* unregister interest in events */
431 rcm_unregister_event(rcm_handle_t
*hd
, char *rsrcname
, uint_t flag
)
435 if (flag
& ~RCM_REGISTER_MASK
) {
437 return (RCM_FAILURE
);
440 flag
|= RCM_REGISTER_EVENT
;
442 rsrcnames
[0] = rsrcname
;
445 return (rcm_common(CMD_UNREGISTER
, hd
, rsrcnames
, flag
, NULL
, NULL
));
449 * Register interest in a resource. This requires a module to exist in module
450 * directory. It should be called prior to using a new resource.
452 * Registration may be denied if it is presently locked by a DR operation.
456 rcm_register_interest(rcm_handle_t
*hd
, char *rsrcname
, uint_t flag
,
461 if (flag
& ~RCM_REGISTER_MASK
) {
463 return (RCM_FAILURE
);
466 flag
|= RCM_REGISTER_DR
;
468 rsrcnames
[0] = rsrcname
;
471 return (rcm_common(CMD_REGISTER
, hd
, rsrcnames
, flag
, NULL
, NULL
));
474 /* unregister interest in rsrcname */
476 rcm_unregister_interest(rcm_handle_t
*hd
, char *rsrcname
, uint_t flag
)
480 if (flag
& ~RCM_REGISTER_MASK
) {
482 return (RCM_FAILURE
);
485 flag
|= RCM_REGISTER_DR
;
487 rsrcnames
[0] = rsrcname
;
490 return (rcm_common(CMD_UNREGISTER
, hd
, rsrcnames
, flag
, NULL
, NULL
));
493 /* get the current state of a resource */
495 rcm_get_rsrcstate(rcm_handle_t
*hd
, char *rsrcname
, int *statep
)
499 rcm_info_t
*infop
= NULL
;
500 rcm_info_tuple_t
*tuple
= NULL
;
503 if (statep
== NULL
) {
505 return (RCM_FAILURE
);
508 rsrcnames
[0] = rsrcname
;
511 result
= rcm_common(CMD_GETSTATE
, hd
, rsrcnames
, flag
, NULL
, &infop
);
514 * A successful result implies the presence of exactly one RCM info
515 * tuple containing the state of this resource (a combination of each
516 * client's resources). If that's not true, change the result to
519 if (result
== RCM_SUCCESS
) {
520 if ((infop
== NULL
) ||
521 ((tuple
= rcm_info_next(infop
, NULL
)) == NULL
) ||
522 (rcm_info_next(infop
, tuple
) != NULL
)) {
523 result
= RCM_FAILURE
;
524 } else if (infop
&& tuple
) {
525 *statep
= rcm_info_state(tuple
);
530 rcm_free_info(infop
);
536 * RCM helper functions exposed to librcm callers.
539 /* Free linked list of registration info */
541 rcm_free_info(rcm_info_t
*info
)
544 rcm_info_t
*tmp
= info
->next
;
546 nvlist_free(info
->info
);
553 /* return the next tuple in the info structure */
555 rcm_info_next(rcm_info_t
*info
, rcm_info_tuple_t
*tuple
)
563 return ((rcm_info_tuple_t
*)info
);
565 return ((rcm_info_tuple_t
*)tuple
->next
);
568 /* return resource name */
570 rcm_info_rsrc(rcm_info_tuple_t
*tuple
)
572 char *rsrcname
= NULL
;
574 if (tuple
== NULL
|| tuple
->info
== NULL
) {
579 if (errno
= nvlist_lookup_string(tuple
->info
, RCM_RSRCNAME
, &rsrcname
))
586 rcm_info_info(rcm_info_tuple_t
*tuple
)
590 if (tuple
== NULL
|| tuple
->info
== NULL
) {
595 if (errno
= nvlist_lookup_string(tuple
->info
, RCM_CLIENT_INFO
, &info
))
602 rcm_info_error(rcm_info_tuple_t
*tuple
)
606 if (tuple
== NULL
|| tuple
->info
== NULL
) {
611 if (errno
= nvlist_lookup_string(tuple
->info
, RCM_CLIENT_ERROR
,
618 /* return info string in the tuple */
620 rcm_info_modname(rcm_info_tuple_t
*tuple
)
622 char *modname
= NULL
;
624 if (tuple
== NULL
|| tuple
->info
== NULL
) {
629 if (errno
= nvlist_lookup_string(tuple
->info
, RCM_CLIENT_MODNAME
,
636 /* return client pid in the tuple */
638 rcm_info_pid(rcm_info_tuple_t
*tuple
)
640 uint64_t pid64
= (uint64_t)0;
642 if (tuple
== NULL
|| tuple
->info
== NULL
) {
647 if (errno
= nvlist_lookup_uint64(tuple
->info
, RCM_CLIENT_ID
, &pid64
))
650 return ((pid_t
)pid64
);
653 /* return client state in the tuple */
655 rcm_info_state(rcm_info_tuple_t
*tuple
)
659 if (tuple
== NULL
|| tuple
->info
== NULL
) {
661 return (RCM_STATE_UNKNOWN
);
664 if (errno
= nvlist_lookup_int32(tuple
->info
, RCM_RSRCSTATE
, &state
))
665 return (RCM_STATE_UNKNOWN
);
670 /* return the generic properties in the tuple */
672 rcm_info_properties(rcm_info_tuple_t
*tuple
)
678 if (tuple
== NULL
|| tuple
->info
== NULL
) {
683 if (errno
= nvlist_lookup_byte_array(tuple
->info
, RCM_CLIENT_PROPERTIES
,
684 (uchar_t
**)&buf
, &buflen
))
687 if (errno
= nvlist_unpack(buf
, buflen
, &nvl
, 0)) {
696 * return operation sequence number
698 * This is private. Called by rcmctl only for testing purposes.
701 rcm_info_seqnum(rcm_info_tuple_t
*tuple
)
705 if (tuple
== NULL
|| tuple
->info
== NULL
) {
710 if (errno
= nvlist_lookup_int32(tuple
->info
, RCM_SEQ_NUM
, &seqnum
))
718 * The following interfaces are PRIVATE to the RCM framework. They are not
719 * declared static because they are called by rcm_daemon.
723 * Invoke shell to execute command in MT safe manner.
724 * Returns wait status or -1 on error.
727 rcm_exec_cmd(char *cmd
)
731 char *argvec
[] = {"sh", "-c", NULL
, NULL
};
734 if ((pid
= fork1()) == 0) {
735 (void) execv("/bin/sh", argvec
);
737 } else if (pid
== -1) {
742 w
= waitpid(pid
, &status
, 0);
743 } while (w
== -1 && errno
== EINTR
);
745 return ((w
== -1) ? w
: status
);
748 /* Append info at the very end */
750 rcm_append_info(rcm_info_t
**head
, rcm_info_t
*info
)
756 return (RCM_FAILURE
);
759 if ((tuple
= *head
) == NULL
) {
761 return (RCM_SUCCESS
);
764 while (tuple
->next
) {
768 return (RCM_SUCCESS
);
771 /* get rcm module and rcm script directory names */
773 #define N_MODULE_DIR 2
774 #define MODULE_DIR_HW "/usr/platform/lib/rcm/modules/"
775 #define MODULE_DIR_GEN "/usr/lib/rcm/modules/"
777 #define N_SCRIPT_DIR 3
778 #define SCRIPT_DIR_HW "/usr/platform/lib/rcm/scripts/"
779 #define SCRIPT_DIR_GEN "/usr/lib/rcm/scripts/"
780 #define SCRIPT_DIR_ETC "/etc/rcm/scripts/"
784 rcm_module_dir(uint_t dirnum
)
786 if (dirnum
< N_MODULE_DIR
)
787 return (rcm_dir(dirnum
, NULL
));
793 rcm_script_dir(uint_t dirnum
)
795 if (dirnum
< N_SCRIPT_DIR
)
796 return (rcm_dir(dirnum
+ N_MODULE_DIR
, NULL
));
802 rcm_dir(uint_t dirnum
, int *rcm_script
)
804 static char dir_name
[N_MODULE_DIR
+ N_SCRIPT_DIR
][MAXPATHLEN
];
806 if (dirnum
>= (N_MODULE_DIR
+ N_SCRIPT_DIR
))
809 if (dir_name
[0][0] == '\0') {
810 strlcpy(dir_name
[0], MODULE_DIR_HW
, MAXPATHLEN
);
811 strlcpy(dir_name
[N_MODULE_DIR
+ 1], SCRIPT_DIR_HW
, MAXPATHLEN
);
813 if (strlcpy(dir_name
[2], MODULE_DIR_GEN
, MAXPATHLEN
) >=
815 strlcpy(dir_name
[N_MODULE_DIR
+ 3], SCRIPT_DIR_GEN
,
816 MAXPATHLEN
) >= MAXPATHLEN
||
817 strlcpy(dir_name
[N_MODULE_DIR
+ 0], SCRIPT_DIR_ETC
,
818 MAXPATHLEN
) >= MAXPATHLEN
) {
820 "invalid module or script generic directory\n"));
826 *rcm_script
= (dirnum
< N_MODULE_DIR
) ? 0 : 1;
828 return (dir_name
[dirnum
]);
832 * Find the directory where the script is located.
833 * If the script is found return a pointer to the directory where the
834 * script was found otherwise return NULL.
837 rcm_get_script_dir(char *script_name
)
841 char path
[MAXPATHLEN
];
844 for (i
= 0; (dir_name
= rcm_script_dir(i
)) != NULL
; i
++) {
845 if (snprintf(path
, MAXPATHLEN
, "%s%s", dir_name
, script_name
)
847 dprintf((stderr
, "invalid script %s skipped\n",
851 if (stat(path
, &stats
) == 0)
859 * Returns 1 if the filename is an rcm script.
860 * Returns 0 if the filename is an rcm module.
863 rcm_is_script(char *filename
)
867 if (((tmp
= strstr(filename
, RCM_MODULE_SUFFIX
)) != NULL
) &&
868 (tmp
[strlen(RCM_MODULE_SUFFIX
)] == '\0'))
874 /* Locate the module and call dlopen */
876 rcm_module_open(char *modname
)
880 void *dlhandle
= NULL
;
881 char modpath
[MAXPATHLEN
];
890 for (i
= 0; (dir_name
= rcm_module_dir(i
)) != NULL
; i
++) {
891 if (snprintf(modpath
, MAXPATHLEN
, "%s%s", dir_name
, modname
)
893 dprintf((stderr
, "invalid module %s skipped\n",
898 if ((dlhandle
= dlopen(modpath
, RTLD_LAZY
)) != NULL
) {
902 dprintf((stderr
, "failure (dlopen=%s)\n", dlerror()));
904 if (stat(modpath
, &sbuf
) == 0) {
905 (void) fprintf(stderr
, "%s is not a valid module\n",
911 dprintf((stderr
, "module %s not found\n", modname
));
917 rcm_module_close(void *dlhandle
)
919 if (dlclose(dlhandle
) == 0)
922 dprintf((stderr
, "dlclose: %s\n", dlerror()));
927 * stub implementation of rcm_log_message allows dlopen of rcm modules
928 * to proceed in absence of rcm_daemon.
930 * This definition is interposed by the definition in rcm_daemon because of the
931 * default search order implemented by the linker and dlsym(). All RCM modules
932 * will see the daemon version when loaded by the rcm_daemon.
936 rcm_log_message(int level
, char *message
, ...)
938 dprintf((stderr
, "rcm_log_message stub\n"));
946 * Common routine for all rcm calls which require daemon processing
949 rcm_common(int cmd
, rcm_handle_t
*hd
, char **rsrcnames
, uint_t flag
, void *arg
,
956 return (RCM_FAILURE
);
961 return (RCM_FAILURE
);
964 if ((flag
& (RCM_DR_OPERATION
| RCM_MOD_INFO
)) == 0) {
965 if ((rsrcnames
== NULL
) || (rsrcnames
[0] == NULL
)) {
967 return (RCM_FAILURE
);
970 for (i
= 0; rsrcnames
[i
] != NULL
; i
++) {
971 if (*rsrcnames
[i
] == '\0') {
973 return (RCM_FAILURE
);
979 * Check if handle is allocated by rcm_daemon. If so, this call came
980 * from an RCM module, so we make a direct call into rcm_daemon.
982 if (hd
->lrcm_ops
!= NULL
) {
983 return (rcm_direct_call(cmd
, hd
, rsrcnames
, flag
, arg
, infop
));
987 * When not called from a RCM module (i.e. no recursion), zero the
988 * pointer just in case caller did not do so. For recursive calls,
989 * we want to append rcm_info_t after infop; zero it may cause
997 * Now call into the daemon.
999 return (rcm_daemon_call(cmd
, hd
, rsrcnames
, flag
, arg
, infop
));
1003 * Caller is an RCM module, call directly into rcm_daemon.
1006 rcm_direct_call(int cmd
, rcm_handle_t
*hd
, char **rsrcnames
, uint_t flag
,
1007 void *arg
, rcm_info_t
**infop
)
1011 librcm_ops_t
*ops
= (librcm_ops_t
*)hd
->lrcm_ops
;
1014 error
= ops
->librcm_getinfo(rsrcnames
, flag
, hd
->seq_num
,
1019 error
= ops
->librcm_offline(rsrcnames
, hd
->pid
, flag
,
1020 hd
->seq_num
, infop
);
1024 error
= ops
->librcm_online(rsrcnames
, hd
->pid
, flag
,
1025 hd
->seq_num
, infop
);
1029 error
= ops
->librcm_remove(rsrcnames
, hd
->pid
, flag
,
1030 hd
->seq_num
, infop
);
1034 error
= ops
->librcm_suspend(rsrcnames
, hd
->pid
, flag
,
1035 hd
->seq_num
, (timespec_t
*)arg
, infop
);
1039 error
= ops
->librcm_resume(rsrcnames
, hd
->pid
, flag
,
1040 hd
->seq_num
, infop
);
1044 error
= ops
->librcm_regis(hd
->modname
, rsrcnames
[0], hd
->pid
,
1048 case CMD_UNREGISTER
:
1049 error
= ops
->librcm_unregis(hd
->modname
, rsrcnames
[0], hd
->pid
,
1053 case CMD_REQUEST_CHANGE
:
1054 error
= ops
->librcm_request_change(rsrcnames
[0], hd
->pid
, flag
,
1055 hd
->seq_num
, (nvlist_t
*)arg
, infop
);
1058 case CMD_NOTIFY_CHANGE
:
1059 error
= ops
->librcm_notify_change(rsrcnames
[0], hd
->pid
, flag
,
1060 hd
->seq_num
, (nvlist_t
*)arg
, infop
);
1064 error
= ops
->librcm_notify_event(rsrcnames
[0], hd
->pid
, flag
,
1065 hd
->seq_num
, (nvlist_t
*)arg
, infop
);
1069 error
= ops
->librcm_getstate(rsrcnames
[0], hd
->pid
, infop
);
1073 dprintf((stderr
, "invalid command: %d\n", cmd
));
1079 error
= RCM_FAILURE
;
1085 * Call into rcm_daemon door to process the request
1088 rcm_daemon_call(int cmd
, rcm_handle_t
*hd
, char **rsrcnames
, uint_t flag
,
1089 void *arg
, rcm_info_t
**infop
)
1092 int daemon_errno
= 0;
1093 int error
= RCM_SUCCESS
;
1095 int maxdelay
= 10000; /* 10 seconds */
1096 char *nvl_packed
= NULL
;
1097 size_t nvl_size
= 0;
1098 nvlist_t
*ret
= NULL
;
1101 rcm_info_t
*info
= NULL
;
1106 * Decide whether to start the daemon
1116 case CMD_UNREGISTER
:
1118 case CMD_REQUEST_CHANGE
:
1119 case CMD_NOTIFY_CHANGE
:
1125 return (RCM_FAILURE
);
1128 if (rcm_daemon_is_alive() != 1) {
1129 dprintf((stderr
, "failed to start rcm_daemon\n"));
1131 return (RCM_FAILURE
);
1135 * Generate a packed nvlist for the request
1137 if (rcm_generate_nvlist(cmd
, hd
, rsrcnames
, flag
, arg
, &nvl_packed
,
1139 dprintf((stderr
, "error in nvlist generation\n"));
1141 return (RCM_FAILURE
);
1145 * Make the door call and get a return event. We go into a retry loop
1146 * when RCM_ET_EAGAIN is returned.
1149 if (get_event_service(RCM_SERVICE_DOOR
, (void *)nvl_packed
, nvl_size
,
1150 (void **)&ret
, &rsize
) < 0) {
1151 dprintf((stderr
, "rcm_daemon call failed: %s\n",
1154 return (RCM_FAILURE
);
1157 assert(ret
!= NULL
);
1160 * nvlist_lookup_* routines don't work because the returned nvlist
1161 * was nvlist_alloc'ed without the NV_UNIQUE_NAME flag. Implement
1162 * a sequential search manually, which is fine since there is only
1163 * one RCM_RESULT value in the nvlist.
1167 while (nvp
= nvlist_next_nvpair(ret
, nvp
)) {
1168 if (strcmp(nvpair_name(nvp
), RCM_RESULT
) == 0) {
1169 if (errno
= nvpair_value_int32(nvp
, &daemon_errno
)) {
1170 error
= RCM_FAILURE
;
1177 if (errno_found
== 0) {
1179 error
= RCM_FAILURE
;
1183 if (daemon_errno
== EAGAIN
) {
1187 dprintf((stderr
, "retry door_call\n"));
1189 if (delay
> maxdelay
) {
1191 error
= RCM_FAILURE
;
1195 (void) poll(NULL
, 0, delay
);
1196 delay
*= 2; /* exponential back off */
1202 * The door call succeeded. Now extract info from returned event.
1204 if (extract_info(ret
, &info
) != 0) {
1205 dprintf((stderr
, "error in extracting event data\n"));
1207 error
= RCM_FAILURE
;
1214 rcm_free_info(info
);
1217 if (daemon_errno
> 0) {
1218 errno
= daemon_errno
;
1219 error
= RCM_FAILURE
;
1221 error
= daemon_errno
;
1228 dprintf((stderr
, "daemon call is done. error = %d, errno = %s\n", error
,
1234 * Extract registration info from event data.
1235 * Return 0 on success and -1 on failure.
1238 extract_info(nvlist_t
*nvl
, rcm_info_t
**infop
)
1240 rcm_info_t
*info
= NULL
;
1241 rcm_info_t
*prev
= NULL
;
1242 rcm_info_t
*tmp
= NULL
;
1245 nvpair_t
*nvp
= NULL
;
1247 while (nvp
= nvlist_next_nvpair(nvl
, nvp
)) {
1252 if (strcmp(nvpair_name(nvp
), RCM_RESULT_INFO
) != 0)
1255 if ((tmp
= calloc(1, sizeof (*tmp
))) == NULL
) {
1256 dprintf((stderr
, "out of memory\n"));
1260 if (errno
= nvpair_value_byte_array(nvp
, (uchar_t
**)&buf
,
1263 dprintf((stderr
, "failed (nvpair_value=%s)\n",
1267 if (errno
= nvlist_unpack(buf
, buflen
, &(tmp
->info
), 0)) {
1269 dprintf((stderr
, "failed (nvlist_unpack=%s)\n",
1286 rcm_free_info(info
);
1291 /* Generate a packed nvlist for communicating with RCM daemon */
1293 rcm_generate_nvlist(int cmd
, rcm_handle_t
*hd
, char **rsrcnames
, uint_t flag
,
1294 void *arg
, char **nvl_packed
, size_t *nvl_size
)
1299 nvlist_t
*nvl
= NULL
;
1301 assert((nvl_packed
!= NULL
) && (nvl_size
!= NULL
));
1306 /* Allocate an empty nvlist */
1307 if ((errno
= nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0)) > 0) {
1308 dprintf((stderr
, "failed (nvlist_alloc=%s).\n",
1313 /* Stuff in all the arguments for the daemon call */
1314 if (nvlist_add_int32(nvl
, RCM_CMD
, cmd
) != 0) {
1315 dprintf((stderr
, "failed (nvlist_add(CMD)=%s).\n",
1321 while (rsrcnames
[nrsrcnames
] != NULL
)
1323 if (nvlist_add_string_array(nvl
, RCM_RSRCNAMES
, rsrcnames
,
1325 dprintf((stderr
, "failed (nvlist_add(RSRCNAMES)=%s).\n",
1331 if (nvlist_add_string(nvl
, RCM_CLIENT_MODNAME
, hd
->modname
)
1334 "failed (nvlist_add(CLIENT_MODNAME)=%s).\n",
1340 if (nvlist_add_uint64(nvl
, RCM_CLIENT_ID
, hd
->pid
) != 0) {
1341 dprintf((stderr
, "failed (nvlist_add(CLIENT_ID)=%s).\n",
1347 if (nvlist_add_uint32(nvl
, RCM_REQUEST_FLAG
, flag
) != 0) {
1349 "failed (nvlist_add(REQUEST_FLAG)=%s).\n",
1354 if (arg
&& cmd
== CMD_SUSPEND
) {
1355 if (nvlist_add_byte_array(nvl
, RCM_SUSPEND_INTERVAL
,
1356 (uchar_t
*)arg
, sizeof (timespec_t
)) != 0) {
1358 "failed (nvlist_add(SUSPEND_INTERVAL)=%s).\n",
1364 ((cmd
== CMD_REQUEST_CHANGE
) || (cmd
== CMD_NOTIFY_CHANGE
))) {
1365 if (errno
= nvlist_pack(arg
, &buf
, &buflen
, NV_ENCODE_NATIVE
,
1368 "failed (nvlist_pack(CHANGE_DATA)=%s).\n",
1372 if (nvlist_add_byte_array(nvl
, RCM_CHANGE_DATA
, (uchar_t
*)buf
,
1375 "failed (nvlist_add(CHANGE_DATA)=%s).\n",
1380 if (arg
&& cmd
== CMD_EVENT
) {
1381 if (errno
= nvlist_pack(arg
, &buf
, &buflen
, NV_ENCODE_NATIVE
,
1384 "failed (nvlist_pack(CHANGE_DATA)=%s).\n",
1388 if (nvlist_add_byte_array(nvl
, RCM_EVENT_DATA
, (uchar_t
*)buf
,
1391 "failed (nvlist_add(EVENT_DATA)=%s).\n",
1397 /* Pack the nvlist */
1398 if (errno
= nvlist_pack(nvl
, nvl_packed
, nvl_size
, NV_ENCODE_NATIVE
,
1400 dprintf((stderr
, "failed (nvlist_pack=%s).\n",
1405 /* If an argument was packed intermediately, free the buffer */
1408 /* Free the unpacked version of the nvlist and return the packed list */
1421 /* check if rcm_daemon is up and running */
1423 rcm_daemon_is_alive()
1431 const int maxdelay
= 10000; /* 10 sec */
1433 /* generate a packed nvlist for the door knocking */
1434 if (errno
= nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0)) {
1435 dprintf((stderr
, "nvlist_alloc failed: %s\n", strerror(errno
)));
1438 if (errno
= nvlist_add_int32(nvl
, RCM_CMD
, CMD_KNOCK
)) {
1439 dprintf((stderr
, "nvlist_add failed: %s\n", strerror(errno
)));
1443 if (errno
= nvlist_pack(nvl
, &buf
, &buflen
, NV_ENCODE_NATIVE
, 0)) {
1444 dprintf((stderr
, "nvlist_pack failed: %s\n", strerror(errno
)));
1451 * check the door and knock on it
1453 if ((stat(RCM_SERVICE_DOOR
, &st
) == 0) &&
1454 (get_event_service(RCM_SERVICE_DOOR
, (void *)buf
, buflen
, NULL
,
1457 return (1); /* daemon is alive */
1461 * Attempt to start the daemon.
1462 * If caller has SIGCHLD set to SIG_IGN or its SA_NOCLDWAIT
1463 * flag set, waitpid(2) (hence rcm_exec_cmd) will fail.
1464 * get_event_service will determine if the rcm_daemon started.
1466 dprintf((stderr
, "exec: %s\n", RCM_DAEMON_START
));
1467 (void) rcm_exec_cmd(RCM_DAEMON_START
);
1470 * Wait for daemon to respond, timeout at 10 sec
1472 while (((lasttry
= get_event_service(RCM_SERVICE_DOOR
, (void *)buf
,
1473 buflen
, NULL
, NULL
)) != 0) &&
1474 ((errno
== EBADF
) || (errno
== ESRCH
))) {
1475 if (delay
> maxdelay
) {
1478 (void) poll(NULL
, 0, delay
);
1491 * The policy is root only for now. Need to relax this when interface level
1495 rcm_check_permission(void)
1497 return (getuid() == 0);
1501 * Project private function - for use by RCM MSTC tests
1503 * Get the client name (rcm module name or script name) corresponding to
1504 * the given rcm handle.
1507 rcm_get_client_name(rcm_handle_t
*hd
)
1509 return (hd
->modname
);