summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 4ec014a)
raw | patch | inline | side by side (parent: 4ec014a)
author | octo <octo> | |
Sat, 20 May 2006 10:37:15 +0000 (10:37 +0000) | ||
committer | octo <octo> | |
Sat, 20 May 2006 10:37:15 +0000 (10:37 +0000) |
Improved the config function: Set `global_host' to NULL if not configured and copy the value otherwise. Don't use a static variable within the config routine and reference to it..
src/apcups.c | patch | blob | history |
diff --git a/src/apcups.c b/src/apcups.c
index 4f5f760ebb12daa5e2dad73625ae5a11e7a01dfe..cec39df72d3814d5f7ecf617ca83b91dfaf1f962 100644 (file)
--- a/src/apcups.c
+++ b/src/apcups.c
#define MODULE_NAME "apcups"
/* Default values for contacting daemon */
-static char *host = "localhost";
-static int port = NISPORT;
+static char *global_host = NULL;
+static int global_port = NISPORT;
/*
* The following are only if not compiled to test the module with its own main.
* It is possible that the total bytes require in several
* read requests
*/
-static int read_nbytes(int fd, char *ptr, int nbytes)
+static int read_nbytes (int fd, char *ptr, int nbytes)
{
int nleft, nread;
while (nleft > 0) {
do {
- nread = read(fd, ptr, nleft);
+ nread = read (fd, ptr, nleft);
} while (nread == -1 && (errno == EINTR || errno == EAGAIN));
if (nread <= 0) {
* Write nbytes to the network.
* It may require several writes.
*/
-static int write_nbytes(int fd, void *buf, int buflen)
+static int write_nbytes (int fd, void *buf, int buflen)
{
int nleft;
int nwritten;
nleft = buflen;
while (nleft > 0)
{
- nwritten = write(fd, ptr, nleft);
+ nwritten = write (fd, ptr, nleft);
if (nwritten <= 0)
{
}
return (sd);
-} /* int net_open(char *host, char *service, int port) */
+} /* int net_open (char *host, char *service, int port) */
/*
* Receive a message from the other end. Each message consists of
if (nbytes != sizeof (short))
return (-2);
- pktsiz = ntohs(pktsiz);
+ pktsiz = ntohs (pktsiz);
if (pktsiz > buflen)
{
DBG ("record length too large");
/* send short containing size of data packet */
packet_size = htons ((short) len);
- rc = write_nbytes(sockfd, &packet_size, sizeof (packet_size));
- if (rc != sizeof(packet_size))
+ rc = write_nbytes (sockfd, &packet_size, sizeof (packet_size));
+ if (rc != sizeof (packet_size))
return (-1);
/* send data packet */
}
/* Get and print status from apcupsd NIS server */
-static int do_pthreads_status (char *host, int port,
+static int apc_query_server (char *host, int port,
struct apc_detail_s *apcups_detail)
{
int sockfd;
}
#ifdef APCMAIN
-int main(int argc, char **argv)
+int main (int argc, char **argv)
{
/* we are not really going to use this */
struct apc_detail_s apcups_detail;
- if (!*host || strcmp(host, "0.0.0.0") == 0)
+ if (!*host || strcmp (host, "0.0.0.0") == 0)
host = "localhost";
- do_pthreads_status(host, port, &apcups_detail);
+ apc_query_server (global_host, global_port, &apcups_detail);
return 0;
}
#else
static int apcups_config (char *key, char *value)
{
- static char lhost[126];
-
- if (strcasecmp (key, "host") == 0)
- {
- lhost[0] = '\0';
- strcpy(lhost,key);
- host = lhost;
- }
- else if (strcasecmp (key, "Port") == 0)
- {
- int port_tmp = atoi (value);
- if(port_tmp < 1 || port_tmp > 65535) {
- syslog (LOG_WARNING, "apcups: `port' failed: %s",
- value);
- return (1);
- } else {
- port = port_tmp;
- }
- }
- else
- {
- return (-1);
- }
- return(0);
+ if (strcasecmp (key, "host") == 0)
+ {
+ if (global_host != NULL)
+ {
+ free (global_host);
+ global_host = NULL;
+ }
+ if ((global_host = strdup (value)) == NULL)
+ return (1);
+ }
+ else if (strcasecmp (key, "Port") == 0)
+ {
+ int port_tmp = atoi (value);
+ if (port_tmp < 1 || port_tmp > 65535)
+ {
+ syslog (LOG_WARNING, "apcups plugin: Invalid port: %i", port_tmp);
+ return (1);
+ }
+ global_port = port_tmp;
+ }
+ else
+ {
+ return (-1);
+ }
+ return (0);
}
static void apcups_init (void)
struct apc_detail_s apcups_detail;
int status;
- if (host == NULL)
+ if (global_host == NULL)
return;
apcups_detail.linev = -1.0;
apcups_detail.itemp = -300.0;
apcups_detail.linefreq = -1.0;
- status = do_pthreads_status(host, port, &apcups_detail);
+ status = apc_query_server (global_host, global_port, &apcups_detail);
/*
* if we did not connect then do not bother submitting