summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: b366118)
raw | patch | inline | side by side (parent: b366118)
author | Florian Forster <octo@huhu.verplant.org> | |
Fri, 6 Apr 2007 11:33:32 +0000 (13:33 +0200) | ||
committer | Florian Forster <octo@huhu.verplant.org> | |
Fri, 6 Apr 2007 11:33:32 +0000 (13:33 +0200) |
src/nut.c | patch | blob | history |
diff --git a/src/nut.c b/src/nut.c
index d839ba49a460b9bcde35e41f23008e961d25d432..f6b9fafd4c5f441843949362ad7d1f7649f73494 100644 (file)
--- a/src/nut.c
+++ b/src/nut.c
typedef struct nut_ups_s nut_ups_t;
struct nut_ups_s
{
- UPSCONN conn;
+ UPSCONN *conn;
char *upsname;
char *hostname;
int port;
static void free_nut_ups_t (nut_ups_t *ups)
{
- upscli_disconnect (&ups->conn);
- sfree (ups->hostname);
- sfree (ups->upsname);
- sfree (ups);
+ if (ups->conn != NULL)
+ {
+ upscli_disconnect (ups->conn);
+ sfree (ups->conn);
+ }
+ sfree (ups->hostname);
+ sfree (ups->upsname);
+ sfree (ups);
} /* void free_nut_ups_t */
static int nut_add_ups (const char *name)
return (1);
}
- status = upscli_connect (&ups->conn, ups->hostname, ups->port,
- UPSCLI_CONN_TRYSSL);
- if (status != 0)
- {
- ERROR ("nut plugin: nut_add_ups: upscli_connect (%s, %i) failed: %s",
- ups->hostname, ups->port, upscli_strerror (&ups->conn));
- free_nut_ups_t (ups);
- return (1);
- }
-
if (upslist_head == NULL)
upslist_head = ups;
else
unsigned int answer_num;
int status;
+ /* (Re-)Connect if we have no connection */
+ if (ups->conn == NULL)
+ {
+ ups->conn = (UPSCONN *) malloc (sizeof (UPSCONN));
+ if (ups->conn == NULL)
+ {
+ ERROR ("nut plugin: malloc failed.");
+ return (-1);
+ }
+
+ status = upscli_connect (ups->conn, ups->hostname, ups->port,
+ UPSCLI_CONN_TRYSSL);
+ if (status != 0)
+ {
+ ERROR ("nut plugin: nut_read_one: upscli_connect (%s, %i) failed: %s",
+ ups->hostname, ups->port, upscli_strerror (ups->conn));
+ sfree (ups->conn);
+ return (-1);
+ }
+
+ INFO ("nut plugin: Connection to (%s, %i) established.",
+ ups->hostname, ups->port);
+ } /* if (ups->conn == NULL) */
+
/* nut plugin: nut_read_one: upscli_list_start (adpos) failed: Protocol
* error */
- status = upscli_list_start (&ups->conn, query_num, query);
+ status = upscli_list_start (ups->conn, query_num, query);
if (status != 0)
{
ERROR ("nut plugin: nut_read_one: upscli_list_start (%s) failed: %s",
- ups->upsname, upscli_strerror (&ups->conn));
+ ups->upsname, upscli_strerror (ups->conn));
+ upscli_disconnect (ups->conn);
+ sfree (ups->conn);
return (-1);
}
- while ((status = upscli_list_next (&ups->conn, query_num, query,
+ while ((status = upscli_list_next (ups->conn, query_num, query,
&answer_num, &answer)) == 1)
{
char *key;