Skip to content

Commit 95e2d98

Browse files
authored
Merge pull request #2552 from jimklimov/issue-266-intro
Update log messages and comments in "state" machinery
2 parents 8bbbb9e + 55f016b commit 95e2d98

File tree

8 files changed

+75
-42
lines changed

8 files changed

+75
-42
lines changed

common/state.c

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
2003 Russell Kroll <[email protected]>
55
2008 Arjen de Korte <[email protected]>
66
2012 Arnaud Quette <[email protected]>
7+
2020-2024 Jim Klimov <[email protected]>
78
89
This program is free software; you can redistribute it and/or modify
910
it under the terms of the GNU General Public License as published by
@@ -295,6 +296,7 @@ int state_setinfo(st_tree_t **nptr, const char *var, const char *val)
295296

296297
/* changes should be ignored */
297298
if (node->flags & ST_FLAG_IMMUTABLE) {
299+
upsdebugx(6, "%s: not changing immutable variable [%s]", __func__, var);
298300
return 0; /* no change */
299301
}
300302

@@ -357,8 +359,8 @@ int state_addenum(st_tree_t *root, const char *var, const char *val)
357359
sttmp = state_tree_find(root, var);
358360

359361
if (!sttmp) {
360-
upslogx(LOG_ERR, "state_addenum: base variable (%s) "
361-
"does not exist", var);
362+
upslogx(LOG_ERR, "%s: base variable (%s) "
363+
"does not exist", __func__, var);
362364
return 0; /* failed */
363365
}
364366

@@ -400,17 +402,17 @@ int state_addrange(st_tree_t *root, const char *var, const int min, const int ma
400402

401403
/* sanity check */
402404
if (min > max) {
403-
upslogx(LOG_ERR, "state_addrange: min is superior to max! (%i, %i)",
404-
min, max);
405+
upslogx(LOG_ERR, "%s: min is superior to max! (%i, %i)",
406+
__func__, min, max);
405407
return 0;
406408
}
407409

408410
/* find the tree node for var */
409411
sttmp = state_tree_find(root, var);
410412

411413
if (!sttmp) {
412-
upslogx(LOG_ERR, "state_addrange: base variable (%s) "
413-
"does not exist", var);
414+
upslogx(LOG_ERR, "%s: base variable (%s) "
415+
"does not exist", __func__, var);
414416
return 0; /* failed */
415417
}
416418

@@ -427,8 +429,8 @@ int state_setaux(st_tree_t *root, const char *var, const char *auxs)
427429
sttmp = state_tree_find(root, var);
428430

429431
if (!sttmp) {
430-
upslogx(LOG_ERR, "state_addenum: base variable (%s) "
431-
"does not exist", var);
432+
upslogx(LOG_ERR, "%s: base variable (%s) "
433+
"does not exist", __func__, var);
432434
return -1; /* failed */
433435
}
434436

@@ -524,8 +526,8 @@ void state_setflags(st_tree_t *root, const char *var, size_t numflags, char **fl
524526
sttmp = state_tree_find(root, var);
525527

526528
if (!sttmp) {
527-
upslogx(LOG_ERR, "state_setflags: base variable (%s) "
528-
"does not exist", var);
529+
upslogx(LOG_ERR, "%s: base variable (%s) "
530+
"does not exist", __func__, var);
529531
return;
530532
}
531533

@@ -549,7 +551,7 @@ void state_setflags(st_tree_t *root, const char *var, size_t numflags, char **fl
549551
continue;
550552
}
551553

552-
upsdebugx(2, "Unrecognized flag [%s]", flag[i]);
554+
upsdebugx(2, "%s: Unrecognized flag [%s]", __func__, flag[i]);
553555
}
554556
}
555557

docs/net-protocol.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ Response:
142142
- 'ENUM': an enumerated type, which supports a few specific values
143143
- 'STRING:n': this is a string of maximum length n
144144
- 'RANGE': this is an numeric, either integer or float, comprised in the
145-
range (see LIST RANGE)
145+
range (see LIST RANGE)
146146
- 'NUMBER': this is a simple numeric value, either integer or float
147147

148148
ENUM, STRING and RANGE are usually associated with RW, but not always.
@@ -153,7 +153,8 @@ float.
153153
Note that float values are expressed using decimal (base 10) english-based
154154
representation, so using a dot, in non-scientific notation. So hexadecimal,
155155
exponents, and comma for thousands separator are forbidden.
156-
For example: "1200.20" is valid, while "1,200.20" and "1200,20" are invalid.
156+
For example: "1200.20" is valid, while "1,200.20" and "1200,20" and "1.2e4"
157+
are invalid.
157158

158159

159160
This replaces the old "VARTYPE" command.

docs/sock-protocol.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,14 @@ SETFLAGS
108108

109109
Note that this command takes a variable number of arguments, as multiple
110110
flags are supported. Also note that they are not crammed together in
111-
"", since "RW STRING" would mean something completely different.
111+
"" quotes, since "RW STRING" would mean something completely different.
112112

113113
This also replaces any previous flags for a given variable.
114114

115+
Currently supported flags include `RW`, `STRING` and `NUMBER`
116+
(detailed in the NUT Network Protocol documentation); unrecognized values
117+
are quietly ignored.
118+
115119
ADDCMD
116120
~~~~~~
117121

drivers/dstate.c

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ static void send_to_all(const char *fmt, ...)
282282

283283
result = WriteFile (conn->fd, buf, buflen, &bytesWritten, NULL);
284284
if( result == 0 ) {
285-
upsdebugx(2, "write failed on handle %p, disconnecting", conn->fd);
285+
upsdebugx(2, "%s: write failed on handle %p, disconnecting", __func__, conn->fd);
286286
sock_disconnect(conn);
287287
continue;
288288
}
@@ -301,7 +301,7 @@ static void send_to_all(const char *fmt, ...)
301301
"handle %p failed (ret=%" PRIiSIZE "), disconnecting: %s",
302302
__func__, buflen, conn->fd, ret, strerror(errno));
303303
#endif
304-
upsdebugx(6, "failed write: %s", buf);
304+
upsdebugx(6, "%s: failed write: %s", __func__, buf);
305305

306306
sock_disconnect(conn);
307307

@@ -427,7 +427,7 @@ static int send_to_one(conn_t *conn, const char *fmt, ...)
427427
"handle %p failed (ret=%" PRIiSIZE "), disconnecting: %s",
428428
__func__, buflen, conn->fd, ret, strerror(errno));
429429
#endif
430-
upsdebugx(6, "failed write: %s", buf);
430+
upsdebugx(6, "%s: failed write: %s", __func__, buf);
431431
sock_disconnect(conn);
432432

433433
/* TOTHINK: Maybe fallback elsewhere in other cases? */
@@ -475,7 +475,7 @@ static void sock_connect(TYPE_FD sock)
475475
fd = accept(sock, (struct sockaddr *) &sa, &salen);
476476

477477
if (INVALID_FD(fd)) {
478-
upslog_with_errno(LOG_ERR, "accept on unix fd failed");
478+
upslog_with_errno(LOG_ERR, "%s: accept on unix fd failed", __func__);
479479
return;
480480
}
481481

@@ -491,15 +491,15 @@ static void sock_connect(TYPE_FD sock)
491491
ret = fcntl(fd, F_GETFL, 0);
492492

493493
if (ret < 0) {
494-
upslog_with_errno(LOG_ERR, "fcntl get on unix fd failed");
494+
upslog_with_errno(LOG_ERR, "%s: fcntl get on unix fd failed", __func__);
495495
close(fd);
496496
return;
497497
}
498498

499499
ret = fcntl(fd, F_SETFL, ret | O_NDELAY);
500500

501501
if (ret < 0) {
502-
upslog_with_errno(LOG_ERR, "fcntl set O_NDELAY on unix fd failed");
502+
upslog_with_errno(LOG_ERR, "%s: fcntl set O_NDELAY on unix fd failed", __func__);
503503
close(fd);
504504
return;
505505
}
@@ -581,9 +581,9 @@ static void sock_connect(TYPE_FD sock)
581581
connhead = conn;
582582

583583
#ifndef WIN32
584-
upsdebugx(3, "new connection on fd %d", fd);
584+
upsdebugx(3, "%s: new connection on fd %d", __func__, fd);
585585
#else
586-
upsdebugx(3, "new connection on handle %p", sock);
586+
upsdebugx(3, "%s: new connection on handle %p", __func__, sock);
587587
#endif
588588

589589
}
@@ -685,8 +685,8 @@ static int sock_arg(conn_t *conn, size_t numarg, char **arg)
685685
char *sockfn = pipename; /* Just for the report below; not a global var in WIN32 builds */
686686
#endif
687687

688-
upsdebugx(6, "Driver on %s is now handling %s with %" PRIuSIZE " args",
689-
sockfn, numarg ? arg[0] : "<skipped: no command>", numarg);
688+
upsdebugx(6, "%s: Driver on %s is now handling %s with %" PRIuSIZE " args",
689+
__func__, sockfn, numarg ? arg[0] : "<skipped: no command>", numarg);
690690

691691
if (numarg < 1) {
692692
return 0;
@@ -1037,9 +1037,9 @@ char * dstate_init(const char *prog, const char *devname)
10371037
sockfd = sock_open(sockname);
10381038

10391039
#ifndef WIN32
1040-
upsdebugx(2, "dstate_init: sock %s open on fd %d", sockname, sockfd);
1040+
upsdebugx(2, "%s: sock %s open on fd %d", __func__, sockname, sockfd);
10411041
#else
1042-
upsdebugx(2, "dstate_init: sock %s open on handle %p", sockname, sockfd);
1042+
upsdebugx(2, "%s: sock %s open on handle %p", __func__, sockname, sockfd);
10431043
#endif
10441044

10451045
/* NOTE: Caller must free this string */
@@ -1112,7 +1112,7 @@ int dstate_poll_fds(struct timeval timeout, TYPE_FD arg_extrafd)
11121112
break;
11131113

11141114
default:
1115-
upslog_with_errno(LOG_ERR, "select unix sockets failed");
1115+
upslog_with_errno(LOG_ERR, "%s: select unix sockets failed", __func__);
11161116
}
11171117

11181118
return overrun;
@@ -1189,7 +1189,7 @@ int dstate_poll_fds(struct timeval timeout, TYPE_FD arg_extrafd)
11891189
}
11901190

11911191
if (ret == WAIT_FAILED) {
1192-
upslog_with_errno(LOG_ERR, "waitfor failed");
1192+
upslog_with_errno(LOG_ERR, "%s: waitfor failed", __func__);
11931193
return overrun;
11941194
}
11951195

@@ -1390,7 +1390,7 @@ void dstate_setaux(const char *var, long aux)
13901390
sttmp = state_tree_find(dtree_root, var);
13911391

13921392
if (!sttmp) {
1393-
upslogx(LOG_ERR, "dstate_setaux: base variable (%s) does not exist", var);
1393+
upslogx(LOG_ERR, "%s: base variable (%s) does not exist", __func__, var);
13941394
return;
13951395
}
13961396

drivers/main.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,9 @@ void dparam_setinfo(const char *var, const char *val)
269269
{
270270
char vtmp[SMALLBUF];
271271

272-
/* store these in dstate for debugging and other help */
272+
/* store these in dstate for debugging and other help
273+
* note these are not immutable since we can reload
274+
*/
273275
if (val) {
274276
snprintf(vtmp, sizeof(vtmp), "driver.parameter.%s", var);
275277
dstate_setinfo(vtmp, "%s", val);
@@ -304,6 +306,10 @@ void storeval(const char *var, char *val)
304306
/* NOTE: No regard for VAR_SENSITIVE here */
305307
dstate_setinfo(var+9, "%s", val);
306308
dstate_setflags(var+9, ST_FLAG_IMMUTABLE);
309+
310+
/* note these are not immutable since we can reload
311+
* although the effect of this is questionable (FIXME)
312+
*/
307313
dparam_setinfo(var, val);
308314
return;
309315
}

server/netget.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,11 @@ static void get_type(nut_ctype_t *client, const char *upsname, const char *var)
157157
/* Any variable that is not string | range | enum is just a simple
158158
* numeric value */
159159

160+
if (!(node->flags & ST_FLAG_NUMBER)) {
161+
upsdebugx(3, "%s: assuming that UPS[%s] variable %s which has no type flag is a NUMBER",
162+
__func__, upsname, var);
163+
}
164+
160165
sendback(client, "%s NUMBER\n", buf);
161166
}
162167

server/netset.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,19 @@ static void set_var(nut_ctype_t *client, const char *upsname, const char *var,
5656
val = sstate_getinfo(ups, var);
5757

5858
if (!val) {
59+
/* Technically, this includes state tree entries
60+
* with a defined name but null value so far.
61+
* FIXME? Use sstate_getnode() to differentiate
62+
* the cases? Any practical use for this?
63+
*/
5964
send_err(client, NUT_ERR_VAR_NOT_SUPPORTED);
6065
return;
6166
}
6267

6368
/* make sure this variable is writable (RW) */
6469
if ((sstate_getflags(ups, var) & ST_FLAG_RW) == 0) {
70+
upsdebugx(3, "%s: UPS [%s]: value of %s is read-only",
71+
__func__, ups->name, var);
6572
send_err(client, NUT_ERR_READONLY);
6673
return;
6774
}
@@ -87,6 +94,8 @@ static void set_var(nut_ctype_t *client, const char *upsname, const char *var,
8794
* even on architectures with a moderate INTMAX
8895
*/
8996
if (aux < (int) strlen(newval)) {
97+
upsdebugx(3, "%s: UPS [%s]: Not a value fitting in STRING:%ld %s: %s",
98+
__func__, ups->name, aux, var, newval);
9099
send_err(client, NUT_ERR_TOO_LONG);
91100
return;
92101
}
@@ -109,6 +118,9 @@ static void set_var(nut_ctype_t *client, const char *upsname, const char *var,
109118
}
110119

111120
if (!found) {
121+
/* Not a value known in the pre-defined enumeration */
122+
upsdebugx(3, "%s: UPS [%s]: Not a value known in ENUM %s: %s",
123+
__func__, ups->name, var, newval);
112124
send_err(client, NUT_ERR_INVALID_VALUE);
113125
return;
114126
}
@@ -132,6 +144,9 @@ static void set_var(nut_ctype_t *client, const char *upsname, const char *var,
132144
}
133145

134146
if (!found) {
147+
/* Not in range */
148+
upsdebugx(3, "%s: UPS [%s]: Not a value fitting in RANGE %s: %s",
149+
__func__, ups->name, var, newval);
135150
send_err(client, NUT_ERR_INVALID_VALUE);
136151
return;
137152
}

0 commit comments

Comments
 (0)