summaryrefslogtreecommitdiffstats
path: root/lib/plugins/stonith/riloe.c
blob: a4a831247e2d76ebc122121ee3ff85226e9ce49b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*
 * Stonith module for RILOE Stonith device
 *
 * Copyright (c) 2004 Alain St-Denis <alain.st-denis@ec.gc.ca>
 *
 * Mangled by Zhaokai <zhaokai@cn.ibm.com>, IBM, 2005
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#define	DEVICE	"Compaq RILOE"
#include "stonith_plugin_common.h"

#define PIL_PLUGIN              riloe
#define PIL_PLUGIN_S            "riloe"
#define PIL_PLUGINLICENSE 	LICENSE_LGPL
#define PIL_PLUGINLICENSEURL 	URL_LGPL
#include <pils/plugin.h>

static StonithPlugin *	riloe_new(const char *);
static void		riloe_destroy(StonithPlugin *);
static int		riloe_set_config(StonithPlugin *, StonithNVpair *);
static const char * const *	riloe_get_confignames(StonithPlugin * );
static const char *	riloe_getinfo(StonithPlugin * s, int InfoType);
static int		riloe_status(StonithPlugin * );
static int		riloe_reset_req(StonithPlugin * s, int request, const char * host);
static char **		riloe_hostlist(StonithPlugin  *);

static struct stonith_ops riloeOps ={
	riloe_new,		/* Create new STONITH object		*/
	riloe_destroy,		/* Destroy STONITH object		*/
	riloe_getinfo,		/* Return STONITH info string		*/
	riloe_get_confignames,	/* Return STONITH info string		*/
	riloe_set_config,	/* Get configuration from NVpairs	*/
	riloe_status,		/* Return STONITH device status		*/
	riloe_reset_req,	/* Request a reset 			*/
	riloe_hostlist,		/* Return list of supported hosts 	*/
};

PIL_PLUGIN_BOILERPLATE2("1.0", Debug)
static const PILPluginImports*  PluginImports;
static PILPlugin*               OurPlugin;
static PILInterface*		OurInterface;
static StonithImports*		OurImports;
static void*			interfprivate;

PIL_rc
PIL_PLUGIN_INIT(PILPlugin*us, const PILPluginImports* imports);

PIL_rc
PIL_PLUGIN_INIT(PILPlugin*us, const PILPluginImports* imports)
{
	/* Force the compiler to do a little type checking */
	(void)(PILPluginInitFun)PIL_PLUGIN_INIT;

	PluginImports = imports;
	OurPlugin = us;

	/* Register ourself as a plugin */
	imports->register_plugin(us, &OurPIExports);  

	/*  Register our interface implementation */
 	return imports->register_interface(us, PIL_PLUGINTYPE_S
	,	PIL_PLUGIN_S
	,	&riloeOps
	,	NULL		/*close */
	,	&OurInterface
	,	(void*)&OurImports
	,	&interfprivate); 
}

#define RILOE_COMMAND   STONITH_MODULES "/ribcl.py"

/*
 *	Riloe STONITH device.  We are very agreeable, but don't do much :-)
 */

struct pluginDevice {
	StonithPlugin	sp;
	const char *	pluginid;
	const char *	idinfo;
	char **		hostlist;
	int		hostcount;
};

static const char * pluginid = "RiloeDevice-Stonith";
static const char * NOTriloeID = "Riloe device has been destroyed";

#include "stonith_config_xml.h"

static const char *riloeXML = 
  XML_PARAMETERS_BEGIN
    XML_HOSTLIST_PARM
  XML_PARAMETERS_END;

static int
riloe_status(StonithPlugin  *s)
{

	if (Debug) {
		LOG(PIL_DEBUG, "%s:called.", __FUNCTION__);
	}

	ERRIFWRONGDEV(s,S_OOPS);
	return S_OK;
}


/*
 *	Return the list of hosts configured for this RILOE device
 */

static char **
riloe_hostlist(StonithPlugin  *s)
{
	struct pluginDevice*	nd;

	if (Debug) {
		LOG(PIL_DEBUG, "%s:called.", __FUNCTION__);
	}

	ERRIFWRONGDEV(s,NULL);
	nd = (struct pluginDevice*) s;
	if (nd->hostcount < 0) {
		LOG(PIL_CRIT
		,	"unconfigured stonith object in %s", __FUNCTION__);
		return(NULL);
	}

	return OurImports->CopyHostList((const char * const*)nd->hostlist);
}

/*
 *	Parse the config information, and stash it away...
 */

static int
RILOE_parse_config_info(struct pluginDevice* nd, const char * info)
{
	if (Debug) {
		LOG(PIL_DEBUG, "%s:called.", __FUNCTION__);
	}

	if (nd->hostcount >= 0) {
		return(S_OOPS);
	}

	nd->hostlist = OurImports->StringToHostList(info);
	if (nd->hostlist == NULL) {
		LOG(PIL_CRIT,"StringToHostList() failed");
		return S_OOPS;
	}
	for (nd->hostcount = 0; nd->hostlist[nd->hostcount]; nd->hostcount++) {
		strdown(nd->hostlist[nd->hostcount]);
	}
	return(S_OK);
}


/*
 *	Pretend to reset the given host on this Stonith device.
 *	(we don't even error check the "request" type)
 */
static int
riloe_reset_req(StonithPlugin * s, int request, const char * host)
{
	char cmd[4096];

	if (Debug) {
		LOG(PIL_DEBUG, "%s:called.", __FUNCTION__);
	}

	ERRIFWRONGDEV(s,S_OOPS);
	
	if (Debug) {
		LOG(PIL_DEBUG, "%s:called.", __FUNCTION__);
	}
	
	snprintf(cmd, sizeof(cmd), "%s %s reset", RILOE_COMMAND, host);
	
	if (Debug) {
		LOG(PIL_DEBUG, "command %s will be executed", cmd);
	}

	if (system(cmd) == 0) {
		return S_OK;
	} else {
		LOG(PIL_CRIT, "command %s failed", cmd);
		return(S_RESETFAIL);
	}
}

/*
 *	Parse the information in the given string,
 *	and stash it away...
 */
static int
riloe_set_config(StonithPlugin* s, StonithNVpair *list)
{
	StonithNamesToGet	namestocopy [] =
	{	{ST_HOSTLIST,	NULL}
	,	{NULL,		NULL}
	};
	struct pluginDevice*	nd;
	int rc;

	if (Debug) {
		LOG(PIL_DEBUG, "%s:called.", __FUNCTION__);
	}

	ERRIFWRONGDEV(s,S_OOPS);
	nd = (struct pluginDevice*) s;
	
	if ((rc = OurImports->CopyAllValues(namestocopy, list)) != S_OK) {
		return rc;
	}
	
	rc = RILOE_parse_config_info(nd , namestocopy[0].s_value);
	FREE(namestocopy[0].s_value);
	return rc;
}

/*
 *  Return the  Stonith plugin configuration parameter
 */
static const char* const *
riloe_get_confignames(StonithPlugin* p)
{
	static const char *	RiloeParams[] = {ST_HOSTLIST, NULL };

	if (Debug) {
		LOG(PIL_DEBUG, "%s:called.", __FUNCTION__);
	}

	return RiloeParams;
}

/*
 * Return STONITH info string
 */

static const char *
riloe_getinfo(StonithPlugin * s, int reqtype)
{
	struct pluginDevice* nd;
	const char * ret;

	if (Debug) {
		LOG(PIL_DEBUG, "%s:called.", __FUNCTION__);
	}

	ERRIFWRONGDEV(s,NULL);
	/*
	 *	We look in the ST_TEXTDOMAIN catalog for our messages
	 */
	nd = (struct pluginDevice *)s;

	switch (reqtype) {
		case ST_DEVICEID:
			ret = nd->idinfo;
			break;
		case ST_DEVICEDESCR:
			ret = "Compaq RILOE STONITH device\n"
			"Very early version!";
			break;
		case ST_DEVICEURL:
			ret = "http://www.hp.com/";
			break;
		case ST_CONF_XML:		/* XML metadata */
			ret = riloeXML;
			break;
		default:
			ret = NULL;
			break;
	}
	return ret;
}

/*
 *	RILOE Stonith destructor...
 */
static void
riloe_destroy(StonithPlugin *s)
{
	struct pluginDevice* nd;

	if (Debug) {
		LOG(PIL_DEBUG, "%s:called.", __FUNCTION__);
	}

	VOIDERRIFWRONGDEV(s);
	nd = (struct pluginDevice *)s;

	nd->pluginid = NOTriloeID;
	if (nd->hostlist) {
		stonith_free_hostlist(nd->hostlist);
		nd->hostlist = NULL;
	}
	nd->hostcount = -1;
	FREE(nd);
}

/* Create a new Riloe Stonith device.  Too bad this function can't be static */
static StonithPlugin *
riloe_new(const char *subplugin)
{
	struct pluginDevice*	nd = ST_MALLOCT(struct pluginDevice);

	if (Debug) {
		LOG(PIL_DEBUG, "%s:called.", __FUNCTION__);
	}

	if (nd == NULL) {
		LOG(PIL_CRIT, "out of memory");
		return(NULL);
	}
	memset(nd, 0, sizeof(*nd));
	nd->pluginid = pluginid;
	nd->hostlist = NULL;
	nd->hostcount = -1;
	nd->idinfo = DEVICE;
	nd->sp.s_ops = &riloeOps;

	return &(nd->sp);
}