summaryrefslogtreecommitdiffstats
path: root/src/openlmi.c
diff options
context:
space:
mode:
authorRadek Novacek <rnovacek@redhat.com>2013-10-30 15:51:12 +0100
committerRadek Novacek <rnovacek@redhat.com>2013-10-31 08:53:46 +0100
commit49394a84fe1235b692fc32d903bf3486e41f76b4 (patch)
treee702181ad69ea995e48ce103f1f3e2b90fa97597 /src/openlmi.c
parent793843369a4445f8602ef176b41c828730cb4404 (diff)
downloadopenlmi-providers-49394a84fe1235b692fc32d903bf3486e41f76b4.tar.gz
openlmi-providers-49394a84fe1235b692fc32d903bf3486e41f76b4.tar.xz
openlmi-providers-49394a84fe1235b692fc32d903bf3486e41f76b4.zip
Add lmi_get_computer_system function and fix lmi_get_system_name
PG_ComputerSystem has different method how to get hostname than our providers. In order to create the associations to this class we need to enumerate it. The downside is that all providers must supply CMPIContext to the lmi_init function. New function lmi_get_computer_system returns CMPIObjectPath to the configured CIM_ComputerSystem subclass instance. This object should be used in all references with ComputerSystem. Function lmi_get_system_name has been altered to return same value as ComputerSystem "Name" property.
Diffstat (limited to 'src/openlmi.c')
-rw-r--r--src/openlmi.c120
1 files changed, 69 insertions, 51 deletions
diff --git a/src/openlmi.c b/src/openlmi.c
index de21d3c..1dc1b62 100644
--- a/src/openlmi.c
+++ b/src/openlmi.c
@@ -30,7 +30,8 @@
#include <cmpimacs.h>
#include <glib.h>
-static char *_fqdn = NULL;
+static char *_system_name = NULL;
+static CMPIObjectPath *_computer_system = NULL;
static const CMPIBroker *_cb = NULL;
static char *_provider = NULL;
@@ -60,62 +61,76 @@ static ConfigEntry _toplevel_config_defaults[] = {
#define TOPLEVEL_CONFIG_FILE "/etc/openlmi/openlmi.conf"
#define PROVIDER_CONFIG_FILE "/etc/openlmi/%s/%s.conf"
-/** Gets Fully Quantified Domain Name of the system
- * @return FQDN (must be freed by the caller)
+/**
+ * Enumerate configured ComputerSystem class in order to obtain
+ * ComputerSystem ObjectPath and Hostname
+ *
+ * @param cb CMPIBroker
+ * @param ctx CMPIContext
+ * @retval true Success
+ * @retval false Failure
*/
-static char *getFQDN(void)
+static bool get_computer_system(const CMPIBroker *cb, const CMPIContext *ctx)
{
- struct utsname uts;
- if ((uname(&uts) > 0) && (strlen(uts.nodename) > 0)) {
- char *nodename;
- if ((nodename = strdup(uts.nodename)) == NULL) {
- lmi_error("Memory allocation failed");
- return NULL;
- }
- return nodename;
+ free(_system_name);
+ _system_name = NULL;
+ if (_computer_system != NULL) {
+ CMRelease(_computer_system);
+ _computer_system = NULL;
}
- char hostname[256];
- hostname[255] = '\0';
- if (gethostname(hostname, 255) == -1) {
- // FIXME: what to do, if we can't use gethostname?
- return NULL;
+
+ const char *class_name = lmi_get_system_creation_class_name();
+ const char *namespace = lmi_read_config("CIM", "Namespace");
+ CMPIStatus rc = { 0, NULL };
+ CMPIObjectPath *op = CMNewObjectPath(cb, namespace, class_name, &rc);
+ if (rc.rc != CMPI_RC_OK) {
+ lmi_error("Unable to create object path: %s", rc.msg);
+ return false;
+ }
+ CMPIEnumeration *en = CBEnumInstanceNames(cb, ctx, op, &rc);
+ if (rc.rc != CMPI_RC_OK) {
+ lmi_error("Unable to enumerate instance names of class %s", class_name);
+ return false;
}
- struct addrinfo hints;
- struct addrinfo *info = NULL, *p;
- memset(&hints, 0, sizeof hints);
- hints.ai_family = AF_UNSPEC; // either IPV4 or IPV6
- hints.ai_socktype = SOCK_STREAM;
- hints.ai_flags = AI_CANONNAME;
-
- if (getaddrinfo(hostname, "http", &hints, &info) == 0) {
- for (p = info; p != NULL; p = p->ai_next) {
- if (p->ai_canonname && strstr(p->ai_canonname, "localhost") == NULL) {
- char *dn = strdup(p->ai_canonname);
- freeaddrinfo(info);
- if (dn == NULL) {
- lmi_error("Memory allocation failed");
- return NULL;
- }
- return dn;
- }
- }
+ if (!CMHasNext(en, &rc)) {
+ lmi_error("No instance of class %s exists", class_name);
+ CMRelease(en);
+ return false;
+ }
+ CMPIData data = CMGetNext(en, &rc);
+ if (rc.rc != CMPI_RC_OK) {
+ lmi_error("Unable to get instance name of class %s", class_name);
+ CMRelease(en);
+ return false;
}
- if (info != NULL) {
- freeaddrinfo(info);
+ if (data.type != CMPI_ref) {
+ lmi_error("EnumInstanceNames didn't return CMPI_ref type, but %d", data.type);
+ CMRelease(en);
+ return false;
+ }
+ _computer_system = CMClone(data.value.ref, &rc);
+ if (rc.rc != CMPI_RC_OK) {
+ lmi_error("Unable to clone ComputerSystem object path");
+ CMRelease(en);
+ return false;
}
- return strdup(hostname);
+ _system_name = strdup(CMGetCharPtr(CMGetKey(_computer_system, "Name", &rc).value.string));
+ CMRelease(en);
+ return true;
}
-const char *lmi_get_system_name()
+CMPIObjectPath *lmi_get_computer_system(void)
{
- if (_fqdn == NULL) {
- _fqdn = getFQDN();
- }
- return _fqdn;
+ return _computer_system;
+}
+
+const char *lmi_get_system_name(void)
+{
+ return _system_name;
}
-const char *lmi_get_system_creation_class_name()
+const char *lmi_get_system_creation_class_name(void)
{
if (_system_creation_class_name == NULL) {
if (_masterKeyFile == NULL) {
@@ -242,7 +257,9 @@ GKeyFile *parse_config(const char *provider_name, const ConfigEntry *provider_co
return masterKeyFile;
}
-void lmi_init(const char *provider, const CMPIBroker *cb, const ConfigEntry *provider_config_defaults)
+void lmi_init(const char *provider, const CMPIBroker *cb,
+ const CMPIContext *ctx,
+ const ConfigEntry *provider_config_defaults)
{
pthread_mutex_lock(&_init_mutex);
// Broker can change between threads
@@ -267,12 +284,13 @@ void lmi_init(const char *provider, const CMPIBroker *cb, const ConfigEntry *pro
_masterKeyFile = parse_config(provider, provider_config_defaults);
// We might read different log config, reread it in next _log_debug call
_log_level = _LMI_DEBUG_NONE;
- pthread_mutex_unlock(&_init_mutex);
-}
-void lmi_init_logging(const char *log_id, const CMPIBroker *cb)
-{
- lmi_init(log_id, cb, NULL);
+ // Read ComputerSystem instance
+ if (_computer_system == NULL || _system_name == NULL) {
+ get_computer_system(cb, ctx);
+ }
+
+ pthread_mutex_unlock(&_init_mutex);
}
int lmi_log_level(void)