@@ -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)
236236static 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
0 commit comments