10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*1914Scasper * Common Development and Distribution License (the "License"). 6*1914Scasper * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 20*1914Scasper */ 21*1914Scasper /* 22*1914Scasper * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate #ifndef _RCM_IMPL_H 270Sstevel@tonic-gate #define _RCM_IMPL_H 280Sstevel@tonic-gate 290Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 300Sstevel@tonic-gate 310Sstevel@tonic-gate #ifdef __cplusplus 320Sstevel@tonic-gate extern "C" { 330Sstevel@tonic-gate #endif 340Sstevel@tonic-gate 350Sstevel@tonic-gate #include <assert.h> 360Sstevel@tonic-gate #include <stdarg.h> 370Sstevel@tonic-gate #include <stdio.h> 38*1914Scasper #include <stdio_ext.h> 390Sstevel@tonic-gate #include <stdlib.h> 400Sstevel@tonic-gate #include <dirent.h> 410Sstevel@tonic-gate #include <dlfcn.h> 420Sstevel@tonic-gate #include <errno.h> 430Sstevel@tonic-gate #include <fcntl.h> 440Sstevel@tonic-gate #include <limits.h> 450Sstevel@tonic-gate #include <locale.h> 460Sstevel@tonic-gate #include <poll.h> 470Sstevel@tonic-gate #include <signal.h> 480Sstevel@tonic-gate #include <strings.h> 490Sstevel@tonic-gate #include <syslog.h> 500Sstevel@tonic-gate #include <thread.h> 510Sstevel@tonic-gate #include <unistd.h> 520Sstevel@tonic-gate #include <sys/mman.h> 530Sstevel@tonic-gate #include <sys/param.h> 540Sstevel@tonic-gate #include <sys/stat.h> 550Sstevel@tonic-gate #include <sys/types.h> 560Sstevel@tonic-gate #include <librcm.h> 570Sstevel@tonic-gate #include <librcm_impl.h> 580Sstevel@tonic-gate 590Sstevel@tonic-gate #include "rcm_module.h" 600Sstevel@tonic-gate 610Sstevel@tonic-gate 620Sstevel@tonic-gate /* 630Sstevel@tonic-gate * Daemon states for thread control 640Sstevel@tonic-gate */ 650Sstevel@tonic-gate #define RCMD_INIT 1 660Sstevel@tonic-gate #define RCMD_NORMAL 2 670Sstevel@tonic-gate #define RCMD_CLEANUP 3 680Sstevel@tonic-gate #define RCMD_FINI 4 690Sstevel@tonic-gate 700Sstevel@tonic-gate /* 710Sstevel@tonic-gate * flags for node operation 720Sstevel@tonic-gate */ 730Sstevel@tonic-gate #define RSRC_NODE_CREATE 1 740Sstevel@tonic-gate #define RSRC_NODE_REMOVE 2 /* not used */ 750Sstevel@tonic-gate 760Sstevel@tonic-gate /* 770Sstevel@tonic-gate * Resource types 780Sstevel@tonic-gate */ 790Sstevel@tonic-gate #define RSRC_TYPE_NORMAL 0 800Sstevel@tonic-gate #define RSRC_TYPE_DEVICE 1 810Sstevel@tonic-gate #define RSRC_TYPE_FILESYS 2 820Sstevel@tonic-gate #define RSRC_TYPE_ABSTRACT 3 830Sstevel@tonic-gate 840Sstevel@tonic-gate /* 850Sstevel@tonic-gate * lock conflict checking flags 860Sstevel@tonic-gate */ 870Sstevel@tonic-gate #define LOCK_FOR_DR 0 880Sstevel@tonic-gate #define LOCK_FOR_USE 1 890Sstevel@tonic-gate 900Sstevel@tonic-gate /* 910Sstevel@tonic-gate * Sequence number encoding constants 920Sstevel@tonic-gate */ 930Sstevel@tonic-gate #define SEQ_NUM_SHIFT 8 /* lowest 8 bits indicate cascade operation */ 940Sstevel@tonic-gate #define SEQ_NUM_MASK ((1 << SEQ_NUM_SHIFT) - 1) 950Sstevel@tonic-gate 960Sstevel@tonic-gate /* 970Sstevel@tonic-gate * RCM queuing structure 980Sstevel@tonic-gate */ 990Sstevel@tonic-gate typedef struct rcm_queue { 1000Sstevel@tonic-gate struct rcm_queue *next; 1010Sstevel@tonic-gate struct rcm_queue *prev; 1020Sstevel@tonic-gate } rcm_queue_t; 1030Sstevel@tonic-gate 1040Sstevel@tonic-gate #define RCM_STRUCT_BASE_ADDR(struct_type, x, y) \ 1050Sstevel@tonic-gate ((struct_type *) ((void *)(((char *)(x)) - \ 1060Sstevel@tonic-gate (int)(&((struct_type *)0)->y)))) 1070Sstevel@tonic-gate 1080Sstevel@tonic-gate /* 1090Sstevel@tonic-gate * Struct for client loadable module 1100Sstevel@tonic-gate */ 1110Sstevel@tonic-gate typedef struct module { 1120Sstevel@tonic-gate struct module *next; 1130Sstevel@tonic-gate void *dlhandle; 1140Sstevel@tonic-gate struct rcm_mod_ops *(*init)(); 1150Sstevel@tonic-gate const char *(*info)(); 1160Sstevel@tonic-gate int (*fini)(); 1170Sstevel@tonic-gate struct rcm_mod_ops *modops; /* ops vector */ 1180Sstevel@tonic-gate char *name; /* module name */ 1190Sstevel@tonic-gate rcm_handle_t *rcmhandle; 1200Sstevel@tonic-gate int ref_count; 1210Sstevel@tonic-gate rcm_queue_t client_q; /* list of module's clients */ 1220Sstevel@tonic-gate struct script_info *rsi; /* scripting data */ 1230Sstevel@tonic-gate } module_t; 1240Sstevel@tonic-gate 1250Sstevel@tonic-gate /* 1260Sstevel@tonic-gate * Struct for describing a resource client 1270Sstevel@tonic-gate */ 1280Sstevel@tonic-gate typedef struct client { 1290Sstevel@tonic-gate rcm_queue_t queue; /* per module queue */ 1300Sstevel@tonic-gate struct client *next; /* next client on rsrc node list */ 1310Sstevel@tonic-gate module_t *module; /* per-client module */ 1320Sstevel@tonic-gate char *alias; /* rsrc_name known to client */ 1330Sstevel@tonic-gate pid_t pid; /* pid of regis process */ 1340Sstevel@tonic-gate int state; /* rsrc state known to client */ 1350Sstevel@tonic-gate uint_t flag; /* flag specified for registration */ 1360Sstevel@tonic-gate uint_t prv_flags; /* currently used by rcm scripting */ 1370Sstevel@tonic-gate } client_t; 1380Sstevel@tonic-gate 1390Sstevel@tonic-gate /* 1400Sstevel@tonic-gate * defines for client_t:prv_flags (used by rcm scripting) 1410Sstevel@tonic-gate */ 1420Sstevel@tonic-gate #define RCM_NEED_TO_UNREGISTER 1 1430Sstevel@tonic-gate 1440Sstevel@tonic-gate /* 1450Sstevel@tonic-gate * Struct for a list of outstanding rcm requests 1460Sstevel@tonic-gate */ 1470Sstevel@tonic-gate typedef struct { 1480Sstevel@tonic-gate int n_req; 1490Sstevel@tonic-gate int n_req_max; /* max entries in this block */ 1500Sstevel@tonic-gate struct { 1510Sstevel@tonic-gate int seq_num; /* sequence number of request */ 1520Sstevel@tonic-gate int state; /* current state */ 1530Sstevel@tonic-gate id_t id; /* id of initiator */ 1540Sstevel@tonic-gate uint_t flag; /* request flags */ 1550Sstevel@tonic-gate int type; /* resource(device) type */ 1560Sstevel@tonic-gate char device[MAXPATHLEN]; /* name of device or resource */ 1570Sstevel@tonic-gate } req[1]; 1580Sstevel@tonic-gate /* more entries may follow */ 1590Sstevel@tonic-gate } rcm_req_t; 1600Sstevel@tonic-gate 1610Sstevel@tonic-gate /* 1620Sstevel@tonic-gate * struct for describing resource tree node 1630Sstevel@tonic-gate */ 1640Sstevel@tonic-gate typedef struct rsrc_node { 1650Sstevel@tonic-gate struct rsrc_node *parent; 1660Sstevel@tonic-gate struct rsrc_node *sibling; 1670Sstevel@tonic-gate struct rsrc_node *child; 1680Sstevel@tonic-gate char *name; /* phys path for devices */ 1690Sstevel@tonic-gate client_t *users; /* linked list of users */ 1700Sstevel@tonic-gate int type; /* resource type */ 1710Sstevel@tonic-gate } rsrc_node_t; 1720Sstevel@tonic-gate 1730Sstevel@tonic-gate /* 1740Sstevel@tonic-gate * struct for tree action args 1750Sstevel@tonic-gate */ 1760Sstevel@tonic-gate typedef struct { 1770Sstevel@tonic-gate int cmd; /* command */ 1780Sstevel@tonic-gate int seq_num; /* unique sequence number */ 1790Sstevel@tonic-gate int retcode; /* return code */ 1800Sstevel@tonic-gate uint_t flag; /* flag assoc. w command */ 1810Sstevel@tonic-gate timespec_t *interval; /* for suspend command */ 1820Sstevel@tonic-gate nvlist_t *nvl; /* for state changes */ 1830Sstevel@tonic-gate rcm_info_t **info; /* info to be filled in */ 1840Sstevel@tonic-gate } tree_walk_arg_t; 1850Sstevel@tonic-gate 1860Sstevel@tonic-gate /* 1870Sstevel@tonic-gate * for synchrizing various threads 1880Sstevel@tonic-gate */ 1890Sstevel@tonic-gate typedef struct { 1900Sstevel@tonic-gate int thr_count; 1910Sstevel@tonic-gate short wanted; 1920Sstevel@tonic-gate short state; 1930Sstevel@tonic-gate time_t last_update; 1940Sstevel@tonic-gate cond_t cv; 1950Sstevel@tonic-gate mutex_t lock; 1960Sstevel@tonic-gate } barrier_t; 1970Sstevel@tonic-gate 1980Sstevel@tonic-gate /* 1990Sstevel@tonic-gate * locks 2000Sstevel@tonic-gate */ 2010Sstevel@tonic-gate extern mutex_t rcm_req_lock; 2020Sstevel@tonic-gate 2030Sstevel@tonic-gate /* 2040Sstevel@tonic-gate * global variables 2050Sstevel@tonic-gate */ 2060Sstevel@tonic-gate extern librcm_ops_t rcm_ops; /* ops for module callback */ 2070Sstevel@tonic-gate extern int need_cleanup; 2080Sstevel@tonic-gate 2090Sstevel@tonic-gate /* 2100Sstevel@tonic-gate * comparison macros 2110Sstevel@tonic-gate * EQUAL, AFTER, DESCENDENT 2120Sstevel@tonic-gate */ 2130Sstevel@tonic-gate #define EQUAL(x, y) (strcmp(x, y) == 0) 2140Sstevel@tonic-gate #define AFTER(x, y) (strcmp(x, y) > 0) 2150Sstevel@tonic-gate #define DESCENDENT(x, y) \ 2160Sstevel@tonic-gate ((strlen(x) > strlen(y)) && \ 2170Sstevel@tonic-gate (strncmp(x, y, strlen(y)) == 0) && \ 2180Sstevel@tonic-gate ((x[strlen(y)] == '/') || \ 2190Sstevel@tonic-gate (x[strlen(y)] == ':') || \ 2200Sstevel@tonic-gate (x[strlen(y) - 1] == '/'))) 2210Sstevel@tonic-gate 2220Sstevel@tonic-gate /* 2230Sstevel@tonic-gate * function prototypes 2240Sstevel@tonic-gate */ 2250Sstevel@tonic-gate 2260Sstevel@tonic-gate /* top level request handling routines */ 2270Sstevel@tonic-gate 2280Sstevel@tonic-gate void event_service(void **, size_t *); 2290Sstevel@tonic-gate int process_resource_suspend(char **, pid_t, uint_t, int, timespec_t *, 2300Sstevel@tonic-gate rcm_info_t **); 2310Sstevel@tonic-gate int notify_resource_resume(char **, pid_t, uint_t, int, rcm_info_t **); 2320Sstevel@tonic-gate int process_resource_offline(char **, pid_t, uint_t, int, rcm_info_t **); 2330Sstevel@tonic-gate int notify_resource_online(char **, pid_t, uint_t, int, rcm_info_t **); 2340Sstevel@tonic-gate int notify_resource_remove(char **, pid_t, uint_t, int, rcm_info_t **); 2350Sstevel@tonic-gate int add_resource_client(char *, char *, pid_t, uint_t, rcm_info_t **); 2360Sstevel@tonic-gate int remove_resource_client(char *, char *, pid_t, uint_t); 2370Sstevel@tonic-gate int get_resource_info(char **, uint_t, int, rcm_info_t **); 2380Sstevel@tonic-gate int notify_resource_event(char *, pid_t, uint_t, int, nvlist_t *, 2390Sstevel@tonic-gate rcm_info_t **); 2400Sstevel@tonic-gate int request_capacity_change(char *, pid_t, uint_t, int, nvlist_t *, 2410Sstevel@tonic-gate rcm_info_t **); 2420Sstevel@tonic-gate int notify_capacity_change(char *, pid_t, uint_t, int, nvlist_t *, 2430Sstevel@tonic-gate rcm_info_t **); 2440Sstevel@tonic-gate int get_resource_state(char *, pid_t, rcm_info_t **); 2450Sstevel@tonic-gate rcm_info_t *rsrc_mod_info(); 2460Sstevel@tonic-gate 2470Sstevel@tonic-gate /* dr request list routines */ 2480Sstevel@tonic-gate 2490Sstevel@tonic-gate rcm_info_t *rsrc_dr_info(); 2500Sstevel@tonic-gate void clean_dr_list(); 2510Sstevel@tonic-gate int dr_req_add(char *, pid_t, uint_t, int, int, timespec_t *, rcm_info_t **); 2520Sstevel@tonic-gate int dr_req_update(char *, pid_t, uint_t, int, int, rcm_info_t **); 2530Sstevel@tonic-gate int dr_req_lookup(int, char *); 2540Sstevel@tonic-gate void dr_req_remove(char *, uint_t); 2550Sstevel@tonic-gate int info_req_add(char *, uint_t, int); 2560Sstevel@tonic-gate void info_req_remove(int); 2570Sstevel@tonic-gate int rsrc_check_lock_conflicts(char *, uint_t, int, rcm_info_t **); 2580Sstevel@tonic-gate 2590Sstevel@tonic-gate /* node related routines */ 2600Sstevel@tonic-gate 2610Sstevel@tonic-gate int rsrc_get_type(const char *); 2620Sstevel@tonic-gate int rsrc_node_find(char *, int, rsrc_node_t **); 2630Sstevel@tonic-gate int rsrc_node_add_user(rsrc_node_t *, char *, char *, pid_t, uint_t); 2640Sstevel@tonic-gate int rsrc_node_remove_user(rsrc_node_t *, char *, pid_t, uint_t); 2650Sstevel@tonic-gate client_t *rsrc_client_find(char *, pid_t, client_t **); 2660Sstevel@tonic-gate int rsrc_client_action_list(client_t *, int cmd, void *); 2670Sstevel@tonic-gate 2680Sstevel@tonic-gate /* tree related routines */ 2690Sstevel@tonic-gate 2700Sstevel@tonic-gate int rsrc_usage_info(char **, uint_t, int, rcm_info_t **); 2710Sstevel@tonic-gate int rsrc_tree_action(rsrc_node_t *, int, tree_walk_arg_t *); 2720Sstevel@tonic-gate 2730Sstevel@tonic-gate /* database helpers and misc */ 2740Sstevel@tonic-gate 2750Sstevel@tonic-gate void rcmd_set_state(int); 2760Sstevel@tonic-gate int rcmd_thr_incr(int); 2770Sstevel@tonic-gate void rcmd_thr_decr(void); 2780Sstevel@tonic-gate void rcmd_thr_signal(void); 2790Sstevel@tonic-gate void rcmd_lock_init(void); 2800Sstevel@tonic-gate void rcmd_db_init(void); 2810Sstevel@tonic-gate void rcmd_db_sync(void); 2820Sstevel@tonic-gate void rcmd_db_clean(void); 2830Sstevel@tonic-gate void rcmd_start_timer(int); 2840Sstevel@tonic-gate void rcmd_exit(int); 2850Sstevel@tonic-gate void rcm_log_message(int, char *, ...); 2860Sstevel@tonic-gate void rcm_log_msg(int, char *, ...); 2870Sstevel@tonic-gate void add_busy_rsrc_to_list(char *, pid_t, int, int, char *, const char *, 2880Sstevel@tonic-gate const char *, nvlist_t *, rcm_info_t **); 2890Sstevel@tonic-gate char *resolve_name(char *); 2900Sstevel@tonic-gate int proc_exist(pid_t); 2910Sstevel@tonic-gate void *s_malloc(size_t); 2920Sstevel@tonic-gate void *s_calloc(int, size_t); 2930Sstevel@tonic-gate void *s_realloc(void *, size_t); 2940Sstevel@tonic-gate char *s_strdup(const char *); 2950Sstevel@tonic-gate 2960Sstevel@tonic-gate /* 2970Sstevel@tonic-gate * RCM queuing function prototypes 2980Sstevel@tonic-gate */ 2990Sstevel@tonic-gate void rcm_init_queue(rcm_queue_t *); 3000Sstevel@tonic-gate void rcm_enqueue_head(rcm_queue_t *, rcm_queue_t *); 3010Sstevel@tonic-gate void rcm_enqueue_tail(rcm_queue_t *, rcm_queue_t *); 3020Sstevel@tonic-gate void rcm_enqueue(rcm_queue_t *, rcm_queue_t *); 3030Sstevel@tonic-gate rcm_queue_t *rcm_dequeue_head(rcm_queue_t *); 3040Sstevel@tonic-gate rcm_queue_t *rcm_dequeue_tail(rcm_queue_t *); 3050Sstevel@tonic-gate void rcm_dequeue(rcm_queue_t *); 3060Sstevel@tonic-gate 3070Sstevel@tonic-gate /* 3080Sstevel@tonic-gate * Function protoypes related to rcm scripting 3090Sstevel@tonic-gate */ 3100Sstevel@tonic-gate int script_main_init(void); 3110Sstevel@tonic-gate int script_main_fini(void); 3120Sstevel@tonic-gate struct rcm_mod_ops *script_init(module_t *); 3130Sstevel@tonic-gate char *script_info(module_t *); 3140Sstevel@tonic-gate int script_fini(module_t *); 3150Sstevel@tonic-gate 3160Sstevel@tonic-gate 3170Sstevel@tonic-gate #ifdef __cplusplus 3180Sstevel@tonic-gate } 3190Sstevel@tonic-gate #endif 3200Sstevel@tonic-gate 3210Sstevel@tonic-gate #endif /* _RCM_IMPL_H */ 322