netstat: Initial implementation.
[wine] / dlls / msi / custom.c
1 /*
2  * Custom Action processing for the Microsoft Installer (msi.dll)
3  *
4  * Copyright 2005 Aric Stewart for CodeWeavers
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #define COBJMACROS
25
26 #include <stdarg.h>
27 #include "windef.h"
28 #include "winbase.h"
29 #include "winerror.h"
30 #include "msidefs.h"
31 #include "winuser.h"
32 #include "objbase.h"
33 #include "oleauto.h"
34
35 #include "msipriv.h"
36 #include "msiserver.h"
37 #include "wine/debug.h"
38 #include "wine/unicode.h"
39 #include "wine/exception.h"
40
41 WINE_DEFAULT_DEBUG_CHANNEL(msi);
42
43 #define CUSTOM_ACTION_TYPE_MASK 0x3F
44
45 typedef struct tagMSIRUNNINGACTION
46 {
47     struct list entry;
48     HANDLE handle;
49     BOOL   process;
50     LPWSTR name;
51 } MSIRUNNINGACTION;
52
53 typedef UINT (WINAPI *MsiCustomActionEntryPoint)( MSIHANDLE );
54
55 static CRITICAL_SECTION msi_custom_action_cs;
56 static CRITICAL_SECTION_DEBUG msi_custom_action_cs_debug =
57 {
58     0, 0, &msi_custom_action_cs,
59     { &msi_custom_action_cs_debug.ProcessLocksList,
60       &msi_custom_action_cs_debug.ProcessLocksList },
61       0, 0, { (DWORD_PTR)(__FILE__ ": msi_custom_action_cs") }
62 };
63 static CRITICAL_SECTION msi_custom_action_cs = { &msi_custom_action_cs_debug, -1, 0, 0, 0, 0 };
64
65 static struct list msi_pending_custom_actions = LIST_INIT( msi_pending_custom_actions );
66
67 UINT msi_schedule_action( MSIPACKAGE *package, UINT script, const WCHAR *action )
68 {
69     UINT count;
70     WCHAR **newbuf = NULL;
71
72     if (script >= SCRIPT_MAX)
73     {
74         FIXME("Unknown script requested %u\n", script);
75         return ERROR_FUNCTION_FAILED;
76     }
77     TRACE("Scheduling action %s in script %u\n", debugstr_w(action), script);
78
79     count = package->script->ActionCount[script];
80     package->script->ActionCount[script]++;
81     if (count != 0) newbuf = msi_realloc( package->script->Actions[script],
82                                           package->script->ActionCount[script] * sizeof(WCHAR *) );
83     else newbuf = msi_alloc( sizeof(WCHAR *) );
84
85     newbuf[count] = strdupW( action );
86     package->script->Actions[script] = newbuf;
87     return ERROR_SUCCESS;
88 }
89
90 UINT msi_register_unique_action( MSIPACKAGE *package, const WCHAR *action )
91 {
92     UINT count;
93     WCHAR **newbuf = NULL;
94
95     if (!package->script) return FALSE;
96
97     TRACE("Registering %s as unique action\n", debugstr_w(action));
98
99     count = package->script->UniqueActionsCount;
100     package->script->UniqueActionsCount++;
101     if (count != 0) newbuf = msi_realloc( package->script->UniqueActions,
102                                           package->script->UniqueActionsCount * sizeof(WCHAR *) );
103     else newbuf = msi_alloc( sizeof(WCHAR *) );
104
105     newbuf[count] = strdupW( action );
106     package->script->UniqueActions = newbuf;
107     return ERROR_SUCCESS;
108 }
109
110 BOOL msi_action_is_unique( const MSIPACKAGE *package, const WCHAR *action )
111 {
112     UINT i;
113
114     if (!package->script) return FALSE;
115
116     for (i = 0; i < package->script->UniqueActionsCount; i++)
117     {
118         if (!strcmpW( package->script->UniqueActions[i], action )) return TRUE;
119     }
120     return FALSE;
121 }
122
123 static BOOL check_execution_scheduling_options(MSIPACKAGE *package, LPCWSTR action, UINT options)
124 {
125     if (!package->script)
126         return TRUE;
127
128     if ((options & msidbCustomActionTypeClientRepeat) ==
129             msidbCustomActionTypeClientRepeat)
130     {
131         if (!(package->script->InWhatSequence & SEQUENCE_UI &&
132             package->script->InWhatSequence & SEQUENCE_EXEC))
133         {
134             TRACE("Skipping action due to dbCustomActionTypeClientRepeat option.\n");
135             return FALSE;
136         }
137     }
138     else if (options & msidbCustomActionTypeFirstSequence)
139     {
140         if (package->script->InWhatSequence & SEQUENCE_UI &&
141             package->script->InWhatSequence & SEQUENCE_EXEC )
142         {
143             TRACE("Skipping action due to msidbCustomActionTypeFirstSequence option.\n");
144             return FALSE;
145         }
146     }
147     else if (options & msidbCustomActionTypeOncePerProcess)
148     {
149         if (msi_action_is_unique(package, action))
150         {
151             TRACE("Skipping action due to msidbCustomActionTypeOncePerProcess option.\n");
152             return FALSE;
153         }
154         else
155             msi_register_unique_action(package, action);
156     }
157
158     return TRUE;
159 }
160
161 /* stores the following properties before the action:
162  *
163  *    [CustomActionData<=>UserSID<=>ProductCode]Action
164  */
165 static LPWSTR msi_get_deferred_action(LPCWSTR action, LPCWSTR actiondata,
166                                       LPCWSTR usersid, LPCWSTR prodcode)
167 {
168     LPWSTR deferred;
169     DWORD len;
170
171     static const WCHAR format[] = {
172             '[','%','s','<','=','>','%','s','<','=','>','%','s',']','%','s',0
173     };
174
175     if (!actiondata)
176         return strdupW(action);
177
178     len = lstrlenW(action) + lstrlenW(actiondata) +
179           lstrlenW(usersid) + lstrlenW(prodcode) +
180           lstrlenW(format) - 7;
181     deferred = msi_alloc(len * sizeof(WCHAR));
182
183     sprintfW(deferred, format, actiondata, usersid, prodcode, action);
184     return deferred;
185 }
186
187 static void set_deferred_action_props( MSIPACKAGE *package, const WCHAR *deferred_data )
188 {
189     static const WCHAR sep[] = {'<','=','>',0};
190     const WCHAR *end, *beg = deferred_data + 1;
191
192     end = strstrW(beg, sep);
193     msi_set_property( package->db, szCustomActionData, beg, end - beg );
194     beg = end + 3;
195
196     end = strstrW(beg, sep);
197     msi_set_property( package->db, szUserSID, beg, end - beg );
198     beg = end + 3;
199
200     end = strchrW(beg, ']');
201     msi_set_property( package->db, szProductCode, beg, end - beg );
202 }
203
204 static MSIBINARY *create_temp_binary( MSIPACKAGE *package, LPCWSTR source, BOOL dll )
205 {
206     static const WCHAR query[] = {
207         'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
208         '`','B','i' ,'n','a','r','y','`',' ','W','H','E','R','E',' ',
209         '`','N','a','m','e','`',' ','=',' ','\'','%','s','\'',0};
210     MSIRECORD *row;
211     MSIBINARY *binary;
212     HANDLE file;
213     CHAR buffer[1024];
214     WCHAR fmt[MAX_PATH], tmpfile[MAX_PATH];
215     DWORD sz = MAX_PATH, write;
216     UINT r;
217
218     if (msi_get_property(package->db, szTempFolder, fmt, &sz) != ERROR_SUCCESS)
219         GetTempPathW(MAX_PATH, fmt);
220
221     if (!GetTempFileNameW( fmt, szMsi, 0, tmpfile ))
222     {
223         TRACE("unable to create temp file %s (%u)\n", debugstr_w(tmpfile), GetLastError());
224         return NULL;
225     }
226
227     row = MSI_QueryGetRecord(package->db, query, source);
228     if (!row)
229         return NULL;
230
231     if (!(binary = msi_alloc_zero( sizeof(MSIBINARY) )))
232     {
233         msiobj_release( &row->hdr );
234         return NULL;
235     }
236     file = CreateFileW( tmpfile, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL );
237     if (file == INVALID_HANDLE_VALUE)
238     {
239         msiobj_release( &row->hdr );
240         msi_free( binary );
241         return NULL;
242     }
243     do
244     {
245         sz = sizeof(buffer);
246         r = MSI_RecordReadStream( row, 2, buffer, &sz );
247         if (r != ERROR_SUCCESS)
248         {
249             ERR("Failed to get stream\n");
250             break;
251         }
252         WriteFile( file, buffer, sz, &write, NULL );
253     } while (sz == sizeof buffer);
254
255     CloseHandle( file );
256     msiobj_release( &row->hdr );
257     if (r != ERROR_SUCCESS)
258     {
259         DeleteFileW( tmpfile );
260         msi_free( binary );
261         return NULL;
262     }
263
264     /* keep a reference to prevent the dll from being unloaded */
265     if (dll && !(binary->module = LoadLibraryW( tmpfile )))
266     {
267         WARN( "failed to load dll %s (%u)\n", debugstr_w( tmpfile ), GetLastError() );
268     }
269     binary->source = strdupW( source );
270     binary->tmpfile = strdupW( tmpfile );
271     list_add_tail( &package->binaries, &binary->entry );
272     return binary;
273 }
274
275 static MSIBINARY *get_temp_binary( MSIPACKAGE *package, LPCWSTR source, BOOL dll )
276 {
277     MSIBINARY *binary;
278
279     LIST_FOR_EACH_ENTRY( binary, &package->binaries, MSIBINARY, entry )
280     {
281         if (!strcmpW( binary->source, source ))
282             return binary;
283     }
284
285     return create_temp_binary( package, source, dll );
286 }
287
288 static void file_running_action(MSIPACKAGE* package, HANDLE Handle,
289                                 BOOL process, LPCWSTR name)
290 {
291     MSIRUNNINGACTION *action;
292
293     action = msi_alloc( sizeof(MSIRUNNINGACTION) );
294
295     action->handle = Handle;
296     action->process = process;
297     action->name = strdupW(name);
298
299     list_add_tail( &package->RunningActions, &action->entry );
300 }
301
302 static UINT custom_get_process_return( HANDLE process )
303 {
304     DWORD rc = 0;
305
306     GetExitCodeProcess( process, &rc );
307     TRACE("exit code is %u\n", rc);
308     if (rc != 0)
309         return ERROR_FUNCTION_FAILED;
310     return ERROR_SUCCESS;
311 }
312
313 static UINT custom_get_thread_return( MSIPACKAGE *package, HANDLE thread )
314 {
315     DWORD rc = 0;
316
317     GetExitCodeThread( thread, &rc );
318
319     switch (rc)
320     {
321     case ERROR_FUNCTION_NOT_CALLED:
322     case ERROR_SUCCESS:
323     case ERROR_INSTALL_USEREXIT:
324     case ERROR_INSTALL_FAILURE:
325         return rc;
326     case ERROR_NO_MORE_ITEMS:
327         return ERROR_SUCCESS;
328     case ERROR_INSTALL_SUSPEND:
329         ACTION_ForceReboot( package );
330         return ERROR_SUCCESS;
331     default:
332         ERR("Invalid Return Code %d\n",rc);
333         return ERROR_INSTALL_FAILURE;
334     }
335 }
336
337 static UINT wait_process_handle(MSIPACKAGE* package, UINT type,
338                            HANDLE ProcessHandle, LPCWSTR name)
339 {
340     UINT rc = ERROR_SUCCESS;
341
342     if (!(type & msidbCustomActionTypeAsync))
343     {
344         TRACE("waiting for %s\n", debugstr_w(name));
345
346         msi_dialog_check_messages(ProcessHandle);
347
348         if (!(type & msidbCustomActionTypeContinue))
349             rc = custom_get_process_return(ProcessHandle);
350
351         CloseHandle(ProcessHandle);
352     }
353     else
354     {
355         TRACE("%s running in background\n", debugstr_w(name));
356
357         if (!(type & msidbCustomActionTypeContinue))
358             file_running_action(package, ProcessHandle, TRUE, name);
359         else
360             CloseHandle(ProcessHandle);
361     }
362
363     return rc;
364 }
365
366 typedef struct _msi_custom_action_info {
367     struct list entry;
368     LONG refs;
369     MSIPACKAGE *package;
370     LPWSTR source;
371     LPWSTR target;
372     HANDLE handle;
373     LPWSTR action;
374     INT type;
375     GUID guid;
376 } msi_custom_action_info;
377
378 static void release_custom_action_data( msi_custom_action_info *info )
379 {
380     EnterCriticalSection( &msi_custom_action_cs );
381
382     if (!--info->refs)
383     {
384         list_remove( &info->entry );
385         if (info->handle)
386             CloseHandle( info->handle );
387         msi_free( info->action );
388         msi_free( info->source );
389         msi_free( info->target );
390         msiobj_release( &info->package->hdr );
391         msi_free( info );
392     }
393
394     LeaveCriticalSection( &msi_custom_action_cs );
395 }
396
397 /* must be called inside msi_custom_action_cs if info is in the pending custom actions list */
398 static void addref_custom_action_data( msi_custom_action_info *info )
399 {
400     info->refs++;
401  }
402
403 static UINT wait_thread_handle( msi_custom_action_info *info )
404 {
405     UINT rc = ERROR_SUCCESS;
406
407     if (!(info->type & msidbCustomActionTypeAsync))
408     {
409         TRACE("waiting for %s\n", debugstr_w( info->action ));
410
411         msi_dialog_check_messages( info->handle );
412
413         if (!(info->type & msidbCustomActionTypeContinue))
414             rc = custom_get_thread_return( info->package, info->handle );
415
416         release_custom_action_data( info );
417     }
418     else
419     {
420         TRACE("%s running in background\n", debugstr_w( info->action ));
421     }
422
423     return rc;
424 }
425
426 static msi_custom_action_info *find_action_by_guid( const GUID *guid )
427 {
428     msi_custom_action_info *info;
429     BOOL found = FALSE;
430
431     EnterCriticalSection( &msi_custom_action_cs );
432
433     LIST_FOR_EACH_ENTRY( info, &msi_pending_custom_actions, msi_custom_action_info, entry )
434     {
435         if (IsEqualGUID( &info->guid, guid ))
436         {
437             addref_custom_action_data( info );
438             found = TRUE;
439             break;
440         }
441     }
442
443     LeaveCriticalSection( &msi_custom_action_cs );
444
445     if (!found)
446         return NULL;
447
448     return info;
449 }
450
451 static void handle_msi_break( LPCWSTR target )
452 {
453     LPWSTR msg;
454     WCHAR val[MAX_PATH];
455
456     static const WCHAR MsiBreak[] = { 'M','s','i','B','r','e','a','k',0 };
457     static const WCHAR WindowsInstaller[] = {
458         'W','i','n','d','o','w','s',' ','I','n','s','t','a','l','l','e','r',0
459     };
460
461     static const WCHAR format[] = {
462         'T','o',' ','d','e','b','u','g',' ','y','o','u','r',' ',
463         'c','u','s','t','o','m',' ','a','c','t','i','o','n',',',' ',
464         'a','t','t','a','c','h',' ','y','o','u','r',' ','d','e','b','u','g','g','e','r',' ',
465         't','o',' ','p','r','o','c','e','s','s',' ','%','i',' ','(','0','x','%','X',')',' ',
466         'a','n','d',' ','p','r','e','s','s',' ','O','K',0
467     };
468
469     if( !GetEnvironmentVariableW( MsiBreak, val, MAX_PATH ))
470         return;
471
472     if( strcmpiW( val, target ))
473         return;
474
475     msg = msi_alloc( (lstrlenW(format) + 10) * sizeof(WCHAR) );
476     if (!msg)
477         return;
478
479     wsprintfW( msg, format, GetCurrentProcessId(), GetCurrentProcessId());
480     MessageBoxW( NULL, msg, WindowsInstaller, MB_OK);
481     msi_free(msg);
482     DebugBreak();
483 }
484
485 static UINT get_action_info( const GUID *guid, INT *type, MSIHANDLE *handle,
486                              BSTR *dll, BSTR *funcname,
487                              IWineMsiRemotePackage **package )
488 {
489     IClassFactory *cf = NULL;
490     IWineMsiRemoteCustomAction *rca = NULL;
491     HRESULT r;
492
493     r = DllGetClassObject( &CLSID_WineMsiRemoteCustomAction,
494                            &IID_IClassFactory, (LPVOID *)&cf );
495     if (FAILED(r))
496     {
497         ERR("failed to get IClassFactory interface\n");
498         return ERROR_FUNCTION_FAILED;
499     }
500
501     r = IClassFactory_CreateInstance( cf, NULL, &IID_IWineMsiRemoteCustomAction, (LPVOID *)&rca );
502     if (FAILED(r))
503     {
504         ERR("failed to get IWineMsiRemoteCustomAction interface\n");
505         return ERROR_FUNCTION_FAILED;
506     }
507
508     r = IWineMsiRemoteCustomAction_GetActionInfo( rca, guid, type, handle, dll, funcname, package );
509     IWineMsiRemoteCustomAction_Release( rca );
510     if (FAILED(r))
511     {
512         ERR("GetActionInfo failed\n");
513         return ERROR_FUNCTION_FAILED;
514     }
515
516     return ERROR_SUCCESS;
517 }
518
519 #ifdef __i386__
520 extern UINT CUSTOMPROC_wrapper( MsiCustomActionEntryPoint proc, MSIHANDLE handle );
521 __ASM_GLOBAL_FUNC( CUSTOMPROC_wrapper,
522     "pushl %ebp\n\t"
523     __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
524     __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
525     "movl %esp,%ebp\n\t"
526     __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
527     "pushl 12(%ebp)\n\t"
528     "movl 8(%ebp),%eax\n\t"
529     "call *%eax\n\t"
530     "leave\n\t"
531     __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
532     __ASM_CFI(".cfi_same_value %ebp\n\t")
533     "ret" )
534 #else
535 static inline UINT CUSTOMPROC_wrapper( MsiCustomActionEntryPoint proc, MSIHANDLE handle )
536 {
537     return proc(handle);
538 }
539 #endif
540
541 static DWORD ACTION_CallDllFunction( const GUID *guid )
542 {
543     MsiCustomActionEntryPoint fn;
544     MSIHANDLE hPackage, handle;
545     HANDLE hModule;
546     LPSTR proc;
547     UINT r = ERROR_FUNCTION_FAILED;
548     BSTR dll = NULL, function = NULL;
549     INT type;
550     IWineMsiRemotePackage *remote_package = NULL;
551
552     TRACE("%s\n", debugstr_guid( guid ));
553
554     r = get_action_info( guid, &type, &handle, &dll, &function, &remote_package );
555     if (r != ERROR_SUCCESS)
556         return r;
557
558     hModule = LoadLibraryW( dll );
559     if (!hModule)
560     {
561         WARN( "failed to load dll %s (%u)\n", debugstr_w( dll ), GetLastError() );
562         return ERROR_SUCCESS;
563     }
564
565     proc = strdupWtoA( function );
566     fn = (MsiCustomActionEntryPoint) GetProcAddress( hModule, proc );
567     msi_free( proc );
568     if (fn)
569     {
570         hPackage = alloc_msi_remote_handle( (IUnknown *)remote_package );
571         if (hPackage)
572         {
573             IWineMsiRemotePackage_SetMsiHandle( remote_package, handle );
574             TRACE("calling %s\n", debugstr_w( function ) );
575             handle_msi_break( function );
576
577             __TRY
578             {
579                 r = CUSTOMPROC_wrapper( fn, hPackage );
580             }
581             __EXCEPT_PAGE_FAULT
582             {
583                 ERR("Custom action (%s:%s) caused a page fault: %08x\n",
584                     debugstr_w(dll), debugstr_w(function), GetExceptionCode());
585                 r = ERROR_SUCCESS;
586             }
587             __ENDTRY;
588
589             MsiCloseHandle( hPackage );
590         }
591         else
592             ERR("failed to create handle for %p\n", remote_package );
593     }
594     else
595         ERR("GetProcAddress(%s) failed\n", debugstr_w( function ) );
596
597     FreeLibrary(hModule);
598
599     IWineMsiRemotePackage_Release( remote_package );
600     SysFreeString( dll );
601     SysFreeString( function );
602     MsiCloseHandle( handle );
603
604     return r;
605 }
606
607 static DWORD WINAPI DllThread( LPVOID arg )
608 {
609     LPGUID guid = arg;
610     DWORD rc = 0;
611
612     TRACE("custom action (%x) started\n", GetCurrentThreadId() );
613
614     rc = ACTION_CallDllFunction( guid );
615
616     TRACE("custom action (%x) returned %i\n", GetCurrentThreadId(), rc );
617
618     MsiCloseAllHandles();
619     return rc;
620 }
621
622 static DWORD ACTION_CAInstallPackage(const GUID *guid)
623 {
624     msi_custom_action_info *info;
625     UINT r = ERROR_FUNCTION_FAILED;
626     INSTALLUILEVEL old_level;
627
628     info = find_action_by_guid(guid);
629     if (!info)
630     {
631         ERR("failed to find action %s\n", debugstr_guid(guid));
632         return r;
633     }
634
635     old_level = MsiSetInternalUI(INSTALLUILEVEL_BASIC, NULL);
636     r = MsiInstallProductW(info->source, info->target);
637     MsiSetInternalUI(old_level, NULL);
638
639     release_custom_action_data(info);
640
641     return r;
642 }
643
644 static DWORD WINAPI ConcurrentInstallThread(LPVOID arg)
645 {
646     LPGUID guid = arg;
647     DWORD rc;
648
649     TRACE("concurrent installation (%x) started\n", GetCurrentThreadId());
650
651     rc = ACTION_CAInstallPackage(guid);
652
653     TRACE("concurrent installation (%x) returned %i\n", GetCurrentThreadId(), rc);
654
655     MsiCloseAllHandles();
656     return rc;
657 }
658
659 static msi_custom_action_info *do_msidbCustomActionTypeDll(
660     MSIPACKAGE *package, INT type, LPCWSTR source, LPCWSTR target, LPCWSTR action )
661 {
662     msi_custom_action_info *info;
663
664     info = msi_alloc( sizeof *info );
665     if (!info)
666         return NULL;
667
668     msiobj_addref( &package->hdr );
669     info->refs = 2; /* 1 for our caller and 1 for thread we created */
670     info->package = package;
671     info->type = type;
672     info->target = strdupW( target );
673     info->source = strdupW( source );
674     info->action = strdupW( action );
675     CoCreateGuid( &info->guid );
676
677     EnterCriticalSection( &msi_custom_action_cs );
678     list_add_tail( &msi_pending_custom_actions, &info->entry );
679     LeaveCriticalSection( &msi_custom_action_cs );
680
681     info->handle = CreateThread( NULL, 0, DllThread, &info->guid, 0, NULL );
682     if (!info->handle)
683     {
684         /* release both references */
685         release_custom_action_data( info );
686         release_custom_action_data( info );
687         return NULL;
688     }
689
690     return info;
691 }
692
693 static msi_custom_action_info *do_msidbCAConcurrentInstall(
694     MSIPACKAGE *package, INT type, LPCWSTR source, LPCWSTR target, LPCWSTR action)
695 {
696     msi_custom_action_info *info;
697
698     info = msi_alloc( sizeof *info );
699     if (!info)
700         return NULL;
701
702     msiobj_addref( &package->hdr );
703     info->refs = 2; /* 1 for our caller and 1 for thread we created */
704     info->package = package;
705     info->type = type;
706     info->target = strdupW( target );
707     info->source = strdupW( source );
708     info->action = strdupW( action );
709     CoCreateGuid( &info->guid );
710
711     EnterCriticalSection( &msi_custom_action_cs );
712     list_add_tail( &msi_pending_custom_actions, &info->entry );
713     LeaveCriticalSection( &msi_custom_action_cs );
714
715     info->handle = CreateThread( NULL, 0, ConcurrentInstallThread, &info->guid, 0, NULL );
716     if (!info->handle)
717     {
718         /* release both references */
719         release_custom_action_data( info );
720         release_custom_action_data( info );
721         return NULL;
722     }
723
724     return info;
725 }
726
727 static UINT HANDLE_CustomType23(MSIPACKAGE *package, LPCWSTR source,
728                                 LPCWSTR target, const INT type, LPCWSTR action)
729 {
730     msi_custom_action_info *info;
731     WCHAR package_path[MAX_PATH];
732     DWORD size;
733
734     size = MAX_PATH;
735     msi_get_property(package->db, szSourceDir, package_path, &size);
736     lstrcatW(package_path, szBackSlash);
737     lstrcatW(package_path, source);
738
739     TRACE("Installing package %s concurrently\n", debugstr_w(package_path));
740
741     info = do_msidbCAConcurrentInstall(package, type, package_path, target, action);
742     return wait_thread_handle(info);
743 }
744
745 static UINT HANDLE_CustomType1(MSIPACKAGE *package, LPCWSTR source,
746                                LPCWSTR target, const INT type, LPCWSTR action)
747 {
748     msi_custom_action_info *info;
749     MSIBINARY *binary;
750
751     if (!(binary = get_temp_binary( package, source, TRUE )))
752         return ERROR_FUNCTION_FAILED;
753
754     TRACE("Calling function %s from %s\n", debugstr_w(target), debugstr_w(binary->tmpfile));
755
756     info = do_msidbCustomActionTypeDll( package, type, binary->tmpfile, target, action );
757     return wait_thread_handle( info );
758 }
759
760 static HANDLE execute_command( const WCHAR *app, WCHAR *arg, const WCHAR *dir )
761 {
762     static const WCHAR dotexeW[] = {'.','e','x','e',0};
763     STARTUPINFOW si;
764     PROCESS_INFORMATION info;
765     WCHAR *exe = NULL, *cmd = NULL, *p;
766     BOOL ret;
767
768     if (app)
769     {
770         int len_arg = 0;
771         DWORD len_exe;
772
773         if (!(exe = msi_alloc( MAX_PATH * sizeof(WCHAR) ))) return INVALID_HANDLE_VALUE;
774         len_exe = SearchPathW( NULL, app, dotexeW, MAX_PATH, exe, NULL );
775         if (len_exe >= MAX_PATH)
776         {
777             msi_free( exe );
778             if (!(exe = msi_alloc( len_exe * sizeof(WCHAR) ))) return INVALID_HANDLE_VALUE;
779             len_exe = SearchPathW( NULL, app, dotexeW, len_exe, exe, NULL );
780         }
781         if (!len_exe)
782         {
783             WARN("can't find executable %u\n", GetLastError());
784             msi_free( exe );
785             return INVALID_HANDLE_VALUE;
786         }
787
788         if (arg) len_arg = strlenW( arg );
789         if (!(cmd = msi_alloc( (len_exe + len_arg + 4) * sizeof(WCHAR) )))
790         {
791             msi_free( exe );
792             return INVALID_HANDLE_VALUE;
793         }
794         p = cmd;
795         if (strchrW( exe, ' ' ))
796         {
797             *p++ = '\"';
798             memcpy( p, exe, len_exe * sizeof(WCHAR) );
799             p += len_exe;
800             *p++ = '\"';
801             *p = 0;
802         }
803         else
804         {
805             strcpyW( p, exe );
806             p += len_exe;
807         }
808         if (arg)
809         {
810             *p++ = ' ';
811             memcpy( p, arg, len_arg * sizeof(WCHAR) );
812             p[len_arg] = 0;
813         }
814     }
815     memset( &si, 0, sizeof(STARTUPINFOW) );
816     ret = CreateProcessW( exe, exe ? cmd : arg, NULL, NULL, FALSE, 0, NULL, dir, &si, &info );
817     msi_free( cmd );
818     msi_free( exe );
819     if (!ret)
820     {
821         WARN("unable to execute command %u\n", GetLastError());
822         return INVALID_HANDLE_VALUE;
823     }
824     CloseHandle( info.hThread );
825     return info.hProcess;
826 }
827
828 static UINT HANDLE_CustomType2(MSIPACKAGE *package, LPCWSTR source,
829                                LPCWSTR target, const INT type, LPCWSTR action)
830 {
831     MSIBINARY *binary;
832     HANDLE handle;
833     WCHAR *arg;
834
835     if (!(binary = get_temp_binary( package, source, FALSE ))) return ERROR_FUNCTION_FAILED;
836
837     deformat_string( package, target, &arg );
838     TRACE("exe %s arg %s\n", debugstr_w(binary->tmpfile), debugstr_w(arg));
839
840     handle = execute_command( binary->tmpfile, arg, szCRoot );
841     msi_free( arg );
842     if (handle == INVALID_HANDLE_VALUE) return ERROR_SUCCESS;
843     return wait_process_handle( package, type, handle, action );
844 }
845
846 static UINT HANDLE_CustomType17(MSIPACKAGE *package, LPCWSTR source,
847                                 LPCWSTR target, const INT type, LPCWSTR action)
848 {
849     msi_custom_action_info *info;
850     MSIFILE *file;
851
852     TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
853
854     file = msi_get_loaded_file( package, source );
855     if (!file)
856     {
857         ERR("invalid file key %s\n", debugstr_w( source ));
858         return ERROR_FUNCTION_FAILED;
859     }
860
861     info = do_msidbCustomActionTypeDll( package, type, file->TargetPath, target, action );
862     return wait_thread_handle( info );
863 }
864
865 static UINT HANDLE_CustomType18(MSIPACKAGE *package, LPCWSTR source,
866                                 LPCWSTR target, const INT type, LPCWSTR action)
867 {
868     MSIFILE *file;
869     HANDLE handle;
870     WCHAR *arg;
871
872     if (!(file = msi_get_loaded_file( package, source ))) return ERROR_FUNCTION_FAILED;
873
874     deformat_string( package, target, &arg );
875     TRACE("exe %s arg %s\n", debugstr_w(file->TargetPath), debugstr_w(arg));
876
877     handle = execute_command( file->TargetPath, arg, szCRoot );
878     msi_free( arg );
879     if (handle == INVALID_HANDLE_VALUE) return ERROR_SUCCESS;
880     return wait_process_handle( package, type, handle, action );
881 }
882
883 static UINT HANDLE_CustomType19(MSIPACKAGE *package, LPCWSTR source,
884                                 LPCWSTR target, const INT type, LPCWSTR action)
885 {
886     static const WCHAR query[] = {
887       'S','E','L','E','C','T',' ','`','M','e','s','s','a','g','e','`',' ',
888       'F','R','O','M',' ','`','E','r','r','o','r','`',' ',
889       'W','H','E','R','E',' ','`','E','r','r','o','r','`',' ','=',' ',
890       '%','s',0
891     };
892     MSIRECORD *row = 0;
893     LPWSTR deformated = NULL;
894
895     deformat_string( package, target, &deformated );
896
897     /* first try treat the error as a number */
898     row = MSI_QueryGetRecord( package->db, query, deformated );
899     if( row )
900     {
901         LPCWSTR error = MSI_RecordGetString( row, 1 );
902         if ((package->ui_level & INSTALLUILEVEL_MASK) != INSTALLUILEVEL_NONE)
903             MessageBoxW( NULL, error, NULL, MB_OK );
904         msiobj_release( &row->hdr );
905     }
906     else if ((package->ui_level & INSTALLUILEVEL_MASK) != INSTALLUILEVEL_NONE)
907         MessageBoxW( NULL, deformated, NULL, MB_OK );
908
909     msi_free( deformated );
910
911     return ERROR_INSTALL_FAILURE;
912 }
913
914 static UINT HANDLE_CustomType50(MSIPACKAGE *package, LPCWSTR source,
915                                 LPCWSTR target, const INT type, LPCWSTR action)
916 {
917     WCHAR *exe, *arg;
918     HANDLE handle;
919
920     if (!(exe = msi_dup_property( package->db, source ))) return ERROR_SUCCESS;
921
922     deformat_string( package, target, &arg );
923     TRACE("exe %s arg %s\n", debugstr_w(exe), debugstr_w(arg));
924
925     handle = execute_command( exe, arg, szCRoot );
926     msi_free( arg );
927     if (handle == INVALID_HANDLE_VALUE) return ERROR_SUCCESS;
928     return wait_process_handle( package, type, handle, action );
929 }
930
931 static UINT HANDLE_CustomType34(MSIPACKAGE *package, LPCWSTR source,
932                                 LPCWSTR target, const INT type, LPCWSTR action)
933 {
934     const WCHAR *workingdir = NULL;
935     HANDLE handle;
936     WCHAR *cmd;
937
938     if (source)
939     {
940         workingdir = msi_get_target_folder( package, source );
941         if (!workingdir) return ERROR_FUNCTION_FAILED;
942     }
943     deformat_string( package, target, &cmd );
944     if (!cmd) return ERROR_FUNCTION_FAILED;
945
946     TRACE("cmd %s dir %s\n", debugstr_w(cmd), debugstr_w(workingdir));
947
948     handle = execute_command( NULL, cmd, workingdir );
949     msi_free( cmd );
950     if (handle == INVALID_HANDLE_VALUE) return ERROR_SUCCESS;
951     return wait_process_handle( package, type, handle, action );
952 }
953
954 static DWORD ACTION_CallScript( const GUID *guid )
955 {
956     msi_custom_action_info *info;
957     MSIHANDLE hPackage;
958     UINT r = ERROR_FUNCTION_FAILED;
959
960     info = find_action_by_guid( guid );
961     if (!info)
962     {
963         ERR("failed to find action %s\n", debugstr_guid( guid) );
964         return ERROR_FUNCTION_FAILED;
965     }
966
967     TRACE("function %s, script %s\n", debugstr_w( info->target ), debugstr_w( info->source ) );
968
969     hPackage = alloc_msihandle( &info->package->hdr );
970     if (hPackage)
971     {
972         r = call_script( hPackage, info->type, info->source, info->target, info->action );
973         TRACE("script returned %u\n", r);
974         MsiCloseHandle( hPackage );
975     }
976     else
977         ERR("failed to create handle for %p\n", info->package );
978
979     release_custom_action_data( info );
980     return r;
981 }
982
983 static DWORD WINAPI ScriptThread( LPVOID arg )
984 {
985     LPGUID guid = arg;
986     DWORD rc;
987
988     TRACE("custom action (%x) started\n", GetCurrentThreadId() );
989
990     rc = ACTION_CallScript( guid );
991
992     TRACE("custom action (%x) returned %i\n", GetCurrentThreadId(), rc );
993
994     MsiCloseAllHandles();
995     return rc;
996 }
997
998 static msi_custom_action_info *do_msidbCustomActionTypeScript(
999     MSIPACKAGE *package, INT type, LPCWSTR script, LPCWSTR function, LPCWSTR action )
1000 {
1001     msi_custom_action_info *info;
1002
1003     info = msi_alloc( sizeof *info );
1004     if (!info)
1005         return NULL;
1006
1007     msiobj_addref( &package->hdr );
1008     info->refs = 2; /* 1 for our caller and 1 for thread we created */
1009     info->package = package;
1010     info->type = type;
1011     info->target = strdupW( function );
1012     info->source = strdupW( script );
1013     info->action = strdupW( action );
1014     CoCreateGuid( &info->guid );
1015
1016     EnterCriticalSection( &msi_custom_action_cs );
1017     list_add_tail( &msi_pending_custom_actions, &info->entry );
1018     LeaveCriticalSection( &msi_custom_action_cs );
1019
1020     info->handle = CreateThread( NULL, 0, ScriptThread, &info->guid, 0, NULL );
1021     if (!info->handle)
1022     {
1023         /* release both references */
1024         release_custom_action_data( info );
1025         release_custom_action_data( info );
1026         return NULL;
1027     }
1028
1029     return info;
1030 }
1031
1032 static UINT HANDLE_CustomType37_38(MSIPACKAGE *package, LPCWSTR source,
1033                                LPCWSTR target, const INT type, LPCWSTR action)
1034 {
1035     msi_custom_action_info *info;
1036
1037     TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
1038
1039     info = do_msidbCustomActionTypeScript( package, type, target, NULL, action );
1040     return wait_thread_handle( info );
1041 }
1042
1043 static UINT HANDLE_CustomType5_6(MSIPACKAGE *package, LPCWSTR source,
1044                                LPCWSTR target, const INT type, LPCWSTR action)
1045 {
1046     static const WCHAR query[] = {
1047         'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1048         '`','B','i' ,'n','a','r','y','`',' ','W','H','E','R','E',' ',
1049         '`','N','a','m','e','`',' ','=',' ','\'','%','s','\'',0};
1050     MSIRECORD *row = 0;
1051     msi_custom_action_info *info;
1052     CHAR *buffer = NULL;
1053     WCHAR *bufferw = NULL;
1054     DWORD sz = 0;
1055     UINT r;
1056
1057     TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
1058
1059     row = MSI_QueryGetRecord(package->db, query, source);
1060     if (!row)
1061         return ERROR_FUNCTION_FAILED;
1062
1063     r = MSI_RecordReadStream(row, 2, NULL, &sz);
1064     if (r != ERROR_SUCCESS) return r;
1065
1066     buffer = msi_alloc( sz + 1 );
1067     if (!buffer) return ERROR_FUNCTION_FAILED;
1068
1069     r = MSI_RecordReadStream(row, 2, buffer, &sz);
1070     if (r != ERROR_SUCCESS)
1071         goto done;
1072
1073     buffer[sz] = 0;
1074     bufferw = strdupAtoW(buffer);
1075     if (!bufferw)
1076     {
1077         r = ERROR_FUNCTION_FAILED;
1078         goto done;
1079     }
1080
1081     info = do_msidbCustomActionTypeScript( package, type, bufferw, target, action );
1082     r = wait_thread_handle( info );
1083
1084 done:
1085     msi_free(bufferw);
1086     msi_free(buffer);
1087     return r;
1088 }
1089
1090 static UINT HANDLE_CustomType21_22(MSIPACKAGE *package, LPCWSTR source,
1091                                LPCWSTR target, const INT type, LPCWSTR action)
1092 {
1093     msi_custom_action_info *info;
1094     MSIFILE *file;
1095     HANDLE hFile;
1096     DWORD sz, szHighWord = 0, read;
1097     CHAR *buffer=NULL;
1098     WCHAR *bufferw=NULL;
1099     BOOL bRet;
1100     UINT r;
1101
1102     TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
1103
1104     file = msi_get_loaded_file(package, source);
1105     if (!file)
1106     {
1107         ERR("invalid file key %s\n", debugstr_w(source));
1108         return ERROR_FUNCTION_FAILED;
1109     }
1110
1111     hFile = CreateFileW(file->TargetPath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
1112     if (hFile == INVALID_HANDLE_VALUE) return ERROR_FUNCTION_FAILED;
1113
1114     sz = GetFileSize(hFile, &szHighWord);
1115     if (sz == INVALID_FILE_SIZE || szHighWord != 0)
1116     {
1117         CloseHandle(hFile);
1118         return ERROR_FUNCTION_FAILED;
1119     }
1120     buffer = msi_alloc( sz + 1 );
1121     if (!buffer)
1122     {
1123         CloseHandle(hFile);
1124         return ERROR_FUNCTION_FAILED;
1125     }
1126     bRet = ReadFile(hFile, buffer, sz, &read, NULL);
1127     CloseHandle(hFile);
1128     if (!bRet)
1129     {
1130         r = ERROR_FUNCTION_FAILED;
1131         goto done;
1132     }
1133     buffer[read] = 0;
1134     bufferw = strdupAtoW(buffer);
1135     if (!bufferw)
1136     {
1137         r = ERROR_FUNCTION_FAILED;
1138         goto done;
1139     }
1140     info = do_msidbCustomActionTypeScript( package, type, bufferw, target, action );
1141     r = wait_thread_handle( info );
1142
1143 done:
1144     msi_free(bufferw);
1145     msi_free(buffer);
1146     return r;
1147 }
1148
1149 static UINT HANDLE_CustomType53_54(MSIPACKAGE *package, LPCWSTR source,
1150                                LPCWSTR target, const INT type, LPCWSTR action)
1151 {
1152     msi_custom_action_info *info;
1153     WCHAR *prop;
1154
1155     TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
1156
1157     prop = msi_dup_property( package->db, source );
1158     if (!prop) return ERROR_SUCCESS;
1159
1160     info = do_msidbCustomActionTypeScript( package, type, prop, NULL, action );
1161     msi_free(prop);
1162     return wait_thread_handle( info );
1163 }
1164
1165 static BOOL action_type_matches_script( MSIPACKAGE *package, UINT type, UINT script )
1166 {
1167     switch (script)
1168     {
1169     case SCRIPT_NONE:
1170     case SCRIPT_INSTALL:
1171         return !(type & msidbCustomActionTypeCommit) && !(type & msidbCustomActionTypeRollback);
1172     case SCRIPT_COMMIT:
1173         return (type & msidbCustomActionTypeCommit);
1174     case SCRIPT_ROLLBACK:
1175         return (type & msidbCustomActionTypeRollback);
1176     default:
1177         ERR("unhandled script %u\n", script);
1178     }
1179     return FALSE;
1180 }
1181
1182 static UINT defer_custom_action( MSIPACKAGE *package, const WCHAR *action, UINT type )
1183 {
1184     WCHAR *actiondata = msi_dup_property( package->db, action );
1185     WCHAR *usersid = msi_dup_property( package->db, szUserSID );
1186     WCHAR *prodcode = msi_dup_property( package->db, szProductCode );
1187     WCHAR *deferred = msi_get_deferred_action( action, actiondata, usersid, prodcode );
1188
1189     if (!deferred)
1190     {
1191         msi_free( actiondata );
1192         msi_free( usersid );
1193         msi_free( prodcode );
1194         return ERROR_OUTOFMEMORY;
1195     }
1196     if (type & msidbCustomActionTypeCommit)
1197     {
1198         TRACE("deferring commit action\n");
1199         msi_schedule_action( package, SCRIPT_COMMIT, deferred );
1200     }
1201     else if (type & msidbCustomActionTypeRollback)
1202     {
1203         TRACE("deferring rollback action\n");
1204         msi_schedule_action( package, SCRIPT_ROLLBACK, deferred );
1205     }
1206     else
1207     {
1208         TRACE("deferring install action\n");
1209         msi_schedule_action( package, SCRIPT_INSTALL, deferred );
1210     }
1211
1212     msi_free( actiondata );
1213     msi_free( usersid );
1214     msi_free( prodcode );
1215     msi_free( deferred );
1216     return ERROR_SUCCESS;
1217 }
1218
1219 UINT ACTION_CustomAction(MSIPACKAGE *package, LPCWSTR action, UINT script, BOOL execute)
1220 {
1221     static const WCHAR query[] = {
1222         'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1223         '`','C','u','s','t','o','m','A','c','t','i','o','n','`',' ','W','H','E','R','E',' ',
1224         '`','A','c','t','i' ,'o','n','`',' ','=',' ','\'','%','s','\'',0};
1225     UINT rc = ERROR_SUCCESS;
1226     MSIRECORD *row;
1227     UINT type;
1228     const WCHAR *source, *target, *ptr, *deferred_data = NULL;
1229     WCHAR *deformated = NULL;
1230     int len;
1231
1232     /* deferred action: [properties]Action */
1233     if ((ptr = strrchrW(action, ']')))
1234     {
1235         deferred_data = action;
1236         action = ptr + 1;
1237     }
1238
1239     row = MSI_QueryGetRecord( package->db, query, action );
1240     if (!row)
1241         return ERROR_CALL_NOT_IMPLEMENTED;
1242
1243     type = MSI_RecordGetInteger(row,2);
1244     source = MSI_RecordGetString(row,3);
1245     target = MSI_RecordGetString(row,4);
1246
1247     TRACE("Handling custom action %s (%x %s %s)\n",debugstr_w(action),type,
1248           debugstr_w(source), debugstr_w(target));
1249
1250     /* handle some of the deferred actions */
1251     if (type & msidbCustomActionTypeTSAware)
1252         FIXME("msidbCustomActionTypeTSAware not handled\n");
1253
1254     if (type & msidbCustomActionTypeInScript)
1255     {
1256         if (type & msidbCustomActionTypeNoImpersonate)
1257             WARN("msidbCustomActionTypeNoImpersonate not handled\n");
1258
1259         if (!execute || !action_type_matches_script( package, type, script ))
1260         {
1261             rc = defer_custom_action( package, action, type );
1262             goto end;
1263         }
1264         else
1265         {
1266             LPWSTR actiondata = msi_dup_property( package->db, action );
1267
1268             if (type & msidbCustomActionTypeInScript)
1269                 package->scheduled_action_running = TRUE;
1270
1271             if (type & msidbCustomActionTypeCommit)
1272                 package->commit_action_running = TRUE;
1273
1274             if (type & msidbCustomActionTypeRollback)
1275                 package->rollback_action_running = TRUE;
1276
1277             if (deferred_data)
1278                 set_deferred_action_props(package, deferred_data);
1279             else if (actiondata)
1280                 msi_set_property( package->db, szCustomActionData, actiondata, -1 );
1281             else
1282                 msi_set_property( package->db, szCustomActionData, szEmpty, -1 );
1283
1284             msi_free(actiondata);
1285         }
1286     }
1287     else if (!check_execution_scheduling_options(package,action,type))
1288     {
1289         rc = ERROR_SUCCESS;
1290         goto end;
1291     }
1292
1293     switch (type & CUSTOM_ACTION_TYPE_MASK)
1294     {
1295         case 1: /* DLL file stored in a Binary table stream */
1296             rc = HANDLE_CustomType1(package,source,target,type,action);
1297             break;
1298         case 2: /* EXE file stored in a Binary table stream */
1299             rc = HANDLE_CustomType2(package,source,target,type,action);
1300             break;
1301         case 18: /*EXE file installed with package */
1302             rc = HANDLE_CustomType18(package,source,target,type,action);
1303             break;
1304         case 19: /* Error that halts install */
1305             rc = HANDLE_CustomType19(package,source,target,type,action);
1306             break;
1307         case 17:
1308             rc = HANDLE_CustomType17(package,source,target,type,action);
1309             break;
1310         case 23: /* installs another package in the source tree */
1311             deformat_string(package,target,&deformated);
1312             rc = HANDLE_CustomType23(package,source,deformated,type,action);
1313             msi_free(deformated);
1314             break;
1315         case 50: /*EXE file specified by a property value */
1316             rc = HANDLE_CustomType50(package,source,target,type,action);
1317             break;
1318         case 34: /*EXE to be run in specified directory */
1319             rc = HANDLE_CustomType34(package,source,target,type,action);
1320             break;
1321         case 35: /* Directory set with formatted text. */
1322             deformat_string(package,target,&deformated);
1323             MSI_SetTargetPathW(package, source, deformated);
1324             msi_free(deformated);
1325             break;
1326         case 51: /* Property set with formatted text. */
1327             if (!source)
1328                 break;
1329
1330             len = deformat_string( package, target, &deformated );
1331             rc = msi_set_property( package->db, source, deformated, len );
1332             if (rc == ERROR_SUCCESS && !strcmpW( source, szSourceDir ))
1333                 msi_reset_folders( package, TRUE );
1334             msi_free(deformated);
1335             break;
1336     case 37: /* JScript/VBScript text stored in target column. */
1337     case 38:
1338         rc = HANDLE_CustomType37_38(package,source,target,type,action);
1339         break;
1340     case 5:
1341     case 6: /* JScript/VBScript file stored in a Binary table stream. */
1342         rc = HANDLE_CustomType5_6(package,source,target,type,action);
1343         break;
1344     case 21: /* JScript/VBScript file installed with the product. */
1345     case 22:
1346         rc = HANDLE_CustomType21_22(package,source,target,type,action);
1347         break;
1348     case 53: /* JScript/VBScript text specified by a property value. */
1349     case 54:
1350         rc = HANDLE_CustomType53_54(package,source,target,type,action);
1351         break;
1352     default:
1353         FIXME("unhandled action type %u (%s %s)\n", type & CUSTOM_ACTION_TYPE_MASK,
1354               debugstr_w(source), debugstr_w(target));
1355     }
1356
1357 end:
1358     package->scheduled_action_running = FALSE;
1359     package->commit_action_running = FALSE;
1360     package->rollback_action_running = FALSE;
1361     msiobj_release(&row->hdr);
1362     return rc;
1363 }
1364
1365 void ACTION_FinishCustomActions(const MSIPACKAGE* package)
1366 {
1367     struct list *item;
1368     HANDLE *wait_handles;
1369     unsigned int handle_count, i;
1370     msi_custom_action_info *info, *cursor;
1371
1372     while ((item = list_head( &package->RunningActions )))
1373     {
1374         MSIRUNNINGACTION *action = LIST_ENTRY( item, MSIRUNNINGACTION, entry );
1375
1376         list_remove( &action->entry );
1377
1378         TRACE("waiting for %s\n", debugstr_w( action->name ) );
1379         msi_dialog_check_messages( action->handle );
1380
1381         CloseHandle( action->handle );
1382         msi_free( action->name );
1383         msi_free( action );
1384     }
1385
1386     EnterCriticalSection( &msi_custom_action_cs );
1387
1388     handle_count = list_count( &msi_pending_custom_actions );
1389     wait_handles = msi_alloc( handle_count * sizeof(HANDLE) );
1390
1391     handle_count = 0;
1392     LIST_FOR_EACH_ENTRY_SAFE( info, cursor, &msi_pending_custom_actions, msi_custom_action_info, entry )
1393     {
1394         if (info->package == package )
1395         {
1396             if (DuplicateHandle(GetCurrentProcess(), info->handle, GetCurrentProcess(), &wait_handles[handle_count], SYNCHRONIZE, FALSE, 0))
1397                 handle_count++;
1398         }
1399     }
1400
1401     LeaveCriticalSection( &msi_custom_action_cs );
1402
1403     for (i = 0; i < handle_count; i++)
1404     {
1405         msi_dialog_check_messages( wait_handles[i] );
1406         CloseHandle( wait_handles[i] );
1407     }
1408     msi_free( wait_handles );
1409
1410     EnterCriticalSection( &msi_custom_action_cs );
1411     LIST_FOR_EACH_ENTRY_SAFE( info, cursor, &msi_pending_custom_actions, msi_custom_action_info, entry )
1412     {
1413         if (info->package == package) release_custom_action_data( info );
1414     }
1415     LeaveCriticalSection( &msi_custom_action_cs );
1416 }
1417
1418 typedef struct _msi_custom_remote_impl {
1419     IWineMsiRemoteCustomAction IWineMsiRemoteCustomAction_iface;
1420     LONG refs;
1421 } msi_custom_remote_impl;
1422
1423 static inline msi_custom_remote_impl *impl_from_IWineMsiRemoteCustomAction( IWineMsiRemoteCustomAction *iface )
1424 {
1425     return CONTAINING_RECORD(iface, msi_custom_remote_impl, IWineMsiRemoteCustomAction_iface);
1426 }
1427
1428 static HRESULT WINAPI mcr_QueryInterface( IWineMsiRemoteCustomAction *iface,
1429                 REFIID riid,LPVOID *ppobj)
1430 {
1431     if( IsEqualCLSID( riid, &IID_IUnknown ) ||
1432         IsEqualCLSID( riid, &IID_IWineMsiRemoteCustomAction ) )
1433     {
1434         IWineMsiRemoteCustomAction_AddRef( iface );
1435         *ppobj = iface;
1436         return S_OK;
1437     }
1438
1439     return E_NOINTERFACE;
1440 }
1441
1442 static ULONG WINAPI mcr_AddRef( IWineMsiRemoteCustomAction *iface )
1443 {
1444     msi_custom_remote_impl* This = impl_from_IWineMsiRemoteCustomAction( iface );
1445
1446     return InterlockedIncrement( &This->refs );
1447 }
1448
1449 static ULONG WINAPI mcr_Release( IWineMsiRemoteCustomAction *iface )
1450 {
1451     msi_custom_remote_impl* This = impl_from_IWineMsiRemoteCustomAction( iface );
1452     ULONG r;
1453
1454     r = InterlockedDecrement( &This->refs );
1455     if (r == 0)
1456         msi_free( This );
1457     return r;
1458 }
1459
1460 static HRESULT WINAPI mcr_GetActionInfo( IWineMsiRemoteCustomAction *iface, LPCGUID custom_action_guid,
1461          INT *type, MSIHANDLE *handle, BSTR *dll, BSTR *func, IWineMsiRemotePackage **remote_package )
1462 {
1463     msi_custom_action_info *info;
1464
1465     info = find_action_by_guid( custom_action_guid );
1466     if (!info)
1467         return E_FAIL;
1468
1469     *type = info->type;
1470     *handle = alloc_msihandle( &info->package->hdr );
1471     *dll = SysAllocString( info->source );
1472     *func = SysAllocString( info->target );
1473
1474     release_custom_action_data( info );
1475     return create_msi_remote_package( NULL, (LPVOID *)remote_package );
1476 }
1477
1478 static const IWineMsiRemoteCustomActionVtbl msi_custom_remote_vtbl =
1479 {
1480     mcr_QueryInterface,
1481     mcr_AddRef,
1482     mcr_Release,
1483     mcr_GetActionInfo,
1484 };
1485
1486 HRESULT create_msi_custom_remote( IUnknown *pOuter, LPVOID *ppObj )
1487 {
1488     msi_custom_remote_impl* This;
1489
1490     This = msi_alloc( sizeof *This );
1491     if (!This)
1492         return E_OUTOFMEMORY;
1493
1494     This->IWineMsiRemoteCustomAction_iface.lpVtbl = &msi_custom_remote_vtbl;
1495     This->refs = 1;
1496
1497     *ppObj = This;
1498
1499     return S_OK;
1500 }