ReactOS  0.4.15-dev-1201-gb2cf5a4
control.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/control.c
5  * PURPOSE: Pauses and resumes 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 
17 DWORD
19  HWND hProgress,
20  DWORD Control)
21 {
22  SC_HANDLE hSCManager;
23  SC_HANDLE hService;
26  DWORD BytesNeeded = 0;
27  DWORD StartTickCount;
28  DWORD OldCheckPoint;
29  DWORD WaitTime;
30  DWORD MaxWait;
31  DWORD ReqState, i;
32  BOOL Result;
33  DWORD dwResult = ERROR_SUCCESS;
34 
35  /* Set the state we're interested in */
36  switch (Control)
37  {
39  ReqState = SERVICE_PAUSED;
40  break;
42  ReqState = SERVICE_RUNNING;
43  break;
44  default:
45  /* Unhandled control code */
46  DPRINT1("Unknown control command: 0x%X\n", Control);
48  }
49 
51  NULL,
53  if (!hSCManager) return GetLastError();
54 
55  hService = OpenServiceW(hSCManager,
58  if (!hService)
59  {
60  dwResult = GetLastError();
62  return dwResult;
63  }
64 
65  /* Send the control message to the service */
66  Result = ControlService(hService,
67  Control,
68  &Status);
69  if (Result)
70  {
71  if (hProgress)
72  {
73  /* Increment the progress bar */
75  }
76 
77  /* Get the service status */
78  Result = QueryServiceStatusEx(hService,
81  sizeof(SERVICE_STATUS_PROCESS),
82  &BytesNeeded);
83  if (Result)
84  {
85  Result = FALSE;
86  MaxWait = MAX_WAIT_TIME;
87  OldCheckPoint = ServiceStatus.dwCheckPoint;
88  StartTickCount = GetTickCount();
89 
90  /* Loop until it's at the correct state */
91  while (ServiceStatus.dwCurrentState != ReqState)
92  {
93  /* Fixup the wait time */
94  WaitTime = ServiceStatus.dwWaitHint / 10;
95 
96  if (WaitTime < 1000) WaitTime = 1000;
97  else if (WaitTime > 10000) WaitTime = 10000;
98 
99  /* We don't wanna wait for up to 10 secs without incrementing */
100  for (i = WaitTime / 1000; i > 0; i--)
101  {
102  Sleep(1000);
103  if (hProgress)
104  {
105  /* Increment the progress bar */
107  }
108  }
109 
110  /* Get the latest status info */
111  if (!QueryServiceStatusEx(hService,
114  sizeof(SERVICE_STATUS_PROCESS),
115  &BytesNeeded))
116  {
117  /* Something went wrong... */
118  dwResult = GetLastError();
119  DPRINT1("QueryServiceStatusEx failed: %d\n", dwResult);
120  break;
121  }
122 
123  /* Is the service making progress? */
124  if (ServiceStatus.dwCheckPoint > OldCheckPoint)
125  {
126  /* It is, get the latest tickcount to reset the max wait time */
127  StartTickCount = GetTickCount();
128  OldCheckPoint = ServiceStatus.dwCheckPoint;
130  }
131  else
132  {
133  /* It's not, make sure we haven't exceeded our wait time */
134  if (GetTickCount() >= StartTickCount + MaxWait)
135  {
136  /* We have, give up */
137  DPRINT1("Timeout\n");
139  break;
140  }
141  }
142  }
143  }
144  else
145  {
146  dwResult = GetLastError();
147  }
148 
149  if (ServiceStatus.dwCurrentState == ReqState)
150  {
151  dwResult = ERROR_SUCCESS;
152  }
153  }
154  else
155  {
156  dwResult = GetLastError();
157  }
158 
159  CloseServiceHandle(hService);
161 
162  return dwResult;
163 }
#define MAX_WAIT_TIME
Definition: control.c:15
VOID WINAPI DECLSPEC_HOTPATCH Sleep(IN DWORD dwMilliseconds)
Definition: synch.c:790
#define ERROR_SUCCESS
Definition: deptool.c:10
DWORD dwCurrentState
Definition: winsvc.h:100
#define SC_MANAGER_CONNECT
Definition: winsvc.h:14
#define SERVICE_PAUSED
Definition: winsvc.h:27
#define ERROR_SERVICE_REQUEST_TIMEOUT
Definition: winerror.h:604
DWORD WINAPI GetTickCount(VOID)
Definition: time.c:455
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1044
#define SERVICE_INTERROGATE
Definition: winsvc.h:60
BOOL WINAPI QueryServiceStatusEx(SC_HANDLE hService, SC_STATUS_TYPE InfoLevel, LPBYTE lpBuffer, DWORD cbBufSize, LPDWORD pcbBytesNeeded)
Definition: scm.c:2830
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
DWORD dwCheckPoint
Definition: winsvc.h:104
unsigned char * LPBYTE
Definition: typedefs.h:53
#define FALSE
Definition: types.h:117
unsigned int BOOL
Definition: ntddk_ex.h:94
#define SERVICE_RUNNING
Definition: winsvc.h:24
#define ERROR_INVALID_SERVICE_CONTROL
Definition: winerror.h:603
SC_HANDLE WINAPI OpenServiceW(SC_HANDLE hSCManager, LPCWSTR lpServiceName, DWORD dwDesiredAccess)
Definition: scm.c:2103
BOOL WINAPI CloseServiceHandle(SC_HANDLE hSCObject)
Definition: scm.c:577
smooth NULL
Definition: ftsmooth.c:416
BOOL Control(DWORD Control, LPCTSTR ServiceName, LPCTSTR *Args, INT ArgCount)
Definition: control.c:13
_At_(*)(_In_ PWSK_CLIENT Client, _In_opt_ PUNICODE_STRING NodeName, _In_opt_ PUNICODE_STRING ServiceName, _In_opt_ ULONG NameSpace, _In_opt_ GUID *Provider, _In_opt_ PADDRINFOEXW Hints, _Outptr_ PADDRINFOEXW *Result, _In_opt_ PEPROCESS OwningProcess, _In_opt_ PETHREAD OwningThread, _Inout_ PIRP Irp Result)(Mem)) NTSTATUS(WSKAPI *PFN_WSK_GET_ADDRESS_INFO
Definition: wsk.h:426
#define SERVICE_QUERY_STATUS
Definition: winsvc.h:55
DWORD DoControlService(LPWSTR ServiceName, HWND hProgress, DWORD Control)
Definition: control.c:18
LPTSTR ServiceName
Definition: ServiceMain.c:15
DWORD dwWaitHint
Definition: winsvc.h:105
static SERVICE_STATUS ServiceStatus
Definition: browser.c:22
unsigned long DWORD
Definition: ntddk_ex.h:95
#define SERVICE_CONTROL_PAUSE
Definition: winsvc.h:37
Status
Definition: gdiplustypes.h:24
SC_HANDLE WINAPI OpenSCManagerW(LPCWSTR lpMachineName, LPCWSTR lpDatabaseName, DWORD dwDesiredAccess)
Definition: scm.c:2011
SC_HANDLE hSCManager
Definition: sc.c:12
VOID IncrementProgressBar(HANDLE hProgress, UINT NewPos)
Definition: progress.c:338
#define DEFAULT_STEP
Definition: precomp.h:110
#define SERVICE_CONTROL_CONTINUE
Definition: winsvc.h:38
BOOL WINAPI ControlService(SC_HANDLE hService, DWORD dwControl, LPSERVICE_STATUS lpServiceStatus)
Definition: scm.c:619
#define DPRINT1
Definition: precomp.h:8
WCHAR * LPWSTR
Definition: xmlstorage.h:184
#define SERVICE_PAUSE_CONTINUE
Definition: winsvc.h:59