| 1 |
#define _GNU_SOURCE |
| 2 |
|
| 3 |
#include "platform.h" |
| 4 |
|
| 5 |
#include "platform/windows.c" |
| 6 |
#include "platform/mremap.c" |
| 7 |
|
| 8 |
void *GC_mmapAnon (void *start, size_t length) { |
| 9 |
return Windows_mmapAnon (start, length); |
| 10 |
} |
| 11 |
|
| 12 |
void *GC_mmapAnonFlags (void *start, size_t length, __attribute__ ((unused)) int flags) { |
| 13 |
return GC_mmapAnon(start, length); |
| 14 |
} |
| 15 |
|
| 16 |
void GC_release (void *base, size_t length) { |
| 17 |
Windows_release (base, length); |
| 18 |
} |
| 19 |
|
| 20 |
void *GC_extendHead (void *base, size_t length) { |
| 21 |
return Windows_mmapAnon (base, length); |
| 22 |
} |
| 23 |
|
| 24 |
void *GC_extendTail (void *base, size_t length) { |
| 25 |
return Windows_extend (base, length); |
| 26 |
} |
| 27 |
|
| 28 |
uintmax_t GC_physMem (void) { |
| 29 |
#ifdef _WIN64 |
| 30 |
MEMORYSTATUSEX memstat; |
| 31 |
memstat.dwLength = sizeof(memstat); |
| 32 |
GlobalMemoryStatusEx(&memstat); |
| 33 |
return (uintmax_t)memstat.ullTotalPhys; |
| 34 |
#else |
| 35 |
MEMORYSTATUS memstat; |
| 36 |
memstat.dwLength = sizeof(memstat); |
| 37 |
GlobalMemoryStatus(&memstat); |
| 38 |
return (uintmax_t)memstat.dwTotalPhys; |
| 39 |
#endif |
| 40 |
} |
| 41 |
|
| 42 |
size_t GC_pageSize (void) { |
| 43 |
SYSTEM_INFO sysinfo; |
| 44 |
GetSystemInfo(&sysinfo); |
| 45 |
return (size_t)sysinfo.dwAllocationGranularity; |
| 46 |
} |
| 47 |
|
| 48 |
HANDLE fileDesHandle (int fd) { |
| 49 |
|
| 50 |
intptr_t t; |
| 51 |
|
| 52 |
t = _get_osfhandle (fd); |
| 53 |
return (HANDLE)t; |
| 54 |
} |
| 55 |
|
| 56 |
int mkstemp (char *template) { |
| 57 |
char file_path[255]; |
| 58 |
char file_name[255]; |
| 59 |
char templ[4]; |
| 60 |
|
| 61 |
if (0 == GetTempPath (sizeof (file_path), file_path)) |
| 62 |
diee ("unable to make temporary file"); |
| 63 |
strncpy (templ, template, sizeof (templ) - 1); |
| 64 |
templ[sizeof (templ) - 1] = 0x00; |
| 65 |
if (0 == GetTempFileName (file_path, templ, 0, file_name)) |
| 66 |
diee ("unable to make temporary file"); |
| 67 |
return _open (file_name, _O_CREAT | _O_RDWR, _S_IREAD | _S_IWRITE); |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
#ifndef __GNUC__ |
| 75 |
#define EPOCHFILETIME (116444736000000000i64) |
| 76 |
#else |
| 77 |
#define EPOCHFILETIME (116444736000000000LL) |
| 78 |
#endif |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
int gettimeofday (struct timeval *tv, |
| 95 |
__attribute__ ((unused)) struct timezone *tz) { |
| 96 |
static LARGE_INTEGER frequency; |
| 97 |
static LARGE_INTEGER baseCounter; |
| 98 |
static LARGE_INTEGER baseMicroSeconds; |
| 99 |
|
| 100 |
LARGE_INTEGER nowCounter; |
| 101 |
LARGE_INTEGER deltaCounter; |
| 102 |
LARGE_INTEGER nowMicroSeconds; |
| 103 |
double deltaMicroseconds; |
| 104 |
|
| 105 |
|
| 106 |
if (frequency.QuadPart == 0) { |
| 107 |
FILETIME ft; |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
tzset(); |
| 114 |
|
| 115 |
QueryPerformanceCounter(&baseCounter); |
| 116 |
QueryPerformanceFrequency(&frequency); |
| 117 |
if (frequency.QuadPart == 0) |
| 118 |
die("no high resolution clock"); |
| 119 |
|
| 120 |
GetSystemTimeAsFileTime (&ft); |
| 121 |
baseMicroSeconds.LowPart = ft.dwLowDateTime; |
| 122 |
baseMicroSeconds.HighPart = ft.dwHighDateTime; |
| 123 |
baseMicroSeconds.QuadPart -= EPOCHFILETIME; |
| 124 |
baseMicroSeconds.QuadPart /= 10; |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
QueryPerformanceCounter(&nowCounter); |
| 133 |
deltaCounter.QuadPart = nowCounter.QuadPart - baseCounter.QuadPart; |
| 134 |
deltaMicroseconds = deltaCounter.QuadPart; |
| 135 |
deltaMicroseconds /= frequency.QuadPart; |
| 136 |
deltaMicroseconds *= 1000000.0; |
| 137 |
nowMicroSeconds.QuadPart = |
| 138 |
baseMicroSeconds.QuadPart + deltaMicroseconds; |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
if (frequency.HighPart != 0) { |
| 145 |
LARGE_INTEGER nowLowResMicroSeconds; |
| 146 |
FILETIME ft; |
| 147 |
|
| 148 |
|
| 149 |
GetSystemTimeAsFileTime (&ft); |
| 150 |
nowLowResMicroSeconds.LowPart = ft.dwLowDateTime; |
| 151 |
nowLowResMicroSeconds.HighPart = ft.dwHighDateTime; |
| 152 |
nowLowResMicroSeconds.QuadPart -= EPOCHFILETIME; |
| 153 |
nowLowResMicroSeconds.QuadPart /= 10; |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
if (nowMicroSeconds.QuadPart + 1000000 < nowLowResMicroSeconds.QuadPart) { |
| 161 |
baseCounter = nowCounter; |
| 162 |
baseMicroSeconds = nowLowResMicroSeconds; |
| 163 |
nowMicroSeconds = nowLowResMicroSeconds; |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
if ((deltaCounter.HighPart & 0xffff0000UL) != 0) { |
| 173 |
baseCounter = nowCounter; |
| 174 |
baseMicroSeconds = nowMicroSeconds; |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
tv->tv_sec = (long)(nowMicroSeconds.QuadPart / 1000000); |
| 179 |
tv->tv_usec = (long)(nowMicroSeconds.QuadPart % 1000000); |
| 180 |
|
| 181 |
return 0; |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
static HANDLE MainThread = NULL; |
| 193 |
static HANDLE TimerQueue = NULL; |
| 194 |
static HANDLE RealTimer = NULL; |
| 195 |
static HANDLE VirtTimer = NULL; |
| 196 |
static HANDLE ProfTimer = NULL; |
| 197 |
static HANDLE PrioTimer = NULL; |
| 198 |
static void (*SIGALRM_handler)(int sig) = SIG_DFL; |
| 199 |
static void (*SIGVTAM_handler)(int sig) = SIG_DFL; |
| 200 |
static void (*SIGPROF_handler)(int sig) = SIG_DFL; |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
static VOID CALLBACK MLton_SIGALRM(__attribute__ ((unused)) PVOID myArg, |
| 212 |
__attribute__ ((unused)) BOOLEAN timeout) { |
| 213 |
SuspendThread(MainThread); |
| 214 |
if (SIGALRM_handler == SIG_IGN) { |
| 215 |
|
| 216 |
} else if (SIGALRM_handler == SIG_DFL) { |
| 217 |
die("alarm"); |
| 218 |
} else { |
| 219 |
(*SIGALRM_handler)(SIGALRM); |
| 220 |
} |
| 221 |
ResumeThread(MainThread); |
| 222 |
} |
| 223 |
static VOID CALLBACK MLton_SIGVTAM(__attribute__ ((unused)) PVOID myArg, |
| 224 |
__attribute__ ((unused)) BOOLEAN timeout) { |
| 225 |
SuspendThread(MainThread); |
| 226 |
if (SIGVTAM_handler == SIG_IGN) { |
| 227 |
|
| 228 |
} else if (SIGVTAM_handler == SIG_DFL) { |
| 229 |
die("vtalarm"); |
| 230 |
} else { |
| 231 |
(*SIGVTAM_handler)(SIGVTALRM); |
| 232 |
} |
| 233 |
ResumeThread(MainThread); |
| 234 |
} |
| 235 |
static VOID CALLBACK MLton_SIGPROF(__attribute__ ((unused)) PVOID myArg, |
| 236 |
__attribute__ ((unused)) BOOLEAN timeout) { |
| 237 |
SuspendThread(MainThread); |
| 238 |
if (SIGPROF_handler == SIG_IGN) { |
| 239 |
|
| 240 |
} else if (SIGPROF_handler == SIG_DFL) { |
| 241 |
die("sigprof"); |
| 242 |
} else { |
| 243 |
(*SIGPROF_handler)(SIGPROF); |
| 244 |
} |
| 245 |
ResumeThread(MainThread); |
| 246 |
} |
| 247 |
|
| 248 |
static void CALLBACK fixPriority(__attribute__ ((unused)) PVOID myArg, |
| 249 |
__attribute__ ((unused)) BOOLEAN timeout) { |
| 250 |
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_ABOVE_NORMAL); |
| 251 |
DeleteTimerQueueTimer(TimerQueue, PrioTimer, NULL); |
| 252 |
} |
| 253 |
|
| 254 |
static int MLTimer(HANDLE *timer, |
| 255 |
const struct itimerval *value, |
| 256 |
WAITORTIMERCALLBACK callback) { |
| 257 |
DWORD DueTime, Period; |
| 258 |
|
| 259 |
|
| 260 |
if (MainThread == 0) { |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
timeBeginPeriod(1); |
| 265 |
|
| 266 |
TimerQueue = CreateTimerQueue(); |
| 267 |
if (TimerQueue == NULL) { errno = ENOMEM; return -1; } |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
CreateTimerQueueTimer(&PrioTimer, TimerQueue, fixPriority, |
| 276 |
0, 1, 0, WT_EXECUTEINTIMERTHREAD); |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
DuplicateHandle(GetCurrentProcess(), |
| 283 |
GetCurrentThread(), |
| 284 |
GetCurrentProcess(), |
| 285 |
&MainThread, |
| 286 |
0, |
| 287 |
FALSE, |
| 288 |
DUPLICATE_SAME_ACCESS); |
| 289 |
|
| 290 |
if (MainThread == 0) die("Cannot get handle to initial thread"); |
| 291 |
} |
| 292 |
|
| 293 |
|
| 294 |
DueTime = value->it_value.tv_sec * 1000 |
| 295 |
+ (value->it_value.tv_usec + 999) / 1000; |
| 296 |
Period = value->it_interval.tv_sec * 1000 |
| 297 |
+ (value->it_interval.tv_usec + 999) / 1000; |
| 298 |
|
| 299 |
if (timer != NULL) { |
| 300 |
DeleteTimerQueueTimer(TimerQueue, *timer, NULL); |
| 301 |
*timer = NULL; |
| 302 |
} |
| 303 |
|
| 304 |
if (DueTime == 0) { |
| 305 |
return 0; |
| 306 |
} |
| 307 |
|
| 308 |
if (!CreateTimerQueueTimer( |
| 309 |
timer, |
| 310 |
TimerQueue, |
| 311 |
callback, |
| 312 |
0, |
| 313 |
DueTime, |
| 314 |
Period, |
| 315 |
WT_EXECUTEINTIMERTHREAD)) { |
| 316 |
errno = ENOMEM; |
| 317 |
return -1; |
| 318 |
} |
| 319 |
|
| 320 |
return 0; |
| 321 |
} |
| 322 |
|
| 323 |
int setitimer (int which, |
| 324 |
const struct itimerval *value, |
| 325 |
struct itimerval *ovalue) { |
| 326 |
if (ovalue != 0) die("setitimer doesn't support retrieving old state"); |
| 327 |
|
| 328 |
switch (which) { |
| 329 |
case ITIMER_REAL: return MLTimer(&RealTimer, value, &MLton_SIGALRM); |
| 330 |
case ITIMER_VIRT: return MLTimer(&VirtTimer, value, &MLton_SIGVTAM); |
| 331 |
case ITIMER_PROF: return MLTimer(&ProfTimer, value, &MLton_SIGPROF); |
| 332 |
default: errno = EINVAL; return -1; |
| 333 |
} |
| 334 |
|
| 335 |
} |
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
static struct rlimit rlimits[RLIM_NLIMITS]; |
| 342 |
|
| 343 |
static void initRlimits (void) { |
| 344 |
static int done = FALSE; |
| 345 |
int lim; |
| 346 |
|
| 347 |
if (done) |
| 348 |
return; |
| 349 |
done = TRUE; |
| 350 |
for (lim = 0; lim < RLIM_NLIMITS; ++lim ) { |
| 351 |
rlimits[lim].rlim_cur = 0; |
| 352 |
rlimits[lim].rlim_max = UINT_MAX; |
| 353 |
} |
| 354 |
} |
| 355 |
|
| 356 |
int getrlimit (int resource, struct rlimit *rlp) { |
| 357 |
initRlimits (); |
| 358 |
if (resource < 0 or resource >= RLIM_NLIMITS) { |
| 359 |
errno = EINVAL; |
| 360 |
return -1; |
| 361 |
} |
| 362 |
*rlp = rlimits[resource]; |
| 363 |
return 0; |
| 364 |
} |
| 365 |
|
| 366 |
int setrlimit (int resource, const struct rlimit *rlp) { |
| 367 |
initRlimits (); |
| 368 |
if (resource < 0 or resource >= RLIM_NLIMITS) { |
| 369 |
errno = EINVAL; |
| 370 |
return -1; |
| 371 |
} |
| 372 |
if (rlp->rlim_cur < rlimits[resource].rlim_max) |
| 373 |
rlimits[resource].rlim_cur = rlp->rlim_cur; |
| 374 |
else { |
| 375 |
errno = EPERM; |
| 376 |
return -1; |
| 377 |
} |
| 378 |
rlimits[resource].rlim_max = rlp->rlim_max; |
| 379 |
return 0; |
| 380 |
} |
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
int getrusage (int who, struct rusage *usage) { |
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
FILETIME creation_time; |
| 394 |
FILETIME exit_time; |
| 395 |
FILETIME kernel_time; |
| 396 |
FILETIME user_time; |
| 397 |
|
| 398 |
uint64_t user_usecs, kernel_usecs; |
| 399 |
|
| 400 |
if (who == RUSAGE_CHILDREN) { |
| 401 |
|
| 402 |
memset(usage, 0, sizeof(struct rusage)); |
| 403 |
return 0; |
| 404 |
} |
| 405 |
|
| 406 |
if (who != RUSAGE_SELF) { |
| 407 |
errno = EINVAL; |
| 408 |
return -1; |
| 409 |
} |
| 410 |
|
| 411 |
if (GetProcessTimes(GetCurrentProcess(), |
| 412 |
&creation_time, &exit_time, |
| 413 |
&kernel_time, &user_time) == 0) { |
| 414 |
errno = EFAULT; |
| 415 |
return -1; |
| 416 |
} |
| 417 |
|
| 418 |
kernel_usecs = kernel_time.dwHighDateTime; |
| 419 |
kernel_usecs <<= sizeof(kernel_time.dwHighDateTime)*8; |
| 420 |
kernel_usecs |= kernel_time.dwLowDateTime; |
| 421 |
kernel_usecs /= 10; |
| 422 |
|
| 423 |
user_usecs = user_time.dwHighDateTime; |
| 424 |
user_usecs <<= sizeof(user_time.dwHighDateTime)*8; |
| 425 |
user_usecs |= user_time.dwLowDateTime; |
| 426 |
user_usecs /= 10; |
| 427 |
|
| 428 |
usage->ru_utime.tv_sec = user_usecs / 1000000; |
| 429 |
usage->ru_utime.tv_usec = user_usecs % 1000000; |
| 430 |
usage->ru_stime.tv_sec = kernel_usecs / 1000000; |
| 431 |
usage->ru_stime.tv_usec = kernel_usecs % 1000000; |
| 432 |
return 0; |
| 433 |
} |
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
__attribute__ ((noreturn)) |
| 440 |
int poll (__attribute__ ((unused)) struct pollfd *ufds, |
| 441 |
__attribute__ ((unused)) unsigned int nfds, |
| 442 |
__attribute__ ((unused)) int timeout) { |
| 443 |
die ("poll not implemented"); |
| 444 |
} |
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
static void GetWin32FileName (int fd, char* fname) { |
| 451 |
HANDLE fh, fhmap; |
| 452 |
DWORD fileSize, fileSizeHi; |
| 453 |
void* pMem = NULL; |
| 454 |
intptr_t tmp; |
| 455 |
|
| 456 |
tmp = _get_osfhandle (fd); |
| 457 |
fh = (HANDLE)tmp; |
| 458 |
fileSize = GetFileSize (fh, &fileSizeHi); |
| 459 |
fhmap = CreateFileMapping (fh, NULL, PAGE_READONLY, 0, fileSize, NULL); |
| 460 |
if (fhmap) { |
| 461 |
pMem = MapViewOfFile (fhmap, FILE_MAP_READ, 0, 0, 1); |
| 462 |
if (pMem) { |
| 463 |
GetMappedFileNameA (GetCurrentProcess(), |
| 464 |
pMem, fname, MAX_PATH); |
| 465 |
UnmapViewOfFile (pMem); |
| 466 |
} |
| 467 |
CloseHandle (fhmap); |
| 468 |
} |
| 469 |
} |
| 470 |
|
| 471 |
int fchmod (int filedes, mode_t mode) { |
| 472 |
char fname[MAX_PATH + 1]; |
| 473 |
|
| 474 |
GetWin32FileName (filedes, fname); |
| 475 |
return _chmod (fname, mode); |
| 476 |
} |
| 477 |
|
| 478 |
int fchdir (int filedes) { |
| 479 |
char fname[MAX_PATH + 1]; |
| 480 |
|
| 481 |
GetWin32FileName (filedes, fname); |
| 482 |
return chdir (fname); |
| 483 |
} |
| 484 |
|
| 485 |
__attribute__ ((noreturn)) |
| 486 |
int chown (__attribute__ ((unused)) const char *path, |
| 487 |
__attribute__ ((unused)) uid_t owner, |
| 488 |
__attribute__ ((unused)) gid_t group) { |
| 489 |
die ("chown not implemented"); |
| 490 |
} |
| 491 |
|
| 492 |
__attribute__ ((noreturn)) |
| 493 |
int fchown (__attribute__ ((unused)) int fd, |
| 494 |
__attribute__ ((unused)) uid_t owner, |
| 495 |
__attribute__ ((unused)) gid_t group) { |
| 496 |
die ("fchown not implemented"); |
| 497 |
} |
| 498 |
|
| 499 |
__attribute__ ((noreturn)) |
| 500 |
long fpathconf (__attribute__ ((unused)) int filedes, |
| 501 |
__attribute__ ((unused)) int name) { |
| 502 |
die ("fpathconf not implemented"); |
| 503 |
} |
| 504 |
|
| 505 |
__attribute__ ((noreturn)) |
| 506 |
int link (__attribute__ ((unused)) const char *oldpath, |
| 507 |
__attribute__ ((unused)) const char *newpath) { |
| 508 |
die ("link not implemented"); |
| 509 |
} |
| 510 |
|
| 511 |
int lstat (const char *file_name, struct stat *buf) { |
| 512 |
|
| 513 |
return stat (file_name, buf); |
| 514 |
} |
| 515 |
|
| 516 |
__attribute__ ((noreturn)) |
| 517 |
int mkfifo (__attribute__ ((unused)) const char *pathname, |
| 518 |
__attribute__ ((unused)) mode_t mode) { |
| 519 |
die ("mkfifo not implemented"); |
| 520 |
} |
| 521 |
|
| 522 |
__attribute__ ((noreturn)) |
| 523 |
long pathconf (__attribute__ ((unused)) const char *path, |
| 524 |
__attribute__ ((unused)) int name) { |
| 525 |
die ("pathconf not implemented"); |
| 526 |
} |
| 527 |
|
| 528 |
__attribute__ ((noreturn)) |
| 529 |
int readlink (__attribute__ ((unused)) const char *path, |
| 530 |
__attribute__ ((unused)) char *buf, |
| 531 |
__attribute__ ((unused)) size_t bufsiz) { |
| 532 |
die ("readlink not implemented"); |
| 533 |
} |
| 534 |
|
| 535 |
__attribute__ ((noreturn)) |
| 536 |
int symlink (__attribute__ ((unused)) const char *oldpath, |
| 537 |
__attribute__ ((unused)) const char *newpath) { |
| 538 |
die ("symlink not implemented"); |
| 539 |
} |
| 540 |
|
| 541 |
int truncate (const char *path, off_t len) { |
| 542 |
int fd; |
| 543 |
|
| 544 |
if ((fd = open(path, O_RDWR)) == -1) |
| 545 |
return -1; |
| 546 |
if (ftruncate(fd, len) < 0) { |
| 547 |
close(fd); |
| 548 |
return -1; |
| 549 |
} |
| 550 |
close(fd); |
| 551 |
return 0; |
| 552 |
} |
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
__attribute__ ((noreturn)) |
| 560 |
int fcntl (__attribute__ ((unused)) int fd, |
| 561 |
__attribute__ ((unused)) int cmd, |
| 562 |
...) { |
| 563 |
die ("fcntl not implemented"); |
| 564 |
} |
| 565 |
|
| 566 |
int fsync (int fd) { |
| 567 |
return _commit (fd); |
| 568 |
} |
| 569 |
|
| 570 |
int pipe (int filedes[2]) { |
| 571 |
HANDLE read_h; |
| 572 |
HANDLE write_h; |
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
if (!CreatePipe(&read_h, &write_h, 0, 4096)) { |
| 580 |
errno = ENOMEM; |
| 581 |
return -1; |
| 582 |
} |
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
filedes[0] = _open_osfhandle((intptr_t)read_h, _O_RDONLY); |
| 587 |
filedes[1] = _open_osfhandle((intptr_t)write_h, _O_WRONLY); |
| 588 |
if (filedes[0] == -1 or filedes[1] == -1) { |
| 589 |
if (filedes[0] == -1) |
| 590 |
CloseHandle(read_h); |
| 591 |
else close(filedes[0]); |
| 592 |
if (filedes[1] == -1) |
| 593 |
CloseHandle(write_h); |
| 594 |
else close(filedes[1]); |
| 595 |
|
| 596 |
errno = ENFILE; |
| 597 |
return -1; |
| 598 |
} |
| 599 |
return 0; |
| 600 |
} |
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
__attribute__ ((noreturn)) |
| 607 |
char *ctermid (__attribute__ ((unused)) char* s) { |
| 608 |
die ("*ctermid not implemented"); |
| 609 |
} |
| 610 |
|
| 611 |
__attribute__ ((noreturn)) |
| 612 |
gid_t getegid (void) { |
| 613 |
die ("getegid not implemented"); |
| 614 |
} |
| 615 |
|
| 616 |
__attribute__ ((noreturn)) |
| 617 |
uid_t geteuid (void) { |
| 618 |
die ("geteuid not implemented"); |
| 619 |
} |
| 620 |
|
| 621 |
__attribute__ ((noreturn)) |
| 622 |
gid_t getgid (void) { |
| 623 |
die ("getgid not implemented"); |
| 624 |
} |
| 625 |
|
| 626 |
__attribute__ ((noreturn)) |
| 627 |
int getgroups (__attribute__ ((unused)) int size, |
| 628 |
__attribute__ ((unused)) gid_t list[]) { |
| 629 |
die ("getgroups not implemented"); |
| 630 |
} |
| 631 |
|
| 632 |
__attribute__ ((noreturn)) |
| 633 |
char *getlogin (void) { |
| 634 |
die ("getlogin not implemented"); |
| 635 |
} |
| 636 |
|
| 637 |
__attribute__ ((noreturn)) |
| 638 |
pid_t getpgid(__attribute__ ((unused)) pid_t pid) { |
| 639 |
die ("getpgid not implemented"); |
| 640 |
} |
| 641 |
|
| 642 |
__attribute__ ((noreturn)) |
| 643 |
pid_t getpgrp(void) { |
| 644 |
die ("getpgrp not implemented"); |
| 645 |
} |
| 646 |
|
| 647 |
__attribute__ ((noreturn)) |
| 648 |
pid_t getppid (void) { |
| 649 |
die ("getppid not implemented"); |
| 650 |
} |
| 651 |
|
| 652 |
__attribute__ ((noreturn)) |
| 653 |
uid_t getuid (void) { |
| 654 |
die ("getuid not implemented"); |
| 655 |
} |
| 656 |
|
| 657 |
int setenv (const char *name, const char *value, int overwrite) { |
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
if (not overwrite and getenv (name)) { |
| 662 |
errno = EEXIST; |
| 663 |
return -1; |
| 664 |
} |
| 665 |
|
| 666 |
if (SetEnvironmentVariable (name, value)) { |
| 667 |
errno = ENOMEM; |
| 668 |
return -1; |
| 669 |
} |
| 670 |
|
| 671 |
return 0; |
| 672 |
} |
| 673 |
|
| 674 |
__attribute__ ((noreturn)) |
| 675 |
int setgid (__attribute__ ((unused)) gid_t gid) { |
| 676 |
die ("setgid not implemented"); |
| 677 |
} |
| 678 |
|
| 679 |
__attribute__ ((noreturn)) |
| 680 |
int setgroups (__attribute__ ((unused)) size_t size, |
| 681 |
__attribute__ ((unused)) const gid_t *list) { |
| 682 |
die ("setgroups not implemented"); |
| 683 |
} |
| 684 |
|
| 685 |
__attribute__ ((noreturn)) |
| 686 |
int setpgid (__attribute__ ((unused)) pid_t pid, |
| 687 |
__attribute__ ((unused)) pid_t pgid) { |
| 688 |
die ("setpgid not implemented"); |
| 689 |
} |
| 690 |
|
| 691 |
__attribute__ ((noreturn)) |
| 692 |
pid_t setsid (void) { |
| 693 |
die ("setsid not implemented"); |
| 694 |
} |
| 695 |
|
| 696 |
__attribute__ ((noreturn)) |
| 697 |
int setuid (__attribute__ ((unused)) uid_t uid) { |
| 698 |
die ("setuid not implemented"); |
| 699 |
} |
| 700 |
|
| 701 |
__attribute__ ((noreturn)) |
| 702 |
long sysconf (__attribute__ ((unused)) int name) { |
| 703 |
die ("sysconf not implemented"); |
| 704 |
} |
| 705 |
|
| 706 |
__attribute__ ((noreturn)) |
| 707 |
clock_t times (__attribute__ ((unused)) struct tms *buf) { |
| 708 |
die ("times not implemented"); |
| 709 |
} |
| 710 |
|
| 711 |
__attribute__ ((noreturn)) |
| 712 |
char *ttyname (__attribute__ ((unused)) int desc) { |
| 713 |
die ("*ttyname not implemented"); |
| 714 |
} |
| 715 |
|
| 716 |
static void setMachine (struct utsname *buf) { |
| 717 |
int level; |
| 718 |
const char* platform = "unknown"; |
| 719 |
SYSTEM_INFO si; |
| 720 |
|
| 721 |
GetSystemInfo (&si); |
| 722 |
level = si.dwProcessorType; |
| 723 |
switch (si.wProcessorArchitecture) { |
| 724 |
case PROCESSOR_ARCHITECTURE_INTEL: |
| 725 |
if (level < 3) level = 3; |
| 726 |
if (level > 6) level = 6; |
| 727 |
platform = "i%d86"; |
| 728 |
break; |
| 729 |
case PROCESSOR_ARCHITECTURE_IA64: platform = "ia64"; break; |
| 730 |
case PROCESSOR_ARCHITECTURE_AMD64: platform = "amd64"; break; |
| 731 |
case PROCESSOR_ARCHITECTURE_PPC: platform = "ppc"; break; |
| 732 |
case PROCESSOR_ARCHITECTURE_ALPHA: platform = "alpha"; break; |
| 733 |
case PROCESSOR_ARCHITECTURE_MIPS: platform = "mips"; break; |
| 734 |
case PROCESSOR_ARCHITECTURE_ARM: platform = "arm"; break; |
| 735 |
case PROCESSOR_ARCHITECTURE_ALPHA64: platform = "alpha64"; break; |
| 736 |
|
| 737 |
default: platform = "unknown"; break; |
| 738 |
} |
| 739 |
sprintf (buf->machine, platform, level); |
| 740 |
} |
| 741 |
|
| 742 |
static void setSysname (struct utsname *buf) { |
| 743 |
OSVERSIONINFOEX osv; |
| 744 |
const char* os = "??"; |
| 745 |
|
| 746 |
#ifndef _WIN64 |
| 747 |
|
| 748 |
SYSTEM_INFO si; |
| 749 |
void (WINAPI *pGNSI)(LPSYSTEM_INFO); |
| 750 |
pGNSI = (void(WINAPI *)(LPSYSTEM_INFO)) |
| 751 |
GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")), |
| 752 |
"GetNativeSystemInfo"); |
| 753 |
if (NULL != pGNSI) |
| 754 |
pGNSI(&si); |
| 755 |
else |
| 756 |
GetSystemInfo(&si); |
| 757 |
#endif |
| 758 |
|
| 759 |
osv.dwOSVersionInfoSize = sizeof (osv); |
| 760 |
|
| 761 |
|
| 762 |
if (! GetVersionEx ((OSVERSIONINFO *)(void*) &osv)) { |
| 763 |
ZeroMemory(&osv, sizeof(OSVERSIONINFOEX)); |
| 764 |
osv.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); |
| 765 |
GetVersionEx((OSVERSIONINFO *)(void*) &osv); |
| 766 |
} |
| 767 |
switch (osv.dwPlatformId) { |
| 768 |
case VER_PLATFORM_WIN32_NT: |
| 769 |
#ifdef _WIN64 |
| 770 |
if (osv.dwMinorVersion == 0) { |
| 771 |
if (osv.dwMajorVersion <= 6) { |
| 772 |
if (osv.wProductType == VER_NT_WORKSTATION) |
| 773 |
os = "Vista_64"; |
| 774 |
else |
| 775 |
os = "2008_64"; |
| 776 |
} else os = "NTx_64"; |
| 777 |
} else if (osv.dwMinorVersion <= 2) os = "XP_64"; |
| 778 |
else os = "NTx_64"; |
| 779 |
#else |
| 780 |
if (osv.dwMinorVersion == 0) { |
| 781 |
if (osv.dwMajorVersion <= 4) os = "NT"; |
| 782 |
else if (osv.dwMajorVersion <= 5) os = "2000"; |
| 783 |
else { |
| 784 |
if (osv.wProductType == VER_NT_WORKSTATION) { |
| 785 |
if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64) |
| 786 |
os = "Vista_WOW64"; |
| 787 |
else |
| 788 |
os = "Vista"; |
| 789 |
} else { |
| 790 |
if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64) |
| 791 |
os = "2008"; |
| 792 |
else |
| 793 |
os = "2008_WOW64"; |
| 794 |
} |
| 795 |
} |
| 796 |
} else if (osv.dwMinorVersion <= 1) os = "XP"; |
| 797 |
else if (osv.dwMinorVersion <= 2) { |
| 798 |
if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64) |
| 799 |
os = "XP_WOW64"; |
| 800 |
else |
| 801 |
os = "2003"; |
| 802 |
} else os = "NTx"; |
| 803 |
#endif |
| 804 |
break; |
| 805 |
case VER_PLATFORM_WIN32_WINDOWS: |
| 806 |
if (osv.dwMinorVersion == 0) os = "95"; |
| 807 |
else if (osv.dwMinorVersion < 90) os = "98"; |
| 808 |
else if (osv.dwMinorVersion == 90) os = "Me"; |
| 809 |
else os = "9X"; |
| 810 |
break; |
| 811 |
case VER_PLATFORM_WIN32s: |
| 812 |
os = "31"; |
| 813 |
break; |
| 814 |
default: |
| 815 |
os = "unknown"; |
| 816 |
break; |
| 817 |
} |
| 818 |
#ifdef _WIN64 |
| 819 |
#define mingw_name "MINGW64" |
| 820 |
#else |
| 821 |
#define mingw_name "MINGW32" |
| 822 |
#endif |
| 823 |
sprintf (buf->sysname, "%s_%s-%d.%d", mingw_name, |
| 824 |
os, (int)osv.dwMajorVersion, (int)osv.dwMinorVersion); |
| 825 |
} |
| 826 |
|
| 827 |
int uname (struct utsname *buf) { |
| 828 |
MLton_initSockets(); |
| 829 |
setMachine (buf); |
| 830 |
setSysname (buf); |
| 831 |
unless (0 == gethostname (buf->nodename, sizeof (buf->nodename))) { |
| 832 |
strcpy (buf->nodename, "unknown"); |
| 833 |
} |
| 834 |
#ifdef _WIN64 |
| 835 |
sprintf (buf->release, "%d", __MINGW64_VERSION_MINOR); |
| 836 |
sprintf (buf->version, "%d", __MINGW64_VERSION_MAJOR); |
| 837 |
#else |
| 838 |
sprintf (buf->release, "%d", __MINGW32_MINOR_VERSION); |
| 839 |
sprintf (buf->version, "%d", __MINGW32_MAJOR_VERSION); |
| 840 |
#endif |
| 841 |
return 0; |
| 842 |
} |
| 843 |
|
| 844 |
|
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
int alarm (int secs) { |
| 849 |
struct itimerval new; |
| 850 |
new.it_interval.tv_usec = 0; |
| 851 |
new.it_interval.tv_sec = 0; |
| 852 |
new.it_value.tv_usec = 0; |
| 853 |
new.it_value.tv_sec = secs; |
| 854 |
return setitimer(ITIMER_REAL, &new, 0); |
| 855 |
} |
| 856 |
|
| 857 |
__attribute__ ((noreturn)) |
| 858 |
int fork (void) { |
| 859 |
die ("fork not implemented"); |
| 860 |
} |
| 861 |
|
| 862 |
int kill (pid_t pid, int sig) { |
| 863 |
HANDLE h = (HANDLE)pid; |
| 864 |
unless (TerminateProcess (h, SIGNALLED_BIT | sig)) { |
| 865 |
errno = ECHILD; |
| 866 |
return -1; |
| 867 |
} |
| 868 |
return 0; |
| 869 |
} |
| 870 |
|
| 871 |
int nanosleep (const struct timespec *req, struct timespec *rem) { |
| 872 |
Sleep (req->tv_sec * 1000 + (req->tv_nsec + 999999) / 1000000); |
| 873 |
rem->tv_nsec = 0; |
| 874 |
rem->tv_sec = 0; |
| 875 |
return 0; |
| 876 |
} |
| 877 |
|
| 878 |
__attribute__ ((noreturn)) |
| 879 |
int pause (void) { |
| 880 |
die ("pause not implemented"); |
| 881 |
} |
| 882 |
|
| 883 |
unsigned int sleep (unsigned int seconds) { |
| 884 |
Sleep (seconds * 1000); |
| 885 |
return 0; |
| 886 |
} |
| 887 |
|
| 888 |
__attribute__ ((noreturn)) |
| 889 |
pid_t wait (__attribute__ ((unused)) int *status) { |
| 890 |
die ("wait not implemented"); |
| 891 |
} |
| 892 |
|
| 893 |
pid_t waitpid (pid_t pid, int *status, int options) { |
| 894 |
HANDLE h; |
| 895 |
DWORD delay; |
| 896 |
|
| 897 |
|
| 898 |
h = (HANDLE)pid; |
| 899 |
|
| 900 |
delay = ((options & WNOHANG) != 0) ? 0 : INFINITE; |
| 901 |
|
| 902 |
switch (WaitForSingleObject (h, delay)) { |
| 903 |
case WAIT_OBJECT_0: |
| 904 |
break; |
| 905 |
case WAIT_TIMEOUT: |
| 906 |
return 0; |
| 907 |
default: |
| 908 |
errno = ECHILD; |
| 909 |
return -1; |
| 910 |
} |
| 911 |
|
| 912 |
unless (GetExitCodeProcess (h, (DWORD*)status)) { |
| 913 |
errno = ECHILD; |
| 914 |
return -1; |
| 915 |
} |
| 916 |
|
| 917 |
return pid; |
| 918 |
} |
| 919 |
|
| 920 |
|
| 921 |
|
| 922 |
|
| 923 |
|
| 924 |
int sigaction (int signum, |
| 925 |
const struct sigaction *newact, |
| 926 |
struct sigaction *oldact) { |
| 927 |
_sig_func_ptr old; |
| 928 |
|
| 929 |
if (signum < 0 or signum >= NSIG) { |
| 930 |
errno = EINVAL; |
| 931 |
return -1; |
| 932 |
} |
| 933 |
|
| 934 |
switch (signum) { |
| 935 |
case SIGKILL: |
| 936 |
case SIGSTOP: |
| 937 |
errno = EINVAL; |
| 938 |
return -1; |
| 939 |
case SIGALRM: |
| 940 |
old = SIGALRM_handler; |
| 941 |
if (newact) SIGALRM_handler = newact->sa_handler; |
| 942 |
break; |
| 943 |
case SIGVTALRM: |
| 944 |
old = SIGVTAM_handler; |
| 945 |
if (newact) SIGVTAM_handler = newact->sa_handler; |
| 946 |
break; |
| 947 |
case SIGPROF: |
| 948 |
old = SIGPROF_handler; |
| 949 |
if (newact) SIGPROF_handler = newact->sa_handler; |
| 950 |
break; |
| 951 |
default: |
| 952 |
old = signal (signum, newact?newact->sa_handler:0); |
| 953 |
if (!newact) signal (signum, old); |
| 954 |
break; |
| 955 |
} |
| 956 |
|
| 957 |
if (oldact) |
| 958 |
oldact->sa_handler = old; |
| 959 |
return 0; |
| 960 |
} |
| 961 |
|
| 962 |
int sigaddset (sigset_t *set, const int signum) { |
| 963 |
if (signum < 0 or signum >= NSIG) { |
| 964 |
errno = EINVAL; |
| 965 |
return -1; |
| 966 |
} |
| 967 |
*set |= SIGTOMASK (signum); |
| 968 |
return 0; |
| 969 |
} |
| 970 |
|
| 971 |
int sigdelset (sigset_t *set, const int signum) { |
| 972 |
if (signum < 0 or signum >= NSIG) { |
| 973 |
errno = EINVAL; |
| 974 |
return -1; |
| 975 |
} |
| 976 |
*set &= ~SIGTOMASK (signum); |
| 977 |
return 0; |
| 978 |
} |
| 979 |
|
| 980 |
int sigemptyset (sigset_t *set) { |
| 981 |
*set = (sigset_t) 0; |
| 982 |
return 0; |
| 983 |
} |
| 984 |
|
| 985 |
int sigfillset (sigset_t *set) { |
| 986 |
*set = ~((sigset_t) 0); |
| 987 |
return 0; |
| 988 |
} |
| 989 |
|
| 990 |
int sigismember (const sigset_t *set, const int signum) { |
| 991 |
if (signum < 0 or signum >= NSIG) { |
| 992 |
errno = EINVAL; |
| 993 |
return -1; |
| 994 |
} |
| 995 |
return (*set & SIGTOMASK(signum)) ? 1 : 0; |
| 996 |
} |
| 997 |
|
| 998 |
|
| 999 |
|
| 1000 |
|
| 1001 |
|
| 1002 |
static sigset_t signals_blocked = 0; |
| 1003 |
static sigset_t signals_pending = 0; |
| 1004 |
|
| 1005 |
int sigpending (sigset_t *set) { |
| 1006 |
*set = signals_pending; |
| 1007 |
return 0; |
| 1008 |
} |
| 1009 |
|
| 1010 |
int sigprocmask (int how, const sigset_t *set, sigset_t *oldset) { |
| 1011 |
if (oldset) { |
| 1012 |
*oldset = signals_blocked; |
| 1013 |
} |
| 1014 |
if (set) { |
| 1015 |
sigset_t newmask = signals_blocked; |
| 1016 |
|
| 1017 |
switch (how) { |
| 1018 |
case SIG_BLOCK: |
| 1019 |
|
| 1020 |
newmask |= *set; |
| 1021 |
break; |
| 1022 |
case SIG_UNBLOCK: |
| 1023 |
|
| 1024 |
newmask &= ~*set; |
| 1025 |
break; |
| 1026 |
case SIG_SETMASK: |
| 1027 |
|
| 1028 |
newmask = *set; |
| 1029 |
break; |
| 1030 |
default: |
| 1031 |
return -1; |
| 1032 |
} |
| 1033 |
|
| 1034 |
signals_blocked = newmask; |
| 1035 |
} |
| 1036 |
return 0; |
| 1037 |
} |
| 1038 |
|
| 1039 |
__attribute__ ((noreturn)) |
| 1040 |
int sigsuspend (__attribute__ ((unused)) const sigset_t *mask) { |
| 1041 |
die("sigsuspend is unimplemented, but could be hacked in if needed"); |
| 1042 |
} |
| 1043 |
|
| 1044 |
|
| 1045 |
|
| 1046 |
|
| 1047 |
|
| 1048 |
void Posix_IO_setbin (C_Fd_t fd) { |
| 1049 |
_setmode (fd, _O_BINARY); |
| 1050 |
} |
| 1051 |
|
| 1052 |
void Posix_IO_settext (C_Fd_t fd) { |
| 1053 |
_setmode (fd, _O_TEXT); |
| 1054 |
} |
| 1055 |
|
| 1056 |
|
| 1057 |
|
| 1058 |
|
| 1059 |
|
| 1060 |
#define INFO_LEVEL 3 |
| 1061 |
static LPUSER_INFO_3 usrData = NULL; |
| 1062 |
|
| 1063 |
static struct passwd passwd; |
| 1064 |
|
| 1065 |
__attribute__ ((noreturn)) |
| 1066 |
struct group *getgrgid (__attribute__ ((unused)) gid_t gid) { |
| 1067 |
die ("getgrgid not implemented"); |
| 1068 |
} |
| 1069 |
|
| 1070 |
__attribute__ ((noreturn)) |
| 1071 |
struct group *getgrnam (__attribute__ ((unused)) const char *name) { |
| 1072 |
die ("getgrnam not implemented"); |
| 1073 |
} |
| 1074 |
|
| 1075 |
struct passwd *getpwnam (__attribute__ ((unused)) const char *name) { |
| 1076 |
return NULL; |
| 1077 |
|
| 1078 |
|
| 1079 |
|
| 1080 |
|
| 1081 |
passwd.pw_dir = (char*)usrData->usri3_home_dir; |
| 1082 |
passwd.pw_gid = usrData->usri3_primary_group_id; |
| 1083 |
passwd.pw_name = (char*)usrData->usri3_name; |
| 1084 |
passwd.pw_shell = (char*)usrData->usri3_script_path; |
| 1085 |
passwd.pw_uid = usrData->usri3_user_id; |
| 1086 |
return &passwd; |
| 1087 |
} |
| 1088 |
|
| 1089 |
__attribute__ ((noreturn)) |
| 1090 |
struct passwd *getpwuid (__attribute__ ((unused)) uid_t uid) { |
| 1091 |
die ("getpwuid not implemented"); |
| 1092 |
} |
| 1093 |
|
| 1094 |
|
| 1095 |
|
| 1096 |
|
| 1097 |
|
| 1098 |
__attribute__ ((noreturn)) |
| 1099 |
speed_t cfgetispeed (__attribute__ ((unused)) struct termios *termios_p) { |
| 1100 |
die ("cfgetispeed not implemented"); |
| 1101 |
} |
| 1102 |
|
| 1103 |
__attribute__ ((noreturn)) |
| 1104 |
speed_t cfgetospeed (__attribute__ ((unused)) struct termios *termios_p) { |
| 1105 |
die ("cfgetospeed not implemented"); |
| 1106 |
} |
| 1107 |
|
| 1108 |
__attribute__ ((noreturn)) |
| 1109 |
int cfsetispeed (__attribute__ ((unused)) struct termios *termios_p, |
| 1110 |
__attribute__ ((unused)) speed_t speed) { |
| 1111 |
die ("cfsetispeed not implemented"); |
| 1112 |
} |
| 1113 |
|
| 1114 |
__attribute__ ((noreturn)) |
| 1115 |
int cfsetospeed (__attribute__ ((unused)) struct termios *termios_p, |
| 1116 |
__attribute__ ((unused)) speed_t speed) { |
| 1117 |
die ("cfsetospeed not implemented"); |
| 1118 |
} |
| 1119 |
|
| 1120 |
__attribute__ ((noreturn)) |
| 1121 |
int tcdrain (__attribute__ ((unused)) int fd) { |
| 1122 |
die ("tcdrain not implemented"); |
| 1123 |
} |
| 1124 |
|
| 1125 |
__attribute__ ((noreturn)) |
| 1126 |
int tcflow (__attribute__ ((unused)) int fd, |
| 1127 |
__attribute__ ((unused)) int action) { |
| 1128 |
die ("tcflow not implemented"); |
| 1129 |
} |
| 1130 |
|
| 1131 |
__attribute__ ((noreturn)) |
| 1132 |
int tcflush (__attribute__ ((unused)) int fd, |
| 1133 |
__attribute__ ((unused)) int queue_selector) { |
| 1134 |
die ("tcflush not implemented"); |
| 1135 |
} |
| 1136 |
|
| 1137 |
__attribute__ ((noreturn)) |
| 1138 |
int tcgetattr (__attribute__ ((unused)) int fd, |
| 1139 |
__attribute__ ((unused)) struct termios *termios_p) { |
| 1140 |
die ("tcgetattr not implemented"); |
| 1141 |
} |
| 1142 |
|
| 1143 |
__attribute__ ((noreturn)) |
| 1144 |
pid_t tcgetpgrp (__attribute__ ((unused)) int fd) { |
| 1145 |
die ("tcgetpgrp not implemented"); |
| 1146 |
} |
| 1147 |
|
| 1148 |
__attribute__ ((noreturn)) |
| 1149 |
int tcsendbreak (__attribute__ ((unused)) int fd, |
| 1150 |
__attribute__ ((unused)) int duration) { |
| 1151 |
die ("tcsendbreak not implemented"); |
| 1152 |
} |
| 1153 |
|
| 1154 |
__attribute__ ((noreturn)) |
| 1155 |
int tcsetattr (__attribute__ ((unused)) int fd, |
| 1156 |
__attribute__ ((unused)) int optional_actions, |
| 1157 |
__attribute__ ((unused)) struct termios *termios_p) { |
| 1158 |
die ("tcsetattr not implemented"); |
| 1159 |
} |
| 1160 |
|
| 1161 |
__attribute__ ((noreturn)) |
| 1162 |
int tcsetpgrp (__attribute__ ((unused)) int fd, |
| 1163 |
__attribute__ ((unused)) pid_t pgrpid) { |
| 1164 |
die ("tcsetpgrp not implemented"); |
| 1165 |
} |
| 1166 |
|
| 1167 |
|
| 1168 |
|
| 1169 |
|
| 1170 |
|
| 1171 |
__attribute__ ((noreturn)) |
| 1172 |
int ioctl (__attribute__ ((unused)) int d, |
| 1173 |
__attribute__ ((unused)) int request, |
| 1174 |
...) { |
| 1175 |
die ("ioctl not implemented"); |
| 1176 |
} |
| 1177 |
|
| 1178 |
__attribute__ ((noreturn)) |
| 1179 |
int socketpair (__attribute__ ((unused)) int d, |
| 1180 |
__attribute__ ((unused)) int type, |
| 1181 |
__attribute__ ((unused)) int protocol, |
| 1182 |
__attribute__ ((unused)) int sv[2]) { |
| 1183 |
die ("socketpair not implemented"); |
| 1184 |
} |
| 1185 |
|
| 1186 |
void MLton_initSockets (void) { |
| 1187 |
static Bool isInitialized = FALSE; |
| 1188 |
WORD version; |
| 1189 |
WSADATA wsaData; |
| 1190 |
|
| 1191 |
unless (isInitialized) { |
| 1192 |
isInitialized = TRUE; |
| 1193 |
version = MAKEWORD (2,2); |
| 1194 |
WSAStartup (version, &wsaData); |
| 1195 |
} |
| 1196 |
} |
| 1197 |
|
| 1198 |
|
| 1199 |
|
| 1200 |
|
| 1201 |
|
| 1202 |
void MLton_fixSocketErrno (void) { |
| 1203 |
int status = WSAGetLastError (); |
| 1204 |
|
| 1205 |
switch (status) { |
| 1206 |
case 0: errno = 0; break; |
| 1207 |
case WSAEINTR: errno = EINTR; break; |
| 1208 |
case WSAEBADF: errno = EBADF; break; |
| 1209 |
case WSAEACCES: errno = EACCES; break; |
| 1210 |
case WSAEFAULT: errno = EFAULT; break; |
| 1211 |
case WSAEINVAL: errno = EINVAL; break; |
| 1212 |
case WSAEMFILE: errno = EMFILE; break; |
| 1213 |
case WSAEWOULDBLOCK: errno = EWOULDBLOCK; break; |
| 1214 |
case WSAEINPROGRESS: errno = EINPROGRESS; break; |
| 1215 |
case WSAEALREADY: errno = EALREADY; break; |
| 1216 |
case WSAENOTSOCK: errno = ENOTSOCK; break; |
| 1217 |
case WSAEDESTADDRREQ: errno = EDESTADDRREQ; break; |
| 1218 |
case WSAEMSGSIZE: errno = EMSGSIZE; break; |
| 1219 |
case WSAEPROTOTYPE: errno = EPROTOTYPE; break; |
| 1220 |
case WSAENOPROTOOPT: errno = ENOPROTOOPT; break; |
| 1221 |
case WSAEPROTONOSUPPORT: errno = EPROTONOSUPPORT; break; |
| 1222 |
case WSAESOCKTNOSUPPORT: errno = ESOCKTNOSUPPORT; break; |
| 1223 |
case WSAEOPNOTSUPP: errno = EOPNOTSUPP; break; |
| 1224 |
case WSAEPFNOSUPPORT: errno = EPFNOSUPPORT; break; |
| 1225 |
case WSAEAFNOSUPPORT: errno = EAFNOSUPPORT; break; |
| 1226 |
case WSAEADDRINUSE: errno = EADDRINUSE; break; |
| 1227 |
case WSAEADDRNOTAVAIL: errno = EADDRNOTAVAIL; break; |
| 1228 |
case WSAENETDOWN: errno = ENETDOWN; break; |
| 1229 |
case WSAENETUNREACH: errno = ENETUNREACH; break; |
| 1230 |
case WSAENETRESET: errno = ENETRESET; break; |
| 1231 |
case WSAECONNABORTED: errno = ECONNABORTED; break; |
| 1232 |
case WSAECONNRESET: errno = ECONNRESET; break; |
| 1233 |
case WSAENOBUFS: errno = ENOBUFS; break; |
| 1234 |
case WSAEISCONN: errno = EISCONN; break; |
| 1235 |
case WSAENOTCONN: errno = ENOTCONN; break; |
| 1236 |
case WSAESHUTDOWN: errno = ESHUTDOWN; break; |
| 1237 |
case WSAETIMEDOUT: errno = ETIMEDOUT; break; |
| 1238 |
case WSAECONNREFUSED: errno = ECONNREFUSED; break; |
| 1239 |
case WSAELOOP: errno = ELOOP; break; |
| 1240 |
case WSAENAMETOOLONG: errno = ENAMETOOLONG; break; |
| 1241 |
case WSAEHOSTDOWN: errno = EHOSTDOWN; break; |
| 1242 |
case WSAEHOSTUNREACH: errno = EHOSTUNREACH; break; |
| 1243 |
case WSAENOTEMPTY: errno = ENOTEMPTY; break; |
| 1244 |
case WSAEDQUOT: errno = EDQUOT; break; |
| 1245 |
case WSAESTALE: errno = ESTALE; break; |
| 1246 |
case WSAEREMOTE: errno = EREMOTE; break; |
| 1247 |
|
| 1248 |
|
| 1249 |
|
| 1250 |
case WSAEUSERS: errno = EUSERS; break; |
| 1251 |
case WSAECANCELLED: errno = ECANCELED; break; |
| 1252 |
case WSA_E_CANCELLED: errno = ECANCELED; break; |
| 1253 |
|
| 1254 |
case WSAEPROCLIM: errno = EBUSY; break; |
| 1255 |
case WSAETOOMANYREFS: errno = ENOMEM; break; |
| 1256 |
case WSAEDISCON: errno = ESHUTDOWN; break; |
| 1257 |
case WSA_E_NO_MORE: |
| 1258 |
case WSAENOMORE: |
| 1259 |
case WSASYSCALLFAILURE: errno = EIO; break; |
| 1260 |
|
| 1261 |
|
| 1262 |
|
| 1263 |
|
| 1264 |
|
| 1265 |
|
| 1266 |
|
| 1267 |
|
| 1268 |
case WSASYSNOTREADY: |
| 1269 |
case WSAVERNOTSUPPORTED: |
| 1270 |
case WSANOTINITIALISED: |
| 1271 |
case WSAEINVALIDPROCTABLE: |
| 1272 |
case WSAEINVALIDPROVIDER: |
| 1273 |
case WSAEPROVIDERFAILEDINIT: |
| 1274 |
case WSASERVICE_NOT_FOUND: |
| 1275 |
case WSATYPE_NOT_FOUND: |
| 1276 |
die("Problem loading winsock"); |
| 1277 |
case WSAEREFUSED: |
| 1278 |
case WSAHOST_NOT_FOUND: |
| 1279 |
case WSATRY_AGAIN: |
| 1280 |
case WSANO_RECOVERY: |
| 1281 |
case WSANO_DATA: |
| 1282 |
die("Strange winsock specific status code"); |
| 1283 |
default: |
| 1284 |
die("Unknown winsock status code"); |
| 1285 |
} |
| 1286 |
} |
| 1287 |
|
| 1288 |
static const char *MLton_strerrorExtension(int code) { |
| 1289 |
switch (code) { |
| 1290 |
case EINTR: return "Interrupted function call"; |
| 1291 |
case EBADF: return "Bad file descriptor"; |
| 1292 |
case EACCES: return "Permission denied"; |
| 1293 |
case EFAULT: return "Bad address"; |
| 1294 |
case EINVAL: return "Invalid argument"; |
| 1295 |
case EMFILE: return "Too many open files"; |
| 1296 |
case EAGAIN: return "Resource temporarily unavailable"; |
| 1297 |
case EINPROGRESS: return "Operation in progress"; |
| 1298 |
case EALREADY: return "Connection already in progress"; |
| 1299 |
case ENOTSOCK: return "Not a socket"; |
| 1300 |
case EDESTADDRREQ: return "Destination address required"; |
| 1301 |
case EMSGSIZE: return "Message too long"; |
| 1302 |
case EPROTOTYPE: return "Protocol wrong type for socket"; |
| 1303 |
case ENOPROTOOPT: return "Protocol not available"; |
| 1304 |
case EPROTONOSUPPORT: return "Protocol not supported"; |
| 1305 |
case ESOCKTNOSUPPORT: return "Socket type not supported"; |
| 1306 |
case EOPNOTSUPP: return "Operation not supported on socket"; |
| 1307 |
case EPFNOSUPPORT: return "Protocol family not supported"; |
| 1308 |
case EAFNOSUPPORT: return "Address family not supported"; |
| 1309 |
case EADDRINUSE: return "Address already in use"; |
| 1310 |
case EADDRNOTAVAIL: return "Address not available"; |
| 1311 |
case ENETDOWN: return "Network is down"; |
| 1312 |
case ENETUNREACH: return "Network unreachable"; |
| 1313 |
case ENETRESET: return "Connection aborted by network"; |
| 1314 |
case ECONNABORTED: return "Connection aborted"; |
| 1315 |
case ECONNRESET: return "Connection reset"; |
| 1316 |
case ENOBUFS: return "No buffer space available"; |
| 1317 |
case EISCONN: return "Socket is connected"; |
| 1318 |
case ENOTCONN: return "The socket is not connected"; |
| 1319 |
case ESHUTDOWN: return "Cannot send after transport endpoint shutdown"; |
| 1320 |
case ETIMEDOUT: return "Connection timed out"; |
| 1321 |
case ECONNREFUSED: return "Connection refused"; |
| 1322 |
case ELOOP: return "Too many levels of symbolic links"; |
| 1323 |
case ENAMETOOLONG: return "Filename too long"; |
| 1324 |
case EHOSTDOWN: return "Host is down"; |
| 1325 |
case EHOSTUNREACH: return "Host is unreachable"; |
| 1326 |
case ENOTEMPTY: return "Directory not empty"; |
| 1327 |
case EDQUOT: return "Disk quota exceeded"; |
| 1328 |
case ESTALE: return "Stale file handle"; |
| 1329 |
case EREMOTE: return "Object is remote"; |
| 1330 |
case EUSERS: return "Too many users"; |
| 1331 |
case ECANCELED: return "Operation canceled"; |
| 1332 |
default: return "Unknown error"; |
| 1333 |
} |
| 1334 |
} |
| 1335 |
|
| 1336 |
|
| 1337 |
|
| 1338 |
|
| 1339 |
|
| 1340 |
|
| 1341 |
|
| 1342 |
|
| 1343 |
|
| 1344 |
|
| 1345 |
|
| 1346 |
|
| 1347 |
|
| 1348 |
|
| 1349 |
char *MLton_strerror(int code) { |
| 1350 |
static char buffer[80]; |
| 1351 |
|
| 1352 |
#undef strerror |
| 1353 |
if (code < sys_nerr) return strerror(code); |
| 1354 |
#define strerror MLton_strerror |
| 1355 |
|
| 1356 |
strcpy(buffer, MLton_strerrorExtension(code)); |
| 1357 |
return buffer; |
| 1358 |
} |
| 1359 |
|
| 1360 |
int MLton_recv(int s, void *buf, int len, int flags) { |
| 1361 |
int ret, status = 0; |
| 1362 |
|
| 1363 |
if (flags & MSG_DONTWAIT) MinGW_setNonBlock(s); |
| 1364 |
ret = recv(s, buf, len, flags & ~MSG_DONTWAIT); |
| 1365 |
|
| 1366 |
|
| 1367 |
if (ret == -1) status = WSAGetLastError(); |
| 1368 |
if (flags & MSG_DONTWAIT) MinGW_clearNonBlock(s); |
| 1369 |
if (ret == -1) WSASetLastError(status); |
| 1370 |
|
| 1371 |
return ret; |
| 1372 |
} |
| 1373 |
|
| 1374 |
int MLton_recvfrom(int s, void *buf, int len, int flags, void *from, |
| 1375 |
socklen_t *fromlen) { |
| 1376 |
int ret, status = 0; |
| 1377 |
|
| 1378 |
if (flags & MSG_DONTWAIT) MinGW_setNonBlock(s); |
| 1379 |
ret = recvfrom(s, buf, len, flags & ~MSG_DONTWAIT, from, fromlen); |
| 1380 |
|
| 1381 |
|
| 1382 |
if (ret == -1) status = WSAGetLastError(); |
| 1383 |
if (flags & MSG_DONTWAIT) MinGW_clearNonBlock(s); |
| 1384 |
if (ret == -1) WSASetLastError(status); |
| 1385 |
|
| 1386 |
return ret; |
| 1387 |
} |
| 1388 |
|
| 1389 |
|
| 1390 |
|
| 1391 |
|
| 1392 |
static const char* logident = "<unknown>"; |
| 1393 |
static int logopt = LOG_PERROR; |
| 1394 |
static int logfacility = LOG_LOCAL0; |
| 1395 |
|
| 1396 |
void openlog(const char* ident, int opt, int facility) { |
| 1397 |
logident = ident; |
| 1398 |
logopt = opt; |
| 1399 |
logfacility = facility; |
| 1400 |
} |
| 1401 |
|
| 1402 |
void closelog(void) { |
| 1403 |
} |
| 1404 |
|
| 1405 |
void syslog(int priority, __attribute__ ((unused)) const char* fmt, const char* msg) { |
| 1406 |
static const char* severity[] = { |
| 1407 |
"debug", |
| 1408 |
"informational", |
| 1409 |
"notice", |
| 1410 |
"warning", |
| 1411 |
"error", |
| 1412 |
"CRITICAL", |
| 1413 |
"ALERT", |
| 1414 |
"EMERGENCY" |
| 1415 |
}; |
| 1416 |
|
| 1417 |
if (priority < 0) priority = LOG_DEBUG; |
| 1418 |
if (priority > LOG_EMERG) priority = LOG_EMERG; |
| 1419 |
|
| 1420 |
|
| 1421 |
|
| 1422 |
|
| 1423 |
if ((logopt & LOG_PERROR) != 0) { |
| 1424 |
if ((logopt & LOG_PID) != 0) |
| 1425 |
fprintf(stderr, "%s(%d): %s: %s\n", logident, getpid(), severity[priority], msg); |
| 1426 |
else |
| 1427 |
fprintf(stderr, "%s: %s: %s\n", logident, severity[priority], msg); |
| 1428 |
} |
| 1429 |
} |
| 1430 |
|
| 1431 |
|
| 1432 |
|
| 1433 |
|
| 1434 |
|
| 1435 |
C_Size_t MinGW_getTempPath(C_Size_t buf_size, Array(Char8_t) buf) { |
| 1436 |
return GetTempPath(buf_size, (char*)buf); |
| 1437 |
} |
| 1438 |
|
| 1439 |
void MinGW_setNonBlock(C_Fd_t fd) { |
| 1440 |
unsigned long yes = 1; |
| 1441 |
ioctlsocket(fd, FIONBIO, &yes); |
| 1442 |
} |
| 1443 |
|
| 1444 |
void MinGW_clearNonBlock(C_Fd_t fd) { |
| 1445 |
unsigned long no = 0; |
| 1446 |
ioctlsocket(fd, FIONBIO, &no); |
| 1447 |
} |