summaryrefslogtreecommitdiffstats
path: root/other-licenses/nsis/Contrib/ServicesHelper/Services.cpp
blob: 5c3d2fe59f555cbcdc5a2a2295ef9c33050c78b4 (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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include <windows.h>
#include "../../../../toolkit/mozapps/update/common/pathhash.h"

#pragma comment(lib, "advapi32.lib") 

typedef struct _stack_t {
  struct _stack_t *next;
  TCHAR text[MAX_PATH];
} stack_t;

int popstring(stack_t **stacktop, LPTSTR str, int len);
void pushstring(stack_t **stacktop, LPCTSTR str, int len);

/**
 * Determines if the specified service exists or not
 *
 * @param  serviceName The name of the service to check
 * @param  exists      Whether or not the service exists 
 * @return TRUE if there were no errors
 */
static BOOL
IsServiceInstalled(LPCWSTR serviceName, BOOL &exists)
{
  exists = FALSE;

  // Get a handle to the local computer SCM database with full access rights.
  SC_HANDLE serviceManager = OpenSCManager(NULL, NULL, 
                                           SC_MANAGER_ENUMERATE_SERVICE);
  if (!serviceManager) {
    return FALSE;
  }

  SC_HANDLE serviceHandle = OpenServiceW(serviceManager, 
                                         serviceName, 
                                         SERVICE_QUERY_CONFIG);
  if (!serviceHandle && GetLastError() != ERROR_SERVICE_DOES_NOT_EXIST) {
    CloseServiceHandle(serviceManager);
    return FALSE;
  }
 
  if (serviceHandle) {
    CloseServiceHandle(serviceHandle);
    exists = TRUE;
  } 

  CloseServiceHandle(serviceManager);
  return TRUE;
}

/**
 * Determines if the specified service is installed or not
 * 
 * @param  stacktop  A pointer to the top of the stack
 * @param  variables A pointer to the NSIS variables
 * @return 0 if the service does not exist
 *         1 if the service does exist
 *         -1 if there was an error.
 */
extern "C" void __declspec(dllexport)
IsInstalled(HWND hwndParent, int string_size, 
            TCHAR *variables, stack_t **stacktop, void *extra)
{
  TCHAR tmp[MAX_PATH] = { L'\0' };
  WCHAR serviceName[MAX_PATH] = { '\0' };
  popstring(stacktop, tmp, MAX_PATH);

#if !defined(UNICODE)
    MultiByteToWideChar(CP_ACP, 0, tmp, -1, serviceName, MAX_PATH);
#else
    wcscpy(serviceName, tmp);
#endif

  BOOL serviceInstalled;
  if (!IsServiceInstalled(serviceName, serviceInstalled)) {
    pushstring(stacktop, TEXT("-1"), 3);
  } else {
    pushstring(stacktop, serviceInstalled ? TEXT("1") : TEXT("0"), 2);
  }
}

/**
 * Stops the specified service.
 * 
 * @param  serviceName The name of the service to stop
 * @return TRUE if the operation was successful 
 */
static BOOL
StopService(LPCWSTR serviceName)
{
  // Get a handle to the local computer SCM database with full access rights.
  SC_HANDLE serviceManager = OpenSCManager(NULL, NULL, 
                                           SC_MANAGER_ENUMERATE_SERVICE);
  if (!serviceManager) {
    return FALSE;
  }

  SC_HANDLE serviceHandle = OpenServiceW(serviceManager, 
                                         serviceName, 
                                         SERVICE_STOP);
  if (!serviceHandle) {
    CloseServiceHandle(serviceManager);
    return FALSE;
  }

  //Stop the service so it deletes faster and so the uninstaller
  // can actually delete its EXE.
  DWORD totalWaitTime = 0;
  SERVICE_STATUS status;
  static const int maxWaitTime = 1000 * 60; // Never wait more than a minute
  BOOL stopped = FALSE;
  if (ControlService(serviceHandle, SERVICE_CONTROL_STOP, &status)) {
    do {
      Sleep(status.dwWaitHint);
      // + 10 milliseconds to make sure we always approach maxWaitTime
      totalWaitTime += (status.dwWaitHint + 10);
      if (status.dwCurrentState == SERVICE_STOPPED) {
        stopped = true;
        break;
      } else if (totalWaitTime > maxWaitTime) {
        break;
      }
    } while (QueryServiceStatus(serviceHandle, &status));
  }

  CloseServiceHandle(serviceHandle);
  CloseServiceHandle(serviceManager);
  return stopped;
}

/**
 * Stops the specified service
 * 
 * @param  stacktop  A pointer to the top of the stack
 * @param  variables A pointer to the NSIS variables 
 * @return 1 if the service was stopped, 0 on error
 */
extern "C" void __declspec(dllexport)
Stop(HWND hwndParent, int string_size, 
     TCHAR *variables, stack_t **stacktop, void *extra)
{
  TCHAR tmp[MAX_PATH] = { L'\0' };
  WCHAR serviceName[MAX_PATH] = { '\0' };

  popstring(stacktop, tmp, MAX_PATH);

#if !defined(UNICODE)
    MultiByteToWideChar(CP_ACP, 0, tmp, -1, serviceName, MAX_PATH);
#else
    wcscpy(serviceName, tmp);
#endif

  if (StopService(serviceName)) {
    pushstring(stacktop, TEXT("1"), 2);
  } else {
    pushstring(stacktop, TEXT("0"), 2);
  }
}

/**
 * Determines a unique registry path from a file or directory path
 * 
 * @param  stacktop  A pointer to the top of the stack
 * @param  variables A pointer to the NSIS variables 
 * @return The unique registry path or an empty string on error
 */
extern "C" void __declspec(dllexport)
PathToUniqueRegistryPath(HWND hwndParent, int string_size, 
                         TCHAR *variables, stack_t **stacktop, 
                         void *extra)
{
  TCHAR tmp[MAX_PATH] = { L'\0' };
  WCHAR installBasePath[MAX_PATH] = { '\0' };
  popstring(stacktop, tmp, MAX_PATH);

#if !defined(UNICODE)
    MultiByteToWideChar(CP_ACP, 0, tmp, -1, installBasePath, MAX_PATH);
#else
    wcscpy(installBasePath, tmp);
#endif

  WCHAR registryPath[MAX_PATH + 1] = { '\0' };
  if (CalculateRegistryPathFromFilePath(installBasePath, registryPath)) {
    pushstring(stacktop, registryPath, wcslen(registryPath) + 1);
  } else {
    pushstring(stacktop, TEXT(""), 1);
  }
}

BOOL WINAPI 
DllMain(HANDLE hInst, ULONG ul_reason_for_call, LPVOID lpReserved)
{
  return TRUE;
}

/**
 * Removes an element from the top of the NSIS stack
 *
 * @param  stacktop A pointer to the top of the stack
 * @param  str      The string to pop to
 * @param  len      The max length
 * @return 0 on success
*/
int popstring(stack_t **stacktop, TCHAR *str, int len)
{
  // Removes the element from the top of the stack and puts it in the buffer
  stack_t *th;
  if (!stacktop || !*stacktop) {
    return 1;
  }

  th = (*stacktop);
  lstrcpyn(str,th->text, len);
  *stacktop = th->next;
  GlobalFree((HGLOBAL)th);
  return 0;
}

/**
 * Adds an element to the top of the NSIS stack
 *
 * @param  stacktop A pointer to the top of the stack
 * @param  str      The string to push on the stack
 * @param  len      The length of the string to push on the stack
 * @return 0 on success
*/
void pushstring(stack_t **stacktop, const TCHAR *str, int len)
{
  stack_t *th;
  if (!stacktop) { 
    return;
  }

  th = (stack_t*)GlobalAlloc(GPTR, sizeof(stack_t) + len);
  lstrcpyn(th->text, str, len);
  th->next = *stacktop;
  *stacktop = th;
}