⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 nw_rpc100s.c

📁 在LINUX下实现HA的源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/* $Id: nw_rpc100s.c,v 1.10.2.1 2004/04/20 08:50:06 alan Exp $ *//* *	Stonith module for Night/Ware RPC100S  * *      Original code from baytech.c by *	Copyright (c) 2000 Alan Robertson <alanr@unix.sh> * *      Modifications for NW RPC100S *	Copyright (c) 2000 Computer Generation Incorporated *               Eric Z. Ayers <eric.ayers@compgen.com> * * 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 <termios.h>#include <sys/time.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <glib.h>#include <stonith/stonith.h>#define PIL_PLUGINTYPE          STONITH_TYPE#define PIL_PLUGINTYPE_S        STONITH_TYPE_S#define PIL_PLUGIN              nw_rpc100s#define PIL_PLUGIN_S            "nw_rpc100s"#define PIL_PLUGINLICENSE 	LICENSE_LGPL#define PIL_PLUGINLICENSEURL 	URL_LGPL#include <pils/plugin.h>/* * nw_rpc100sclose is called as part of unloading the nw_rpc100s 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 voidnw_rpc100sclosepi(PILPlugin*pi){}/* * nw_rpc100scloseintf called as part of shutting down the nw_rpc100s STONITH * interface.  If there was any global data allocated, or file descriptors * opened, etc.  which is associated with the nw_rpc100s implementation, * here's our chance to clean it up. */static PIL_rcnw_rpc100scloseintf(PILInterface* pi, void* pd){	return PIL_OK;}static void *		nw_rpc100s_new(void);static void		nw_rpc100s_destroy(Stonith *);static int		nw_rpc100s_set_config_file(Stonith *, const char * cfgname);static int		nw_rpc100s_set_config_info(Stonith *, const char * info);static const char *	nw_rpc100s_getinfo(Stonith * s, int InfoType);static int		nw_rpc100s_status(Stonith * );static int		nw_rpc100s_reset_req(Stonith * s, int request, const char * host);static char **		nw_rpc100s_hostlist(Stonith  *);static void		nw_rpc100s_free_hostlist(char **);static struct stonith_ops nw_rpc100sOps ={	nw_rpc100s_new,		/* Create new STONITH object	*/	nw_rpc100s_destroy,		/* Destroy STONITH object	*/	nw_rpc100s_set_config_file,	/* set configuration from file	*/	nw_rpc100s_set_config_info,	/* Get configuration from file	*/	nw_rpc100s_getinfo,		/* Return STONITH info string	*/	nw_rpc100s_status,		/* Return STONITH device status	*/	nw_rpc100s_reset_req,		/* Request a reset */	nw_rpc100s_hostlist,		/* Return list of supported hosts */	nw_rpc100s_free_hostlist	/* free above list */};PIL_PLUGIN_BOILERPLATE("1.0", Debug, nw_rpc100sclosepi);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 STRDUP  	PluginImports->mstrdup#define FREE		PluginImports->mfree#define EXPECT_TOK	OurImports->ExpectToken#define STARTPROC	OurImports->StartProcessPIL_rcPIL_PLUGIN_INIT(PILPlugin*us, const PILPluginImports* imports);PIL_rcPIL_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	,	&nw_rpc100sOps	,	nw_rpc100scloseintf		/*close */	,	&OurInterface	,	(void*)&OurImports	,	&interfprivate); }#define	DEVICE	"NW RPC100S Power Switch"#define N_(text)	(text)#define _(text)		dgettext(ST_TEXTDOMAIN, text)/*   The Nightware RPS-100S is manufactured by:         Micro Energetics Corp      +1 703 250-3000      http://www.nightware.com/   Thank you to David Hicks of Micro Energetics Corp. for providing   a demo unit to write this software.         This switch has a very simple protocol,    You issue a command and it  gives a response.   Sample commands are conveniently documented on a sticker on the      bottom of the device.         The switch accepts a single command of the form   //0,yyy,zzz[/m][/h]<CR>        Where yyy is the wait time before activiting the relay.           zzz is the relay time.     The default is that the relay is in a default state of ON, which     means that  usually yyy is the number of seconds to wait     before shutting off the power  and zzz is the number of seconds the     power remains off.  There is a dip switch to change the default     state to 'OFF'.  Don't set this switch. It will screw up this code.      An asterisk can be used for zzz to specify an infinite switch time.     The /m /and /h command options will convert the specified wait and     switch times to either minutewes or hours.       A response is either    <cr><lf>OK<cr><lf>       or    <cr><lf>Invalid Entry<cr><lf>   As far as THIS software is concerned, we have to implement 4 commands:   status     -->    //0,0,BOGUS; # Not a real command, this is just a                                  #   probe to see if switch is alive   open(on)   -->    //0,0,0;     # turn power to default state (on)   close(off) -->    //0,0,*;     # leave power off indefinitely   reboot     -->    //0,0,10;    # immediately turn power off for 10 seconds.   and expect the response 'OK' to confirm that the unit is operational.*/struct NW_RPC100S {	const char *	RPCid;	char *	idinfo;  /* ??? What's this for Alan ??? */	char *	unitid;  /* ??? What's this for Alan ??? */	int	fd;      /* FD open to the serial port */	int	config;  /* 0 if not configured, 				    1 if configured with nw_rpc100s_set_config_file() 				    or nw_rpc100s_set_config_info()				 */	char *	device;  /* Serial device name to use to communicate                             to this RPS10			 */	char *  node;    /* Name of the node that this is controlling */};/* This string is used to identify this type of object in the config file */static const char * RPCid = "NW_RPC100S";static const char * NOTrpcid = "OBJECT DESTROYED: (NW RPC100S)";#ifndef DEBUG#define DEBUG 0#endifstatic int gbl_debug = DEBUG;#define	ISNWRPC100S(i)	(((i)!= NULL && (i)->pinfo != NULL)	\	&& ((struct NW_RPC100S *)(i->pinfo))->RPCid == RPCid)#define	ISCONFIGED(i)	(ISNWRPC100S(i) && ((struct NW_RPC100S *)(i->pinfo))->config)#ifndef MALLOCT#	define     MALLOCT(t)      ((t *)(MALLOC(sizeof(t)))) #endif#define WHITESPACE	" \t\n\r\f"#define	REPLSTR(s,v)	{					\			if ((s) != NULL) {			\				FREE(s);			\				(s)=NULL;			\			}					\			(s) = STRDUP(v);			\			if ((s) == NULL) {			\				syslog(LOG_ERR, _("out of memory"));\			}					\			}/* *	Different expect strings that we get from the NW_RPC100S *	Remote Power Controllers... */static struct Etoken NWtokOK[] =	{ {"OK", 0, 0}, {NULL,0,0}};static struct Etoken NWtokInvalidEntry[] = { {"Invalid Entry", 0, 0}, {NULL,0,0}};/* Accept either a CR/NL or an NL/CR */static struct Etoken NWtokCRNL[] =	{ {"\n\r",0,0},{"\r\n",0,0},{NULL,0,0}};static int	RPCConnect(struct NW_RPC100S * ctx);static int	RPCDisconnect(struct NW_RPC100S * ctx);static int	RPCReset(struct NW_RPC100S*, int unitnum, const char * rebootid);#if defined(ST_POWERON) static int	RPCOn(struct NW_RPC100S*, int unitnum, const char * rebootid);#endif#if defined(ST_POWEROFF) static int	RPCOff(struct NW_RPC100S*, int unitnum, const char * rebootid);#endifstatic int	RPCNametoOutlet ( struct NW_RPC100S * ctx, const char * host );static int RPC_parse_config_info(struct NW_RPC100S* ctx, const char * info);/* *	We do these things a lot.  Here are a few shorthand macros. */#define	SEND(cmd, timeout)		{                       \	int return_val = RPCSendCommand(ctx, cmd, timeout);     \	if (return_val != S_OK)  return return_val;                     \}#define	EXPECT(p,t)	{						\			if (RPCLookFor(ctx, p, t) < 0)			\				return(errno == ETIMEDOUT		\			?	S_TIMEOUT : S_OOPS);			\			}#define	NULLEXPECT(p,t)	{						\				if (RPCLookFor(ctx, p, t) < 0)		\					return(NULL);			\			}#define	SNARF(s, to)	{						\				if (RPCScanLine(ctx,to,(s),sizeof(s))	\				!=	S_OK)				\					return(S_OOPS);			\			}#define	NULLSNARF(s, to)	{					\				if (RPCScanLine(ctx,to,(s),sizeof(s))	\				!=	S_OK)				\					return(NULL);			\				}/* Look for any of the given patterns.  We don't care which */static intRPCLookFor(struct NW_RPC100S* ctx, struct Etoken * tlist, int timeout){	int	rc;	if ((rc = EXPECT_TOK(ctx->fd, tlist, timeout, NULL, 0)) < 0) {		syslog(LOG_ERR, _("Did not find string: '%s' from" DEVICE ".")		,	tlist[0].string);		RPCDisconnect(ctx);		return(-1);	}	return(rc);}/* * RPCSendCommand - send a command to the specified outlet */static intRPCSendCommand (struct NW_RPC100S *ctx, const char *command, int timeout){	char            writebuf[64]; /* All commands are short.					 They should be WAY LESS					 than 64 chars long!				      */	int		return_val;  /* system call result */	fd_set          rfds, wfds, xfds;				     /*  list of FDs for select() */	struct timeval 	tv;	     /*  */	FD_ZERO(&rfds);	FD_ZERO(&wfds);	FD_ZERO(&xfds);	snprintf (writebuf, sizeof(writebuf), "%s\r", command);	if (gbl_debug) printf ("Sending %s\n", writebuf);	/* Make sure the serial port won't block on us. use select()  */	FD_SET(ctx->fd, &wfds);	FD_SET(ctx->fd, &xfds);		tv.tv_sec = timeout;	tv.tv_usec = 0;		return_val = select(ctx->fd+1, NULL, &wfds,&xfds, &tv);	if (return_val == 0) {		/* timeout waiting on serial port */		syslog (LOG_ERR, "%s: Timeout writing to %s",			RPCid, ctx->device);		return S_TIMEOUT;	} else if ((return_val == -1) || FD_ISSET(ctx->fd, &xfds)) {		/* an error occured */		syslog (LOG_ERR, "%s: Error before writing to %s: %s",			RPCid, ctx->device, strerror(errno));				return S_OOPS;	}	/* send the command */	if (write(ctx->fd, writebuf, strlen(writebuf)) != 			(int)strlen(writebuf)) {		syslog (LOG_ERR, "%s: Error writing to  %s : %s",			RPCid, ctx->device, strerror(errno));		return S_OOPS;	}	/* suceeded! */	return S_OK;}  /* end RPCSendCommand() *//*  * RPCReset - Reset (power-cycle) the given outlet number * * This device can only control one power outlet - unitnum is ignored. * */static intRPCReset(struct NW_RPC100S* ctx, int unitnum, const char * rebootid){	if (gbl_debug) printf ("Calling RPCReset (%s)\n", RPCid);		if (ctx->fd < 0) {		syslog(LOG_ERR, "%s: device %s is not open!", RPCid, 		       ctx->device);		return S_OOPS;	}	/* send the "toggle power" command */	SEND("//0,0,10;\r\n", 12);	/* Expect "OK" */	EXPECT(NWtokOK, 5);	if (gbl_debug) printf ("Got OK\n");	EXPECT(NWtokCRNL, 2);	if (gbl_debug) printf ("Got NL\n");		return(S_OK);} /* end RPCReset() */#if defined(ST_POWERON) /*  * RPCOn - Turn OFF the given outlet number  */static intRPCOn(struct NW_RPC100S* ctx, int unitnum, const char * host){	if (ctx->fd < 0) {		syslog(LOG_ERR, "%s: device %s is not open!", RPCid, 		       ctx->device);		return S_OOPS;	}	/* send the "On" command */	SEND("//0,0,0;\r\n", 10);	/* Expect "OK" */	EXPECT(NWtokOK, 5);	EXPECT(NWtokCRNL, 2);	return(S_OK);} /* end RPCOn() */#endif#if defined(ST_POWEROFF) /*  * RPCOff - Turn Off the given outlet number  */static intRPCOff(struct NW_RPC100S* ctx, int unitnum, const char * host){	if (ctx->fd < 0) {		syslog(LOG_ERR, "%s: device %s is not open!", RPCid, 		       ctx->device);		return S_OOPS;	}	/* send the "Off" command */	SEND("//0,0,*;\r\n", 10);	/* Expect "OK" */	EXPECT(NWtokOK, 5);	EXPECT(NWtokCRNL, 2);	return(S_OK);} /* end RPCOff() */#endif/* * nw_rpc100s_status - API entry point to probe the status of the stonith device  *           (basically just "is it reachable and functional?", not the *            status of the individual outlets) *  * Returns: *    S_OOPS - some error occured *    S_OK   - if the stonith device is reachable and online. */static intnw_rpc100s_status(Stonith  *s){	struct NW_RPC100S*	ctx;		if (gbl_debug) printf ("Calling nw_rpc100s_status (%s)\n", RPCid);		if (!ISNWRPC100S(s)) {		syslog(LOG_ERR, "invalid argument to RPC_status");		return(S_OOPS);	}	if (!ISCONFIGED(s)) {		syslog(LOG_ERR		,	"unconfigured stonith object in RPC_status");		return(S_OOPS);	}	ctx = (struct NW_RPC100S*) s->pinfo;	if (RPCConnect(ctx) != S_OK) {		return(S_OOPS);	}	/* The "connect" really does enough work to see if the 	   controller is alive...  It verifies that it is returning 	   RPS-10 Ready 	*/	return(RPCDisconnect(ctx));}/* * nw_rpc100s_hostlist - API entry point to return the list of hosts  *                 for the devices on this NW_RPC100S unit

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -