diff --git a/bindings/java/src/org/hyperic/sigar/test/TestPTQL.java b/bindings/java/src/org/hyperic/sigar/test/TestPTQL.java index 38cf63bc..7f504169 100644 --- a/bindings/java/src/org/hyperic/sigar/test/TestPTQL.java +++ b/bindings/java/src/org/hyperic/sigar/test/TestPTQL.java @@ -61,6 +61,7 @@ public class TestPTQL extends SigarTestCase { "Args.*.eq=org.apache.tools.ant.Main", //'*' == any arg "Args.*.ct=java", //'*' == any arg "Args.*.ew=sigar.jar", //'*' == any arg + "Modules.*.re=libc|kernel", "Port.tcp.eq=80,Cred.Uid.eq=0", //root owned http port "Port.udp.eq=161,Cred.Uid.eq=0", //root owned snmp port "Port.tcp.eq=8080,Cred.Uid.eq=1003", //dougm owned jboss port diff --git a/src/os/darwin/darwin_sigar.c b/src/os/darwin/darwin_sigar.c index b32e726b..d18f317c 100644 --- a/src/os/darwin/darwin_sigar.c +++ b/src/os/darwin/darwin_sigar.c @@ -39,6 +39,7 @@ #include #include #include +#include #define __OPENTRANSPORTPROVIDERS__ #include #include @@ -460,7 +461,8 @@ int sigar_mem_get(sigar_t *sigar, sigar_mem_t *mem) return status; } - mem->free = vmstat.free_count * sigar->pagesize; + mem->free = vmstat.free_count; + mem->free *= sigar->pagesize; #elif defined(__FreeBSD__) if ((status = sigar_vmstat(sigar, &vmstat)) == SIGAR_OK) { mem->free = vmstat.v_free_count; @@ -1884,10 +1886,36 @@ int sigar_proc_exe_get(sigar_t *sigar, sigar_pid_t pid, #endif } +#ifdef DARWIN +static int sigar_dlinfo_modules(sigar_t *sigar, sigar_proc_modules_t *procmods) +{ + uint32_t i, count = _dyld_image_count(); + + for (i=0; imodule_getter(procmods->data, + (char *)name, strlen(name)); + + if (status != SIGAR_OK) { + /* not an error; just stop iterating */ + break; + } + } + return SIGAR_OK; +} +#endif /* DARWIN */ + int sigar_proc_modules_get(sigar_t *sigar, sigar_pid_t pid, sigar_proc_modules_t *procmods) { -#ifdef SIGAR_HAS_DLINFO_MODULES +#if defined(SIGAR_HAS_DLINFO_MODULES) || defined(DARWIN) if (pid == sigar_pid_get(sigar)) { return sigar_dlinfo_modules(sigar, procmods); } diff --git a/src/sigar_ptql.c b/src/sigar_ptql.c index 834d6c11..4fc560d4 100644 --- a/src/sigar_ptql.c +++ b/src/sigar_ptql.c @@ -1144,6 +1144,65 @@ static int SIGAPI ptql_args_match(sigar_t *sigar, return matched ? SIGAR_OK : !SIGAR_OK; } +typedef struct { + sigar_t *sigar; + ptql_branch_t *branch; + sigar_uint32_t ix; + int matched; +} proc_modules_match_t; + +static int proc_modules_match(void *data, char *name, int len) +{ + proc_modules_match_t *matcher = + (proc_modules_match_t *)data; + ptql_branch_t *branch = matcher->branch; + + if (branch->op_flags & PTQL_OP_FLAG_GLOB) { /* Modules.*.ct=libc */ + matcher->matched = + ptql_str_match(matcher->sigar, branch, name); + + if (matcher->matched) { + return !SIGAR_OK; /* stop iterating */ + } + } + else { + if (matcher->ix++ == branch->data.ui32) { /* Modules.3.ct=libc */ + matcher->matched = + ptql_str_match(matcher->sigar, branch, name); + return !SIGAR_OK; /* stop iterating */ + } + } + + return SIGAR_OK; +} + +static int SIGAPI ptql_modules_match(sigar_t *sigar, + sigar_pid_t pid, + void *data) +{ + ptql_branch_t *branch = + (ptql_branch_t *)data; + int status; + sigar_proc_modules_t procmods; + proc_modules_match_t matcher; + + matcher.sigar = sigar; + matcher.branch = branch; + matcher.ix = 0; + matcher.matched = 0; + + procmods.module_getter = proc_modules_match; + procmods.data = &matcher; + + status = sigar_proc_modules_get(sigar, pid, &procmods); + + if (status != SIGAR_OK) { + return status; + } + + return matcher.matched ? SIGAR_OK : !SIGAR_OK; +} + typedef struct { const char *key; int klen; @@ -1316,6 +1375,10 @@ static ptql_lookup_t PTQL_Args[] = { { NULL, ptql_args_match, 0, 0, PTQL_VALUE_TYPE_ANY, ptql_args_branch_init } }; +static ptql_lookup_t PTQL_Modules[] = { + { NULL, ptql_modules_match, 0, 0, PTQL_VALUE_TYPE_ANY, ptql_args_branch_init } +}; + static ptql_lookup_t PTQL_Env[] = { { NULL, ptql_env_match, 0, 0, PTQL_VALUE_TYPE_ANY, ptql_branch_init_any } }; @@ -1342,6 +1405,7 @@ static ptql_entry_t ptql_map[] = { { "State", PTQL_State }, { "Fd", PTQL_Fd }, { "Args", PTQL_Args }, + { "Modules", PTQL_Modules }, { "Env", PTQL_Env }, { "Port", PTQL_Port }, { "Pid", PTQL_Pid },