summaryrefslogtreecommitdiffstats
path: root/src/libs/zbxsysinfo/win32/services.c
blob: 4065e2cfb4876db63ec22c99468921686134ffa6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/* 
** ZABBIX
** Copyright (C) 2000-2005 SIA Zabbix
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
**/

#include "common.h"

#include "sysinfo.h"

int	SERVICE_STATE(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result)
{
	SC_HANDLE
		mgr,
		service;

	char	name[MAX_STRING_LEN];
	char	service_name[MAX_STRING_LEN];

	unsigned long
		max_len_name = MAX_STRING_LEN;

	int	ret = SYSINFO_RET_FAIL;
	int	i;

	SERVICE_STATUS status;

	if(num_param(param) > 1)
	{
		return SYSINFO_RET_FAIL;
	}

	if(get_param(param, 1, name, sizeof(name)) != 0)
	{
		name[0] = '\0';
	}
	if(name[0] == '\0')
	{
		return SYSINFO_RET_FAIL;
	}

	if(NULL == (mgr = OpenSCManager(NULL,NULL,GENERIC_READ)) )
	{
		return SYSINFO_RET_FAIL;
	}

	service = OpenService(mgr,name,SERVICE_QUERY_STATUS);

	if(NULL == service && 0 != GetServiceKeyName(mgr, name, service_name, &max_len_name))
	{
		service = OpenService(mgr,service_name,SERVICE_QUERY_STATUS);
	}

	if(NULL == service)
	{
		SET_UI64_RESULT(result, 255);
	}
	else
	{
		if (QueryServiceStatus(service, &status))
		{
			static DWORD states[7] = 
			{
				SERVICE_RUNNING,
				SERVICE_PAUSED,
				SERVICE_START_PENDING,
				SERVICE_PAUSE_PENDING,
				SERVICE_CONTINUE_PENDING,
				SERVICE_STOP_PENDING,
				SERVICE_STOPPED 
			};

			for(i=0; i < 7 && status.dwCurrentState != states[i]; i++);
			
			SET_UI64_RESULT(result, i);
		}
		else
		{
			SET_UI64_RESULT(result, 7);
		}

		CloseServiceHandle(service);
	}

	CloseServiceHandle(mgr);

	return SYSINFO_RET_OK;
}