2024-03-05 11:06:29 +00:00
|
|
|
/***
|
|
|
|
Copyright (C) 2024 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
|
|
|
|
|
|
|
File: AuProcAddresses.UNIX.cpp
|
|
|
|
Date: 2024-3-5
|
|
|
|
Author: Reece
|
|
|
|
***/
|
|
|
|
#include "RuntimeInternal.hpp"
|
|
|
|
#include "AuProcAddresses.UNIX.hpp"
|
2024-04-14 18:48:40 +00:00
|
|
|
#if defined(AURORA_IS_LINUX_DERIVED)
|
|
|
|
#include "AuProcAddresses.Linux.hpp"
|
|
|
|
#endif
|
|
|
|
|
2024-03-05 11:06:29 +00:00
|
|
|
#include <Source/Debug/ExceptionWatcher.Unix.hpp>
|
2024-04-14 18:48:40 +00:00
|
|
|
|
|
|
|
#include <unistd.h>
|
2024-03-19 15:47:42 +00:00
|
|
|
#include <sys/mman.h>
|
2024-03-05 11:06:29 +00:00
|
|
|
|
|
|
|
namespace Aurora::Process
|
|
|
|
{
|
|
|
|
void PosixForkResetLocks();
|
|
|
|
}
|
|
|
|
|
2024-03-19 15:47:42 +00:00
|
|
|
namespace Aurora::Memory
|
|
|
|
{
|
|
|
|
AuUInt32 RoundPageUp(AuUInt32 value);
|
|
|
|
}
|
|
|
|
|
2024-03-05 11:06:29 +00:00
|
|
|
namespace Aurora
|
|
|
|
{
|
2024-03-15 06:18:32 +00:00
|
|
|
int PosixOpen(const char *pathname, int flags, mode_t mode)
|
|
|
|
{
|
|
|
|
return ::open(pathname, flags, mode);
|
|
|
|
}
|
|
|
|
|
2024-03-05 11:06:29 +00:00
|
|
|
void PosixDoForkHooks()
|
|
|
|
{
|
|
|
|
Process::PosixForkResetLocks();
|
|
|
|
Debug::DeinitSignalHandlers();
|
|
|
|
}
|
|
|
|
|
2024-03-15 06:18:32 +00:00
|
|
|
void PosixShutup()
|
|
|
|
{
|
|
|
|
int fd = PosixOpen("/dev/null", O_RDWR);
|
|
|
|
::dup2(fd, STDERR_FILENO);
|
|
|
|
::dup2(fd, STDOUT_FILENO);
|
|
|
|
::dup2(fd, STDIN_FILENO);
|
|
|
|
::close(fd);
|
|
|
|
}
|
|
|
|
|
2024-04-14 18:48:40 +00:00
|
|
|
void PosixFDYeetus()
|
|
|
|
{
|
|
|
|
#if defined(AURORA_IS_BSD_DERIVED)
|
|
|
|
closefrom(STDERR_FILENO + 1);
|
|
|
|
#elif defined(AURORA_IS_LINUX_DERIVED)
|
|
|
|
close_range(STDERR_FILENO + 1, UINT_MAX, 0);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2024-03-05 11:06:29 +00:00
|
|
|
void PosixTerminate()
|
|
|
|
{
|
2024-03-15 06:18:32 +00:00
|
|
|
::killpg(0, SIGKILL);
|
2024-03-05 11:06:29 +00:00
|
|
|
}
|
2024-03-05 18:37:44 +00:00
|
|
|
|
|
|
|
static bool PosixLseek63(int fd, AuUInt64 offset, int whence, AuUInt64 *pOffset)
|
|
|
|
{
|
|
|
|
if (offset > std::numeric_limits<off_t>::max())
|
|
|
|
{
|
|
|
|
SysPushErrorIO("int overflow exploit?");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(AURORA_IS_LINUX_DERIVED)
|
|
|
|
auto ret = lseek64(fd, offset, whence);
|
|
|
|
#elif defined(AURORA_IS_64BIT)
|
|
|
|
static_assert(std::numeric_limits<off_t>::max() >= std::numeric_limits<AuInt64>::max(), "unsupported posix os");
|
|
|
|
auto ret = lseek(fd, offset, whence);
|
|
|
|
#elif defined(AURORA_FORCE_32BIT_FILESYSTEMS_ONLY)
|
|
|
|
auto ret = lseek(fd, (off_t)offset, whence);
|
|
|
|
#else
|
|
|
|
#error ancient 32-bit posix operating systems require 64-bit file awareness
|
|
|
|
#endif
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
// Intentionally omitted (thank linuxs' mess of incomplete filesystems)
|
|
|
|
// SysPushErrorIO("PosixLseek63 IO Error: {}", ret);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pOffset)
|
|
|
|
{
|
|
|
|
*pOffset = ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-03-06 20:29:43 +00:00
|
|
|
AuUInt64 PosixGetLength(int fd)
|
2024-03-05 18:37:44 +00:00
|
|
|
{
|
|
|
|
AuUInt64 ret {}, old {};
|
|
|
|
bool status {};
|
|
|
|
|
|
|
|
if (!PosixLseek63(fd, 0, SEEK_CUR, &old))
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
status = PosixLseek63(fd, 0, SEEK_END, &ret);
|
|
|
|
status &= PosixLseek63(fd, old, SEEK_SET, nullptr);
|
|
|
|
return status ? ret : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
AuUInt64 PosixGetOffset(int fd)
|
|
|
|
{
|
|
|
|
AuUInt64 ret;
|
|
|
|
if (!PosixLseek63(fd, 0, SEEK_CUR, &ret))
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PosixSetOffset(int fd, AuUInt64 offset)
|
|
|
|
{
|
|
|
|
return PosixLseek63(fd, offset, SEEK_SET, nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PosixRead(int fd, void *buf, AuUInt32 count, AuUInt32 *pRead)
|
|
|
|
{
|
|
|
|
AuSInt ret;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
ret = ::read(fd, buf, count);
|
|
|
|
}
|
|
|
|
while ((ret == -1 && errno == EINTR));
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
SysPushErrorIO("PosixRead IO Error: {}", errno);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pRead)
|
|
|
|
{
|
|
|
|
*pRead = ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PosixWrite(int fd, const void *buf, AuUInt32 count, AuUInt32 *pWritten)
|
|
|
|
{
|
|
|
|
AuSInt ret;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
ret = ::write(fd, buf, count);
|
|
|
|
}
|
|
|
|
while ((ret == -1 && errno == EINTR));
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
SysPushErrorIO("PosixWrite IO Error: {}", errno);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pWritten)
|
|
|
|
{
|
|
|
|
*pWritten = ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2024-03-07 06:49:23 +00:00
|
|
|
|
2024-03-27 03:12:49 +00:00
|
|
|
void PosixWriteEoS(int fd)
|
|
|
|
{
|
|
|
|
::ftruncate(fd, PosixGetOffset(fd));
|
|
|
|
}
|
|
|
|
|
|
|
|
void SysWriteEoS(AuUInt uOSHandle)
|
|
|
|
{
|
|
|
|
PosixWriteEoS(uOSHandle);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SysCloseHandle(AuUInt uOSHandle)
|
|
|
|
{
|
|
|
|
::close(uOSHandle);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SysHandleIsNonZero(AuUInt uOSHandle)
|
|
|
|
{
|
|
|
|
#if defined(AURORA_IS_64BIT)
|
|
|
|
return uOSHandle && (uOSHandle != 0xFFFFFFFFull) && (uOSHandle != 0xFFFFFFFFFFFFFFFFull);
|
|
|
|
#else
|
|
|
|
return uOSHandle && (uOSHandle != 0xFFFFFFFF);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void SysFlushHandle(AuUInt uOSHandle)
|
|
|
|
{
|
|
|
|
::fsync(uOSHandle);
|
|
|
|
}
|
|
|
|
|
|
|
|
int PosixUnlink(const char *pathname)
|
|
|
|
{
|
|
|
|
return ::unlink(pathname);
|
|
|
|
}
|
|
|
|
|
2024-03-07 06:49:23 +00:00
|
|
|
AuUInt64 SysGetFileLength(AuUInt uOSHandle)
|
|
|
|
{
|
|
|
|
return PosixGetLength(uOSHandle);
|
|
|
|
}
|
2024-03-19 15:47:42 +00:00
|
|
|
|
|
|
|
void *SysAllocateLarge(AuUInt uLength)
|
|
|
|
{
|
|
|
|
uLength = AuMemory::RoundPageUp(uLength);
|
|
|
|
return ::mmap(0, uLength, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SysAllocateFree(void *pBuffer, AuUInt uLength)
|
|
|
|
{
|
|
|
|
uLength = AuMemory::RoundPageUp(uLength);
|
|
|
|
::munmap(pBuffer, uLength);
|
|
|
|
}
|
2024-04-09 21:29:11 +00:00
|
|
|
|
|
|
|
bool SysMemoryLockPages(const void *pVoid, AuUInt uLength)
|
|
|
|
{
|
|
|
|
return ::mlock(pVoid, uLength) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SysMemoryUnlockPages(const void *pVoid, AuUInt uLength)
|
|
|
|
{
|
|
|
|
return ::munlock(pVoid, uLength) == 0;
|
|
|
|
}
|
2024-03-05 11:06:29 +00:00
|
|
|
}
|