Merge illumos-gate
[unleashed.git] / usr / src / lib / librcm / librcm.c
blobf26b466192c75daa650e95b6fbb5dd70e938d90d
1 /*
2 * CDDL HEADER START
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]
19 * CDDL HEADER END
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"
29 #ifdef DEBUG
30 static int rcm_debug = 1;
31 #define dprintf(args) if (rcm_debug) (void) fprintf args
32 #else
33 #define dprintf(args) /* nothing */
34 #endif /* DEBUG */
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 *,
39 rcm_info_t **);
40 static int rcm_direct_call(int, rcm_handle_t *, char **, uint_t, void *,
41 rcm_info_t **);
42 static int rcm_daemon_call(int, rcm_handle_t *, char **, uint_t, void *,
43 rcm_info_t **);
44 static int rcm_generate_nvlist(int, rcm_handle_t *, char **, uint_t, void *,
45 char **, size_t *);
46 static int rcm_check_permission(void);
49 * Allocate a handle structure
51 /*ARGSUSED2*/
52 int
53 rcm_alloc_handle(char *modname, uint_t flag, void *arg, rcm_handle_t **hdp)
55 rcm_handle_t *hd;
56 void *temp;
57 char namebuf[MAXPATHLEN];
59 if ((hdp == NULL) || (flag & ~RCM_ALLOC_HDL_MASK)) {
60 errno = EINVAL;
61 return (RCM_FAILURE);
64 if (rcm_check_permission() == 0) {
65 errno = EPERM;
66 return (RCM_FAILURE);
69 if ((hd = calloc(1, sizeof (*hd))) == NULL) {
70 return (RCM_FAILURE);
73 if (modname) {
74 (void) snprintf(namebuf, MAXPATHLEN, "%s%s", modname,
75 RCM_MODULE_SUFFIX);
77 if ((hd->modname = strdup(namebuf)) == NULL) {
78 free(hd);
79 return (RCM_FAILURE);
82 if ((temp = rcm_module_open(namebuf)) == NULL) {
83 free(hd->modname);
84 free(hd);
85 errno = EINVAL;
86 return (RCM_FAILURE);
89 rcm_module_close(temp);
92 if (flag & RCM_NOPID) {
93 hd->pid = (pid_t)0;
94 } else {
95 hd->pid = (pid_t)getpid();
98 *hdp = hd;
99 return (RCM_SUCCESS);
102 /* free handle structure */
104 rcm_free_handle(rcm_handle_t *hd)
106 if (hd == NULL) {
107 errno = EINVAL;
108 return (RCM_FAILURE);
111 if (hd->modname) {
112 free(hd->modname);
115 free(hd);
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)
128 char *rsrcnames[2];
130 if ((flag & ~RCM_GET_INFO_MASK) || (infop == NULL)) {
131 errno = EINVAL;
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) {
140 errno = EINVAL;
141 return (RCM_FAILURE);
144 rsrcnames[0] = rsrcname;
145 rsrcnames[1] = NULL;
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,
153 rcm_info_t **infop)
155 /* Requesting the current DR operations with a *list() is invalid */
156 if ((flag & RCM_DR_OPERATION) || (flag & RCM_MOD_INFO)) {
157 errno = EINVAL;
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,
167 rcm_info_t **infop)
169 char *rsrcnames[2];
171 rsrcnames[0] = rsrcname;
172 rsrcnames[1] = NULL;
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,
180 rcm_info_t **infop)
182 if (flag & ~RCM_REQUEST_MASK) {
183 errno = EINVAL;
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,
193 rcm_info_t **infop)
195 char *rsrcnames[2];
197 rsrcnames[0] = rsrcname;
198 rsrcnames[1] = NULL;
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,
206 rcm_info_t **infop)
208 if (flag & ~RCM_NOTIFY_MASK) {
209 errno = EINVAL;
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,
219 rcm_info_t **infop)
221 char *rsrcnames[2];
223 rsrcnames[0] = rsrcname;
224 rsrcnames[1] = NULL;
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,
232 rcm_info_t **infop)
234 if (flag & ~RCM_NOTIFY_MASK) {
235 errno = EINVAL;
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)
247 char *rsrcnames[2];
249 rsrcnames[0] = rsrcname;
250 rsrcnames[1] = NULL;
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)) {
262 errno = EINVAL;
263 return (RCM_FAILURE);
266 return (rcm_common(CMD_SUSPEND, hd, rsrcnames, flag, (void *)interval,
267 infop));
270 /* notify apps of the completion of resource suspension */
272 rcm_notify_resume(rcm_handle_t *hd, char *rsrcname, uint_t flag,
273 rcm_info_t **infop)
275 char *rsrcnames[2];
277 rsrcnames[0] = rsrcname;
278 rsrcnames[1] = NULL;
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,
286 rcm_info_t **infop)
288 if (flag & ~(RCM_NOTIFY_MASK | RCM_SUSPENDED)) {
289 errno = EINVAL;
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)
301 int rv;
302 char *rsrcnames[2];
304 if ((nvl == NULL) || (flag & ~RCM_REQUEST_MASK)) {
305 errno = EINVAL;
306 return (RCM_FAILURE);
309 rsrcnames[0] = rsrcname;
310 rsrcnames[1] = NULL;
312 rv = rcm_common(CMD_REQUEST_CHANGE, hd, rsrcnames, flag, (void *)nvl,
313 infop);
315 return (rv);
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)
323 int rv;
324 char *rsrcnames[2];
326 if ((nvl == NULL) || (flag & ~RCM_REQUEST_MASK)) {
327 errno = EINVAL;
328 return (RCM_FAILURE);
331 rsrcnames[0] = rsrcname;
332 rsrcnames[1] = NULL;
334 rv = rcm_common(CMD_NOTIFY_CHANGE, hd, rsrcnames, flag, (void *)nvl,
335 infop);
337 return (rv);
340 /* notify apps of an event */
342 rcm_notify_event(rcm_handle_t *hd, char *rsrcname, uint_t flag, nvlist_t *nvl,
343 rcm_info_t **infop)
345 int rv;
346 char *rsrcnames[2];
348 /* No flags are defined yet for rcm_notify_event() */
349 if ((nvl == NULL) || (flag != 0)) {
350 errno = EINVAL;
351 return (RCM_FAILURE);
354 rsrcnames[0] = rsrcname;
355 rsrcnames[1] = NULL;
357 rv = rcm_common(CMD_EVENT, hd, rsrcnames, 0, (void *)nvl, infop);
359 return (rv);
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.
366 /* ARGSUSED */
368 rcm_register_capacity(rcm_handle_t *hd, char *rsrcname, uint_t flag,
369 rcm_info_t **infop)
371 char *rsrcnames[2];
373 if (flag & ~RCM_REGISTER_MASK) {
374 errno = EINVAL;
375 return (RCM_FAILURE);
378 flag |= RCM_REGISTER_CAPACITY;
380 rsrcnames[0] = rsrcname;
381 rsrcnames[1] = NULL;
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)
390 char *rsrcnames[2];
392 if (flag & ~RCM_REGISTER_MASK) {
393 errno = EINVAL;
394 return (RCM_FAILURE);
397 flag |= RCM_REGISTER_CAPACITY;
399 rsrcnames[0] = rsrcname;
400 rsrcnames[1] = NULL;
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.
409 /* ARGSUSED */
411 rcm_register_event(rcm_handle_t *hd, char *rsrcname, uint_t flag,
412 rcm_info_t **infop)
414 char *rsrcnames[2];
416 if (flag & ~RCM_REGISTER_MASK) {
417 errno = EINVAL;
418 return (RCM_FAILURE);
421 flag |= RCM_REGISTER_EVENT;
423 rsrcnames[0] = rsrcname;
424 rsrcnames[1] = NULL;
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)
433 char *rsrcnames[2];
435 if (flag & ~RCM_REGISTER_MASK) {
436 errno = EINVAL;
437 return (RCM_FAILURE);
440 flag |= RCM_REGISTER_EVENT;
442 rsrcnames[0] = rsrcname;
443 rsrcnames[1] = NULL;
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.
454 /* ARGSUSED */
456 rcm_register_interest(rcm_handle_t *hd, char *rsrcname, uint_t flag,
457 rcm_info_t **infop)
459 char *rsrcnames[2];
461 if (flag & ~RCM_REGISTER_MASK) {
462 errno = EINVAL;
463 return (RCM_FAILURE);
466 flag |= RCM_REGISTER_DR;
468 rsrcnames[0] = rsrcname;
469 rsrcnames[1] = NULL;
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)
478 char *rsrcnames[2];
480 if (flag & ~RCM_REGISTER_MASK) {
481 errno = EINVAL;
482 return (RCM_FAILURE);
485 flag |= RCM_REGISTER_DR;
487 rsrcnames[0] = rsrcname;
488 rsrcnames[1] = NULL;
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)
497 int result;
498 int flag = 0;
499 rcm_info_t *infop = NULL;
500 rcm_info_tuple_t *tuple = NULL;
501 char *rsrcnames[2];
503 if (statep == NULL) {
504 errno = EINVAL;
505 return (RCM_FAILURE);
508 rsrcnames[0] = rsrcname;
509 rsrcnames[1] = NULL;
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
517 * RCM_FAILURE.
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);
529 if (infop)
530 rcm_free_info(infop);
532 return (result);
536 * RCM helper functions exposed to librcm callers.
539 /* Free linked list of registration info */
540 void
541 rcm_free_info(rcm_info_t *info)
543 while (info) {
544 rcm_info_t *tmp = info->next;
546 nvlist_free(info->info);
547 free(info);
549 info = tmp;
553 /* return the next tuple in the info structure */
554 rcm_info_tuple_t *
555 rcm_info_next(rcm_info_t *info, rcm_info_tuple_t *tuple)
557 if (info == NULL) {
558 errno = EINVAL;
559 return (NULL);
562 if (tuple == NULL) {
563 return ((rcm_info_tuple_t *)info);
565 return ((rcm_info_tuple_t *)tuple->next);
568 /* return resource name */
569 const char *
570 rcm_info_rsrc(rcm_info_tuple_t *tuple)
572 char *rsrcname = NULL;
574 if (tuple == NULL || tuple->info == NULL) {
575 errno = EINVAL;
576 return (NULL);
579 if (errno = nvlist_lookup_string(tuple->info, RCM_RSRCNAME, &rsrcname))
580 return (NULL);
582 return (rsrcname);
585 const char *
586 rcm_info_info(rcm_info_tuple_t *tuple)
588 char *info = NULL;
590 if (tuple == NULL || tuple->info == NULL) {
591 errno = EINVAL;
592 return (NULL);
595 if (errno = nvlist_lookup_string(tuple->info, RCM_CLIENT_INFO, &info))
596 return (NULL);
598 return (info);
601 const char *
602 rcm_info_error(rcm_info_tuple_t *tuple)
604 char *errstr = NULL;
606 if (tuple == NULL || tuple->info == NULL) {
607 errno = EINVAL;
608 return (NULL);
611 if (errno = nvlist_lookup_string(tuple->info, RCM_CLIENT_ERROR,
612 &errstr))
613 return (NULL);
615 return (errstr);
618 /* return info string in the tuple */
619 const char *
620 rcm_info_modname(rcm_info_tuple_t *tuple)
622 char *modname = NULL;
624 if (tuple == NULL || tuple->info == NULL) {
625 errno = EINVAL;
626 return (NULL);
629 if (errno = nvlist_lookup_string(tuple->info, RCM_CLIENT_MODNAME,
630 &modname))
631 return (NULL);
633 return (modname);
636 /* return client pid in the tuple */
637 pid_t
638 rcm_info_pid(rcm_info_tuple_t *tuple)
640 uint64_t pid64 = (uint64_t)0;
642 if (tuple == NULL || tuple->info == NULL) {
643 errno = EINVAL;
644 return ((pid_t)0);
647 if (errno = nvlist_lookup_uint64(tuple->info, RCM_CLIENT_ID, &pid64))
648 return ((pid_t)0);
650 return ((pid_t)pid64);
653 /* return client state in the tuple */
655 rcm_info_state(rcm_info_tuple_t *tuple)
657 int state;
659 if (tuple == NULL || tuple->info == NULL) {
660 errno = EINVAL;
661 return (RCM_STATE_UNKNOWN);
664 if (errno = nvlist_lookup_int32(tuple->info, RCM_RSRCSTATE, &state))
665 return (RCM_STATE_UNKNOWN);
667 return (state);
670 /* return the generic properties in the tuple */
671 nvlist_t *
672 rcm_info_properties(rcm_info_tuple_t *tuple)
674 char *buf;
675 uint_t buflen;
676 nvlist_t *nvl;
678 if (tuple == NULL || tuple->info == NULL) {
679 errno = EINVAL;
680 return (NULL);
683 if (errno = nvlist_lookup_byte_array(tuple->info, RCM_CLIENT_PROPERTIES,
684 (uchar_t **)&buf, &buflen))
685 return (NULL);
687 if (errno = nvlist_unpack(buf, buflen, &nvl, 0)) {
688 free(buf);
689 return (NULL);
692 return (nvl);
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)
703 int seqnum;
705 if (tuple == NULL || tuple->info == NULL) {
706 errno = EINVAL;
707 return (-1);
710 if (errno = nvlist_lookup_int32(tuple->info, RCM_SEQ_NUM, &seqnum))
711 return (-1);
713 return (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)
729 pid_t pid;
730 int status, w;
731 char *argvec[] = {"sh", "-c", NULL, NULL};
733 argvec[2] = cmd;
734 if ((pid = fork1()) == 0) {
735 (void) execv("/bin/sh", argvec);
736 _exit(127);
737 } else if (pid == -1) {
738 return (-1);
741 do {
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)
752 rcm_info_t *tuple;
754 if (head == NULL) {
755 errno = EINVAL;
756 return (RCM_FAILURE);
759 if ((tuple = *head) == NULL) {
760 *head = info;
761 return (RCM_SUCCESS);
764 while (tuple->next) {
765 tuple = tuple->next;
767 tuple->next = info;
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/"
783 char *
784 rcm_module_dir(uint_t dirnum)
786 if (dirnum < N_MODULE_DIR)
787 return (rcm_dir(dirnum, NULL));
788 else
789 return (NULL);
792 char *
793 rcm_script_dir(uint_t dirnum)
795 if (dirnum < N_SCRIPT_DIR)
796 return (rcm_dir(dirnum + N_MODULE_DIR, NULL));
797 else
798 return (NULL);
801 char *
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))
807 return (NULL);
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) >=
814 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) {
819 dprintf((stderr,
820 "invalid module or script generic directory\n"));
821 return (NULL);
825 if (rcm_script)
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.
836 char *
837 rcm_get_script_dir(char *script_name)
839 uint_t i;
840 char *dir_name;
841 char path[MAXPATHLEN];
842 struct stat stats;
844 for (i = 0; (dir_name = rcm_script_dir(i)) != NULL; i++) {
845 if (snprintf(path, MAXPATHLEN, "%s%s", dir_name, script_name)
846 >= MAXPATHLEN) {
847 dprintf((stderr, "invalid script %s skipped\n",
848 script_name));
849 continue;
851 if (stat(path, &stats) == 0)
852 return (dir_name);
855 return (NULL);
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)
865 char *tmp;
867 if (((tmp = strstr(filename, RCM_MODULE_SUFFIX)) != NULL) &&
868 (tmp[strlen(RCM_MODULE_SUFFIX)] == '\0'))
869 return (0);
870 else
871 return (1);
874 /* Locate the module and call dlopen */
875 void *
876 rcm_module_open(char *modname)
878 unsigned i;
879 char *dir_name;
880 void *dlhandle = NULL;
881 char modpath[MAXPATHLEN];
883 #ifdef DEBUG
884 struct stat sbuf;
885 #endif
888 * dlopen the module
890 for (i = 0; (dir_name = rcm_module_dir(i)) != NULL; i++) {
891 if (snprintf(modpath, MAXPATHLEN, "%s%s", dir_name, modname)
892 >= MAXPATHLEN) {
893 dprintf((stderr, "invalid module %s skipped\n",
894 modname));
895 continue;
898 if ((dlhandle = dlopen(modpath, RTLD_LAZY)) != NULL) {
899 return (dlhandle);
902 dprintf((stderr, "failure (dlopen=%s)\n", dlerror()));
903 #ifdef DEBUG
904 if (stat(modpath, &sbuf) == 0) {
905 (void) fprintf(stderr, "%s is not a valid module\n",
906 modpath);
908 #endif
911 dprintf((stderr, "module %s not found\n", modname));
912 return (NULL);
915 /* dlclose module */
916 void
917 rcm_module_close(void *dlhandle)
919 if (dlclose(dlhandle) == 0)
920 return;
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.
934 /* ARGSUSED */
935 void
936 rcm_log_message(int level, char *message, ...)
938 dprintf((stderr, "rcm_log_message stub\n"));
942 * Helper functions
946 * Common routine for all rcm calls which require daemon processing
948 static int
949 rcm_common(int cmd, rcm_handle_t *hd, char **rsrcnames, uint_t flag, void *arg,
950 rcm_info_t **infop)
952 int i;
954 if (hd == NULL) {
955 errno = EINVAL;
956 return (RCM_FAILURE);
959 if (getuid() != 0) {
960 errno = EPERM;
961 return (RCM_FAILURE);
964 if ((flag & (RCM_DR_OPERATION | RCM_MOD_INFO)) == 0) {
965 if ((rsrcnames == NULL) || (rsrcnames[0] == NULL)) {
966 errno = EINVAL;
967 return (RCM_FAILURE);
970 for (i = 0; rsrcnames[i] != NULL; i++) {
971 if (*rsrcnames[i] == '\0') {
972 errno = EINVAL;
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
990 * memory leaks.
992 if (infop) {
993 *infop = NULL;
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.
1005 static int
1006 rcm_direct_call(int cmd, rcm_handle_t *hd, char **rsrcnames, uint_t flag,
1007 void *arg, rcm_info_t **infop)
1009 int error;
1011 librcm_ops_t *ops = (librcm_ops_t *)hd->lrcm_ops;
1012 switch (cmd) {
1013 case CMD_GETINFO:
1014 error = ops->librcm_getinfo(rsrcnames, flag, hd->seq_num,
1015 infop);
1016 break;
1018 case CMD_OFFLINE:
1019 error = ops->librcm_offline(rsrcnames, hd->pid, flag,
1020 hd->seq_num, infop);
1021 break;
1023 case CMD_ONLINE:
1024 error = ops->librcm_online(rsrcnames, hd->pid, flag,
1025 hd->seq_num, infop);
1026 break;
1028 case CMD_REMOVE:
1029 error = ops->librcm_remove(rsrcnames, hd->pid, flag,
1030 hd->seq_num, infop);
1031 break;
1033 case CMD_SUSPEND:
1034 error = ops->librcm_suspend(rsrcnames, hd->pid, flag,
1035 hd->seq_num, (timespec_t *)arg, infop);
1036 break;
1038 case CMD_RESUME:
1039 error = ops->librcm_resume(rsrcnames, hd->pid, flag,
1040 hd->seq_num, infop);
1041 break;
1043 case CMD_REGISTER:
1044 error = ops->librcm_regis(hd->modname, rsrcnames[0], hd->pid,
1045 flag, infop);
1046 break;
1048 case CMD_UNREGISTER:
1049 error = ops->librcm_unregis(hd->modname, rsrcnames[0], hd->pid,
1050 flag);
1051 break;
1053 case CMD_REQUEST_CHANGE:
1054 error = ops->librcm_request_change(rsrcnames[0], hd->pid, flag,
1055 hd->seq_num, (nvlist_t *)arg, infop);
1056 break;
1058 case CMD_NOTIFY_CHANGE:
1059 error = ops->librcm_notify_change(rsrcnames[0], hd->pid, flag,
1060 hd->seq_num, (nvlist_t *)arg, infop);
1061 break;
1063 case CMD_EVENT:
1064 error = ops->librcm_notify_event(rsrcnames[0], hd->pid, flag,
1065 hd->seq_num, (nvlist_t *)arg, infop);
1066 break;
1068 case CMD_GETSTATE:
1069 error = ops->librcm_getstate(rsrcnames[0], hd->pid, infop);
1070 break;
1072 default:
1073 dprintf((stderr, "invalid command: %d\n", cmd));
1074 error = EFAULT;
1077 if (error > 0) {
1078 errno = error;
1079 error = RCM_FAILURE;
1081 return (error);
1085 * Call into rcm_daemon door to process the request
1087 static int
1088 rcm_daemon_call(int cmd, rcm_handle_t *hd, char **rsrcnames, uint_t flag,
1089 void *arg, rcm_info_t **infop)
1091 int errno_found;
1092 int daemon_errno = 0;
1093 int error = RCM_SUCCESS;
1094 int delay = 300;
1095 int maxdelay = 10000; /* 10 seconds */
1096 char *nvl_packed = NULL;
1097 size_t nvl_size = 0;
1098 nvlist_t *ret = NULL;
1099 nvpair_t *nvp;
1100 size_t rsize = 0;
1101 rcm_info_t *info = NULL;
1103 errno = 0;
1106 * Decide whether to start the daemon
1108 switch (cmd) {
1109 case CMD_GETINFO:
1110 case CMD_OFFLINE:
1111 case CMD_ONLINE:
1112 case CMD_REMOVE:
1113 case CMD_SUSPEND:
1114 case CMD_RESUME:
1115 case CMD_REGISTER:
1116 case CMD_UNREGISTER:
1117 case CMD_EVENT:
1118 case CMD_REQUEST_CHANGE:
1119 case CMD_NOTIFY_CHANGE:
1120 case CMD_GETSTATE:
1121 break;
1123 default:
1124 errno = EFAULT;
1125 return (RCM_FAILURE);
1128 if (rcm_daemon_is_alive() != 1) {
1129 dprintf((stderr, "failed to start rcm_daemon\n"));
1130 errno = EFAULT;
1131 return (RCM_FAILURE);
1135 * Generate a packed nvlist for the request
1137 if (rcm_generate_nvlist(cmd, hd, rsrcnames, flag, arg, &nvl_packed,
1138 &nvl_size) < 0) {
1139 dprintf((stderr, "error in nvlist generation\n"));
1140 errno = EFAULT;
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.
1148 retry:
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",
1152 strerror(errno)));
1153 free(nvl_packed);
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.
1165 errno_found = 0;
1166 nvp = NULL;
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;
1171 goto out;
1173 errno_found++;
1174 break;
1177 if (errno_found == 0) {
1178 errno = EFAULT;
1179 error = RCM_FAILURE;
1180 goto out;
1183 if (daemon_errno == EAGAIN) {
1185 * Wait and retry
1187 dprintf((stderr, "retry door_call\n"));
1189 if (delay > maxdelay) {
1190 errno = EAGAIN;
1191 error = RCM_FAILURE;
1192 goto out;
1195 (void) poll(NULL, 0, delay);
1196 delay *= 2; /* exponential back off */
1197 nvlist_free(ret);
1198 goto retry;
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"));
1206 errno = EFAULT;
1207 error = RCM_FAILURE;
1208 goto out;
1211 if (infop)
1212 *infop = info;
1213 else
1214 rcm_free_info(info);
1216 if (daemon_errno) {
1217 if (daemon_errno > 0) {
1218 errno = daemon_errno;
1219 error = RCM_FAILURE;
1220 } else {
1221 error = daemon_errno;
1225 out:
1226 free(nvl_packed);
1227 nvlist_free(ret);
1228 dprintf((stderr, "daemon call is done. error = %d, errno = %s\n", error,
1229 strerror(errno)));
1230 return (error);
1234 * Extract registration info from event data.
1235 * Return 0 on success and -1 on failure.
1237 static int
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;
1243 char *buf;
1244 uint_t buflen;
1245 nvpair_t *nvp = NULL;
1247 while (nvp = nvlist_next_nvpair(nvl, nvp)) {
1249 buf = NULL;
1250 buflen = 0;
1252 if (strcmp(nvpair_name(nvp), RCM_RESULT_INFO) != 0)
1253 continue;
1255 if ((tmp = calloc(1, sizeof (*tmp))) == NULL) {
1256 dprintf((stderr, "out of memory\n"));
1257 goto fail;
1260 if (errno = nvpair_value_byte_array(nvp, (uchar_t **)&buf,
1261 &buflen)) {
1262 free(tmp);
1263 dprintf((stderr, "failed (nvpair_value=%s)\n",
1264 strerror(errno)));
1265 goto fail;
1267 if (errno = nvlist_unpack(buf, buflen, &(tmp->info), 0)) {
1268 free(tmp);
1269 dprintf((stderr, "failed (nvlist_unpack=%s)\n",
1270 strerror(errno)));
1271 goto fail;
1274 if (info == NULL) {
1275 prev = info = tmp;
1276 } else {
1277 prev->next = tmp;
1278 prev = tmp;
1282 *infop = info;
1283 return (0);
1285 fail:
1286 rcm_free_info(info);
1287 *infop = NULL;
1288 return (-1);
1291 /* Generate a packed nvlist for communicating with RCM daemon */
1292 static int
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)
1296 int nrsrcnames;
1297 char *buf = NULL;
1298 size_t buflen = 0;
1299 nvlist_t *nvl = NULL;
1301 assert((nvl_packed != NULL) && (nvl_size != NULL));
1303 *nvl_size = 0;
1304 *nvl_packed = 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",
1309 strerror(errno)));
1310 return (-1);
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",
1316 strerror(errno)));
1317 goto fail;
1319 if (rsrcnames) {
1320 nrsrcnames = 0;
1321 while (rsrcnames[nrsrcnames] != NULL)
1322 nrsrcnames++;
1323 if (nvlist_add_string_array(nvl, RCM_RSRCNAMES, rsrcnames,
1324 nrsrcnames) != 0) {
1325 dprintf((stderr, "failed (nvlist_add(RSRCNAMES)=%s).\n",
1326 strerror(errno)));
1327 goto fail;
1330 if (hd->modname) {
1331 if (nvlist_add_string(nvl, RCM_CLIENT_MODNAME, hd->modname)
1332 != 0) {
1333 dprintf((stderr,
1334 "failed (nvlist_add(CLIENT_MODNAME)=%s).\n",
1335 strerror(errno)));
1336 goto fail;
1339 if (hd->pid) {
1340 if (nvlist_add_uint64(nvl, RCM_CLIENT_ID, hd->pid) != 0) {
1341 dprintf((stderr, "failed (nvlist_add(CLIENT_ID)=%s).\n",
1342 strerror(errno)));
1343 goto fail;
1346 if (flag) {
1347 if (nvlist_add_uint32(nvl, RCM_REQUEST_FLAG, flag) != 0) {
1348 dprintf((stderr,
1349 "failed (nvlist_add(REQUEST_FLAG)=%s).\n",
1350 strerror(errno)));
1351 goto fail;
1354 if (arg && cmd == CMD_SUSPEND) {
1355 if (nvlist_add_byte_array(nvl, RCM_SUSPEND_INTERVAL,
1356 (uchar_t *)arg, sizeof (timespec_t)) != 0) {
1357 dprintf((stderr,
1358 "failed (nvlist_add(SUSPEND_INTERVAL)=%s).\n",
1359 strerror(errno)));
1360 goto fail;
1363 if (arg &&
1364 ((cmd == CMD_REQUEST_CHANGE) || (cmd == CMD_NOTIFY_CHANGE))) {
1365 if (errno = nvlist_pack(arg, &buf, &buflen, NV_ENCODE_NATIVE,
1366 0)) {
1367 dprintf((stderr,
1368 "failed (nvlist_pack(CHANGE_DATA)=%s).\n",
1369 strerror(errno)));
1370 goto fail;
1372 if (nvlist_add_byte_array(nvl, RCM_CHANGE_DATA, (uchar_t *)buf,
1373 buflen) != 0) {
1374 dprintf((stderr,
1375 "failed (nvlist_add(CHANGE_DATA)=%s).\n",
1376 strerror(errno)));
1377 goto fail;
1380 if (arg && cmd == CMD_EVENT) {
1381 if (errno = nvlist_pack(arg, &buf, &buflen, NV_ENCODE_NATIVE,
1382 0)) {
1383 dprintf((stderr,
1384 "failed (nvlist_pack(CHANGE_DATA)=%s).\n",
1385 strerror(errno)));
1386 goto fail;
1388 if (nvlist_add_byte_array(nvl, RCM_EVENT_DATA, (uchar_t *)buf,
1389 buflen) != 0) {
1390 dprintf((stderr,
1391 "failed (nvlist_add(EVENT_DATA)=%s).\n",
1392 strerror(errno)));
1393 goto fail;
1397 /* Pack the nvlist */
1398 if (errno = nvlist_pack(nvl, nvl_packed, nvl_size, NV_ENCODE_NATIVE,
1399 0)) {
1400 dprintf((stderr, "failed (nvlist_pack=%s).\n",
1401 strerror(errno)));
1402 goto fail;
1405 /* If an argument was packed intermediately, free the buffer */
1406 free(buf);
1408 /* Free the unpacked version of the nvlist and return the packed list */
1409 nvlist_free(nvl);
1410 return (0);
1412 fail:
1413 free(buf);
1414 nvlist_free(nvl);
1415 free(*nvl_packed);
1416 *nvl_packed = NULL;
1417 *nvl_size = 0;
1418 return (-1);
1421 /* check if rcm_daemon is up and running */
1422 static int
1423 rcm_daemon_is_alive()
1425 int lasttry;
1426 struct stat st;
1427 nvlist_t *nvl;
1428 char *buf = NULL;
1429 size_t buflen = 0;
1430 int delay = 300;
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)));
1436 return (0);
1438 if (errno = nvlist_add_int32(nvl, RCM_CMD, CMD_KNOCK)) {
1439 dprintf((stderr, "nvlist_add failed: %s\n", strerror(errno)));
1440 nvlist_free(nvl);
1441 return (0);
1443 if (errno = nvlist_pack(nvl, &buf, &buflen, NV_ENCODE_NATIVE, 0)) {
1444 dprintf((stderr, "nvlist_pack failed: %s\n", strerror(errno)));
1445 nvlist_free(nvl);
1446 return (0);
1448 nvlist_free(nvl);
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,
1455 NULL) == 0)) {
1456 free(buf);
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) {
1476 break;
1478 (void) poll(NULL, 0, delay);
1479 delay *= 2;
1482 free(buf);
1483 if (lasttry == 0)
1484 return (1);
1485 return (0);
1489 * Check permission.
1491 * The policy is root only for now. Need to relax this when interface level
1492 * is raised.
1494 static int
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.
1506 const char *
1507 rcm_get_client_name(rcm_handle_t *hd)
1509 return (hd->modname);