-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathURoutine.pas
1055 lines (879 loc) · 30 KB
/
URoutine.pas
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
(*
Letterpress
Windows is a piece of crap.
Copyright 2009-2010, Garnet
*)
{$UNDEF DEBUG}
unit URoutine;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, Registry, Math, ShellAPI, SHDocVw_TLB;
type
{ Output (stdout reader) thread events }
TOutputBufferReady = procedure(Sender: TObject;
const Data: UTF8String) of object; // Event for buffered output in HTML win
TOuputCompleted = procedure(Sender: TObject;
const Data: UTF8String) of object; // Event for complete output (all others)
TOutputProcessFinished = procedure(Sender: TObject;
const ExitCode: Cardinal) of object; // Output thread controls process termination
{ Input (stdin writer) thread events }
TInputRequested = procedure(Sender: TObject;
const Data: PUTF8String) of object;
{ stdout reader }
TOutputCommandThread = class(TThread)
private
{ General properties }
fExitCode: Cardinal;
fWaitForCompleteOutput: Boolean;
fOutputStream, fProcess: THandle;
{ Buffer accumulator }
fAccumulated: Integer;
fOutputBuffer, fOutputAccumulator: UTF8String;
{ Events }
fOnOutputBufferReady: TOutputBufferReady;
fOnOutputCompleted: TOuputCompleted;
fOnOutputProcessFinished: TOutputProcessFinished;
{ Sync methods }
procedure FlushOutput;
procedure OutputCompleted;
procedure ProcessFinished;
protected
procedure Execute; override;
public
constructor Create(CreateSuspended, WaitForCompleteOutput,
FreeOnTerm: Boolean; OutStream, Process: THandle);
property OnOutputBufferReady: TOutputBufferReady
write fOnOutputBufferReady;
property OnOutputCompleted: TOuputCompleted
write fOnOutputCompleted;
property OnOutputProcessFinished: TOutputProcessFinished
write fOnOutputProcessFinished;
end;
TInputCommandThread = class(TThread)
private
{ States }
fHandleClosed, // fInputStream pipe was closed
fInputReady, // Input prompt was issued and user entered data
fCanceled, // Halt command issued or input prompt was issued and user
// refused to enter anything
fAwaitingInput, // Input prompt hs been issued and waiting for input data
fWriteInitialInput: Boolean; // Thread was created with some initial data
// to send in
{ Handles }
fInputStream, fMonitorStream1, fMonitorStream2,
fProcess: THandle;
{ Input buffers }
fInput: PUTF8String;
fInitialInput: RawByteString;
{ Input event }
fOnInputRequested: TInputRequested;
{ Input event & state methods }
procedure InputRequest;
procedure GotInput;
procedure Cancel;
{ Sync methods }
procedure SetHandleClosed;
procedure SetInputReady(Value: Boolean);
procedure SetCanceled(Value: Boolean);
protected
procedure Execute; override;
public
constructor Create(CreateSuspended, FreeOnTerm: Boolean;
InStream, MonStream1, MonStream2, Process: THandle;
const InitialInput: RawByteString);
destructor Destroy; override;
property InputReady: Boolean write SetInputReady;
property Canceled: Boolean write SetCanceled;
property HandleClosed: Boolean read fHandleClosed;
property OnInputRequested: TInputRequested write fOnInputRequested;
end;
function NtSuspendProcess(dwProcess: Cardinal): DWORD; stdcall; external 'ntdll.dll';
function NtResumeProcess(dwProcess: Cardinal): DWORD; stdcall; external 'ntdll.dll';
function LaunchPipedProcess(var g_hChildStd_OUT_Rd, g_hChildStd_IN_Wr,
g_hChildStd_Inf1, g_hChildStd_Inf2, g_Proc: THandle;
Command, Dir, CommandDir, Env: UnicodeString;
Editor: TObject = nil): Boolean;
implementation
uses
SynEdit, SynUniHighlighter, SynUnicode, SynEditMiscProcs,
UConst, UInput, USettings, UProjectDrawer;
const
nBufferSize = 4096;
nDelayTime = 50;
{ URoutine.pas }
// -----------------------------------------------------------------------------
function InjectDLL(hProcess: THandle; DllPath: AnsiString): Boolean;
var
kern32: HMODULE;
hThread, Dummy: Cardinal;
Addr: Pointer;
begin
{ Initialize }
Result := False;
{ Get kernel32.dll handle }
kern32 := GetModuleHandle('kernel32');
{ Allocate space for dll name in victim's address space }
Addr := VirtualAllocEx(hProcess, nil, Length(DllPath) + 1, MEM_COMMIT, PAGE_READWRITE);
if Addr = nil then
Exit;
{ Write DLL name into newely allocated memory }
WriteProcessMemory(hProcess, Addr, PAnsiChar(DllPath), Length(DllPath) + 1, Dummy);
{ Call LoadLibrary in victim's address space in order to load our DLL }
hThread := CreateRemoteThread(hProcess, nil, 0,
GetProcAddress(kern32, 'LoadLibraryA'), Addr, 0, Dummy);
{ Free memory used for DLL name }
VirtualFreeEx(hProcess, Addr, Length(DllPath) + 1, MEM_RELEASE);
{ Check if CreateRemoteThread failed }
if hThread = 0 then
Exit;
{ Wait for thread to establish }
WaitForSingleObject(hThread, INFINITE);
{ Get hModule of our DLL }
GetExitCodeThread(hThread, Dummy);
CloseHandle(hThread);
if Dummy = 0 then
Exit;
{ Success }
Result := True;
end;
function LaunchPipedProcess(var g_hChildStd_OUT_Rd, g_hChildStd_IN_Wr,
g_hChildStd_Inf1, g_hChildStd_Inf2, g_Proc: THandle;
Command, Dir, CommandDir, Env: UnicodeString;
Editor: TObject = nil): Boolean;
const
cEnvironmentNull: AnsiChar = #0;
var
g_hChildStd_OUT_Rd_Tmp, g_hChildStd_OUT_Wr,
g_hChildStd_IN_Wr_Tmp, g_hChildStd_IN_Rd,
g_hChildStd_Err: THandle;
Directory: PChar;
{ Ensure environment variables ready }
procedure RecreateEnvironmentVariables;
var
I, J, K: Integer;
EnironmentList: TStringList;
CurrLine, CurrChar: UnicodeString;
CurrLineConverted: UTF8String;
Runner: PAnsiChar;
begin
{ Assemble all environment variables together }
EnironmentList := TStringList.Create;
try
{ Configure }
with EnironmentList do
begin
NameValueSeparator := '=';
LineBreak := #26;
Text := Env;
BeginUpdate;
end;
{ Windows }
with EnironmentList do
begin
Add('NUMBER_OF_PROCESSORS=' + GetEnvironmentVariable('NUMBER_OF_PROCESSORS'));
Add('PROCESSOR_ARCHITECTURE=' + GetEnvironmentVariable('PROCESSOR_ARCHITECTURE'));
Add('PROCESSOR_IDENTIFIER=' + GetEnvironmentVariable('PROCESSOR_IDENTIFIER'));
Add('PROCESSOR_LEVEL=' + GetEnvironmentVariable('PROCESSOR_LEVEL'));
Add('PROCESSOR_REVISION=' + GetEnvironmentVariable('PROCESSOR_REVISION'));
Add('OS=' + GetEnvironmentVariable('OS'));
Add('COMSPEC=' + GetEnvironmentVariable('COMSPEC'));
Add('HOMEDRIVE=' + GetEnvironmentVariable('HOMEDRIVE'));
Add('HOMEPATH=' + GetEnvironmentVariable('HOMEPATH'));
Add('PATH=' + GetEnvironmentVariable('PATH'));
Add('PATHEXT=' + GetEnvironmentVariable('PATHEXT'));
Add('PROMPT=' + GetEnvironmentVariable('PROMPT'));
Add('SYSTEMDRIVE=' + GetEnvironmentVariable('SYSTEMDRIVE'));
Add('SYSTEMROOT=' + GetEnvironmentVariable('SYSTEMROOT'));
Add('WINDIR=' + GetEnvironmentVariable('WINDIR'));
Add('TEMP=' + GetEnvironmentVariable('TEMP'));
Add('TMP=' + GetEnvironmentVariable('TMP'));
end;
{ Global }
with SetEnvironment.SetGlobal do
for I := 0 to Pred(Count) do
EnironmentList.Add(Names[I] + '=' + ExpandLetterpressVariables(ValueFromIndex[I], CommandDir));
{ Project (can override global) }
if Assigned(FrmProjectDrawer) then
begin
with FrmProjectDrawer.Environment do
for I := 0 to Pred(Count) do
begin
J := EnironmentList.IndexOfName(Names[I]);
if J > -1 then
EnironmentList.ValueFromIndex[J] := ValueFromIndex[I]
else
EnironmentList.Add(Names[I] + '=' + ValueFromIndex[I]);
end;
EnironmentList.Add('LETTERPRESS_SELECTED_FILE=' + FrmProjectDrawer.GetSelectedFileOrFolder);
EnironmentList.Add('LETTERPRESS_SELECTED_FILES=' + FrmProjectDrawer.GetSelectedFilesOrFolders);
end;
{ Grammar }
if Assigned(Editor) and Assigned(TSynEdit(Editor).Highlighter) then
with TSynUniSyn(TSynEdit(Editor).Highlighter).Environment do
for I := 0 to Pred(Count) do
EnironmentList.Add(Names[I] + '=' + ValueFromIndex[I]);
{ Editor }
if Assigned(Editor) then
with Editor as TSynEdit do
begin
CurrLine := LineText;
if CaretX <= Length(CurrLine) then
CurrChar := CurrLine[CaretX]
else
CurrChar := EmptyStr;
CurrLineConverted := UnicodeStringToUTF8(CurrLine);
with EnironmentList do
begin
Add('LETTERPRESS_CARET_X=' + IntToStr(CaretX));
Add('LETTERPRESS_CARET_Y=' + IntToStr(CaretY));
Add('LETTERPRESS_COLUMN=' + IntToStr(DisplayX));
Add('LETTERPRESS_COLUMNS=' + IntToStr(RightEdge));
Add('LETTERPRESS_CURRENT_SCOPE=' + CurrentScope);
Add('LETTERPRESS_CURRENT_LINE=' + CurrLine);
Add('LETTERPRESS_CURRENT_WORD=' + WordAtCursor);
Add('LETTERPRESS_CURRENT_CHAR=' + CurrChar);
Add('LETTERPRESS_LINE_INDEX=' + IntToStr(Max(Pred(CountUnicodeChars(CurrLineConverted, CaretX)), 0)));
Add('LETTERPRESS_LINE_NUMBER=' + IntToStr(CaretY));
Add('LETTERPRESS_TAB_SIZE=' + IntToStr(TabWidth));
(*
if FileExists(FileName) then
begin
Add('LETTERPRESS_FILE_NAME=' + FileName);
Add('LETTERPRESS_FILE_PATH=' + IncludeTrailingPathDelimiter(ExtractFilePath(FileName)));
end;
if eoTabsToSpaces in Options then
Add('LETTERPRESS_SOFT_TABS=YES')
else
Add('LETTERPRESS_SOFT_TABS=NO');
*)
end;
end;
{ Letterpress }
with EnironmentList do
begin
Add('LETTERPRESS_EXE=' + Application.ExeName);
Add('LETTERPRESS_PATH=' + GetAppDir);
Add('LETTERPRESS_DATA_PATH=' + GetSettingsDir + sCommandsDir);
Add('LETTERPRESS_COMMAND_PATH=' + CommandDir);
Add('LETTERPRESS_TEMP_PATH=' + GetTempDir);
Add('LETTERPRESS_PID=' + IntToStr(GetCurrentProcessId));
end;
{ Sort }
with EnironmentList do
begin
Sort;
EndUpdate;
end;
{ Expand environment variables }
for I := 0 to Pred(EnironmentList.Count) do
begin
Command := StringReplace(Command, '${' + EnironmentList.Names[I] + '}',
EnironmentList.ValueFromIndex[I], [rfReplaceAll]);
Dir := StringReplace(Dir, '${' + EnironmentList.Names[I] + '}',
EnironmentList.ValueFromIndex[I], [rfReplaceAll]);
end;
{ Assemble environment string. Count length of block }
K := 1;
for I := 0 to Pred(EnironmentList.Count) do
Inc(K, Length(UnicodeStringToUTF8(EnironmentList[I])) + 1);
{ No environment variables }
if K = 1 then
Inc(K);
{ Copy to block }
if Environment = nil then
Environment := AllocMem(K)
else
Environment := ReallocMemory(Environment, K);
Runner := Environment;
if EnironmentList.Count > 0 then
for I := 0 to Pred(EnironmentList.Count) do
begin
CurrLineConverted := UnicodeStringToUTF8(EnironmentList[I]);
Move(CurrLineConverted[1], Runner^, Length(CurrLineConverted));
Inc(Runner, Length(CurrLineConverted));
Move(cEnvironmentNull, Runner^, 1);
Inc(Runner, 1);
end
else begin
Move(cEnvironmentNull, Runner^, 1);
Inc(Runner, 1);
end;
Move(cEnvironmentNull, Runner^, 1);
finally
FreeAndNil(EnironmentList);
end;
end;
{ Launch target process }
function CreateChildProcess: Boolean;
var
Start: TStartUpInfo;
ProcessInfo: TProcessInformation;
begin
{ Initialize }
Result := True;
{ Prepare starup info }
FillChar(Start, SizeOf(Start), #0);
with Start do
begin
cb := SizeOf(Start);
hStdError := g_hChildStd_Err;
hStdOutput := g_hChildStd_OUT_Wr;
hStdInput := g_hChildStd_IN_Rd;
dwFlags := STARTF_USESTDHANDLES or STARTF_USESHOWWINDOW;
wShowWindow := SW_HIDE;
end;
{ Execute command }
try
{ Get enbironment block }
RecreateEnvironmentVariables;
{ Get working directory }
if Dir <> EmptyStr then
Directory := PChar(Dir)
else if CommandDir <> EmptyStr then
Directory := PChar(CommandDir);
{ Create child process }
if CreateProcess(nil, PChar(Command), nil, nil, True,
CREATE_NEW_CONSOLE, Environment, Directory, Start, ProcessInfo) then
{ Retrieve handles which will be closed after execution }
begin
{ Get process handles }
g_Proc := ProcessInfo.hProcess;
{ Close pipe handles (do not continue to modify the parent) }
CloseHandle(g_hChildStd_OUT_Wr);
CloseHandle(g_hChildStd_IN_Rd);
CloseHandle(g_hChildStd_Err);
{ Close unneeded handles. Process handle will be used to retrieve
exit code and to suspend entire child }
CloseHandle(ProcessInfo.hThread);
{ Wait for process to start up }
WaitForInputIdle(g_Proc, INFINITE);
{ Inject our DLL in process' space and load id }
if not SetSystem.SetAlwaysPresentInputPrompt then
if not InjectDll(g_Proc, UnicodeStringToUTF8(GetAppDir + 'binary.dll')) then
begin
CloseHandle(g_hChildStd_Inf1);
g_hChildStd_Inf1 := INVALID_HANDLE_VALUE;
end;
end
(*
{ Output error message }
else begin
Msg(MainForm.Handle, SysErrorMessage(GetLastError), MB_ICONERROR or MB_OK);
Result := False;
Exit;
end;
*)
{ Output critical error message }
except
on E: Exception do
begin
// Msg(MainForm.Handle, E.Message, MB_ICONERROR or MB_OK);
Result := False;
Exit;
end;
end;
end;
var
Security: TSecurityAttributes;
Descriptor: TSecurityDescriptor;
execCommand, execPath, srchPath, sUUID: UnicodeString;
Dummy: PChar;
begin
{ Initialize }
Result := False;
{ Check command format }
if Command[1] <> '"' then
begin
{ Get executable part }
execCommand := Copy(Command, 1, Pred(Pos(#32, Command)));
if execCommand = EmptyStr then
execCommand := Command;
{ Fix command name }
if ExtractFileExt(execCommand) = EmptyStr then
execCommand := execCommand + '.exe';
{ See if relative path specified }
if not FileExists(execCommand) then
begin
{ Get search path }
srchPath := EmptyStr;
try
with TRegistry.Create(KEY_QUERY_VALUE) do
begin
RootKey := HKEY_LOCAL_MACHINE;
OpenKey('SYSTEM\CurrentControlSet\Control\Session Manager\Environment', False);
srchPath := ReadString('Path');
Free;
end;
finally
end;
{ Get full executable path }
SetLength(execPath, 1024);
SetLength(execPath, SearchPath(PChar(srchPath), PChar(execCommand), nil,
Length(execPath), PChar(execPath), Dummy));
{ Construct back }
if execPath <> EmptyStr then
Command := '"' + execPath + '"' + Copy(Command, Pos(#32, Command), MAXINT);
end;
end;
{ Fill in security info }
with Security do
begin
nLength := SizeOf(Security);
bInheritHandle := True;
end;
{ Fill in security descriptor }
InitializeSecurityDescriptor(@Descriptor, SECURITY_DESCRIPTOR_REVISION);
SetSecurityDescriptorDacl(@Descriptor, True, nil, False);
Security.lpSecurityDescriptor := @Descriptor;
{ Ensure our pipes will be unique system-wide for each command }
sUUID := GenerateUUID;
{ Create reading (stdout) named pipe }
g_hChildStd_OUT_Rd_Tmp := CreateNamedPipe(PChar('\\.\pipe\read ' + sUUID),
PIPE_ACCESS_INBOUND, PIPE_READMODE_BYTE or PIPE_WAIT,
1, 4096, 4096, 0, @Security
);
g_hChildStd_OUT_Wr := CreateFile(PChar('\\.\pipe\read ' + sUUID),
{FILE_WRITE_DATA}2 or SYNCHRONIZE, 0, @Security,
OPEN_EXISTING, // !important
FILE_ATTRIBUTE_NORMAL, 0
);
{ We are going to erdirect error messages to the same output }
DuplicateHandle(GetCurrentProcess, g_hChildStd_OUT_Wr, GetCurrentProcess,
@g_hChildStd_Err, 0, True, DUPLICATE_SAME_ACCESS);
{ Create writing (stdin) named pipe }
g_hChildStd_IN_Rd := CreateNamedPipe(PChar('\\.\pipe\write ' + sUUID),
PIPE_ACCESS_INBOUND, PIPE_TYPE_BYTE or PIPE_READMODE_BYTE or PIPE_WAIT,
1, 4096, 4096, 0, @Security
);
g_hChildStd_IN_Wr_Tmp := CreateFile(PChar('\\.\pipe\write ' + sUUID),
{FILE_WRITE_DATA}2 or SYNCHRONIZE, 0, @Security,
OPEN_EXISTING, // !important
FILE_ATTRIBUTE_NORMAL, 0
);
{ Duplicate handles as says MSDN }
DuplicateHandle(GetCurrentProcess, g_hChildStd_OUT_Rd_Tmp, GetCurrentProcess,
@g_hChildStd_OUT_Rd, 0, False, DUPLICATE_SAME_ACCESS);
DuplicateHandle(GetCurrentProcess, g_hChildStd_IN_Wr_Tmp, GetCurrentProcess,
@g_hChildStd_IN_Wr, 0, False, DUPLICATE_SAME_ACCESS);
{ Close orphaned handles }
CloseHandle(g_hChildStd_OUT_Rd_Tmp);
CloseHandle(g_hChildStd_IN_Wr_Tmp);
{ Allow access to our pipes for child process for some obscure case }
if Length(Env) > 0 then
Env := Env + #26;
Env := Env + 'LETTERPRESS_STDOUT_PIPE=\\.\pipe\read ' + sUUID + #26 +
'LETTERPRESS_STDIN_PIPE=\\.\pipe\write ' + sUUID;
{ Create monitor pipes }
if not SetSystem.SetAlwaysPresentInputPrompt then
begin
{ Create monitor pipe1. This is for injected DLL.
One monitoring pipe isn't enough because once we connect it on client side
it becomes unavaialbe for other connections. I have no fucking idea why.
It's Windows }
g_hChildStd_Inf1 := CreateNamedPipe(PChar('\\.\pipe\monitor1 ' + sUUID),
PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE or PIPE_READMODE_BYTE or PIPE_WAIT,
1, 4096, 4096, 0, nil
);
{ Create monitor pipe2. This is for manual operation inside child }
g_hChildStd_Inf2 := CreateNamedPipe(PChar('\\.\pipe\monitor2 ' + sUUID),
PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE or PIPE_READMODE_BYTE or PIPE_WAIT,
1, 4096, 4096, 0, nil
);
{ Allow access to our pipes for child process for some obscure case }
if Length(Env) > 0 then
Env := Env + #26;
Env := Env + 'LETTERPRESS_MONITOR_DLL_PIPE=\\.\pipe\monitor1 ' + sUUID + #26 +
'LETTERPRESS_MONITOR_PIPE=\\.\pipe\monitor2 ' + sUUID;
end
else begin
g_hChildStd_Inf1 := INVALID_HANDLE_VALUE;
g_hChildStd_Inf2 := INVALID_HANDLE_VALUE;
end;
{ Try to create child process }
Result := CreateChildProcess;
end;
{ TOutputCommandThread }
// -----------------------------------------------------------------------------
constructor TOutputCommandThread.Create(CreateSuspended, WaitForCompleteOutput,
FreeOnTerm: Boolean; OutStream, Process: THandle);
begin
{ Prepare }
inherited Create(CreateSuspended);
FreeOnTerminate := FreeOnTerm;
{ Setup input / output streams }
fOutputStream := OutStream;
fProcess := Process;
{ Need to flush only complete results? }
fWaitForCompleteOutput := WaitForCompleteOutput;
end;
// -----------------------------------------------------------------------------
procedure TOutputCommandThread.Execute;
var
chBuf: RawByteString;
dwRead, dwReaded, dwWritten: Cardinal;
bSuccess: Boolean;
{ Finalize accumulating buffer }
procedure QuitExecution;
begin
if fWaitForCompleteOutput then
begin
SetLength(fOutputAccumulator, fAccumulated);
Synchronize(OutputCompleted);
end;
end;
begin
{ Prepare accumulator }
if fWaitForCompleteOutput then
begin
fAccumulated := 0;
SetLength(fOutputAccumulator, nBufferSize);
end;
{ Read output from started process }
try
{ Do it while possible }
while True do
begin
{ Check if there's information to read from the pipe }
dwRead := 0;
bSuccess := PeekNamedPipe(fOutputStream, nil, 0, nil, @dwRead, nil);
if not bSuccess then
begin
{$IFDEF DEBUG}
WriteLn('STDOUT: Output pipe died. ', SysErrorMessage(GetLastError), ' => ', IntToStr(GetLastError));
{$ENDIF}
QuitExecution;
Terminate;
Break;
end
{ Child thread is either busy or awaiting for input }
else if dwRead = 0 then
begin
Sleep(nDelayTime);
Continue;
end
{ Read }
else begin
{ Prepare buffer and read }
SetLength(chBuf, dwRead);
bSuccess := ReadFile(fOutputStream, chBuf[1], dwRead, dwReaded, nil);
{ What's wrong? }
if not bSuccess then
begin
{ Flush }
QuitExecution;
{ Some reading error? }
if GetLastError <> ERROR_BROKEN_PIPE then
begin
Terminate;
Exit;
end
{ 109 is pipe end, normal behaviour }
else
QuitExecution;
{ Exit from loop }
Break;
end
{ Output read data }
else begin
{ Accumulate data }
if fWaitForCompleteOutput then
begin
if Length(fOutputAccumulator) < Integer(dwReaded) then
SetLength(fOutputAccumulator, dwRead * 2);
Move(chBuf[1], fOutputAccumulator[Succ(fAccumulated)], dwReaded);
Inc(fAccumulated, dwReaded);
end
{ Output immidiately }
else begin
fOutputBuffer := Copy(chBuf, 1, dwReaded);
Synchronize(FlushOutput);
end;
end;
end;
end;
{ Free buffer }
finally
SetLength(chBuf, 0);
end;
{ Wait for process to finish. At this stage both pipes are dead }
dwRead := 0;
bSuccess := GetExitCodeProcess(fProcess, dwRead);
if bSuccess then
begin
{ Still running? }
if dwRead = STILL_ACTIVE then
begin
dwRead := 0;
WaitForSingleObject(fProcess, INFINITE);
{bSuccess := }GetExitCodeProcess(fProcess, dwRead);
end;
end;
{ Done }
fExitCode := dwRead;
Synchronize(ProcessFinished);
end;
procedure TOutputCommandThread.FlushOutput;
begin
if fWaitForCompleteOutput then Exit;
if Assigned(fOnOutputBufferReady) then
fOnOutputBufferReady(Self, fOutputBuffer);
end;
procedure TOutputCommandThread.OutputCompleted;
begin
if fWaitForCompleteOutput and Assigned(fOnOutputCompleted) then
fOnOutputCompleted(Self, fOutputAccumulator);
end;
procedure TOutputCommandThread.ProcessFinished;
begin
if Assigned(fOnOutputProcessFinished) then
fOnOutputProcessFinished(Self, fExitCode);
end;
{ TInputCommandThread }
// -----------------------------------------------------------------------------
constructor TInputCommandThread.Create(CreateSuspended, FreeOnTerm: Boolean;
InStream, MonStream1, MonStream2, Process: THandle;
const InitialInput: RawByteString);
begin
{ Initialize }
inherited Create(CreateSuspended);
FreeOnTerminate := FreeOnTerm;
fHandleClosed := False;
{ Assign handles }
fInputStream := InStream;
fMonitorStream1 := MonStream1;
fMonitorStream2 := MonStream2;
fProcess := Process;
{ Prepare input data }
fInitialInput := InitialInput;
fWriteInitialInput := fInitialInput <> EmptyAnsiStr;
New(fInput);
end;
destructor TInputCommandThread.Destroy;
begin
Dispose(fInput);
inherited Destroy;
end;
// -----------------------------------------------------------------------------
procedure TInputCommandThread.Execute;
var
dwRead, dwReaded, dwWritten: Cardinal;
bSuccess: Boolean;
chBuff: RawByteString;
Hnd: THandle;
procedure DoWriteInput;
begin
{ Need to write initial input? }
if fWriteInitialInput then
begin
{ Initial input written }
fWriteInitialInput := False;
{ Do buffered write }
dwReaded := 0;
while dwReaded < Length(fInitialInput) do
begin
{ Write what's left }
if Length(fInitialInput) - dwReaded >= nBufferSize then
dwRead := nBufferSize
else
dwRead := Length(fInitialInput) - dwReaded;
{ Do write }
if not WriteFile(fInputStream, fInitialInput[1], dwRead, dwWritten, nil) then
begin
Terminate;
Break;
end;
{ Inc step }
Inc(dwReaded, dwWritten);
end;
{ Finalize input buffer }
SetLength(fInitialInput, 0);
try
Synchronize(SetHandleClosed);
CloseHandle(fInputStream);
fInputStream := INVALID_HANDLE_VALUE;
finally
end;
Terminate;
end
{ Input stream dead? }
else if fInputStream = INVALID_HANDLE_VALUE then
Terminate
{ Don't need initial input, simple open input prompt }
else
Synchronize(InputRequest);
end;
begin
{ Wait for client to connect }
if not SetSystem.SetAlwaysPresentInputPrompt then
if fMonitorStream1 <> INVALID_HANDLE_VALUE then
ConnectNamedPipe(fMonitorStream1, nil)
else if fMonitorStream2 <> INVALID_HANDLE_VALUE then
ConnectNamedPipe(fMonitorStream2, nil)
else begin
Terminate;
Exit;
end;
{ Wait for input until pipe is destroyed / abandoned / etc }
try
while True do
begin
{ Terminated? }
if Terminated then
Exit;
{ Awaiting input? }
if fAwaitingInput then
begin
Sleep(nDelayTime);
Continue;
end;
{ Canceled? }
if fCanceled then
begin
Terminate;
Exit;
end;
{ Input ready? }
if fInputReady then
begin
Synchronize(InputRequest);
Continue;
end;
{ Choose the way we are working }
if SetSystem.SetAlwaysPresentInputPrompt then
begin
{ Wait for pipe to signal }
dwRead := WaitForSingleObject(fInputStream, INFINITE);
{ Terminate on bad return }
if (dwRead = WAIT_FAILED) or (dwRead = WAIT_ABANDONED) or
(dwRead = WAIT_TIMEOUT) then
begin
Terminate;
Exit;
end;
{ Do write operation }
DoWriteInput;
end
else begin
{ Check monitor pipe }
dwRead := 0;
if fMonitorStream1 <> ERROR_INVALID_HANDLE then
Hnd := fMonitorStream1
else
Hnd := fMonitorStream2;
bSuccess := PeekNamedPipe(Hnd, nil, 0, nil, @dwRead, nil);
{ Try to read from alternative monitor pipe }
if bSuccess and (dwRead = 0) and (Hnd = fMonitorStream1) then
begin
{ Check second monitor pipe }
Hnd := fMonitorStream2;
bSuccess := PeekNamedPipe(Hnd, nil, 0, nil, @dwRead, nil);
{ It might be closed }
if not bSuccess then
begin
bSuccess := True;
Hnd := fMonitorStream1;
end;
end;
{ Both monitor pipes are dead. No reason for us to exist anymore }
if not bSuccess then
begin
Terminate;
Exit;
end
{ No input request so far? }
else if dwRead = 0 then
begin
Sleep(nDelayTime);
Continue;
end
{ Input requested }
else begin
{ Read that byte of data to make PeekNamedPipe() stop reporting it
and report only on next written byte }
dwReaded := 0;
SetLength(chBuff, dwRead);
ReadFile(Hnd, chBuff[1], dwRead, dwReaded, nil);
{ Small delay }
Sleep(nDelayTime);
{ Do write operation }
DoWriteInput;
end;
end;
end;
finally
SetLength(chBuff, 0);
end;
end;
// -----------------------------------------------------------------------------
procedure TInputCommandThread.InputRequest;
var
bEOF: Boolean;
nWritten: Cardinal;
begin
{ Wait for user input }
if not fInputReady then
if Assigned(fOnInputRequested) then
begin
fInputReady := False;
fCanceled := False;
fAwaitingInput := True;
SetLength(fInput^, 0);
fOnInputRequested(Self, fInput);
Exit;
end
else
Terminate;
{ Write to stream }
if fInputReady then
begin
fInputReady := False;
if Length(fInput^) = 0 then
Terminate
else begin
{ See if EOD char has been passed }
bEOF := fInput^[Length(fInput^)] = #26;