-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#795 / win servers: implement enumerate function
- Loading branch information
Showing
7 changed files
with
223 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* Copyright (c) 2009, Giampaolo Rodola'. All rights reserved. | ||
* Use of this source code is governed by a BSD-style license that can be | ||
* found in the LICENSE file. | ||
*/ | ||
|
||
#include <Python.h> | ||
#include <windows.h> | ||
#include <Winsvc.h> | ||
|
||
#include "services.h" | ||
|
||
|
||
/* | ||
* Enumerate all services. | ||
*/ | ||
PyObject * | ||
psutil_winservice_enumerate(PyObject *self, PyObject *args) { | ||
ENUM_SERVICE_STATUS *lpService = NULL; | ||
SC_HANDLE sc = NULL; | ||
BOOL ok; | ||
DWORD bytesNeeded = 0; | ||
DWORD srvCount; | ||
DWORD resumeHandle = 0; | ||
DWORD dwBytes = 0; | ||
DWORD i; | ||
PyObject *py_retlist = PyList_New(0); | ||
PyObject *py_tuple = NULL; | ||
|
||
if (py_retlist == NULL) | ||
return NULL; | ||
|
||
sc = OpenSCManager(NULL, NULL, SC_MANAGER_ENUMERATE_SERVICE); | ||
if (sc == NULL) { | ||
PyErr_SetFromWindowsErr(0); | ||
return NULL; | ||
} | ||
|
||
for (;;) { | ||
ok = EnumServicesStatus( | ||
sc, SERVICE_WIN32, SERVICE_STATE_ALL, lpService, dwBytes, | ||
&bytesNeeded, &srvCount, &resumeHandle); | ||
if (ok || (GetLastError() != ERROR_MORE_DATA)) | ||
break; | ||
if (lpService) | ||
free(lpService); | ||
dwBytes = bytesNeeded; | ||
lpService = (ENUM_SERVICE_STATUS*)malloc(dwBytes); | ||
} | ||
|
||
for (i = 0; i < srvCount; i++) { | ||
py_tuple = Py_BuildValue( | ||
"(ssi)", | ||
lpService[i].lpServiceName, // name | ||
lpService[i].lpDisplayName, // display name | ||
lpService[i].ServiceStatus.dwCurrentState // status | ||
); | ||
if (py_tuple == NULL) | ||
goto error; | ||
if (PyList_Append(py_retlist, py_tuple)) | ||
goto error; | ||
Py_DECREF(py_tuple); | ||
} | ||
|
||
CloseServiceHandle(sc); | ||
free(lpService); | ||
return py_retlist; | ||
|
||
error: | ||
Py_XDECREF(py_tuple); | ||
Py_DECREF(py_retlist); | ||
if (sc != NULL) | ||
CloseServiceHandle(sc); | ||
if (lpService != NULL) | ||
free(lpService); | ||
return NULL; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/* | ||
* Copyright (c) 2009, Giampaolo Rodola'. All rights reserved. | ||
* Use of this source code is governed by a BSD-style license that can be | ||
* found in the LICENSE file. | ||
*/ | ||
|
||
#include <Python.h> | ||
|
||
PyObject *psutil_winservice_enumerate(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters