ReactOS  0.4.14-dev-41-g31d7680
start.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/start.c
5  * PURPOSE: Start a service
6  * COPYRIGHT: Copyright 2005-2015 Ged Murphy <gedmurphy@reactos.org>
7  *
8  */
9 
10 #include "precomp.h"
11 
12 #define MAX_WAIT_TIME 30000
13 
14 BOOL
16  HANDLE hProgress,
17  LPWSTR lpStartParams)
18 {
19  SC_HANDLE hSCManager;
20  SC_HANDLE hService;
22  DWORD BytesNeeded = 0;
23  DWORD StartTickCount;
24  DWORD OldCheckPoint;
25  DWORD WaitTime;
26  DWORD MaxWait;
27  BOOL Result = FALSE;
28 
29  BOOL bWhiteSpace = TRUE;
30  LPWSTR lpChar;
31  DWORD dwArgsCount = 0;
32  LPCWSTR *lpArgsVector = NULL;
33 
34  if (lpStartParams != NULL)
35  {
36  /* Count the number of arguments */
37  lpChar = lpStartParams;
38  while (*lpChar != 0)
39  {
40  if (iswspace(*lpChar))
41  {
42  bWhiteSpace = TRUE;
43  }
44  else
45  {
46  if (bWhiteSpace != FALSE)
47  {
48  dwArgsCount++;
49  bWhiteSpace = FALSE;
50  }
51  }
52 
53  lpChar++;
54  }
55 
56  /*
57  * Allocate the arguments vector.
58  * Do not add the service name here because services.exe does it for us!
59  */
60  lpArgsVector = LocalAlloc(LMEM_FIXED, dwArgsCount * sizeof(LPCWSTR));
61  if (!lpArgsVector)
62  return FALSE;
63 
64  /* Fill the arguments vector */
65  dwArgsCount = 0;
66  bWhiteSpace = TRUE;
67  lpChar = lpStartParams;
68  while (*lpChar != 0)
69  {
70  if (iswspace(*lpChar))
71  {
72  *lpChar = 0;
73  bWhiteSpace = TRUE;
74  }
75  else
76  {
77  if (bWhiteSpace != FALSE)
78  {
79  lpArgsVector[dwArgsCount] = lpChar;
80  dwArgsCount++;
81  bWhiteSpace = FALSE;
82  }
83  }
84 
85  lpChar++;
86  }
87  }
88 
90  NULL,
92  if (!hSCManager)
93  {
94  if (lpArgsVector)
95  LocalFree((LPVOID)lpArgsVector);
96  return FALSE;
97  }
98 
99  hService = OpenServiceW(hSCManager,
100  ServiceName,
102  if (!hService)
103  {
105  if (lpArgsVector)
106  LocalFree((LPVOID)lpArgsVector);
107  return FALSE;
108  }
109 
110  /* Start the service */
111  Result = StartServiceW(hService,
112  dwArgsCount,
113  lpArgsVector);
115  {
116  /* If it's already running, just return TRUE */
117  Result = TRUE;
118  }
119  else if (Result)
120  {
121  if (hProgress)
122  {
123  /* Increment the progress bar */
125  }
126 
127  /* Get the service status to check if it's running */
128  Result = QueryServiceStatusEx(hService,
131  sizeof(SERVICE_STATUS_PROCESS),
132  &BytesNeeded);
133  if (Result)
134  {
135  Result = FALSE;
136  MaxWait = MAX_WAIT_TIME;
137  OldCheckPoint = ServiceStatus.dwCheckPoint;
138  StartTickCount = GetTickCount();
139 
140  /* Loop until it's running */
142  {
143  int i;
144  /* Fixup the wait time */
145  WaitTime = ServiceStatus.dwWaitHint / 10;
146 
147  if (WaitTime < 1000) WaitTime = 1000;
148  else if (WaitTime > 10000) WaitTime = 10000;
149 
150  /* We don't wanna wait for up to 10 secs without incrementing */
151  for (i = WaitTime / 1000; i > 0; i--)
152  {
153  Sleep(1000);
154  if (hProgress)
155  {
156  /* Increment the progress bar */
158  }
159  }
160 
161 
162  /* Get the latest status info */
163  if (!QueryServiceStatusEx(hService,
166  sizeof(SERVICE_STATUS_PROCESS),
167  &BytesNeeded))
168  {
169  /* Something went wrong... */
170  break;
171  }
172 
173  /* Is the service making progress? */
174  if (ServiceStatus.dwCheckPoint > OldCheckPoint)
175  {
176  /* It is, get the latest tickcount to reset the max wait time */
177  StartTickCount = GetTickCount();
178  OldCheckPoint = ServiceStatus.dwCheckPoint;
179  }
180  else
181  {
182  /* It's not, make sure we haven't exceeded our wait time */
183  if (GetTickCount() >= StartTickCount + MaxWait)
184  {
185  /* We have, give up */
186  break;
187  }
188  }
189  }
190  }
191 
193  {
194  Result = TRUE;
195  }
196  }
197 
198  CloseServiceHandle(hService);
199 
201 
202  if (lpArgsVector)
203  LocalFree((LPVOID)lpArgsVector);
204 
205  return Result;
206 }
#define TRUE
Definition: types.h:120
VOID WINAPI DECLSPEC_HOTPATCH Sleep(IN DWORD dwMilliseconds)
Definition: synch.c:790
DWORD dwCurrentState
Definition: winsvc.h:100
#define SC_MANAGER_CONNECT
Definition: winsvc.h:14
const WCHAR * LPCWSTR
Definition: xmlstorage.h:185
static SERVICE_STATUS ServiceStatus
Definition: dcomlaunch.c:27
DWORD WINAPI GetTickCount(VOID)
Definition: time.c:445
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1059
BOOL WINAPI QueryServiceStatusEx(SC_HANDLE hService, SC_STATUS_TYPE InfoLevel, LPBYTE lpBuffer, DWORD cbBufSize, LPDWORD pcbBytesNeeded)
Definition: scm.c:2830
BOOL DoStartService(LPWSTR ServiceName, HANDLE hProgress, LPWSTR lpStartParams)
Definition: start.c:15
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:52
unsigned int BOOL
Definition: ntddk_ex.h:94
#define SERVICE_RUNNING
Definition: winsvc.h:24
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
_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
#define LMEM_FIXED
Definition: winbase.h:349
BOOL WINAPI StartServiceW(SC_HANDLE hService, DWORD dwNumServiceArgs, LPCWSTR *lpServiceArgVectors)
Definition: scm.c:2923
LPTSTR ServiceName
Definition: ServiceMain.c:15
DWORD dwWaitHint
Definition: winsvc.h:105
unsigned long DWORD
Definition: ntddk_ex.h:95
#define MAX_WAIT_TIME
Definition: start.c:12
#define iswspace(_c)
Definition: ctype.h:669
SC_HANDLE WINAPI OpenSCManagerW(LPCWSTR lpMachineName, LPCWSTR lpDatabaseName, DWORD dwDesiredAccess)
Definition: scm.c:2011
SC_HANDLE hSCManager
Definition: sc.c:12
#define SERVICE_START
Definition: winsvc.h:57
VOID IncrementProgressBar(HANDLE hProgress, UINT NewPos)
Definition: progress.c:288
#define DEFAULT_STEP
Definition: precomp.h:108
HLOCAL NTAPI LocalFree(HLOCAL hMem)
Definition: heapmem.c:1577
HLOCAL NTAPI LocalAlloc(UINT uFlags, SIZE_T dwBytes)
Definition: heapmem.c:1373
WCHAR * LPWSTR
Definition: xmlstorage.h:184
#define ERROR_SERVICE_ALREADY_RUNNING
Definition: winerror.h:607