[prev in list] [next in list] [prev in thread] [next in thread] 

List:       linux-ha-dev
Subject:    [Linux-ha-dev] script for creating new STONITH plugin
From:       "Zou, Yixiong" <yixiong.zou () intel ! com>
Date:       2003-08-18 23:52:51
[Download RAW message or body]

I am working on the IPMI STONITH plugin right now and I can see that 
many STONITH plugins just copy and paste each other.  So I think it
is a good idea to have this shell script that will generate a new
plugin file.  The advantage of using this script is that the comments
in the code will be consistent. :p 

The shell script is totally based on the null.c.  If you guys thinks
it's a good idea, I can check it in. 

------------------------------------------------------------------------
Yixiong Zou (yixiong.zou@intel.com)
(503) 677-4988

All views expressed in this email are those of the individual sender.  


["newplugin.sh" (application/octet-stream)]

#!/bin/bash

if [ $# -lt 1 ]; then
	echo 
	echo "Error: No plugin name specified."
	echo "Usage: newplugin pluginname"
	echo "	A file named pluginname.c will be created. You can use this file as the base"
	echo "	for the new STONITH plugin."
	echo
	exit
fi

if [ -e $1.c ]; then
	echo "$1.c already exists."
	echo "Please specify another name."
	exit
fi

cat > $1.c << EOF
/*
 * Stonith module for $1 Stonith device
 *
 * Copyright (c) 2000 Alan Robertson <alanr@unix.sh>
 *
 * 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
 *
 */

#include <portability.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <syslog.h>
#include <libintl.h>
#include <sys/wait.h>

#include <stonith/stonith.h>

#define PIL_PLUGINTYPE          STONITH_TYPE
#define PIL_PLUGINTYPE_S        STONITH_TYPE_S
#define PIL_PLUGIN              $1
#define PIL_PLUGIN_S            "$1"
#define PIL_PLUGINLICENSE 	LICENSE_LGPL
#define PIL_PLUGINLICENSEURL 	URL_LGPL
#include <pils/plugin.h>

/*
 * $1close is called as part of unloading the $1 STONITH plugin.
 * If there was any global data allocated, or file descriptors opened, etc.
 * which is associated with the plugin, and not a single interface
 * in particular, here's our chance to clean it up.
 */

static void
$1closepi(PILPlugin*pi)
{
}


/*
 * $1closeintf called as part of shutting down the $1 STONITH
 * interface.  If there was any global data allocated, or file descriptors
 * opened, etc.  which is associated with the $1 implementation,
 * here's our chance to clean it up.
 */
static PIL_rc
$1closeintf(PILInterface* pi, void* pd)
{
	return PIL_OK;
}

static void *		$1_new(void);
static void		$1_destroy(Stonith *);
static int		$1_set_config_file(Stonith *, const char * cfgname);
static int		$1_set_config_info(Stonith *, const char * info);
static const char *	$1_getinfo(Stonith * s, int InfoType);
static int		$1_status(Stonith * );
static int		$1_reset_req(Stonith * s, int request, const char * host);
static char **		$1_hostlist(Stonith  *);
static void		$1_free_hostlist(char **);

static struct stonith_ops $1Ops ={
	$1_new,		/* Create new STONITH object	*/
	$1_destroy,		/* Destroy STONITH object	*/
	$1_set_config_file,	/* set configuration from file	*/
	$1_set_config_info,	/* Get configuration from file	*/
	$1_getinfo,		/* Return STONITH info string	*/
	$1_status,		/* Return STONITH device status	*/
	$1_reset_req,		/* Request a reset */
	$1_hostlist,		/* Return list of supported hosts */
	$1_free_hostlist	/* free above list */
};

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

#define LOG		PluginImports->log
#define MALLOC		PluginImports->alloc
#define FREE		PluginImports->mfree
#define EXPECT_TOK	OurImports->ExpectToken
#define STARTPROC	OurImports->StartProcess

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
	,	&$1Ops
	,	$1closeintf		/*close */
	,	&OurInterface
	,	(void*)&OurImports
	,	&interfprivate); 
}

#define	DEVICE	"$1 STONITH device"
#define WHITESPACE	" \t\n\r\f"

/*
 *	$1 STONITH device.  
 */

struct $1Device {
	const char *	$1id;
	char **		hostlist;
	int		hostcount;
};

static const char * $1id = "$1Device-Stonith";
static const char * NOT$1ID = "Hey, dummy this has been destroyed ($1Dev)";

#define	IS$1DEV(i)	(((i)!= NULL && (i)->pinfo != NULL)	\\
	&& ((struct $1Device *)(i->pinfo))->$1id == $1id)


#ifndef MALLOC
#	define	MALLOC	malloc
#endif
#ifndef FREE
#	define	FREE	free
#endif
#ifndef MALLOCT
#	define     MALLOCT(t)      ((t *)(MALLOC(sizeof(t)))) 
#endif

#define N_(text)	(text)
#define _(text)		dgettext(ST_TEXTDOMAIN, text)


static int
$1_status(Stonith  *s)
{

	if (!IS$1DEV(s)) {
		syslog(LOG_ERR, "invalid argument to $1_status");
		return(S_OOPS);
	}
	return S_OK;
}


/*
 *	Return the list of hosts configured for this $1 device
 */

static char **
$1_hostlist(Stonith  *s)
{
	int		numnames = 0;
	char **		ret = NULL;
	struct $1Device*	nd;
	int		j;

	if (!IS$1DEV(s)) {
		syslog(LOG_ERR, "invalid argument to $1_list_hosts");
		return(NULL);
	}
	nd = (struct $1Device*) s->pinfo;
	if (nd->hostcount < 0) {
		syslog(LOG_ERR
		,	"unconfigured stonith object in $1_list_hosts");
		return(NULL);
	}
	numnames = nd->hostcount;

	ret = (char **)MALLOC(numnames*sizeof(char*));
	if (ret == NULL) {
		syslog(LOG_ERR, "out of memory");
		return ret;
	}

	memset(ret, 0, numnames*sizeof(char*));

	for (j=0; j < numnames-1; ++j) {
		ret[j] = MALLOC(strlen(nd->hostlist[j])+1);
		if (ret[j] == NULL) {
			$1_free_hostlist(ret);
			ret = NULL;
			return ret;
		}
		strcpy(ret[j], nd->hostlist[j]);
	}
	return(ret);
}

static void
$1_free_hostlist (char ** hlist)
{
	char **	hl = hlist;
	if (hl == NULL) {
		return;
	}
	while (*hl) {
		FREE(*hl);
		*hl = NULL;
		++hl;
	}
	FREE(hlist);
	hlist = NULL;
}


static int
WordCount(const char * s)
{
	int	wc = 0;
	if (!s) {
		return wc;
	}
	do {
		s += strspn(s, WHITESPACE);
		if (*s)  {
			++wc;
			s += strcspn(s, WHITESPACE);
		}
	}while (*s);

	return(wc);
}

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

static int
$1_parse_config_info(struct $1Device* nd, const char * info)
{
	char **			ret;
	int			wc;
	int			numnames;
	const char *		s = info;
	int			j;

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

	wc = WordCount(info);
	numnames = wc + 1;

	ret = (char **)MALLOC(numnames*sizeof(char*));
	if (ret == NULL) {
		syslog(LOG_ERR, "out of memory");
		return S_OOPS;
	}

	memset(ret, 0, numnames*sizeof(char*));

	for (j=0; j < wc; ++j) {
		s += strspn(s, WHITESPACE);
		if (*s)  {
			const char *	start = s;
			s += strcspn(s, WHITESPACE);
			ret[j] = MALLOC((1+(s-start))*sizeof(char));
			if (ret[j] == NULL) {
				$1_free_hostlist(ret);
				ret = NULL;
				return S_OOPS;
			}
			strncpy(ret[j], start, (s-start));
		}
	}
	nd->hostlist = ret;
	nd->hostcount = numnames;
	return(S_OK);
}


/*
 *	Pretend to reset the given host on this Stonith device.
 *	(we don't even error check the "request" type)
 */
static int
$1_reset_req(Stonith * s, int request, const char * host)
{

	if (!IS$1DEV(s)) {
		syslog(LOG_ERR, "invalid argument to %s", __FUNCTION__);
		return(S_OOPS);
	}

	/* Real devices need to pay attention to the "request" */
	/* (but we don't care ;-)) */

	syslog(LOG_INFO, _("Host %s $1-reset."), host);
	return S_OK;
}

/*
 *	Parse the information in the given configuration file,
 *	and stash it away...
 */
static int
$1_set_config_file(Stonith* s, const char * configname)
{
	FILE *	cfgfile;

	char	$1line[256];

	struct $1Device*	nd;

	if (!IS$1DEV(s)) {
		syslog(LOG_ERR, "invalid argument to $1_set_configfile");
		return(S_OOPS);
	}
	nd = (struct $1Device*) s->pinfo;

	if ((cfgfile = fopen(configname, "r")) == NULL)  {
		syslog(LOG_ERR, "Cannot open %s", configname);
		return(S_BADCONFIG);
	}
	while (fgets($1line, sizeof($1line), cfgfile) != NULL){
		if (*$1line == '#' || *$1line == '\n' || *$1line == EOS) {
			continue;
		}
		return($1_parse_config_info(nd, $1line));
	}
	return(S_BADCONFIG);
}

/*
 *	Parse the config information in the given string, and stash it away...
 */
static int
$1_set_config_info(Stonith* s, const char * info)
{
	struct $1Device* nd;

	if (!IS$1DEV(s)) {
		syslog(LOG_ERR, "%s: invalid argument", __FUNCTION__);
		return(S_OOPS);
	}
	nd = (struct $1Device *)s->pinfo;

	return($1_parse_config_info(nd, info));
}

static const char *
$1_getinfo(Stonith * s, int reqtype)
{
	struct $1Device* nd;
	char *		ret;

	if (!IS$1DEV(s)) {
		syslog(LOG_ERR, "$1_idinfo: invalid argument");
		return NULL;
	}
	/*
	 *	We look in the ST_TEXTDOMAIN catalog for our messages
	 */
	nd = (struct $1Device *)s->pinfo;

	switch (reqtype) {
		case ST_DEVICEID:
			ret = _("$1 STONITH device");
			break;

		case ST_CONF_INFO_SYNTAX:
			ret = _("hostname ...\n"
			"host names are white-space delimited.");
			break;

		case ST_CONF_FILE_SYNTAX:
			ret = _("hostname ...\n"
			"host names are white-space delimited.  "
			"All host names must be on one line.  "
			"Blank lines and lines beginning with # are ignored");
			break;

		case ST_DEVICEDESCR:
			ret = _("Dummy (do-nothing) STONITH device\n"
			"FOR TESTING ONLY!");
			break;

		default:
			ret = NULL;
			break;
	}
	return ret;
}

/*
 *	$1 Stonith destructor...
 */
static void
$1_destroy(Stonith *s)
{
	struct $1Device* nd;

	if (!IS$1DEV(s)) {
		syslog(LOG_ERR, "%s: invalid argument", __FUNCTION__);
		return;
	}
	nd = (struct $1Device *)s->pinfo;

	nd->$1id = NOT$1ID;
	if (nd->hostlist) {
		$1_free_hostlist(nd->hostlist);
		nd->hostlist = NULL;
	}
	nd->hostcount = -1;
	FREE(nd);
}

/* Create a new $1 Stonith device.  Too bad this function can't be static */
static void *
$1_new(void)
{
	struct $1Device*	nd = MALLOCT(struct $1Device);

	if (nd == NULL) {
		syslog(LOG_ERR, "out of memory");
		return(NULL);
	}
	memset(nd, 0, sizeof(*nd));
	nd->$1id = $1id;
	nd->hostlist = NULL;
	nd->hostcount = -1;
	return((void *)nd);
}

_______________________________________________________
Linux-HA-Dev: Linux-HA-Dev@lists.linux-ha.org
http://lists.linux-ha.org/mailman/listinfo/linux-ha-dev
Home Page: http://linux-ha.org/

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic