| 1 |
/* |
| 2 |
* The Regina Rexx Interpreter |
| 3 |
* Copyright (C) 2005 Florian Grosse-Coosmann |
| 4 |
* |
| 5 |
* This file contains Windows specific code. |
| 6 |
* |
| 7 |
* This library is free software; you can redistribute it and/or |
| 8 |
* modify it under the terms of the GNU Library General Public |
| 9 |
* License as published by the Free Software Foundation; either |
| 10 |
* version 2 of the License, or (at your option) any later version. |
| 11 |
* |
| 12 |
* This library is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 |
* Library General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU Library General Public |
| 18 |
* License along with this library; if not, write to the Free |
| 19 |
* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 20 |
*/ |
| 21 |
|
| 22 |
#include "rexx.h" |
| 23 |
|
| 24 |
#include <stdio.h> |
| 25 |
#include <stdlib.h> |
| 26 |
#include <string.h> |
| 27 |
#include <errno.h> |
| 28 |
#include <assert.h> |
| 29 |
|
| 30 |
#if defined(WIN32) |
| 31 |
# include <share.h> |
| 32 |
# if defined(__BORLANDC__) || defined(__LCC__) |
| 33 |
# include <time.h> |
| 34 |
# include <process.h> |
| 35 |
# endif |
| 36 |
# ifdef _MSC_VER |
| 37 |
# if _MSC_VER >= 1100 |
| 38 |
/* Stupid MSC can't compile own headers without warning at least in VC 5.0 */ |
| 39 |
# pragma warning(disable: 4115 4201 4214 4514) |
| 40 |
# endif |
| 41 |
# endif |
| 42 |
# include <windows.h> |
| 43 |
# ifdef _MSC_VER |
| 44 |
# if _MSC_VER >= 1100 |
| 45 |
# pragma warning(default: 4115 4201 4214) |
| 46 |
# endif |
| 47 |
# endif |
| 48 |
#endif |
| 49 |
|
| 50 |
#if (defined(__WATCOMC__) && !defined(__QNX__)) || defined(_MSC_VER) || defined(__MINGW32__) || defined(__BORLANDC__) || defined(__EPOC32__) || defined(__WINS__) || defined(__LCC__) |
| 51 |
# include "utsname.h" /* MH 10-06-96 */ |
| 52 |
# define NEED_UNAME |
| 53 |
# if !defined(__WINS__) && !defined(__EPOC32__) |
| 54 |
# define MAXPATHLEN _MAX_PATH /* MH 10-06-96 */ |
| 55 |
# endif |
| 56 |
#else /* MH 10-06-96 */ |
| 57 |
# if defined(WIN32) && defined(__IBMC__) /* LM 26-02-99 */ |
| 58 |
# include "utsname.h" |
| 59 |
# define NEED_UNAME |
| 60 |
# define MAXPATHLEN (8192) |
| 61 |
# include <io.h> |
| 62 |
# else |
| 63 |
# include <sys/param.h> /* MH 10-06-96 */ |
| 64 |
# include <sys/utsname.h> /* MH 10-06-96 */ |
| 65 |
# include <sys/wait.h> |
| 66 |
# endif |
| 67 |
#endif |
| 68 |
|
| 69 |
#if (defined(__WATCOMC__) && !defined(__QNX__)) || defined(_MSC_VER) || defined(DJGPP) || defined(__CYGWIN32__) || defined(__BORLANDC__) || defined(__MINGW32__) || defined(__WINS__) || defined(__EPOC32__) |
| 70 |
# define HAVE_BROKEN_TMPNAM |
| 71 |
# define PATH_DELIMS ":\\/" |
| 72 |
# if defined(__CYGWIN32__) |
| 73 |
# define ISTR_SLASH "/" /* This is not a must, \\ works, too */ |
| 74 |
# define I_SLASH '/' /* This is not a must, \\ works, too */ |
| 75 |
# else |
| 76 |
# define ISTR_SLASH "\\" /* This is not a must, / works at least for MSC, too */ |
| 77 |
# define I_SLASH '\\' /* This is not a must, / works at least for MSC, too */ |
| 78 |
# endif |
| 79 |
# if !defined(__WINS__) && !defined(__EPOC32__) && !defined(__CYGWIN__) |
| 80 |
# ifndef HAVE_UNISTD_H |
| 81 |
# include <io.h> /* access() */ |
| 82 |
# endif |
| 83 |
# include <process.h> |
| 84 |
# include <share.h> |
| 85 |
# endif |
| 86 |
# include <time.h> |
| 87 |
#endif |
| 88 |
|
| 89 |
#if defined(__LCC__) |
| 90 |
# if !defined(HasOverlappedIoCompleted) |
| 91 |
# define HasOverlappedIoCompleted(lpOverlapped) ((lpOverlapped)->Internal != STATUS_PENDING) |
| 92 |
# endif |
| 93 |
#endif |
| 94 |
|
| 95 |
static int IsWin9X(void) |
| 96 |
{ |
| 97 |
static int retval = -1; |
| 98 |
OSVERSIONINFO osinfo; |
| 99 |
|
| 100 |
if ( retval >= 0 ) |
| 101 |
return retval; |
| 102 |
|
| 103 |
#ifdef DEBUG |
| 104 |
if (getenv("REGINA_ACT_AS_WIN9X") != NULL) |
| 105 |
{ |
| 106 |
retval = 1; |
| 107 |
return retval; |
| 108 |
} |
| 109 |
#endif |
| 110 |
|
| 111 |
osinfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); |
| 112 |
GetVersionEx( &osinfo ); |
| 113 |
|
| 114 |
switch( osinfo.dwPlatformId ) { |
| 115 |
case VER_PLATFORM_WIN32s: /* Very stupid system */ |
| 116 |
case VER_PLATFORM_WIN32_WINDOWS: /* Stupid systems */ |
| 117 |
default: |
| 118 |
retval = 1; |
| 119 |
break; |
| 120 |
case VER_PLATFORM_WIN32_NT: /* Reasonable systems ... just my 2 ct, FGC */ |
| 121 |
retval = 0; |
| 122 |
break; |
| 123 |
} |
| 124 |
return retval; |
| 125 |
} |
| 126 |
|
| 127 |
typedef struct { |
| 128 |
HANDLE hdl; |
| 129 |
OVERLAPPED ol; |
| 130 |
char *buf; |
| 131 |
unsigned maxbuf; |
| 132 |
unsigned rused; |
| 133 |
unsigned rusedbegin; |
| 134 |
unsigned reading; |
| 135 |
unsigned wused; |
| 136 |
int is_reader; |
| 137 |
} Win32IO; |
| 138 |
|
| 139 |
typedef struct { |
| 140 |
const tsd_t *TSD; |
| 141 |
Win32IO h[3]; |
| 142 |
} AsyncInfo; |
| 143 |
|
| 144 |
static BOOL MyCancelIo(HANDLE handle) |
| 145 |
{ |
| 146 |
static BOOL (WINAPI *DoCancelIo)(HANDLE handle) = NULL; |
| 147 |
static BOOL first = TRUE; |
| 148 |
HMODULE mod; |
| 149 |
|
| 150 |
if ( first ) |
| 151 |
{ |
| 152 |
/* |
| 153 |
* The kernel is always mapped, a LoadLibrary is useless |
| 154 |
*/ |
| 155 |
if ( ( mod = GetModuleHandle( "kernel32" ) ) != NULL ) |
| 156 |
{ |
| 157 |
DoCancelIo = (BOOL (WINAPI*)(HANDLE)) GetProcAddress( mod, "CancelIo" ); |
| 158 |
} |
| 159 |
|
| 160 |
/* |
| 161 |
* It's safe now to set first. Never do it before the main work, |
| 162 |
* otherwise we're not reentrant. |
| 163 |
*/ |
| 164 |
first = FALSE; |
| 165 |
} |
| 166 |
|
| 167 |
if ( DoCancelIo == NULL ) |
| 168 |
return FALSE; |
| 169 |
|
| 170 |
return DoCancelIo( handle ); |
| 171 |
} |
| 172 |
|
| 173 |
static int Win_setenv( const char *name, const char *value ) |
| 174 |
{ |
| 175 |
return SetEnvironmentVariable( name, value ); |
| 176 |
} |
| 177 |
|
| 178 |
/* fork_exec spawns a new process with the given commandline. |
| 179 |
* it returns -1 on error (errno set), 0 on process start error (rcode set), |
| 180 |
* a process descriptor otherwise. |
| 181 |
* Basically this is a child process and we run in the child's environment |
| 182 |
* after the first few lines. The setup hasn't been done and the command needs |
| 183 |
* to be started. |
| 184 |
* Redirection takes place if one of the handles env->input.hdls[0], |
| 185 |
* env->output.hdls[1] or env->error.hdls[1] is defined. Other handles (except |
| 186 |
* standard handles) are closed. env->subtype must be a SUBENVIR_... constant. |
| 187 |
* cmdline is the whole command line. |
| 188 |
* Never use TSD after the fork() since this is not only a different thread, |
| 189 |
* it's a different process! |
| 190 |
*/ |
| 191 |
static int Win_fork_exec(tsd_t *TSD, environment *env, const char *cmdline, int *rcode) |
| 192 |
{ |
| 193 |
static const char *interpreter[] = { "regina.exe", /* preferable even if */ |
| 194 |
/* not dynamic */ |
| 195 |
"rexx.exe" }; |
| 196 |
PROCESS_INFORMATION pinfo; |
| 197 |
STARTUPINFO sinfo; |
| 198 |
DWORD done; |
| 199 |
char *execname = NULL; |
| 200 |
const char *commandline = NULL; |
| 201 |
char *argline = NULL; |
| 202 |
BOOL rc; |
| 203 |
int broken_address_command = get_options_flag( TSD->currlevel, EXT_BROKEN_ADDRESS_COMMAND ); |
| 204 |
int subtype; |
| 205 |
|
| 206 |
if (env->subtype == SUBENVIR_REXX) /*special situation caused by recursion*/ |
| 207 |
{ |
| 208 |
environment e = *env; |
| 209 |
char *new_cmdline; |
| 210 |
int i, rc1; |
| 211 |
unsigned len; |
| 212 |
|
| 213 |
if (argv0 == NULL) |
| 214 |
len = 11; /* max("rexx.exe", "regina.exe") */ |
| 215 |
else |
| 216 |
{ |
| 217 |
len = strlen(argv0) + 2; |
| 218 |
if (len < 11) |
| 219 |
len = 11; /* max("rexx.exe", "regina.exe") */ |
| 220 |
} |
| 221 |
len += strlen(cmdline) + 2; /* Blank + term ASCII0 */ |
| 222 |
|
| 223 |
if ((new_cmdline = malloc(len)) == NULL) |
| 224 |
return(-1); /* ENOMEM is set */ |
| 225 |
|
| 226 |
if (argv0 != NULL) /* always the best choice */ |
| 227 |
{ |
| 228 |
strcpy(new_cmdline, "\""); |
| 229 |
strcat(new_cmdline, argv0); |
| 230 |
strcat(new_cmdline, "\" "); |
| 231 |
strcat(new_cmdline, cmdline); |
| 232 |
e.subtype = SUBENVIR_COMMAND; |
| 233 |
rc1 = Win_fork_exec(TSD, &e, new_cmdline, &rc1); |
| 234 |
if ( ( rc1 != 0 ) && ( rc1 != -1 ) ) |
| 235 |
{ |
| 236 |
free(new_cmdline); |
| 237 |
return(rc1); |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
/* load an interpreter by name from the path */ |
| 242 |
for (i = 0; i < sizeof(interpreter) / sizeof(interpreter[0]);i++) |
| 243 |
{ |
| 244 |
strcpy(new_cmdline, interpreter[i]); |
| 245 |
strcat(new_cmdline, " "); |
| 246 |
strcat(new_cmdline, cmdline); |
| 247 |
e.subtype = SUBENVIR_COMMAND; |
| 248 |
rc1 = Win_fork_exec(TSD, &e, new_cmdline, &rc1); |
| 249 |
if ( ( rc1 != 0 ) && ( rc1 != -1 ) ) |
| 250 |
{ |
| 251 |
free(new_cmdline); |
| 252 |
return(rc1); |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
*rcode = -errno; /* assume a load error */ |
| 257 |
free(new_cmdline); |
| 258 |
return(0); |
| 259 |
} |
| 260 |
|
| 261 |
memset(&sinfo, 0, sizeof(sinfo)); |
| 262 |
sinfo.cb = sizeof(sinfo); |
| 263 |
|
| 264 |
sinfo.dwFlags = STARTF_USESTDHANDLES; |
| 265 |
|
| 266 |
/* The following three handles have been created inheritable */ |
| 267 |
if (env->input.hdls[0] != -1) |
| 268 |
{ |
| 269 |
if ( IsWin9X() ) |
| 270 |
{ |
| 271 |
sinfo.hStdInput = (HANDLE) _get_osfhandle(env->input.hdls[0]); |
| 272 |
/* |
| 273 |
* fixes Bug 587687 |
| 274 |
* We must ensure the called process already *uses* the handles; |
| 275 |
* they are closed by the OS in the other case. One chance is to |
| 276 |
* delay the execution until this happens, the other chance is to |
| 277 |
* wait with the closing until the called process works or |
| 278 |
* terminates. We use the latter one. For more info see MSDN. |
| 279 |
* Topics: Q190351, *Q150956* |
| 280 |
* I think, Q150956 is a workaround for the bug they didn't solve. |
| 281 |
* M$ just close the handles too early. |
| 282 |
*/ |
| 283 |
DuplicateHandle( GetCurrentProcess(), |
| 284 |
sinfo.hStdInput, |
| 285 |
GetCurrentProcess(), |
| 286 |
&sinfo.hStdInput, |
| 287 |
0, |
| 288 |
TRUE, |
| 289 |
DUPLICATE_SAME_ACCESS ); |
| 290 |
/* |
| 291 |
* fixes bug 700405 |
| 292 |
*/ |
| 293 |
env->input.hdls[2] = (int) sinfo.hStdInput; |
| 294 |
} |
| 295 |
else |
| 296 |
sinfo.hStdInput = (HANDLE) env->input.hdls[0]; |
| 297 |
} |
| 298 |
else |
| 299 |
sinfo.hStdInput = GetStdHandle(STD_INPUT_HANDLE); |
| 300 |
if (env->output.hdls[1] != -1) |
| 301 |
{ |
| 302 |
if ( IsWin9X() ) |
| 303 |
{ |
| 304 |
sinfo.hStdOutput = (HANDLE) _get_osfhandle(env->output.hdls[1]); |
| 305 |
DuplicateHandle( GetCurrentProcess(), |
| 306 |
sinfo.hStdOutput, |
| 307 |
GetCurrentProcess(), |
| 308 |
&sinfo.hStdOutput, |
| 309 |
0, |
| 310 |
TRUE, |
| 311 |
DUPLICATE_SAME_ACCESS ); |
| 312 |
/* |
| 313 |
* fixes bug 700405 |
| 314 |
*/ |
| 315 |
env->output.hdls[2] = (int) sinfo.hStdOutput; |
| 316 |
} |
| 317 |
else |
| 318 |
sinfo.hStdOutput = (HANDLE) env->output.hdls[1]; |
| 319 |
} |
| 320 |
else |
| 321 |
sinfo.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE); |
| 322 |
if (env->error.SameAsOutput) |
| 323 |
sinfo.hStdError = (HANDLE) sinfo.hStdOutput; |
| 324 |
else if (env->error.hdls[1] != -1) |
| 325 |
{ |
| 326 |
if ( IsWin9X() ) |
| 327 |
{ |
| 328 |
sinfo.hStdError = (HANDLE) _get_osfhandle(env->error.hdls[1]); |
| 329 |
DuplicateHandle( GetCurrentProcess(), |
| 330 |
sinfo.hStdError, |
| 331 |
GetCurrentProcess(), |
| 332 |
&sinfo.hStdError, |
| 333 |
0, |
| 334 |
TRUE, |
| 335 |
DUPLICATE_SAME_ACCESS ); |
| 336 |
/* |
| 337 |
* fixes bug 700405 |
| 338 |
*/ |
| 339 |
env->error.hdls[2] = (int) sinfo.hStdError; |
| 340 |
} |
| 341 |
else |
| 342 |
sinfo.hStdError = (HANDLE) env->error.hdls[1]; |
| 343 |
} |
| 344 |
else |
| 345 |
sinfo.hStdError = GetStdHandle(STD_ERROR_HANDLE); |
| 346 |
|
| 347 |
/* |
| 348 |
* If the BROKEN_ADDRESS_COMMAND OPTION is in place, |
| 349 |
* and our environment is COMMAND, change it to SYSTEM |
| 350 |
*/ |
| 351 |
if ( env->subtype == SUBENVIR_PATH /* was SUBENVIR_COMMAND */ |
| 352 |
&& broken_address_command ) |
| 353 |
subtype = SUBENVIR_SYSTEM; |
| 354 |
else |
| 355 |
subtype = env->subtype; |
| 356 |
|
| 357 |
switch ( subtype ) |
| 358 |
{ |
| 359 |
case SUBENVIR_PATH: |
| 360 |
execname = NULL; |
| 361 |
commandline = cmdline; |
| 362 |
break; |
| 363 |
|
| 364 |
case SUBENVIR_COMMAND: |
| 365 |
execname = NULL; |
| 366 |
commandline = cmdline; |
| 367 |
#define NEED_SPLITOFFARG |
| 368 |
execname = splitoffarg(cmdline, NULL, '^'); |
| 369 |
commandline = cmdline; |
| 370 |
break; |
| 371 |
|
| 372 |
case SUBENVIR_SYSTEM: |
| 373 |
/* insert "%COMSPEC% /c " or "%SHELL% -c " in front */ |
| 374 |
if ((done = GetEnvironmentVariable("COMSPEC","",0)) != 0) |
| 375 |
{ |
| 376 |
argline = MallocTSD(done + strlen(cmdline) + 5); |
| 377 |
GetEnvironmentVariable("COMSPEC",argline,done + 5); |
| 378 |
strcat(argline," /c "); |
| 379 |
} |
| 380 |
else if ((done = GetEnvironmentVariable("SHELL","",0)) != 0) |
| 381 |
{ |
| 382 |
argline = MallocTSD(done + strlen(cmdline) + 5); |
| 383 |
GetEnvironmentVariable("SHELL",argline,done + 5); |
| 384 |
strcat(argline," -c "); |
| 385 |
} |
| 386 |
else |
| 387 |
{ |
| 388 |
argline = MallocTSD(11 + strlen(cmdline) + 5); |
| 389 |
if (GetVersion() & 0x80000000) /* not NT ? */ |
| 390 |
strcpy(argline,"COMMAND.COM /c "); |
| 391 |
else |
| 392 |
strcpy(argline,"CMD.EXE /c "); |
| 393 |
} |
| 394 |
strcat(argline, cmdline); |
| 395 |
execname = NULL; |
| 396 |
commandline = argline; |
| 397 |
break; |
| 398 |
|
| 399 |
case SUBENVIR_REXX: |
| 400 |
/* fall through */ |
| 401 |
|
| 402 |
default: /* illegal subtype */ |
| 403 |
errno = EINVAL; |
| 404 |
return -1; |
| 405 |
} |
| 406 |
|
| 407 |
/* |
| 408 |
* FGC: I checked different configurations. |
| 409 |
* 1) Never use DETACHED_PROCESS, the communication gets lost in NT |
| 410 |
* kernels to a text program invoked using shells. |
| 411 |
* 2) Never use STARTF_USESHOWWINDOW/SW_HIDE in Win9x, this gives problems |
| 412 |
* under Win9x or you have to know which combination fits to COMSPEC and |
| 413 |
* your called program's type. |
| 414 |
* --> We can fiddle with CREATE_NEW_CONSOLE, CREATE_NEW_PROCESS_GROUP, |
| 415 |
* CREATE_DEFAULT_ERROR_MODE. Select your choice! |
| 416 |
*/ |
| 417 |
if ( !IsWin9X() ) |
| 418 |
{ |
| 419 |
sinfo.dwFlags |= STARTF_USESHOWWINDOW; |
| 420 |
sinfo.wShowWindow = SW_HIDE; |
| 421 |
} |
| 422 |
rc = CreateProcess(execname, |
| 423 |
(char *) commandline, |
| 424 |
NULL, /* pointer to process security attributes */ |
| 425 |
NULL, /* pointer to thread security attributes */ |
| 426 |
TRUE, /* no inheritance except sinfo.hStd... */ |
| 427 |
CREATE_NEW_PROCESS_GROUP | CREATE_DEFAULT_ERROR_MODE, |
| 428 |
NULL, /* pointer to new environment block */ |
| 429 |
NULL, /* pointer to current directory name */ |
| 430 |
&sinfo, |
| 431 |
&pinfo); |
| 432 |
*rcode = (int) GetLastError(); |
| 433 |
if (argline) |
| 434 |
FreeTSD(argline); |
| 435 |
if (execname) |
| 436 |
free(execname); |
| 437 |
if ( !rc ) |
| 438 |
return 0; |
| 439 |
|
| 440 |
CloseHandle(pinfo.hThread); /* We don't need it */ |
| 441 |
return (int) pinfo.hProcess; |
| 442 |
} |
| 443 |
|
| 444 |
/* __regina_wait waits for a process started by fork_exec. |
| 445 |
* In general, this is called after the complete IO to the called process but |
| 446 |
* it isn't guaranteed. Never call if you don't expect a sudden death of the |
| 447 |
* subprocess. |
| 448 |
* Returns the exit status of the subprocess under normal circumstances. If |
| 449 |
* the subprocess has died by a signal, the return value is -signalnumber. |
| 450 |
*/ |
| 451 |
static int Win_wait(int process) |
| 452 |
{ |
| 453 |
DWORD code = (DWORD) -1; /* in case something goes wrong */ |
| 454 |
|
| 455 |
WaitForSingleObject((HANDLE) process, INFINITE); |
| 456 |
GetExitCodeProcess((HANDLE) process, &code); |
| 457 |
CloseHandle((HANDLE) process); |
| 458 |
|
| 459 |
if ((code & 0xC0000000) == 0xC0000000) /* assume signal */ |
| 460 |
return -(int)(code & ~0xC0000000); |
| 461 |
return (int) code; |
| 462 |
} |
| 463 |
|
| 464 |
/* open_subprocess_connection acts like the unix-known function pipe and sets |
| 465 |
* ep->RedirectedFile if necessary. Just in the latter case ep->data |
| 466 |
* is set to the filename. |
| 467 |
* Close the handles with __regina_close later. |
| 468 |
* Do IO by using __regina_read() and __regina_write(). |
| 469 |
*/ |
| 470 |
static int Win_open_subprocess_connection(const tsd_t *TSD, environpart *ep) |
| 471 |
{ |
| 472 |
#define MAGIC_MAX 2000000 /* Much beyond maximum, see below */ |
| 473 |
static volatile unsigned BaseIndex = MAGIC_MAX; |
| 474 |
char buf[40]; |
| 475 |
unsigned i; |
| 476 |
unsigned start,run; |
| 477 |
DWORD openmode, err; |
| 478 |
HANDLE in, out; |
| 479 |
OVERLAPPED ol; |
| 480 |
SECURITY_ATTRIBUTES sa; |
| 481 |
|
| 482 |
in = INVALID_HANDLE_VALUE; |
| 483 |
|
| 484 |
/* Anonymous pipes can't be run in overlapped mode. Therefore we use |
| 485 |
* named pipes (and files for W9x). |
| 486 |
*/ |
| 487 |
sa.nLength = sizeof(sa); |
| 488 |
sa.lpSecurityDescriptor = NULL; |
| 489 |
sa.bInheritHandle = TRUE; |
| 490 |
openmode = (ep->flags.isinput) ? PIPE_ACCESS_OUTBOUND : PIPE_ACCESS_INBOUND; |
| 491 |
openmode |= FILE_FLAG_OVERLAPPED; |
| 492 |
|
| 493 |
/* algorithm: |
| 494 |
* select a random number, e.g. a mixture of pid, tid and time. |
| 495 |
* increment this number by a fixed amount until we reach the starting |
| 496 |
* value again. Do a wrap around and an increment which is a unique prime. |
| 497 |
* The number 1000000 has the primes 2 and 5. We may use all primes |
| 498 |
* except 2 and 5; 9901 (which is prime) will give a good distribution. |
| 499 |
* |
| 500 |
* We want to be able to create several temporary files at once without |
| 501 |
* more OS calls than needed. Thus, we have a program wide runner to |
| 502 |
* ensure a simple distribution without strength. |
| 503 |
*/ |
| 504 |
if (BaseIndex == MAGIC_MAX) |
| 505 |
{ |
| 506 |
/* |
| 507 |
* We have to create (nearly) reentrant code. |
| 508 |
*/ |
| 509 |
i = (unsigned) getpid() * (unsigned) time(NULL); |
| 510 |
i %= 1000000; |
| 511 |
if (!i) |
| 512 |
i = 1; |
| 513 |
BaseIndex = i; |
| 514 |
} |
| 515 |
if (++BaseIndex >= 1000000) |
| 516 |
BaseIndex = 1; |
| 517 |
|
| 518 |
start = TSD->thread_id; |
| 519 |
if (start == 0) |
| 520 |
start = 999999; |
| 521 |
start *= (unsigned) (clock() + 1); |
| 522 |
start *= BaseIndex; |
| 523 |
start %= 1000000; |
| 524 |
|
| 525 |
run = start; |
| 526 |
for (i = 0;i <= 1000000;i++) |
| 527 |
{ |
| 528 |
sprintf(buf,"%s%06u._rx", "\\\\.\\pipe\\tmp\\", run ); |
| 529 |
in = CreateNamedPipe(buf, |
| 530 |
openmode, |
| 531 |
PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT, |
| 532 |
1, /* just this instance! */ |
| 533 |
0, /* OutBufferSize: use system minimum, e.g.4K */ |
| 534 |
0, /* InBufferSize: use system minimum, e.g.4K */ |
| 535 |
0, /* nDefaultTimeout */ |
| 536 |
NULL); /* This handle won't be shared */ |
| 537 |
if (in != INVALID_HANDLE_VALUE) |
| 538 |
break; |
| 539 |
|
| 540 |
err = GetLastError(); |
| 541 |
if (err != ERROR_ACCESS_DENIED) |
| 542 |
{ |
| 543 |
errno = EPIPE; |
| 544 |
return(-1); |
| 545 |
} |
| 546 |
|
| 547 |
/* Check the next possible candidate */ |
| 548 |
run += 9901; |
| 549 |
run %= 1000000; |
| 550 |
if (run == start) /* paranoia check. i <= 1000000 should hit exactly */ |
| 551 |
break; /* here */ |
| 552 |
} |
| 553 |
|
| 554 |
if (in == INVALID_HANDLE_VALUE) |
| 555 |
{ |
| 556 |
errno = EPIPE; |
| 557 |
return(-1); |
| 558 |
} |
| 559 |
|
| 560 |
/* Prepare this pipe and then try to connect it to ourself. */ |
| 561 |
if ((ol.hEvent = CreateEvent(NULL, TRUE, TRUE, NULL)) == NULL) |
| 562 |
{ |
| 563 |
CloseHandle(in); |
| 564 |
errno = EPIPE; |
| 565 |
return(-1); |
| 566 |
} |
| 567 |
ConnectNamedPipe(in, &ol); /* ignore the return */ |
| 568 |
|
| 569 |
out = CreateFile(buf, |
| 570 |
(ep->flags.isinput) ? GENERIC_READ : GENERIC_WRITE, |
| 571 |
0, /* dwShareMode */ |
| 572 |
&sa, |
| 573 |
OPEN_EXISTING, |
| 574 |
FILE_ATTRIBUTE_NORMAL, /* not OVERLAPPED! */ |
| 575 |
NULL); /* hTemplateFile */ |
| 576 |
if (out == INVALID_HANDLE_VALUE) |
| 577 |
{ |
| 578 |
MyCancelIo(in); |
| 579 |
CloseHandle(ol.hEvent); |
| 580 |
CloseHandle(in); |
| 581 |
errno = EPIPE; /* guess */ |
| 582 |
return(-1); |
| 583 |
} |
| 584 |
|
| 585 |
/* Now do the final checking, the server end must be connected */ |
| 586 |
if (!GetOverlappedResult(in, &ol, &openmode /* dummy */, FALSE)) |
| 587 |
{ |
| 588 |
CloseHandle(out); |
| 589 |
MyCancelIo(in); |
| 590 |
CloseHandle(ol.hEvent); |
| 591 |
CloseHandle(in); |
| 592 |
errno = EPIPE; /* guess */ |
| 593 |
return(-1); |
| 594 |
} |
| 595 |
CloseHandle(ol.hEvent); |
| 596 |
|
| 597 |
/* We always want to have the server's end of the named pipe: */ |
| 598 |
if (ep->flags.isinput) |
| 599 |
{ |
| 600 |
ep->hdls[0] = (int) out; |
| 601 |
ep->hdls[1] = (int) in; |
| 602 |
} |
| 603 |
else |
| 604 |
{ |
| 605 |
ep->hdls[0] = (int) in; |
| 606 |
ep->hdls[1] = (int) out; |
| 607 |
} |
| 608 |
return(0); |
| 609 |
#undef MAGIC_MAX |
| 610 |
} |
| 611 |
|
| 612 |
/* sets the given handle to the non-blocking mode. The value may change. |
| 613 |
* async_info CAN be used to add the handle to the internal list of watched |
| 614 |
* handles. |
| 615 |
*/ |
| 616 |
static void Win_unblock_handle( int *handle, void *async_info ) |
| 617 |
{ |
| 618 |
AsyncInfo *ai; |
| 619 |
Win32IO *w; |
| 620 |
HANDLE hdl; |
| 621 |
unsigned i; |
| 622 |
|
| 623 |
ai = async_info; |
| 624 |
hdl = (HANDLE) *handle; |
| 625 |
|
| 626 |
assert(async_info != NULL); |
| 627 |
for (i = 0, w = ai->h; i < 3; i++, w++) |
| 628 |
if (w->hdl == INVALID_HANDLE_VALUE) |
| 629 |
break; |
| 630 |
assert(i < 3); |
| 631 |
w->hdl = hdl; |
| 632 |
w->ol.hEvent = CreateEvent(NULL, TRUE, TRUE, NULL); |
| 633 |
} |
| 634 |
|
| 635 |
/* sets the file pointer of the given handle to the beginning. |
| 636 |
*/ |
| 637 |
static void Win_restart_file(int hdl) |
| 638 |
{ |
| 639 |
assert((HANDLE) hdl == INVALID_HANDLE_VALUE); |
| 640 |
} |
| 641 |
|
| 642 |
/* __regina_close acts like close() but closes a handle returned by |
| 643 |
* open_subprocess_connection. |
| 644 |
* async_info MAY be used to delete the handle from the internal list of |
| 645 |
* watched handles. |
| 646 |
*/ |
| 647 |
static int Win_close(int handle, void *async_info) |
| 648 |
{ |
| 649 |
AsyncInfo *ai; |
| 650 |
Win32IO *w; |
| 651 |
HANDLE hdl; |
| 652 |
unsigned i; |
| 653 |
|
| 654 |
ai = async_info; |
| 655 |
hdl = (HANDLE) handle; |
| 656 |
|
| 657 |
if (hdl == INVALID_HANDLE_VALUE) |
| 658 |
{ |
| 659 |
errno = EINVAL; |
| 660 |
return(-1); |
| 661 |
} |
| 662 |
|
| 663 |
if (ai == NULL) /* unblocked handle? */ |
| 664 |
{ |
| 665 |
if (CloseHandle(hdl)) |
| 666 |
return(0); |
| 667 |
errno = ENOSPC; /* guess */ |
| 668 |
return(-1); |
| 669 |
} |
| 670 |
|
| 671 |
for (i = 0, w = ai->h; i < 3; i++, w++) |
| 672 |
if (w->hdl == hdl) |
| 673 |
break; |
| 674 |
|
| 675 |
if (i == 3) |
| 676 |
{ |
| 677 |
CloseHandle(hdl); |
| 678 |
errno = EINVAL; |
| 679 |
return(-1); |
| 680 |
} |
| 681 |
|
| 682 |
MyCancelIo(hdl); |
| 683 |
CloseHandle(w->ol.hEvent); |
| 684 |
w->hdl = INVALID_HANDLE_VALUE; |
| 685 |
if (w->buf) |
| 686 |
Free_TSD(ai->TSD, w->buf); |
| 687 |
if (CloseHandle(hdl)) |
| 688 |
return(0); |
| 689 |
errno = ENOSPC; /* guess */ |
| 690 |
return(-1); |
| 691 |
} |
| 692 |
|
| 693 |
/* |
| 694 |
* __regina_close_special acts like close() but closes any OS specific handle. |
| 695 |
* The close happens if the handle is not -1. A specific operation may be |
| 696 |
* associated with this. Have a look for occurances of "hdls[2]". |
| 697 |
*/ |
| 698 |
static void Win_close_special( int handle ) |
| 699 |
{ |
| 700 |
if ( handle ) |
| 701 |
CloseHandle( (HANDLE) handle ); |
| 702 |
} |
| 703 |
|
| 704 |
/* __regina_read acts like read() but returns either an error (return code |
| 705 |
* == -errno) or the number of bytes read. EINTR and friends leads to a |
| 706 |
* re-read. |
| 707 |
* async_info is both a structure and a flag. If set, asynchronous IO shall |
| 708 |
* be used, otherwise blocked IO has to be used. |
| 709 |
*/ |
| 710 |
static int Win_read(int handle, void *buf, unsigned size, void *async_info) |
| 711 |
{ |
| 712 |
AsyncInfo *ai; |
| 713 |
Win32IO *w; |
| 714 |
HANDLE hdl; |
| 715 |
unsigned i; |
| 716 |
OVERLAPPED *ol; |
| 717 |
DWORD done; |
| 718 |
int retval; |
| 719 |
|
| 720 |
ai = async_info; |
| 721 |
hdl = (HANDLE) handle; |
| 722 |
retval = 0; |
| 723 |
|
| 724 |
if (ai == NULL) |
| 725 |
{ |
| 726 |
if (!ReadFile( (HANDLE) hdl, buf, size, &done, NULL)) |
| 727 |
{ |
| 728 |
retval = (int) GetLastError(); |
| 729 |
if (retval == ERROR_BROKEN_PIPE) |
| 730 |
return(0); /* "Normal" EOF */ |
| 731 |
return(-EPIPE); /* guess */ |
| 732 |
} |
| 733 |
retval = (int) done; |
| 734 |
return(retval); |
| 735 |
} |
| 736 |
|
| 737 |
/* Async IO */ |
| 738 |
for (i = 0, w = ai->h; i < 3; i++, w++) |
| 739 |
{ |
| 740 |
if (w->hdl == hdl) |
| 741 |
break; |
| 742 |
} |
| 743 |
if (i == 3) |
| 744 |
return(-EINVAL); |
| 745 |
ol = &w->ol; |
| 746 |
|
| 747 |
if (w->reading) /* pending IO? */ |
| 748 |
{ |
| 749 |
if (!HasOverlappedIoCompleted(ol)) |
| 750 |
{ |
| 751 |
done = 0; |
| 752 |
retval = -EAGAIN; |
| 753 |
} |
| 754 |
else if (!GetOverlappedResult(hdl, ol, &done, FALSE)) |
| 755 |
{ |
| 756 |
done = 0; |
| 757 |
if (GetLastError() == ERROR_IO_PENDING) |
| 758 |
{ |
| 759 |
retval = -EAGAIN; |
| 760 |
} |
| 761 |
else |
| 762 |
{ |
| 763 |
w->reading = 0; |
| 764 |
retval = -EPIPE; |
| 765 |
} |
| 766 |
} |
| 767 |
else |
| 768 |
w->reading = 0; |
| 769 |
|
| 770 |
if (done) |
| 771 |
w->rused += done; |
| 772 |
} |
| 773 |
|
| 774 |
if (w->rused) |
| 775 |
{ |
| 776 |
if (size > w->rused) |
| 777 |
size = w->rused; |
| 778 |
memcpy(buf, w->buf + w->rusedbegin, size); |
| 779 |
w->rusedbegin += size; |
| 780 |
w->rused -= size; |
| 781 |
retval = size; |
| 782 |
} |
| 783 |
else |
| 784 |
retval = -EAGAIN; /* still may change! */ |
| 785 |
|
| 786 |
if (w->maxbuf < REGINA_BUFFER_SIZE) /* Buffer not allocated? */ |
| 787 |
{ |
| 788 |
/* Never allocate too much, we want a fast response to do some |
| 789 |
* work! |
| 790 |
*/ |
| 791 |
if (w->buf) /* THIS IS DEFINITELY A BUG! */ |
| 792 |
Free_TSD(ai->TSD, w->buf); |
| 793 |
w->maxbuf = REGINA_BUFFER_SIZE; |
| 794 |
w->rusedbegin = 0; |
| 795 |
w->rused = 0; |
| 796 |
w->reading = 0; |
| 797 |
w->buf = Malloc_TSD(ai->TSD, w->maxbuf); |
| 798 |
} |
| 799 |
|
| 800 |
if (w->reading) /* Pending IO, we can't do more */ |
| 801 |
return(retval); |
| 802 |
|
| 803 |
/* not reading or no longer reading, initiate a new reading */ |
| 804 |
if (w->rused && (w->rusedbegin != 0)) |
| 805 |
memmove(w->buf, w->buf + w->rusedbegin, w->rused); |
| 806 |
w->rusedbegin = 0; |
| 807 |
|
| 808 |
if (w->rused < w->maxbuf) |
| 809 |
{ |
| 810 |
w->reading = w->maxbuf - w->rused; |
| 811 |
ResetEvent(w->ol.hEvent); |
| 812 |
if (!ReadFile((HANDLE) hdl, |
| 813 |
w->buf + w->rused, |
| 814 |
w->reading, |
| 815 |
&done, |
| 816 |
ol)) |
| 817 |
{ |
| 818 |
done = GetLastError(); |
| 819 |
if (done == ERROR_IO_PENDING) |
| 820 |
return(retval); |
| 821 |
|
| 822 |
w->reading = 0; |
| 823 |
if ((retval > 0) || (w->rused != 0)) |
| 824 |
return(retval); |
| 825 |
|
| 826 |
if (done == ERROR_BROKEN_PIPE) |
| 827 |
return(0); /* "Normal" EOF */ |
| 828 |
return(-EPIPE); /* guess */ |
| 829 |
} |
| 830 |
|
| 831 |
/* success */ |
| 832 |
w->reading = 0; |
| 833 |
w->rused += done; |
| 834 |
} |
| 835 |
|
| 836 |
if ((retval < 0) && w->rused) /* fresh data? return at once */ |
| 837 |
{ |
| 838 |
if (size > w->rused) |
| 839 |
size = w->rused; |
| 840 |
memcpy(buf, w->buf + w->rusedbegin, size); |
| 841 |
w->rusedbegin += size; |
| 842 |
w->rused -= size; |
| 843 |
retval = size; |
| 844 |
} |
| 845 |
return(retval); |
| 846 |
} |
| 847 |
|
| 848 |
/* __regina_write acts like write() but returns either an error (return code |
| 849 |
* == -errno) or the number of bytes written. EINTR and friends leads to a |
| 850 |
* re-write. |
| 851 |
* async_info is both a structure and a flag. If set, asynchronous IO shall |
| 852 |
* be used, otherwise blocked IO has to be used. |
| 853 |
* The file must be flushed if buf or size are 0. Even in this case -EAGAIN |
| 854 |
* may be returned. |
| 855 |
*/ |
| 856 |
static int Win_write(int handle, const void *buf, unsigned size, void *async_info) |
| 857 |
{ |
| 858 |
AsyncInfo *ai; |
| 859 |
Win32IO *w; |
| 860 |
HANDLE hdl; |
| 861 |
unsigned i; |
| 862 |
OVERLAPPED *ol; |
| 863 |
DWORD done; |
| 864 |
int retval; |
| 865 |
int pending = 0; |
| 866 |
|
| 867 |
ai = async_info; |
| 868 |
hdl = (HANDLE) handle; |
| 869 |
retval = 0; |
| 870 |
|
| 871 |
if (!ai) |
| 872 |
{ |
| 873 |
if ((buf == NULL) | (size == 0)) |
| 874 |
return(0); /* flushing is useless here! */ |
| 875 |
|
| 876 |
retval = 0; |
| 877 |
do { |
| 878 |
if (!WriteFile(hdl, buf, size, &done, NULL)) |
| 879 |
{ |
| 880 |
return(-EPIPE); /* guess */ |
| 881 |
} |
| 882 |
retval += (int) done; |
| 883 |
buf = (const void *) ((char *) buf + done); |
| 884 |
size -= done; |
| 885 |
} while (retval < (int) size); |
| 886 |
return retval; |
| 887 |
} |
| 888 |
|
| 889 |
/* Async IO */ |
| 890 |
for (i = 0, w = ai->h; i < 3; i++, w++) |
| 891 |
{ |
| 892 |
if (w->hdl == hdl) |
| 893 |
break; |
| 894 |
} |
| 895 |
if (i == 3) |
| 896 |
return(-EINVAL); |
| 897 |
ol = &w->ol; |
| 898 |
|
| 899 |
/* |
| 900 |
* Reap any pending overlapped IO first. |
| 901 |
*/ |
| 902 |
if (w->wused) /* pending IO? */ |
| 903 |
{ |
| 904 |
if (!HasOverlappedIoCompleted(ol)) |
| 905 |
{ |
| 906 |
if ((w->wused == w->maxbuf) || (buf == NULL) || (size == 0)) |
| 907 |
return(-EAGAIN); |
| 908 |
pending = 1; |
| 909 |
} |
| 910 |
else |
| 911 |
{ |
| 912 |
if (!GetOverlappedResult(hdl, ol, &done, FALSE)) |
| 913 |
{ |
| 914 |
done = GetLastError(); |
| 915 |
if (done == ERROR_IO_PENDING) |
| 916 |
return(-EAGAIN); |
| 917 |
return(-EPIPE); /* guess */ |
| 918 |
} |
| 919 |
if (done < w->wused) |
| 920 |
{ |
| 921 |
memmove(w->buf, w->buf + done, w->wused - done); |
| 922 |
w->wused -= done; |
| 923 |
} |
| 924 |
else |
| 925 |
w->wused = 0; |
| 926 |
} |
| 927 |
} |
| 928 |
|
| 929 |
if (((buf == NULL) || (size == 0)) && !w->wused) |
| 930 |
{ |
| 931 |
return 0; |
| 932 |
} |
| 933 |
|
| 934 |
if (w->buf == NULL) |
| 935 |
{ |
| 936 |
w->maxbuf = REGINA_MAX_BUFFER_SIZE; |
| 937 |
w->buf = Malloc_TSD(ai->TSD, w->maxbuf); |
| 938 |
} |
| 939 |
|
| 940 |
/* |
| 941 |
* At this point we may have set pending, in which case it is forbidden |
| 942 |
* to reinitiate a write or a semaphore event. We just may ADD something to |
| 943 |
* a buffer. |
| 944 |
*/ |
| 945 |
|
| 946 |
if (w->wused < w->maxbuf) |
| 947 |
{ |
| 948 |
retval = (int) (w->maxbuf - w->wused); |
| 949 |
if (retval > (int) size) |
| 950 |
{ |
| 951 |
retval = (int) size; |
| 952 |
} |
| 953 |
memcpy(w->buf + w->wused, buf, retval); |
| 954 |
w->wused += (unsigned) retval; |
| 955 |
if (pending) |
| 956 |
{ |
| 957 |
return retval; /* fixes bug 945218 */ |
| 958 |
} |
| 959 |
} |
| 960 |
else |
| 961 |
return -EAGAIN; /* Nothing written/copied, must be pending */ |
| 962 |
|
| 963 |
ResetEvent(ol->hEvent); |
| 964 |
|
| 965 |
if (!WriteFile(hdl, w->buf, w->wused, &done, ol)) |
| 966 |
// if (!WriteFile(hdl, w->buf, w->wused, NULL, ol)) |
| 967 |
{ |
| 968 |
done = (int) GetLastError(); |
| 969 |
if (done == ERROR_IO_PENDING) |
| 970 |
return((retval == 0) ? -EAGAIN : retval); |
| 971 |
return(-EPIPE); /* guess */ |
| 972 |
} |
| 973 |
/* No errors, thus success. We don't want to redo all the stuff. We |
| 974 |
* simply set your wait-semaphore to prevent sleeping. |
| 975 |
*/ |
| 976 |
SetEvent(ol->hEvent); |
| 977 |
|
| 978 |
return(retval); |
| 979 |
} |
| 980 |
|
| 981 |
/* create_async_info return an opaque structure to allow the process wait for |
| 982 |
* asyncronous IO. There are three IO slots (in, out, error) which can be |
| 983 |
* filled by add_waiter. The structure can be cleaned by reset_async_info. |
| 984 |
* The structure must be destroyed by delete_async_info. |
| 985 |
*/ |
| 986 |
static void *Win_create_async_info(const tsd_t *TSD) |
| 987 |
{ |
| 988 |
AsyncInfo *retval; |
| 989 |
|
| 990 |
retval = MallocTSD(sizeof(AsyncInfo)); |
| 991 |
memset(retval, 0, sizeof(AsyncInfo)); |
| 992 |
|
| 993 |
retval->TSD = TSD; |
| 994 |
retval->h[0].hdl = INVALID_HANDLE_VALUE; |
| 995 |
retval->h[0].ol.hEvent = INVALID_HANDLE_VALUE; |
| 996 |
retval->h[1] = retval->h[0]; |
| 997 |
retval->h[2] = retval->h[0]; |
| 998 |
return(retval); |
| 999 |
} |
| 1000 |
|
| 1001 |
/* delete_async_info deletes the structure created by create_async_info and |
| 1002 |
* all of its components. |
| 1003 |
*/ |
| 1004 |
static void Win_delete_async_info(void *async_info) |
| 1005 |
{ |
| 1006 |
AsyncInfo *ai; |
| 1007 |
unsigned i; |
| 1008 |
|
| 1009 |
ai = async_info; |
| 1010 |
if (ai == NULL) |
| 1011 |
return; |
| 1012 |
|
| 1013 |
for (i = 0; i < 3;i++) |
| 1014 |
Win_close((int) ai->h[i].hdl, ai); |
| 1015 |
Free_TSD(ai->TSD, ai); |
| 1016 |
} |
| 1017 |
|
| 1018 |
/* reset_async_info clears async_info in such a way that fresh add_waiter() |
| 1019 |
* calls can be performed. |
| 1020 |
*/ |
| 1021 |
static void Win_reset_async_info(void *async_info) |
| 1022 |
{ |
| 1023 |
} |
| 1024 |
|
| 1025 |
/* add_async_waiter adds a further handle to the asyncronous IO structure. |
| 1026 |
* add_as_read_handle must be != 0 if the next operation shall be a |
| 1027 |
* __regina_read, else it must be 0 for __regina_write. |
| 1028 |
* Call reset_async_info before a wait-cycle to different handles and use |
| 1029 |
* wait_async_info to wait for at least one IO-able handle. |
| 1030 |
*/ |
| 1031 |
static void Win_add_async_waiter(void *async_info, int handle, int add_as_read_handle) |
| 1032 |
{ |
| 1033 |
AsyncInfo *ai; |
| 1034 |
HANDLE hdl; |
| 1035 |
unsigned i; |
| 1036 |
|
| 1037 |
ai = async_info; |
| 1038 |
hdl = (HANDLE) handle; |
| 1039 |
if (ai) |
| 1040 |
{ |
| 1041 |
for (i = 0;i < 3;i++) |
| 1042 |
if (ai->h[i].hdl == hdl) |
| 1043 |
{ |
| 1044 |
if (add_as_read_handle) |
| 1045 |
ai->h[i].is_reader = 1; |
| 1046 |
else |
| 1047 |
ai->h[i].is_reader = 0; |
| 1048 |
} |
| 1049 |
} |
| 1050 |
} |
| 1051 |
|
| 1052 |
/* wait_async_info waits for some handles to become ready. This function |
| 1053 |
* returns if at least one handle becomes ready. |
| 1054 |
* A handle can be added with add_async_waiter to the bundle of handles to |
| 1055 |
* wait for. |
| 1056 |
* No special handling is implemented if an asyncronous interrupt occurs. |
| 1057 |
* Thus, there is no guarantee to have handle which works. |
| 1058 |
*/ |
| 1059 |
static void Win_wait_async_info(void *async_info) |
| 1060 |
{ |
| 1061 |
AsyncInfo *ai; |
| 1062 |
Win32IO *w; |
| 1063 |
unsigned i, used; |
| 1064 |
HANDLE list[3]; |
| 1065 |
|
| 1066 |
ai = async_info; |
| 1067 |
for (i = 0, used = 0, w = ai->h; i < 3; i++, w++) |
| 1068 |
{ |
| 1069 |
if (w->hdl == INVALID_HANDLE_VALUE) |
| 1070 |
continue; |
| 1071 |
if (w->ol.hEvent == INVALID_HANDLE_VALUE) |
| 1072 |
continue; |
| 1073 |
if (w->is_reader) |
| 1074 |
{ |
| 1075 |
if (w->rused != 0) /* Still data to read? this is a killer! */ |
| 1076 |
return; |
| 1077 |
if (w->reading == 0) |
| 1078 |
continue; |
| 1079 |
} |
| 1080 |
else |
| 1081 |
{ |
| 1082 |
if (w->wused == 0) |
| 1083 |
continue; |
| 1084 |
} |
| 1085 |
if (HasOverlappedIoCompleted(&w->ol)) /* fresh meat arrived? */ |
| 1086 |
return; |
| 1087 |
list[used++] = w->ol.hEvent; |
| 1088 |
} |
| 1089 |
if (used) |
| 1090 |
WaitForMultipleObjects(used, list, FALSE, INFINITE); |
| 1091 |
} |
| 1092 |
|
| 1093 |
|
| 1094 |
#if defined(__WINS__) || defined(__EPOC32__) |
| 1095 |
# define Win_uname epoc32_uname |
| 1096 |
#elif defined(NEED_UNAME) |
| 1097 |
int Win_uname(struct regina_utsname *name) /* MH 10-06-96 */ |
| 1098 |
{ /* MH 10-06-96 */ |
| 1099 |
SYSTEM_INFO sysinfo; |
| 1100 |
OSVERSIONINFOEX osinfo; |
| 1101 |
char computername[MAX_COMPUTERNAME_LENGTH+1]; |
| 1102 |
DWORD namelen = sizeof(computername); |
| 1103 |
char buf[128]; |
| 1104 |
|
| 1105 |
ZeroMemory( &sysinfo, sizeof(SYSTEM_INFO) ); |
| 1106 |
ZeroMemory( &osinfo, sizeof(OSVERSIONINFOEX) ); |
| 1107 |
GetSystemInfo( &sysinfo ); |
| 1108 |
osinfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); |
| 1109 |
GetVersionEx( &osinfo ); |
| 1110 |
|
| 1111 |
sprintf( buf, "%u.%u", osinfo.dwMajorVersion, osinfo.dwMinorVersion ); |
| 1112 |
if (osinfo.wServicePackMajor) |
| 1113 |
sprintf( buf + strlen(buf), "SP%u", ( unsigned ) osinfo.wServicePackMajor ); |
| 1114 |
strncpy( name->version, buf, sizeof(name->version) - 1 ); |
| 1115 |
sprintf( name->release, "%u", osinfo.dwBuildNumber ); |
| 1116 |
|
| 1117 |
/* get specific OS version... */ |
| 1118 |
switch( osinfo.dwPlatformId ) |
| 1119 |
{ |
| 1120 |
case VER_PLATFORM_WIN32s: |
| 1121 |
strcpy( name->sysname, "WIN32S" ); |
| 1122 |
break; |
| 1123 |
case VER_PLATFORM_WIN32_WINDOWS: |
| 1124 |
if ( osinfo.dwMinorVersion >= 90 ) |
| 1125 |
strcpy( name->sysname, "WINME" ); |
| 1126 |
else if ( osinfo.dwMinorVersion >= 10 ) |
| 1127 |
strcpy( name->sysname, "WIN98" ); |
| 1128 |
else |
| 1129 |
strcpy( name->sysname, "WIN95" ); |
| 1130 |
break; |
| 1131 |
case VER_PLATFORM_WIN32_NT: |
| 1132 |
if ( osinfo.dwMajorVersion == 4 ) |
| 1133 |
strcpy( name->sysname, "WINNT" ); |
| 1134 |
else if ( osinfo.dwMajorVersion == 5 ) |
| 1135 |
{ |
| 1136 |
if ( osinfo.dwMinorVersion == 1 ) |
| 1137 |
strcpy( name->sysname, "WINXP" ); |
| 1138 |
else if ( osinfo.dwMinorVersion == 2 ) |
| 1139 |
{ |
| 1140 |
if ( osinfo.wProductType == VER_NT_WORKSTATION |
| 1141 |
&& sysinfo.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64 ) |
| 1142 |
strcpy( name->sysname, "WINXP64" ); |
| 1143 |
else |
| 1144 |
strcpy( name->sysname, "WIN2003" ); |
| 1145 |
} |
| 1146 |
else |
| 1147 |
strcpy( name->sysname, "WIN2K" ); |
| 1148 |
} |
| 1149 |
else if ( osinfo.dwMajorVersion == 6 ) |
| 1150 |
{ |
| 1151 |
if ( osinfo.dwMinorVersion == 0 ) |
| 1152 |
{ |
| 1153 |
if ( osinfo.wProductType == VER_NT_WORKSTATION ) |
| 1154 |
strcpy( name->sysname, "WINVISTA" ); |
| 1155 |
else |
| 1156 |
strcpy( name->sysname, "WIN2008" ); |
| 1157 |
} |
| 1158 |
else if ( osinfo.dwMinorVersion == 1 ) |
| 1159 |
{ |
| 1160 |
if ( osinfo.wProductType == VER_NT_WORKSTATION |
| 1161 |
|| osinfo.wProductType == 0 ) |
| 1162 |
strcpy( name->sysname, "WIN7" ); |
| 1163 |
else |
| 1164 |
strcpy( name->sysname, "WIN2008R2" ); |
| 1165 |
} |
| 1166 |
else if ( osinfo.dwMinorVersion == 2 ) |
| 1167 |
{ |
| 1168 |
if ( osinfo.wProductType == VER_NT_WORKSTATION |
| 1169 |
|| osinfo.wProductType == 0 ) |
| 1170 |
strcpy( name->sysname, "WIN8" ); |
| 1171 |
else |
| 1172 |
strcpy( name->sysname, "WIN2012" ); |
| 1173 |
} |
| 1174 |
/* |
| 1175 |
* The following tests for Windows 8.1 won't work as GetVersionEx() has been |
| 1176 |
* deprecated in Windows 8.1 and unless you completley change the way that |
| 1177 |
* version information is determined for Windows, then Windows 8.1 and above will |
| 1178 |
* always return WIN8. I'm not going to waste my time to |
| 1179 |
* change Regina because of the stupid morons who work for Microsoft!! |
| 1180 |
*/ |
| 1181 |
else if ( osinfo.dwMinorVersion == 3 ) |
| 1182 |
{ |
| 1183 |
if ( osinfo.wProductType == VER_NT_WORKSTATION |
| 1184 |
|| osinfo.wProductType == 0 ) |
| 1185 |
strcpy( name->sysname, "WIN8.1" ); |
| 1186 |
else |
| 1187 |
strcpy( name->sysname, "WIN2012R2" ); |
| 1188 |
} |
| 1189 |
else |
| 1190 |
strcpy( name->sysname, "UNKNOWN" ); |
| 1191 |
} |
| 1192 |
else if ( osinfo.dwMajorVersion == 10 ) |
| 1193 |
{ |
| 1194 |
strcpy( name->sysname, "WIN10" ); |
| 1195 |
} |
| 1196 |
else if ( osinfo.dwMajorVersion == 11 ) |
| 1197 |
{ |
| 1198 |
strcpy( name->sysname, "WIN11" ); |
| 1199 |
} |
| 1200 |
else |
| 1201 |
strcpy( name->sysname, "UNKNOWN" ); |
| 1202 |
break; |
| 1203 |
default: |
| 1204 |
strcpy( name->sysname, "UNKNOWN" ); |
| 1205 |
break; |
| 1206 |
} |
| 1207 |
/* |
| 1208 |
* get name of computer if possible. |
| 1209 |
*/ |
| 1210 |
if ( GetComputerName(computername, &namelen) ) |
| 1211 |
strcpy( name->nodename, computername ); |
| 1212 |
else |
| 1213 |
strcpy( name->nodename, "UNKNOWN" ); |
| 1214 |
|
| 1215 |
if ( sysinfo.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_INTEL ) |
| 1216 |
{ |
| 1217 |
switch( sysinfo.dwProcessorType ) |
| 1218 |
{ |
| 1219 |
case PROCESSOR_INTEL_386: |
| 1220 |
strcpy( name->machine, "i386" ); |
| 1221 |
break; |
| 1222 |
case PROCESSOR_INTEL_486: |
| 1223 |
strcpy( name->machine, "i486" ); |
| 1224 |
break; |
| 1225 |
case PROCESSOR_INTEL_PENTIUM: |
| 1226 |
strcpy( name->machine, "i586" ); |
| 1227 |
break; |
| 1228 |
#if defined(PROCESSOR_INTEL_MIPS_R4000) |
| 1229 |
case PROCESSOR_INTEL_MIPS_R4000: |
| 1230 |
strcpy( name->machine, "mipsR4000" ); |
| 1231 |
break; |
| 1232 |
#endif |
| 1233 |
default: |
| 1234 |
strcpy( name->machine, "UNKNOWN" ); |
| 1235 |
break; |
| 1236 |
} |
| 1237 |
} |
| 1238 |
else if ( sysinfo.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_IA64 ) |
| 1239 |
{ |
| 1240 |
strcpy( name->machine, "alpha21064" ); |
| 1241 |
} |
| 1242 |
else if ( sysinfo.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64 ) |
| 1243 |
{ |
| 1244 |
strcpy( name->machine, "x86_64" ); |
| 1245 |
} |
| 1246 |
else |
| 1247 |
{ |
| 1248 |
strcpy( name->machine, "UNKNOWN" ); |
| 1249 |
} |
| 1250 |
|
| 1251 |
return 0; |
| 1252 |
} |
| 1253 |
#else |
| 1254 |
int Win_uname(struct regina_utsname *name) |
| 1255 |
{ |
| 1256 |
struct utsname osdata; |
| 1257 |
|
| 1258 |
/* |
| 1259 |
* Don't know whether utsname uses pointer or char[]. |
| 1260 |
* So just copy data. |
| 1261 |
*/ |
| 1262 |
if ( uname( &osdata ) < 0 ) |
| 1263 |
{ |
| 1264 |
memset( name, 0, sizeof(struct regina_utsname) ); |
| 1265 |
return -1; |
| 1266 |
} |
| 1267 |
|
| 1268 |
strcpy( name->sysname, osdata.sysname ); |
| 1269 |
strcpy( name->nodename, osdata.nodename ); |
| 1270 |
strcpy( name->release, osdata.release ); |
| 1271 |
strcpy( name->version, osdata.version ); |
| 1272 |
strcpy( name->machine, osdata.machine ); |
| 1273 |
return 0; |
| 1274 |
} |
| 1275 |
#endif |
| 1276 |
|
| 1277 |
static void Win_init(void); |
| 1278 |
|
| 1279 |
OS_Dep_funcs __regina_OS_Win = |
| 1280 |
{ |
| 1281 |
Win_init, /* init */ |
| 1282 |
Win_setenv, /* setenv */ |
| 1283 |
Win_fork_exec, /* fork_exec */ |
| 1284 |
Win_wait, /* wait */ |
| 1285 |
Win_open_subprocess_connection, /* open_subprocess_connection */ |
| 1286 |
Win_unblock_handle, /* unblock_handle */ |
| 1287 |
Win_restart_file, /* restart_file */ |
| 1288 |
Win_close, /* close */ |
| 1289 |
Win_close_special, /* close_special */ |
| 1290 |
Win_read, /* read */ |
| 1291 |
Win_write, /* write */ |
| 1292 |
Win_create_async_info, /* create_async_info */ |
| 1293 |
Win_delete_async_info, /* delete_async_info */ |
| 1294 |
Win_reset_async_info, /* reset_async_info */ |
| 1295 |
Win_add_async_waiter, /* add_async_waiter */ |
| 1296 |
Win_wait_async_info, /* wait_async_info */ |
| 1297 |
Win_uname /* uname */ |
| 1298 |
}; |
| 1299 |
|
| 1300 |
extern OS_Dep_funcs __regina_OS_Other; |
| 1301 |
|
| 1302 |
static void Win_init(void) |
| 1303 |
{ |
| 1304 |
#define WIN __regina_OS_Win |
| 1305 |
#define DOS __regina_OS_Other |
| 1306 |
if (IsWin9X()) |
| 1307 |
{ |
| 1308 |
WIN.fork_exec = DOS.fork_exec; |
| 1309 |
WIN.open_subprocess_connection = DOS.open_subprocess_connection; |
| 1310 |
WIN.unblock_handle = DOS.unblock_handle; |
| 1311 |
WIN.restart_file = DOS.restart_file; |
| 1312 |
WIN.close_exec = DOS.close_exec; |
| 1313 |
WIN.read_exec = DOS.read_exec; |
| 1314 |
WIN.write_exec = DOS.write_exec; |
| 1315 |
WIN.create_async_info = DOS.create_async_info; |
| 1316 |
WIN.delete_async_info = DOS.delete_async_info; |
| 1317 |
WIN.reset_async_info = DOS.reset_async_info; |
| 1318 |
WIN.add_async_waiter = DOS.add_async_waiter; |
| 1319 |
WIN.wait_async_info = DOS.wait_async_info; |
| 1320 |
WIN.wait_exec = DOS.wait_exec; |
| 1321 |
} |
| 1322 |
} |