Repository navigation
Expand file tree
/
Copy pathProcess.cpp
More file actions
754 lines (681 loc) · 25.8 KB
/
Copy pathProcess.cpp
File metadata and controls
754 lines (681 loc) · 25.8 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
#ifdef _WIN32
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <windows.h>
#include <shellapi.h>
#include <tlhelp32.h>
#else
#include <sched.h>
#include <unistd.h>
#endif
#if defined(__linux__) && defined(__x86_64__)
#include <asm/prctl.h>
#include <sys/syscall.h>
#endif
#include "SceTypes.hpp"
#include "prx/libc/include/General.hpp"
#include "prx/libc/include/ApplicationHeap.hpp"
#include "prx/libc/include/HostEntropy.hpp"
#include "prx/libc/include/Shutdown.hpp"
#include "prx/libkernel/AppMetadata/include/AppMetadata.hpp"
#include "prx/libkernel/DirectMemory/DirectMemory.hpp"
#include "prx/libkernel/KernelErrors.hpp"
#include "prx/libkernel/Pthread/include/Pthread.hpp"
#include <algorithm>
#include <array>
#include <atomic>
#include <cerrno>
#include <cstddef>
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <limits>
#include <memory>
#include <mutex>
#include <optional>
#include <stdexcept>
#include <string>
#include <system_error>
#include <vector>
#ifndef _WIN32
#include <sys/resource.h>
#endif
extern "C" int* APS5_VABI __error_nid_postfix();
extern "C" int APS5_VABI stat_nid_postfix(const char* path, FileStat* sb);
namespace {
constexpr int sceInvalidArgument = static_cast<int>(0x80020016u);
constexpr int errnoNoSuchProcess = 3;
constexpr int errnoNoChild = 10;
constexpr int errnoBadAddress = 14;
constexpr int errnoInvalidArgument = 22;
constexpr int freebsdGroupsMax = 1023;
std::mutex supplementaryGroupsMutex;
std::vector<std::uint32_t> supplementaryGroups{0};
constexpr unsigned freebsdWaitOptions = 0x8000003Fu;
std::atomic<std::uint32_t> gpoBits{0};
constexpr std::array<std::uint8_t, 16> openPsId{'A', 'n', 'y', 'P', 'S', '5', 'O', 'p', 'e', 'n', 'P', 's', 'I', 'd', 0, 1};
#ifdef _WIN32
std::string toUtf8(const wchar_t* value) {
const int size = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, value, -1, nullptr, 0, nullptr, nullptr);
if (size <= 0)
throw std::system_error(GetLastError(), std::system_category(), "Converting a process argument to UTF-8");
std::string text(static_cast<std::size_t>(size), '\0');
if (WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, value, -1, text.data(), size, nullptr, nullptr) != size)
throw std::system_error(GetLastError(), std::system_category(), "Converting a process argument to UTF-8");
text.pop_back();
return text;
}
#endif
class ProcessArguments {
public:
ProcessArguments() {
#ifdef _WIN32
int count = 0;
const std::unique_ptr<LPWSTR, void (*)(LPWSTR*)> values(CommandLineToArgvW(GetCommandLineW(), &count),
[](LPWSTR* parsed) { LocalFree(parsed); });
if (!values)
throw std::system_error(GetLastError(), std::system_category(), "Reading process arguments");
for (int index = 0; index < count; ++index)
arguments.push_back(toUtf8(values.get()[index]));
wchar_t skip[2]{};
if (GetEnvironmentVariableW(L"ANYPS5_LOADEXEC_ARGV_SKIP", skip, 2) == 1 && skip[0] == L'1') {
if (arguments.size() < 2) throw std::runtime_error("LoadExec: missing guest process arguments");
arguments.erase(arguments.begin());
if (_wputenv_s(L"ANYPS5_LOADEXEC_ARGV_SKIP", L"") != 0 || !SetEnvironmentVariableW(L"ANYPS5_LOADEXEC_ARGV_SKIP", nullptr))
throw std::runtime_error("LoadExec: cannot clear the argument marker");
}
#else
std::ifstream stream("/proc/self/cmdline", std::ios::binary);
if (!stream)
throw std::runtime_error("Cannot read process arguments");
std::string argument;
while (std::getline(stream, argument, '\0')) {
if (stream.eof())
throw std::runtime_error("Unterminated process argument");
arguments.push_back(argument);
}
if (stream.bad() || !stream.eof())
throw std::runtime_error("Reading process arguments failed");
#endif
if (arguments.empty() || arguments.size() > static_cast<std::size_t>(std::numeric_limits<int>::max()))
throw std::runtime_error("Invalid process argument count");
for (const auto& argument : arguments)
pointers.push_back(argument.c_str());
pointers.push_back(nullptr);
}
int GetCount() const { return static_cast<int>(arguments.size()); }
const char** GetValues() { return pointers.data(); }
private:
std::vector<std::string> arguments;
std::vector<const char*> pointers;
};
ProcessArguments& getProcessArguments() {
static ProcessArguments arguments;
return arguments;
}
bool hasPriorityRange(int policy) {
return policy == 1 || policy == 3;
}
constexpr int guestNoProcess = 3;
constexpr int guestFault = 14;
constexpr int guestInvalid = 22;
constexpr int guestRlimitCpu = 0;
constexpr int guestRlimitFsize = 1;
constexpr int guestPriorityProcess = 0;
constexpr int guestPriorityGroup = 1;
constexpr int guestPriorityUser = 2;
std::atomic<int> guestNice{0};
constexpr int guestNiceMin = -20;
constexpr int guestNiceMax = 20;
constexpr int guestRlimitData = 2;
constexpr int guestRlimitCore = 4;
constexpr int guestRlimitNofile = 8;
constexpr int guestRlimitSbsize = 9;
constexpr int guestRlimitVmem = 10;
constexpr int guestRlimitNpts = 11;
constexpr int guestRlimitSwap = 12;
constexpr int guestRlimitKqueues = 13;
constexpr int guestRlimitUmtxp = 14;
constexpr int guestRlimitCount = 15;
constexpr std::int64_t guestRlimitInfinity = std::numeric_limits<std::int64_t>::max();
#ifdef _WIN32
std::int64_t hostMemoryLimit() {
BOOL inJob = FALSE;
if (!IsProcessInJob(GetCurrentProcess(), nullptr, &inJob))
throw std::system_error(GetLastError(), std::system_category(), "getrlimit: IsProcessInJob failed");
if (!inJob)
return guestRlimitInfinity;
JOBOBJECT_EXTENDED_LIMIT_INFORMATION limits{};
if (!QueryInformationJobObject(nullptr, JobObjectExtendedLimitInformation, &limits, sizeof(limits), nullptr))
throw std::system_error(GetLastError(), std::system_category(), "getrlimit: QueryInformationJobObject failed");
if ((limits.BasicLimitInformation.LimitFlags & JOB_OBJECT_LIMIT_PROCESS_MEMORY) == 0 || limits.ProcessMemoryLimit > static_cast<SIZE_T>(guestRlimitInfinity))
return guestRlimitInfinity;
return static_cast<std::int64_t>(limits.ProcessMemoryLimit);
}
#else
std::int64_t toGuestLimit(rlim_t value) {
if (value == RLIM_INFINITY || value > static_cast<rlim_t>(guestRlimitInfinity))
return guestRlimitInfinity;
return static_cast<std::int64_t>(value);
}
#endif
}
extern "C" int* APS5_VABI __error_nid_postfix();
struct GuestResourceLimit {
std::int64_t rlim_cur;
std::int64_t rlim_max;
};
namespace {
std::mutex openFileLimitMutex;
std::optional<GuestResourceLimit> guestOpenFileLimit;
}
struct GuestResourceUsage {
KernelTimeval ru_utime;
KernelTimeval ru_stime;
std::int64_t ru_maxrss;
std::int64_t ru_ixrss;
std::int64_t ru_idrss;
std::int64_t ru_isrss;
std::int64_t ru_minflt;
std::int64_t ru_majflt;
std::int64_t ru_nswap;
std::int64_t ru_inblock;
std::int64_t ru_oublock;
std::int64_t ru_msgsnd;
std::int64_t ru_msgrcv;
std::int64_t ru_nsignals;
std::int64_t ru_nvcsw;
std::int64_t ru_nivcsw;
};
extern "C" Pthread APS5_VABI scePthreadSelf();
extern "C" {
// unknown data
const char* __progname_nid_postfix = "eboot.bin";
static char* emptyEnvironment[1];
char** environ_nid_postfix = emptyEnvironment;
int APS5_VABI getargc_nid_postfix(void) {
return getProcessArguments().GetCount();
}
const char** APS5_VABI getargv_nid_postfix(void) {
return getProcessArguments().GetValues();
}
int APS5_VABI getpagesize_nid_postfix(void) {
return PS5_PAGE_SIZE;
}
int APS5_VABI getpid_nid_postfix(void) {
#ifdef _WIN32
const auto pid = GetCurrentProcessId();
#else
const auto pid = ::getpid();
#endif
if (pid == 0 || static_cast<std::uint64_t>(pid) > static_cast<std::uint64_t>(std::numeric_limits<int>::max()))
throw std::runtime_error("Process identifier is outside the guest range");
return static_cast<int>(pid);
}
int APS5_VABI getsid_nid_postfix(int pid) {
const int self = getpid_nid_postfix();
if (pid == 0 || pid == self) return self;
*__error_nid_postfix() = errnoNoSuchProcess;
return -1;
}
int APS5_VABI getppid_nid_postfix(void) {
#ifdef _WIN32
const DWORD self = GetCurrentProcessId();
const HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (snapshot == INVALID_HANDLE_VALUE)
throw std::system_error(static_cast<int>(GetLastError()), std::system_category(), "getppid: CreateToolhelp32Snapshot");
PROCESSENTRY32W entry{};
entry.dwSize = sizeof(entry);
bool found = false;
DWORD pid = 0;
for (BOOL more = Process32FirstW(snapshot, &entry); more && !found; more = Process32NextW(snapshot, &entry)) {
if (entry.th32ProcessID == self) {
pid = entry.th32ParentProcessID;
found = true;
}
}
CloseHandle(snapshot);
if (!found) throw std::runtime_error("getppid: the process is missing from the process snapshot");
#else
const auto pid = ::getppid();
#endif
if (static_cast<std::uint64_t>(pid) > static_cast<std::uint64_t>(std::numeric_limits<int>::max()))
throw std::runtime_error("Parent process identifier is outside the guest range");
return static_cast<int>(pid);
}
int APS5_VABI sceKernelGetAppInfo(int pid, void* info) {
if (pid != getpid_nid_postfix()) return SCE_KERNEL_ERROR_EPERM;
if (info == nullptr) return 0;
constexpr std::size_t TitleIdOffset = 0x10;
constexpr std::size_t TitleIdBytes = 10;
const auto titleId = GetAppTitleId_nid_postfix();
char field[TitleIdBytes] = {};
std::strncpy(field, titleId.value, sizeof(field) - 1);
auto* bytes = static_cast<std::uint8_t*>(info);
const std::uint32_t appId = 1;
std::memcpy(bytes, &appId, sizeof(appId));
std::memcpy(bytes + TitleIdOffset, field, TitleIdBytes);
return 0;
}
int APS5_VABI getuid_nid_postfix(void) {
return 0;
}
int APS5_VABI geteuid_nid_postfix(void) {
return 0;
}
int APS5_VABI getgid_nid_postfix(void) {
return 0;
}
int APS5_VABI getegid_nid_postfix(void) {
return 0;
}
int APS5_VABI issetugid_nid_postfix(void) {
return 0;
}
void APS5_VABI exit_nid_postfix(int code) {
LibcExit_nid_no_patch(code);
}
[[noreturn]] void APS5_VABI _exit_nid_postfix(int status) {
LibcTerminate_nid_no_patch(status);
}
int APS5_VABI system_nid_postfix(const char* command) {
constexpr int shellNotExecuted = 127 << 8;
return command == nullptr ? 1 : shellNotExecuted;
}
int APS5_VABI waitpid_nid_postfix(int pid, int* status, int options) {
(void)pid;
(void)status;
*__error_nid_postfix() = (static_cast<unsigned>(options) & ~freebsdWaitOptions) != 0 ? errnoInvalidArgument : errnoNoChild;
return -1;
}
int APS5_VABI getrlimit_nid_postfix(int resource, GuestResourceLimit* limit);
int APS5_VABI setrlimit_nid_postfix(int resource, const GuestResourceLimit* limit) {
if (limit == nullptr) {
*__error_nid_postfix() = guestFault;
return -1;
}
if (resource < 0 || resource >= guestRlimitCount) {
*__error_nid_postfix() = guestInvalid;
return -1;
}
GuestResourceLimit requested = *limit;
if (requested.rlim_cur < 0)
requested.rlim_cur = guestRlimitInfinity;
if (requested.rlim_max < 0)
requested.rlim_max = guestRlimitInfinity;
if (requested.rlim_cur > requested.rlim_max)
requested.rlim_cur = requested.rlim_max;
GuestResourceLimit current{};
getrlimit_nid_postfix(resource, ¤t);
if (resource == guestRlimitNofile) {
if (requested.rlim_max <= current.rlim_max && requested.rlim_cur <= requested.rlim_max) {
std::lock_guard lock(openFileLimitMutex);
guestOpenFileLimit = requested;
return 0;
}
}
if (requested.rlim_cur != current.rlim_cur || requested.rlim_max != current.rlim_max)
throw std::runtime_error("setrlimit: changing the limit of resource " + std::to_string(resource) + " is not supported");
return 0;
}
int APS5_VABI wait_nid_postfix(int* status) {
return waitpid_nid_postfix(-1, status, 0);
}
int APS5_VABI execvp_nid_postfix(const char* file, char* const* arguments) {
(void)file;
(void)arguments;
NotImplemented_nid_no_patch("execvp: executable replacement");
return -1;
}
int APS5_VABI execv_nid_postfix(const char* path, char* const* arguments) {
(void)path;
(void)arguments;
NotImplemented_nid_no_patch("execv: executable replacement");
return -1;
}
int APS5_VABI execve_nid_postfix(const char* path, char* const* arguments, char* const* environment) {
(void)path;
(void)arguments;
(void)environment;
NotImplemented_nid_no_patch("execve: executable replacement");
return -1;
}
int APS5_VABI sceKernelGetCurrentCpu(void) {
#ifdef _WIN32
PROCESSOR_NUMBER processor{};
GetCurrentProcessorNumberEx(&processor);
unsigned index = processor.Number;
for (WORD group = 0; group < processor.Group; ++group) {
const auto count = GetActiveProcessorCount(group);
if (count == 0)
throw std::system_error(GetLastError(), std::system_category(), "Reading processor group size");
index += count;
}
#else
const int cpu = ::sched_getcpu();
if (cpu < 0)
throw std::system_error(errno, std::generic_category(), "Reading current processor");
const auto index = static_cast<unsigned>(cpu);
#endif
return GuestCpuFromHost(index, scePthreadSelf()->affinity.load(std::memory_order_relaxed));
}
int APS5_VABI sceKernelGetMainSocId(void) {
return 0;
}
int APS5_VABI sceKernelGetCpumode(void) {
return 0;
}
std::uint64_t APS5_VABI sceKernelGetGPI(void) {
return gpoBits.load(std::memory_order_relaxed);
}
void APS5_VABI sceKernelSetGPO(std::uint32_t bits) {
gpoBits.store(bits, std::memory_order_relaxed);
}
int APS5_VABI sceKernelGetOpenPsId(void* openPsIdOutput) {
if (!openPsIdOutput)
throw std::invalid_argument("sceKernelGetOpenPsId: null output");
std::memcpy(openPsIdOutput, openPsId.data(), openPsId.size());
return 0;
}
void* APS5_VABI sceKernelGetProcParam(void) {
return const_cast<void*>(ApplicationProcessParameters_nid_no_patch());
}
int APS5_VABI sceKernelUuidCreate(std::uint32_t* uuid) {
if (!uuid) return sceInvalidArgument;
std::array<std::uint32_t, 4> value;
HostEntropy_nid_no_patch(value.data(), sizeof(value));
value[1] = (value[1] & 0x0fffffffu) | 0x40000000u;
value[2] = (value[2] & 0xffffff3fu) | 0x80u;
std::memcpy(uuid, value.data(), sizeof(value));
return 0;
}
void APS5_VABI sceKernelSync(void) {
SyncWrittenPaths_nid_no_patch();
}
void APS5_VABI sync_nid_postfix(void) {
sceKernelSync();
}
int APS5_VABI sched_get_priority_max_nid_postfix(int policy) {
if (!hasPriorityRange(policy)) {
*__error_nid_postfix() = guestInvalid;
return -1;
}
return 767;
}
int APS5_VABI sched_get_priority_min_nid_postfix(int policy) {
if (!hasPriorityRange(policy)) {
*__error_nid_postfix() = guestInvalid;
return -1;
}
return 256;
}
int APS5_VABI sysarch(int operation, void* parameters) {
constexpr int getFsBase = 128;
constexpr int getGsBase = 130;
constexpr int setGsBase = 131;
constexpr std::uint64_t userAddressEnd = 0x0000800000000000ull;
if (operation != getFsBase && operation != getGsBase && operation != setGsBase)
NotImplemented_nid_no_patch(("sysarch: operation " + std::to_string(operation)).c_str());
#if defined(__linux__) && defined(__x86_64__)
if (parameters == nullptr) {
*__error_nid_postfix() = guestFault;
return -1;
}
std::uint64_t base = 0;
if (operation == setGsBase) {
std::memcpy(&base, parameters, sizeof(base));
if (base >= userAddressEnd) {
*__error_nid_postfix() = guestInvalid;
return -1;
}
if (::syscall(SYS_arch_prctl, ARCH_SET_GS, base) != 0) throw std::system_error(errno, std::generic_category(), "sysarch: setting the GS base");
return 0;
}
if (::syscall(SYS_arch_prctl, operation == getFsBase ? ARCH_GET_FS : ARCH_GET_GS, &base) != 0)
throw std::system_error(errno, std::generic_category(), "sysarch: reading a segment base");
std::memcpy(parameters, &base, sizeof(base));
return 0;
#else
static_cast<void>(parameters);
NotImplemented_nid_no_patch("sysarch: segment bases on this host");
return 0;
#endif
}
int APS5_VABI getrusage_nid_postfix(int who, GuestResourceUsage* usage) {
if (who != -1 && who != 0 && who != 1) {
*__error_nid_postfix() = guestInvalid;
return -1;
}
if (usage == nullptr) {
*__error_nid_postfix() = guestFault;
return -1;
}
if (who == -1) {
*usage = {};
return 0;
}
#ifdef _WIN32
FILETIME creation{};
FILETIME exitTime{};
FILETIME kernel{};
FILETIME user{};
if (who == 0) {
if (!GetProcessTimes(GetCurrentProcess(), &creation, &exitTime, &kernel, &user))
throw std::system_error(GetLastError(), std::system_category(), "getrusage: GetProcessTimes failed");
} else {
if (!GetThreadTimes(GetCurrentThread(), &creation, &exitTime, &kernel, &user))
throw std::system_error(GetLastError(), std::system_category(), "getrusage: GetThreadTimes failed");
}
const auto toMicros = [](const FILETIME& time) {
return ((static_cast<std::uint64_t>(time.dwHighDateTime) << 32) | time.dwLowDateTime) / 10ULL;
};
const auto userMicros = toMicros(user);
const auto kernelMicros = toMicros(kernel);
usage->ru_utime.tv_sec = static_cast<std::int64_t>(userMicros / 1000000ULL);
usage->ru_utime.tv_usec = static_cast<std::int64_t>(userMicros % 1000000ULL);
usage->ru_stime.tv_sec = static_cast<std::int64_t>(kernelMicros / 1000000ULL);
usage->ru_stime.tv_usec = static_cast<std::int64_t>(kernelMicros % 1000000ULL);
usage->ru_maxrss = 0;
usage->ru_ixrss = 0;
usage->ru_idrss = 0;
usage->ru_isrss = 0;
usage->ru_minflt = 0;
usage->ru_majflt = 0;
usage->ru_nswap = 0;
usage->ru_inblock = 0;
usage->ru_oublock = 0;
usage->ru_msgsnd = 0;
usage->ru_msgrcv = 0;
usage->ru_nsignals = 0;
usage->ru_nvcsw = 0;
usage->ru_nivcsw = 0;
#else
struct rusage native{};
if (::getrusage(who == 0 ? RUSAGE_SELF : RUSAGE_THREAD, &native) != 0)
throw std::system_error(errno, std::generic_category(), "getrusage: getrusage failed");
usage->ru_utime.tv_sec = static_cast<std::int64_t>(native.ru_utime.tv_sec);
usage->ru_utime.tv_usec = static_cast<std::int64_t>(native.ru_utime.tv_usec);
usage->ru_stime.tv_sec = static_cast<std::int64_t>(native.ru_stime.tv_sec);
usage->ru_stime.tv_usec = static_cast<std::int64_t>(native.ru_stime.tv_usec);
usage->ru_maxrss = static_cast<std::int64_t>(native.ru_maxrss);
usage->ru_ixrss = static_cast<std::int64_t>(native.ru_ixrss);
usage->ru_idrss = static_cast<std::int64_t>(native.ru_idrss);
usage->ru_isrss = static_cast<std::int64_t>(native.ru_isrss);
usage->ru_minflt = static_cast<std::int64_t>(native.ru_minflt);
usage->ru_majflt = static_cast<std::int64_t>(native.ru_majflt);
usage->ru_nswap = static_cast<std::int64_t>(native.ru_nswap);
usage->ru_inblock = static_cast<std::int64_t>(native.ru_inblock);
usage->ru_oublock = static_cast<std::int64_t>(native.ru_oublock);
usage->ru_msgsnd = static_cast<std::int64_t>(native.ru_msgsnd);
usage->ru_msgrcv = static_cast<std::int64_t>(native.ru_msgrcv);
usage->ru_nsignals = static_cast<std::int64_t>(native.ru_nsignals);
usage->ru_nvcsw = static_cast<std::int64_t>(native.ru_nvcsw);
usage->ru_nivcsw = static_cast<std::int64_t>(native.ru_nivcsw);
#endif
return 0;
}
int APS5_VABI getrlimit_nid_postfix(int resource, GuestResourceLimit* limit) {
if (resource < 0 || resource >= guestRlimitCount) {
*__error_nid_postfix() = guestInvalid;
return -1;
}
if (limit == nullptr) {
*__error_nid_postfix() = guestFault;
return -1;
}
switch (resource) {
case guestRlimitData: {
#ifdef _WIN32
const std::int64_t memory = hostMemoryLimit();
limit->rlim_cur = memory;
limit->rlim_max = memory;
#else
rlimit native{};
if (::getrlimit(RLIMIT_DATA, &native) != 0)
throw std::system_error(errno, std::generic_category(), "getrlimit: getrlimit failed");
limit->rlim_cur = toGuestLimit(native.rlim_cur);
limit->rlim_max = toGuestLimit(native.rlim_max);
#endif
return 0;
}
case guestRlimitNofile: {
std::lock_guard lock(openFileLimitMutex);
if (!guestOpenFileLimit) {
#ifdef _WIN32
constexpr std::int64_t crtDescriptors = 8192;
guestOpenFileLimit = GuestResourceLimit{crtDescriptors, crtDescriptors};
#else
rlimit native{};
if (::getrlimit(RLIMIT_NOFILE, &native) != 0)
throw std::system_error(errno, std::generic_category(), "getrlimit: getrlimit failed");
guestOpenFileLimit = GuestResourceLimit{toGuestLimit(native.rlim_cur), toGuestLimit(native.rlim_max)};
#endif
}
*limit = *guestOpenFileLimit;
return 0;
}
case guestRlimitCpu:
case guestRlimitFsize:
case guestRlimitCore:
case guestRlimitSbsize:
case guestRlimitVmem:
case guestRlimitNpts:
case guestRlimitSwap:
case guestRlimitKqueues:
case guestRlimitUmtxp:
limit->rlim_cur = guestRlimitInfinity;
limit->rlim_max = guestRlimitInfinity;
return 0;
default:
throw std::runtime_error("getrlimit: unsupported resource " + std::to_string(resource));
}
}
int APS5_VABI getpriority_nid_postfix(int which, int who) {
if (which != guestPriorityProcess && which != guestPriorityGroup && which != guestPriorityUser) {
*__error_nid_postfix() = guestInvalid;
return -1;
}
const bool found = which == guestPriorityUser ? (who == 0 ? getuid_nid_postfix() : who) == getuid_nid_postfix()
: who == 0 || who == getpid_nid_postfix();
if (!found) {
*__error_nid_postfix() = guestNoProcess;
return -1;
}
return guestNice.load(std::memory_order_relaxed);
}
int APS5_VABI setpriority_nid_postfix(int which, int who, int niceval) {
if (which != guestPriorityProcess && which != guestPriorityGroup && which != guestPriorityUser) {
*__error_nid_postfix() = guestInvalid;
return -1;
}
const bool found = which == guestPriorityUser ? (who == 0 ? getuid_nid_postfix() : who) == getuid_nid_postfix()
: who == 0 || who == getpid_nid_postfix();
if (!found) {
*__error_nid_postfix() = guestNoProcess;
return -1;
}
const int clamped = niceval < guestNiceMin ? guestNiceMin : (niceval > guestNiceMax ? guestNiceMax : niceval);
guestNice.store(clamped, std::memory_order_relaxed);
return 0;
}
int APS5_VABI getdtablesize_nid_postfix(void) {
NotImplemented_nid_no_patch(__func__);
return -1;
}
int APS5_VABI sceKernelIsTrinityMode(void) {
return 0;
}
void APS5_VABI sceKernelGetOperationMode(int* mode, int* submode) {
if (!mode || !submode)
throw std::invalid_argument("sceKernelGetOperationMode: null output");
*mode = 2;
*submode = 0;
}
int APS5_VABI seteuid_nid_postfix(std::uint32_t euid) {
if (euid != 0)
throw std::runtime_error("seteuid: unsupported user id " + std::to_string(euid));
return 0;
}
int APS5_VABI setegid_nid_postfix(std::uint32_t egid) {
if (egid != 0)
throw std::runtime_error("setegid: unsupported group id " + std::to_string(egid));
return 0;
}
int APS5_VABI setuid_nid_postfix(std::uint32_t uid) {
if (uid != 0)
throw std::runtime_error("setuid: unsupported user id " + std::to_string(uid));
return 0;
}
int APS5_VABI setreuid_nid_postfix(std::uint32_t ruid, std::uint32_t euid) {
constexpr std::uint32_t unchanged = 0xffffffffu;
if (ruid != 0 && ruid != unchanged)
throw std::runtime_error("setreuid: unsupported real user id " + std::to_string(ruid));
if (euid != 0 && euid != unchanged)
throw std::runtime_error("setreuid: unsupported effective user id " + std::to_string(euid));
return 0;
}
int APS5_VABI setregid_nid_postfix(std::uint32_t rgid, std::uint32_t egid) {
constexpr std::uint32_t unchanged = 0xffffffffu;
if (rgid != 0 && rgid != unchanged)
throw std::runtime_error("setregid: unsupported real group id " + std::to_string(rgid));
if (egid != 0 && egid != unchanged)
throw std::runtime_error("setregid: unsupported effective group id " + std::to_string(egid));
return 0;
}
int APS5_VABI setgroups_nid_postfix(int count, const std::uint32_t* groups) {
if (count < 0 || count > freebsdGroupsMax) {
*__error_nid_postfix() = errnoInvalidArgument;
return -1;
}
if (count > 0 && groups == nullptr) {
*__error_nid_postfix() = errnoBadAddress;
return -1;
}
if (count > 0 && groups[0] != 0)
throw std::runtime_error("setgroups: unsupported effective group id " + std::to_string(groups[0]));
std::lock_guard lock(supplementaryGroupsMutex);
if (count == 0) {
supplementaryGroups.resize(1);
return 0;
}
supplementaryGroups.assign(groups, groups + count);
return 0;
}
int APS5_VABI getgroups_nid_postfix(int capacity, std::uint32_t* groups) {
std::lock_guard lock(supplementaryGroupsMutex);
const int count = static_cast<int>(supplementaryGroups.size());
if (capacity == 0) return count;
if (capacity < count) {
*__error_nid_postfix() = errnoInvalidArgument;
return -1;
}
if (groups == nullptr) {
*__error_nid_postfix() = errnoBadAddress;
return -1;
}
std::copy(supplementaryGroups.begin(), supplementaryGroups.end(), groups);
return count;
}
}