This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/
process.cpp
4819 lines (4056 loc) · 133 KB
/
process.cpp
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
/*++
Module Name:
process.cpp
Abstract:
Implementation of process object and functions related to processes.
--*/
#include "pal/dbgmsg.h"
SET_DEFAULT_DEBUG_CHANNEL(PROCESS); // some headers have code with asserts, so do this first
#include "pal/procobj.hpp"
#include "pal/thread.hpp"
#include "pal/file.hpp"
#include "pal/handlemgr.hpp"
#include "pal/module.h"
#include "procprivate.hpp"
#include "pal/palinternal.h"
#include "pal/process.h"
#include "pal/init.h"
#include "pal/critsect.h"
#include "pal/debug.h"
#include "pal/utils.h"
#include "pal/environ.h"
#include "pal/virtual.h"
#include "pal/stackstring.hpp"
#include <errno.h>
#if HAVE_POLL
#include <poll.h>
#else
#include "pal/fakepoll.h"
#endif // HAVE_POLL
#include <unistd.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <signal.h>
#if HAVE_PRCTL_H
#include <sys/prctl.h>
#include <sys/syscall.h>
#endif
#include <sys/wait.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <debugmacrosext.h>
#include <semaphore.h>
#include <stdint.h>
#include <dlfcn.h>
#ifdef __APPLE__
#include <sys/sysctl.h>
#endif
#ifdef __NetBSD__
#include <sys/cdefs.h>
#include <sys/param.h>
#include <sys/sysctl.h>
#include <kvm.h>
#endif
extern char *g_szCoreCLRPath;
using namespace CorUnix;
CObjectType CorUnix::otProcess(
otiProcess,
NULL, // No cleanup routine
NULL, // No initialization routine
0, // No immutable data
NULL, // No immutable data copy routine
NULL, // No immutable data cleanup routine
sizeof(CProcProcessLocalData),
NULL, // No process local data cleanup routine
0, // No shared data
PROCESS_ALL_ACCESS,
CObjectType::SecuritySupported,
CObjectType::SecurityInfoNotPersisted,
CObjectType::UnnamedObject,
CObjectType::CrossProcessDuplicationAllowed,
CObjectType::WaitableObject,
CObjectType::SingleTransitionObject,
CObjectType::ThreadReleaseHasNoSideEffects,
CObjectType::NoOwner
);
//
// Helper memory page used by the FlushProcessWriteBuffers
//
static int* s_helperPage = 0;
//
// Mutex to make the FlushProcessWriteBuffersMutex thread safe
//
pthread_mutex_t flushProcessWriteBuffersMutex;
CAllowedObjectTypes aotProcess(otiProcess);
//
// The representative IPalObject for this process
//
IPalObject* CorUnix::g_pobjProcess;
//
// Critical section that protects process data (e.g., the
// list of active threads)/
//
CRITICAL_SECTION g_csProcess;
//
// List and count of active threads
//
CPalThread* CorUnix::pGThreadList;
DWORD g_dwThreadCount;
//
// The command line and app name for the process
//
LPWSTR g_lpwstrCmdLine = NULL;
LPWSTR g_lpwstrAppDir = NULL;
// Thread ID of thread that has started the ExitProcess process
Volatile<LONG> terminator = 0;
// Process and session ID of this process.
DWORD gPID = (DWORD) -1;
DWORD gSID = (DWORD) -1;
// The lowest common supported semaphore length, including null character
// NetBSD-7.99.25: 15 characters
// MacOSX 10.11: 31 -- Core 1.0 RC2 compatibility
#if defined(__NetBSD__)
#define CLR_SEM_MAX_NAMELEN 15
#else
#define CLR_SEM_MAX_NAMELEN (NAME_MAX - 4)
#endif
// Function to call during PAL/process shutdown/abort
Volatile<PSHUTDOWN_CALLBACK> g_shutdownCallback = nullptr;
// Crash dump generating program arguments. Initialized in PROCAbortInitialize().
char* g_argvCreateDump[8] = { nullptr };
//
// Key used for associating CPalThread's with the underlying pthread
// (through pthread_setspecific)
//
pthread_key_t CorUnix::thObjKey;
#define PROCESS_PELOADER_FILENAME "clix"
static WCHAR W16_WHITESPACE[]= {0x0020, 0x0009, 0x000D, 0};
static WCHAR W16_WHITESPACE_DQUOTE[]= {0x0020, 0x0009, 0x000D, '"', 0};
enum FILETYPE
{
FILE_ERROR,/*ERROR*/
FILE_PE, /*PE/COFF file*/
FILE_UNIX, /*Unix Executable*/
FILE_DIR /*Directory*/
};
static
DWORD
PALAPI
StartupHelperThread(
LPVOID p);
static
BOOL
GetProcessIdDisambiguationKey(
IN DWORD processId,
OUT UINT64 *disambiguationKey);
PAL_ERROR
PROCGetProcessStatus(
CPalThread *pThread,
HANDLE hProcess,
PROCESS_STATE *pps,
DWORD *pdwExitCode);
static BOOL getFileName(LPCWSTR lpApplicationName, LPWSTR lpCommandLine, PathCharString& lpFileName);
static char ** buildArgv(LPCWSTR lpCommandLine, PathCharString& lpAppPath, UINT *pnArg, BOOL prependLoader);
static BOOL getPath(PathCharString& lpFileName, PathCharString& lpPathFileName);
static int checkFileType(LPCSTR lpFileName);
static BOOL PROCEndProcess(HANDLE hProcess, UINT uExitCode, BOOL bTerminateUnconditionally);
ProcessModules *GetProcessModulesFromHandle(IN HANDLE hProcess, OUT LPDWORD lpCount);
ProcessModules *CreateProcessModules(IN DWORD dwProcessId, OUT LPDWORD lpCount);
void DestroyProcessModules(IN ProcessModules *listHead);
/*++
Function:
GetCurrentProcessId
See MSDN doc.
--*/
DWORD
PALAPI
GetCurrentProcessId(
VOID)
{
PERF_ENTRY(GetCurrentProcessId);
ENTRY("GetCurrentProcessId()\n" );
LOGEXIT("GetCurrentProcessId returns DWORD %#x\n", gPID);
PERF_EXIT(GetCurrentProcessId);
return gPID;
}
/*++
Function:
GetCurrentSessionId
See MSDN doc.
--*/
DWORD
PALAPI
GetCurrentSessionId(
VOID)
{
PERF_ENTRY(GetCurrentSessionId);
ENTRY("GetCurrentSessionId()\n" );
LOGEXIT("GetCurrentSessionId returns DWORD %#x\n", gSID);
PERF_EXIT(GetCurrentSessionId);
return gSID;
}
/*++
Function:
GetCurrentProcess
See MSDN doc.
--*/
HANDLE
PALAPI
GetCurrentProcess(
VOID)
{
PERF_ENTRY(GetCurrentProcess);
ENTRY("GetCurrentProcess()\n" );
LOGEXIT("GetCurrentProcess returns HANDLE %p\n", hPseudoCurrentProcess);
PERF_EXIT(GetCurrentProcess);
/* return a pseudo handle */
return hPseudoCurrentProcess;
}
/*++
Function:
CreateProcessA
Note:
Only Standard handles need to be inherited.
Security attributes parameters are not used.
See MSDN doc.
--*/
BOOL
PALAPI
CreateProcessA(
IN LPCSTR lpApplicationName,
IN LPSTR lpCommandLine,
IN LPSECURITY_ATTRIBUTES lpProcessAttributes,
IN LPSECURITY_ATTRIBUTES lpThreadAttributes,
IN BOOL bInheritHandles,
IN DWORD dwCreationFlags,
IN LPVOID lpEnvironment,
IN LPCSTR lpCurrentDirectory,
IN LPSTARTUPINFOA lpStartupInfo,
OUT LPPROCESS_INFORMATION lpProcessInformation)
{
PAL_ERROR palError = NO_ERROR;
CPalThread *pThread;
STARTUPINFOW StartupInfoW;
LPWSTR CommandLineW = NULL;
LPWSTR ApplicationNameW = NULL;
LPWSTR CurrentDirectoryW = NULL;
int n;
PERF_ENTRY(CreateProcessA);
ENTRY("CreateProcessA(lpAppName=%p (%s), lpCmdLine=%p (%s), lpProcessAttr=%p, "
"lpThreadAttr=%p, bInherit=%d, dwFlags=%#x, lpEnv=%p, "
"lpCurrentDir=%p (%s), lpStartupInfo=%p, lpProcessInfo=%p)\n",
lpApplicationName?lpApplicationName:"NULL",
lpApplicationName?lpApplicationName:"NULL",
lpCommandLine?lpCommandLine:"NULL",
lpCommandLine?lpCommandLine:"NULL",
lpProcessAttributes, lpThreadAttributes, bInheritHandles,
dwCreationFlags, lpEnvironment,
lpCurrentDirectory?lpCurrentDirectory:"NULL",
lpCurrentDirectory?lpCurrentDirectory:"NULL",
lpStartupInfo, lpProcessInformation);
pThread = InternalGetCurrentThread();
if(lpStartupInfo == NULL)
{
ASSERT("lpStartupInfo is NULL!\n");
palError = ERROR_INVALID_PARAMETER;
goto done;
}
/* convert parameters to Unicode */
if(lpApplicationName)
{
n = MultiByteToWideChar(CP_ACP, 0, lpApplicationName, -1, NULL, 0);
if(0 == n)
{
ASSERT("MultiByteToWideChar failed!\n");
palError = ERROR_INTERNAL_ERROR;
goto done;
}
ApplicationNameW = (LPWSTR)InternalMalloc(sizeof(WCHAR)*n);
if(!ApplicationNameW)
{
ERROR("malloc() failed!\n");
palError = ERROR_NOT_ENOUGH_MEMORY;
goto done;
}
MultiByteToWideChar(CP_ACP, 0, lpApplicationName, -1, ApplicationNameW,
n);
}
if(lpCommandLine)
{
n = MultiByteToWideChar(CP_ACP, 0, lpCommandLine, -1, NULL, 0);
if(0 == n)
{
ASSERT("MultiByteToWideChar failed!\n");
palError = ERROR_INTERNAL_ERROR;
goto done;
}
CommandLineW = (LPWSTR)InternalMalloc(sizeof(WCHAR)*n);
if(!CommandLineW)
{
ERROR("malloc() failed!\n");
palError = ERROR_NOT_ENOUGH_MEMORY;
goto done;
}
MultiByteToWideChar(CP_ACP, 0, lpCommandLine, -1, CommandLineW, n);
}
if(lpCurrentDirectory)
{
n = MultiByteToWideChar(CP_ACP, 0, lpCurrentDirectory, -1, NULL, 0);
if(0 == n)
{
ASSERT("MultiByteToWideChar failed!\n");
palError = ERROR_INTERNAL_ERROR;
goto done;
}
CurrentDirectoryW = (LPWSTR)InternalMalloc(sizeof(WCHAR)*n);
if(!CurrentDirectoryW)
{
ERROR("malloc() failed!\n");
palError = ERROR_NOT_ENOUGH_MEMORY;
goto done;
}
MultiByteToWideChar(CP_ACP, 0, lpCurrentDirectory, -1,
CurrentDirectoryW, n);
}
// lpEnvironment should remain ansi on the call to CreateProcessW
StartupInfoW.cb = sizeof StartupInfoW;
StartupInfoW.dwFlags = lpStartupInfo->dwFlags;
StartupInfoW.hStdError = lpStartupInfo->hStdError;
StartupInfoW.hStdInput = lpStartupInfo->hStdInput;
StartupInfoW.hStdOutput = lpStartupInfo->hStdOutput;
/* all other members are PAL_Undefined, we can ignore them */
palError = InternalCreateProcess(
pThread,
ApplicationNameW,
CommandLineW,
lpProcessAttributes,
lpThreadAttributes,
bInheritHandles,
dwCreationFlags,
lpEnvironment,
CurrentDirectoryW,
&StartupInfoW,
lpProcessInformation
);
done:
free(ApplicationNameW);
free(CommandLineW);
free(CurrentDirectoryW);
if (NO_ERROR != palError)
{
pThread->SetLastError(palError);
}
LOGEXIT("CreateProcessA returns BOOL %d\n", NO_ERROR == palError);
PERF_EXIT(CreateProcessA);
return NO_ERROR == palError;
}
/*++
Function:
CreateProcessW
Note:
Only Standard handles need to be inherited.
Security attributes parameters are not used.
See MSDN doc.
--*/
BOOL
PALAPI
CreateProcessW(
IN LPCWSTR lpApplicationName,
IN LPWSTR lpCommandLine,
IN LPSECURITY_ATTRIBUTES lpProcessAttributes,
IN LPSECURITY_ATTRIBUTES lpThreadAttributes,
IN BOOL bInheritHandles,
IN DWORD dwCreationFlags,
IN LPVOID lpEnvironment,
IN LPCWSTR lpCurrentDirectory,
IN LPSTARTUPINFOW lpStartupInfo,
OUT LPPROCESS_INFORMATION lpProcessInformation)
{
PAL_ERROR palError = NO_ERROR;
CPalThread *pThread;
PERF_ENTRY(CreateProcessW);
ENTRY("CreateProcessW(lpAppName=%p (%S), lpCmdLine=%p (%S), lpProcessAttr=%p,"
"lpThreadAttr=%p, bInherit=%d, dwFlags=%#x, lpEnv=%p,"
"lpCurrentDir=%p (%S), lpStartupInfo=%p, lpProcessInfo=%p)\n",
lpApplicationName?lpApplicationName:W16_NULLSTRING,
lpApplicationName?lpApplicationName:W16_NULLSTRING,
lpCommandLine?lpCommandLine:W16_NULLSTRING,
lpCommandLine?lpCommandLine:W16_NULLSTRING,lpProcessAttributes,
lpThreadAttributes, bInheritHandles, dwCreationFlags,lpEnvironment,
lpCurrentDirectory?lpCurrentDirectory:W16_NULLSTRING,
lpCurrentDirectory?lpCurrentDirectory:W16_NULLSTRING,
lpStartupInfo, lpProcessInformation);
pThread = InternalGetCurrentThread();
palError = InternalCreateProcess(
pThread,
lpApplicationName,
lpCommandLine,
lpProcessAttributes,
lpThreadAttributes,
bInheritHandles,
dwCreationFlags,
lpEnvironment,
lpCurrentDirectory,
lpStartupInfo,
lpProcessInformation
);
if (NO_ERROR != palError)
{
pThread->SetLastError(palError);
}
LOGEXIT("CreateProcessW returns BOOL %d\n", NO_ERROR == palError);
PERF_EXIT(CreateProcessW);
return NO_ERROR == palError;
}
PAL_ERROR
PrepareStandardHandle(
CPalThread *pThread,
HANDLE hFile,
IPalObject **ppobjFile,
int *piFd
)
{
PAL_ERROR palError = NO_ERROR;
IPalObject *pobjFile = NULL;
IDataLock *pDataLock = NULL;
CFileProcessLocalData *pLocalData = NULL;
int iError = 0;
palError = g_pObjectManager->ReferenceObjectByHandle(
pThread,
hFile,
&aotFile,
0,
&pobjFile
);
if (NO_ERROR != palError)
{
ERROR("Bad handle passed through CreateProcess\n");
goto PrepareStandardHandleExit;
}
palError = pobjFile->GetProcessLocalData(
pThread,
ReadLock,
&pDataLock,
reinterpret_cast<void **>(&pLocalData)
);
if (NO_ERROR != palError)
{
ASSERT("Unable to access file data\n");
goto PrepareStandardHandleExit;
}
//
// The passed in file needs to be inheritable
//
if (!pLocalData->inheritable)
{
ERROR("Non-inheritable handle passed through CreateProcess\n");
palError = ERROR_INVALID_HANDLE;
goto PrepareStandardHandleExit;
}
iError = fcntl(pLocalData->unix_fd, F_SETFD, 0);
if (-1 == iError)
{
ERROR("Unable to remove close-on-exec for file (errno %i)\n", errno);
palError = ERROR_INVALID_HANDLE;
goto PrepareStandardHandleExit;
}
*piFd = pLocalData->unix_fd;
pDataLock->ReleaseLock(pThread, FALSE);
pDataLock = NULL;
//
// Transfer pobjFile reference to out parameter
//
*ppobjFile = pobjFile;
pobjFile = NULL;
PrepareStandardHandleExit:
if (NULL != pDataLock)
{
pDataLock->ReleaseLock(pThread, FALSE);
}
if (NULL != pobjFile)
{
pobjFile->ReleaseReference(pThread);
}
return palError;
}
PAL_ERROR
CorUnix::InternalCreateProcess(
CPalThread *pThread,
LPCWSTR lpApplicationName,
LPWSTR lpCommandLine,
LPSECURITY_ATTRIBUTES lpProcessAttributes,
LPSECURITY_ATTRIBUTES lpThreadAttributes,
BOOL bInheritHandles,
DWORD dwCreationFlags,
LPVOID lpEnvironment,
LPCWSTR lpCurrentDirectory,
LPSTARTUPINFOW lpStartupInfo,
LPPROCESS_INFORMATION lpProcessInformation
)
{
PAL_ERROR palError = NO_ERROR;
IPalObject *pobjProcess = NULL;
IPalObject *pobjProcessRegistered = NULL;
IDataLock *pLocalDataLock = NULL;
CProcProcessLocalData *pLocalData;
IDataLock *pSharedDataLock = NULL;
CPalThread *pDummyThread = NULL;
HANDLE hDummyThread = NULL;
HANDLE hProcess = NULL;
CObjectAttributes oa(NULL, lpProcessAttributes);
IPalObject *pobjFileIn = NULL;
int iFdIn = -1;
IPalObject *pobjFileOut = NULL;
int iFdOut = -1;
IPalObject *pobjFileErr = NULL;
int iFdErr = -1;
pid_t processId;
PathCharString lpFileNamePS;
char **lppArgv = NULL;
UINT nArg;
int iRet;
char **EnvironmentArray=NULL;
int child_blocking_pipe = -1;
int parent_blocking_pipe = -1;
/* Validate parameters */
/* note : specs indicate lpApplicationName should always
be NULL; however support for it is already implemented. Leaving the code
in, specs can change; but rejecting non-NULL for now to conform to the
spec. */
if( NULL != lpApplicationName )
{
ASSERT("lpApplicationName should be NULL, but is %S instead\n",
lpApplicationName);
palError = ERROR_INVALID_PARAMETER;
goto InternalCreateProcessExit;
}
if (0 != (dwCreationFlags & ~(CREATE_SUSPENDED|CREATE_NEW_CONSOLE)))
{
ASSERT("Unexpected creation flags (%#x)\n", dwCreationFlags);
palError = ERROR_INVALID_PARAMETER;
goto InternalCreateProcessExit;
}
/* Security attributes parameters are ignored */
if (lpProcessAttributes != NULL &&
(lpProcessAttributes->lpSecurityDescriptor != NULL ||
lpProcessAttributes->bInheritHandle != TRUE))
{
ASSERT("lpProcessAttributes is invalid, parameter ignored (%p)\n",
lpProcessAttributes);
palError = ERROR_INVALID_PARAMETER;
goto InternalCreateProcessExit;
}
if (lpThreadAttributes != NULL)
{
ASSERT("lpThreadAttributes parameter must be NULL (%p)\n",
lpThreadAttributes);
palError = ERROR_INVALID_PARAMETER;
goto InternalCreateProcessExit;
}
/* note : Win32 crashes in this case */
if(NULL == lpStartupInfo)
{
ERROR("lpStartupInfo is NULL\n");
palError = ERROR_INVALID_PARAMETER;
goto InternalCreateProcessExit;
}
/* Validate lpStartupInfo.cb field */
if (lpStartupInfo->cb < sizeof(STARTUPINFOW))
{
ASSERT("lpStartupInfo parameter structure size is invalid (%u)\n",
lpStartupInfo->cb);
palError = ERROR_INVALID_PARAMETER;
goto InternalCreateProcessExit;
}
/* lpStartupInfo should be either zero or STARTF_USESTDHANDLES */
if (lpStartupInfo->dwFlags & ~STARTF_USESTDHANDLES)
{
ASSERT("lpStartupInfo parameter invalid flags (%#x)\n",
lpStartupInfo->dwFlags);
palError = ERROR_INVALID_PARAMETER;
goto InternalCreateProcessExit;
}
/* validate given standard handles if we have any */
if (lpStartupInfo->dwFlags & STARTF_USESTDHANDLES)
{
palError = PrepareStandardHandle(
pThread,
lpStartupInfo->hStdInput,
&pobjFileIn,
&iFdIn
);
if (NO_ERROR != palError)
{
goto InternalCreateProcessExit;
}
palError = PrepareStandardHandle(
pThread,
lpStartupInfo->hStdOutput,
&pobjFileOut,
&iFdOut
);
if (NO_ERROR != palError)
{
goto InternalCreateProcessExit;
}
palError = PrepareStandardHandle(
pThread,
lpStartupInfo->hStdError,
&pobjFileErr,
&iFdErr
);
if (NO_ERROR != palError)
{
goto InternalCreateProcessExit;
}
}
if (!getFileName(lpApplicationName, lpCommandLine, lpFileNamePS))
{
ERROR("Can't find executable!\n");
palError = ERROR_FILE_NOT_FOUND;
goto InternalCreateProcessExit;
}
/* check type of file */
iRet = checkFileType(lpFileNamePS);
switch (iRet)
{
case FILE_ERROR: /* file not found, or not an executable */
WARN ("File is not valid (%s)", lpFileNamePS.GetString());
palError = ERROR_FILE_NOT_FOUND;
goto InternalCreateProcessExit;
case FILE_PE: /* PE/COFF file */
//Get the path name where the PAL DLL was loaded from
if ( PAL_GetPALDirectoryA( lpFileNamePS ))
{
if (lpFileNamePS.Append("/", 1) == FALSE ||
lpFileNamePS.Append( PROCESS_PELOADER_FILENAME, strlen(PROCESS_PELOADER_FILENAME)) == FALSE)
{
ERROR("Append failed!\n");
palError = ERROR_INTERNAL_ERROR;
goto InternalCreateProcessExit;
}
}
else
{
ASSERT("PAL_GetPALDirectoryA failed to return the"
"pal installation directory \n");
palError = ERROR_INTERNAL_ERROR;
goto InternalCreateProcessExit;
}
break;
case FILE_UNIX: /* Unix binary file */
break; /* nothing to do */
case FILE_DIR:/*Directory*/
WARN ("File is a Directory (%s)", lpFileNamePS.GetString());
palError = ERROR_ACCESS_DENIED;
goto InternalCreateProcessExit;
break;
default: /* not supposed to get here */
ASSERT ("Invalid return type from checkFileType");
palError = ERROR_FILE_NOT_FOUND;
goto InternalCreateProcessExit;
}
/* build Argument list, lppArgv is allocated in buildArgv function and
requires to be freed */
lppArgv = buildArgv(lpCommandLine, lpFileNamePS, &nArg, iRet==1);
/* set the Environment variable */
if (lpEnvironment != NULL)
{
unsigned i;
// Since CREATE_UNICODE_ENVIRONMENT isn't supported we know the string is ansi
unsigned EnvironmentEntries = 0;
// Convert the environment block to array of strings
// Count the number of entries
// Is it a string that contains null terminated string, the end is delimited
// by two null in a row.
for (i = 0; ((char *)lpEnvironment)[i]!='\0'; i++)
{
EnvironmentEntries ++;
for (;((char *)lpEnvironment)[i]!='\0'; i++)
{
}
}
EnvironmentEntries++;
EnvironmentArray = (char **)InternalMalloc(EnvironmentEntries * sizeof(char *));
EnvironmentEntries = 0;
// Convert the environment block to array of strings
// Count the number of entries
// Is it a string that contains null terminated string, the end is delimited
// by two null in a row.
for (i = 0; ((char *)lpEnvironment)[i]!='\0'; i++)
{
EnvironmentArray[EnvironmentEntries] = &((char *)lpEnvironment)[i];
EnvironmentEntries ++;
for (;((char *)lpEnvironment)[i]!='\0'; i++)
{
}
}
EnvironmentArray[EnvironmentEntries] = NULL;
}
//
// Allocate and register the process object for the new process
//
palError = g_pObjectManager->AllocateObject(
pThread,
&otProcess,
&oa,
&pobjProcess
);
if (NO_ERROR != palError)
{
ERROR("Unable to allocate object for new proccess\n");
goto InternalCreateProcessExit;
}
palError = g_pObjectManager->RegisterObject(
pThread,
pobjProcess,
&aotProcess,
PROCESS_ALL_ACCESS,
&hProcess,
&pobjProcessRegistered
);
//
// pobjProcess is invalidated by the above call, so
// NULL it out here
//
pobjProcess = NULL;
if (NO_ERROR != palError)
{
ERROR("Unable to register new process object\n");
goto InternalCreateProcessExit;
}
//
// Create a new "dummy" thread object
//
palError = InternalCreateDummyThread(
pThread,
lpThreadAttributes,
&pDummyThread,
&hDummyThread
);
if (dwCreationFlags & CREATE_SUSPENDED)
{
int pipe_descs[2];
if (-1 == pipe(pipe_descs))
{
ERROR("pipe() failed! error is %d (%s)\n", errno, strerror(errno));
palError = ERROR_NOT_ENOUGH_MEMORY;
goto InternalCreateProcessExit;
}
/* [0] is read end, [1] is write end */
pDummyThread->suspensionInfo.SetBlockingPipe(pipe_descs[1]);
parent_blocking_pipe = pipe_descs[1];
child_blocking_pipe = pipe_descs[0];
}
palError = pobjProcessRegistered->GetProcessLocalData(
pThread,
WriteLock,
&pLocalDataLock,
reinterpret_cast<void **>(&pLocalData)
);
if (NO_ERROR != palError)
{
ASSERT("Unable to obtain local data for new process object\n");
goto InternalCreateProcessExit;
}
/* fork the new process */
processId = fork();
if (processId == -1)
{
ASSERT("Unable to create a new process with fork()\n");
if (-1 != child_blocking_pipe)
{
close(child_blocking_pipe);
close(parent_blocking_pipe);
}
palError = ERROR_INTERNAL_ERROR;
goto InternalCreateProcessExit;
}
/* From the time the child process begins running, to when it reaches execve,
the child process is not a real PAL process and does not own any PAL
resources, although it has access to the PAL resources of its parent process.
Thus, while the child process is in this window, it is dangerous for it to affect
its parent's PAL resources. As a consequence, no PAL code should be used
in this window; all code should make unix calls. Note the use of _exit
instead of exit to avoid calling PAL_Terminate and the lack of TRACE's and
ASSERT's. */
if (processId == 0) /* child process */
{
// At this point, the PAL should be considered uninitialized for this child process.
// Don't want to enter the init_critsec here since we're trying to avoid
// calling PAL functions. Furthermore, nothing should be changing
// the init_count in the child process at this point since this is the only
// thread executing.
init_count = 0;
sigset_t sm;
//
// Clear out the signal mask for the new process.
//
sigemptyset(&sm);
iRet = sigprocmask(SIG_SETMASK, &sm, NULL);
if (iRet != 0)
{
_exit(EXIT_FAILURE);
}
if (dwCreationFlags & CREATE_SUSPENDED)
{
BYTE resume_code = 0;
ssize_t read_ret;
/* close the write end of the pipe, the child doesn't need it */
close(parent_blocking_pipe);
read_again:
/* block until ResumeThread writes something to the pipe */
read_ret = read(child_blocking_pipe, &resume_code, sizeof(resume_code));
if (sizeof(resume_code) != read_ret)
{
if (read_ret == -1 && EINTR == errno)
{
goto read_again;
}
else
{
/* note : read might return 0 (and return EAGAIN) if the other
end of the pipe gets closed - for example because the parent
process dies (very) abruptly */
_exit(EXIT_FAILURE);
}
}
if (WAKEUPCODE != resume_code)
{
// resume_code should always equal WAKEUPCODE.
_exit(EXIT_FAILURE);
}
close(child_blocking_pipe);
}
/* Set the current directory */
if (lpCurrentDirectory)
{
SetCurrentDirectoryW(lpCurrentDirectory);
}
/* Set the standard handles to the incoming values */
if (lpStartupInfo->dwFlags & STARTF_USESTDHANDLES)
{
/* For each handle, we need to duplicate the incoming unix
fd to the corresponding standard one. The API that I use,
dup2, will copy the source to the destination, automatically
closing the existing destination, in an atomic way */
if (dup2(iFdIn, STDIN_FILENO) == -1)
{
// Didn't duplicate standard in.
_exit(EXIT_FAILURE);
}
if (dup2(iFdOut, STDOUT_FILENO) == -1)
{
// Didn't duplicate standard out.