Add SetPosixErrno function

I used a switch statement for future proofing, as some codes differ between Windows, Mac, Linux, or Orbis. Right now I've only added the codes that should be possible to encounter.
This commit is contained in:
Stephen Miller 2024-08-16 17:20:21 -05:00
parent 93f14e8ae9
commit 9fce6f7c01
3 changed files with 39 additions and 111 deletions

View File

@ -125,6 +125,37 @@ int ErrnoToSceKernelError(int e) {
return res > SCE_KERNEL_ERROR_ESTOP ? SCE_KERNEL_ERROR_UNKNOWN : res; return res > SCE_KERNEL_ERROR_ESTOP ? SCE_KERNEL_ERROR_UNKNOWN : res;
} }
void SetPosixErrno(int e) {
//Some error numbers are different between supported OSes or the PS4
switch (e) {
case EPERM:
g_posix_errno = POSIX_EPERM;
break;
case EAGAIN:
g_posix_errno = POSIX_EAGAIN;
break;
case ENOMEM:
g_posix_errno = POSIX_ENOMEM;
break;
case EINVAL:
g_posix_errno = POSIX_EINVAL;
break;
case ENOSPC:
g_posix_errno = POSIX_ENOSPC;
break;
case ERANGE:
g_posix_errno = POSIX_ERANGE;
break;
case EDEADLK:
g_posix_errno = POSIX_EDEADLK;
break;
case ETIMEDOUT:
g_posix_errno = POSIX_ETIMEDOUT;
break;
default:
g_posix_errno = e;
}
}
int PS4_SYSV_ABI sceKernelMmap(void* addr, u64 len, int prot, int flags, int fd, size_t offset, int PS4_SYSV_ABI sceKernelMmap(void* addr, u64 len, int prot, int flags, int fd, size_t offset,
void** res) { void** res) {
LOG_INFO(Kernel_Vmm, "called addr = {}, len = {}, prot = {}, flags = {}, fd = {}, offset = {}", LOG_INFO(Kernel_Vmm, "called addr = {}, len = {}, prot = {}, flags = {}, fd = {}, offset = {}",

View File

@ -14,6 +14,7 @@ namespace Libraries::Kernel {
void ErrSceToPosix(int result); void ErrSceToPosix(int result);
int ErrnoToSceKernelError(int e); int ErrnoToSceKernelError(int e);
void SetPosixErrno(int e);
struct OrbisTimesec { struct OrbisTimesec {
time_t t; time_t t;

View File

@ -1377,21 +1377,7 @@ int PS4_SYSV_ABI posix_pthread_detach(ScePthread thread) {
int PS4_SYSV_ABI posix_sem_init(sem_t* sem, int pshared, unsigned int value) { int PS4_SYSV_ABI posix_sem_init(sem_t* sem, int pshared, unsigned int value) {
int result = sem_init(sem, pshared, value); int result = sem_init(sem, pshared, value);
if (result == -1) { if (result == -1) {
switch (errno) { SetPosixErrno(errno);
case ENOMEM:
ErrSceToPosix(ORBIS_KERNEL_ERROR_ENOMEM);
break;
case EPERM:
ErrSceToPosix(ORBIS_KERNEL_ERROR_EPERM);
break;
case ENOSPC:
ErrSceToPosix(ORBIS_KERNEL_ERROR_ENOSPC);
break;
case EINVAL:
default:
ErrSceToPosix(ORBIS_KERNEL_ERROR_EINVAL);
break;
}
} }
return result; return result;
} }
@ -1399,21 +1385,7 @@ int PS4_SYSV_ABI posix_sem_init(sem_t* sem, int pshared, unsigned int value) {
int PS4_SYSV_ABI posix_sem_wait(sem_t* sem) { int PS4_SYSV_ABI posix_sem_wait(sem_t* sem) {
int result = sem_wait(sem); int result = sem_wait(sem);
if (result == -1) { if (result == -1) {
switch (errno) { SetPosixErrno(errno);
case ENOMEM:
ErrSceToPosix(ORBIS_KERNEL_ERROR_ENOMEM);
break;
case EPERM:
ErrSceToPosix(ORBIS_KERNEL_ERROR_EPERM);
break;
case ETIMEDOUT:
ErrSceToPosix(ORBIS_KERNEL_ERROR_ETIMEDOUT);
break;
case EINVAL:
default:
ErrSceToPosix(ORBIS_KERNEL_ERROR_EINVAL);
break;
}
} }
return result; return result;
} }
@ -1421,21 +1393,7 @@ int PS4_SYSV_ABI posix_sem_wait(sem_t* sem) {
int PS4_SYSV_ABI posix_sem_trywait(sem_t* sem) { int PS4_SYSV_ABI posix_sem_trywait(sem_t* sem) {
int result = sem_trywait(sem); int result = sem_trywait(sem);
if (result == -1) { if (result == -1) {
switch (errno) { SetPosixErrno(errno);
case ENOMEM:
ErrSceToPosix(ORBIS_KERNEL_ERROR_ENOMEM);
break;
case EPERM:
ErrSceToPosix(ORBIS_KERNEL_ERROR_EPERM);
break;
case EAGAIN:
ErrSceToPosix(ORBIS_KERNEL_ERROR_EAGAIN);
break;
case EINVAL:
default:
ErrSceToPosix(ORBIS_KERNEL_ERROR_EINVAL);
break;
}
} }
return result; return result;
} }
@ -1473,21 +1431,7 @@ int sem_timedwait(sem_t* sem, const struct timespec* abstime) {
int PS4_SYSV_ABI posix_sem_timedwait(sem_t* sem, const timespec* t) { int PS4_SYSV_ABI posix_sem_timedwait(sem_t* sem, const timespec* t) {
int result = sem_timedwait(sem, t); int result = sem_timedwait(sem, t);
if (result == -1) { if (result == -1) {
switch (errno) { SetPosixErrno(errno);
case ENOMEM:
ErrSceToPosix(ORBIS_KERNEL_ERROR_ENOMEM);
break;
case EPERM:
ErrSceToPosix(ORBIS_KERNEL_ERROR_EPERM);
break;
case ETIMEDOUT:
ErrSceToPosix(ORBIS_KERNEL_ERROR_ETIMEDOUT);
break;
case EINVAL:
default:
ErrSceToPosix(ORBIS_KERNEL_ERROR_EINVAL);
break;
}
} }
return result; return result;
} }
@ -1495,21 +1439,7 @@ int PS4_SYSV_ABI posix_sem_timedwait(sem_t* sem, const timespec* t) {
int PS4_SYSV_ABI posix_sem_post(sem_t* sem) { int PS4_SYSV_ABI posix_sem_post(sem_t* sem) {
int result = sem_post(sem); int result = sem_post(sem);
if (result == -1) { if (result == -1) {
switch (errno) { SetPosixErrno(errno);
case ENOMEM:
ErrSceToPosix(ORBIS_KERNEL_ERROR_ENOMEM);
break;
case EPERM:
ErrSceToPosix(ORBIS_KERNEL_ERROR_EPERM);
break;
case ERANGE:
ErrSceToPosix(ORBIS_KERNEL_ERROR_ERANGE);
break;
case EINVAL:
default:
ErrSceToPosix(ORBIS_KERNEL_ERROR_EINVAL);
break;
}
} }
return result; return result;
} }
@ -1517,24 +1447,7 @@ int PS4_SYSV_ABI posix_sem_post(sem_t* sem) {
int PS4_SYSV_ABI posix_sem_destroy(sem_t* sem) { int PS4_SYSV_ABI posix_sem_destroy(sem_t* sem) {
int result = sem_destroy(sem); int result = sem_destroy(sem);
if (result == -1) { if (result == -1) {
switch (errno) { SetPosixErrno(errno);
case ENOMEM:
ErrSceToPosix(ORBIS_KERNEL_ERROR_ENOMEM);
break;
case EPERM:
ErrSceToPosix(ORBIS_KERNEL_ERROR_EPERM);
break;
case ETIMEDOUT:
ErrSceToPosix(ORBIS_KERNEL_ERROR_ETIMEDOUT);
break;
case EDEADLK:
ErrSceToPosix(ORBIS_KERNEL_ERROR_EDEADLK);
break;
case EINVAL:
default:
ErrSceToPosix(ORBIS_KERNEL_ERROR_EINVAL);
break;
}
} }
return result; return result;
} }
@ -1542,24 +1455,7 @@ int PS4_SYSV_ABI posix_sem_destroy(sem_t* sem) {
int PS4_SYSV_ABI posix_sem_getvalue(sem_t* sem, int* sval) { int PS4_SYSV_ABI posix_sem_getvalue(sem_t* sem, int* sval) {
int result = sem_getvalue(sem, sval); int result = sem_getvalue(sem, sval);
if (result == -1) { if (result == -1) {
switch (errno) { SetPosixErrno(errno);
case ENOMEM:
ErrSceToPosix(ORBIS_KERNEL_ERROR_ENOMEM);
break;
case EPERM:
ErrSceToPosix(ORBIS_KERNEL_ERROR_EPERM);
break;
case ETIMEDOUT:
ErrSceToPosix(ORBIS_KERNEL_ERROR_ETIMEDOUT);
break;
case EDEADLK:
ErrSceToPosix(ORBIS_KERNEL_ERROR_EDEADLK);
break;
case EINVAL:
default:
ErrSceToPosix(ORBIS_KERNEL_ERROR_EINVAL);
break;
}
} }
return result; return result;
} }