-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCatalystCX.hpp
More file actions
1258 lines (1084 loc) · 42.9 KB
/
Copy pathCatalystCX.hpp
File metadata and controls
1258 lines (1084 loc) · 42.9 KB
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
// SPDX-License-Identifier: GPL-3.0-or-later
// Copyright (c) 2025 assembler-0
// Licensed under GPL-3.0-or-later
/**
* @brief CatalystCX - A cross-platform single-file C++ library/module for executing and managing external processes (or commands).
* @file CatalystCX.hpp
* @version 0.0.1
* @date 20-09-25 (last modified)
* @author assembler-0
*/
#pragma once
#if __cplusplus < 202002L
#error "CatalystCX requires C++20 or later (-std=c++20)"
#define CATALYST_VERSION_CHECK_FAILED
#endif
#ifndef CATALYST_VERSION_CHECK_FAILED
#ifndef CATALYSTCX_HPP
#define CATALYSTCX_HPP
#include <algorithm>
#include <array>
#include <chrono>
#include <concepts>
#include <csignal>
#include <cstring>
#include <filesystem>
#include <future>
#include <optional>
#include <ranges>
#include <sstream>
#include <string>
#include <string_view>
#include <thread>
#include <unordered_map>
#include <variant>
#include <vector>
#ifdef _WIN32
#include <windows.h>
#include <psapi.h>
#include <io.h>
#include <fcntl.h>
#include <processthreadsapi.h>
#else
#include <fcntl.h>
#include <poll.h>
#include <sys/resource.h>
#include <sys/stat.h>
#include <sys/wait.h>
#ifdef __APPLE__
#include <spawn.h>
extern char **environ;
#endif
#endif
#define CatalystCX_VERSION "0.0.1"
#define CatalystCX_VERSION_MAJOR 0
#define CatalystCX_VERSION_MINOR 0
#define CatalystCX_VERSION_PATCH 1
namespace fs = std::filesystem;
// Constants
namespace Constants {
constexpr int EXIT_FAIL_EC = 127;
constexpr size_t PIPE_BUFFER_SIZE = 8192;
constexpr size_t STDERR_BUFFER_SIZE = 4096;
constexpr int POLL_TIMEOUT_MS = 50;
constexpr auto SLEEP_INTERVAL = std::chrono::milliseconds(10);
}
// Concepts
namespace Concepts {
template<typename T>
concept StringLike = std::convertible_to<T, std::string_view>;
template<typename T>
concept DurationLike = requires(T t) {
std::chrono::duration_cast<std::chrono::duration<double>>(t);
};
}
// Error Handling System
namespace Errors {
enum class ErrorCategory : uint8_t {
None = 0,
Validation, // Invalid arguments, missing executable, etc.
System, // System call failures (pipe, fork, etc.)
Process, // Process-related errors (spawn failure, etc.)
Timeout, // Timeout-related errors
Permission, // Permission-related errors
Resource, // Resource exhaustion (memory, file descriptors, etc.)
Platform // Platform-specific errors
};
enum class ErrorCode : uint16_t {
// Success
Success = 0,
// Validation Errors (100-199)
EmptyCommand = 100,
ExecutableNotFound = 101,
InvalidArguments = 102,
InvalidWorkingDirectory = 103,
// System Errors (200-299)
PipeCreationFailed = 200,
ForkFailed = 201,
ExecFailed = 202,
EnvironmentSetupFailed = 203,
FileDescriptorError = 204,
// Process Errors (300-399)
SpawnFailed = 300,
ProcessAlreadyFinished = 301,
ProcessNotFound = 302,
KillFailed = 303,
WaitFailed = 304,
// Timeout Errors (400-499)
ExecutionTimeout = 400,
WaitTimeout = 401,
// Permission Errors (500-599)
ExecutePermissionDenied = 500,
DirectoryAccessDenied = 501,
// Resource Errors (600-699)
OutOfMemory = 600,
TooManyOpenFiles = 601,
ProcessLimitReached = 602,
// Platform Errors (700-799)
WindowsApiError = 700,
PosixError = 701,
MacOSError = 702,
// Unknown
Unknown = 999
};
struct ErrorInfo {
ErrorCode Code = ErrorCode::Success;
ErrorCategory Category = ErrorCategory::None;
std::string Message;
std::string Details; // Platform-specific details
std::string Suggestion; // Recovery suggestion
int SystemErrorCode = 0; // Platform errno/GetLastError()
[[nodiscard]] constexpr bool IsSuccess() const noexcept {
return Code == ErrorCode::Success;
}
[[nodiscard]] constexpr bool IsFailure() const noexcept {
return !IsSuccess();
}
[[nodiscard]] std::string FullMessage() const {
std::string full = Message;
if (!Details.empty()) {
full += " (Details: " + Details + ")";
}
if (!Suggestion.empty()) {
full += " Suggestion: " + Suggestion;
}
if (SystemErrorCode != 0) {
full += " [System Error: " + std::to_string(SystemErrorCode) + "]";
}
return full;
}
};
// Result-like type for better error handling
template<typename T>
class Result {
std::variant<T, ErrorInfo> data_;
public:
constexpr explicit Result(T&& value) noexcept : data_(std::move(value)) {}
constexpr explicit Result(const T& value) : data_(value) {}
constexpr explicit Result(ErrorInfo error) noexcept : data_(std::move(error)) {}
[[nodiscard]] constexpr bool IsOk() const noexcept {
return std::holds_alternative<T>(data_);
}
[[nodiscard]] constexpr bool IsError() const noexcept {
return !IsOk();
}
[[nodiscard]] constexpr const T& Value() const& {
if (IsError()) {
throw std::runtime_error("Attempted to access value of error result: " + Error().FullMessage());
}
return std::get<T>(data_);
}
[[nodiscard]] constexpr T&& Value() && {
if (IsError()) {
throw std::runtime_error("Attempted to access value of error result: " + Error().FullMessage());
}
return std::get<T>(std::move(data_));
}
[[nodiscard]] constexpr const ErrorInfo& Error() const& {
if (IsOk()) {
static const ErrorInfo success{};
return success;
}
return std::get<ErrorInfo>(data_);
}
[[nodiscard]] constexpr T ValueOr(T&& default_value) const& {
return IsOk() ? Value() : std::move(default_value);
}
// Monadic operations
template<typename F>
[[nodiscard]] constexpr auto Map(F&& func) const& -> Result<std::invoke_result_t<F, const T&>> {
if (IsError()) {
return Error();
}
return func(Value());
}
template<typename F>
[[nodiscard]] constexpr auto AndThen(F&& func) const& -> std::invoke_result_t<F, const T&> {
if (IsError()) {
return Error();
}
return func(Value());
}
};
// Specialization for void operations
template<>
class Result<void> {
std::optional<ErrorInfo> error_;
public:
constexpr Result() noexcept : error_(std::nullopt) {}
constexpr explicit Result(ErrorInfo error) noexcept : error_(std::move(error)) {}
[[nodiscard]] constexpr bool IsOk() const noexcept {
return !error_.has_value();
}
[[nodiscard]] constexpr bool IsError() const noexcept {
return error_.has_value();
}
[[nodiscard]] constexpr const ErrorInfo& Error() const& {
if (IsOk()) {
static const ErrorInfo success{};
return success;
}
return *error_;
}
constexpr void Value() const {
if (IsError()) throw std::runtime_error("Attempted to access value of error result: " + Error().FullMessage());
}
template<typename F>
[[nodiscard]] constexpr auto AndThen(F&& func) const -> std::invoke_result_t<F> {
if (IsError()) return Error();
return func();
}
};
// Error creation helpers
[[nodiscard]] inline ErrorInfo MakeError(ErrorCode code, std::string message,
std::string details = "", std::string suggestion = "") {
ErrorInfo error;
error.Code = code;
error.Message = std::move(message);
error.Details = std::move(details);
error.Suggestion = std::move(suggestion);
// Determine category from code
if (const auto code_val = static_cast<uint16_t>(code); code_val >= 100 && code_val < 200) error.Category = ErrorCategory::Validation;
else if (code_val >= 200 && code_val < 300) error.Category = ErrorCategory::System;
else if (code_val >= 300 && code_val < 400) error.Category = ErrorCategory::Process;
else if (code_val >= 400 && code_val < 500) error.Category = ErrorCategory::Timeout;
else if (code_val >= 500 && code_val < 600) error.Category = ErrorCategory::Permission;
else if (code_val >= 600 && code_val < 700) error.Category = ErrorCategory::Resource;
else if (code_val >= 700 && code_val < 800) error.Category = ErrorCategory::Platform;
#ifdef _WIN32
error.SystemErrorCode = static_cast<int>(GetLastError());
#else
error.SystemErrorCode = errno;
#endif
return error;
}
[[nodiscard]] inline ErrorInfo MakeSystemError(const ErrorCode code, std::string message) {
std::string details;
std::string suggestion;
#ifdef _WIN32
const DWORD error_code = GetLastError();
LPSTR message_buffer = nullptr;
const size_t size = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
nullptr, error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
reinterpret_cast<LPSTR>(&message_buffer), 0, nullptr);
if (message_buffer) {
details = std::string(message_buffer, size);
LocalFree(message_buffer);
}
#else
const int err = errno; // snapshot
details = std::strerror(err);
switch (err) {
case EACCES: suggestion = "Check file permissions and executable bit"; break;
case ENOENT: suggestion = "Verify the executable path exists and is in PATH"; break;
case ENOMEM: suggestion = "Free up system memory or increase limits"; break;
case EMFILE: suggestion = "Close unused file descriptors or increase ulimits"; break;
case EAGAIN: suggestion = "Retry the operation or check system process limits"; break;
default: break;
}
#endif
return MakeError(code, std::move(message), std::move(details), std::move(suggestion));
}
}
struct CommandResult {
int ExitCode{};
std::string Stdout;
std::string Stderr;
std::chrono::duration<double> ExecutionTime{};
bool TimedOut = false;
bool KilledBySignal = false;
int TerminatingSignal = 0;
bool CoreDumped = false;
bool Stopped = false;
int StopSignal = 0;
// Enhanced error information
Errors::ErrorInfo ExecutionError{}; // Details about any execution issues
struct ResourceUsage {
#if defined(__linux__)
long UserCpuTime = 0;
long SystemCpuTime = 0;
long MaxResidentSetSize = 0;
long MinorPageFaults = 0;
long MajorPageFaults = 0;
long VoluntaryContextSwitches = 0;
long InvoluntaryContextSwitches = 0;
#elif defined(_WIN32)
FILETIME UserTime{};
FILETIME KernelTime{};
SIZE_T PeakWorkingSetSize = 0;
SIZE_T PageFaultCount = 0;
#endif
} Usage{};
[[nodiscard]] constexpr bool IsSuccessful() const noexcept {
return ExitCode == 0 && !TimedOut && !KilledBySignal && ExecutionError.IsSuccess();
}
[[nodiscard]] constexpr bool HasOutput() const noexcept {
return !Stdout.empty() || !Stderr.empty();
}
[[nodiscard]] constexpr bool HasExecutionError() const noexcept {
return ExecutionError.IsFailure();
}
[[nodiscard]] std::string GetErrorSummary() const {
if (ExecutionError.IsFailure()) {
return ExecutionError.FullMessage();
}
if (TimedOut) {
return "Process execution timed out";
}
if (KilledBySignal) {
return "Process was killed by signal " + std::to_string(TerminatingSignal);
}
if (ExitCode != 0) {
return "Process exited with non-zero code: " + std::to_string(ExitCode);
}
return "No errors";
}
};
class Child {
public:
#ifdef _WIN32
Child(HANDLE process, HANDLE thread, HANDLE stdout_handle, HANDLE stderr_handle)
: ProcessHandle(process), ThreadHandle(thread), StdoutHandle(stdout_handle), StderrHandle(stderr_handle), PipesClosed(false) {
ProcessId = GetProcessId(process);
}
~Child() {
if (ProcessHandle != INVALID_HANDLE_VALUE) CloseHandle(ProcessHandle);
if (ThreadHandle != INVALID_HANDLE_VALUE) CloseHandle(ThreadHandle);
if (!PipesClosed) {
if (StdoutHandle != INVALID_HANDLE_VALUE) CloseHandle(StdoutHandle);
if (StderrHandle != INVALID_HANDLE_VALUE) CloseHandle(StderrHandle);
}
}
#else
Child(const pid_t pid, const int stdout_fd, const int stderr_fd)
: ProcessId(pid), StdoutFd(stdout_fd), StderrFd(stderr_fd) {}
#endif
[[nodiscard]] Errors::Result<CommandResult> Wait(std::optional<std::chrono::duration<double>> timeout = std::nullopt) const;
[[nodiscard]] pid_t GetPid() const { return ProcessId; }
#ifdef _WIN32
[[nodiscard]] Errors::Result<void> Kill(int signal = 0) const;
#else
[[nodiscard]] Errors::Result<void> Kill(int signal = SIGTERM) const;
#endif
private:
pid_t ProcessId;
#ifdef _WIN32
HANDLE ProcessHandle;
HANDLE ThreadHandle;
HANDLE StdoutHandle;
HANDLE StderrHandle;
mutable bool PipesClosed;
#else
int StdoutFd;
int StderrFd;
#endif
};
class Command {
public:
template<Concepts::StringLike T>
explicit Command(T&& executable) : Executable(std::forward<T>(executable)) {
Arguments.reserve(8); // Reserve space for typical argument count
}
template<Concepts::StringLike T>
Command& Arg(T&& argument) {
Arguments.emplace_back(std::forward<T>(argument));
return *this;
}
template<std::ranges::range R>
requires Concepts::StringLike<std::ranges::range_value_t<R>>
Command& Args(R&& arguments) {
if constexpr (std::ranges::sized_range<R>) {
Arguments.reserve(static_cast<size_t>(std::ranges::size(arguments)));
}
std::ranges::copy(arguments, std::back_inserter(Arguments));
return *this;
}
template<Concepts::StringLike T>
Command& WorkingDirectory(T&& path) {
WorkDir = std::forward<T>(path);
return *this;
}
template<Concepts::StringLike K, Concepts::StringLike V>
Command& Environment(K&& key, V&& value) {
EnvVars.emplace(std::forward<K>(key), std::forward<V>(value));
return *this;
}
template<Concepts::DurationLike D>
Command& Timeout(D&& duration) {
TimeoutDuration = std::chrono::duration_cast<std::chrono::duration<double>>(
std::forward<D>(duration));
return *this;
}
[[nodiscard]] Errors::Result<CommandResult> Execute();
[[nodiscard]] Errors::Result<Child> Spawn();
private:
std::string Executable;
std::vector<std::string> Arguments;
std::optional<std::string> WorkDir;
std::unordered_map<std::string, std::string> EnvVars;
std::optional<std::chrono::duration<double>> TimeoutDuration;
};
class AsyncPipeReader {
struct PipeData {
#ifdef _WIN32
HANDLE Handle;
#else
int Fd;
#endif
std::string Buffer;
bool Finished = false;
explicit PipeData(
#ifdef _WIN32
HANDLE handle
#else
const int fd
#endif
) :
#ifdef _WIN32
Handle(handle)
#else
Fd(fd)
#endif
{
Buffer.reserve(Constants::PIPE_BUFFER_SIZE);
}
};
using Buffer = std::array<char, Constants::PIPE_BUFFER_SIZE>;
public:
#ifdef _WIN32
[[nodiscard]] static std::pair<std::string, std::string> ReadPipes(HANDLE stdout_handle, HANDLE stderr_handle);
private:
static bool ReadFromPipe(PipeData& pipe_data, Buffer& buffer) noexcept;
#else
[[nodiscard]] static std::pair<std::string, std::string> ReadPipes(int stdout_fd, int stderr_fd);
private:
static bool ReadFromPipe(PipeData& pipe_data, Buffer& buffer) noexcept;
static bool IsPipeOpen(int fd) noexcept;
#endif
};
namespace Utils {
template<Concepts::StringLike T>
[[nodiscard]] constexpr bool IsEmpty(const T& str) noexcept {
return std::string_view(str).empty();
}
template<std::ranges::range R>
[[nodiscard]] constexpr bool IsEmpty(const R& range) noexcept {
return std::ranges::empty(range);
}
[[nodiscard]] inline std::string QuoteArgumentWindows(const std::string_view arg) {
if (const bool need_quotes = arg.find_first_of(" \t\"") != std::string_view::npos; !need_quotes) return std::string(arg);
std::string result;
result.reserve(arg.size() + 10); // Reserve space for quotes and escaping
result.push_back('"');
size_t backslash_count = 0;
for (const char c : arg) {
if (c == '\\') {
++backslash_count;
continue;
}
if (c == '"') {
result.append(backslash_count * 2 + 1, '\\');
result.push_back('"');
backslash_count = 0;
continue;
}
if (backslash_count > 0) {
result.append(backslash_count, '\\');
backslash_count = 0;
}
result.push_back(c);
}
if (backslash_count > 0) {
result.append(backslash_count * 2, '\\');
}
result.push_back('"');
return result;
}
/**
* @brief Expand initializer list or any range-like container into a std::vector
* @details This helper allows passing braced-init-lists to Command::Args()
* @example Command("git").Args(Utils::Expand({"commit", "-m", "message"}))
*/
template<Concepts::StringLike T>
[[nodiscard]] constexpr std::vector<std::string> Expand(std::initializer_list<T> args) {
std::vector<std::string> result;
result.reserve(args.size());
for (const auto& arg : args) result.emplace_back(arg);
return result;
}
/**
* @brief Expand any range into a std::vector (for consistency)
* @details Provides a uniform interface for all container types
*/
template<std::ranges::range R>
requires Concepts::StringLike<std::ranges::range_value_t<R>>
[[nodiscard]] constexpr std::vector<std::string> Expand(R&& range) {
std::vector<std::string> result;
if constexpr (std::ranges::sized_range<R>) result.reserve(std::ranges::size(range));
for (const auto& item : range) result.emplace_back(item);
return result;
}
}
class ExecutionValidator {
public:
template<Concepts::StringLike T>
[[nodiscard]] static bool IsFileExecutable(T&& path) {
const std::string_view path_view(path);
#ifdef _WIN32
const DWORD attrs = GetFileAttributesA(std::string(path_view).c_str());
return attrs != INVALID_FILE_ATTRIBUTES && !(attrs & FILE_ATTRIBUTE_DIRECTORY);
#else
return access(std::string(path_view).c_str(), X_OK) == 0;
#endif
}
template<Concepts::StringLike T>
[[nodiscard]] static bool IsCommandExecutable(T&& command) {
const std::string_view cmd_view(command);
#ifdef _WIN32
const std::wstring wcommand(cmd_view.begin(), cmd_view.end());
const DWORD needed = SearchPathW(nullptr, wcommand.c_str(), L".exe", 0, nullptr, nullptr);
return needed > 0;
#else
if (cmd_view.find('/') != std::string_view::npos) {
return access(std::string(cmd_view).c_str(), X_OK) == 0;
}
const char* path_env = getenv("PATH");
if (!path_env) return false;
const std::string_view path_str(path_env);
return std::ranges::any_of(
std::views::split(path_str, ':'),
[cmd_view](const auto& dir_range) {
const std::string_view dir{dir_range.begin(), dir_range.end()};
if (dir.empty()) return false;
const auto full_path = std::filesystem::path(dir) / std::string(cmd_view);
return access(full_path.c_str(), X_OK) == 0;
});
#endif
}
template<std::ranges::range R>
requires Concepts::StringLike<std::ranges::range_value_t<R>>
[[nodiscard]] static bool CanExecuteCommand(const R& args) {
return !Utils::IsEmpty(args) && IsCommandExecutable(*std::ranges::begin(args));
}
};
// Windows implementations
#ifdef _WIN32
inline Errors::Result<CommandResult> Child::Wait(std::optional<std::chrono::duration<double>> timeout) const {
const auto start_time = std::chrono::steady_clock::now();
CommandResult result;
// Start asynchronous pipe reader to avoid deadlocks on full pipes
auto reader_future = std::async(std::launch::async, AsyncPipeReader::ReadPipes, StdoutHandle, StderrHandle);
const DWORD wait_time = timeout ? static_cast<DWORD>(timeout->count() * 1000.0) : INFINITE;
const DWORD wait_result = WaitForSingleObject(ProcessHandle, wait_time);
if (wait_result == WAIT_TIMEOUT) {
result.TimedOut = true;
TerminateProcess(ProcessHandle, 1);
WaitForSingleObject(ProcessHandle, INFINITE);
}
DWORD exit_code = 0;
if (GetExitCodeProcess(ProcessHandle, &exit_code)) {
result.ExitCode = static_cast<int>(exit_code);
} else {
result.ExitCode = Constants::EXIT_FAIL_EC;
}
FILETIME creation_time{}, exit_time{};
GetProcessTimes(ProcessHandle, &creation_time, &exit_time,
&result.Usage.KernelTime, &result.Usage.UserTime);
PROCESS_MEMORY_COUNTERS pmc{};
if (GetProcessMemoryInfo(ProcessHandle, &pmc, sizeof(pmc))) {
result.Usage.PeakWorkingSetSize = pmc.PeakWorkingSetSize;
result.Usage.PageFaultCount = pmc.PageFaultCount;
}
// Gather output after process has exited (child should close pipes)
auto [stdout_result, stderr_result] = reader_future.get();
result.Stdout = std::move(stdout_result);
result.Stderr = std::move(stderr_result);
CloseHandle(StdoutHandle);
CloseHandle(StderrHandle);
PipesClosed = true;
const auto end_time = std::chrono::steady_clock::now();
result.ExecutionTime = end_time - start_time;
return Errors::Result(result);
}
inline Errors::Result<void> Child::Kill(int) const {
if (!TerminateProcess(ProcessHandle, 1)) {
return Errors::Result<void>(
Errors::MakeSystemError(Errors::ErrorCode::KillFailed, "Failed to terminate process"));
}
return {};
}
inline Errors::Result<Child> Command::Spawn() {
// Validate command
if (Executable.empty()) {
return Errors::Result<Child>(Errors::MakeError(Errors::ErrorCode::EmptyCommand,
"Command executable cannot be empty",
"", "Provide a valid executable name or path"));
}
std::vector<std::string> args_vec;
args_vec.push_back(Executable);
args_vec.insert(args_vec.end(), Arguments.begin(), Arguments.end());
if (!ExecutionValidator::CanExecuteCommand(args_vec)) {
return Errors::Result<Child>(Errors::MakeError(Errors::ErrorCode::ExecutableNotFound,
"Executable not found: " + Executable, "",
"Verify the executable exists and is in PATH"));
}
// Validate working directory if specified
if (WorkDir && !fs::exists(*WorkDir)) {
return Errors::Result<Child>(Errors::MakeError(Errors::ErrorCode::InvalidWorkingDirectory,
"Working directory does not exist: " + *WorkDir, "",
"Create the directory or specify a valid path"));
}
SECURITY_ATTRIBUTES sa = {sizeof(SECURITY_ATTRIBUTES), nullptr, TRUE};
HANDLE stdout_read, stdout_write, stderr_read, stderr_write;
if (!CreatePipe(&stdout_read, &stdout_write, &sa, 0)) {
return Errors::Result<Child>(
Errors::MakeSystemError(Errors::ErrorCode::PipeCreationFailed, "Failed to create stdout pipe"));
}
if (!CreatePipe(&stderr_read, &stderr_write, &sa, 0)) {
CloseHandle(stdout_read);
CloseHandle(stdout_write);
return Errors::Result<Child>(
Errors::MakeSystemError(Errors::ErrorCode::PipeCreationFailed, "Failed to create stderr pipe"));
}
SetHandleInformation(stdout_read, HANDLE_FLAG_INHERIT, 0);
SetHandleInformation(stderr_read, HANDLE_FLAG_INHERIT, 0);
STARTUPINFOA si = {sizeof(STARTUPINFOA)};
si.dwFlags = STARTF_USESTDHANDLES;
si.hStdOutput = stdout_write;
si.hStdError = stderr_write;
si.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
PROCESS_INFORMATION pi = {};
// Build command line using ranges and utility function
std::string cmdline = Utils::QuoteArgumentWindows(Executable);
const auto quoted_args = Arguments | std::views::transform(Utils::QuoteArgumentWindows);
for (const auto& quoted_arg : quoted_args) {
cmdline += ' ';
cmdline += quoted_arg;
}
// Build environment block: merge current environment with overrides (if any)
std::string env_block;
if (!EnvVars.empty()) {
// Gather current environment
LPCH env_strings = GetEnvironmentStringsA();
if (env_strings) {
// Copy all existing vars unless overridden (case-insensitive on Windows)
std::unordered_map<std::string, std::string> lower_over;
lower_over.reserve(EnvVars.size());
for (const auto& [k, v] : EnvVars) {
std::string lk = k;
std::transform(lk.begin(), lk.end(), lk.begin(), [](char c) { return std::tolower(c); });
lower_over.emplace(std::move(lk), v);
}
for (LPCSTR p = env_strings; *p; ) {
std::string entry = p;
size_t eq = entry.find('=');
if (eq != std::string::npos) {
std::string key = entry.substr(0, eq);
std::string lk = key;
std::transform(lk.begin(), lk.end(), lk.begin(), [](char c) { return std::tolower(c); });
if (lower_over.find(lk) == lower_over.end()) {
env_block += entry;
env_block.push_back('\0');
}
}
p += entry.size() + 1;
}
FreeEnvironmentStringsA(env_strings);
}
// Add/override with provided variables
for (const auto& [key, value] : EnvVars) {
env_block += key;
env_block += '=';
env_block += value;
env_block.push_back('\0');
}
env_block.push_back('\0');
}
BOOL success = CreateProcessA(
nullptr, const_cast<char*>(cmdline.c_str()),
nullptr, nullptr, TRUE, 0,
env_block.empty() ? nullptr : const_cast<char*>(env_block.c_str()),
WorkDir ? WorkDir->c_str() : nullptr,
&si, &pi
);
CloseHandle(stdout_write);
CloseHandle(stderr_write);
if (!success) {
CloseHandle(stdout_read);
CloseHandle(stderr_read);
const DWORD error_code = GetLastError();
std::string suggestion;
switch (error_code) {
case ERROR_FILE_NOT_FOUND:
suggestion = "Verify the executable path exists";
break;
case ERROR_ACCESS_DENIED:
suggestion = "Check file permissions and security settings";
break;
case ERROR_NOT_ENOUGH_MEMORY:
suggestion = "Free up system memory";
break;
default:
suggestion = "Check Windows event logs for more details";
break;
}
return Errors::Result<Child>(Errors::MakeError(Errors::ErrorCode::SpawnFailed,
"Failed to create process: " + Executable,
"CreateProcessA failed with error " + std::to_string(error_code),
suggestion));
}
return Errors::Result<Child>(Child(pi.hProcess, pi.hThread, stdout_read, stderr_read));
}
inline std::pair<std::string, std::string> AsyncPipeReader::ReadPipes(HANDLE stdout_handle, HANDLE stderr_handle) {
auto read_all = [](HANDLE h) -> std::string {
std::string acc;
acc.reserve(Constants::PIPE_BUFFER_SIZE);
Buffer buf{};
DWORD n = 0;
for (;;) {
if (!ReadFile(h, buf.data(), static_cast<DWORD>(buf.size()), &n, nullptr)) {
const DWORD err = GetLastError();
if (err == ERROR_BROKEN_PIPE || err == ERROR_HANDLE_EOF) break;
// Transient: small backoff
Sleep(1);
continue;
}
if (n == 0) break;
acc.append(buf.data(), n);
}
return acc;
};
auto f_out = std::async(std::launch::async, read_all, stdout_handle);
auto f_err = std::async(std::launch::async, read_all, stderr_handle);
return {f_out.get(), f_err.get()};
}
inline bool AsyncPipeReader::ReadFromPipe(PipeData& pipe_data, Buffer& buffer) noexcept {
DWORD bytes_read;
if (ReadFile(pipe_data.Handle, buffer.data(), static_cast<DWORD>(buffer.size()), &bytes_read, nullptr)) {
if (bytes_read > 0) {
pipe_data.Buffer.append(buffer.data(), bytes_read);
return true;
}
}
return false;
}
#else
// Unix implementations
inline Errors::Result<CommandResult> Child::Wait(std::optional<std::chrono::duration<double>> timeout) const {
const auto start_time = std::chrono::steady_clock::now();
CommandResult result;
int status = 0;
rusage usage{};
// Start asynchronous pipe reader to avoid deadlocks while child runs
auto reader_future = std::async(std::launch::async, AsyncPipeReader::ReadPipes, StdoutFd, StderrFd);
if (timeout) {
const auto timeout_time = start_time + *timeout;
while (std::chrono::steady_clock::now() < timeout_time) {
const int wait_result = waitpid(ProcessId, &status, WNOHANG);
if (wait_result == ProcessId) {
// Child finished; collect final status
waitpid(ProcessId, &status, 0);
break; // Process finished
}
if (wait_result == -1) {
result.ExitCode = Constants::EXIT_FAIL_EC;
result.Stderr = "waitpid failed";
break;
}
std::this_thread::sleep_for(Constants::SLEEP_INTERVAL);
}
// Check if we timed out
if (std::chrono::steady_clock::now() >= timeout_time) {
if (const int wait_result = waitpid(ProcessId, &status, WNOHANG); wait_result == 0) { // Still running
static_cast<void>(Kill());
result.TimedOut = true;
// Collect final status after terminating
waitpid(ProcessId, &status, 0);
} else if (wait_result == ProcessId) {
// Already finished; ensure status is reaped
waitpid(ProcessId, &status, 0);
}
}
} else {
// Blocking wait for child process
waitpid(ProcessId, &status, 0);
}
// Collect outputs (reader finishes when pipes close)
auto [stdout_result, stderr_result] = reader_future.get();
result.Stdout = std::move(stdout_result);
result.Stderr = std::move(stderr_result);
close(StdoutFd);
close(StderrFd);
// Populate resource usage after child has terminated
getrusage(RUSAGE_CHILDREN, &usage);
#ifdef __linux__
constexpr long MICROSECONDS_PER_SECOND = 1000000;
result.Usage.UserCpuTime = usage.ru_utime.tv_sec * MICROSECONDS_PER_SECOND + usage.ru_utime.tv_usec;
result.Usage.SystemCpuTime = usage.ru_stime.tv_sec * MICROSECONDS_PER_SECOND + usage.ru_stime.tv_usec;
result.Usage.MaxResidentSetSize = usage.ru_maxrss;
result.Usage.MinorPageFaults = usage.ru_minflt;
result.Usage.MajorPageFaults = usage.ru_majflt;
result.Usage.VoluntaryContextSwitches = usage.ru_nvcsw;
result.Usage.InvoluntaryContextSwitches = usage.ru_nivcsw;
#endif
// Enhanced process termination analysis
if (!result.TimedOut) {
if (WIFEXITED(status)) {
result.ExitCode = WEXITSTATUS(status);
} else if (WIFSIGNALED(status)) {
result.KilledBySignal = true;
result.TerminatingSignal = WTERMSIG(status);
result.ExitCode = 128 + result.TerminatingSignal;
#if defined(WCOREFLAG)
result.CoreDumped = (status & WCOREFLAG) != 0;
#elif defined(WCOREDUMP)
result.CoreDumped = WCOREDUMP(status);
#else
result.CoreDumped = false;
#endif
} else if (WIFSTOPPED(status)) {
result.Stopped = true;
result.StopSignal = WSTOPSIG(status);
}
}
const auto end_time = std::chrono::steady_clock::now();
result.ExecutionTime = end_time - start_time;
return Errors::Result(std::move(result));
}
inline Errors::Result<void> Child::Kill(const int signal) const {
if (kill(ProcessId, signal) == -1) {
return Errors::Result<void>(
Errors::MakeSystemError(Errors::ErrorCode::KillFailed,
"Failed to send signal " + std::to_string(signal) +
" to process " + std::to_string(ProcessId)));
}
return {};
}
inline Errors::Result<Child> Command::Spawn() {
if (Executable.empty()) {
return Errors::Result<Child>(Errors::MakeError(Errors::ErrorCode::EmptyCommand,
"Command executable cannot be empty", "",
"Provide a valid executable name or path"));
}
std::vector<std::string> args_vec;