refactor for Pid.Service ptql

This commit is contained in:
Doug MacEachern 2007-03-07 06:51:18 +00:00
parent 29ef270b9a
commit 795db98647
3 changed files with 52 additions and 50 deletions

View File

@ -1386,15 +1386,6 @@ JNIEXPORT void SIGAR_JNI(SigarLog_setLevel)
sigar_log_level_set(sigar, level); 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) JNIEXPORT jlong SIGAR_JNIx(getServicePid)
(JNIEnv *env, jobject sigar_obj, jstring jname) (JNIEnv *env, jobject sigar_obj, jstring jname)
{ {
@ -1402,56 +1393,20 @@ JNIEXPORT jlong SIGAR_JNIx(getServicePid)
const char *name; const char *name;
jboolean is_copy; jboolean is_copy;
jlong pid = 0; jlong pid = 0;
DWORD err = ERROR_SUCCESS; int status;
SC_HANDLE mgr;
dSIGAR(0); dSIGAR(0);
if (!sigar_QueryServiceStatusEx) {
sigar_throw_notimpl(env,
"QueryServiceStatusEx not available");
return 0;
}
name = JENV->GetStringUTFChars(env, jname, &is_copy); name = JENV->GetStringUTFChars(env, jname, &is_copy);
mgr = OpenSCManager(NULL, SERVICES_ACTIVE_DATABASE, status =
SC_MANAGER_ALL_ACCESS); sigar_service_pid_get(sigar, (char *)name, &pid);
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();
}
if (is_copy) { if (is_copy) {
JENV->ReleaseStringUTFChars(env, jname, name); JENV->ReleaseStringUTFChars(env, jname, name);
} }
if (err != ERROR_SUCCESS) { if (status != ERROR_SUCCESS) {
sigar_throw_error(env, jsigar, err); sigar_throw_error(env, jsigar, status);
} }
return pid; return pid;

View File

@ -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_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) #define SIGAR_NO_SUCH_PROCESS (SIGAR_OS_START_ERROR+1)
#endif /* SIGAR_OS_H */ #endif /* SIGAR_OS_H */

View File

@ -3187,3 +3187,48 @@ int sigar_os_sys_info_get(sigar_t *sigar,
return SIGAR_OK; 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;
}