Skip to content

Commit

Permalink
#795 / win services: add description
Browse files Browse the repository at this point in the history
  • Loading branch information
giampaolo committed Mar 7, 2016
1 parent 6f7f118 commit 68bce7f
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 1 deletion.
2 changes: 2 additions & 0 deletions psutil/_psutil_windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -3353,6 +3353,8 @@ PsutilMethods[] = {
// --- windows services
{"winservice_enumerate", psutil_winservice_enumerate, METH_VARARGS,
"List all services"},
{"winservice_get_srv_descr", psutil_winservice_get_srv_descr, METH_VARARGS,
"Return the description of a service"},

// --- windows API bindings
{"win32_QueryDosDevice", psutil_win32_QueryDosDevice, METH_VARARGS,
Expand Down
3 changes: 3 additions & 0 deletions psutil/_pswindows.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,9 @@ def username(self):
def start_type(self):
return self._info['startup']

def description(self):
return cext.winservice_get_srv_descr(self.name())

def as_dict(self):
return self._info

Expand Down
71 changes: 70 additions & 1 deletion psutil/arch/windows/services.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ psutil_winservice_enumerate(PyObject *self, PyObject *args) {
// TODO: handle encoding errs
qsc->lpBinaryPathName, // binpath
qsc->lpServiceStartName, // username
get_startup_string(qsc->dwStartType), // startup
get_startup_string(qsc->dwStartType) // startup
);
if (py_tuple == NULL)
goto error;
Expand Down Expand Up @@ -145,3 +145,72 @@ psutil_winservice_enumerate(PyObject *self, PyObject *args) {
free(lpService);
return NULL;
}


/*
* Get service description.
*/
PyObject *
psutil_winservice_get_srv_descr(PyObject *self, PyObject *args) {
ENUM_SERVICE_STATUS_PROCESS *lpService = NULL;
SC_HANDLE sc = NULL;
BOOL ok;
DWORD bytesNeeded = 0;
DWORD resumeHandle = 0;
DWORD dwBytes = 0;
SC_HANDLE hService = NULL;
SERVICE_DESCRIPTION *scd = NULL;
char *service_name;
PyObject *py_retstr = NULL;

if (!PyArg_ParseTuple(args, "s", &service_name))
return NULL;

sc = OpenSCManager(NULL, NULL, SC_MANAGER_ENUMERATE_SERVICE);
if (sc == NULL) {
PyErr_SetFromWindowsErr(0);
return NULL;
}

hService = OpenService(sc, service_name, SERVICE_QUERY_CONFIG);
if (hService == NULL) {
PyErr_SetFromWindowsErr(0);
goto error;
}

// This first call to QueryServiceConfig2() is necessary in order
// to get the right size.
bytesNeeded = 0;
QueryServiceConfig2(hService, SERVICE_CONFIG_DESCRIPTION, NULL, 0,
&bytesNeeded);
if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
PyErr_SetFromWindowsErr(0);
goto error;
}

scd = (SERVICE_DESCRIPTION *)malloc(bytesNeeded);
ok = QueryServiceConfig2(hService, SERVICE_CONFIG_DESCRIPTION,
(LPBYTE)scd, bytesNeeded, &bytesNeeded);
if (ok == 0) {
PyErr_SetFromWindowsErr(0);
goto error;
}

// TODO: handle encoding errors.
py_retstr = Py_BuildValue("s", scd->lpDescription);
if (!py_retstr)
goto error;

CloseServiceHandle(sc);
free(scd);
return py_retstr;

error:
if (hService != NULL)
CloseServiceHandle(hService);
if (sc != NULL)
CloseServiceHandle(sc);
if (lpService != NULL)
free(lpService);
return NULL;
}
1 change: 1 addition & 0 deletions psutil/arch/windows/services.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
#include <Python.h>

PyObject *psutil_winservice_enumerate();
PyObject *psutil_winservice_get_srv_descr();

0 comments on commit 68bce7f

Please sign in to comment.