831 lines
26 KiB
C
831 lines
26 KiB
C
/**
|
|
* @PROJECT AerScript Interpreter
|
|
* @COPYRIGHT See COPYING in the top level directory
|
|
* @FILE modules/system/system.c
|
|
* @DESCRIPTION Static System class module for AerScript Interpreter
|
|
* @DEVELOPERS Rafal Kupiec <belliash@codingworkshop.eu.org>
|
|
*/
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#ifdef __WINNT__
|
|
#include <windows.h>
|
|
#include <sysinfoapi.h>
|
|
#include <processthreadsapi.h>
|
|
#include <winnt.h>
|
|
#include <wchar.h>
|
|
#else
|
|
#include <errno.h>
|
|
#include <pwd.h>
|
|
#include <time.h>
|
|
#include <unistd.h>
|
|
#include <sys/utsname.h>
|
|
extern char **environ;
|
|
#endif
|
|
#include "system.h"
|
|
|
|
#define SCLS_SYSTEM "System"
|
|
#define SCLS_EXC "SystemException"
|
|
|
|
#ifdef __WINNT__
|
|
#define SYS_NEW_LINE "\r\n"
|
|
#else
|
|
#define SYS_NEW_LINE "\n"
|
|
#endif
|
|
|
|
/* ------------------------------------------------------------------ */
|
|
/* Platform helpers */
|
|
/* ------------------------------------------------------------------ */
|
|
/* Throw the two SystemException flavours used all over this module. Both
|
|
* helpers return the exact status of the core-owned throw routine. */
|
|
static int SystemArgError(ph7_context *pCtx, const char *zMsg) {
|
|
return PH7_VmThrowNativeException(pCtx, SCLS_EXC, SYS_ERROR_ARGUMENT, zMsg);
|
|
}
|
|
static int SystemIoError(ph7_context *pCtx, const char *zMsg) {
|
|
return PH7_VmThrowNativeException(pCtx, SCLS_EXC, SYS_ERROR_IO, zMsg);
|
|
}
|
|
|
|
/* Validate a variable name shared by GetEnvironmentVariable() and
|
|
* SetEnvironmentVariable(): non-empty, no '=' and no embedded NUL. */
|
|
static int SystemEnvName(ph7_context *pCtx, ph7_value *pArg, const char **pzName) {
|
|
const char *zData;
|
|
int nByte = 0;
|
|
if(pArg == 0 || (pArg->nType & MEMOBJ_NULL) || !ph7_value_is_string(pArg)) {
|
|
return SystemArgError(pCtx, "Environment variable name must be a string");
|
|
}
|
|
zData = ph7_value_to_string(pArg, &nByte);
|
|
if(zData == 0 || nByte < 1 || memchr(zData, '=', (size_t)nByte) != 0 ||
|
|
memchr(zData, '\0', (size_t)nByte) != 0) {
|
|
return SystemArgError(pCtx, "Invalid environment variable name");
|
|
}
|
|
*pzName = zData;
|
|
return PH7_OK;
|
|
}
|
|
|
|
/* Compilation-target architecture of the running process: 64-bit spellings
|
|
* are checked before their 32-bit counterparts, as required by the contract. */
|
|
static const char *SystemProcessArch(void) {
|
|
#if defined(__x86_64__) || defined(__amd64__) || defined(_M_X64)
|
|
return "x64";
|
|
#elif defined(__aarch64__) || defined(__arm64__) || defined(_M_ARM64)
|
|
return "arm64";
|
|
#elif defined(__i386__) || defined(_M_IX86)
|
|
return "x86";
|
|
#elif defined(__arm__) || defined(_M_ARM)
|
|
return "arm";
|
|
#else
|
|
return "Unknown";
|
|
#endif
|
|
}
|
|
#ifndef __WINNT__
|
|
/* Map uname(2)'s machine field onto the four contract spellings. */
|
|
static const char *SystemArchFromMachine(const char *zMachine) {
|
|
if(zMachine == 0) {
|
|
return "Unknown";
|
|
}
|
|
if(strcmp(zMachine, "x86_64") == 0 || strcmp(zMachine, "amd64") == 0 ||
|
|
strcmp(zMachine, "AMD64") == 0) {
|
|
return "x64";
|
|
}
|
|
if(strcmp(zMachine, "aarch64") == 0 || strcmp(zMachine, "arm64") == 0) {
|
|
return "arm64";
|
|
}
|
|
if(strcmp(zMachine, "i386") == 0 || strcmp(zMachine, "i486") == 0 ||
|
|
strcmp(zMachine, "i586") == 0 || strcmp(zMachine, "i686") == 0 ||
|
|
strcmp(zMachine, "x86") == 0) {
|
|
return "x86";
|
|
}
|
|
if(strncmp(zMachine, "arm", 3) == 0) {
|
|
return "arm";
|
|
}
|
|
return "Unknown";
|
|
}
|
|
#endif
|
|
/* Architecture of the host, not of the compiled interpreter. */
|
|
static const char *SystemOSArch(void) {
|
|
#ifdef __WINNT__
|
|
SYSTEM_INFO sInfo;
|
|
GetNativeSystemInfo(&sInfo);
|
|
switch(sInfo.wProcessorArchitecture) {
|
|
case PROCESSOR_ARCHITECTURE_AMD64: return "x64";
|
|
#ifdef PROCESSOR_ARCHITECTURE_ARM64
|
|
case PROCESSOR_ARCHITECTURE_ARM64: return "arm64";
|
|
#endif
|
|
case PROCESSOR_ARCHITECTURE_INTEL: return "x86";
|
|
case PROCESSOR_ARCHITECTURE_ARM: return "arm";
|
|
default: return "Unknown";
|
|
}
|
|
#else
|
|
struct utsname sInfo;
|
|
if(uname(&sInfo) != 0) {
|
|
return "Unknown";
|
|
}
|
|
return SystemArchFromMachine(sInfo.machine);
|
|
#endif
|
|
}
|
|
static int SystemProcessorCount(void) {
|
|
sxu64 nCount = 0;
|
|
#ifdef __WINNT__
|
|
#ifdef ALL_PROCESSOR_GROUPS
|
|
nCount = (sxu64)GetActiveProcessorCount(ALL_PROCESSOR_GROUPS);
|
|
#else
|
|
{
|
|
SYSTEM_INFO sInfo;
|
|
GetSystemInfo(&sInfo);
|
|
nCount = (sxu64)sInfo.dwNumberOfProcessors;
|
|
}
|
|
#endif
|
|
#else
|
|
{
|
|
/* sysconf() signals failure with -1; casting that to sxu64 would make
|
|
* the final > 0 test succeed and yield a negative processor count. */
|
|
long nRaw = sysconf(_SC_NPROCESSORS_ONLN);
|
|
nCount = nRaw > 0 ? (sxu64)nRaw : 0;
|
|
}
|
|
#endif
|
|
return nCount > 0 ? (int)nCount : 1;
|
|
}
|
|
static ph7_int64 SystemTickCount(void) {
|
|
#ifdef __WINNT__
|
|
return (ph7_int64)GetTickCount64();
|
|
#else
|
|
struct timespec sNow;
|
|
if(clock_gettime(CLOCK_MONOTONIC, &sNow) != 0) {
|
|
return 0;
|
|
}
|
|
return (ph7_int64)sNow.tv_sec * 1000 + (ph7_int64)(sNow.tv_nsec / 1000000L);
|
|
#endif
|
|
}
|
|
/* Sleep in milliseconds with EINTR resumption; iMs is always >= 0 here. */
|
|
static void SystemSleepMs(ph7_int64 iMs) {
|
|
#ifdef __WINNT__
|
|
Sleep((DWORD)iMs);
|
|
#else
|
|
struct timespec sReq, sRem;
|
|
sReq.tv_sec = (time_t)(iMs / 1000);
|
|
sReq.tv_nsec = (long)((iMs % 1000) * 1000000);
|
|
while(nanosleep(&sReq, &sRem) != 0) {
|
|
if(errno == EINTR) {
|
|
sReq = sRem;
|
|
continue;
|
|
}
|
|
break;
|
|
}
|
|
#endif
|
|
}
|
|
#ifdef __WINNT__
|
|
/* UTF-16 <-> UTF-8 conversion. Buffers are heap allocated and owned by the
|
|
* caller; the VFS does not expose wide-string environment helpers. */
|
|
static char *SystemUtf8FromWide(const WCHAR *zWide, int nWide) {
|
|
char *zOut;
|
|
int nByte;
|
|
if(nWide < 0) {
|
|
nWide = (int)wcslen(zWide);
|
|
}
|
|
if(nWide == 0) {
|
|
/* An empty environment value is a valid value, not a failure. */
|
|
char *zEmpty = (char *)malloc(1);
|
|
if(zEmpty) {
|
|
zEmpty[0] = '\0';
|
|
}
|
|
return zEmpty;
|
|
}
|
|
nByte = WideCharToMultiByte(CP_UTF8, 0, zWide, nWide, 0, 0, 0, 0);
|
|
if(nByte < 1) {
|
|
return 0;
|
|
}
|
|
zOut = (char *)malloc((size_t)nByte + 1);
|
|
if(zOut == 0) {
|
|
return 0;
|
|
}
|
|
if(WideCharToMultiByte(CP_UTF8, 0, zWide, nWide, zOut, nByte, 0, 0) < 1) {
|
|
free(zOut);
|
|
return 0;
|
|
}
|
|
zOut[nByte] = '\0';
|
|
return zOut;
|
|
}
|
|
static WCHAR *SystemWideFromUtf8(const char *zUtf8) {
|
|
WCHAR *zOut;
|
|
int nByte, nWide;
|
|
if(zUtf8 == 0) {
|
|
return 0;
|
|
}
|
|
nByte = (int)strlen(zUtf8);
|
|
if(nByte == 0) {
|
|
/* Keep an empty value distinct from an absent variable: the caller
|
|
* passes this NUL-only buffer to SetEnvironmentVariableW(). */
|
|
zOut = (WCHAR *)malloc(sizeof(WCHAR));
|
|
if(zOut) {
|
|
zOut[0] = L'\0';
|
|
}
|
|
return zOut;
|
|
}
|
|
nWide = MultiByteToWideChar(CP_UTF8, 0, zUtf8, nByte, 0, 0);
|
|
if(nWide < 1) {
|
|
return 0;
|
|
}
|
|
zOut = (WCHAR *)malloc(((size_t)nWide + 1) * sizeof(WCHAR));
|
|
if(zOut == 0) {
|
|
return 0;
|
|
}
|
|
if(MultiByteToWideChar(CP_UTF8, 0, zUtf8, nByte, zOut, nWide) < 1) {
|
|
free(zOut);
|
|
return 0;
|
|
}
|
|
zOut[nWide] = L'\0';
|
|
return zOut;
|
|
}
|
|
static int SystemIsElevated(void) {
|
|
HANDLE hToken = 0;
|
|
TOKEN_ELEVATION sElevation;
|
|
DWORD nSize = sizeof(sElevation);
|
|
if(!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken)) {
|
|
return 0;
|
|
}
|
|
if(!GetTokenInformation(hToken, TokenElevation, &sElevation, sizeof(sElevation), &nSize)) {
|
|
CloseHandle(hToken);
|
|
return 0;
|
|
}
|
|
CloseHandle(hToken);
|
|
return sElevation.TokenIsElevated ? 1 : 0;
|
|
}
|
|
#endif
|
|
|
|
/* ------------------------------------------------------------------ */
|
|
/* Methods available under every SAPI */
|
|
/* ------------------------------------------------------------------ */
|
|
static int System_GetEnvironmentVariable(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
#ifndef __WINNT__
|
|
const ph7_vfs *pVfs;
|
|
#endif
|
|
const char *zName;
|
|
int rc;
|
|
rc = SystemEnvName(pCtx, nArg > 0 ? apArg[0] : 0, &zName);
|
|
if(rc != PH7_OK) {
|
|
return rc;
|
|
}
|
|
/* Absent variables (and a missing VFS) keep the NULL fallback result. */
|
|
(void)ph7_result_null(pCtx);
|
|
#ifdef __WINNT__
|
|
{
|
|
/* WinVfs_Getenv() maps GetEnvironmentVariableA() == 0 to "not found",
|
|
* which is also returned for a variable set to "". Query the wide API
|
|
* directly: only ERROR_ENVVAR_NOT_FOUND yields NULL, "" stays "". */
|
|
WCHAR *zWideName = SystemWideFromUtf8(zName);
|
|
DWORD nSize, nErr;
|
|
if(zWideName == 0) {
|
|
return SystemIoError(pCtx, "Unable to convert the variable name");
|
|
}
|
|
nSize = GetEnvironmentVariableW(zWideName, 0, 0);
|
|
nErr = GetLastError();
|
|
if(nSize == 0) {
|
|
free(zWideName);
|
|
if(nErr == ERROR_ENVVAR_NOT_FOUND) {
|
|
return PH7_OK;
|
|
}
|
|
return SystemIoError(pCtx, "Unable to read the environment variable");
|
|
}
|
|
{
|
|
WCHAR *zBuf = (WCHAR *)malloc(((size_t)nSize + 1) * sizeof(WCHAR));
|
|
if(zBuf == 0) {
|
|
free(zWideName);
|
|
return PH7_ABORT;
|
|
}
|
|
(void)GetEnvironmentVariableW(zWideName, zBuf, nSize);
|
|
free(zWideName);
|
|
{
|
|
char *zUtf = SystemUtf8FromWide(zBuf, -1);
|
|
free(zBuf);
|
|
if(zUtf == 0) {
|
|
return SystemIoError(pCtx, "Unable to convert the environment variable value");
|
|
}
|
|
rc = ph7_result_string(pCtx, zUtf, -1);
|
|
free(zUtf);
|
|
return rc;
|
|
}
|
|
}
|
|
}
|
|
#else
|
|
pVfs = pCtx->pVm->pEngine->pVfs;
|
|
if(pVfs == 0 || pVfs->xGetenv == 0) {
|
|
return PH7_OK;
|
|
}
|
|
(void)pVfs->xGetenv(zName, pCtx);
|
|
return PH7_OK;
|
|
#endif
|
|
}
|
|
static int System_GetOSPlatform(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
SXUNUSED(nArg);
|
|
SXUNUSED(apArg);
|
|
#if defined(__WINNT__)
|
|
return ph7_result_string(pCtx, "Windows", sizeof("Windows") - 1);
|
|
#elif defined(__APPLE__)
|
|
return ph7_result_string(pCtx, "macOS", sizeof("macOS") - 1);
|
|
#elif defined(__FreeBSD__)
|
|
return ph7_result_string(pCtx, "FreeBSD", sizeof("FreeBSD") - 1);
|
|
#elif defined(__OpenBSD__)
|
|
return ph7_result_string(pCtx, "OpenBSD", sizeof("OpenBSD") - 1);
|
|
#elif defined(__NetBSD__)
|
|
return ph7_result_string(pCtx, "NetBSD", sizeof("NetBSD") - 1);
|
|
#elif defined(__linux__)
|
|
return ph7_result_string(pCtx, "Linux", sizeof("Linux") - 1);
|
|
#else
|
|
return ph7_result_string(pCtx, "Unknown", sizeof("Unknown") - 1);
|
|
#endif
|
|
}
|
|
static int System_GetOSArchitecture(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
SXUNUSED(nArg);
|
|
SXUNUSED(apArg);
|
|
return ph7_result_string(pCtx, SystemOSArch(), -1);
|
|
}
|
|
static int System_GetProcessArchitecture(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
SXUNUSED(nArg);
|
|
SXUNUSED(apArg);
|
|
return ph7_result_string(pCtx, SystemProcessArch(), -1);
|
|
}
|
|
static int System_GetProcessorCount(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
SXUNUSED(nArg);
|
|
SXUNUSED(apArg);
|
|
return ph7_result_int(pCtx, SystemProcessorCount());
|
|
}
|
|
static int System_GetTickCount(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
SXUNUSED(nArg);
|
|
SXUNUSED(apArg);
|
|
return ph7_result_int64(pCtx, SystemTickCount());
|
|
}
|
|
/* nWhich: 0 = current, 1 = peak, 2 = configured limit. */
|
|
static int SystemMemory(ph7_context *pCtx, int nWhich) {
|
|
const SyMemHeap *pHeap = pCtx->pVm->sAllocator.pHeap;
|
|
ph7_int64 iValue = 0;
|
|
if(pHeap != 0) {
|
|
if(nWhich == 0) {
|
|
iValue = (ph7_int64)pHeap->nSize;
|
|
} else if(nWhich == 1) {
|
|
iValue = (ph7_int64)pHeap->nPeak;
|
|
} else {
|
|
iValue = (ph7_int64)pHeap->nLimit;
|
|
}
|
|
}
|
|
return ph7_result_int64(pCtx, iValue);
|
|
}
|
|
static int System_GetMemoryUsage(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
SXUNUSED(nArg);
|
|
SXUNUSED(apArg);
|
|
return SystemMemory(pCtx, 0);
|
|
}
|
|
static int System_GetPeakMemoryUsage(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
SXUNUSED(nArg);
|
|
SXUNUSED(apArg);
|
|
return SystemMemory(pCtx, 1);
|
|
}
|
|
static int System_GetMemoryLimit(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
SXUNUSED(nArg);
|
|
SXUNUSED(apArg);
|
|
return SystemMemory(pCtx, 2);
|
|
}
|
|
static int System_Sleep(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
ph7_int64 iMs;
|
|
if(nArg < 1 || apArg[0] == 0 || (apArg[0]->nType & MEMOBJ_NULL) ||
|
|
!ph7_value_is_int(apArg[0])) {
|
|
return SystemArgError(pCtx, "Sleep() expects an integer number of milliseconds");
|
|
}
|
|
iMs = ph7_value_to_int64(apArg[0]);
|
|
if(iMs < 0) {
|
|
return SystemArgError(pCtx, "Sleep() rejects a negative delay");
|
|
}
|
|
if(iMs > 0) {
|
|
SystemSleepMs(iMs);
|
|
}
|
|
return PH7_OK;
|
|
}
|
|
static int System_Exit(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
sxi32 iStatus = 0;
|
|
if(nArg > 0) {
|
|
if(apArg[0] == 0 || (apArg[0]->nType & MEMOBJ_NULL) ||
|
|
!ph7_value_is_int(apArg[0])) {
|
|
return SystemArgError(pCtx, "Exit() expects an integer status code");
|
|
}
|
|
iStatus = ph7_value_to_int(apArg[0]);
|
|
}
|
|
/* Under a future FastCGI SAPI this must end the current request, not the
|
|
* process; the VM's existing PH7_ABORT path owns shutdown handlers and
|
|
* destructors in both cases. */
|
|
pCtx->pVm->iExitStatus = iStatus;
|
|
return PH7_ABORT;
|
|
}
|
|
|
|
/* ------------------------------------------------------------------ */
|
|
/* Methods registered only under the "cli" SAPI */
|
|
/* ------------------------------------------------------------------ */
|
|
static int System_GetEnvironmentVariables(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
ph7_value *pArray;
|
|
SXUNUSED(nArg);
|
|
SXUNUSED(apArg);
|
|
pArray = ph7_context_new_array(pCtx);
|
|
if(pArray == 0) {
|
|
return PH7_ABORT;
|
|
}
|
|
#ifdef __WINNT__
|
|
{
|
|
LPWCH pEnv = GetEnvironmentStringsW();
|
|
LPWCH pCur;
|
|
if(pEnv == 0) {
|
|
return SystemIoError(pCtx, "Unable to read the environment block");
|
|
}
|
|
for(pCur = pEnv ; *pCur != 0 ; ) {
|
|
WCHAR *zEq = wcschr(pCur, L'=');
|
|
if(zEq != 0 && zEq != pCur) {
|
|
ph7_value *pKey = ph7_context_new_scalar(pCtx);
|
|
ph7_value *pVal = ph7_context_new_scalar(pCtx);
|
|
char *zName = SystemUtf8FromWide(pCur, (int)(zEq - pCur));
|
|
char *zValue = SystemUtf8FromWide(zEq + 1, -1);
|
|
if(pKey == 0 || pVal == 0 || zName == 0 || zValue == 0) {
|
|
free(zName);
|
|
free(zValue);
|
|
FreeEnvironmentStringsW(pEnv);
|
|
return PH7_ABORT;
|
|
}
|
|
(void)ph7_value_string(pKey, zName, -1);
|
|
(void)ph7_value_string(pVal, zValue, -1);
|
|
if(ph7_array_add_elem(pArray, pKey, pVal) != PH7_OK) {
|
|
free(zName);
|
|
free(zValue);
|
|
FreeEnvironmentStringsW(pEnv);
|
|
return PH7_ABORT;
|
|
}
|
|
free(zName);
|
|
free(zValue);
|
|
}
|
|
pCur += wcslen(pCur) + 1;
|
|
}
|
|
FreeEnvironmentStringsW(pEnv);
|
|
}
|
|
#else
|
|
{
|
|
char **pEnv;
|
|
for(pEnv = environ ; pEnv != 0 && *pEnv != 0 ; ++pEnv) {
|
|
const char *zEntry = *pEnv;
|
|
const char *zEq = strchr(zEntry, '=');
|
|
ph7_value *pKey, *pVal;
|
|
if(zEq == 0 || zEq == zEntry) {
|
|
continue;
|
|
}
|
|
pKey = ph7_context_new_scalar(pCtx);
|
|
pVal = ph7_context_new_scalar(pCtx);
|
|
if(pKey == 0 || pVal == 0) {
|
|
return PH7_ABORT;
|
|
}
|
|
(void)ph7_value_string(pKey, zEntry, (int)(zEq - zEntry));
|
|
(void)ph7_value_string(pVal, zEq + 1, -1);
|
|
if(ph7_array_add_elem(pArray, pKey, pVal) != PH7_OK) {
|
|
return PH7_ABORT;
|
|
}
|
|
}
|
|
}
|
|
#endif
|
|
/* Stamp the primitive element type: the declared string[] result must
|
|
* match exactly, else VmCheckReturn() rejects the array on return. */
|
|
pArray->nType = MEMOBJ_STRING | MEMOBJ_HASHMAP;
|
|
return ph7_result_value(pCtx, pArray);
|
|
}
|
|
static int System_SetEnvironmentVariable(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
#ifndef __WINNT__
|
|
const ph7_vfs *pVfs;
|
|
#endif
|
|
const char *zName;
|
|
/* A NULL (or missing) value removes the variable. A NULL literal passed
|
|
* through a MIXED formal arrives as MEMOBJ_MIXED | MEMOBJ_VOID. */
|
|
int bRemove = (nArg < 2 || apArg[1] == 0 ||
|
|
(apArg[1]->nType & (MEMOBJ_NULL | MEMOBJ_VOID))) ? 1 : 0;
|
|
int rc;
|
|
rc = SystemEnvName(pCtx, nArg > 0 ? apArg[0] : 0, &zName);
|
|
if(rc != PH7_OK) {
|
|
return rc;
|
|
}
|
|
if(!bRemove && !ph7_value_is_string(apArg[1])) {
|
|
return SystemArgError(pCtx, "Environment variable value must be a string or NULL");
|
|
}
|
|
#ifdef __WINNT__
|
|
{
|
|
/* Keep both Windows paths wide: the VFS xSetenv() uses the ANSI API
|
|
* and would store raw UTF-8 bytes that the W-based enumeration then
|
|
* decodes as UTF-16, breaking non-ASCII round trips. */
|
|
WCHAR *zWideName = SystemWideFromUtf8(zName);
|
|
WCHAR *zWideValue = 0;
|
|
BOOL bOk;
|
|
if(zWideName == 0) {
|
|
return SystemIoError(pCtx, "Unable to convert the variable name");
|
|
}
|
|
if(!bRemove) {
|
|
zWideValue = SystemWideFromUtf8(ph7_value_to_string(apArg[1], 0));
|
|
if(zWideValue == 0) {
|
|
free(zWideName);
|
|
return SystemIoError(pCtx, "Unable to convert the environment variable value");
|
|
}
|
|
}
|
|
/* A NULL value pointer removes the variable. */
|
|
bOk = SetEnvironmentVariableW(zWideName, zWideValue);
|
|
free(zWideValue);
|
|
free(zWideName);
|
|
if(!bOk) {
|
|
return SystemIoError(pCtx, bRemove ?
|
|
"Unable to remove the environment variable" :
|
|
"Unable to set the environment variable");
|
|
}
|
|
return PH7_OK;
|
|
}
|
|
#else
|
|
if(bRemove) {
|
|
if(unsetenv(zName) != 0) {
|
|
return SystemIoError(pCtx, "Unable to remove the environment variable");
|
|
}
|
|
return PH7_OK;
|
|
}
|
|
pVfs = pCtx->pVm->pEngine->pVfs;
|
|
if(pVfs == 0 || pVfs->xSetenv == 0) {
|
|
return SystemIoError(pCtx, "Environment writes are not available in the underlying VFS");
|
|
}
|
|
if(pVfs->xSetenv(zName, ph7_value_to_string(apArg[1], 0)) != PH7_OK) {
|
|
return SystemIoError(pCtx, "Unable to set the environment variable");
|
|
}
|
|
return PH7_OK;
|
|
#endif
|
|
}
|
|
static int System_GetProcessId(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
const ph7_vfs *pVfs;
|
|
SXUNUSED(nArg);
|
|
SXUNUSED(apArg);
|
|
pVfs = pCtx->pVm->pEngine->pVfs;
|
|
return ph7_result_int64(pCtx, pVfs != 0 && pVfs->xProcessId != 0 ?
|
|
(ph7_int64)pVfs->xProcessId() : 0);
|
|
}
|
|
static int System_GetUserName(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
SXUNUSED(nArg);
|
|
SXUNUSED(apArg);
|
|
#ifdef __WINNT__
|
|
{
|
|
WCHAR zBuf[256];
|
|
DWORD nSize = (DWORD)SX_ARRAYSIZE(zBuf);
|
|
char *zUtf;
|
|
int rc;
|
|
if(!GetUserNameW(zBuf, &nSize)) {
|
|
return SystemIoError(pCtx, "Unable to determine the current user name");
|
|
}
|
|
zUtf = SystemUtf8FromWide(zBuf, (int)nSize - 1);
|
|
if(zUtf == 0) {
|
|
return SystemIoError(pCtx, "Unable to convert the current user name");
|
|
}
|
|
rc = ph7_result_string(pCtx, zUtf, -1);
|
|
free(zUtf);
|
|
return rc;
|
|
}
|
|
#else
|
|
{
|
|
/* The VFS xUsername() helper uses the real UID; the contract asks for
|
|
* the effective user, so query getpwuid(geteuid()) directly. */
|
|
struct passwd *pPwd = getpwuid(geteuid());
|
|
if(pPwd == 0 || pPwd->pw_name == 0) {
|
|
return SystemIoError(pCtx, "Unable to determine the current user name");
|
|
}
|
|
return ph7_result_string(pCtx, pPwd->pw_name, -1);
|
|
}
|
|
#endif
|
|
}
|
|
static int System_IsPrivilegedProcess(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
SXUNUSED(nArg);
|
|
SXUNUSED(apArg);
|
|
#ifdef __WINNT__
|
|
return ph7_result_bool(pCtx, SystemIsElevated());
|
|
#else
|
|
return ph7_result_bool(pCtx, geteuid() == 0 ? 1 : 0);
|
|
#endif
|
|
}
|
|
static int System_GetMachineName(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
SXUNUSED(nArg);
|
|
SXUNUSED(apArg);
|
|
#ifdef __WINNT__
|
|
{
|
|
WCHAR zBuf[256];
|
|
DWORD nSize = (DWORD)SX_ARRAYSIZE(zBuf);
|
|
char *zUtf;
|
|
int rc;
|
|
if(!GetComputerNameExW(ComputerNameDnsHostname, zBuf, &nSize)) {
|
|
return SystemIoError(pCtx, "Unable to determine the machine name");
|
|
}
|
|
zUtf = SystemUtf8FromWide(zBuf, (int)nSize);
|
|
if(zUtf == 0) {
|
|
return SystemIoError(pCtx, "Unable to convert the machine name");
|
|
}
|
|
rc = ph7_result_string(pCtx, zUtf, -1);
|
|
free(zUtf);
|
|
return rc;
|
|
}
|
|
#else
|
|
{
|
|
char zBuf[256];
|
|
if(gethostname(zBuf, sizeof(zBuf) - 1) != 0) {
|
|
return SystemIoError(pCtx, "Unable to determine the machine name");
|
|
}
|
|
zBuf[sizeof(zBuf) - 1] = '\0';
|
|
return ph7_result_string(pCtx, zBuf, -1);
|
|
}
|
|
#endif
|
|
}
|
|
static int System_GetOSVersion(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
SXUNUSED(nArg);
|
|
SXUNUSED(apArg);
|
|
#ifdef __WINNT__
|
|
{
|
|
typedef LONG (WINAPI *RtlGetVersionFunc)(PRTL_OSVERSIONINFOW);
|
|
HMODULE hNtdll = GetModuleHandleW(L"ntdll.dll");
|
|
RtlGetVersionFunc xGetVersion;
|
|
RTL_OSVERSIONINFOW sInfo;
|
|
char zBuf[64];
|
|
if(hNtdll == 0) {
|
|
return SystemIoError(pCtx, "Unable to query the operating system version");
|
|
}
|
|
/* RtlGetVersion() bypasses the compatibility layer's fake version. */
|
|
xGetVersion = (RtlGetVersionFunc)GetProcAddress(hNtdll, "RtlGetVersion");
|
|
if(xGetVersion == 0) {
|
|
return SystemIoError(pCtx, "Unable to query the operating system version");
|
|
}
|
|
SyZero(&sInfo, sizeof(sInfo));
|
|
sInfo.dwOSVersionInfoSize = sizeof(sInfo);
|
|
if(xGetVersion(&sInfo) != 0) {
|
|
return SystemIoError(pCtx, "Unable to query the operating system version");
|
|
}
|
|
(void)snprintf(zBuf, sizeof(zBuf), "%lu.%lu.%lu",
|
|
(unsigned long)sInfo.dwMajorVersion, (unsigned long)sInfo.dwMinorVersion,
|
|
(unsigned long)sInfo.dwBuildNumber);
|
|
return ph7_result_string(pCtx, zBuf, -1);
|
|
}
|
|
#else
|
|
{
|
|
struct utsname sInfo;
|
|
if(uname(&sInfo) != 0) {
|
|
return SystemIoError(pCtx, "Unable to query the operating system version");
|
|
}
|
|
return ph7_result_string(pCtx, sInfo.release, -1);
|
|
}
|
|
#endif
|
|
}
|
|
|
|
/* ------------------------------------------------------------------ */
|
|
/* Class registration */
|
|
/* ------------------------------------------------------------------ */
|
|
static int SystemConstruct(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
SXUNUSED(pCtx);
|
|
SXUNUSED(nArg);
|
|
SXUNUSED(apArg);
|
|
return PH7_ABORT; /* final class: no public construction */
|
|
}
|
|
static int SystemExceptionConstruct(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
ph7_value *pResult = ph7_context_new_scalar(pCtx);
|
|
sxi32 rc;
|
|
SXUNUSED(nArg);
|
|
if(pResult == 0) {
|
|
return PH7_ABORT;
|
|
}
|
|
/* apArg[1] is filled by the VM from the registered default snapshot. */
|
|
rc = ph7_context_parent(pCtx, "__construct", 2, apArg, pResult);
|
|
return rc;
|
|
}
|
|
static sxi32 SystemRegisterClasses(ph7_vm *pVm) {
|
|
#define STY(T) { PH7_TYPE_##T, 0, 0 }
|
|
#define SYSC(N) { N, 0, PH7_MEMBER_PUBLIC, 0, 0 }
|
|
#define SYSM(NAME,RET,PARAMS,NPARAM,FUNC) \
|
|
{ NAME, STY(RET), PARAMS, NPARAM, FUNC, 0, PH7_MEMBER_PUBLIC | PH7_MEMBER_STATIC }
|
|
static ph7_constant_def aConst[] = {
|
|
SYSC("ERROR_NONE"), SYSC("ERROR_ARGUMENT"), SYSC("ERROR_IO"), SYSC("NEW_LINE")
|
|
};
|
|
static const int aConstValue[] = { SYS_ERROR_NONE, SYS_ERROR_ARGUMENT, SYS_ERROR_IO };
|
|
static ph7_param_def aExcCtor[] = {
|
|
{ "message", STY(STRING), 0, 0, 0, 0 },
|
|
{ "code", STY(INT), 0, 0, 0, 0 }
|
|
};
|
|
static const ph7_param_def aNameParam[] = { { "name", STY(STRING), 0, 0, 0, 0 } };
|
|
/* MIXED, not STRING: a STRING formal casts NULL to "" before the callback,
|
|
* but NULL is the "remove variable" sentinel here. */
|
|
static const ph7_param_def aSetParam[] = {
|
|
{ "name", STY(STRING), 0, 0, 0, 0 },
|
|
{ "value", STY(MIXED), 0, 0, 0, 0 }
|
|
};
|
|
static const ph7_param_def aIntParam[] = { { "milliseconds", STY(INT), 0, 0, 0, 0 } };
|
|
/* MIXED so a wrong-typed status reaches System_Exit() and raises
|
|
* SystemException(ERROR_ARGUMENT) instead of a VM overload error. */
|
|
static ph7_param_def aExitParam[] = { { "code", STY(MIXED), 0, 0, 0, 0 } };
|
|
static const ph7_method_def aExcMethod[] = {
|
|
{ "__construct", STY(VOID), aExcCtor, 2, SystemExceptionConstruct, 0, 0 }
|
|
};
|
|
static const ph7_method_def aMethodCommon[] = {
|
|
{ "__construct", STY(VOID), 0, 0, SystemConstruct, 0, PH7_MEMBER_PRIVATE },
|
|
/* MIXED, not STRING: a STRING return would cast the NULL sentinel for
|
|
* an absent variable into an empty string (VmCheckReturn() cast). */
|
|
SYSM("GetEnvironmentVariable", MIXED, aNameParam, 1, System_GetEnvironmentVariable),
|
|
SYSM("GetOSPlatform", STRING, 0, 0, System_GetOSPlatform),
|
|
SYSM("GetOSArchitecture", STRING, 0, 0, System_GetOSArchitecture),
|
|
SYSM("GetProcessArchitecture", STRING, 0, 0, System_GetProcessArchitecture),
|
|
SYSM("GetProcessorCount", INT, 0, 0, System_GetProcessorCount),
|
|
SYSM("GetTickCount", INT, 0, 0, System_GetTickCount),
|
|
SYSM("GetMemoryUsage", INT, 0, 0, System_GetMemoryUsage),
|
|
SYSM("GetPeakMemoryUsage", INT, 0, 0, System_GetPeakMemoryUsage),
|
|
SYSM("GetMemoryLimit", INT, 0, 0, System_GetMemoryLimit),
|
|
SYSM("Sleep", VOID, aIntParam, 1, System_Sleep),
|
|
SYSM("Exit", VOID, aExitParam, 1, System_Exit)
|
|
};
|
|
static const ph7_method_def aMethodCli[] = {
|
|
{ "GetEnvironmentVariables", { PH7_TYPE_STRING, 0, 1 }, 0, 0,
|
|
System_GetEnvironmentVariables, 0, PH7_MEMBER_PUBLIC | PH7_MEMBER_STATIC },
|
|
SYSM("SetEnvironmentVariable", VOID, aSetParam, 2, System_SetEnvironmentVariable),
|
|
SYSM("GetProcessId", INT, 0, 0, System_GetProcessId),
|
|
SYSM("GetUserName", STRING, 0, 0, System_GetUserName),
|
|
SYSM("IsPrivilegedProcess", BOOL, 0, 0, System_IsPrivilegedProcess),
|
|
SYSM("GetMachineName", STRING, 0, 0, System_GetMachineName),
|
|
SYSM("GetOSVersion", STRING, 0, 0, System_GetOSVersion)
|
|
};
|
|
static const char *const aExcBase[] = { "Exception" };
|
|
ph7_method_def aMethod[SX_ARRAYSIZE(aMethodCommon) + SX_ARRAYSIZE(aMethodCli)];
|
|
ph7_class_def sExc = {
|
|
sizeof(ph7_class_def), PH7_CLASS_DEF_VERSION, SCLS_EXC,
|
|
0, 0, 0, 0, aExcMethod, SX_ARRAYSIZE(aExcMethod), 0,
|
|
aExcBase, 1, 0, 0, 0, 0
|
|
};
|
|
ph7_class_def sSystem = {
|
|
sizeof(ph7_class_def), PH7_CLASS_DEF_VERSION, SCLS_SYSTEM,
|
|
0, 0, aConst, SX_ARRAYSIZE(aConst), aMethod, 0,
|
|
PH7_DECL_FINAL | PH7_DECL_VIRTUAL, 0, 0, 0, 0, 0, 0
|
|
};
|
|
ph7_value *apConst[SX_ARRAYSIZE(aConst)];
|
|
ph7_value *apDefault[2];
|
|
sxu32 n;
|
|
sxi32 rc = PH7_OK;
|
|
SyZero(apConst, sizeof(apConst));
|
|
SyZero(apDefault, sizeof(apDefault));
|
|
/* Constant and default snapshots; ph7_create_class() deep-copies them. */
|
|
for(n = 0 ; n < SX_ARRAYSIZE(apConst) ; ++n) {
|
|
apConst[n] = ph7_new_scalar(pVm);
|
|
if(apConst[n] == 0) {
|
|
rc = PH7_NOMEM;
|
|
goto Cleanup;
|
|
}
|
|
if(n < SX_ARRAYSIZE(aConstValue)) {
|
|
ph7_value_int(apConst[n], aConstValue[n]);
|
|
} else {
|
|
(void)ph7_value_string(apConst[n], SYS_NEW_LINE, -1);
|
|
}
|
|
aConst[n].pValue = apConst[n];
|
|
}
|
|
/* SystemException("message") gets ERROR_NONE from this snapshot. */
|
|
apDefault[0] = ph7_new_scalar(pVm);
|
|
if(apDefault[0] == 0) {
|
|
rc = PH7_NOMEM;
|
|
goto Cleanup;
|
|
}
|
|
ph7_value_int(apDefault[0], SYS_ERROR_NONE);
|
|
aExcCtor[1].pDefault = apDefault[0];
|
|
/* Exit()'s optional status code defaults to zero. */
|
|
apDefault[1] = ph7_new_scalar(pVm);
|
|
if(apDefault[1] == 0) {
|
|
rc = PH7_NOMEM;
|
|
goto Cleanup;
|
|
}
|
|
ph7_value_int(apDefault[1], 0);
|
|
aExitParam[0].pDefault = apDefault[1];
|
|
/* Per-SAPI method table: only the CLI SAPI sees the process/environment
|
|
* helpers; every other SAPI gets the common, portable surface. */
|
|
memcpy(aMethod, aMethodCommon, sizeof(aMethodCommon));
|
|
sSystem.nMethod = SX_ARRAYSIZE(aMethodCommon);
|
|
if(PH7_VmSapiIs(pVm, "cli")) {
|
|
memcpy(aMethod + SX_ARRAYSIZE(aMethodCommon), aMethodCli, sizeof(aMethodCli));
|
|
sSystem.nMethod += SX_ARRAYSIZE(aMethodCli);
|
|
}
|
|
rc = ph7_create_class(pVm, &sExc);
|
|
if(rc == PH7_OK) {
|
|
rc = ph7_create_class(pVm, &sSystem);
|
|
}
|
|
Cleanup:
|
|
aExcCtor[1].pDefault = 0;
|
|
aExitParam[0].pDefault = 0;
|
|
for(n = 0 ; n < SX_ARRAYSIZE(apDefault) ; ++n) {
|
|
if(apDefault[n]) {
|
|
ph7_release_value(pVm, apDefault[n]);
|
|
}
|
|
}
|
|
for(n = 0 ; n < SX_ARRAYSIZE(apConst) ; ++n) {
|
|
if(apConst[n]) {
|
|
ph7_release_value(pVm, apConst[n]);
|
|
aConst[n].pValue = 0;
|
|
}
|
|
}
|
|
return rc;
|
|
#undef SYSM
|
|
#undef SYSC
|
|
#undef STY
|
|
}
|
|
PH7_PRIVATE sxi32 initializeModule(ph7_vm *pVm, ph7_real *ver, SyString *desc) {
|
|
desc->zString = MODULE_DESC;
|
|
*ver = MODULE_VER;
|
|
return SystemRegisterClasses(pVm);
|
|
}
|