@@ -2007,6 +2007,8 @@ int upscli_splitaddr(const char *buf, char **hostname, uint16_t *port)
20072007
20082008int upscli_disconnect (UPSCONN_t * ups )
20092009{
2010+ char tmp [UPSCLI_NETBUF_LEN ];
2011+
20102012 if (!ups ) {
20112013 return -1 ;
20122014 }
@@ -2026,6 +2028,23 @@ int upscli_disconnect(UPSCONN_t *ups)
20262028
20272029 net_write (ups , "LOGOUT\n" , 7 , 0 );
20282030
2031+ /* Give it a bit of time to gracefully close connections,
2032+ * drain the buffer and avoid noise in logs of upsd like:
2033+ * write() failed for 127.0.0.1: Transport endpoint is not connected
2034+ */
2035+ if (net_read (ups , tmp , sizeof (tmp ), 5 ) > 0 ) {
2036+ if (!strcmp (tmp , "OK Goodbye" )) {
2037+ /* There may be trailing garbage from the buffer after the newline, not sure why */
2038+ upsdebugx (1 , "%s: We logged out, and server said '%s' nicely, as expected" , __func__ , tmp );
2039+ } else if (!strncmp (tmp , "OK" , 2 )) {
2040+ upsdebugx (1 , "%s: We logged out, and server said '%s' nicely, good enough" , __func__ , tmp );
2041+ } else {
2042+ upsdebugx (1 , "%s: We logged out, and server said '%s', not OK but oh well" , __func__ , tmp );
2043+ }
2044+ } else {
2045+ upsdebugx (1 , "%s: We logged out, and server did not reply in a short time frame" , __func__ );
2046+ }
2047+
20292048#ifdef WITH_OPENSSL
20302049 if (ups -> ssl ) {
20312050 SSL_shutdown (ups -> ssl );
0 commit comments