2 * Custom Action processing for the Microsoft Installer (msi.dll)
4 * Copyright 2005 Aric Stewart for CodeWeavers
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
30 #include "wine/debug.h"
31 #include "wine/unicode.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(msi);
35 #define CUSTOM_ACTION_TYPE_MASK 0x3F
36 static const WCHAR c_collen[] = {'C',':','\\',0};
37 static const WCHAR cszTempFolder[]= {'T','e','m','p','F','o','l','d','e','r',0};
39 typedef struct tagMSIRUNNINGACTION
47 static UINT HANDLE_CustomType1(MSIPACKAGE *package, LPCWSTR source,
48 LPCWSTR target, const INT type, LPCWSTR action);
49 static UINT HANDLE_CustomType2(MSIPACKAGE *package, LPCWSTR source,
50 LPCWSTR target, const INT type, LPCWSTR action);
51 static UINT HANDLE_CustomType17(MSIPACKAGE *package, LPCWSTR source,
52 LPCWSTR target, const INT type, LPCWSTR action);
53 static UINT HANDLE_CustomType18(MSIPACKAGE *package, LPCWSTR source,
54 LPCWSTR target, const INT type, LPCWSTR action);
55 static UINT HANDLE_CustomType19(MSIPACKAGE *package, LPCWSTR source,
56 LPCWSTR target, const INT type, LPCWSTR action);
57 static UINT HANDLE_CustomType50(MSIPACKAGE *package, LPCWSTR source,
58 LPCWSTR target, const INT type, LPCWSTR action);
59 static UINT HANDLE_CustomType34(MSIPACKAGE *package, LPCWSTR source,
60 LPCWSTR target, const INT type, LPCWSTR action);
62 typedef UINT (WINAPI *MsiCustomActionEntryPoint)( MSIHANDLE );
64 static CRITICAL_SECTION msi_custom_action_cs;
65 static CRITICAL_SECTION_DEBUG msi_custom_action_cs_debug =
67 0, 0, &msi_custom_action_cs,
68 { &msi_custom_action_cs_debug.ProcessLocksList,
69 &msi_custom_action_cs_debug.ProcessLocksList },
70 0, 0, { (DWORD_PTR)(__FILE__ ": msi_custom_action_cs") }
72 static CRITICAL_SECTION msi_custom_action_cs = { &msi_custom_action_cs_debug, -1, 0, 0, 0, 0 };
74 static struct list msi_pending_custom_actions = LIST_INIT( msi_pending_custom_actions );
76 static BOOL check_execution_scheduling_options(MSIPACKAGE *package, LPCWSTR action, UINT options)
81 if ((options & msidbCustomActionTypeClientRepeat) ==
82 msidbCustomActionTypeClientRepeat)
84 if (!(package->script->InWhatSequence & SEQUENCE_UI &&
85 package->script->InWhatSequence & SEQUENCE_EXEC))
87 TRACE("Skipping action due to dbCustomActionTypeClientRepeat option.\n");
91 else if (options & msidbCustomActionTypeFirstSequence)
93 if (package->script->InWhatSequence & SEQUENCE_UI &&
94 package->script->InWhatSequence & SEQUENCE_EXEC )
96 TRACE("Skipping action due to msidbCustomActionTypeFirstSequence option.\n");
100 else if (options & msidbCustomActionTypeOncePerProcess)
102 if (check_unique_action(package,action))
104 TRACE("Skipping action due to msidbCustomActionTypeOncePerProcess option.\n");
108 register_unique_action(package,action);
114 /* stores the CustomActionData before the action:
115 * [CustomActionData]Action
117 static LPWSTR msi_get_deferred_action(LPCWSTR action, LPWSTR actiondata)
122 static const WCHAR begin[] = {'[',0};
123 static const WCHAR end[] = {']',0};
126 return strdupW(action);
128 len = lstrlenW(action) + lstrlenW(actiondata) + 3;
129 deferred = msi_alloc(len * sizeof(WCHAR));
131 lstrcpyW(deferred, begin);
132 lstrcatW(deferred, actiondata);
133 lstrcatW(deferred, end);
134 lstrcatW(deferred, action);
139 UINT ACTION_CustomAction(MSIPACKAGE *package,LPCWSTR action, BOOL execute)
141 UINT rc = ERROR_SUCCESS;
143 static const WCHAR ExecSeqQuery[] =
144 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
145 '`','C','u','s','t','o' ,'m','A','c','t','i','o','n','`',
146 ' ','W','H','E','R','E',' ','`','A','c','t','i' ,'o','n','`',' ',
147 '=',' ','\'','%','s','\'',0};
149 LPCWSTR source, target;
150 LPWSTR ptr, deferred_data = NULL;
151 LPWSTR action_copy = strdupW(action);
152 WCHAR *deformated=NULL;
154 /* deferred action: [CustomActionData]Action */
155 if ((ptr = strchrW(action_copy, ']')))
157 deferred_data = action_copy + 1;
162 row = MSI_QueryGetRecord( package->db, ExecSeqQuery, action );
165 msi_free(action_copy);
166 return ERROR_CALL_NOT_IMPLEMENTED;
169 type = MSI_RecordGetInteger(row,2);
171 source = MSI_RecordGetString(row,3);
172 target = MSI_RecordGetString(row,4);
174 TRACE("Handling custom action %s (%x %s %s)\n",debugstr_w(action),type,
175 debugstr_w(source), debugstr_w(target));
177 /* handle some of the deferred actions */
178 if (type & msidbCustomActionTypeTSAware)
179 FIXME("msidbCustomActionTypeTSAware not handled\n");
181 if (type & msidbCustomActionTypeInScript)
183 if (type & msidbCustomActionTypeNoImpersonate)
184 FIXME("msidbCustomActionTypeNoImpersonate not handled\n");
186 if (type & msidbCustomActionTypeRollback)
188 FIXME("Rollback only action... rollbacks not supported yet\n");
189 schedule_action(package, ROLLBACK_SCRIPT, action);
195 LPWSTR actiondata = msi_dup_property(package, action);
196 LPWSTR deferred = msi_get_deferred_action(action, actiondata);
198 if (type & msidbCustomActionTypeCommit)
200 TRACE("Deferring Commit Action!\n");
201 schedule_action(package, COMMIT_SCRIPT, deferred);
205 TRACE("Deferring Action!\n");
206 schedule_action(package, INSTALL_SCRIPT, deferred);
217 static const WCHAR szActionData[] = {
218 'C','u','s','t','o','m','A','c','t','i','o','n','D','a','t','a',0};
219 static const WCHAR szBlank[] = {0};
220 LPWSTR actiondata = msi_dup_property( package, action );
222 MSI_SetPropertyW(package,szActionData,deferred_data);
224 MSI_SetPropertyW(package,szActionData,actiondata);
226 MSI_SetPropertyW(package,szActionData,szBlank);
227 msi_free(actiondata);
230 else if (!check_execution_scheduling_options(package,action,type))
236 switch (type & CUSTOM_ACTION_TYPE_MASK)
238 case 1: /* DLL file stored in a Binary table stream */
239 rc = HANDLE_CustomType1(package,source,target,type,action);
241 case 2: /* EXE file stored in a Binary table stream */
242 rc = HANDLE_CustomType2(package,source,target,type,action);
244 case 18: /*EXE file installed with package */
245 rc = HANDLE_CustomType18(package,source,target,type,action);
247 case 19: /* Error that halts install */
248 rc = HANDLE_CustomType19(package,source,target,type,action);
251 rc = HANDLE_CustomType17(package,source,target,type,action);
253 case 50: /*EXE file specified by a property value */
254 rc = HANDLE_CustomType50(package,source,target,type,action);
256 case 34: /*EXE to be run in specified directory */
257 rc = HANDLE_CustomType34(package,source,target,type,action);
259 case 35: /* Directory set with formatted text. */
260 deformat_string(package,target,&deformated);
261 MSI_SetTargetPathW(package, source, deformated);
262 msi_free(deformated);
264 case 51: /* Property set with formatted text. */
265 deformat_string(package,target,&deformated);
266 rc = MSI_SetPropertyW(package,source,deformated);
267 msi_free(deformated);
270 FIXME("UNHANDLED ACTION TYPE %i (%s %s)\n",
271 type & CUSTOM_ACTION_TYPE_MASK, debugstr_w(source),
276 msi_free(action_copy);
277 msiobj_release(&row->hdr);
282 static UINT store_binary_to_temp(MSIPACKAGE *package, LPCWSTR source,
285 static const WCHAR query[] = {
286 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
287 '`','B','i' ,'n','a','r','y','`',' ','W','H','E','R','E',' ',
288 '`','N','a','m','e','`',' ','=',' ','\'','%','s','\'',0};
292 static const WCHAR f1[] = {'m','s','i',0};
297 if (MSI_GetPropertyW(package, cszTempFolder, fmt, &sz) != ERROR_SUCCESS)
298 GetTempPathW(MAX_PATH, fmt);
300 if (GetTempFileNameW(fmt, f1, 0, tmp_file) == 0)
302 TRACE("Unable to create file\n");
303 return ERROR_FUNCTION_FAILED;
305 track_tempfile(package, tmp_file);
307 row = MSI_QueryGetRecord(package->db, query, source);
309 return ERROR_FUNCTION_FAILED;
311 /* write out the file */
312 file = CreateFileW(tmp_file, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
313 FILE_ATTRIBUTE_NORMAL, NULL);
314 if (file == INVALID_HANDLE_VALUE)
315 r = ERROR_FUNCTION_FAILED;
322 r = MSI_RecordReadStream(row, 2, buffer, &sz);
323 if (r != ERROR_SUCCESS)
325 ERR("Failed to get stream\n");
328 WriteFile(file, buffer, sz, &write, NULL);
329 } while (sz == sizeof buffer);
333 msiobj_release(&row->hdr);
338 static void file_running_action(MSIPACKAGE* package, HANDLE Handle,
339 BOOL process, LPCWSTR name)
341 MSIRUNNINGACTION *action;
343 action = msi_alloc( sizeof(MSIRUNNINGACTION) );
345 action->handle = Handle;
346 action->process = process;
347 action->name = strdupW(name);
349 list_add_tail( &package->RunningActions, &action->entry );
352 static UINT custom_get_process_return( HANDLE process )
356 GetExitCodeProcess( process, &rc );
358 return ERROR_FUNCTION_FAILED;
359 return ERROR_SUCCESS;
362 static UINT custom_get_thread_return( HANDLE thread )
366 GetExitCodeThread( thread, &rc );
370 case ERROR_FUNCTION_NOT_CALLED:
372 case ERROR_INSTALL_USEREXIT:
373 case ERROR_INSTALL_FAILURE:
375 case ERROR_NO_MORE_ITEMS:
376 return ERROR_SUCCESS;
378 ERR("Invalid Return Code %d\n",rc);
379 return ERROR_INSTALL_FAILURE;
383 static UINT wait_process_handle(MSIPACKAGE* package, UINT type,
384 HANDLE ProcessHandle, LPCWSTR name)
386 UINT rc = ERROR_SUCCESS;
388 if (!(type & msidbCustomActionTypeAsync))
390 TRACE("waiting for %s\n", debugstr_w(name));
392 msi_dialog_check_messages(ProcessHandle);
394 if (!(type & msidbCustomActionTypeContinue))
395 rc = custom_get_process_return(ProcessHandle);
397 CloseHandle(ProcessHandle);
401 TRACE("%s running in background\n", debugstr_w(name));
403 if (!(type & msidbCustomActionTypeContinue))
404 file_running_action(package, ProcessHandle, TRUE, name);
406 CloseHandle(ProcessHandle);
412 typedef struct _msi_custom_action_info {
421 } msi_custom_action_info;
423 static void free_custom_action_data( msi_custom_action_info *info )
425 EnterCriticalSection( &msi_custom_action_cs );
426 list_remove( &info->entry );
427 LeaveCriticalSection( &msi_custom_action_cs );
429 CloseHandle( info->handle );
430 msi_free( info->action );
431 msi_free( info->dllname );
432 msi_free( info->function );
433 msiobj_release( &info->package->hdr );
437 static UINT wait_thread_handle( msi_custom_action_info *info )
439 UINT rc = ERROR_SUCCESS;
441 if (!(info->type & msidbCustomActionTypeAsync))
443 TRACE("waiting for %s\n", debugstr_w( info->action ));
445 msi_dialog_check_messages( info->handle );
447 if (!(info->type & msidbCustomActionTypeContinue))
448 rc = custom_get_thread_return( info->handle );
450 free_custom_action_data( info );
454 TRACE("%s running in background\n", debugstr_w( info->action ));
460 static msi_custom_action_info *find_action_by_guid( const LPGUID guid )
462 msi_custom_action_info *info;
465 EnterCriticalSection( &msi_custom_action_cs );
467 LIST_FOR_EACH_ENTRY( info, &msi_pending_custom_actions, msi_custom_action_info, entry )
469 if (IsEqualGUID( &info->guid, guid ))
476 LeaveCriticalSection( &msi_custom_action_cs );
484 static DWORD WINAPI ACTION_CallDllFunction( const LPGUID guid )
486 msi_custom_action_info *info;
487 MsiCustomActionEntryPoint fn;
491 UINT r = ERROR_FUNCTION_FAILED;
493 info = find_action_by_guid( guid );
496 ERR("failed to find action %s\n", debugstr_guid( guid) );
500 TRACE("%s %s\n", debugstr_w( info->dllname ), debugstr_w( info->function ) );
502 hModule = LoadLibraryW( info->dllname );
505 ERR("failed to load dll %s\n", debugstr_w( info->dllname ) );
509 proc = strdupWtoA( info->function );
510 fn = (MsiCustomActionEntryPoint) GetProcAddress( hModule, proc );
514 hPackage = alloc_msihandle( &info->package->hdr );
517 TRACE("calling %s\n", debugstr_w( info->function ) );
519 MsiCloseHandle( hPackage );
522 ERR("failed to create handle for %p\n", info->package );
525 ERR("GetProcAddress(%s) failed\n", debugstr_w( info->function ) );
527 FreeLibrary(hModule);
529 if (info->type & msidbCustomActionTypeAsync &&
530 info->type & msidbCustomActionTypeContinue)
531 free_custom_action_data( info );
536 static DWORD WINAPI DllThread( LPVOID arg )
541 TRACE("custom action (%x) started\n", GetCurrentThreadId() );
543 rc = ACTION_CallDllFunction( guid );
545 TRACE("custom action (%x) returned %i\n", GetCurrentThreadId(), rc );
547 MsiCloseAllHandles();
551 static msi_custom_action_info *do_msidbCustomActionTypeDll(
552 MSIPACKAGE *package, INT type, LPCWSTR dllname, LPCWSTR function, LPCWSTR action )
554 msi_custom_action_info *info;
556 info = msi_alloc( sizeof *info );
560 msiobj_addref( &package->hdr );
561 info->package = package;
563 info->function = strdupW( function );
564 info->dllname = strdupW( dllname );
565 info->action = strdupW( action );
566 CoCreateGuid( &info->guid );
568 EnterCriticalSection( &msi_custom_action_cs );
569 list_add_tail( &msi_pending_custom_actions, &info->entry );
570 LeaveCriticalSection( &msi_custom_action_cs );
572 info->handle = CreateThread( NULL, 0, DllThread, &info->guid, 0, NULL );
575 free_custom_action_data( info );
582 static UINT HANDLE_CustomType1(MSIPACKAGE *package, LPCWSTR source,
583 LPCWSTR target, const INT type, LPCWSTR action)
585 msi_custom_action_info *info;
586 WCHAR tmp_file[MAX_PATH];
589 r = store_binary_to_temp(package, source, tmp_file);
590 if (r != ERROR_SUCCESS)
593 TRACE("Calling function %s from %s\n",debugstr_w(target),
594 debugstr_w(tmp_file));
596 if (!strchrW(tmp_file,'.'))
598 static const WCHAR dot[]={'.',0};
599 strcatW(tmp_file,dot);
602 info = do_msidbCustomActionTypeDll( package, type, tmp_file, target, action );
604 return wait_thread_handle( info );
607 static UINT HANDLE_CustomType2(MSIPACKAGE *package, LPCWSTR source,
608 LPCWSTR target, const INT type, LPCWSTR action)
610 WCHAR tmp_file[MAX_PATH];
612 PROCESS_INFORMATION info;
615 WCHAR *deformated = NULL;
617 static const WCHAR spc[] = {' ',0};
620 memset(&si,0,sizeof(STARTUPINFOW));
622 r = store_binary_to_temp(package, source, tmp_file);
623 if (r != ERROR_SUCCESS)
626 deformat_string(package,target,&deformated);
628 len = strlenW(tmp_file)+2;
631 len += strlenW(deformated);
633 cmd = msi_alloc(sizeof(WCHAR)*len);
635 strcpyW(cmd,tmp_file);
639 strcatW(cmd,deformated);
641 msi_free(deformated);
644 TRACE("executing exe %s\n", debugstr_w(cmd));
646 rc = CreateProcessW(NULL, cmd, NULL, NULL, FALSE, 0, NULL,
647 c_collen, &si, &info);
652 ERR("Unable to execute command %s\n", debugstr_w(cmd));
653 return ERROR_SUCCESS;
655 CloseHandle( info.hThread );
657 r = wait_process_handle(package, type, info.hProcess, action);
662 static UINT HANDLE_CustomType17(MSIPACKAGE *package, LPCWSTR source,
663 LPCWSTR target, const INT type, LPCWSTR action)
665 msi_custom_action_info *info;
668 TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
670 file = get_loaded_file( package, source );
673 ERR("invalid file key %s\n", debugstr_w( source ));
674 return ERROR_FUNCTION_FAILED;
677 info = do_msidbCustomActionTypeDll( package, type, file->TargetPath, target, action );
679 return wait_thread_handle( info );
682 static UINT HANDLE_CustomType18(MSIPACKAGE *package, LPCWSTR source,
683 LPCWSTR target, const INT type, LPCWSTR action)
686 PROCESS_INFORMATION info;
691 static const WCHAR spc[] = {' ',0};
694 memset(&si,0,sizeof(STARTUPINFOW));
696 file = get_loaded_file(package,source);
698 return ERROR_FUNCTION_FAILED;
700 len = lstrlenW( file->TargetPath );
702 deformat_string(package,target,&deformated);
704 len += strlenW(deformated);
707 cmd = msi_alloc(len * sizeof(WCHAR));
709 lstrcpyW( cmd, file->TargetPath);
713 strcatW(cmd, deformated);
715 msi_free(deformated);
718 TRACE("executing exe %s\n", debugstr_w(cmd));
720 rc = CreateProcessW(NULL, cmd, NULL, NULL, FALSE, 0, NULL,
721 c_collen, &si, &info);
725 ERR("Unable to execute command %s\n", debugstr_w(cmd));
727 return ERROR_SUCCESS;
730 CloseHandle( info.hThread );
732 return wait_process_handle(package, type, info.hProcess, action);
735 static UINT HANDLE_CustomType19(MSIPACKAGE *package, LPCWSTR source,
736 LPCWSTR target, const INT type, LPCWSTR action)
738 static const WCHAR query[] = {
739 'S','E','L','E','C','T',' ','`','M','e','s','s','a','g','e','`',' ',
740 'F','R','O','M',' ','`','E','r','r','o','r','`',' ',
741 'W','H','E','R','E',' ','`','E','r','r','o','r','`',' ','=',' ',
745 LPWSTR deformated = NULL;
747 deformat_string( package, target, &deformated );
749 /* first try treat the error as a number */
750 row = MSI_QueryGetRecord( package->db, query, deformated );
753 LPCWSTR error = MSI_RecordGetString( row, 1 );
754 MessageBoxW( NULL, error, NULL, MB_OK );
755 msiobj_release( &row->hdr );
758 MessageBoxW( NULL, deformated, NULL, MB_OK );
760 msi_free( deformated );
762 return ERROR_FUNCTION_FAILED;
765 static UINT HANDLE_CustomType50(MSIPACKAGE *package, LPCWSTR source,
766 LPCWSTR target, const INT type, LPCWSTR action)
769 PROCESS_INFORMATION info;
775 static const WCHAR spc[] = {' ',0};
777 memset(&si,0,sizeof(STARTUPINFOW));
778 memset(&info,0,sizeof(PROCESS_INFORMATION));
780 prop = msi_dup_property( package, source );
782 return ERROR_SUCCESS;
784 deformat_string(package,target,&deformated);
785 len = strlenW(prop) + 2;
787 len += strlenW(deformated);
789 cmd = msi_alloc(sizeof(WCHAR)*len);
795 strcatW(cmd,deformated);
797 msi_free(deformated);
801 TRACE("executing exe %s\n", debugstr_w(cmd));
803 rc = CreateProcessW(NULL, cmd, NULL, NULL, FALSE, 0, NULL,
804 c_collen, &si, &info);
808 ERR("Unable to execute command %s\n", debugstr_w(cmd));
810 return ERROR_SUCCESS;
814 CloseHandle( info.hThread );
816 return wait_process_handle(package, type, info.hProcess, action);
819 static UINT HANDLE_CustomType34(MSIPACKAGE *package, LPCWSTR source,
820 LPCWSTR target, const INT type, LPCWSTR action)
822 LPWSTR filename, deformated;
824 PROCESS_INFORMATION info;
827 memset(&si,0,sizeof(STARTUPINFOW));
829 filename = resolve_folder(package, source, FALSE, FALSE, NULL);
832 return ERROR_FUNCTION_FAILED;
834 SetCurrentDirectoryW(filename);
837 deformat_string(package,target,&deformated);
840 return ERROR_FUNCTION_FAILED;
842 TRACE("executing exe %s\n", debugstr_w(deformated));
844 rc = CreateProcessW(NULL, deformated, NULL, NULL, FALSE, 0, NULL,
845 c_collen, &si, &info);
849 ERR("Unable to execute command %s\n", debugstr_w(deformated));
850 msi_free(deformated);
851 return ERROR_SUCCESS;
853 msi_free(deformated);
854 CloseHandle( info.hThread );
856 return wait_process_handle(package, type, info.hProcess, action);
859 void ACTION_FinishCustomActions(MSIPACKAGE* package)
862 HANDLE *wait_handles;
863 unsigned int handle_count, i;
864 msi_custom_action_info *info, *cursor;
866 while ((item = list_head( &package->RunningActions )))
868 MSIRUNNINGACTION *action = LIST_ENTRY( item, MSIRUNNINGACTION, entry );
870 list_remove( &action->entry );
872 TRACE("waiting for %s\n", debugstr_w( action->name ) );
873 msi_dialog_check_messages( action->handle );
875 CloseHandle( action->handle );
876 msi_free( action->name );
880 EnterCriticalSection( &msi_custom_action_cs );
882 handle_count = list_count( &msi_pending_custom_actions );
883 wait_handles = HeapAlloc( GetProcessHeap(), 0, handle_count * sizeof(HANDLE) );
886 LIST_FOR_EACH_ENTRY_SAFE( info, cursor, &msi_pending_custom_actions, msi_custom_action_info, entry )
888 if (info->package == package )
890 wait_handles[handle_count++] = info->handle;
891 free_custom_action_data( info );
895 LeaveCriticalSection( &msi_custom_action_cs );
897 for (i = 0; i < handle_count; i++)
898 msi_dialog_check_messages( wait_handles[i] );
900 HeapFree( GetProcessHeap(), 0, wait_handles );