Пример #1
0
static int is_ups_critical(utype_t *ups)
{
    time_t	now;

    /* FSD = the master is forcing a shutdown */
    if (flag_isset(ups->status, ST_FSD))
        return 1;

    /* not OB or not LB = not critical yet */
    if ((!flag_isset(ups->status, ST_ONBATT)) ||
            (!flag_isset(ups->status, ST_LOWBATT)))
        return 0;

    /* must be OB+LB now */

    /* if we're a master, declare it critical so we set FSD on it */
    if (flag_isset(ups->status, ST_MASTER))
        return 1;

    /* must be a slave now */

    /* FSD isn't set, so the master hasn't seen it yet */

    time(&now);

    /* give the master up to HOSTSYNC seconds before shutting down */
    if ((now - ups->lastnoncrit) > hostsync) {
        upslogx(LOG_WARNING, "Giving up on the master for UPS [%s]",
                ups->sys);
        return 1;
    }

    /* there's still time left */
    return 0;
}
Пример #2
0
/* remember the last time the ups was not critical (OB + LB) */
static void update_crittimer(utype_t *ups)
{
    /* if !OB or !LB, then it's not critical, so log the time */
    if ((!flag_isset(ups->status, ST_ONBATT)) ||
            (!flag_isset(ups->status, ST_LOWBATT))) {

        time(&ups->lastnoncrit);
        return;
    }

    /* fallthrough: let the timer age */
}
Пример #3
0
static void forceshutdown(void)
{
    utype_t	*ups;
    int	isamaster = 0;

    upsdebugx(1, "Shutting down any UPSes in MASTER mode...");

    /* set FSD on any "master" UPS entries (forced shutdown in progress) */
    for (ups = firstups; ups != NULL; ups = ups->next)
        if (flag_isset(ups->status, ST_MASTER)) {
            isamaster = 1;
            setfsd(ups);
        }

    /* if we're not a master on anything, we should shut down now */
    if (!isamaster)
        doshutdown();

    /* must be the master now */
    upsdebugx(1, "This system is a master... waiting for slave logout...");

    /* wait up to HOSTSYNC seconds for slaves to logout */
    slavesync();

    /* time expired or all the slaves are gone, so shutdown */
    doshutdown();
}
Пример #4
0
// game logging to std error.. now with sprintf support
void mudlog(const char *str, ...)
{
	MSOCKET *sock;
	char logstr[MAX_BUFFER*2];
	char buf[MAX_BUFFER];
	char imm[MAX_BUFFER];
	va_list ap;
	static long numlogs=0;

	if(++numlogs > MUDLOG_LIMIT)
		return;

	va_start(ap, str);
	(void)vsnprintf(buf, MAX_BUFFER, str, ap);

	sprintf(imm,"&+M[LOG] &+W");
	strcat(imm,buf);
	for(sock = socket_list; sock; sock = sock->next)
	{
		if(sock->pc && (!IsPlaying(sock->pc) || !flag_isset(sock->pc->flags, CFLAG_NOTIFY)))
			continue;
		sendcrit(sock->pc, imm);
	}

	strftime(logstr, MAX_BUFFER, "%m/%d/%Y %H:%M:%S :: ", localtime(&current_time) );
	strcat(logstr,buf);

//	fprintf(stderr, "%s\n", wraptext(23,logstr,100));
	fprintf(stderr, "%s\n", logstr);

	va_end(ap);
}
Пример #5
0
inline void flag_reverse(long *flags, long bitnum)
{
	if(flag_isset(flags, bitnum))
		flag_remove(flags,bitnum);
	else
		flag_set(flags,bitnum);
}
Пример #6
0
static int warts_dealias_reply_read(scamper_dealias_reply_t *reply,
				    warts_state_t *state,
				    warts_addrtable_t *table,
				    uint8_t *buf, uint32_t *off, uint32_t len)
{
  warts_param_reader_t handlers[] = {
    {&reply->src,           (wpr_t)extract_addr_gid,              state},
    {&reply->rx,            (wpr_t)extract_timeval,               NULL},
    {&reply->ipid,          (wpr_t)extract_uint16,                NULL},
    {&reply->ttl,           (wpr_t)extract_byte,                  NULL},
    {reply,                 (wpr_t)extract_dealias_reply_icmptc,  NULL},
    {&reply->icmp_q_ip_ttl, (wpr_t)extract_byte,                  NULL},
    {reply,                 (wpr_t)extract_dealias_reply_icmpext, NULL},
    {&reply->proto,         (wpr_t)extract_byte,                  NULL},
    {&reply->tcp_flags,     (wpr_t)extract_byte,                  NULL},
    {&reply->src,           (wpr_t)extract_addr,                  table},
  };
  const int handler_cnt = sizeof(handlers)/sizeof(warts_param_reader_t);
  uint32_t o = *off;
  int i;

  if((i = warts_params_read(buf, off, len, handlers, handler_cnt)) != 0)
    return i;

  if(flag_isset(&buf[o], WARTS_DEALIAS_REPLY_PROTO) == 0)
    {
      if(reply->src->type == SCAMPER_ADDR_TYPE_IPV4)
	reply->proto = IPPROTO_ICMP;
      else
	reply->proto = IPPROTO_ICMPV6;
    }

  return i;
}
Пример #7
0
static int warts_trace_pmtud_n_read(const scamper_trace_pmtud_t *pmtud,
				    scamper_trace_pmtud_n_t *note,
				    const uint8_t *buf, uint32_t *off,
				    uint32_t len)
{
  scamper_trace_hop_t *hop;
  uint16_t u16 = 0;
  warts_param_reader_t handlers[] = {
    {&note->type,  (wpr_t)extract_byte,   NULL},
    {&note->nhmtu, (wpr_t)extract_uint16, NULL},
    {&u16,         (wpr_t)extract_uint16, NULL},
  };
  const int handler_cnt = sizeof(handlers)/sizeof(warts_param_reader_t);
  uint32_t o = *off;

  if(warts_params_read(buf, off, len, handlers, handler_cnt) != 0)
    return -1;

  if(flag_isset(&buf[o], WARTS_TRACE_PMTUD_N_HOP))
    {
      hop = pmtud->hops;
      while(u16 > 0)
	{
	  if(hop == NULL)
	    break;
	  hop = hop->hop_next;
	  u16--;
	}
      if(hop == NULL)
	return -1;
      note->hop = hop;
    }

  return 0;
}
Пример #8
0
static int warts_trace_hop_read(scamper_trace_hop_t *hop, warts_state_t *state,
				warts_addrtable_t *table,
				const uint8_t *buf,uint32_t *off,uint32_t len)
{
  warts_param_reader_t handlers[] = {
    {&hop->hop_addr,       (wpr_t)extract_addr_gid,              state},
    {&hop->hop_probe_ttl,  (wpr_t)extract_byte,                  NULL},
    {&hop->hop_reply_ttl,  (wpr_t)extract_byte,                  NULL},
    {&hop->hop_flags,      (wpr_t)extract_byte,                  NULL},
    {&hop->hop_probe_id,   (wpr_t)warts_trace_hop_read_probe_id, NULL},
    {&hop->hop_rtt,        (wpr_t)extract_rtt,                   NULL},
    {hop,                  (wpr_t)warts_trace_hop_read_icmp_tc,  NULL},
    {&hop->hop_probe_size, (wpr_t)extract_uint16,                NULL},
    {&hop->hop_reply_size, (wpr_t)extract_uint16,                NULL},
    {&hop->hop_reply_ipid, (wpr_t)extract_uint16,                NULL},
    {&hop->hop_reply_tos,  (wpr_t)extract_byte,                  NULL},
    {&hop->hop_icmp_nhmtu, (wpr_t)extract_uint16,                NULL},
    {&hop->hop_icmp_q_ipl, (wpr_t)extract_uint16,                NULL},
    {&hop->hop_icmp_q_ttl, (wpr_t)extract_byte,                  NULL},
    {&hop->hop_tcp_flags,  (wpr_t)extract_byte,                  NULL},
    {&hop->hop_icmp_q_tos, (wpr_t)extract_byte,                  NULL},
    {hop,                  (wpr_t)warts_trace_hop_read_icmpext,  NULL},
    {&hop->hop_addr,       (wpr_t)extract_addr,                  table},
    {&hop->hop_tx,         (wpr_t)extract_timeval,               NULL},
  };
  const int handler_cnt = sizeof(handlers)/sizeof(warts_param_reader_t);
  uint32_t o = *off;
  int rc;

  if((rc = warts_params_read(buf, off, len, handlers, handler_cnt)) != 0)
    return rc;

  if(hop->hop_addr == NULL)
    return -1;
  if(hop->hop_probe_ttl == 0)
    return -1;

  if(SCAMPER_TRACE_HOP_IS_ICMP_Q(hop))
    {
      if(flag_isset(&buf[o], WARTS_TRACE_HOP_Q_IPTTL) == 0)
	hop->hop_icmp_q_ttl = 1;
      if(flag_isset(&buf[o], WARTS_TRACE_HOP_Q_IPLEN) == 0)
	hop->hop_icmp_q_ipl = hop->hop_probe_size;
    }

  return 0;
}
Пример #9
0
static void notify(const char *notice, int flags, const char *ntype,
                   const char *upsname)
{
    char	exec[LARGEBUF];
    int	ret;

    if (flag_isset(flags, NOTIFY_IGNORE))
        return;

    if (flag_isset(flags, NOTIFY_SYSLOG))
        upslogx(LOG_NOTICE, "%s", notice);

    /* fork here so upsmon doesn't get wedged if the notifier is slow */
    ret = fork();

    if (ret < 0) {
        upslog_with_errno(LOG_ERR, "Can't fork to notify");
        return;
    }

    if (ret != 0)	/* parent */
        return;

    /* child continues and does all the work */

    if (flag_isset(flags, NOTIFY_WALL))
        wall(notice);

    if (flag_isset(flags, NOTIFY_EXEC)) {
        if (notifycmd != NULL) {
            snprintf(exec, sizeof(exec), "%s \"%s\"", notifycmd, notice);

            if (upsname)
                setenv("UPSNAME", upsname, 1);
            else
                setenv("UPSNAME", "", 1);

            setenv("NOTIFYTYPE", ntype, 1);
            if (system(exec) == -1) {
                upslog_with_errno(LOG_ERR, "%s", __func__);
            }
        }
    }

    exit(EXIT_SUCCESS);
}
Пример #10
0
static int warts_dealias_prefixscan_read(scamper_dealias_t *dealias,
					 warts_state_t *state,
					 warts_addrtable_t *table,
					 scamper_dealias_probedef_t **defs,
					 uint8_t *buf, uint32_t *off,
					 uint32_t len)
{
  scamper_dealias_prefixscan_t pfs, *p;
  warts_param_reader_t handlers[] = {
    {&pfs.a,            (wpr_t)extract_addr,                  table},
    {&pfs.b,            (wpr_t)extract_addr,                  table},
    {&pfs.ab,           (wpr_t)extract_addr,                  table},
    {&pfs,              (wpr_t)extract_dealias_prefixscan_xs, table},
    {&pfs.prefix,       (wpr_t)extract_byte,                  NULL},
    {&pfs.attempts,     (wpr_t)extract_byte,                  NULL},
    {&pfs.fudge,        (wpr_t)extract_uint16,                NULL},
    {&pfs.wait_probe,   (wpr_t)extract_uint16,                NULL},
    {&pfs.wait_timeout, (wpr_t)extract_byte,                  NULL},
    {&pfs.probedefc,    (wpr_t)extract_uint16,                NULL},
    {&pfs.flags,        (wpr_t)extract_byte,                  NULL},
    {&pfs.replyc,       (wpr_t)extract_byte,                  NULL},
  };
  const int handler_cnt = sizeof(handlers)/sizeof(warts_param_reader_t);
  uint32_t o = *off;
  uint16_t i;

  memset(&pfs, 0, sizeof(pfs));
  if(warts_params_read(buf, off, len, handlers, handler_cnt) != 0)
    return -1;

  if(scamper_dealias_prefixscan_alloc(dealias) != 0)
    return -1;

  p = dealias->data;
  memcpy(p, &pfs, sizeof(pfs));

  /* by default we require five replies before inferring an alias */
  if(flag_isset(&buf[o], WARTS_DEALIAS_PREFIXSCAN_REPLYC) == 0)
    p->replyc = 5;

  if(p->probedefc > 0)
    {
      if(scamper_dealias_prefixscan_probedefs_alloc(p, p->probedefc) != 0)
	return -1;

      for(i=0; i<p->probedefc; i++)
	{
	  if(warts_dealias_probedef_read(&p->probedefs[i], state, table,
					 buf, off, len) != 0)
	    return -1;
	}
    }

  *defs = p->probedefs;
  return 0;
}
Пример #11
0
static void ups_fsd(utype_t *ups)
{
    if (flag_isset(ups->status, ST_FSD)) {		/* no change */
        upsdebugx(4, "%s: %s (no change)", __func__, ups->sys);
        return;
    }

    upsdebugx(3, "%s: %s (first time)", __func__, ups->sys);

    /* must have changed from !FSD to FSD, so notify */

    do_notify(ups, NOTIFY_FSD);
    setflag(&ups->status, ST_FSD);
}
Пример #12
0
static void ups_low_batt(utype_t *ups)
{
    if (flag_isset(ups->status, ST_LOWBATT)) { 	/* no change */
        upsdebugx(4, "%s: %s (no change)", __func__, ups->sys);
        return;
    }

    upsdebugx(3, "%s: %s (first time)", __func__, ups->sys);

    /* must have changed from !LB to LB, so notify */

    do_notify(ups, NOTIFY_LOWBATT);
    setflag(&ups->status, ST_LOWBATT);
}
Пример #13
0
/* see what the status of the UPS is and handle any changes */
static void pollups(utype_t *ups)
{
    char	status[SMALLBUF];

    /* try a reconnect here */
    if (!flag_isset(ups->status, ST_CONNECTED))
        if (try_connect(ups) != 1)
            return;

    if (upscli_ssl(&ups->conn) == 1)
        upsdebugx(2, "%s: %s [SSL]", __func__, ups->sys);
    else
        upsdebugx(2, "%s: %s", __func__, ups->sys);

    set_alarm();

    if (get_var(ups, "status", status, sizeof(status)) == 0) {
        clear_alarm();
        parse_status(ups, status);
        return;
    }

    /* fallthrough: no communications */
    clear_alarm();

    /* try to make some of these a little friendlier */

    switch (upscli_upserror(&ups->conn)) {

    case UPSCLI_ERR_UNKNOWNUPS:
        upslogx(LOG_ERR, "Poll UPS [%s] failed - [%s] "
                "does not exist on server %s",
                ups->sys, ups->upsname,	ups->hostname);

        break;
    default:
        upslogx(LOG_ERR, "Poll UPS [%s] failed - %s",
                ups->sys, upscli_strerror(&ups->conn));
        break;
    }

    /* throw COMMBAD or NOCOMM as conditions may warrant */
    ups_is_gone(ups);

    /* if upsclient lost the connection, clean up things on our side */
    if (upscli_fd(&ups->conn) == -1) {
        drop_connection(ups);
        return;
    }
}
Пример #14
0
void use_channel(CREATURE *crit, char *name, char *text, long level, long flag, char *color)
{
	MSOCKET *sock;
	char buf[MAX_BUFFER];

	sprintf(buf,"%s(%s) [&N&+W%s&N%s]: %s&N",color,name,crit->name,color,text);

	for(sock = socket_list; sock; sock = sock->next)
	{
		if(!IsPlaying(sock->pc) || sock->pc->level < level
		|| flag_isset(sock->pc->flags, flag))
			continue;

		sendcrit(sock->pc,buf);
	}
}
static int warts_tracelb_link_read(scamper_tracelb_t *trace,
				   scamper_tracelb_link_t *link,
				   warts_state_t *state,
				   warts_addrtable_t *table,
				   const uint8_t *buf,
				   uint32_t *off, uint32_t len)
{
  uint16_t from, to;
  warts_param_reader_t handlers[] = {
    {&from,         (wpr_t)extract_uint16, NULL},
    {&to,           (wpr_t)extract_uint16, NULL},
    {&link->hopc,   (wpr_t)extract_byte,   NULL},
  };
  const int handler_cnt = sizeof(handlers)/sizeof(warts_param_reader_t);
  scamper_tracelb_probeset_t *set;
  uint8_t i;
  uint32_t o = *off;

  if(warts_params_read(buf, off, len, handlers, handler_cnt) != 0)
    {
      return -1;
    }
  link->from = trace->nodes[from];

  if(flag_isset(&buf[o], WARTS_TRACELB_LINK_TO) != 0)
    link->to = trace->nodes[to];
  else
    link->to = NULL;

  if(link->hopc > 0)
    {
      if(scamper_tracelb_link_probesets_alloc(link, link->hopc) != 0)
	return -1;

      for(i=0; i<link->hopc; i++)
	{
	  if((set = scamper_tracelb_probeset_alloc()) == NULL)
	    return -1;
	  link->sets[i] = set;

	  if(warts_tracelb_probeset_read(set, state, table, buf, off, len) != 0)
	    return -1;
	}
    }

  return 0;
}
Пример #16
0
static int warts_ping_reply_read(const scamper_ping_t *ping,
				 scamper_ping_reply_t *reply,
				 warts_state_t *state,
				 warts_addrtable_t *table, const uint8_t *buf,
				 uint32_t *off, uint32_t len)
{
  warts_param_reader_t handlers[] = {
    {&reply->addr,            (wpr_t)extract_addr_gid,             state},
    {&reply->flags,           (wpr_t)extract_byte,                 NULL},
    {&reply->reply_ttl,       (wpr_t)extract_byte,                 NULL},
    {&reply->reply_size,      (wpr_t)extract_uint16,               NULL},
    {reply,                   (wpr_t)extract_ping_reply_icmptc,    NULL},
    {&reply->rtt,             (wpr_t)extract_rtt,                  NULL},
    {&reply->probe_id,        (wpr_t)extract_uint16,               NULL},
    {&reply->reply_ipid,      (wpr_t)extract_uint16,               NULL},
    {&reply->probe_ipid,      (wpr_t)extract_uint16,               NULL},
    {&reply->reply_proto,     (wpr_t)extract_byte,                 NULL},
    {&reply->tcp_flags,       (wpr_t)extract_byte,                 NULL},
    {&reply->addr,            (wpr_t)extract_addr,                 table},
    {&reply->v4rr,            (wpr_t)extract_ping_reply_v4rr,      table},
    {&reply->v4ts,            (wpr_t)extract_ping_reply_v4ts,      table},
    {&reply->reply_ipid32,    (wpr_t)extract_uint32,               NULL},
    {&reply->tx,              (wpr_t)extract_timeval,              NULL},
    {&reply->tsreply,         (wpr_t)extract_ping_reply_tsreply,   NULL},
  };
  const int handler_cnt = sizeof(handlers) / sizeof(warts_param_reader_t);
  uint32_t o = *off;
  int i;

  if((i = warts_params_read(buf, off, len, handlers, handler_cnt)) != 0)
    return i;

  /*
   * some earlier versions of the ping reply structure did not include
   * the reply protocol field.  fill it with something valid.
   */
  if(flag_isset(&buf[o], WARTS_PING_REPLY_REPLY_PROTO) == 0)
    {
      if(ping->dst->type == SCAMPER_ADDR_TYPE_IPV4)
	reply->reply_proto = IPPROTO_ICMP;
      else
	reply->reply_proto = IPPROTO_ICMPV6;
    }

  return 0;
}
Пример #17
0
static void slavesync(void)
{
    utype_t	*ups;
    char	temp[SMALLBUF];
    time_t	start, now;
    int	maxlogins, logins;

    time(&start);

    for (;;) {
        maxlogins = 0;

        for (ups = firstups; ups != NULL; ups = ups->next) {

            /* only check login count on our master(s) */
            if (!flag_isset(ups->status, ST_MASTER))
                continue;

            set_alarm();

            if (get_var(ups, "numlogins", temp, sizeof(temp)) >= 0) {
                logins = strtol(temp, (char **)NULL, 10);

                if (logins > maxlogins)
                    maxlogins = logins;
            }

            clear_alarm();
        }

        /* if no UPS has more than 1 login (us), then slaves are gone */
        if (maxlogins <= 1)
            return;

        /* after HOSTSYNC seconds, assume slaves are stuck and bail */
        time(&now);

        if ((now - start) > hostsync) {
            upslogx(LOG_INFO, "Host sync timer expired, forcing shutdown");
            return;
        }

        usleep(250000);
    }
}
static int warts_ping_params_read(scamper_ping_t *ping, warts_state_t *state,
				  warts_addrtable_t *table,
				  uint8_t *buf, uint32_t *off, uint32_t len)
{
  warts_param_reader_t handlers[] = {
    {&ping->list,          (wpr_t)extract_list,            state},
    {&ping->cycle,         (wpr_t)extract_cycle,           state},
    {&ping->src,           (wpr_t)extract_addr_gid,        state},
    {&ping->dst,           (wpr_t)extract_addr_gid,        state},
    {&ping->start,         (wpr_t)extract_timeval,         NULL},
    {&ping->stop_reason,   (wpr_t)extract_byte,            NULL},
    {&ping->stop_data,     (wpr_t)extract_byte,            NULL},
    {&ping->probe_datalen, (wpr_t)extract_uint16,          NULL},
    {&ping->probe_data,    (wpr_t)extract_bytes_alloc,   &ping->probe_datalen},
    {&ping->probe_count,   (wpr_t)extract_uint16,          NULL},
    {&ping->probe_size,    (wpr_t)extract_uint16,          NULL},
    {&ping->probe_wait,    (wpr_t)extract_byte,            NULL},
    {&ping->probe_ttl,     (wpr_t)extract_byte,            NULL},
    {&ping->reply_count,   (wpr_t)extract_uint16,          NULL},
    {&ping->ping_sent,     (wpr_t)extract_uint16,          NULL},
    {&ping->probe_method,  (wpr_t)extract_byte,            NULL},
    {&ping->probe_sport,   (wpr_t)extract_uint16,          NULL},
    {&ping->probe_dport,   (wpr_t)extract_uint16,          NULL},
    {&ping->userid,        (wpr_t)extract_uint32,          NULL},
    {&ping->src,           (wpr_t)extract_addr,            table},
    {&ping->dst,           (wpr_t)extract_addr,            table},
    {&ping->flags,         (wpr_t)extract_byte,            NULL},
    {&ping->probe_tos,     (wpr_t)extract_byte,            NULL},
    {&ping->probe_tsps,    (wpr_t)extract_ping_probe_tsps, table},
    {&ping->probe_icmpsum, (wpr_t)extract_uint16,          NULL},
    {&ping->reply_pmtu,    (wpr_t)extract_uint16,          NULL},
    {&ping->probe_timeout, (wpr_t)extract_byte,            NULL},
    {&ping->probe_wait_us, (wpr_t)extract_uint32,          NULL},
  };
  const int handler_cnt = sizeof(handlers)/sizeof(warts_param_reader_t);
  uint32_t o = *off;
  int rc;

  if((rc = warts_params_read(buf, off, len, handlers, handler_cnt)) != 0)
    return rc;
  if(flag_isset(&buf[o], WARTS_PING_PROBE_TIMEOUT) == 0)
    ping->probe_timeout = ping->probe_wait;
  return 0;
}
Пример #19
0
static void ups_on_batt(utype_t *ups)
{
    if (flag_isset(ups->status, ST_ONBATT)) { 	/* no change */
        upsdebugx(4, "%s: %s (no change)", __func__, ups->sys);
        return;
    }

    sleepval = pollfreqalert;	/* bump up polling frequency */

    ups->linestate = 0;

    upsdebugx(3, "%s: %s (first time)", __func__, ups->sys);

    /* must have changed from OL to OB, so notify */

    do_notify(ups, NOTIFY_ONBATT);
    setflag(&ups->status, ST_ONBATT);
    clearflag(&ups->status, ST_ONLINE);
}
Пример #20
0
static void ups_on_line(utype_t *ups)
{
    if (flag_isset(ups->status, ST_ONLINE)) { 	/* no change */
        upsdebugx(4, "%s: %s (no change)", __func__, ups->sys);
        return;
    }

    sleepval = pollfreq;

    upsdebugx(3, "%s: %s (first time)", __func__, ups->sys);

    /* ignore the first OL at startup, otherwise send the notifier */
    if (ups->linestate != -1)
        do_notify(ups, NOTIFY_ONLINE);

    ups->linestate = 1;

    setflag(&ups->status, ST_ONLINE);
    clearflag(&ups->status, ST_ONBATT);
}
Пример #21
0
/* check for master permissions on the server for this ups */
static int checkmaster(utype_t *ups)
{
    char	buf[SMALLBUF];

    /* don't bother if we're not configured as a master for this ups */
    if (!flag_isset(ups->status, ST_MASTER))
        return 1;

    /* this shouldn't happen (LOGIN checks it earlier) */
    if ((ups->upsname == NULL) || (strlen(ups->upsname) == 0)) {
        upslogx(LOG_ERR, "Set master on UPS [%s] failed: empty upsname",
                ups->sys);
        return 0;
    }

    snprintf(buf, sizeof(buf), "MASTER %s\n", ups->upsname);

    if (upscli_sendline(&ups->conn, buf, strlen(buf)) < 0) {
        upslogx(LOG_ALERT, "Can't set master mode on UPS [%s] - %s",
                ups->sys, upscli_strerror(&ups->conn));
        return 0;
    }

    if (upscli_readline(&ups->conn, buf, sizeof(buf)) == 0) {
        if (!strncmp(buf, "OK", 2))
            return 1;

        /* not ERR, but not caught by readline either? */

        upslogx(LOG_ALERT, "Master privileges unavailable on UPS [%s]",
                ups->sys);
        upslogx(LOG_ALERT, "Response: [%s]", buf);
    }
    else {	/* something caught by readraw's parsing call */
        upslogx(LOG_ALERT, "Master privileges unavailable on UPS [%s]",
                ups->sys);
        upslogx(LOG_ALERT, "Reason: %s", upscli_strerror(&ups->conn));
    }

    return 0;
}
Пример #22
0
/* recalculate the online power value and see if things are still OK */
static void recalc(void)
{
    utype_t	*ups;
    int	val_ol = 0;
    time_t	now;

    time(&now);
    ups = firstups;
    while (ups != NULL) {
        /* promote dead UPSes that were last known OB to OB+LB */
        if ((now - ups->lastpoll) > deadtime)
            if (flag_isset(ups->status, ST_ONBATT)) {
                upsdebugx(1, "Promoting dead UPS: %s", ups->sys);
                setflag(&ups->status, ST_LOWBATT);
            }

        /* note: we assume that a UPS that isn't critical must be OK *
         *                                                           *
         * this means a UPS we've never heard from is assumed OL     *
         * whether this is really the best thing to do is undecided  */

        /* crit = (FSD) || (OB & LB) > HOSTSYNC seconds */
        if (is_ups_critical(ups))
            upsdebugx(1, "Critical UPS: %s", ups->sys);
        else
            val_ol += ups->pv;

        ups = ups->next;
    }

    upsdebugx(3, "Current power value: %d", val_ol);
    upsdebugx(3, "Minimum power value: %d", minsupplies);

    if (val_ol < minsupplies)
        forceshutdown();
}
Пример #23
0
/* change some UPS parameters during reloading */
static void redefine_ups(utype_t *ups, int pv, const char *un,
                         const char *pw, const char *master)
{
    ups->retain = 1;

    if (ups->pv != pv) {
        upslogx(LOG_INFO, "UPS [%s]: redefined power value to %d",
                ups->sys, pv);
        ups->pv = pv;
    }

    totalpv += ups->pv;

    if (ups->un) {
        if (strcmp(ups->un, un) != 0) {
            upslogx(LOG_INFO, "UPS [%s]: redefined username",
                    ups->sys);

            free(ups->un);

            ups->un = xstrdup(un);

            /*
             * if not logged in force a reconnection since this
             * may have been redefined to make a login work
             */

            if (!flag_isset(ups->status, ST_LOGIN)) {
                upslogx(LOG_INFO, "UPS [%s]: retrying connection",
                        ups->sys);

                drop_connection(ups);
            }

        }	/* if (strcmp(ups->un, un) != 0) { */

    } else {

        /* adding a username? (going to new style MONITOR line) */

        if (un) {
            upslogx(LOG_INFO, "UPS [%s]: defined username",
                    ups->sys);

            ups->un = xstrdup(un);

            /* possibly force reconnection - see above */

            if (!flag_isset(ups->status, ST_LOGIN)) {
                upslogx(LOG_INFO, "UPS [%s]: retrying connection",
                        ups->sys);

                drop_connection(ups);
            }

        }	/* if (un) */
    }

    /* paranoia */
    if (!ups->pw)
        ups->pw = xstrdup("");	/* give it a bogus, but non-NULL one */

    /* obviously don't put the new password in the syslog... */
    if (strcmp(ups->pw, pw) != 0) {
        upslogx(LOG_INFO, "UPS [%s]: redefined password", ups->sys);

        free(ups->pw);
        ups->pw = xstrdup(pw);

        /* possibly force reconnection - see above */

        if (!flag_isset(ups->status, ST_LOGIN)) {
            upslogx(LOG_INFO, "UPS [%s]: retrying connection",
                    ups->sys);

            drop_connection(ups);
        }
    }

    /* slave -> master */
    if ((!strcasecmp(master, "master")) && (!flag_isset(ups->status, ST_MASTER))) {
        upslogx(LOG_INFO, "UPS [%s]: redefined as master", ups->sys);
        setflag(&ups->status, ST_MASTER);

        /* reset connection to ensure master mode gets checked */
        drop_connection(ups);
        return;
    }

    /* master -> slave */
    if ((!strcasecmp(master, "slave")) && (flag_isset(ups->status, ST_MASTER))) {
        upslogx(LOG_INFO, "UPS [%s]: redefined as slave", ups->sys);
        clearflag(&ups->status, ST_MASTER);
        return;
    }
}
Пример #24
0
char *strflags(void *obj)
{
	CREATURE *xcrit=0;
	OBJECT *xobj=0;
	ROOM *xroom=0;
	static char buf[MAX_BUFFER];

	buf[0] = '\0';

	switch(*(unsigned int*)obj)
	{
	case TYPE_CREATURE:
		xcrit = (CREATURE*)obj;
		if(flag_isset(xcrit->flags, CFLAG_LOG))		strcat(buf,"LOG ");
		if(flag_isset(xcrit->flags, CFLAG_PLAYER))	strcat(buf,"Player ");
		if(flag_isset(xcrit->flags, CFLAG_ANSI))	strcat(buf,"Ansi ");
		if(flag_isset(xcrit->flags, CFLAG_QUIET))	strcat(buf,"Quiet ");
		if(flag_isset(xcrit->flags, CFLAG_MOUNT))	strcat(buf,"Mount ");
		if(flag_isset(xcrit->flags, CFLAG_BANKER))	strcat(buf,"Banker ");
		if(flag_isset(xcrit->flags, CFLAG_ANTIIDLE))	strcat(buf,"Anti-Idle ");
		if(flag_isset(xcrit->flags, CFLAG_BLANK))	strcat(buf,"Blank ");
		if(flag_isset(xcrit->flags, CFLAG_LOG))		strcat(buf,"Log ");
		if(flag_isset(xcrit->flags, CFLAG_NOMENU))	strcat(buf,"No-Menu ");
		if(flag_isset(xcrit->flags, CFLAG_NOTIFY))	strcat(buf,"Notify ");
		if(flag_isset(xcrit->flags, CFLAG_WIZINVIS))	strcat(buf,"Wizinvis ");
		break;
	case TYPE_OBJECT:
		xobj = (OBJECT*)obj;
		if(flag_isset(xobj->flags, OFLAG_NOTAKE))	strcat(buf,"Notake ");
		if(flag_isset(xobj->flags, OFLAG_GLOW))		strcat(buf,"Glow ");
		if(flag_isset(xobj->flags, OFLAG_HUM))		strcat(buf,"Hum ");
		break;
	case TYPE_ROOM:
		xroom = (ROOM*)obj;
		if(flag_isset(xroom->flags, RFLAG_HEAL))	strcat(buf,"Heal ");
		break;
	default:
		mudlog("strflags: invalid object!");
		strcpy(buf,"INVALID OBJECT");
		break;
	}

	if(buf[0] == '\0')
		strcpy(buf,"None ");
	buf[strlen(buf)-1] = '\0';  // strip last space

	return buf;
}
Пример #25
0
static void addups(int reloading, const char *sys, const char *pvs,
                   const char *un, const char *pw, const char *master)
{
    int	pv;
    utype_t	*tmp, *last;

    /* the username is now required - no more host-based auth */

    if ((!sys) || (!pvs) || (!pw) || (!master) || (!un)) {
        upslogx(LOG_WARNING, "Ignoring invalid MONITOR line in %s!", configfile);
        upslogx(LOG_WARNING, "MONITOR configuration directives require five arguments.");
        return;
    }

    pv = strtol(pvs, (char **) NULL, 10);

    if (pv < 0) {
        upslogx(LOG_WARNING, "UPS [%s]: ignoring invalid power value [%s]",
                sys, pvs);
        return;
    }

    last = tmp = firstups;

    while (tmp) {
        last = tmp;

        /* check for duplicates */
        if (!strcmp(tmp->sys, sys)) {
            if (reloading)
                redefine_ups(tmp, pv, un, pw, master);
            else
                upslogx(LOG_WARNING, "Warning: ignoring duplicate"
                        " UPS [%s]", sys);
            return;
        }

        tmp = tmp->next;
    }

    tmp = xmalloc(sizeof(utype_t));
    tmp->sys = xstrdup(sys);
    tmp->pv = pv;

    /* build this up so the user doesn't run with bad settings */
    totalpv += tmp->pv;

    if (un)
        tmp->un = xstrdup(un);
    else
        tmp->un = NULL;

    tmp->pw = xstrdup(pw);
    tmp->status = 0;
    tmp->retain = 1;

    /* ignore initial COMMOK and ONLINE by default */
    tmp->commstate = -1;
    tmp->linestate = -1;

    tmp->lastpoll = 0;
    tmp->lastnoncrit = 0;
    tmp->lastrbwarn = 0;
    tmp->lastncwarn = 0;

    if (!strcasecmp(master, "master"))
        setflag(&tmp->status, ST_MASTER);

    tmp->next = NULL;

    if (last)
        last->next = tmp;
    else
        firstups = tmp;

    if (tmp->pv)
        upslogx(LOG_INFO, "UPS: %s (%s) (power value %d)", tmp->sys,
                flag_isset(tmp->status, ST_MASTER) ? "master" : "slave",
                tmp->pv);
    else
        upslogx(LOG_INFO, "UPS: %s (monitoring only)", tmp->sys);

    tmp->upsname = tmp->hostname = NULL;

    if (upscli_splitname(tmp->sys, &tmp->upsname, &tmp->hostname,
                         &tmp->port) != 0) {
        upslogx(LOG_ERR, "Error: unable to split UPS name [%s]",
                tmp->sys);
    }

    if (!tmp->upsname)
        upslogx(LOG_WARNING, "Warning: UPS [%s]: no upsname set!",
                tmp->sys);
}