Work around Rosetta signal handling issue (#455)

Rosetta does something strange to the signal handling registers but setting SA_SIGINFO prevents the issue from happening. Detect Rosetta and set the flag when necessary.
This commit is contained in:
Daniil Kulchenko 2022-08-19 09:45:56 -07:00
parent 1ce101c5a5
commit a09500b979
2 changed files with 25 additions and 0 deletions

View file

@ -182,6 +182,7 @@ int symlinkat(const char *, int, const char *);
int sync_file_range(int, int64_t, int64_t, unsigned);
int sys_ptrace(int, ...);
int sysctl(const int *, unsigned, void *, size_t *, void *, size_t);
int sysctlbyname(const char *, size_t, void *, size_t *, void *, size_t);
int tgkill(int, int, int);
int tkill(int, int);
int tmpfd(void);

View file

@ -7174,8 +7174,32 @@ static int WindowsReplThread(void *arg, int tid) {
return 0;
}
static int IsRosetta() {
if(!IsXnu()) {
return 0;
}
int ret = 0;
size_t size = sizeof(ret);
if(sysctlbyname("sysctl.proc_translated", sizeof("sysctl.proc_translated"), &ret, &size, NULL, 0) == -1) {
if(errno == ENOENT)
return 0;
return -1;
}
return ret;
}
static void InstallSignalHandler(int sig, void *handler) {
struct sigaction sa = {.sa_sigaction = handler};
if(IsRosetta()) {
// mitigate Rosetta signal handling strangeness
// https://github.com/jart/cosmopolitan/issues/455
sa.sa_flags = SA_SIGINFO;
}
CHECK_NE(-1, sigaction(sig, &sa, 0));
}