ReactOS 0.4.15-dev-7842-g558ab78
stop.c
Go to the documentation of this file.
1/*
2 * PROJECT: ReactOS Services
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: base/applications/mscutils/servman/stop.c
5 * PURPOSE: Stops running a service
6 * COPYRIGHT: Copyright 2006-2015 Ged Murphy <gedmurphy@reactos.org>
7 *
8 */
9
10#include "precomp.h"
11
12#define NDEBUG
13#include <debug.h>
14
15#define MAX_WAIT_TIME 30000
16
19 _In_opt_ HANDLE hProgress)
20{
21 SC_HANDLE hSCManager;
22 SC_HANDLE hService;
24 DWORD BytesNeeded;
26 DWORD WaitTime;
28 DWORD dwResult = ERROR_SUCCESS;
29
31 NULL,
33 if (!hSCManager) return GetLastError();
34
35 hService = OpenServiceW(hSCManager,
38 if (!hService)
39 {
40 dwResult = GetLastError();
42 return dwResult;
43 }
44
45 if (hProgress)
46 {
47 /* Increment the progress bar */
49 }
50
51 /* Set the start and max wait times */
54
55 /* Send the service the stop code */
56 if (ControlService(hService,
59 {
60 if (hProgress)
61 {
62 /* Increment the progress bar */
64 }
65
67 {
68 int i;
69 /* Fixup the wait time */
70 WaitTime = ServiceStatus.dwWaitHint / 10;
71
72 if (WaitTime < 1000) WaitTime = 1000;
73 else if (WaitTime > 10000) WaitTime = 10000;
74
75 /* We don't wanna wait for up to 10 secs without incrementing */
76 for (i = WaitTime / 1000; i > 0; i--)
77 {
78 Sleep(1000);
79 if (hProgress)
80 {
81 /* Increment the progress bar */
83 }
84 }
85
86 if (QueryServiceStatusEx(hService,
90 &BytesNeeded))
91 {
92 /* Have we exceeded our wait time? */
94 {
95 /* Yep, give up */
96 DPRINT1("Timeout\n");
98 break;
99 }
100 }
101 else
102 {
103 dwResult = GetLastError();
104 DPRINT1("QueryServiceStatusEx failed: %d\n", dwResult);
105 }
106 }
107
108 /* If the service is stopped, return TRUE */
110 {
111 dwResult = ERROR_SUCCESS;
112 }
113 }
114 else
115 {
116 dwResult = GetLastError();
117 }
118
119 CloseServiceHandle(hService);
121
122 return dwResult;
123}
#define DEFAULT_STEP
Definition: precomp.h:111
VOID IncrementProgressBar(HANDLE hProgress, UINT NewPos)
Definition: progress.c:338
#define DPRINT1
Definition: precomp.h:8
static WCHAR ServiceName[]
Definition: browser.c:19
static SERVICE_STATUS ServiceStatus
Definition: browser.c:22
#define ERROR_SUCCESS
Definition: deptool.c:10
#define NULL
Definition: types.h:112
DWORD WINAPI GetTickCount(VOID)
Definition: time.c:455
unsigned long DWORD
Definition: ntddk_ex.h:95
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
#define _In_z_
Definition: ms_sal.h:313
#define _In_opt_
Definition: ms_sal.h:309
static ULONG Timeout
Definition: ping.c:61
SC_HANDLE hSCManager
Definition: sc.c:12
SC_HANDLE WINAPI OpenSCManagerW(LPCWSTR lpMachineName, LPCWSTR lpDatabaseName, DWORD dwDesiredAccess)
Definition: scm.c:2068
BOOL WINAPI ControlService(SC_HANDLE hService, DWORD dwControl, LPSERVICE_STATUS lpServiceStatus)
Definition: scm.c:622
BOOL WINAPI QueryServiceStatusEx(SC_HANDLE hService, SC_STATUS_TYPE InfoLevel, LPBYTE lpBuffer, DWORD cbBufSize, LPDWORD pcbBytesNeeded)
Definition: scm.c:2887
SC_HANDLE WINAPI OpenServiceW(SC_HANDLE hSCManager, LPCWSTR lpServiceName, DWORD dwDesiredAccess)
Definition: scm.c:2160
BOOL WINAPI CloseServiceHandle(SC_HANDLE hSCObject)
Definition: scm.c:580
DWORD DoStopService(_In_z_ LPWSTR ServiceName, _In_opt_ HANDLE hProgress)
Definition: stop.c:18
#define MAX_WAIT_TIME
Definition: stop.c:15
DWORD dwWaitHint
Definition: winsvc.h:105
DWORD dwCurrentState
Definition: winsvc.h:100
VOID WINAPI DECLSPEC_HOTPATCH Sleep(IN DWORD dwMilliseconds)
Definition: synch.c:790
static LARGE_INTEGER StartTime
Definition: sys_arch.c:13
unsigned char * LPBYTE
Definition: typedefs.h:53
DWORD WINAPI GetLastError(void)
Definition: except.c:1042
#define ERROR_SERVICE_REQUEST_TIMEOUT
Definition: winerror.h:604
#define SERVICE_STOPPED
Definition: winsvc.h:21
#define SERVICE_QUERY_STATUS
Definition: winsvc.h:55
@ SC_STATUS_PROCESS_INFO
Definition: winsvc.h:119
#define SC_MANAGER_CONNECT
Definition: winsvc.h:14
#define SERVICE_STOP
Definition: winsvc.h:58
#define SERVICE_CONTROL_STOP
Definition: winsvc.h:36
WCHAR * LPWSTR
Definition: xmlstorage.h:184