diff --git a/include/sigar_util.h b/include/sigar_util.h index 9cc1a445..d6ec067a 100644 --- a/include/sigar_util.h +++ b/include/sigar_util.h @@ -81,6 +81,8 @@ double sigar_file_system_usage_calc_used(sigar_t *sigar, void sigar_cpu_model_adjust(sigar_t *sigar, sigar_cpu_info_t *info); +char *sigar_get_self_path(sigar_t *sigar); + typedef struct sigar_cache_entry_t sigar_cache_entry_t; struct sigar_cache_entry_t { diff --git a/src/sigar_util.c b/src/sigar_util.c index 524934fb..759ce882 100644 --- a/src/sigar_util.c +++ b/src/sigar_util.c @@ -510,6 +510,52 @@ SIGAR_DECLARE(int) sigar_nfs_ping(char *host) #define vsnprintf _vsnprintf #endif +static int proc_module_get_self(void *data, char *name, int len) +{ + sigar_t *sigar = (sigar_t *)data; + char *ptr = rindex(name, '/'); + + if (!ptr) { + return SIGAR_OK; + } + + if (strnEQ(ptr+1, "libsigar-", 9)) { + *ptr = '\0'; /* chop libsigar-powerpc-ibm-aix-4.3.x.so */ + + sigar->self_path = sigar_strdup(name); + + if (SIGAR_LOG_IS_DEBUG(sigar)) { + sigar_log_printf(sigar, SIGAR_LOG_DEBUG, + "detected sigar-lib='%s'", + sigar->self_path); + } + + return !SIGAR_OK; /* break loop */ + } + + return SIGAR_OK; +} + +char *sigar_get_self_path(sigar_t *sigar) +{ + if (!sigar->self_path) { + sigar_proc_modules_t procmods; + procmods.module_getter = proc_module_get_self; + procmods.data = sigar; + + sigar_proc_modules_get(sigar, + sigar_pid_get(sigar), + &procmods); + + if (!sigar->self_path) { + /* dont try again */ + sigar->self_path = sigar_strdup("."); + } + } + + return sigar->self_path; +} + SIGAR_DECLARE(void) sigar_log_printf(sigar_t *sigar, int level, const char *format, ...) {