aboutsummaryrefslogtreecommitdiff
path: root/src/ext/Util/ca/scamanifest.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/ext/Util/ca/scamanifest.cpp')
-rw-r--r--src/ext/Util/ca/scamanifest.cpp377
1 files changed, 377 insertions, 0 deletions
diff --git a/src/ext/Util/ca/scamanifest.cpp b/src/ext/Util/ca/scamanifest.cpp
new file mode 100644
index 00000000..adb8d3d3
--- /dev/null
+++ b/src/ext/Util/ca/scamanifest.cpp
@@ -0,0 +1,377 @@
1// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.
2
3#include "precomp.h"
4
5LPCWSTR vcsPerfmonManifestQuery = L"SELECT `Component_`, `File`, `ResourceFileDirectory` FROM `Wix4PerfmonManifest`";
6LPCWSTR vcsEventManifestQuery = L"SELECT `Component_`, `File` FROM `Wix4EventManifest`";
7enum ePerfMonManifestQuery { pfmComponent = 1, pfmFile, pfmResourceFileDir };
8enum eEventManifestQuery { emComponent = 1, emFile};
9
10BOOL IsVistaOrAbove()
11{
12 OSVERSIONINFO osvi;
13 ZeroMemory(&osvi, sizeof(OSVERSIONINFO));
14 osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
15 #pragma warning(suppress: 4996) //TODO: use non-deprecated function to check OS version
16 if (!::GetVersionEx(&osvi))
17 {
18 return false;
19 }
20 return osvi.dwMajorVersion >= 6;
21}
22
23
24/********************************************************************
25 ConfigurePerfmonManifestRegister - CUSTOM ACTION ENTRY POINT for scheduling
26 Perfmon counter manifest registering
27
28********************************************************************/
29extern "C" UINT __stdcall ConfigurePerfmonManifestRegister(
30 __in MSIHANDLE hInstall
31 )
32{
33 HRESULT hr;
34 UINT er = ERROR_SUCCESS;
35
36 PMSIHANDLE hView, hRec;
37 LPWSTR pwzData = NULL, pwzResourceFilePath = NULL, pwzFile = NULL, pwzCommand = NULL;
38 INSTALLSTATE isInstalled, isAction;
39
40 hr = WcaInitialize(hInstall, "ConfigurePerfmonManifestReg");
41 ExitOnFailure(hr, "Failed to initialize");
42
43 if (!IsVistaOrAbove())
44 {
45 WcaLog(LOGMSG_VERBOSE, "Skipping ConfigurePerfmonManifestRegister() because the target system does not support perfmon manifest");
46 ExitFunction1(hr = S_FALSE);
47 }
48 // check to see if necessary tables are specified
49 if (S_OK != WcaTableExists(L"Wix4PerfmonManifest"))
50 {
51 WcaLog(LOGMSG_VERBOSE, "Skipping ConfigurePerfmonManifestRegister() because Wix4PerfmonManifest table not present");
52 ExitFunction1(hr = S_FALSE);
53 }
54
55 hr = WcaOpenExecuteView(vcsPerfmonManifestQuery, &hView);
56 ExitOnFailure(hr, "failed to open view on PerfMonManifest table");
57 while ((hr = WcaFetchRecord(hView, &hRec)) == S_OK)
58 {
59 // get component install state
60 hr = WcaGetRecordString(hRec, pfmComponent, &pwzData);
61 ExitOnFailure(hr, "failed to get Component for PerfMonManifest");
62 er = ::MsiGetComponentStateW(hInstall, pwzData, &isInstalled, &isAction);
63 hr = HRESULT_FROM_WIN32(er);
64 ExitOnFailure(hr, "failed to get Component state for PerfMonManifest");
65 if (!WcaIsInstalling(isInstalled, isAction))
66 {
67 continue;
68 }
69
70 hr = WcaGetRecordFormattedString(hRec, pfmFile, &pwzFile);
71 ExitOnFailure(hr, "failed to get File for PerfMonManifest");
72
73 hr = WcaGetRecordFormattedString(hRec, pfmResourceFileDir, &pwzResourceFilePath);
74 ExitOnFailure(hr, "failed to get ApplicationIdentity for PerfMonManifest");
75 size_t iResourcePath = lstrlenW(pwzResourceFilePath);
76 if ( iResourcePath > 0 && *(pwzResourceFilePath + iResourcePath -1) == L'\\')
77 *(pwzResourceFilePath + iResourcePath -1) = 0; //remove the trailing '\'
78
79 hr = StrAllocFormatted(&pwzCommand, L"\"unlodctr.exe\" /m:\"%s\"", pwzFile);
80 ExitOnFailure(hr, "failed to copy string in PerfMonManifest");
81
82 hr = WcaDoDeferredAction(CUSTOM_ACTION_DECORATION(L"RollbackRegisterPerfmonManifest"), pwzCommand, COST_PERFMONMANIFEST_UNREGISTER);
83 ExitOnFailure(hr, "failed to schedule RollbackRegisterPerfmonManifest action");
84
85 if ( *pwzResourceFilePath )
86 {
87 hr = StrAllocFormatted(&pwzCommand, L"\"lodctr.exe\" /m:\"%s\" \"%s\"", pwzFile, pwzResourceFilePath);
88 ExitOnFailure(hr, "failed to copy string in PerfMonManifest");
89 }
90 else
91 {
92 hr = StrAllocFormatted(&pwzCommand, L"\"lodctr.exe\" /m:\"%s\"", pwzFile);
93 ExitOnFailure(hr, "failed to copy string in PerfMonManifest");
94 }
95
96 WcaLog(LOGMSG_VERBOSE, "RegisterPerfmonManifest's CustomActionData: '%ls'", pwzCommand);
97
98 hr = WcaDoDeferredAction(CUSTOM_ACTION_DECORATION(L"RegisterPerfmonManifest"), pwzCommand, COST_PERFMONMANIFEST_REGISTER);
99 ExitOnFailure(hr, "failed to schedule RegisterPerfmonManifest action");
100 }
101
102 if (hr == E_NOMOREITEMS)
103 {
104 hr = S_OK;
105 }
106 ExitOnFailure(hr, "Failure while processing PerfMonManifest");
107
108 hr = S_OK;
109
110LExit:
111 ReleaseStr(pwzData);
112 ReleaseStr(pwzResourceFilePath);
113 ReleaseStr(pwzFile);
114 ReleaseStr(pwzCommand);
115
116 er = SUCCEEDED(hr) ? ERROR_SUCCESS : ERROR_INSTALL_FAILURE;
117 return WcaFinalize(er);
118}
119
120
121/********************************************************************
122 ConfigurePerfmonUninstall - CUSTOM ACTION ENTRY POINT for uninstalling
123 Perfmon counters
124
125********************************************************************/
126extern "C" UINT __stdcall ConfigurePerfmonManifestUnregister(
127 __in MSIHANDLE hInstall
128 )
129{
130 HRESULT hr;
131 UINT er = ERROR_SUCCESS;
132
133 PMSIHANDLE hView, hRec;
134 LPWSTR pwzData = NULL, pwzResourceFilePath = NULL, pwzFile = NULL, pwzCommand = NULL;
135 INSTALLSTATE isInstalled, isAction;
136
137 hr = WcaInitialize(hInstall, "ConfigurePerfmonManifestUnreg");
138 ExitOnFailure(hr, "Failed to initialize");
139
140 if (!IsVistaOrAbove())
141 {
142 WcaLog(LOGMSG_VERBOSE, "Skipping ConfigurePerfmonManifestUnregister() because the target system does not support perfmon manifest");
143 ExitFunction1(hr = S_FALSE);
144 }
145 // check to see if necessary tables are specified
146 if (WcaTableExists(L"Wix4PerfmonManifest") != S_OK)
147 {
148 WcaLog(LOGMSG_VERBOSE, "Skipping ConfigurePerfmonManifestUnregister() because Wix4PerfmonManifest table not present");
149 ExitFunction1(hr = S_FALSE);
150 }
151
152 hr = WcaOpenExecuteView(vcsPerfmonManifestQuery, &hView);
153 ExitOnFailure(hr, "failed to open view on Wix4PerfmonManifest table");
154 while ((hr = WcaFetchRecord(hView, &hRec)) == S_OK)
155 {
156 // get component install state
157 hr = WcaGetRecordString(hRec, pfmComponent, &pwzData);
158 ExitOnFailure(hr, "failed to get Component for Wix4PerfmonManifest");
159 er = ::MsiGetComponentStateW(hInstall, pwzData, &isInstalled, &isAction);
160 hr = HRESULT_FROM_WIN32(er);
161 ExitOnFailure(hr, "failed to get Component state for Wix4PerfmonManifest");
162 if (!WcaIsUninstalling(isInstalled, isAction))
163 {
164 continue;
165 }
166
167 hr = WcaGetRecordFormattedString(hRec, pfmFile, &pwzFile);
168 ExitOnFailure(hr, "failed to get File for Wix4PerfmonManifest");
169
170 hr = WcaGetRecordFormattedString(hRec, pfmResourceFileDir, &pwzResourceFilePath);
171 ExitOnFailure(hr, "failed to get ApplicationIdentity for Wix4PerfmonManifest");
172 size_t iResourcePath = lstrlenW(pwzResourceFilePath);
173 if ( iResourcePath > 0 && *(pwzResourceFilePath + iResourcePath -1) == L'\\')
174 *(pwzResourceFilePath + iResourcePath -1) = 0; //remove the trailing '\'
175
176 hr = StrAllocFormatted(&pwzCommand, L"\"lodctr.exe\" /m:\"%s\" \"%s\"", pwzFile, pwzResourceFilePath);
177 ExitOnFailure(hr, "failed to copy string in Wix4PerfmonManifest");
178
179 hr = WcaDoDeferredAction(CUSTOM_ACTION_DECORATION(L"RollbackUnregisterPerfmonManifest"), pwzCommand, COST_PERFMONMANIFEST_REGISTER);
180 ExitOnFailure(hr, "failed to schedule RollbackUnregisterPerfmonManifest action");
181
182 hr = StrAllocFormatted(&pwzCommand, L"\"unlodctr.exe\" /m:\"%s\"", pwzFile);
183 ExitOnFailure(hr, "failed to copy string in PerfMonManifest");
184
185 WcaLog(LOGMSG_VERBOSE, "UnRegisterPerfmonManifest's CustomActionData: '%ls'", pwzCommand);
186
187 hr = WcaDoDeferredAction(CUSTOM_ACTION_DECORATION(L"UnregisterPerfmonManifest"), pwzCommand, COST_PERFMONMANIFEST_UNREGISTER);
188 ExitOnFailure(hr, "failed to schedule UnregisterPerfmonManifest action");
189 }
190
191 if (hr == E_NOMOREITEMS)
192 {
193 hr = S_OK;
194 }
195 ExitOnFailure(hr, "Failure while processing PerfMonManifest");
196
197 hr = S_OK;
198
199LExit:
200 ReleaseStr(pwzData);
201 ReleaseStr(pwzResourceFilePath);
202 ReleaseStr(pwzFile);
203 ReleaseStr(pwzCommand);
204
205 er = SUCCEEDED(hr) ? ERROR_SUCCESS : ERROR_INSTALL_FAILURE;
206 return WcaFinalize(er);
207}
208
209/********************************************************************
210 ConfigureEventManifestRegister - CUSTOM ACTION ENTRY POINT for scheduling
211 Event manifest registering
212
213********************************************************************/
214extern "C" UINT __stdcall ConfigureEventManifestRegister(
215 __in MSIHANDLE hInstall
216 )
217{
218 HRESULT hr;
219 UINT er = ERROR_SUCCESS;
220
221 PMSIHANDLE hView, hRec;
222 LPWSTR pwzData = NULL, pwzFile = NULL, pwzCommand = NULL;
223 INSTALLSTATE isInstalled, isAction;
224
225 hr = WcaInitialize(hInstall, "ConfigureEventManifestReg");
226 ExitOnFailure(hr, "Failed to initialize");
227
228 if (!IsVistaOrAbove())
229 {
230 WcaLog(LOGMSG_VERBOSE, "Skipping ConfigureEventManifestRegister() because the target system does not support event manifest");
231 ExitFunction1(hr = S_FALSE);
232 }
233 // check to see if necessary tables are specified
234 if (S_OK != WcaTableExists(L"Wix4EventManifest"))
235 {
236 WcaLog(LOGMSG_VERBOSE, "Skipping ConfigureEventManifestRegister() because Wix4EventManifest table not present");
237 ExitFunction1(hr = S_FALSE);
238 }
239
240 hr = WcaOpenExecuteView(vcsEventManifestQuery, &hView);
241 ExitOnFailure(hr, "failed to open view on Wix4EventManifest table");
242 while ((hr = WcaFetchRecord(hView, &hRec)) == S_OK)
243 {
244 // get component install state
245 hr = WcaGetRecordString(hRec, emComponent, &pwzData);
246 ExitOnFailure(hr, "failed to get Component for Wix4EventManifest");
247 er = ::MsiGetComponentStateW(hInstall, pwzData, &isInstalled, &isAction);
248 hr = HRESULT_FROM_WIN32(er);
249 ExitOnFailure(hr, "failed to get Component state for Wix4EventManifest");
250 if (!WcaIsInstalling(isInstalled, isAction))
251 {
252 continue;
253 }
254
255 hr = WcaGetRecordFormattedString(hRec, emFile, &pwzFile);
256 ExitOnFailure(hr, "failed to get File for Wix4EventManifest");
257
258 hr = StrAllocFormatted(&pwzCommand, L"\"wevtutil.exe\" um \"%s\"", pwzFile);
259 ExitOnFailure(hr, "failed to copy string in Wix4EventManifest");
260
261 hr = WcaDoDeferredAction(CUSTOM_ACTION_DECORATION(L"RollbackRegisterEventManifest"), pwzCommand, COST_PERFMONMANIFEST_UNREGISTER);
262 ExitOnFailure(hr, "failed to schedule RollbackRegisterEventManifest action");
263
264 hr = StrAllocFormatted(&pwzCommand, L"\"wevtutil.exe\" im \"%s\"", pwzFile);
265 ExitOnFailure(hr, "failed to copy string in Wix4EventManifest");
266 WcaLog(LOGMSG_VERBOSE, "RegisterEventManifest's CustomActionData: '%ls'", pwzCommand);
267
268 hr = WcaDoDeferredAction(CUSTOM_ACTION_DECORATION(L"RegisterEventManifest"), pwzCommand, COST_EVENTMANIFEST_REGISTER);
269 ExitOnFailure(hr, "failed to schedule RegisterEventManifest action");
270 }
271
272 if (hr == E_NOMOREITEMS)
273 {
274 hr = S_OK;
275 }
276 ExitOnFailure(hr, "Failure while processing Wix4EventManifest");
277
278 hr = S_OK;
279
280LExit:
281 ReleaseStr(pwzData);
282 ReleaseStr(pwzFile);
283 ReleaseStr(pwzCommand);
284
285 er = SUCCEEDED(hr) ? ERROR_SUCCESS : ERROR_INSTALL_FAILURE;
286 return WcaFinalize(er);
287}
288
289
290
291/********************************************************************
292 ConfigureEventManifestRegister - CUSTOM ACTION ENTRY POINT for scheduling
293 Event manifest registering
294
295********************************************************************/
296extern "C" UINT __stdcall ConfigureEventManifestUnregister(
297 __in MSIHANDLE hInstall
298 )
299{
300 HRESULT hr;
301 UINT er = ERROR_SUCCESS;
302
303 PMSIHANDLE hView, hRec;
304 LPWSTR pwzData = NULL, pwzFile = NULL, pwzCommand = NULL;
305 INSTALLSTATE isInstalled, isAction;
306
307 hr = WcaInitialize(hInstall, "ConfigureEventManifestUnreg");
308 ExitOnFailure(hr, "Failed to initialize");
309
310 if (!IsVistaOrAbove())
311 {
312 WcaLog(LOGMSG_VERBOSE, "Skipping ConfigureEventManifestUnregister() because the target system does not support event manifest");
313 ExitFunction1(hr = S_FALSE);
314 }
315 // check to see if necessary tables are specified
316 if (S_OK != WcaTableExists(L"Wix4EventManifest"))
317 {
318 WcaLog(LOGMSG_VERBOSE, "Skipping ConfigureEventManifestUnregister() because Wix4EventManifest table not present");
319 ExitFunction1(hr = S_FALSE);
320 }
321
322 hr = WcaOpenExecuteView(vcsEventManifestQuery, &hView);
323 ExitOnFailure(hr, "failed to open view on Wix4EventManifest table");
324 while ((hr = WcaFetchRecord(hView, &hRec)) == S_OK)
325 {
326 // get component install state
327 hr = WcaGetRecordString(hRec, emComponent, &pwzData);
328 ExitOnFailure(hr, "failed to get Component for Wix4EventManifest");
329 er = ::MsiGetComponentStateW(hInstall, pwzData, &isInstalled, &isAction);
330 hr = HRESULT_FROM_WIN32(er);
331 ExitOnFailure(hr, "failed to get Component state for Wix4EventManifest");
332
333 // nothing to do on an install
334 // schedule the rollback action when reinstalling to re-register pre-patch manifest
335 if (!WcaIsUninstalling(isInstalled, isAction) && !WcaIsReInstalling(isInstalled, isAction))
336 {
337 continue;
338 }
339
340 hr = WcaGetRecordFormattedString(hRec, emFile, &pwzFile);
341 ExitOnFailure(hr, "failed to get File for Wix4EventManifest");
342
343 hr = StrAllocFormatted(&pwzCommand, L"\"wevtutil.exe\" im \"%s\"", pwzFile);
344 ExitOnFailure(hr, "failed to copy string in Wix4EventManifest");
345
346 hr = WcaDoDeferredAction(CUSTOM_ACTION_DECORATION(L"RollbackUnregisterEventManifest"), pwzCommand, COST_PERFMONMANIFEST_REGISTER);
347 ExitOnFailure(hr, "failed to schedule RollbackUnregisterEventManifest action");
348
349 // no need to uninstall on a repair/patch. Register action will re-register and update the manifest.
350 if (!WcaIsReInstalling(isInstalled, isAction))
351 {
352 hr = StrAllocFormatted(&pwzCommand, L"\"wevtutil.exe\" um \"%s\"", pwzFile);
353 ExitOnFailure(hr, "failed to copy string in Wix4EventManifest");
354 WcaLog(LOGMSG_VERBOSE, "UnregisterEventManifest's CustomActionData: '%ls'", pwzCommand);
355
356 hr = WcaDoDeferredAction(CUSTOM_ACTION_DECORATION(L"UnregisterEventManifest"), pwzCommand, COST_PERFMONMANIFEST_UNREGISTER);
357 ExitOnFailure(hr, "failed to schedule UnregisterEventManifest action");
358 }
359 }
360
361 if (hr == E_NOMOREITEMS)
362 {
363 hr = S_OK;
364 }
365 ExitOnFailure(hr, "Failure while processing Wix4EventManifest");
366
367 hr = S_OK;
368
369LExit:
370 ReleaseStr(pwzData);
371 ReleaseStr(pwzFile);
372 ReleaseStr(pwzCommand);
373
374 er = SUCCEEDED(hr) ? ERROR_SUCCESS : ERROR_INSTALL_FAILURE;
375 return WcaFinalize(er);
376}
377