new upstream 2.8.0
This commit is contained in:
parent
fc7f4b43c1
commit
b2b0c9995a
836 changed files with 137090 additions and 30018 deletions
225
clients/upsrw.c
225
clients/upsrw.c
|
@ -1,6 +1,8 @@
|
|||
/* upsrw - simple client for read/write variable access (formerly upsct2)
|
||||
|
||||
Copyright (C) 1999 Russell Kroll <rkroll@exploits.org>
|
||||
Copyright (C)
|
||||
1999 Russell Kroll <rkroll@exploits.org>
|
||||
2019 EATON (author: Arnaud Quette <ArnaudQuette@eaton.com>)
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
|
@ -25,10 +27,14 @@
|
|||
#include <netinet/in.h>
|
||||
#include <sys/socket.h>
|
||||
|
||||
#include "nut_stdint.h"
|
||||
#include "upsclient.h"
|
||||
#include "extstate.h"
|
||||
|
||||
static char *upsname = NULL, *hostname = NULL;
|
||||
static char *upsname = NULL, *hostname = NULL;
|
||||
static UPSCONN_t *ups = NULL;
|
||||
static int tracking_enabled = 0;
|
||||
static unsigned int timeout = DEFAULT_TRACKING_TIMEOUT;
|
||||
|
||||
struct list_t {
|
||||
char *name;
|
||||
|
@ -39,18 +45,22 @@ static void usage(const char *prog)
|
|||
{
|
||||
printf("Network UPS Tools %s %s\n\n", prog, UPS_VERSION);
|
||||
printf("usage: %s [-h]\n", prog);
|
||||
printf(" %s [-s <variable>] [-u <username>] [-p <password>] <ups>\n\n", prog);
|
||||
printf(" %s [-s <variable>] [-u <username>] [-p <password>] [-w] [-t <timeout>] <ups>\n\n", prog);
|
||||
printf("Demo program to set variables within UPS hardware.\n");
|
||||
printf("\n");
|
||||
printf(" -h display this help text\n");
|
||||
printf(" -s <variable> specify variable to be changed\n");
|
||||
printf(" use -s VAR=VALUE to avoid prompting for value\n");
|
||||
printf(" -l show all possible read/write variables.\n");
|
||||
printf(" -u <username> set username for command authentication\n");
|
||||
printf(" -p <password> set password for command authentication\n");
|
||||
printf(" -w wait for the completion of setting by the driver\n");
|
||||
printf(" and return its actual result from the device\n");
|
||||
printf(" -t <timeout> set a timeout when using -w (in seconds, default: %u)\n", DEFAULT_TRACKING_TIMEOUT);
|
||||
printf("\n");
|
||||
printf(" <ups> UPS identifier - <upsname>[@<hostname>[:<port>]]\n");
|
||||
printf("\n");
|
||||
printf("Call without -s to show all possible read/write variables.\n");
|
||||
printf("Call without -s to show all possible read/write variables (same as -l).\n");
|
||||
}
|
||||
|
||||
static void clean_exit(void)
|
||||
|
@ -64,9 +74,21 @@ static void clean_exit(void)
|
|||
free(ups);
|
||||
}
|
||||
|
||||
#if (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_PUSH_POP_BESIDEFUNC) && (!defined HAVE_PRAGMA_GCC_DIAGNOSTIC_PUSH_POP_INSIDEFUNC) && ( (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TYPE_LIMITS_BESIDEFUNC) || (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TAUTOLOGICAL_CONSTANT_OUT_OF_RANGE_COMPARE_BESIDEFUNC) )
|
||||
# pragma GCC diagnostic push
|
||||
#endif
|
||||
#if (!defined HAVE_PRAGMA_GCC_DIAGNOSTIC_PUSH_POP_INSIDEFUNC) && (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TYPE_LIMITS_BESIDEFUNC)
|
||||
# pragma GCC diagnostic ignored "-Wtype-limits"
|
||||
#endif
|
||||
#if (!defined HAVE_PRAGMA_GCC_DIAGNOSTIC_PUSH_POP_INSIDEFUNC) && (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TAUTOLOGICAL_CONSTANT_OUT_OF_RANGE_COMPARE_BESIDEFUNC)
|
||||
# pragma GCC diagnostic ignored "-Wtautological-constant-out-of-range-compare"
|
||||
#endif
|
||||
static void do_set(const char *varname, const char *newval)
|
||||
{
|
||||
int cmd_complete = 0;
|
||||
char buf[SMALLBUF], enc[SMALLBUF];
|
||||
char tracking_id[UUID4_LEN];
|
||||
time_t start, now;
|
||||
|
||||
snprintf(buf, sizeof(buf), "SET VAR %s %s \"%s\"\n", upsname, varname, pconf_encode(newval, enc, sizeof(enc)));
|
||||
|
||||
|
@ -78,17 +100,92 @@ static void do_set(const char *varname, const char *newval)
|
|||
fatalx(EXIT_FAILURE, "Set variable failed: %s", upscli_strerror(ups));
|
||||
}
|
||||
|
||||
/* FUTURE: status cookies will tie in here */
|
||||
/* verify answer */
|
||||
if (strncmp(buf, "OK", 2) != 0) {
|
||||
fatalx(EXIT_FAILURE, "Unexpected response from upsd: %s", buf);
|
||||
}
|
||||
|
||||
/* check for status tracking id */
|
||||
if (
|
||||
!tracking_enabled ||
|
||||
/* sanity check on the size: "OK TRACKING " + UUID4_LEN */
|
||||
strlen(buf) != (UUID4_LEN - 1 + strlen("OK TRACKING "))
|
||||
) {
|
||||
/* reply as usual */
|
||||
fprintf(stderr, "%s\n", buf);
|
||||
return;
|
||||
}
|
||||
|
||||
#ifdef HAVE_PRAGMAS_FOR_GCC_DIAGNOSTIC_IGNORED_FORMAT_TRUNCATION
|
||||
#pragma GCC diagnostic push
|
||||
#endif
|
||||
#ifdef HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_FORMAT_TRUNCATION
|
||||
#pragma GCC diagnostic ignored "-Wformat-truncation"
|
||||
#endif
|
||||
/* From the check above, we know that we have exactly UUID4_LEN chars
|
||||
* (aka sizeof(tracking_id)) in the buf after "OK TRACKING " prefix,
|
||||
* plus the null-byte.
|
||||
*/
|
||||
assert (UUID4_LEN == 1 + snprintf(tracking_id, sizeof(tracking_id), "%s", buf + strlen("OK TRACKING ")));
|
||||
#ifdef HAVE_PRAGMAS_FOR_GCC_DIAGNOSTIC_IGNORED_FORMAT_TRUNCATION
|
||||
#pragma GCC diagnostic pop
|
||||
#endif
|
||||
time(&start);
|
||||
|
||||
/* send status tracking request, looping if status is PENDING */
|
||||
while (!cmd_complete) {
|
||||
|
||||
/* check for timeout */
|
||||
time(&now);
|
||||
if (difftime(now, start) >= timeout)
|
||||
fatalx(EXIT_FAILURE, "Can't receive status tracking information: timeout");
|
||||
|
||||
snprintf(buf, sizeof(buf), "GET TRACKING %s\n", tracking_id);
|
||||
|
||||
if (upscli_sendline(ups, buf, strlen(buf)) < 0)
|
||||
fatalx(EXIT_FAILURE, "Can't send status tracking request: %s", upscli_strerror(ups));
|
||||
|
||||
#if (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_PUSH_POP) && ( (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TYPE_LIMITS) || (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TAUTOLOGICAL_CONSTANT_OUT_OF_RANGE_COMPARE) )
|
||||
/* Note for gating macros above: unsuffixed HAVE_PRAGMA_GCC_DIAGNOSTIC_PUSH_POP
|
||||
* means support of contexts both inside and outside function body, so the push
|
||||
* above and pop below (outside this finction) are not used.
|
||||
*/
|
||||
# pragma GCC diagnostic push
|
||||
#endif
|
||||
#ifdef HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TYPE_LIMITS
|
||||
/* Note that the individual warning pragmas for use inside function bodies
|
||||
* are named without a _INSIDEFUNC suffix, for simplicity and legacy reasons
|
||||
*/
|
||||
# pragma GCC diagnostic ignored "-Wtype-limits"
|
||||
#endif
|
||||
#ifdef HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TAUTOLOGICAL_CONSTANT_OUT_OF_RANGE_COMPARE
|
||||
# pragma GCC diagnostic ignored "-Wtautological-constant-out-of-range-compare"
|
||||
#endif
|
||||
/* and get status tracking reply */
|
||||
assert(timeout < LONG_MAX);
|
||||
#if (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_PUSH_POP) && ( (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TYPE_LIMITS) || (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TAUTOLOGICAL_CONSTANT_OUT_OF_RANGE_COMPARE) )
|
||||
# pragma GCC diagnostic pop
|
||||
#endif
|
||||
|
||||
if (upscli_readline_timeout(ups, buf, sizeof(buf), (long)timeout) < 0)
|
||||
fatalx(EXIT_FAILURE, "Can't receive status tracking information: %s", upscli_strerror(ups));
|
||||
|
||||
if (strncmp(buf, "PENDING", 7))
|
||||
cmd_complete = 1;
|
||||
else
|
||||
/* wait a second before retrying */
|
||||
sleep(1);
|
||||
}
|
||||
|
||||
fprintf(stderr, "%s\n", buf);
|
||||
}
|
||||
#if (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_PUSH_POP_BESIDEFUNC) && (!defined HAVE_PRAGMA_GCC_DIAGNOSTIC_PUSH_POP_INSIDEFUNC) && ( (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TYPE_LIMITS_BESIDEFUNC) || (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TAUTOLOGICAL_CONSTANT_OUT_OF_RANGE_COMPARE_BESIDEFUNC) )
|
||||
# pragma GCC diagnostic pop
|
||||
#endif
|
||||
|
||||
static void do_setvar(const char *varname, char *uin, const char *pass)
|
||||
{
|
||||
char newval[SMALLBUF], temp[SMALLBUF], user[SMALLBUF], *ptr;
|
||||
char newval[SMALLBUF], temp[SMALLBUF * 2], user[SMALLBUF], *ptr;
|
||||
struct passwd *pw;
|
||||
|
||||
if (uin) {
|
||||
|
@ -177,13 +274,32 @@ static void do_setvar(const char *varname, char *uin, const char *pass)
|
|||
fatalx(EXIT_FAILURE, "Error: old variable names are not supported");
|
||||
}
|
||||
|
||||
/* enable status tracking ID */
|
||||
if (tracking_enabled) {
|
||||
|
||||
snprintf(temp, sizeof(temp), "SET TRACKING ON\n");
|
||||
|
||||
if (upscli_sendline(ups, temp, strlen(temp)) < 0) {
|
||||
fatalx(EXIT_FAILURE, "Can't enable set variable status tracking: %s", upscli_strerror(ups));
|
||||
}
|
||||
|
||||
if (upscli_readline(ups, temp, sizeof(temp)) < 0) {
|
||||
fatalx(EXIT_FAILURE, "Enabling set variable status tracking failed: %s", upscli_strerror(ups));
|
||||
}
|
||||
|
||||
/* Verify the result */
|
||||
if (strncmp(temp, "OK", 2) != 0) {
|
||||
fatalx(EXIT_FAILURE, "Enabling set variable status tracking failed. upsd answered: %s", temp);
|
||||
}
|
||||
}
|
||||
|
||||
do_set(varname, newval);
|
||||
}
|
||||
|
||||
static const char *get_data(const char *type, const char *varname)
|
||||
{
|
||||
int ret;
|
||||
unsigned int numq, numa;
|
||||
size_t numq, numa;
|
||||
char **answer;
|
||||
const char *query[4];
|
||||
|
||||
|
@ -203,7 +319,7 @@ static const char *get_data(const char *type, const char *varname)
|
|||
return answer[3];
|
||||
}
|
||||
|
||||
static void do_string(const char *varname, const int len)
|
||||
static void do_string(const char *varname, const long len)
|
||||
{
|
||||
const char *val;
|
||||
|
||||
|
@ -214,14 +330,34 @@ static void do_string(const char *varname, const int len)
|
|||
}
|
||||
|
||||
printf("Type: STRING\n");
|
||||
printf("Maximum length: %d\n", len);
|
||||
printf("Maximum length: %ld\n", len);
|
||||
printf("Value: %s\n", val);
|
||||
}
|
||||
|
||||
static void do_enum(const char *varname)
|
||||
static void do_number(const char *varname)
|
||||
{
|
||||
const char *val;
|
||||
|
||||
val = get_data("VAR", varname);
|
||||
|
||||
if (!val) {
|
||||
fatalx(EXIT_FAILURE, "do_number: can't get current value of %s", varname);
|
||||
}
|
||||
|
||||
printf("Type: NUMBER\n");
|
||||
printf("Value: %s\n", val);
|
||||
}
|
||||
|
||||
/**
|
||||
* Display ENUM information
|
||||
* @param varname the name of the NUT variable
|
||||
* @param vartype the type of the NUT variable (ST_FLAG_STRING, ST_FLAG_NUMBER
|
||||
* @param len the length of the NUT variable, if type == ST_FLAG_STRING
|
||||
*/
|
||||
static void do_enum(const char *varname, const int vartype, const long len)
|
||||
{
|
||||
int ret;
|
||||
unsigned int numq, numa;
|
||||
size_t numq, numa;
|
||||
char **answer, buf[SMALLBUF];
|
||||
const char *query[4], *val;
|
||||
|
||||
|
@ -247,14 +383,21 @@ static void do_enum(const char *varname)
|
|||
|
||||
ret = upscli_list_next(ups, numq, query, &numa, &answer);
|
||||
|
||||
printf("Type: ENUM\n");
|
||||
/* Fallback for older upsd versions */
|
||||
if (vartype != ST_FLAG_NONE)
|
||||
printf("Type: ENUM %s\n", (vartype == ST_FLAG_STRING)?"STRING":"NUMBER");
|
||||
else
|
||||
printf("Type: ENUM\n");
|
||||
|
||||
if (vartype == ST_FLAG_STRING)
|
||||
printf("Maximum length: %ld\n", len);
|
||||
|
||||
while (ret == 1) {
|
||||
|
||||
/* ENUM <upsname> <varname> <value> */
|
||||
|
||||
if (numa < 4) {
|
||||
fatalx(EXIT_FAILURE, "Error: insufficient data (got %d args, need at least 4)", numa);
|
||||
fatalx(EXIT_FAILURE, "Error: insufficient data (got %zu args, need at least 4)", numa);
|
||||
}
|
||||
|
||||
printf("Option: \"%s\"", answer[3]);
|
||||
|
@ -272,7 +415,7 @@ static void do_enum(const char *varname)
|
|||
static void do_range(const char *varname)
|
||||
{
|
||||
int ret;
|
||||
unsigned int numq, numa;
|
||||
size_t numq, numa;
|
||||
char **answer;
|
||||
const char *query[4], *val;
|
||||
int ival, min, max;
|
||||
|
@ -299,14 +442,15 @@ static void do_range(const char *varname)
|
|||
|
||||
ret = upscli_list_next(ups, numq, query, &numa, &answer);
|
||||
|
||||
printf("Type: RANGE\n");
|
||||
/* Ranges implies a type "NUMBER" */
|
||||
printf("Type: RANGE NUMBER\n");
|
||||
|
||||
while (ret == 1) {
|
||||
|
||||
/* RANGE <upsname> <varname> <min> <max> */
|
||||
|
||||
if (numa < 5) {
|
||||
fatalx(EXIT_FAILURE, "Error: insufficient data (got %d args, need at least 4)", numa);
|
||||
fatalx(EXIT_FAILURE, "Error: insufficient data (got %zu args, need at least 4)", numa);
|
||||
}
|
||||
|
||||
min = atoi(answer[3]);
|
||||
|
@ -327,7 +471,8 @@ static void do_range(const char *varname)
|
|||
static void do_type(const char *varname)
|
||||
{
|
||||
int ret;
|
||||
unsigned int i, numq, numa;
|
||||
int is_enum = 0; /* 1 if ENUM; FIXME: add a boolean type in common.h */
|
||||
size_t i, numq, numa;
|
||||
char **answer;
|
||||
const char *query[4];
|
||||
|
||||
|
@ -339,16 +484,18 @@ static void do_type(const char *varname)
|
|||
ret = upscli_get(ups, numq, query, &numa, &answer);
|
||||
|
||||
if ((ret < 0) || (numa < numq)) {
|
||||
printf("Unknown type\n");
|
||||
printf("Unknown type\n");
|
||||
return;
|
||||
}
|
||||
|
||||
/* TYPE <upsname> <varname> <type>... */
|
||||
for (i = 3; i < numa; i++) {
|
||||
|
||||
/* ENUM can be NUMBER or STRING
|
||||
* just flag it for latter processing */
|
||||
if (!strcasecmp(answer[i], "ENUM")) {
|
||||
do_enum(varname);
|
||||
return;
|
||||
is_enum = 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!strcasecmp(answer[i], "RANGE")) {
|
||||
|
@ -359,15 +506,21 @@ static void do_type(const char *varname)
|
|||
if (!strncasecmp(answer[i], "STRING:", 7)) {
|
||||
|
||||
char *len = answer[i] + 7;
|
||||
int length = strtol(len, NULL, 10);
|
||||
long length = strtol(len, NULL, 10);
|
||||
|
||||
do_string(varname, length);
|
||||
if (is_enum == 1)
|
||||
do_enum(varname, ST_FLAG_STRING, length);
|
||||
else
|
||||
do_string(varname, length);
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
if (!strcasecmp(answer[i], "NUMBER")) {
|
||||
printf("Type: NUMBER\n");
|
||||
if (is_enum == 1)
|
||||
do_enum(varname, ST_FLAG_NUMBER, 0);
|
||||
else
|
||||
do_number(varname);
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -378,6 +531,10 @@ static void do_type(const char *varname)
|
|||
|
||||
printf("Type: %s (unrecognized)\n", answer[i]);
|
||||
}
|
||||
/* Fallback for older upsd versions, where STRING|NUMBER is not
|
||||
* appended to ENUM */
|
||||
if (is_enum == 1)
|
||||
do_enum(varname, ST_FLAG_NONE, 0);
|
||||
}
|
||||
|
||||
static void print_rw(const char *varname)
|
||||
|
@ -402,7 +559,7 @@ static void print_rw(const char *varname)
|
|||
static void print_rwlist(void)
|
||||
{
|
||||
int ret;
|
||||
unsigned int numq, numa;
|
||||
size_t numq, numa;
|
||||
const char *query[2];
|
||||
char **answer;
|
||||
struct list_t *lhead, *llast, *ltmp, *lnext;
|
||||
|
@ -436,7 +593,7 @@ static void print_rwlist(void)
|
|||
|
||||
/* RW <upsname> <varname> <value> */
|
||||
if (numa < 4) {
|
||||
fatalx(EXIT_FAILURE, "Error: insufficient data (got %d args, need at least 4)", numa);
|
||||
fatalx(EXIT_FAILURE, "Error: insufficient data (got %zu args, need at least 4)", numa);
|
||||
}
|
||||
|
||||
/* sock this entry away for later */
|
||||
|
@ -473,22 +630,36 @@ static void print_rwlist(void)
|
|||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int i, port;
|
||||
int i;
|
||||
uint16_t port;
|
||||
const char *prog = xbasename(argv[0]);
|
||||
char *password = NULL, *username = NULL, *setvar = NULL;
|
||||
|
||||
while ((i = getopt(argc, argv, "+hs:p:u:V")) != -1) {
|
||||
while ((i = getopt(argc, argv, "+hls:p:t:u:wV")) != -1) {
|
||||
switch (i)
|
||||
{
|
||||
case 's':
|
||||
setvar = optarg;
|
||||
break;
|
||||
case 'l':
|
||||
if (setvar) {
|
||||
upslogx(LOG_WARNING, "Listing mode requested, overriding setvar specified earlier!");
|
||||
setvar = NULL;
|
||||
}
|
||||
break;
|
||||
case 'p':
|
||||
password = optarg;
|
||||
break;
|
||||
case 't':
|
||||
if (!str_to_uint(optarg, &timeout, 10))
|
||||
fatal_with_errno(EXIT_FAILURE, "Could not convert the provided value for timeout ('-t' option) to unsigned int");
|
||||
break;
|
||||
case 'u':
|
||||
username = optarg;
|
||||
break;
|
||||
case 'w':
|
||||
tracking_enabled = 1;
|
||||
break;
|
||||
case 'V':
|
||||
printf("Network UPS Tools %s %s\n", prog, UPS_VERSION);
|
||||
exit(EXIT_SUCCESS);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue