From 795db9864727c8ea3515219475460ce5a6ebc03c Mon Sep 17 00:00:00 2001 From: Doug MacEachern Date: Wed, 7 Mar 2007 06:51:18 +0000 Subject: [PATCH] refactor for Pid.Service ptql --- bindings/java/src/jni/javasigar.c | 55 +++---------------------------- src/os/win32/sigar_os.h | 2 ++ src/os/win32/win32_sigar.c | 45 +++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 50 deletions(-) diff --git a/bindings/java/src/jni/javasigar.c b/bindings/java/src/jni/javasigar.c index 0f15a985..f412c8c6 100644 --- a/bindings/java/src/jni/javasigar.c +++ b/bindings/java/src/jni/javasigar.c @@ -1386,15 +1386,6 @@ JNIEXPORT void SIGAR_JNI(SigarLog_setLevel) sigar_log_level_set(sigar, level); } -/* - * XXX temporary function for ptql to map windows service to pid. - * in the future would better to integrate win32bindings w/ sigar. - */ -#ifdef WIN32 -#define sigar_QueryServiceStatusEx \ - sigar->advapi.query_service_status.func -#endif - JNIEXPORT jlong SIGAR_JNIx(getServicePid) (JNIEnv *env, jobject sigar_obj, jstring jname) { @@ -1402,56 +1393,20 @@ JNIEXPORT jlong SIGAR_JNIx(getServicePid) const char *name; jboolean is_copy; jlong pid = 0; - DWORD err = ERROR_SUCCESS; - SC_HANDLE mgr; + int status; dSIGAR(0); - if (!sigar_QueryServiceStatusEx) { - sigar_throw_notimpl(env, - "QueryServiceStatusEx not available"); - return 0; - } - name = JENV->GetStringUTFChars(env, jname, &is_copy); - mgr = OpenSCManager(NULL, SERVICES_ACTIVE_DATABASE, - SC_MANAGER_ALL_ACCESS); - - if (mgr) { - HANDLE svc = OpenService(mgr, name, SERVICE_ALL_ACCESS); - - if (svc) { - SERVICE_STATUS_PROCESS status; - DWORD bytes; - - if (sigar_QueryServiceStatusEx(svc, - SC_STATUS_PROCESS_INFO, - (LPBYTE)&status, - sizeof(status), &bytes)) - { - pid = status.dwProcessId; - } - else { - err = GetLastError(); - } - CloseServiceHandle(svc); - } - else { - err = GetLastError(); - } - - CloseServiceHandle(mgr); - } - else { - err = GetLastError(); - } + status = + sigar_service_pid_get(sigar, (char *)name, &pid); if (is_copy) { JENV->ReleaseStringUTFChars(env, jname, name); } - if (err != ERROR_SUCCESS) { - sigar_throw_error(env, jsigar, err); + if (status != ERROR_SUCCESS) { + sigar_throw_error(env, jsigar, status); } return pid; diff --git a/src/os/win32/sigar_os.h b/src/os/win32/sigar_os.h index 1ecea56f..4cd50390 100644 --- a/src/os/win32/sigar_os.h +++ b/src/os/win32/sigar_os.h @@ -475,6 +475,8 @@ unsigned int sigar_cpu_count(sigar_t *sigar); int sigar_cpu_info_get(sigar_t *sigar, sigar_cpu_info_t *info); +int sigar_service_pid_get(sigar_t *sigar, char *name, sigar_pid_t *pid); + #define SIGAR_NO_SUCH_PROCESS (SIGAR_OS_START_ERROR+1) #endif /* SIGAR_OS_H */ diff --git a/src/os/win32/win32_sigar.c b/src/os/win32/win32_sigar.c index a09d10df..9614754d 100644 --- a/src/os/win32/win32_sigar.c +++ b/src/os/win32/win32_sigar.c @@ -3187,3 +3187,48 @@ int sigar_os_sys_info_get(sigar_t *sigar, return SIGAR_OK; } + +#define sigar_QueryServiceStatusEx \ + sigar->advapi.query_service_status.func + +int sigar_service_pid_get(sigar_t *sigar, char *name, sigar_pid_t *pid) +{ + DWORD rc = ERROR_SUCCESS, len; + SC_HANDLE mgr; + HANDLE svc; + SERVICE_STATUS_PROCESS status; + + if (!sigar_QueryServiceStatusEx) { + return SIGAR_ENOTIMPL; + } + + mgr = OpenSCManager(NULL, + SERVICES_ACTIVE_DATABASE, + SC_MANAGER_ALL_ACCESS); + + if (!mgr) { + return GetLastError(); + } + + if (!(svc = OpenService(mgr, name, SERVICE_ALL_ACCESS))) { + CloseServiceHandle(mgr); + return GetLastError(); + } + + if (sigar_QueryServiceStatusEx(svc, + SC_STATUS_PROCESS_INFO, + (LPBYTE)&status, + sizeof(status), &len)) + { + *pid = status.dwProcessId; + } + else { + *pid = -1; + rc = GetLastError(); + } + + CloseServiceHandle(svc); + CloseServiceHandle(mgr); + + return rc; +}