aboutsummaryrefslogtreecommitdiffstats
path: root/util/system/shellcommand.cpp
blob: b712321c1099e72d2df41428070501b2d2af4f2a (plain) (blame)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
#include "shellcommand.h"
#include "user.h"
#include "nice.h"
#include "sigset.h"
#include "atomic.h"

#include <util/folder/dirut.h>
#include <util/generic/algorithm.h>
#include <util/generic/buffer.h>
#include <util/generic/vector.h>
#include <util/generic/yexception.h>
#include <util/memory/tempbuf.h>
#include <util/network/socket.h>
#include <util/stream/pipe.h>
#include <util/stream/str.h>
#include <util/string/cast.h>
#include <util/system/info.h>

#include <errno.h>

#if defined(_unix_)
    #include <unistd.h>
    #include <fcntl.h>
    #include <grp.h>
    #include <sys/wait.h>

using TPid = pid_t;
using TWaitResult = pid_t;
using TExitStatus = int;
    #define WAIT_PROCEED 0

    #if defined(_darwin_)
using TGetGroupListGid = int;
    #else
using TGetGroupListGid = gid_t;
    #endif
#elif defined(_win_)
    #include <string>

    #include "winint.h"

using TPid = HANDLE;
using TWaitResult = DWORD;
using TExitStatus = DWORD;
    #define WAIT_PROCEED WAIT_TIMEOUT

    #pragma warning(disable : 4296) // 'wait_result >= WAIT_OBJECT_0' : expression is always tru
#else
    #error("unknown os, shell command is not implemented")
#endif

#define DBG(stmt) \
    {}
// #define DBG(stmt) stmt

namespace {
    constexpr static size_t DATA_BUFFER_SIZE = 128 * 1024;

#if defined(_unix_)
    void SetUserGroups(const passwd* pw) {
        int ngroups = 1;
        THolder<gid_t, TFree> groups = THolder<gid_t, TFree>(static_cast<gid_t*>(malloc(ngroups * sizeof(gid_t))));
        if (getgrouplist(pw->pw_name, pw->pw_gid, reinterpret_cast<TGetGroupListGid*>(groups.Get()), &ngroups) == -1) {
            groups.Reset(static_cast<gid_t*>(malloc(ngroups * sizeof(gid_t))));
            if (getgrouplist(pw->pw_name, pw->pw_gid, reinterpret_cast<TGetGroupListGid*>(groups.Get()), &ngroups) == -1) {
                ythrow TSystemError() << "getgrouplist failed: user " << pw->pw_name << " (" << pw->pw_uid << ")";
            }
        }
        if (setgroups(ngroups, groups.Get()) == -1) {
            ythrow TSystemError(errno) << "Unable to set groups for user " << pw->pw_name << Endl;
        }
    }

    void ImpersonateUser(const TShellCommandOptions::TUserOptions& userOpts) {
        if (GetUsername() == userOpts.Name) {
            return;
        }
        const passwd* newUser = getpwnam(userOpts.Name.c_str());
        if (!newUser) {
            ythrow TSystemError(errno) << "getpwnam failed";
        }
        if (userOpts.UseUserGroups) {
            SetUserGroups(newUser);
        }
        if (setuid(newUser->pw_uid)) {
            ythrow TSystemError(errno) << "setuid failed";
        }
    }
#elif defined(_win_)
    constexpr static size_t MAX_COMMAND_LINE = 32 * 1024;

    std::wstring GetWString(const char* astring) {
        if (!astring)
            return std::wstring();

        std::string str(astring);
        return std::wstring(str.begin(), str.end());
    }

    std::string GetAString(const wchar_t* wstring) {
        if (!wstring)
            return std::string();

        std::wstring str(wstring);
        return std::string(str.begin(), str.end());
    }
#endif
}

// temporary measure to avoid rewriting all poll calls on win TPipeHandle
#if defined(_win_)
using REALPIPEHANDLE = HANDLE;
    #define INVALID_REALPIPEHANDLE INVALID_HANDLE_VALUE

class TRealPipeHandle
    : public TNonCopyable {
public:
    inline TRealPipeHandle() noexcept
        : Fd_(INVALID_REALPIPEHANDLE)
    {
    }

    inline TRealPipeHandle(REALPIPEHANDLE fd) noexcept
        : Fd_(fd)
    {
    }

    inline ~TRealPipeHandle() {
        Close();
    }

    bool Close() noexcept {
        bool ok = true;
        if (Fd_ != INVALID_REALPIPEHANDLE)
            ok = CloseHandle(Fd_);
        Fd_ = INVALID_REALPIPEHANDLE;
        return ok;
    }

    inline REALPIPEHANDLE Release() noexcept {
        REALPIPEHANDLE ret = Fd_;
        Fd_ = INVALID_REALPIPEHANDLE;
        return ret;
    }

    inline void Swap(TRealPipeHandle& r) noexcept {
        DoSwap(Fd_, r.Fd_);
    }

    inline operator REALPIPEHANDLE() const noexcept {
        return Fd_;
    }

    inline bool IsOpen() const noexcept {
        return Fd_ != INVALID_REALPIPEHANDLE;
    }

    ssize_t Read(void* buffer, size_t byteCount) const noexcept {
        DWORD doneBytes;
        if (!ReadFile(Fd_, buffer, byteCount, &doneBytes, nullptr))
            return -1;
        return doneBytes;
    }
    ssize_t Write(const void* buffer, size_t byteCount) const noexcept {
        DWORD doneBytes;
        if (!WriteFile(Fd_, buffer, byteCount, &doneBytes, nullptr))
            return -1;
        return doneBytes;
    }

    static void Pipe(TRealPipeHandle& reader, TRealPipeHandle& writer, EOpenMode mode) {
        (void)mode;
        REALPIPEHANDLE fds[2];
        if (!CreatePipe(&fds[0], &fds[1], nullptr /* handles are not inherited */, 0))
            ythrow TFileError() << "failed to create a pipe";
        TRealPipeHandle(fds[0]).Swap(reader);
        TRealPipeHandle(fds[1]).Swap(writer);
    }

private:
    REALPIPEHANDLE Fd_;
};

#else
using TRealPipeHandle = TPipeHandle;
using REALPIPEHANDLE = PIPEHANDLE;
    #define INVALID_REALPIPEHANDLE INVALID_PIPEHANDLE
#endif

class TShellCommand::TImpl
    : public TAtomicRefCount<TShellCommand::TImpl> {
private:
    TPid Pid;
    TString Command;
    TList<TString> Arguments;
    TString WorkDir;
    TAtomic ExecutionStatus; // TShellCommand::ECommandStatus
    TMaybe<int> ExitCode; 
    IInputStream* InputStream;
    IOutputStream* OutputStream;
    IOutputStream* ErrorStream;
    TString CollectedOutput;
    TString CollectedError;
    TString InternalError;
    TThread* WatchThread;
    TMutex TerminateMutex;
    TFileHandle InputHandle;
    TFileHandle OutputHandle;
    TFileHandle ErrorHandle;

    /// @todo: store const TShellCommandOptions, no need for so many vars
    bool TerminateFlag = false;
    bool ClearSignalMask = false;
    bool CloseAllFdsOnExec = false;
    bool AsyncMode = false;
    size_t PollDelayMs = 0;
    bool UseShell = false;
    bool QuoteArguments = false;
    bool DetachSession = false;
    bool CloseStreams = false;
    TAtomic ShouldCloseInput;
    TShellCommandOptions::EHandleMode InputMode = TShellCommandOptions::HANDLE_STREAM;
    TShellCommandOptions::EHandleMode OutputMode = TShellCommandOptions::HANDLE_STREAM;
    TShellCommandOptions::EHandleMode ErrorMode = TShellCommandOptions::HANDLE_STREAM;

    TShellCommandOptions::TUserOptions User;
    THashMap<TString, TString> Environment;
    int Nice = 0;
    std::function<void()> FuncAfterFork = {};

    struct TProcessInfo {
        TImpl* Parent;
        TRealPipeHandle InputFd;
        TRealPipeHandle OutputFd;
        TRealPipeHandle ErrorFd;
        TProcessInfo(TImpl* parent, REALPIPEHANDLE inputFd, REALPIPEHANDLE outputFd, REALPIPEHANDLE errorFd)
            : Parent(parent)
            , InputFd(inputFd)
            , OutputFd(outputFd)
            , ErrorFd(errorFd)
        {
        }
    };

    struct TPipes {
        TRealPipeHandle OutputPipeFd[2];
        TRealPipeHandle ErrorPipeFd[2];
        TRealPipeHandle InputPipeFd[2];
        // pipes are closed by automatic dtor
        void PrepareParents() {
            if (OutputPipeFd[1].IsOpen()) {
                OutputPipeFd[1].Close();
            }
            if (ErrorPipeFd[1].IsOpen()) {
                ErrorPipeFd[1].Close();
            }
            if (InputPipeFd[1].IsOpen()) {
                InputPipeFd[0].Close();
            }
        }
        void ReleaseParents() {
            InputPipeFd[1].Release();
            OutputPipeFd[0].Release();
            ErrorPipeFd[0].Release();
        }
    };

    struct TPipePump {
        TRealPipeHandle* Pipe;
        IOutputStream* OutputStream;
        IInputStream* InputStream;
        TAtomic* ShouldClosePipe;
        TString InternalError;
    };

#if defined(_unix_)
    void OnFork(TPipes& pipes, sigset_t oldmask, char* const* argv, char* const* envp, const std::function<void()>& afterFork) const;
#else
    void StartProcess(TPipes& pipes);
#endif

public:
    inline TImpl(const TStringBuf cmd, const TList<TString>& args, const TShellCommandOptions& options, const TString& workdir)
        : Pid(0)
        , Command(ToString(cmd))
        , Arguments(args)
        , WorkDir(workdir)
        , ExecutionStatus(SHELL_NONE)
        , InputStream(options.InputStream)
        , OutputStream(options.OutputStream)
        , ErrorStream(options.ErrorStream)
        , WatchThread(nullptr)
        , TerminateFlag(false)
        , ClearSignalMask(options.ClearSignalMask)
        , CloseAllFdsOnExec(options.CloseAllFdsOnExec)
        , AsyncMode(options.AsyncMode)
        , PollDelayMs(options.PollDelayMs)
        , UseShell(options.UseShell)
        , QuoteArguments(options.QuoteArguments)
        , DetachSession(options.DetachSession)
        , CloseStreams(options.CloseStreams)
        , ShouldCloseInput(options.ShouldCloseInput)
        , InputMode(options.InputMode)
        , OutputMode(options.OutputMode)
        , ErrorMode(options.ErrorMode)
        , User(options.User)
        , Environment(options.Environment)
        , Nice(options.Nice)
        , FuncAfterFork(options.FuncAfterFork)
    {
        if (InputStream) {
            // TODO change usages to call SetInputStream instead of directly assigning to InputStream
            InputMode = TShellCommandOptions::HANDLE_STREAM;
        }
    }

    inline ~TImpl() {
        if (WatchThread) {
            with_lock (TerminateMutex) {
                TerminateFlag = true;
            }

            delete WatchThread;
        }

#if defined(_win_)
        if (Pid) {
            CloseHandle(Pid);
        }
#endif
    }

    inline void AppendArgument(const TStringBuf argument) {
        if (AtomicGet(ExecutionStatus) == SHELL_RUNNING) {
            ythrow yexception() << "You cannot change command parameters while process is running";
        }
        Arguments.push_back(ToString(argument));
    }

    inline const TString& GetOutput() const {
        if (AtomicGet(ExecutionStatus) == SHELL_RUNNING) {
            ythrow yexception() << "You cannot retrieve output while process is running.";
        }
        return CollectedOutput;
    }

    inline const TString& GetError() const {
        if (AtomicGet(ExecutionStatus) == SHELL_RUNNING) {
            ythrow yexception() << "You cannot retrieve output while process is running.";
        }
        return CollectedError;
    }

    inline const TString& GetInternalError() const {
        if (AtomicGet(ExecutionStatus) != SHELL_INTERNAL_ERROR) {
            ythrow yexception() << "Internal error hasn't occured so can't be retrieved.";
        }
        return InternalError;
    }

    inline ECommandStatus GetStatus() const {
        return static_cast<ECommandStatus>(AtomicGet(ExecutionStatus));
    }

    inline TMaybe<int> GetExitCode() const { 
        return ExitCode; 
    } 
 
    inline TProcessId GetPid() const {
#if defined(_win_)
        return GetProcessId(Pid);
#else
        return Pid;
#endif
    }

    inline TFileHandle& GetInputHandle() {
        return InputHandle;
    }

    inline TFileHandle& GetOutputHandle() {
        return OutputHandle;
    }

    inline TFileHandle& GetErrorHandle() {
        return ErrorHandle;
    }

    // start child process
    void Run();

    inline void Terminate() {
        if (!!Pid && (AtomicGet(ExecutionStatus) == SHELL_RUNNING)) {
            bool ok =
#if defined(_unix_)
                kill(DetachSession ? -1 * Pid : Pid, SIGTERM) == 0;
            if (!ok && (errno == ESRCH) && DetachSession) {
                // this could fail when called before child proc completes setsid().
                ok = kill(Pid, SIGTERM) == 0;
                kill(-Pid, SIGTERM); // between a failed kill(-Pid) and a successful kill(Pid) a grandchild could have been spawned
            }
#else
                TerminateProcess(Pid, 1 /* exit code */);
#endif
            if (!ok) {
                ythrow TSystemError() << "cannot terminate " << Pid;
            }
        }
    }

    inline void Wait() {
        if (WatchThread) {
            WatchThread->Join();
        }
    }

    inline void CloseInput() {
        AtomicSet(ShouldCloseInput, true);
    }

    inline static bool TerminateIsRequired(void* processInfo) {
        TProcessInfo* pi = reinterpret_cast<TProcessInfo*>(processInfo);
        if (!pi->Parent->TerminateFlag) {
            return false;
        }
        pi->InputFd.Close();
        pi->ErrorFd.Close();
        pi->OutputFd.Close();

        if (pi->Parent->CloseStreams) {
            if (pi->Parent->ErrorStream) {
                pi->Parent->ErrorStream->Finish();
            }
            if (pi->Parent->OutputStream) {
                pi->Parent->OutputStream->Finish();
            }
        }

        delete pi;
        return true;
    }

    // interchange io while process is alive
    inline static void Communicate(TProcessInfo* pi);

    inline static void* WatchProcess(void* data) {
        TProcessInfo* pi = reinterpret_cast<TProcessInfo*>(data);
        Communicate(pi);
        return nullptr;
    }

    inline static void* ReadStream(void* data) noexcept {
        TPipePump* pump = reinterpret_cast<TPipePump*>(data);
        try {
            int bytes = 0;
            TBuffer buffer(DATA_BUFFER_SIZE);

            while (true) {
                bytes = pump->Pipe->Read(buffer.Data(), buffer.Capacity());
                if (bytes > 0) {
                    pump->OutputStream->Write(buffer.Data(), bytes);
                } else {
                    break;
                }
            }
            if (pump->Pipe->IsOpen()) {
                pump->Pipe->Close();
            }
        } catch (...) {
            pump->InternalError = CurrentExceptionMessage();
        }
        return nullptr;
    }

    inline static void* WriteStream(void* data) noexcept {
        TPipePump* pump = reinterpret_cast<TPipePump*>(data);
        try {
            int bytes = 0;
            int bytesToWrite = 0;
            char* bufPos = nullptr;
            TBuffer buffer(DATA_BUFFER_SIZE);

            while (true) {
                if (!bytesToWrite) {
                    bytesToWrite = pump->InputStream->Read(buffer.Data(), buffer.Capacity());
                    if (bytesToWrite == 0) {
                        if (AtomicGet(pump->ShouldClosePipe)) {
                            break;
                        }
                        continue;
                    }
                    bufPos = buffer.Data();
                }

                bytes = pump->Pipe->Write(bufPos, bytesToWrite);
                if (bytes > 0) {
                    bytesToWrite -= bytes;
                    bufPos += bytes;
                } else {
                    break;
                }
            }
            if (pump->Pipe->IsOpen()) {
                pump->Pipe->Close();
            }
        } catch (...) {
            pump->InternalError = CurrentExceptionMessage();
        }
        return nullptr;
    }

    TString GetQuotedCommand() const;
};

#if defined(_win_)
void TShellCommand::TImpl::StartProcess(TShellCommand::TImpl::TPipes& pipes) {
    // Setup STARTUPINFO to redirect handles.
    STARTUPINFOW startup_info;
    ZeroMemory(&startup_info, sizeof(startup_info));
    startup_info.cb = sizeof(startup_info);
    startup_info.dwFlags = STARTF_USESTDHANDLES;

    if (OutputMode != TShellCommandOptions::HANDLE_INHERIT) {
        if (!SetHandleInformation(pipes.OutputPipeFd[1], HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT)) {
            ythrow TSystemError() << "cannot set handle info";
        }
    }
    if (ErrorMode != TShellCommandOptions::HANDLE_INHERIT) {
        if (!SetHandleInformation(pipes.ErrorPipeFd[1], HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT)) {
            ythrow TSystemError() << "cannot set handle info";
        }
    }
    if (InputMode != TShellCommandOptions::HANDLE_INHERIT) {
        if (!SetHandleInformation(pipes.InputPipeFd[0], HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT))
            ythrow TSystemError() << "cannot set handle info";
    }

    // A sockets do not work as std streams for some reason
    if (OutputMode != TShellCommandOptions::HANDLE_INHERIT) {
        startup_info.hStdOutput = pipes.OutputPipeFd[1];
    } else {
        startup_info.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
    }
    if (ErrorMode != TShellCommandOptions::HANDLE_INHERIT) {
        startup_info.hStdError = pipes.ErrorPipeFd[1];
    } else {
        startup_info.hStdError = GetStdHandle(STD_ERROR_HANDLE);
    }
    if (InputMode != TShellCommandOptions::HANDLE_INHERIT) {
        startup_info.hStdInput = pipes.InputPipeFd[0];
    } else {
        // Don't leave hStdInput unfilled, otherwise any attempt to retrieve the operating-system file handle
        // that is associated with the specified file descriptor will led to errors.
        startup_info.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
    }

    PROCESS_INFORMATION process_info;
    // TString cmd = "cmd /U" + TUtf16String can be used to read unicode messages from cmd
    // /A - ansi charset /Q - echo off, /C - command, /Q - special quotes
    TString qcmd = GetQuotedCommand();
    TString cmd = UseShell ? "cmd /A /Q /S /C \"" + qcmd + "\"" : qcmd;
    // winapi can modify command text, copy it

    Y_ENSURE_EX(cmd.size() < MAX_COMMAND_LINE, yexception() << "Command is too long (length=" << cmd.size() << ")");
    TTempArray<wchar_t> cmdcopy(MAX_COMMAND_LINE);
    Copy(cmd.data(), cmd.data() + cmd.size(), cmdcopy.Data());
    *(cmdcopy.Data() + cmd.size()) = 0;

    const wchar_t* cwd = NULL;
    std::wstring cwdBuff;
    if (WorkDir.size()) {
        cwdBuff = GetWString(WorkDir.data());
        cwd = cwdBuff.c_str();
    }

    void* lpEnvironment = nullptr;
    TString env;
    if (!Environment.empty()) {
        for (auto e = Environment.begin(); e != Environment.end(); ++e) {
            env += e->first + '=' + e->second + '\0';
        }
        env += '\0';
        lpEnvironment = const_cast<char*>(env.data());
    }

    // disable messagebox (may be in debug too)
    #ifndef NDEBUG
    SetErrorMode(GetErrorMode() | SEM_NOGPFAULTERRORBOX);
    #endif
    BOOL res = 0;
    if (User.Name.empty() || GetUsername() == User.Name) {
        res = CreateProcessW(
            nullptr, // image name
            cmdcopy.Data(),
            nullptr,       // process security attributes
            nullptr,       // thread security attributes
            TRUE,          // inherit handles - needed for IO, CloseAllFdsOnExec not respected
            0,             // obscure creation flags
            lpEnvironment, // environment
            cwd,           // current directory
            &startup_info,
            &process_info);
    } else {
        res = CreateProcessWithLogonW(
            GetWString(User.Name.data()).c_str(),
            nullptr, // domain (if this parameter is NULL, the user name must be specified in UPN format)
            GetWString(User.Password.data()).c_str(),
            0,    // logon flags
            NULL, // image name
            cmdcopy.Data(),
            0,             // obscure creation flags
            lpEnvironment, // environment
            cwd,           // current directory
            &startup_info,
            &process_info);
    }

    if (!res) {
        AtomicSet(ExecutionStatus, SHELL_ERROR);
        /// @todo: write to error stream if set
        TStringOutput out(CollectedError);
        out << "Process was not created: " << LastSystemErrorText() << " command text was: '" << GetAString(cmdcopy.Data()) << "'";
    }
    Pid = process_info.hProcess;
    CloseHandle(process_info.hThread);
    DBG(Cerr << "created process id " << Pid << " in dir: " << cwd << ", cmd: " << cmdcopy.Data() << Endl);
}
#endif

void ShellQuoteArg(TString& dst, TStringBuf argument) {
    dst.append("\"");
    TStringBuf l, r;
    while (argument.TrySplit('"', l, r)) {
        dst.append(l);
        dst.append("\\\"");
        argument = r;
    }
    dst.append(argument);
    dst.append("\"");
}

void ShellQuoteArgSp(TString& dst, TStringBuf argument) {
    dst.append(' ');
    ShellQuoteArg(dst, argument);
}

bool ArgNeedsQuotes(TStringBuf arg) noexcept {
    if (arg.empty()) {
        return true;
    }
    return arg.find_first_of(" \"\'\t&()*<>\\`^|") != TString::npos;
}

TString TShellCommand::TImpl::GetQuotedCommand() const {
    TString quoted = Command; /// @todo command itself should be quoted too
    for (const auto& argument : Arguments) {
        // Don't add unnecessary quotes. It's especially important for the windows with a 32k command line length limit.
        if (QuoteArguments && ArgNeedsQuotes(argument)) {
            ::ShellQuoteArgSp(quoted, argument);
        } else {
            quoted.append(" ").append(argument);
        }
    }
    return quoted;
}

#if defined(_unix_)
void TShellCommand::TImpl::OnFork(TPipes& pipes, sigset_t oldmask, char* const* argv, char* const* envp, const std::function<void()>& afterFork) const {
    try {
        if (DetachSession) {
            setsid();
        }

        // reset signal handlers from parent
        struct sigaction sa;
        sa.sa_handler = SIG_DFL;
        sa.sa_flags = 0;
        SigEmptySet(&sa.sa_mask);
        for (int i = 0; i < NSIG; ++i) {
            // some signals cannot be caught, so just ignore return value
            sigaction(i, &sa, nullptr);
        }
        if (ClearSignalMask) {
            SigEmptySet(&oldmask);
        }
        // clear / restore signal mask
        if (SigProcMask(SIG_SETMASK, &oldmask, nullptr) != 0) {
            ythrow TSystemError() << "Cannot " << (ClearSignalMask ? "clear" : "restore") << " signal mask in child";
        }

        TFileHandle sIn(0);
        TFileHandle sOut(1);
        TFileHandle sErr(2);
        if (InputMode != TShellCommandOptions::HANDLE_INHERIT) {
            pipes.InputPipeFd[1].Close();
            TFileHandle sInNew(pipes.InputPipeFd[0]);
            sIn.LinkTo(sInNew);
            sIn.Release();
            sInNew.Release();
        } else {
            // do not close fd 0 - next open will return it and confuse all readers
            /// @todo in case of real need - reopen /dev/null
        }
        if (OutputMode != TShellCommandOptions::HANDLE_INHERIT) {
            pipes.OutputPipeFd[0].Close();
            TFileHandle sOutNew(pipes.OutputPipeFd[1]);
            sOut.LinkTo(sOutNew);
            sOut.Release();
            sOutNew.Release();
        }
        if (ErrorMode != TShellCommandOptions::HANDLE_INHERIT) {
            pipes.ErrorPipeFd[0].Close();
            TFileHandle sErrNew(pipes.ErrorPipeFd[1]);
            sErr.LinkTo(sErrNew);
            sErr.Release();
            sErrNew.Release();
        }

        if (WorkDir.size()) {
            NFs::SetCurrentWorkingDirectory(WorkDir);
        }

        if (CloseAllFdsOnExec) {
            for (int fd = NSystemInfo::MaxOpenFiles(); fd > STDERR_FILENO; --fd) {
                fcntl(fd, F_SETFD, FD_CLOEXEC);
            }
        }

        if (!User.Name.empty()) {
            ImpersonateUser(User);
        }

        if (Nice) {
            // Don't verify Nice() call - it does not work properly with WSL https://github.com/Microsoft/WSL/issues/1838
            ::Nice(Nice);
        }
        if (afterFork) {
            afterFork();
        }

        if (envp == nullptr) {
            execvp(argv[0], argv);
        } else {
            execve(argv[0], argv, envp);
        }
        Cerr << "Process was not created: " << LastSystemErrorText() << Endl;
    } catch (const std::exception& error) {
        Cerr << "Process was not created: " << error.what() << Endl;
    } catch (...) {
        Cerr << "Process was not created: "
             << "unknown error" << Endl;
    }

    _exit(-1);
}
#endif

void TShellCommand::TImpl::Run() {
    Y_ENSURE(AtomicGet(ExecutionStatus) != SHELL_RUNNING, TStringBuf("Process is already running"));
    // Prepare I/O streams
    CollectedOutput.clear();
    CollectedError.clear();
    TPipes pipes;

    if (OutputMode != TShellCommandOptions::HANDLE_INHERIT) {
        TRealPipeHandle::Pipe(pipes.OutputPipeFd[0], pipes.OutputPipeFd[1], CloseOnExec);
    }
    if (ErrorMode != TShellCommandOptions::HANDLE_INHERIT) {
        TRealPipeHandle::Pipe(pipes.ErrorPipeFd[0], pipes.ErrorPipeFd[1], CloseOnExec);
    }
    if (InputMode != TShellCommandOptions::HANDLE_INHERIT) {
        TRealPipeHandle::Pipe(pipes.InputPipeFd[0], pipes.InputPipeFd[1], CloseOnExec);
    }

    AtomicSet(ExecutionStatus, SHELL_RUNNING);

#if defined(_unix_)
    // block all signals to avoid signal handler race after fork()
    sigset_t oldmask, newmask;
    SigFillSet(&newmask);
    if (SigProcMask(SIG_SETMASK, &newmask, &oldmask) != 0) {
        ythrow TSystemError() << "Cannot block all signals in parent";
    }

    /* arguments holders */
    TString shellArg;
    TVector<char*> qargv;
    /*
      Following "const_cast"s are safe:
      http://pubs.opengroup.org/onlinepubs/9699919799/functions/exec.html
    */
    if (UseShell) {
        shellArg = GetQuotedCommand();
        qargv.reserve(4);
        qargv.push_back(const_cast<char*>("/bin/sh"));
        qargv.push_back(const_cast<char*>("-c"));
        // two args for 'sh -c -- ',
        // one for program name, and one for NULL at the end
        qargv.push_back(const_cast<char*>(shellArg.data()));
    } else {
        qargv.reserve(Arguments.size() + 2);
        qargv.push_back(const_cast<char*>(Command.data()));
        for (auto& i : Arguments) {
            qargv.push_back(const_cast<char*>(i.data()));
        }
    }

    qargv.push_back(nullptr);

    TVector<TString> envHolder;
    TVector<char*> envp;
    if (!Environment.empty()) {
        for (auto& env : Environment) {
            envHolder.emplace_back(env.first + '=' + env.second);
            envp.push_back(const_cast<char*>(envHolder.back().data()));
        }
        envp.push_back(nullptr);
    }

    pid_t pid = fork();
    if (pid == -1) {
        AtomicSet(ExecutionStatus, SHELL_ERROR);
        /// @todo check if pipes are still open
        ythrow TSystemError() << "Cannot fork";
    } else if (pid == 0) { // child
        if (envp.size() != 0) {
            OnFork(pipes, oldmask, qargv.data(), envp.data(), FuncAfterFork);
        } else {
            OnFork(pipes, oldmask, qargv.data(), nullptr, FuncAfterFork);
        }
    } else { // parent
        // restore signal mask
        if (SigProcMask(SIG_SETMASK, &oldmask, nullptr) != 0) {
            ythrow TSystemError() << "Cannot restore signal mask in parent";
        }
    }
    Pid = pid;
#else
    StartProcess(pipes);
#endif
    pipes.PrepareParents();

    if (AtomicGet(ExecutionStatus) != SHELL_RUNNING) {
        return;
    }

    if (InputMode == TShellCommandOptions::HANDLE_PIPE) {
        TFileHandle inputHandle(pipes.InputPipeFd[1].Release());
        InputHandle.Swap(inputHandle);
    }

    if (OutputMode == TShellCommandOptions::HANDLE_PIPE) {
        TFileHandle outputHandle(pipes.OutputPipeFd[0].Release());
        OutputHandle.Swap(outputHandle);
    }

    if (ErrorMode == TShellCommandOptions::HANDLE_PIPE) {
        TFileHandle errorHandle(pipes.ErrorPipeFd[0].Release());
        ErrorHandle.Swap(errorHandle);
    }

    TProcessInfo* processInfo = new TProcessInfo(this,
                                                 pipes.InputPipeFd[1].Release(), pipes.OutputPipeFd[0].Release(), pipes.ErrorPipeFd[0].Release());
    if (AsyncMode) {
        WatchThread = new TThread(&TImpl::WatchProcess, processInfo);
        WatchThread->Start();
        /// @todo wait for child to start its process session (if options.Detach)
    } else {
        Communicate(processInfo);
    }

    pipes.ReleaseParents(); // not needed
}

void TShellCommand::TImpl::Communicate(TProcessInfo* pi) {
    THolder<IOutputStream> outputHolder;
    IOutputStream* output = pi->Parent->OutputStream;
    if (!output) {
        outputHolder.Reset(output = new TStringOutput(pi->Parent->CollectedOutput));
    }

    THolder<IOutputStream> errorHolder;
    IOutputStream* error = pi->Parent->ErrorStream;
    if (!error) {
        errorHolder.Reset(error = new TStringOutput(pi->Parent->CollectedError));
    }

    IInputStream*& input = pi->Parent->InputStream;

#if defined(_unix_)
    // not really needed, io is done via poll
    if (pi->OutputFd.IsOpen()) {
        SetNonBlock(pi->OutputFd);
    }
    if (pi->ErrorFd.IsOpen()) {
        SetNonBlock(pi->ErrorFd);
    }
    if (pi->InputFd.IsOpen()) {
        SetNonBlock(pi->InputFd);
    }
#endif

    try {
#if defined(_win_)
        TPipePump pumps[3] = {0};
        pumps[0] = {&pi->ErrorFd, error};
        pumps[1] = {&pi->OutputFd, output};

        TVector<THolder<TThread>> streamThreads;
        streamThreads.emplace_back(new TThread(&TImpl::ReadStream, &pumps[0]));
        streamThreads.emplace_back(new TThread(&TImpl::ReadStream, &pumps[1]));

        if (input) {
            pumps[2] = {&pi->InputFd, nullptr, input, &pi->Parent->ShouldCloseInput};
            streamThreads.emplace_back(new TThread(&TImpl::WriteStream, &pumps[2]));
        }

        for (auto& threadHolder : streamThreads)
            threadHolder->Start();
#else
        TBuffer buffer(DATA_BUFFER_SIZE);
        TBuffer inputBuffer(DATA_BUFFER_SIZE);
        int bytes;
        int bytesToWrite = 0;
        char* bufPos = nullptr;
#endif
        TWaitResult waitPidResult;
        TExitStatus status = 0;

        while (true) {
            {
                with_lock (pi->Parent->TerminateMutex) {
                    if (TerminateIsRequired(pi)) {
                        return;
                    }
                }

                waitPidResult =
#if defined(_unix_)
                    waitpid(pi->Parent->Pid, &status, WNOHANG);
#else
                    WaitForSingleObject(pi->Parent->Pid /* process_info.hProcess */, pi->Parent->PollDelayMs /* ms */);
                Y_UNUSED(status);
#endif
                // DBG(Cerr << "wait result: " << waitPidResult << Endl);
                if (waitPidResult != WAIT_PROCEED) {
                    break;
                }
            }
/// @todo factor out (poll + wfmo)
#if defined(_unix_)
            bool haveIn = false;
            bool haveOut = false;
            bool haveErr = false;

            if (!input && pi->InputFd.IsOpen()) {
                DBG(Cerr << "closing input stream..." << Endl);
                pi->InputFd.Close();
            }
            if (!output && pi->OutputFd.IsOpen()) {
                DBG(Cerr << "closing output stream..." << Endl);
                pi->OutputFd.Close();
            }
            if (!error && pi->ErrorFd.IsOpen()) {
                DBG(Cerr << "closing error stream..." << Endl);
                pi->ErrorFd.Close();
            }

            if (!input && !output && !error) {
                continue;
            }

            struct pollfd fds[] = {
                {REALPIPEHANDLE(pi->InputFd), POLLOUT, 0},
                {REALPIPEHANDLE(pi->OutputFd), POLLIN, 0},
                {REALPIPEHANDLE(pi->ErrorFd), POLLIN, 0}};
            int res;

            if (!input) {
                fds[0].events = 0;
            }
            if (!output) {
                fds[1].events = 0;
            }
            if (!error) {
                fds[2].events = 0;
            }

            res = PollD(fds, 3, TInstant::Now() + TDuration::MilliSeconds(pi->Parent->PollDelayMs));
            // DBG(Cerr << "poll result: " << res << Endl);
            if (-res == ETIMEDOUT || res == 0) {
                // DBG(Cerr << "poll again..." << Endl);
                continue;
            }
            if (res < 0) {
                ythrow yexception() << "poll failed: " << LastSystemErrorText();
            }

            if ((fds[1].revents & POLLIN) == POLLIN) {
                haveOut = true;
            } else if (fds[1].revents & (POLLERR | POLLHUP)) {
                output = nullptr;
            }

            if ((fds[2].revents & POLLIN) == POLLIN) {
                haveErr = true;
            } else if (fds[2].revents & (POLLERR | POLLHUP)) {
                error = nullptr;
            }

            if (input && ((fds[0].revents & POLLOUT) == POLLOUT)) {
                haveIn = true;
            }

            if (haveOut) {
                bytes = pi->OutputFd.Read(buffer.Data(), buffer.Capacity());
                DBG(Cerr << "transferred " << bytes << " bytes of output" << Endl);
                if (bytes > 0) {
                    output->Write(buffer.Data(), bytes);
                } else {
                    output = nullptr;
                }
            }
            if (haveErr) {
                bytes = pi->ErrorFd.Read(buffer.Data(), buffer.Capacity());
                DBG(Cerr << "transferred " << bytes << " bytes of error" << Endl);
                if (bytes > 0) {
                    error->Write(buffer.Data(), bytes);
                } else {
                    error = nullptr;
                }
            }

            if (haveIn) {
                if (!bytesToWrite) {
                    bytesToWrite = input->Read(inputBuffer.Data(), inputBuffer.Capacity());
                    if (bytesToWrite == 0) {
                        if (AtomicGet(pi->Parent->ShouldCloseInput)) {
                            input = nullptr;
                        }
                        continue;
                    }
                    bufPos = inputBuffer.Data();
                }

                bytes = pi->InputFd.Write(bufPos, bytesToWrite);
                if (bytes > 0) {
                    bytesToWrite -= bytes;
                    bufPos += bytes;
                } else {
                    input = nullptr;
                }

                DBG(Cerr << "transferred " << bytes << " bytes of input" << Endl);
            }
#endif
        }
        DBG(Cerr << "process finished" << Endl);

        // What's the reason of process exit.
        // We need to set exit code before waiting for input thread
        // Otherwise there is no way for input stream provider to discover
        // that process has exited and stream shouldn't wait for new data.
        bool cleanExit = false;
        TMaybe<int> processExitCode; 
#if defined(_unix_)
        processExitCode = WEXITSTATUS(status); 
        if (WIFEXITED(status) && processExitCode == 0) {
            cleanExit = true;
        } else if (WIFSIGNALED(status)) {
            processExitCode = -WTERMSIG(status);
        }
#else
        if (waitPidResult == WAIT_OBJECT_0) {
            DWORD exitCode = STILL_ACTIVE;
            if (!GetExitCodeProcess(pi->Parent->Pid, &exitCode)) {
                ythrow yexception() << "GetExitCodeProcess: " << LastSystemErrorText();
            }
            if (exitCode == 0)
                cleanExit = true;
            processExitCode = static_cast<int>(exitCode); 
            DBG(Cerr << "exit code: " << exitCode << Endl);
        }
#endif
        pi->Parent->ExitCode = processExitCode; 
        if (cleanExit) {
            AtomicSet(pi->Parent->ExecutionStatus, SHELL_FINISHED);
        } else {
            AtomicSet(pi->Parent->ExecutionStatus, SHELL_ERROR);
        }

#if defined(_win_)
        for (auto& threadHolder : streamThreads)
            threadHolder->Join();
        for (const auto pump : pumps) {
            if (!pump.InternalError.empty())
                throw yexception() << pump.InternalError;
        }
#else
        // Now let's read remaining stdout/stderr
        while (output && (bytes = pi->OutputFd.Read(buffer.Data(), buffer.Capacity())) > 0) {
            DBG(Cerr << bytes << " more bytes of output: " << Endl);
            output->Write(buffer.Data(), bytes);
        }
        while (error && (bytes = pi->ErrorFd.Read(buffer.Data(), buffer.Capacity())) > 0) {
            DBG(Cerr << bytes << " more bytes of error" << Endl);
            error->Write(buffer.Data(), bytes);
        }
#endif
    } catch (const yexception& e) {
        // Some error in watch occured, set result to error
        AtomicSet(pi->Parent->ExecutionStatus, SHELL_INTERNAL_ERROR);
        pi->Parent->InternalError = e.what();
        if (input) {
            pi->InputFd.Close();
        }
        Cdbg << "shell command internal error: " << pi->Parent->InternalError << Endl;
    }
    // Now we can safely delete process info struct and other data
    pi->Parent->TerminateFlag = true;
    TerminateIsRequired(pi);
}

TShellCommand::TShellCommand(const TStringBuf cmd, const TList<TString>& args, const TShellCommandOptions& options,
                             const TString& workdir)
    : Impl(new TImpl(cmd, args, options, workdir))
{
}

TShellCommand::TShellCommand(const TStringBuf cmd, const TShellCommandOptions& options, const TString& workdir)
    : Impl(new TImpl(cmd, TList<TString>(), options, workdir))
{
}

TShellCommand::~TShellCommand() = default;

TShellCommand& TShellCommand::operator<<(const TStringBuf argument) {
    Impl->AppendArgument(argument);
    return *this;
}

const TString& TShellCommand::GetOutput() const {
    return Impl->GetOutput();
}

const TString& TShellCommand::GetError() const {
    return Impl->GetError();
}

const TString& TShellCommand::GetInternalError() const {
    return Impl->GetInternalError();
}

TShellCommand::ECommandStatus TShellCommand::GetStatus() const {
    return Impl->GetStatus();
}

TMaybe<int> TShellCommand::GetExitCode() const { 
    return Impl->GetExitCode(); 
} 
 
TProcessId TShellCommand::GetPid() const {
    return Impl->GetPid();
}

TFileHandle& TShellCommand::GetInputHandle() {
    return Impl->GetInputHandle();
}

TFileHandle& TShellCommand::GetOutputHandle() {
    return Impl->GetOutputHandle();
}

TFileHandle& TShellCommand::GetErrorHandle() {
    return Impl->GetErrorHandle();
}

TShellCommand& TShellCommand::Run() {
    Impl->Run();
    return *this;
}

TShellCommand& TShellCommand::Terminate() {
    Impl->Terminate();
    return *this;
}

TShellCommand& TShellCommand::Wait() {
    Impl->Wait();
    return *this;
}

TShellCommand& TShellCommand::CloseInput() {
    Impl->CloseInput();
    return *this;
}

TString TShellCommand::GetQuotedCommand() const {
    return Impl->GetQuotedCommand();
}