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 3 /* search 3 directories for modules */
774 #define MODULE_DIR_HW "/usr/platform/%s/lib/rcm/modules/"
775 #define MODULE_DIR_GEN "/usr/lib/rcm/modules/"
777 #define N_SCRIPT_DIR 4 /* search 4 directories for scripts */
778 #define SCRIPT_DIR_HW "/usr/platform/%s/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 char infobuf
[MAXPATHLEN
];
808 if (dirnum
>= (N_MODULE_DIR
+ N_SCRIPT_DIR
))
811 if (dir_name
[0][0] == '\0') {
813 * construct the module directory names
815 if (sysinfo(SI_PLATFORM
, infobuf
, MAXPATHLEN
) == -1) {
816 dprintf((stderr
, "sysinfo %s\n", strerror(errno
)));
819 if (snprintf(dir_name
[0], MAXPATHLEN
, MODULE_DIR_HW
,
820 infobuf
) >= MAXPATHLEN
||
821 snprintf(dir_name
[N_MODULE_DIR
+ 1], MAXPATHLEN
,
822 SCRIPT_DIR_HW
, infobuf
) >= MAXPATHLEN
) {
824 "invalid module or script directory for "
825 "platform %s\n", infobuf
));
830 if (sysinfo(SI_MACHINE
, infobuf
, MAXPATHLEN
) == -1) {
831 dprintf((stderr
, "sysinfo %s\n", strerror(errno
)));
834 if (snprintf(dir_name
[1], MAXPATHLEN
, MODULE_DIR_HW
,
835 infobuf
) >= MAXPATHLEN
||
836 snprintf(dir_name
[N_MODULE_DIR
+ 2], MAXPATHLEN
,
837 SCRIPT_DIR_HW
, infobuf
) >= MAXPATHLEN
) {
839 "invalid module or script directory for "
840 "machine type %s\n", infobuf
));
845 if (strlcpy(dir_name
[2], MODULE_DIR_GEN
, MAXPATHLEN
) >=
847 strlcpy(dir_name
[N_MODULE_DIR
+ 3], SCRIPT_DIR_GEN
,
848 MAXPATHLEN
) >= MAXPATHLEN
||
849 strlcpy(dir_name
[N_MODULE_DIR
+ 0], SCRIPT_DIR_ETC
,
850 MAXPATHLEN
) >= MAXPATHLEN
) {
852 "invalid module or script generic directory\n"));
858 *rcm_script
= (dirnum
< N_MODULE_DIR
) ? 0 : 1;
860 return (dir_name
[dirnum
]);
864 * Find the directory where the script is located.
865 * If the script is found return a pointer to the directory where the
866 * script was found otherwise return NULL.
869 rcm_get_script_dir(char *script_name
)
873 char path
[MAXPATHLEN
];
876 for (i
= 0; (dir_name
= rcm_script_dir(i
)) != NULL
; i
++) {
877 if (snprintf(path
, MAXPATHLEN
, "%s%s", dir_name
, script_name
)
879 dprintf((stderr
, "invalid script %s skipped\n",
883 if (stat(path
, &stats
) == 0)
891 * Returns 1 if the filename is an rcm script.
892 * Returns 0 if the filename is an rcm module.
895 rcm_is_script(char *filename
)
899 if (((tmp
= strstr(filename
, RCM_MODULE_SUFFIX
)) != NULL
) &&
900 (tmp
[strlen(RCM_MODULE_SUFFIX
)] == '\0'))
906 /* Locate the module and call dlopen */
908 rcm_module_open(char *modname
)
912 void *dlhandle
= NULL
;
913 char modpath
[MAXPATHLEN
];
922 for (i
= 0; (dir_name
= rcm_module_dir(i
)) != NULL
; i
++) {
923 if (snprintf(modpath
, MAXPATHLEN
, "%s%s", dir_name
, modname
)
925 dprintf((stderr
, "invalid module %s skipped\n",
930 if ((dlhandle
= dlopen(modpath
, RTLD_LAZY
)) != NULL
) {
934 dprintf((stderr
, "failure (dlopen=%s)\n", dlerror()));
936 if (stat(modpath
, &sbuf
) == 0) {
937 (void) fprintf(stderr
, "%s is not a valid module\n",
943 dprintf((stderr
, "module %s not found\n", modname
));
949 rcm_module_close(void *dlhandle
)
951 if (dlclose(dlhandle
) == 0)
954 dprintf((stderr
, "dlclose: %s\n", dlerror()));
959 * stub implementation of rcm_log_message allows dlopen of rcm modules
960 * to proceed in absence of rcm_daemon.
962 * This definition is interposed by the definition in rcm_daemon because of the
963 * default search order implemented by the linker and dlsym(). All RCM modules
964 * will see the daemon version when loaded by the rcm_daemon.
968 rcm_log_message(int level
, char *message
, ...)
970 dprintf((stderr
, "rcm_log_message stub\n"));
978 * Common routine for all rcm calls which require daemon processing
981 rcm_common(int cmd
, rcm_handle_t
*hd
, char **rsrcnames
, uint_t flag
, void *arg
,
988 return (RCM_FAILURE
);
993 return (RCM_FAILURE
);
996 if ((flag
& (RCM_DR_OPERATION
| RCM_MOD_INFO
)) == 0) {
997 if ((rsrcnames
== NULL
) || (rsrcnames
[0] == NULL
)) {
999 return (RCM_FAILURE
);
1002 for (i
= 0; rsrcnames
[i
] != NULL
; i
++) {
1003 if (*rsrcnames
[i
] == '\0') {
1005 return (RCM_FAILURE
);
1011 * Check if handle is allocated by rcm_daemon. If so, this call came
1012 * from an RCM module, so we make a direct call into rcm_daemon.
1014 if (hd
->lrcm_ops
!= NULL
) {
1015 return (rcm_direct_call(cmd
, hd
, rsrcnames
, flag
, arg
, infop
));
1019 * When not called from a RCM module (i.e. no recursion), zero the
1020 * pointer just in case caller did not do so. For recursive calls,
1021 * we want to append rcm_info_t after infop; zero it may cause
1029 * Now call into the daemon.
1031 return (rcm_daemon_call(cmd
, hd
, rsrcnames
, flag
, arg
, infop
));
1035 * Caller is an RCM module, call directly into rcm_daemon.
1038 rcm_direct_call(int cmd
, rcm_handle_t
*hd
, char **rsrcnames
, uint_t flag
,
1039 void *arg
, rcm_info_t
**infop
)
1043 librcm_ops_t
*ops
= (librcm_ops_t
*)hd
->lrcm_ops
;
1046 error
= ops
->librcm_getinfo(rsrcnames
, flag
, hd
->seq_num
,
1051 error
= ops
->librcm_offline(rsrcnames
, hd
->pid
, flag
,
1052 hd
->seq_num
, infop
);
1056 error
= ops
->librcm_online(rsrcnames
, hd
->pid
, flag
,
1057 hd
->seq_num
, infop
);
1061 error
= ops
->librcm_remove(rsrcnames
, hd
->pid
, flag
,
1062 hd
->seq_num
, infop
);
1066 error
= ops
->librcm_suspend(rsrcnames
, hd
->pid
, flag
,
1067 hd
->seq_num
, (timespec_t
*)arg
, infop
);
1071 error
= ops
->librcm_resume(rsrcnames
, hd
->pid
, flag
,
1072 hd
->seq_num
, infop
);
1076 error
= ops
->librcm_regis(hd
->modname
, rsrcnames
[0], hd
->pid
,
1080 case CMD_UNREGISTER
:
1081 error
= ops
->librcm_unregis(hd
->modname
, rsrcnames
[0], hd
->pid
,
1085 case CMD_REQUEST_CHANGE
:
1086 error
= ops
->librcm_request_change(rsrcnames
[0], hd
->pid
, flag
,
1087 hd
->seq_num
, (nvlist_t
*)arg
, infop
);
1090 case CMD_NOTIFY_CHANGE
:
1091 error
= ops
->librcm_notify_change(rsrcnames
[0], hd
->pid
, flag
,
1092 hd
->seq_num
, (nvlist_t
*)arg
, infop
);
1096 error
= ops
->librcm_notify_event(rsrcnames
[0], hd
->pid
, flag
,
1097 hd
->seq_num
, (nvlist_t
*)arg
, infop
);
1101 error
= ops
->librcm_getstate(rsrcnames
[0], hd
->pid
, infop
);
1105 dprintf((stderr
, "invalid command: %d\n", cmd
));
1111 error
= RCM_FAILURE
;
1117 * Call into rcm_daemon door to process the request
1120 rcm_daemon_call(int cmd
, rcm_handle_t
*hd
, char **rsrcnames
, uint_t flag
,
1121 void *arg
, rcm_info_t
**infop
)
1124 int daemon_errno
= 0;
1125 int error
= RCM_SUCCESS
;
1127 int maxdelay
= 10000; /* 10 seconds */
1128 char *nvl_packed
= NULL
;
1129 size_t nvl_size
= 0;
1130 nvlist_t
*ret
= NULL
;
1133 rcm_info_t
*info
= NULL
;
1138 * Decide whether to start the daemon
1148 case CMD_UNREGISTER
:
1150 case CMD_REQUEST_CHANGE
:
1151 case CMD_NOTIFY_CHANGE
:
1157 return (RCM_FAILURE
);
1160 if (rcm_daemon_is_alive() != 1) {
1161 dprintf((stderr
, "failed to start rcm_daemon\n"));
1163 return (RCM_FAILURE
);
1167 * Generate a packed nvlist for the request
1169 if (rcm_generate_nvlist(cmd
, hd
, rsrcnames
, flag
, arg
, &nvl_packed
,
1171 dprintf((stderr
, "error in nvlist generation\n"));
1173 return (RCM_FAILURE
);
1177 * Make the door call and get a return event. We go into a retry loop
1178 * when RCM_ET_EAGAIN is returned.
1181 if (get_event_service(RCM_SERVICE_DOOR
, (void *)nvl_packed
, nvl_size
,
1182 (void **)&ret
, &rsize
) < 0) {
1183 dprintf((stderr
, "rcm_daemon call failed: %s\n",
1186 return (RCM_FAILURE
);
1189 assert(ret
!= NULL
);
1192 * nvlist_lookup_* routines don't work because the returned nvlist
1193 * was nvlist_alloc'ed without the NV_UNIQUE_NAME flag. Implement
1194 * a sequential search manually, which is fine since there is only
1195 * one RCM_RESULT value in the nvlist.
1199 while (nvp
= nvlist_next_nvpair(ret
, nvp
)) {
1200 if (strcmp(nvpair_name(nvp
), RCM_RESULT
) == 0) {
1201 if (errno
= nvpair_value_int32(nvp
, &daemon_errno
)) {
1202 error
= RCM_FAILURE
;
1209 if (errno_found
== 0) {
1211 error
= RCM_FAILURE
;
1215 if (daemon_errno
== EAGAIN
) {
1219 dprintf((stderr
, "retry door_call\n"));
1221 if (delay
> maxdelay
) {
1223 error
= RCM_FAILURE
;
1227 (void) poll(NULL
, 0, delay
);
1228 delay
*= 2; /* exponential back off */
1234 * The door call succeeded. Now extract info from returned event.
1236 if (extract_info(ret
, &info
) != 0) {
1237 dprintf((stderr
, "error in extracting event data\n"));
1239 error
= RCM_FAILURE
;
1246 rcm_free_info(info
);
1249 if (daemon_errno
> 0) {
1250 errno
= daemon_errno
;
1251 error
= RCM_FAILURE
;
1253 error
= daemon_errno
;
1261 dprintf((stderr
, "daemon call is done. error = %d, errno = %s\n", error
,
1267 * Extract registration info from event data.
1268 * Return 0 on success and -1 on failure.
1271 extract_info(nvlist_t
*nvl
, rcm_info_t
**infop
)
1273 rcm_info_t
*info
= NULL
;
1274 rcm_info_t
*prev
= NULL
;
1275 rcm_info_t
*tmp
= NULL
;
1278 nvpair_t
*nvp
= NULL
;
1280 while (nvp
= nvlist_next_nvpair(nvl
, nvp
)) {
1285 if (strcmp(nvpair_name(nvp
), RCM_RESULT_INFO
) != 0)
1288 if ((tmp
= calloc(1, sizeof (*tmp
))) == NULL
) {
1289 dprintf((stderr
, "out of memory\n"));
1293 if (errno
= nvpair_value_byte_array(nvp
, (uchar_t
**)&buf
,
1296 dprintf((stderr
, "failed (nvpair_value=%s)\n",
1300 if (errno
= nvlist_unpack(buf
, buflen
, &(tmp
->info
), 0)) {
1302 dprintf((stderr
, "failed (nvlist_unpack=%s)\n",
1319 rcm_free_info(info
);
1324 /* Generate a packed nvlist for communicating with RCM daemon */
1326 rcm_generate_nvlist(int cmd
, rcm_handle_t
*hd
, char **rsrcnames
, uint_t flag
,
1327 void *arg
, char **nvl_packed
, size_t *nvl_size
)
1332 nvlist_t
*nvl
= NULL
;
1334 assert((nvl_packed
!= NULL
) && (nvl_size
!= NULL
));
1339 /* Allocate an empty nvlist */
1340 if ((errno
= nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0)) > 0) {
1341 dprintf((stderr
, "failed (nvlist_alloc=%s).\n",
1346 /* Stuff in all the arguments for the daemon call */
1347 if (nvlist_add_int32(nvl
, RCM_CMD
, cmd
) != 0) {
1348 dprintf((stderr
, "failed (nvlist_add(CMD)=%s).\n",
1354 while (rsrcnames
[nrsrcnames
] != NULL
)
1356 if (nvlist_add_string_array(nvl
, RCM_RSRCNAMES
, rsrcnames
,
1358 dprintf((stderr
, "failed (nvlist_add(RSRCNAMES)=%s).\n",
1364 if (nvlist_add_string(nvl
, RCM_CLIENT_MODNAME
, hd
->modname
)
1367 "failed (nvlist_add(CLIENT_MODNAME)=%s).\n",
1373 if (nvlist_add_uint64(nvl
, RCM_CLIENT_ID
, hd
->pid
) != 0) {
1374 dprintf((stderr
, "failed (nvlist_add(CLIENT_ID)=%s).\n",
1380 if (nvlist_add_uint32(nvl
, RCM_REQUEST_FLAG
, flag
) != 0) {
1382 "failed (nvlist_add(REQUEST_FLAG)=%s).\n",
1387 if (arg
&& cmd
== CMD_SUSPEND
) {
1388 if (nvlist_add_byte_array(nvl
, RCM_SUSPEND_INTERVAL
,
1389 (uchar_t
*)arg
, sizeof (timespec_t
)) != 0) {
1391 "failed (nvlist_add(SUSPEND_INTERVAL)=%s).\n",
1397 ((cmd
== CMD_REQUEST_CHANGE
) || (cmd
== CMD_NOTIFY_CHANGE
))) {
1398 if (errno
= nvlist_pack(arg
, &buf
, &buflen
, NV_ENCODE_NATIVE
,
1401 "failed (nvlist_pack(CHANGE_DATA)=%s).\n",
1405 if (nvlist_add_byte_array(nvl
, RCM_CHANGE_DATA
, (uchar_t
*)buf
,
1408 "failed (nvlist_add(CHANGE_DATA)=%s).\n",
1413 if (arg
&& cmd
== CMD_EVENT
) {
1414 if (errno
= nvlist_pack(arg
, &buf
, &buflen
, NV_ENCODE_NATIVE
,
1417 "failed (nvlist_pack(CHANGE_DATA)=%s).\n",
1421 if (nvlist_add_byte_array(nvl
, RCM_EVENT_DATA
, (uchar_t
*)buf
,
1424 "failed (nvlist_add(EVENT_DATA)=%s).\n",
1430 /* Pack the nvlist */
1431 if (errno
= nvlist_pack(nvl
, nvl_packed
, nvl_size
, NV_ENCODE_NATIVE
,
1433 dprintf((stderr
, "failed (nvlist_pack=%s).\n",
1438 /* If an argument was packed intermediately, free the buffer */
1442 /* Free the unpacked version of the nvlist and return the packed list */
1457 /* check if rcm_daemon is up and running */
1459 rcm_daemon_is_alive()
1467 const int maxdelay
= 10000; /* 10 sec */
1469 /* generate a packed nvlist for the door knocking */
1470 if (errno
= nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0)) {
1471 dprintf((stderr
, "nvlist_alloc failed: %s\n", strerror(errno
)));
1474 if (errno
= nvlist_add_int32(nvl
, RCM_CMD
, CMD_KNOCK
)) {
1475 dprintf((stderr
, "nvlist_add failed: %s\n", strerror(errno
)));
1479 if (errno
= nvlist_pack(nvl
, &buf
, &buflen
, NV_ENCODE_NATIVE
, 0)) {
1480 dprintf((stderr
, "nvlist_pack failed: %s\n", strerror(errno
)));
1487 * check the door and knock on it
1489 if ((stat(RCM_SERVICE_DOOR
, &st
) == 0) &&
1490 (get_event_service(RCM_SERVICE_DOOR
, (void *)buf
, buflen
, NULL
,
1493 return (1); /* daemon is alive */
1497 * Attempt to start the daemon.
1498 * If caller has SIGCHLD set to SIG_IGN or its SA_NOCLDWAIT
1499 * flag set, waitpid(2) (hence rcm_exec_cmd) will fail.
1500 * get_event_service will determine if the rcm_daemon started.
1502 dprintf((stderr
, "exec: %s\n", RCM_DAEMON_START
));
1503 (void) rcm_exec_cmd(RCM_DAEMON_START
);
1506 * Wait for daemon to respond, timeout at 10 sec
1508 while (((lasttry
= get_event_service(RCM_SERVICE_DOOR
, (void *)buf
,
1509 buflen
, NULL
, NULL
)) != 0) &&
1510 ((errno
== EBADF
) || (errno
== ESRCH
))) {
1511 if (delay
> maxdelay
) {
1514 (void) poll(NULL
, 0, delay
);
1527 * The policy is root only for now. Need to relax this when interface level
1531 rcm_check_permission(void)
1533 return (getuid() == 0);
1537 * Project private function - for use by RCM MSTC tests
1539 * Get the client name (rcm module name or script name) corresponding to
1540 * the given rcm handle.
1543 rcm_get_client_name(rcm_handle_t
*hd
)
1545 return (hd
->modname
);