From 3855d8f3a6310d739d35ba9fa86c35d813abf859 Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Thu, 13 Sep 2012 10:05:35 +0200 Subject: [PATCH] apcups plugin: Add function net_shutdown(). --- src/apcups.c | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/src/apcups.c b/src/apcups.c index d6a35c50..60ba7f43 100644 --- a/src/apcups.c +++ b/src/apcups.c @@ -81,26 +81,31 @@ static const char *config_keys[] = }; static int config_keys_num = 2; -/* Close the network connection */ -static int apcups_shutdown (void) +static int net_shutdown (int *fd) { uint16_t packet_size = 0; - if (global_sockfd < 0) - return (0); + if ((fd == NULL) || (*fd < 0)) + return (EINVAL); - DEBUG ("Gracefully shutting down socket %i.", global_sockfd); + swrite (*fd, (void *) &packet_size, sizeof (packet_size)); + close (*fd); + *fd = -1; - /* send EOF sentinel */ - swrite (global_sockfd, (void *) &packet_size, sizeof (packet_size)); + return (0); +} /* int net_shutdown */ - close (global_sockfd); - global_sockfd = -1; +/* Close the network connection */ +static int apcups_shutdown (void) +{ + if (global_sockfd < 0) + return (0); + net_shutdown (&global_sockfd); return (0); } /* int apcups_shutdown */ -/* +/* * Open a TCP connection to the UPS network server * Returns -1 on error * Returns socket file descriptor otherwise -- 2.30.2