From c14bd833b7b1b3b1283d116a87b6f293377f129b Mon Sep 17 00:00:00 2001 From: Radek Novacek Date: Mon, 8 Apr 2013 11:09:53 +0200 Subject: Support for using libopenlmicommon by external providers * add FindOpenLMI.cmake module * add pkgconfig for OpenLMI * add openlmi.c/h with exported functions * add symlink with major version to openlmicommon library Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=948948 --- CMakeLists.txt | 1 + cmake/modules/FindOpenLMI.cmake | 21 +++++ src/CMakeLists.txt | 16 +++- src/fan/LMI_FanAssociatedSensorProvider.c | 2 +- src/fan/LMI_FanSensorProvider.c | 2 +- src/globals.c | 98 --------------------- src/globals.h | 19 ++-- src/hardware/cpuinfo.h | 1 - src/hardware/dmidecode.h | 1 - src/hardware/lscpu.h | 1 - src/hardware/sysfs.h | 1 - src/openlmi.c | 139 ++++++++++++++++++++++++++++++ src/openlmi.h | 80 +++++++++++++++++ src/openlmi.pc.in | 10 +++ 14 files changed, 273 insertions(+), 119 deletions(-) create mode 100644 cmake/modules/FindOpenLMI.cmake delete mode 100644 src/globals.c create mode 100644 src/openlmi.c create mode 100644 src/openlmi.h create mode 100644 src/openlmi.pc.in diff --git a/CMakeLists.txt b/CMakeLists.txt index 230ed26..b42debc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -39,3 +39,4 @@ install(PROGRAMS openlmi-register-pegasus DESTINATION libexec) install(FILES cmake/modules/OpenLMIMacros.cmake DESTINATION share/cmake/Modules) install(FILES cmake/modules/FindCMPI.cmake DESTINATION share/cmake/Modules) install(FILES cmake/modules/FindKonkretCMPI.cmake DESTINATION share/cmake/Modules) +install(FILES cmake/modules/FindOpenLMI.cmake DESTINATION share/cmake/Modules) diff --git a/cmake/modules/FindOpenLMI.cmake b/cmake/modules/FindOpenLMI.cmake new file mode 100644 index 0000000..d4338ca --- /dev/null +++ b/cmake/modules/FindOpenLMI.cmake @@ -0,0 +1,21 @@ + +find_path(OPENLMI_INCLUDE_DIR + NAMES openlmi.h + HINTS $ENV{OPENLMI_INCLUDE_DIR} + PATH_SUFFIXES include/openlmi + PATHS /usr /usr/local +) + +find_library(OPENLMICOMMON_LIBRARY + NAMES openlmicommon + HINTS $ENV{OPENLMI_LIB_DIR} + PATH_SUFFIXES lib64 lib + PATHS /usr /usr/local +) + +set(OPENLMI_LIBRARIES ${OPENLMICOMMON_LIBRARY}) + +include(FindPackageHandleStandardArgs) +find_package_handle_standard_args(OPENLMI DEFAULT_MSG OPENLMI_LIBRARIES OPENLMI_INCLUDE_DIR) + +mark_as_advanced(OPENLMI_INCLUDE_DIR OPENLMI_LIBRARIES) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 7318911..d5335a6 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,13 +1,21 @@ -include_directories(.) +include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${CMPI_INCLUDE_DIR}) add_library(openlmicommon SHARED - globals.c + openlmi.c ) -set_target_properties(openlmicommon PROPERTIES SOVERSION 0.0.1) +set(OPENLMICOMMON_VERSION_MAJOR 0) +set(OPENLMICOMMON_VERSION_MINOR 0) +set(OPENLMICOMMON_VERSION_PATCH 1) +set(OPENLMICOMMON_VERSION "${OPENLMICOMMON_VERSION_MAJOR}.${OPENLMICOMMON_VERSION_MINOR}.${OPENLMICOMMON_VERSION_PATCH}") + +set_target_properties(openlmicommon PROPERTIES VERSION ${OPENLMICOMMON_VERSION}) +set_target_properties(openlmicommon PROPERTIES SOVERSION ${OPENLMICOMMON_VERSION_MAJOR}) install(TARGETS openlmicommon DESTINATION lib${LIB_SUFFIX}) -install(FILES globals.h DESTINATION include/openlmi) +install(FILES openlmi.h DESTINATION include/openlmi) +configure_file(openlmi.pc.in openlmi.pc @ONLY) +install(FILES openlmi.pc DESTINATION lib${LIB_SUFFIX}/pkgconfig) if (WITH-FAN) add_subdirectory(fan) diff --git a/src/fan/LMI_FanAssociatedSensorProvider.c b/src/fan/LMI_FanAssociatedSensorProvider.c index d75bf5c..b462e83 100644 --- a/src/fan/LMI_FanAssociatedSensorProvider.c +++ b/src/fan/LMI_FanAssociatedSensorProvider.c @@ -21,7 +21,7 @@ #include #include "LMI_FanAssociatedSensor.h" #include "fan.h" -#include +#include "globals.h" static const CMPIBroker* _cb; diff --git a/src/fan/LMI_FanSensorProvider.c b/src/fan/LMI_FanSensorProvider.c index 47bd9d5..e63d266 100644 --- a/src/fan/LMI_FanSensorProvider.c +++ b/src/fan/LMI_FanSensorProvider.c @@ -22,7 +22,7 @@ #include #include "LMI_FanSensor.h" #include "fan.h" -#include +#include "globals.h" static const CMPIBroker* _cb = NULL; diff --git a/src/globals.c b/src/globals.c deleted file mode 100644 index 7e58817..0000000 --- a/src/globals.c +++ /dev/null @@ -1,98 +0,0 @@ -/* - * Copyright (C) 2012-2013 Red Hat, Inc. All rights reserved. - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - * - * Authors: Radek Novacek - */ - -#include "globals.h" - -#include -#include -#include -#include -#include -#include -#include - -static char *_fqdn = NULL; -static int _log_level = DEBUG; - -char *getFQDN(void) -{ - struct utsname uts; - if ((uname(&uts) > 0) && (uts.nodename != NULL)) { - return strdup(uts.nodename); - } - char hostname[256]; - hostname[255] = '\0'; - if (gethostname(hostname, 255) == -1) { - // FIXME: what to do, if we can't use gethostname? - return NULL; - } - - 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); - return dn; - } - } - } - if (info != NULL) { - freeaddrinfo(info); - } - return strdup(hostname); -} - -const char *get_system_name() -{ - if (_fqdn == NULL) { - _fqdn = getFQDN(); - } - return _fqdn; -} - -const char *get_system_creation_class_name() -{ - return "Linux_ComputerSystem"; -} - -void _debug(int level, const char *file, int line, const char *format, ...) -{ - if (level > _log_level) { - return; - } - FILE *trace_file = stderr; - const char *lvl[] = { "NONE", "ERROR", "WARNING", "DEBUG" }; - // TODO: use logger from sfcbd and pegasus - fprintf(trace_file, "[%s] %s:%d\t", lvl[level], file, line); - - va_list args; - va_start(args, format); - vfprintf(stderr, format, args); - va_end(args); - - fprintf(stderr, "\n"); -} diff --git a/src/globals.h b/src/globals.h index a1cb18c..094602f 100644 --- a/src/globals.h +++ b/src/globals.h @@ -21,19 +21,16 @@ #ifndef GLOBALS_H #define GLOBALS_H -const char *get_system_name(); +#include "openlmi.h" -const char *get_system_creation_class_name(); - -int log_level(void); -void set_log_level(int level); - -enum { NONE=0, ERROR, WARN, DEBUG }; -void _debug(int level, const char *file, int line, const char *format, ...); -#define debug(...) _debug(DEBUG, __FILE__, __LINE__, __VA_ARGS__) -#define warn(...) _debug(WARN, __FILE__, __LINE__, __VA_ARGS__) -#define error(...) _debug(ERROR, __FILE__, __LINE__, __VA_ARGS__) +// Shortcuts for functions and macros from openlmicommon library +#define get_system_creation_class_name lmi_get_system_creation_class_name +#define get_system_name lmi_get_system_name +#define debug lmi_debug +#define warn lmi_warn +#define error lmi_error #define ORGID "LMI" + #endif diff --git a/src/hardware/cpuinfo.h b/src/hardware/cpuinfo.h index 5ec5dd5..0b2f9ab 100644 --- a/src/hardware/cpuinfo.h +++ b/src/hardware/cpuinfo.h @@ -24,7 +24,6 @@ #include #include #include -#include "globals.h" #include "utils.h" diff --git a/src/hardware/dmidecode.h b/src/hardware/dmidecode.h index 9190ea4..a627553 100644 --- a/src/hardware/dmidecode.h +++ b/src/hardware/dmidecode.h @@ -24,7 +24,6 @@ #include #include #include -#include "globals.h" #include "utils.h" diff --git a/src/hardware/lscpu.h b/src/hardware/lscpu.h index 5dc5bd5..39c85b6 100644 --- a/src/hardware/lscpu.h +++ b/src/hardware/lscpu.h @@ -24,7 +24,6 @@ #include #include #include -#include "globals.h" #include "utils.h" diff --git a/src/hardware/sysfs.h b/src/hardware/sysfs.h index 0e0b523..028b559 100644 --- a/src/hardware/sysfs.h +++ b/src/hardware/sysfs.h @@ -27,7 +27,6 @@ #include #include #include -#include "globals.h" #include "utils.h" #define SYSFS_CPU_PATH "/sys/devices/system/cpu" diff --git a/src/openlmi.c b/src/openlmi.c new file mode 100644 index 0000000..786bdd2 --- /dev/null +++ b/src/openlmi.c @@ -0,0 +1,139 @@ +/* + * Copyright (C) 2012-2013 Red Hat, Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + * Authors: Radek Novacek + */ + +#include "openlmi.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +static char *_fqdn = NULL; +static int _log_level = _LMI_DEBUG_DEBUG; +static const CMPIBroker *_cb = NULL; +static char *_log_id = NULL; + +static char *getFQDN(void) +{ + struct utsname uts; + if ((uname(&uts) > 0) && (uts.nodename != NULL)) { + return strdup(uts.nodename); + } + char hostname[256]; + hostname[255] = '\0'; + if (gethostname(hostname, 255) == -1) { + // FIXME: what to do, if we can't use gethostname? + return NULL; + } + + 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); + return dn; + } + } + } + if (info != NULL) { + freeaddrinfo(info); + } + return strdup(hostname); +} + +const char *lmi_get_system_name() +{ + if (_fqdn == NULL) { + _fqdn = getFQDN(); + } + return _fqdn; +} + +const char *lmi_get_system_creation_class_name() +{ + return "Linux_ComputerSystem"; +} + +void lmi_init_logging(const char *log_id, const CMPIBroker *cb) +{ + if (_log_id != NULL) { + free(_log_id); + } + _log_id = strdup(log_id); + _cb = cb; +} + +int lmi_log_level(void) +{ + return _log_level; +} + +void lmi_set_log_level(int level) +{ + _log_level = level; +} + +void _lmi_debug(int level, const char *file, int line, const char *format, ...) +{ + const char *lvl[] = { "NONE", "ERROR", "INFO", "WARNING", "DEBUG" }; + if (level > 4) { + level = 4; + } + if (level < 1) { + level = 1; + } + + char *message, *text; + va_list args; + va_start(args, format); + vasprintf(&message, format, args); + va_end(args); + asprintf(&text, "[%s] %s:%d\t%s", lvl[level], file, line, message); + free(message); + + CMPIStatus rc; + rc.rc = CMPI_RC_OK; + if (_cb != NULL) { + // try to use standard CMPI logging + rc = _cb->eft->trace(_cb, CMPI_LEV_INFO, _log_id, text, NULL); + } + + if (_cb == NULL || rc.rc != CMPI_RC_OK) { + // Fallback to stderr + if (level > _log_level) { + free(text); + return; + } + + fprintf(stderr, "%s\n", text); + } + free(text); +} diff --git a/src/openlmi.h b/src/openlmi.h new file mode 100644 index 0000000..9b27d07 --- /dev/null +++ b/src/openlmi.h @@ -0,0 +1,80 @@ +/* + * Copyright (C) 2012-2013 Red Hat, Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + * Authors: Radek Novacek + */ + +#ifndef OPENLMI_H +#define OPENLMI_H + +#include + +/** + * This function returns FQDN (fully qualified domain name) of the machine + * + * @note Use this in the SystemName property of all provider created instances. + * + * @return The scoping System's Name. + */ +const char *lmi_get_system_name(); + +/** + * This function returns system creation class name + * + * @note Use this in the SystemCreationClassName property of all provider + * created instances. + * + * @return The scoping System's Creation class name. + */ +const char *lmi_get_system_creation_class_name(); + +/** + * To use standard CIMOM logging facility, broker must be assigned. Without + * calling this function, logging will go to stderr. + * + * \p log_id Identification of log messages + * \p cb CMPIBroker + */ +void lmi_init_logging(const char *log_id, const CMPIBroker *cb); + +/** + * Get currently set logging level + * + * @return logging level + */ +int lmi_log_level(void); + +/** + * Set logging level + * + * @param level new logging level + */ +void lmi_set_log_level(int level); + +enum { + _LMI_DEBUG_NONE=0, _LMI_DEBUG_ERROR, _LMI_DEBUG_WARN, + _LMI_DEBUG_INFO, _LMI_DEBUG_DEBUG +}; + +void _lmi_debug(int level, const char *file, int line, const char *format, ...); + +#define lmi_debug(...) _lmi_debug(_LMI_DEBUG_DEBUG, __FILE__, __LINE__, __VA_ARGS__) +#define lmi_info(...) _lmi_debug(_LMI_DEBUG_INFO, __FILE__, __LINE__, __VA_ARGS__) +#define lmi_warn(...) _lmi_debug(_LMI_DEBUG_WARN, __FILE__, __LINE__, __VA_ARGS__) +#define lmi_error(...) _lmi_debug(_LMI_DEBUG_ERROR, __FILE__, __LINE__, __VA_ARGS__) + +#endif diff --git a/src/openlmi.pc.in b/src/openlmi.pc.in new file mode 100644 index 0000000..939d672 --- /dev/null +++ b/src/openlmi.pc.in @@ -0,0 +1,10 @@ +prefix=@CMAKE_INSTALL_PREFIX@ +exec_prefix=${prefix} +includedir=${prefix}/include +libdir=${exec_prefix}/lib@LIB_SUFFIX@ + +Name: openlmi +Description: OpenLMI provider support +Version: @OPENLMICOMMON_VERSION@ +Libs: -L${libdir} -lopenlmicommon +CFlags: -I${includedir}/openlmi -- cgit