1 /*
2  * CDDL HEADER START
3  *
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.
7  *
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.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*
29  * RCM module for managing the OS Quiesce event (SUNW_OS) in a
30  * clustered environment.
31  */
32 
33 #include <stdlib.h>
34 #include <unistd.h>
35 #include <fcntl.h>
36 #include <string.h>
37 #include <thread.h>
38 #include <synch.h>
39 #include <assert.h>
40 #include <errno.h>
41 #include <libintl.h>
42 #include <sys/param.h>
43 #include <sys/wait.h>
44 #include <sys/types.h>
45 #include <sys/stat.h>
46 #include <sys/cladm.h>
47 #include "rcm_module.h"
48 
49 #define	SUNW_OS		"SUNW_OS"
50 #define	OS_USAGE	gettext("Sun Cluster")
51 #define	OS_SUSPEND_ERR	gettext("OS cannot be quiesced on clustered nodes")
52 #define	OS_OFFLINE_ERR	gettext("Invalid operation: OS cannot be offlined")
53 #define	OS_REMOVE_ERR	gettext("Invalid operation: OS cannot be removed")
54 
55 static int		cluster_register(rcm_handle_t *);
56 static int		cluster_unregister(rcm_handle_t *);
57 static int		cluster_getinfo(rcm_handle_t *, char *, id_t, uint_t,
58 			    char **, char **, nvlist_t *, rcm_info_t **);
59 static int		cluster_suspend(rcm_handle_t *, char *, id_t,
60 			    timespec_t *, uint_t, char **, rcm_info_t **);
61 static int		cluster_resume(rcm_handle_t *, char *, id_t, uint_t,
62 			    char **, rcm_info_t **);
63 static int		cluster_offline(rcm_handle_t *, char *, id_t, uint_t,
64 			    char **, rcm_info_t **);
65 static int		cluster_online(rcm_handle_t *, char *, id_t, uint_t,
66 			    char **, rcm_info_t **);
67 static int		cluster_remove(rcm_handle_t *, char *, id_t, uint_t,
68 			    char **, rcm_info_t **);
69 
70 static int		cluster_SUNW_os_registered = 0;
71 
72 static struct rcm_mod_ops cluster_ops =
73 {
74 	RCM_MOD_OPS_VERSION,
75 	cluster_register,
76 	cluster_unregister,
77 	cluster_getinfo,
78 	cluster_suspend,
79 	cluster_resume,
80 	cluster_offline,
81 	cluster_online,
82 	cluster_remove,
83 	NULL,
84 	NULL,
85 	NULL
86 };
87 
88 struct rcm_mod_ops *
rcm_mod_init()89 rcm_mod_init()
90 {
91 	return (&cluster_ops);
92 }
93 
94 const char *
rcm_mod_info()95 rcm_mod_info()
96 {
97 	return (gettext("RCM Cluster module 1.3"));
98 }
99 
100 int
rcm_mod_fini()101 rcm_mod_fini()
102 {
103 	return (RCM_SUCCESS);
104 }
105 
106 static int
cluster_register(rcm_handle_t * hdl)107 cluster_register(rcm_handle_t *hdl)
108 {
109 	int bootflags;
110 
111 	if (cluster_SUNW_os_registered)
112 		return (RCM_SUCCESS);
113 
114 	if (_cladm(CL_INITIALIZE, CL_GET_BOOTFLAG, &bootflags) != 0) {
115 		rcm_log_message(RCM_ERROR,
116 			gettext("unable to check cluster status\n"));
117 		return (RCM_FAILURE);
118 	}
119 
120 	/* attempt to determine if we are in cluster mode */
121 
122 	if (bootflags & CLUSTER_BOOTED) {
123 		if (rcm_register_interest(hdl, SUNW_OS, 0, NULL) !=
124 		    RCM_SUCCESS) {
125 			rcm_log_message(RCM_ERROR,
126 			    gettext("failed to register\n"));
127 			return (RCM_FAILURE);
128 		} else {
129 			cluster_SUNW_os_registered = 1;
130 			rcm_log_message(RCM_DEBUG, "registered " SUNW_OS
131 					"\n");
132 		}
133 	}
134 
135 	return (RCM_SUCCESS);
136 }
137 
138 static int
cluster_unregister(rcm_handle_t * hdl)139 cluster_unregister(rcm_handle_t *hdl)
140 {
141 
142 	if (cluster_SUNW_os_registered) {
143 		if (rcm_unregister_interest(hdl, SUNW_OS, 0) !=
144 		    RCM_SUCCESS) {
145 			rcm_log_message(RCM_ERROR,
146 			    gettext("failed to unregister"));
147 		}
148 		cluster_SUNW_os_registered = 0;
149 	}
150 	return (RCM_SUCCESS);
151 }
152 
153 /*ARGSUSED*/
154 static int
cluster_getinfo(rcm_handle_t * hdl,char * rsrcname,id_t id,uint_t flags,char ** infostr,char ** errstr,nvlist_t * props,rcm_info_t ** dependent)155 cluster_getinfo(rcm_handle_t *hdl, char *rsrcname, id_t id, uint_t flags,
156     char **infostr, char **errstr, nvlist_t *props, rcm_info_t **dependent)
157 {
158 
159 	assert(rsrcname != NULL && infostr != NULL);
160 
161 	if ((*infostr = strdup(OS_USAGE)) == NULL)
162 		rcm_log_message(RCM_ERROR, gettext("strdup failure\n"));
163 
164 	return (RCM_SUCCESS);
165 }
166 
167 /*ARGSUSED*/
168 static int
cluster_suspend(rcm_handle_t * hdl,char * rsrcname,id_t id,timespec_t * interval,uint_t flags,char ** errstr,rcm_info_t ** dependent)169 cluster_suspend(rcm_handle_t *hdl, char *rsrcname, id_t id,
170     timespec_t *interval, uint_t flags, char **errstr,
171     rcm_info_t **dependent)
172 {
173 	if ((*errstr = strdup(OS_SUSPEND_ERR)) == NULL)
174 		rcm_log_message(RCM_ERROR, gettext("strdup failure\n"));
175 
176 	return (RCM_FAILURE);
177 }
178 
179 /*ARGSUSED*/
180 static int
cluster_resume(rcm_handle_t * hdl,char * rsrcname,id_t id,uint_t flags,char ** errstr,rcm_info_t ** dependent)181 cluster_resume(rcm_handle_t *hdl, char *rsrcname, id_t id, uint_t flags,
182     char **errstr, rcm_info_t **dependent)
183 {
184 	return (RCM_SUCCESS);
185 }
186 
187 /*
188  * By default, reject offline. If offline request is
189  * forced, attempt to relocate the cluster device.
190  */
191 /*ARGSUSED*/
192 static int
cluster_offline(rcm_handle_t * hdl,char * rsrcname,id_t id,uint_t flags,char ** errstr,rcm_info_t ** dependent)193 cluster_offline(rcm_handle_t *hdl, char *rsrcname, id_t id, uint_t flags,
194     char **errstr, rcm_info_t **dependent)
195 {
196 	if ((*errstr = strdup(OS_OFFLINE_ERR)) == NULL)
197 		rcm_log_message(RCM_ERROR, gettext("strdup failure\n"));
198 
199 	return (RCM_FAILURE);
200 }
201 
202 /*ARGSUSED*/
203 static int
cluster_online(rcm_handle_t * hdl,char * rsrcname,id_t id,uint_t flags,char ** errstr,rcm_info_t ** dependent)204 cluster_online(rcm_handle_t *hdl, char *rsrcname, id_t id, uint_t flags,
205     char  **errstr, rcm_info_t **dependent)
206 {
207 	return (RCM_SUCCESS);
208 }
209 
210 /*ARGSUSED*/
211 static int
cluster_remove(rcm_handle_t * hdl,char * rsrcname,id_t id,uint_t flags,char ** errstr,rcm_info_t ** dependent)212 cluster_remove(rcm_handle_t *hdl, char *rsrcname, id_t id, uint_t flags,
213     char **errstr, rcm_info_t **dependent)
214 {
215 	if ((*errstr = strdup(OS_REMOVE_ERR)) == NULL)
216 		rcm_log_message(RCM_ERROR, gettext("strdup failure\n"));
217 
218 	return (RCM_FAILURE);
219 }
220