Skip to content

Commit cf14d94

Browse files
committed
drivers/dummy-ups.c: pepper parse_data_file() with debug tracing logs [#3368]
Signed-off-by: Jim Klimov <jimklimov+nut@gmail.com>
1 parent b8c96e7 commit cf14d94

File tree

2 files changed

+43
-6
lines changed

2 files changed

+43
-6
lines changed

drivers/dstate.c

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -196,11 +196,11 @@ static TYPE_FD sock_open(const char *fn)
196196

197197
fd = CreateNamedPipe(
198198
fn, /* pipe name */
199-
PIPE_ACCESS_DUPLEX | /* read/write access */
200-
FILE_FLAG_OVERLAPPED, /* async IO */
201-
PIPE_TYPE_BYTE |
202-
PIPE_READMODE_BYTE |
203-
PIPE_WAIT,
199+
PIPE_ACCESS_DUPLEX /* read/write access */
200+
| FILE_FLAG_OVERLAPPED, /* async IO */
201+
PIPE_TYPE_BYTE
202+
| PIPE_READMODE_BYTE
203+
| PIPE_WAIT,
204204
PIPE_UNLIMITED_INSTANCES, /* max. instances */
205205
ST_SOCK_BUF_LEN, /* output buffer size */
206206
ST_SOCK_BUF_LEN, /* input buffer size */
@@ -236,11 +236,20 @@ static TYPE_FD sock_open(const char *fn)
236236
static void sock_disconnect(conn_t *conn)
237237
{
238238
#ifndef WIN32
239+
if (VALID_FD(conn->fd)) {
240+
upsdebugx(4, "%s: flushing socket %d", __func__, (int)conn->fd);
241+
fflush(conn->fd);
242+
}
239243
upsdebugx(3, "%s: disconnecting socket %d", __func__, (int)conn->fd);
240244
close(conn->fd);
241245
#else /* WIN32 */
242246
/* FIXME NUT_WIN32_INCOMPLETE not sure if this is the right way to close a connection */
243247
if (conn->read_overlapped.hEvent != INVALID_HANDLE_VALUE) {
248+
if (VALID_FD(conn->fd)) {
249+
upsdebugx(4, "%s: flushing named pipe handle %p", __func__, conn->fd);
250+
FlushFileBuffers(conn->fd);
251+
}
252+
upsdebugx(4, "%s: closing not-invalid named pipe handle %p", __func__, conn->fd);
244253
CloseHandle(conn->read_overlapped.hEvent);
245254
conn->read_overlapped.hEvent = INVALID_HANDLE_VALUE;
246255
}
@@ -326,6 +335,7 @@ static void send_to_all(const char *fmt, ...)
326335
if( result == 0 ) {
327336
upsdebugx(2, "%s: write failed on handle %p, disconnecting", __func__, conn->fd);
328337
sock_disconnect(conn);
338+
conn = NULL;
329339
continue;
330340
}
331341
else {
@@ -346,6 +356,7 @@ static void send_to_all(const char *fmt, ...)
346356
upsdebugx(6, "%s: failed write: %s", __func__, buf);
347357

348358
sock_disconnect(conn);
359+
conn = NULL;
349360

350361
/* TOTHINK: Maybe fallback elsewhere in other cases? */
351362
if (ret < 0 && errno == EAGAIN && do_synchronous == -1) {
@@ -471,6 +482,7 @@ static int send_to_one(conn_t *conn, const char *fmt, ...)
471482
#endif /* WIN32 */
472483
upsdebugx(6, "%s: failed write: %s", __func__, buf);
473484
sock_disconnect(conn);
485+
conn = NULL;
474486

475487
/* TOTHINK: Maybe fallback elsewhere in other cases? */
476488
if (ret < 0 && errno == EAGAIN && do_synchronous == -1) {
@@ -996,6 +1008,7 @@ static void sock_read(conn_t *conn)
9961008

9971009
default:
9981010
sock_disconnect(conn);
1011+
conn = NULL;
9991012
return;
10001013
}
10011014
}
@@ -1028,6 +1041,7 @@ static void sock_read(conn_t *conn)
10281041
if (is_closed) {
10291042
upsdebugx(1, "%s: it seems the other side has closed the connection", __func__);
10301043
sock_disconnect(conn);
1044+
conn = NULL;
10311045
return;
10321046
}
10331047
} else {
@@ -1041,6 +1055,7 @@ static void sock_read(conn_t *conn)
10411055
if( res == 0 ) {
10421056
upslogx(LOG_INFO, "Read error : %d",(int)GetLastError());
10431057
sock_disconnect(conn);
1058+
conn = NULL;
10441059
return;
10451060
}
10461061
ret = bytesRead;
@@ -1115,6 +1130,7 @@ static void sock_close(void)
11151130
for (conn = connhead; conn; conn = cnext) {
11161131
cnext = conn->next;
11171132
sock_disconnect(conn);
1133+
conn = NULL;
11181134
}
11191135

11201136
connhead = NULL;
@@ -1249,6 +1265,7 @@ int dstate_poll_fds(struct timeval timeout, TYPE_FD arg_extrafd)
12491265

12501266
if (conn->closing) {
12511267
sock_disconnect(conn);
1268+
conn = NULL;
12521269
}
12531270
}
12541271

@@ -1338,6 +1355,7 @@ int dstate_poll_fds(struct timeval timeout, TYPE_FD arg_extrafd)
13381355

13391356
if (conn->closing) {
13401357
sock_disconnect(conn);
1358+
conn = NULL;
13411359
}
13421360
}
13431361

drivers/dummy-ups.c

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -786,24 +786,35 @@ static int parse_data_file(TYPE_FD arg_upsfd)
786786

787787
if (now < next_update)
788788
{
789-
upsdebugx(1, "leaving (paused)...");
789+
upsdebugx(1, "%s: leaving (paused)...", __func__);
790790
return 1;
791791
}
792792

793793
/* initialise everything, to loop back at the beginning of the file */
794794
if (ctx == NULL)
795795
{
796+
upsdebugx(2, "%s: (re-)initialize PCONF context", __func__);
796797
ctx = (PCONF_CTX_t *)xmalloc(sizeof(PCONF_CTX_t));
798+
if (!ctx) {
799+
upsdebugx(1, "%s: failed to malloc()", __func__);
800+
return 1;
801+
}
797802

803+
upsdebugx(5, "%s: call prepare_filepath()", __func__);
798804
prepare_filepath(fn, sizeof(fn));
805+
806+
upsdebugx(5, "%s: got '%s', call pconf_init()", __func__, fn);
799807
pconf_init(ctx, upsconf_err);
800808

809+
upsdebugx(5, "%s: call pconf_file_begin()", __func__);
801810
if (!pconf_file_begin(ctx, fn))
802811
fatalx(EXIT_FAILURE, "Can't open dummy-ups definition file %s: %s",
803812
fn, ctx->errmsg);
804813

805814
/* we need this for parsing alarm instructions later */
815+
upsdebugx(5, "%s: call status_init()", __func__);
806816
status_init(); /* in case no ups.status does it */
817+
upsdebugx(5, "%s: call alarm_init()", __func__);
807818
alarm_init(); /* reset alarms at start of parsing */
808819
}
809820

@@ -812,6 +823,7 @@ static int parse_data_file(TYPE_FD arg_upsfd)
812823
next_update = -1;
813824

814825
/* Now start or continue parsing... */
826+
upsdebugx(3, "%s: proceed parsing PCONF context", __func__);
815827
while (pconf_file_next(ctx))
816828
{
817829
if (pconf_parse_error(ctx))
@@ -822,6 +834,10 @@ static int parse_data_file(TYPE_FD arg_upsfd)
822834
}
823835

824836
/* Check if we have something to process */
837+
upsdebugx(4, "%s: %s:%d: numargs:%d token:%s",
838+
__func__, fn, ctx->linenum, ctx->numargs,
839+
(ctx->numargs < 1 ? "<null>" : ctx->arglist[0])
840+
);
825841
if (ctx->numargs < 1)
826842
continue;
827843

@@ -920,9 +936,12 @@ static int parse_data_file(TYPE_FD arg_upsfd)
920936
/* Cleanup parseconf if there is no pending action */
921937
if (next_update == -1)
922938
{
939+
upsdebugx(3, "%s: clean up PCONF context: no pending action", __func__);
923940
pconf_finish(ctx);
924941
free(ctx);
925942
ctx=NULL;
926943
}
944+
945+
upsdebugx(3, "%s: leaving (finished)", __func__);
927946
return 1;
928947
}

0 commit comments

Comments
 (0)