?? rps10.c
字號:
* * e.g. A machine named 'nodea' can kill a machine named 'nodeb' through * a device attached to serial port /dev/ttyS0. * A machine named 'nodeb' can kill machines 'nodea' and 'nodec' * through a device attached to serial port /dev/ttyS1 (outlets 0 * and 1 respectively) * * <assuming this is the heartbeat configuration syntax:> * * stonith nodea rps10 /dev/ttyS0 nodeb 0 * stonith nodeb rps10 /dev/ttyS0 nodea 0 nodec 1 * * Another possible configuration is for 2 stonith devices * accessible through 2 different serial ports on nodeb: * * stonith nodeb rps10 /dev/ttyS0 nodea 0 * stonith nodeb rps10 /dev/ttyS1 nodec 0 *//* * OOPS! * * Most of the large block of comments above is incorrect as far as this * module is concerned. It is somewhat applicable to the heartbeat code, * but not to this Stonith module. * * The format of parameter string for this module is: * <serial device> <remotenode> <outlet> [<remotenode> <outlet>] ... */static intRPS_parse_config_info(struct pluginDevice* ctx, const char * info){ char *copy; char *token; char *outlet, *node; if (Debug) { LOG(PIL_DEBUG, "%s:called.", __FUNCTION__); } if (ctx->config) { /* The module is already configured. */ return(S_OOPS); } /* strtok() is nice to use to parse a string with (other than it isn't threadsafe), but it is destructive, so we're going to alloc our own private little copy for the duration of this function. */ copy = STRDUP(info); if (!copy) { LOG(PIL_CRIT, "out of memory"); return S_OOPS; } /* Grab the serial device */ token = strtok (copy, " \t"); if (!token) { LOG(PIL_CRIT, "%s: Can't find serial device on config line '%s'", pluginid, info); goto token_error; } ctx->device = STRDUP(token); if (!ctx->device) { LOG(PIL_CRIT, "out of memory"); goto token_error; } /* Loop through the rest of the command line which should consist of */ /* <nodename> <outlet> pairs */ while ((node = strtok (NULL, " \t")) && (outlet = strtok (NULL, " \t\n"))) { char outlet_id; /* validate the outlet token */ if ((sscanf (outlet, "%c", &outlet_id) != 1) || !( ((outlet_id >= '0') && (outlet_id <= '9')) || (outlet_id == '*') || (outlet_id == 'A') ) ) { LOG(PIL_CRIT , "%s: the outlet_id %s must be between" " 0 and 9 or '*' / 'A'", pluginid, outlet); goto token_error; } if (outlet_id == 'A') { /* Remap 'A' to '*'; in some configurations, * a '*' can't be configured because it breaks * scripts -- lmb */ outlet_id = '*'; } if (ctx->unit_count >= WTI_NUM_CONTROLLERS) { LOG(PIL_CRIT, "%s: Tried to configure too many controllers", pluginid); goto token_error; } ctx->controllers[ctx->unit_count].node = STRDUP(node); g_strdown(ctx->controllers[ctx->unit_count].node); ctx->controllers[ctx->unit_count].outlet_id = outlet_id; ctx->unit_count++; } /* free our private copy of the string we've been destructively * parsing with strtok() */ FREE(copy); ctx->config = 1; return ((ctx->unit_count > 0) ? S_OK : S_BADCONFIG);token_error: FREE(copy); return(S_BADCONFIG);}/* * dtrtoggle - toggle DTR on the serial port * * snarfed from minicom, sysdep1.c, a well known POSIX trick. * */static void dtrtoggle(int fd) { struct termios tty, old; int sec = 2; if (Debug) { LOG(PIL_DEBUG, "%s:called.", __FUNCTION__); } if (gbl_debug) { printf ("Calling dtrtoggle (%s)\n", pluginid); } tcgetattr(fd, &tty); tcgetattr(fd, &old); cfsetospeed(&tty, B0); cfsetispeed(&tty, B0); tcsetattr(fd, TCSANOW, &tty); if (sec>0) { sleep(sec); tcsetattr(fd, TCSANOW, &old); } if (gbl_debug) { printf ("dtrtoggle Complete (%s)\n", pluginid); }}/* * RPSConnect - * * Connect to the given WTI_RPS10 device. * Side Effects * DTR on the serial port is toggled * ctx->fd now contains a valid file descriptor to the serial port * ??? LOCK THE SERIAL PORT ??? * * Returns * S_OK on success * S_OOPS on error * S_TIMEOUT if the device did not respond * */static intRPSConnect(struct pluginDevice * ctx){ if (Debug) { LOG(PIL_DEBUG, "%s:called.", __FUNCTION__); } /* Open the serial port if it isn't already open */ if (ctx->fd < 0) { struct termios tio; ctx->fd = open (ctx->device, O_RDWR); if (ctx->fd <0) { LOG(PIL_CRIT, "%s: Can't open %s : %s", pluginid, ctx->device, strerror(errno)); return S_OOPS; } /* set the baudrate to 9600 8 - N - 1 */ memset (&tio, 0, sizeof(tio)); /* ??? ALAN - the -tradtitional flag on gcc causes the CRTSCTS constant to generate a warning, and warnings are treated as errors, so I can't set this flag! - EZA ??? Hmmm. now that I look at the documentation, RTS is just wired high on this device! we don't need it. */ /* tio.c_cflag = B9600 | CS8 | CLOCAL | CREAD | CRTSCTS ;*/ tio.c_cflag = B9600 | CS8 | CLOCAL | CREAD ; tio.c_lflag = ICANON; if (tcsetattr (ctx->fd, TCSANOW, &tio) < 0) { LOG(PIL_CRIT, "%s: Can't set attributes %s : %s", pluginid, ctx->device, strerror(errno)); close (ctx->fd); ctx->fd=-1; return S_OOPS; } /* flush all data to and fro the serial port before we start */ if (tcflush (ctx->fd, TCIOFLUSH) < 0) { LOG(PIL_CRIT, "%s: Can't flush %s : %s", pluginid, ctx->device, strerror(errno)); close (ctx->fd); ctx->fd=-1; return S_OOPS; } } /* Toggle DTR - this 'resets' the controller serial port interface In minicom, try CTRL-A H to hangup and you can see this behavior. */ dtrtoggle(ctx->fd); /* Wait for the switch to respond with "RPS-10 Ready". Emperically, this usually takes 5-10 seconds... ... If this fails, this may be a hint that you got a broken serial cable, which doesn't connect hardware flow control. */ if (gbl_debug) { printf ("Waiting for READY\n"); } EXPECT(ctx->fd, WTItokReady, 12); if (gbl_debug) { printf ("Got READY\n"); } EXPECT(ctx->fd, WTItokCRNL, 2); if (gbl_debug) { printf ("Got NL\n"); } return(S_OK);}static intRPSDisconnect(struct pluginDevice * ctx){ if (Debug) { LOG(PIL_DEBUG, "%s:called.", __FUNCTION__); } if (ctx->fd >= 0) { /* Flush the serial port, we don't care what happens to the characters and failing to do this can cause close to hang. */ tcflush(ctx->fd, TCIOFLUSH); close (ctx->fd); } ctx->fd = -1; return S_OK;} /* * RPSNametoOutlet - Map a hostname to an outlet on this stonith device. * * Returns: * 0-9, * on success ( the outlet id on the RPS10 ) * -1 on failure (host not found in the config file) * */static signed charRPSNametoOutlet ( struct pluginDevice * ctx, const char * host ){ int i=0; char *shost; if (Debug) { LOG(PIL_DEBUG, "%s:called.", __FUNCTION__); } if ( (shost = STRDUP(host)) == NULL) { LOG(PIL_CRIT, "strdup failed in RPSNametoOutlet"); return -1; } g_strdown(shost); /* scan the controllers[] array to see if this host is there */ for (i=0;i<ctx->unit_count;i++) { /* return the outlet id */ if ( ctx->controllers[i].node && !strcmp(host, ctx->controllers[i].node)) { /* found it! */ break; } } free(shost); if (i == ctx->unit_count) { return -1; } else { return ctx->controllers[i].outlet_id; }}/* * rps10_reset - API call to Reset (reboot) the given host on * this Stonith device. This involves toggling the power off * and then on again, OR just calling the builtin reset command * on the stonith device. */static intrps10_reset_req(StonithPlugin * s, int request, const char * host){ int rc = S_OK; int lorc = S_OK; signed char outlet_id = -1; struct pluginDevice* ctx; if (Debug) { LOG(PIL_DEBUG, "%s:called.", __FUNCTION__); } if (gbl_debug) { printf ("Calling rps10_reset (%s)\n", pluginid); } ERRIFNOTCONFIGED(s,S_OOPS); ctx = (struct pluginDevice*) s; if ((rc = RPSConnect(ctx)) != S_OK) { return(rc); } outlet_id = RPSNametoOutlet(ctx, host); if (outlet_id < 0) { LOG(PIL_WARN, "%s %s %s[%s]" , ctx->idinfo, ctx->unitid , _("doesn't control host"), host ); RPSDisconnect(ctx); return(S_BADHOST); } switch(request) {#if defined(ST_POWERON) case ST_POWERON: rc = RPSOn(ctx, outlet_id, host); break;#endif#if defined(ST_POWEROFF) case ST_POWEROFF: rc = RPSOff(ctx, outlet_id, host); break;#endif case ST_GENERIC_RESET: rc = RPSReset(ctx, outlet_id, host); break; default: rc = S_INVAL; break; } lorc = RPSDisconnect(ctx); return(rc != S_OK ? rc : lorc);}/* * Parse the information in the given string, * and stash it away... */static intrps10_set_config(StonithPlugin* s, StonithNVpair* list){ char RPSid[MAX_PRSID]; struct pluginDevice* ctx; StonithNamesToGet namestoget [] = { {ST_RPS10, NULL} , {NULL, NULL} }; int rc=0; if (Debug) { LOG(PIL_DEBUG, "%s:called.", __FUNCTION__); } ERRIFWRONGDEV(s,S_OOPS); ctx = (struct pluginDevice*) s; if((rc = OurImports->GetAllValues(namestoget , list)) != S_OK){ LOG(PIL_DEBUG , "get all calues failed"); return rc; } if ((snprintf(RPSid, MAX_PRSID, "%s", namestoget[0].s_value)) <= 0) { LOG(PIL_CRIT, "Can not copy parameter to PRSid"); } return (RPS_parse_config_info(ctx,RPSid)); }/* * Return the Stonith plugin configuration parameter * */static const char**rps10_get_confignames(StonithPlugin* p){ static const char * Rps10Params[] = {ST_RPS10 ,NULL }; if (Debug) { LOG(PIL_DEBUG, "%s:called.", __FUNCTION__); } return Rps10Params;}/* * rps10_getinfo - API entry point to retrieve something from the handle */static const char *rps10_getinfo(StonithPlugin * s, int reqtype){ struct pluginDevice* ctx; const char * ret; if (Debug) { LOG(PIL_DEBUG, "%s:called.", __FUNCTION__); } ERRIFWRONGDEV(s,NULL); /* * We look in the ST_TEXTDOMAIN catalog for our messages */ ctx = (struct pluginDevice *)s; switch (reqtype) { case ST_DEVICEID: ret = ctx->idinfo; break; case ST_DEVICEDESCR: ret = _("Western Telematic Inc. (WTI) " "Remote Power Switch - RPS-10M.\n"); break; default: ret = NULL; break; } return ret;}/* * rps10_destroy - API entry point to destroy a WTI_RPS10 Stonith object. */static voidrps10_destroy(StonithPlugin *s){ struct pluginDevice* ctx; if (Debug) { LOG(PIL_DEBUG, "%s:called.", __FUNCTION__); } VOIDERRIFWRONGDEV(s); ctx = (struct pluginDevice *)s; ctx->pluginid = NOTwtiid; /* close the fd if open and set ctx->fd to invalid */ RPSDisconnect(ctx); if (ctx->device != NULL) { FREE(ctx->device); ctx->device = NULL; } if (ctx->idinfo != NULL) { FREE(ctx->idinfo); ctx->idinfo = NULL; } if (ctx->unitid != NULL) { FREE(ctx->unitid); ctx->unitid = NULL; }}/* * rps10_new - API entry point called to create a new WTI_RPS10 Stonith device * object. */static StonithPlugin * rps10_new(void){ struct pluginDevice* ctx = MALLOCT(struct pluginDevice); if (Debug) { LOG(PIL_DEBUG, "%s:called.", __FUNCTION__); } if (ctx == NULL) { LOG(PIL_CRIT, "out of memory"); return(NULL); } memset(ctx, 0, sizeof(*ctx)); ctx->pluginid = pluginid; ctx->fd = -1; ctx->config = 0; ctx->unit_count = 0; ctx->device = NULL; ctx->idinfo = NULL; ctx->unitid = NULL; REPLSTR(ctx->idinfo, DEVICE); REPLSTR(ctx->unitid, "unknown"); ctx->sp.s_ops = &rps10Ops; return &(ctx->sp);}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -