Imported Upstream version 2.6.4

This commit is contained in:
Arnaud Quette 2012-06-01 15:55:19 +02:00
parent fad6ced6f6
commit fefe62b2bd
257 changed files with 6020 additions and 1394 deletions

View file

@ -23,7 +23,20 @@
#ifdef HAVE_PTHREAD
#include <pthread.h>
#endif
#include <ltdl.h>
/* dynamic link library stuff */
static char * libname = "libupsclient";
static lt_dlhandle dl_handle = NULL;
static const char *dl_error = NULL;
static int (*nut_upscli_splitaddr)(const char *buf,char **hostname, int *port);
static int (*nut_upscli_tryconnect)(UPSCONN_t *ups, const char *host, int port,
int flags,struct timeval * timeout);
static int (*nut_upscli_list_start)(UPSCONN_t *ups, unsigned int numq,
const char **query);
static int (*nut_upscli_list_next)(UPSCONN_t *ups, unsigned int numq,
const char **query,unsigned int *numa, char ***answer);
static nutscan_device_t * dev_ret = NULL;
#ifdef HAVE_PTHREAD
@ -35,6 +48,64 @@ struct scan_nut_arg {
long timeout;
};
/* return 0 on error */
int nutscan_load_upsclient_library()
{
if( dl_handle != NULL ) {
/* if previous init failed */
if( dl_handle == (void *)1 ) {
return 0;
}
/* init has already been done */
return 1;
}
if( lt_dlinit() != 0 ) {
fprintf(stderr, "Error initializing lt_init\n");
return 0;
}
dl_handle = lt_dlopenext(libname);
if (!dl_handle) {
dl_error = lt_dlerror();
goto err;
}
lt_dlerror(); /* Clear any existing error */
*(void **) (&nut_upscli_splitaddr) = lt_dlsym(dl_handle,
"upscli_splitaddr");
if ((dl_error = lt_dlerror()) != NULL) {
goto err;
}
*(void **) (&nut_upscli_tryconnect) = lt_dlsym(dl_handle,
"upscli_tryconnect");
if ((dl_error = lt_dlerror()) != NULL) {
goto err;
}
*(void **) (&nut_upscli_list_start) = lt_dlsym(dl_handle,
"upscli_list_start");
if ((dl_error = lt_dlerror()) != NULL) {
goto err;
}
*(void **) (&nut_upscli_list_next) = lt_dlsym(dl_handle,
"upscli_list_next");
if ((dl_error = lt_dlerror()) != NULL) {
goto err;
}
return 1;
err:
fprintf(stderr, "Cannot load NUT library (%s) : %s. NUT search disabled.\n", libname, dl_error);
dl_handle = (void *)1;
lt_dlexit();
return 0;
}
/* FIXME: SSL support */
static void * list_nut_devices(void * arg)
{
@ -56,28 +127,32 @@ static void * list_nut_devices(void * arg)
query[0] = "UPS";
numq = 1;
if (upscli_splitaddr(target_hostname, &hostname, &port) != 0) {
if ((*nut_upscli_splitaddr)(target_hostname, &hostname, &port) != 0) {
free(target_hostname);
free(nut_arg);
free(ups);
return NULL;
}
if (upscli_tryconnect(ups, hostname, port,UPSCLI_CONN_TRYSSL,&tv) < 0) {
if ((*nut_upscli_tryconnect)(ups, hostname, port,UPSCLI_CONN_TRYSSL,&tv) < 0) {
free(target_hostname);
free(nut_arg);
free(ups);
return NULL;
}
if(upscli_list_start(ups, numq, query) < 0) {
if((*nut_upscli_list_start)(ups, numq, query) < 0) {
free(target_hostname);
free(nut_arg);
free(ups);
return NULL;
}
while (upscli_list_next(ups, numq, query, &numa, &answer) == 1) {
while ((*nut_upscli_list_next)(ups,numq, query, &numa, &answer) == 1) {
/* UPS <upsname> <description> */
if (numa < 3) {
free(target_hostname);
free(nut_arg);
free(ups);
return NULL;
}
/* FIXME: check for duplication by getting driver.port and device.serial
@ -109,6 +184,7 @@ static void * list_nut_devices(void * arg)
free(target_hostname);
free(nut_arg);
free(ups);
return NULL;
}