add native single process finder

This commit is contained in:
Doug MacEachern 2007-04-21 18:04:47 +00:00
parent 697412d938
commit a1a9f3f494
5 changed files with 104 additions and 10 deletions

View File

@ -1163,6 +1163,16 @@ static int jsigar_ptql_re_impl(void *data,
JENV->NewStringUTF(env, needle));
}
static void re_impl_set(JNIEnv *env, sigar_t *sigar, jobject obj, jni_ptql_re_data_t *re)
{
re->env = env;
re->cls = NULL;
re->obj = obj;
re->id = NULL;
sigar_ptql_re_impl_set(sigar, re, jsigar_ptql_re_impl);
}
JNIEXPORT jboolean SIGAR_JNI(ptql_SigarProcessQuery_match)
(JNIEnv *env, jobject obj, jobject sigar_obj, jlong pid)
{
@ -1172,12 +1182,7 @@ JNIEXPORT jboolean SIGAR_JNI(ptql_SigarProcessQuery_match)
(sigar_ptql_query_t *)sigar_get_pointer(env, obj);
dSIGAR(JNI_FALSE);
re.env = env;
re.cls = NULL;
re.obj = obj;
re.id = NULL;
sigar_ptql_re_impl_set(sigar, &re, jsigar_ptql_re_impl);
re_impl_set(env, sigar, obj, &re);
status = sigar_ptql_query_match(sigar, query, pid);
@ -1233,6 +1238,32 @@ JNIEXPORT void SIGAR_JNI(ptql_SigarProcessQuery_destroy)
}
}
JNIEXPORT jlong SIGAR_JNI(ptql_SigarProcessQuery_findProcess)
(JNIEnv *env, jobject obj, jobject sigar_obj)
{
sigar_pid_t pid;
int status;
jni_ptql_re_data_t re;
sigar_ptql_query_t *query =
(sigar_ptql_query_t *)sigar_get_pointer(env, obj);
dSIGAR(0);
re_impl_set(env, sigar, obj, &re);
status = sigar_ptql_query_find_process(sigar, query, &pid);
sigar_ptql_re_impl_set(sigar, NULL, NULL);
if (status < 0) {
sigar_throw_ptql_malformed(env, sigar->errbuf);
}
else if (status != SIGAR_OK) {
sigar_throw_error(env, jsigar, status);
}
return pid;
}
#include "sigar_getline.h"
JNIEXPORT jboolean SIGAR_JNI(util_Getline_isatty)

View File

@ -51,10 +51,10 @@ public class ProcessFinder {
public long findSingleProcess(ProcessQuery query)
throws SigarException, SigarNotImplementedException,
MalformedQueryException {
//XXX bring back in another form
//if (query instanceof PidQuery) {
// return ((PidQuery)query).getPid();
//}
if (query instanceof SigarProcessQuery) {
return ((SigarProcessQuery)query).findProcess(this.proxy);
}
int i, matches = 0;

View File

@ -20,6 +20,7 @@ package org.hyperic.sigar.ptql;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException;
import org.hyperic.sigar.SigarNotImplementedException;
import org.hyperic.sigar.SigarProxy;
import org.hyperic.sigar.SigarProxyCache;
@ -45,6 +46,15 @@ public class SigarProcessQuery implements ProcessQuery {
return match(SigarProxyCache.getSigar(sigar), pid);
}
public native long findProcess(Sigar sigar)
throws SigarException, SigarNotImplementedException, MalformedQueryException;
public long findProcess(SigarProxy sigar)
throws SigarException, SigarNotImplementedException, MalformedQueryException {
return findProcess(SigarProxyCache.getSigar(sigar));
}
static boolean re(String haystack, String needle) {
if (haystack == null) {
return false;

View File

@ -37,4 +37,8 @@ SIGAR_DECLARE(int) sigar_ptql_query_match(sigar_t *sigar,
SIGAR_DECLARE(int) sigar_ptql_query_destroy(sigar_ptql_query_t *query);
SIGAR_DECLARE(int) sigar_ptql_query_find_process(sigar_t *sigar,
sigar_ptql_query_t *query,
sigar_pid_t *pid);
#endif /*SIGAR_PTQL_H*/

View File

@ -1363,3 +1363,52 @@ SIGAR_DECLARE(int) sigar_ptql_query_match(sigar_t *sigar,
return SIGAR_OK;
}
SIGAR_DECLARE(int) sigar_ptql_query_find_process(sigar_t *sigar,
sigar_ptql_query_t *query,
sigar_pid_t *pid)
{
sigar_proc_list_t pids;
int status = sigar_proc_list_get(sigar, &pids);
int i, matches=0;
if (status != SIGAR_OK) {
return status;
}
for (i=0; i<pids.number; i++) {
int query_status =
sigar_ptql_query_match(sigar, query, pids.data[i]);
if (query_status == SIGAR_OK) {
*pid = pids.data[i];
matches++;
}
else if (query_status == SIGAR_ENOTIMPL) {
/* let caller know query is invalid. */
status = query_status;
break;
} /* else ok, e.g. permission denied */
}
sigar_proc_list_destroy(sigar, &pids);
if (status != SIGAR_OK) {
return status;
}
if (matches == 1) {
return SIGAR_OK;
}
else if (matches == 0) {
sigar_strerror_set(sigar,
"Query did not match any processes");
}
else {
sigar_strerror_printf(sigar,
"Query matched multiple processes (%d)",
matches);
}
return -1;
}