forked from parrot/parrot
/
win32.c
702 lines (528 loc) · 15.9 KB
/
win32.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
/*
Copyright (C) 2001-2009, Parrot Foundation.
=head1 NAME
src/io/win32.c - Win32 I/O utility functions
=head1 DESCRIPTION
This file implements OS-specific I/O functions for Win32 platforms.
=head2 References
Win32 System Programming, 2nd Edition.
=head2 Functions
=over 4
=cut
*/
#ifdef WIN32
# include <windows.h>
#endif
#include "parrot/parrot.h"
#include "pmc/pmc_filehandle.h"
#include "io_private.h"
#ifdef PIO_OS_WIN32
/* HEADERIZER HFILE: include/parrot/io_win32.h */
/* HEADERIZER BEGIN: static */
/* Don't modify between HEADERIZER BEGIN / HEADERIZER END. Your changes will be lost. */
static void convert_flags_to_win32(
INTVAL flags,
ARGOUT(DWORD * fdwAccess),
ARGOUT(DWORD * fdwShareMode),
ARGOUT(DWORD * fdwCreate))
__attribute__nonnull__(2)
__attribute__nonnull__(3)
__attribute__nonnull__(4)
FUNC_MODIFIES(* fdwAccess)
FUNC_MODIFIES(* fdwShareMode)
FUNC_MODIFIES(* fdwCreate);
#define ASSERT_ARGS_convert_flags_to_win32 __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(fdwAccess) \
, PARROT_ASSERT_ARG(fdwShareMode) \
, PARROT_ASSERT_ARG(fdwCreate))
/* Don't modify between HEADERIZER BEGIN / HEADERIZER END. Your changes will be lost. */
/* HEADERIZER END: static */
# include <tchar.h>
# define PIO_TRACE 0
/*
=item C<static void convert_flags_to_win32(INTVAL flags, DWORD * fdwAccess,
DWORD * fdwShareMode, DWORD * fdwCreate)>
Convert to platform-specific bit open flags.
=cut
*/
static void
convert_flags_to_win32(INTVAL flags, ARGOUT(DWORD * fdwAccess),
ARGOUT(DWORD * fdwShareMode), ARGOUT(DWORD * fdwCreate))
{
ASSERT_ARGS(convert_flags_to_win32)
static DWORD dwDefaultShareMode;
if (!dwDefaultShareMode) {
OSVERSIONINFO osvi;
osvi.dwOSVersionInfoSize = sizeof (osvi);
GetVersionEx(&osvi);
if (osvi.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) {
dwDefaultShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE;
}
else {
dwDefaultShareMode =
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
}
}
if ((flags & (PIO_F_WRITE | PIO_F_READ)) == (PIO_F_WRITE | PIO_F_READ)) {
*fdwAccess = GENERIC_WRITE | GENERIC_READ;
if (flags & PIO_F_TRUNC)
*fdwCreate = CREATE_ALWAYS;
else
*fdwCreate = OPEN_ALWAYS;
}
else if (flags & PIO_F_WRITE) {
*fdwAccess = GENERIC_WRITE;
if (flags & PIO_F_TRUNC)
*fdwCreate = CREATE_ALWAYS;
else
*fdwCreate = OPEN_ALWAYS;
}
else if (flags & PIO_F_READ) {
*fdwAccess = GENERIC_READ;
*fdwCreate = OPEN_EXISTING;
}
*fdwShareMode = dwDefaultShareMode;
}
/*
=item C<INTVAL Parrot_io_init_win32(PARROT_INTERP)>
Sets up the standard C<std*> IO handles.
=cut
*/
INTVAL
Parrot_io_init_win32(PARROT_INTERP)
{
ASSERT_ARGS(Parrot_io_init_win32)
struct WSAData sockinfo;
int ret;
/* Start Winsock
* no idea where or whether destroy it
*/
ret = WSAStartup(2, &sockinfo);
if (ret != 0) {
fprintf(stderr, "WSAStartup failed!!\n ErrorCode=%i\n\n",
WSAGetLastError());
return -4;
}
return 0;
}
/*
=item C<PIOHANDLE Parrot_io_stdhandle_win32(PARROT_INTERP, INTVAL fileno)>
Returns a standard file handle.
=cut
*/
PIOHANDLE
Parrot_io_stdhandle_win32(PARROT_INTERP, INTVAL fileno)
{
ASSERT_ARGS(Parrot_io_stdhandle_win32)
DWORD nStdHandle;
switch (fileno) {
case PIO_STDIN_FILENO:
default:
nStdHandle = STD_INPUT_HANDLE;
break;
case PIO_STDOUT_FILENO:
nStdHandle = STD_OUTPUT_HANDLE;
break;
case PIO_STDERR_FILENO:
nStdHandle = STD_ERROR_HANDLE;
break;
}
return GetStdHandle(nStdHandle);
}
/*
=item C<INTVAL Parrot_io_getblksize_win32(PIOHANDLE fd)>
Returns C<PIO_BLKSIZE>.
=cut
*/
INTVAL
Parrot_io_getblksize_win32(SHIM(PIOHANDLE fd))
{
ASSERT_ARGS(Parrot_io_getblksize_win32)
/* Hard coded for now */
return PIO_BLKSIZE;
}
/*
=item C<PIOHANDLE Parrot_io_open_win32(PARROT_INTERP, STRING *path, INTVAL
flags)>
Calls C<CreateFile()> to open C<*spath> with the Win32 translation of
C<flags>.
=cut
*/
PARROT_CAN_RETURN_NULL
PIOHANDLE
Parrot_io_open_win32(PARROT_INTERP, ARGIN(STRING *path), INTVAL flags)
{
ASSERT_ARGS(Parrot_io_open_win32)
DWORD fAcc, fShare, fCreat;
PIOHANDLE fd;
char *spath;
# if 0
if ((Interp_flags_TEST(interp, PARROT_DEBUG_FLAG)) != 0) {
fprintf(stderr, "Parrot_io_open_win32: %s\n", spath);
}
# endif
/* Set open flags - <, >, >>, +<, +> */
/* add ? and ! for block/non-block */
convert_flags_to_win32(flags, &fAcc, &fShare, &fCreat);
if (path->encoding == Parrot_ascii_encoding_ptr) {
spath = Parrot_str_to_cstring(interp, path);
fd = CreateFile(spath, fAcc, fShare, NULL, fCreat,
FILE_ATTRIBUTE_NORMAL, NULL);
}
else {
if (path->encoding != Parrot_ucs2_encoding_ptr
&& path->encoding != Parrot_utf16_encoding_ptr)
path = Parrot_utf16_encoding_ptr->to_encoding(interp, path);
spath = Parrot_str_to_cstring(interp, path);
fd = CreateFileW((LPCWSTR)spath, fAcc, fShare, NULL, fCreat,
FILE_ATTRIBUTE_NORMAL, NULL);
}
Parrot_str_free_cstring(spath);
return fd;
}
/*
=item C<PIOHANDLE Parrot_io_dup_win32(PARROT_INTERP, PIOHANDLE handle)>
Duplicates file handle C<handle>.
=cut
*/
PARROT_WARN_UNUSED_RESULT
PIOHANDLE
Parrot_io_dup_win32(PARROT_INTERP, PIOHANDLE handle)
{
ASSERT_ARGS(Parrot_io_dup_win32)
HANDLE current_process = GetCurrentProcess();
HANDLE new_handle = INVALID_HANDLE_VALUE;
DuplicateHandle(current_process,
(HANDLE)handle,
current_process,
&new_handle,
0,
FALSE,
DUPLICATE_SAME_ACCESS);
return new_handle;
}
/*
=item C<INTVAL Parrot_io_close_piohandle_win32(PARROT_INTERP, PIOHANDLE handle)>
Calls C<CloseHandle()> to close the given file descriptor. Returns 0 on
success, -1 on error.
=cut
*/
INTVAL
Parrot_io_close_piohandle_win32(PARROT_INTERP, PIOHANDLE handle)
{
ASSERT_ARGS(Parrot_io_close_piohandle_win32)
if (handle == INVALID_HANDLE_VALUE)
return -1;
return CloseHandle(handle) ? 0 : -1;
}
/*
=item C<INTVAL Parrot_io_close_win32(PARROT_INTERP, PIOHANDLE os_handle)>
Calls C<CloseHandle()> to close C<*io>'s file descriptor.
=cut
*/
INTVAL
Parrot_io_close_win32(PARROT_INTERP, PIOHANDLE os_handle)
{
ASSERT_ARGS(Parrot_io_close_win32)
UINTVAL result = 0;
if (os_handle != INVALID_HANDLE_VALUE) {
if (CloseHandle(os_handle) == 0)
result = GetLastError();
}
return (result != 0);
}
/*
=item C<INTVAL Parrot_io_pipe_wait_win32(PARROT_INTERP, INTVAL procid)>
Calls C<CloseHandle()> to close C<*io>'s file descriptor.
=cut
*/
INTVAL
Parrot_io_pipe_wait_win32(PARROT_INTERP, INTVAL procid)
{
ASSERT_ARGS(Parrot_io_pipe_wait_win32)
HANDLE process = (HANDLE)procid;
DWORD status = WaitForSingleObject(process, INFINITE);
DWORD exit_code;
if (status == WAIT_FAILED || !GetExitCodeProcess(process, &exit_code))
exit_code = 1;
CloseHandle(process);
return exit_code;
}
/*
=item C<INTVAL Parrot_io_is_tty_win32(PARROT_INTERP, PIOHANDLE fd)>
Returns whether C<fd> is a console/tty.
=cut
*/
PARROT_WARN_UNUSED_RESULT
INTVAL
Parrot_io_is_tty_win32(SHIM_INTERP, PIOHANDLE fd)
{
ASSERT_ARGS(Parrot_io_is_tty_win32)
const DWORD ftype = GetFileType(fd);
return (ftype == FILE_TYPE_CHAR);
}
/*
=item C<INTVAL Parrot_io_flush_win32(PARROT_INTERP, PIOHANDLE os_handle)>
Calls C<FlushFileBuffers()> to flush C<*io>'s file descriptor.
=cut
*/
INTVAL
Parrot_io_flush_win32(PARROT_INTERP, PIOHANDLE os_handle)
{
ASSERT_ARGS(Parrot_io_flush_win32)
/*
* FlushFileBuffers won't work for console handles. From the MS help file:
*
* Windows NT: The function fails if hFile is a handle to console
* output. That is because console output is not buffered. The
* function returns FALSE, and GetLastError returns
* ERROR_INVALID_HANDLE.
*
* Windows 9x: The function does nothing if hFile is a handle to
* console output. That is because console output is not buffered.
* The function returns TRUE, but it does nothing.
*/
return FlushFileBuffers(os_handle);
}
/*
=item C<size_t Parrot_io_read_win32(PARROT_INTERP, PIOHANDLE os_handle, char
*buf, size_t len)>
Calls C<ReadFile()> to read up to C<len> bytes from C<*io>'s file
descriptor to the memory starting at C<buffer>.
=cut
*/
size_t
Parrot_io_read_win32(PARROT_INTERP, PIOHANDLE os_handle,
ARGMOD(char *buf), size_t len)
{
ASSERT_ARGS(Parrot_io_read_win32)
DWORD countread;
BOOL success = ReadFile(os_handle, (LPVOID)buf, (DWORD)len,
&countread, NULL);
if (!success) {
DWORD err = GetLastError();
if (err != ERROR_BROKEN_PIPE)
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_PIO_ERROR,
"Read error: %d", err);
}
return countread;
}
/*
=item C<size_t Parrot_io_write_win32(PARROT_INTERP, PIOHANDLE os_handle, const
char *buf, size_t len)>
Calls C<WriteFile()> to write C<len> bytes from the memory starting at
C<buffer> to C<*io>'s file descriptor. Returns C<(size_t)-1> on
failure.
=cut
*/
size_t
Parrot_io_write_win32(PARROT_INTERP, PIOHANDLE os_handle,
ARGIN(const char *buf), size_t len)
{
ASSERT_ARGS(Parrot_io_write_win32)
DWORD countwrote = 0;
DWORD err;
if (WriteFile(os_handle, (LPCSTR)buf, len, &countwrote, NULL))
return countwrote;
/* Write may have failed because of small buffers,
* see TT #710 for example.
* Let's try writing in small chunks */
if ((err = GetLastError()) == ERROR_NOT_ENOUGH_MEMORY || err == ERROR_INVALID_USER_BUFFER) {
DWORD chunk = 4096; /* Arbitrarily choosen value */
DWORD written = 0;
if (len < chunk)
goto fail;
while (len > 0) {
if (chunk > len)
chunk = len;
if (WriteFile(os_handle, (LPCSTR) buf, chunk, &countwrote, NULL) == 0)
goto fail;
buf += countwrote;
len -= countwrote;
written += countwrote;
if (countwrote < chunk)
break;
}
return written;
}
fail:
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_PIO_ERROR,
"Write error: %d", GetLastError());
}
/*
=item C<PIOOFF_T Parrot_io_seek_win32(PARROT_INTERP, PIOHANDLE os_handle,
PIOOFF_T off, INTVAL whence)>
Hard seek.
Calls C<SetFilePointer()> to move the read/write position of C<*io>'s
file descriptor to C<off> bytes relative to the location specified by
C<whence>.
=cut
*/
PIOOFF_T
Parrot_io_seek_win32(PARROT_INTERP, PIOHANDLE os_handle,
PIOOFF_T off, INTVAL whence)
{
ASSERT_ARGS(Parrot_io_seek_win32)
LARGE_INTEGER offset;
offset.QuadPart = off;
/* offset.HighPart gets overwritten */
offset.LowPart = SetFilePointer(os_handle, offset.LowPart,
&offset.HighPart, whence);
if (offset.LowPart == INVALID_SET_FILE_POINTER
&& GetLastError() != NO_ERROR) {
/* Error - exception */
return -1;
}
return offset.QuadPart;
}
/*
=item C<PIOOFF_T Parrot_io_tell_win32(PARROT_INTERP, PIOHANDLE os_handle)>
Returns the current read/write position of C<*io>'s file descriptor.
=cut
*/
PIOOFF_T
Parrot_io_tell_win32(PARROT_INTERP, PIOHANDLE os_handle)
{
ASSERT_ARGS(Parrot_io_tell_win32)
LARGE_INTEGER p;
p.QuadPart = piooffsetzero;
p.LowPart = SetFilePointer(os_handle, 0, &p.HighPart, FILE_CURRENT);
if (p.LowPart == 0xFFFFFFFF && GetLastError() != NO_ERROR) {
/* FIXME: Error - exception */
}
return p.QuadPart;
}
/*
=item C<PIOHANDLE Parrot_io_open_pipe_win32(PARROT_INTERP, STRING *command,
INTVAL flags, INTVAL *pid)>
Open a pipe. Not implemented for this platform.
=cut
*/
PARROT_WARN_UNUSED_RESULT
PIOHANDLE
Parrot_io_open_pipe_win32(PARROT_INTERP, ARGIN(STRING *command), INTVAL flags,
ARGOUT(INTVAL *pid))
{
ASSERT_ARGS(Parrot_io_open_pipe_win32)
HANDLE current = GetCurrentProcess();
HANDLE hnull = INVALID_HANDLE_VALUE;
HANDLE hread = INVALID_HANDLE_VALUE;
HANDLE hwrite = INVALID_HANDLE_VALUE;
HANDLE hchild = INVALID_HANDLE_VALUE;
STARTUPINFO start;
SECURITY_ATTRIBUTES sec;
PROCESS_INFORMATION procinfo;
char *cmd = NULL;
const char *comspec;
STRING *auxcomm;
PIOHANDLE os_handle;
procinfo.hThread = INVALID_HANDLE_VALUE;
procinfo.hProcess = INVALID_HANDLE_VALUE;
sec.nLength = sizeof sec;
sec.lpSecurityDescriptor = NULL;
sec.bInheritHandle = TRUE;
comspec = getenv("COMSPEC");
if (comspec == NULL)
comspec = "cmd";
auxcomm = Parrot_str_new(interp, comspec, 0);
auxcomm = Parrot_str_concat(interp, auxcomm, Parrot_str_new(interp, " /c ", 0));
auxcomm = Parrot_str_concat(interp, auxcomm, command);
cmd = Parrot_str_to_cstring(interp, auxcomm);
start.cb = sizeof start;
GetStartupInfo(&start);
start.dwFlags = STARTF_USESTDHANDLES;
if (CreatePipe(&hread, &hwrite, NULL, 0) == 0)
goto fail;
if (DuplicateHandle(current, flags & PIO_F_READ ? hwrite : hread,
current, &hchild,
0, TRUE,
DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS)
== 0)
goto fail;
if (hchild == INVALID_HANDLE_VALUE)
goto fail;
if (flags & PIO_F_READ) {
/* Redirect input to NULL. This is to avoid
* interferences in case both the child and
* the parent tries to read from stdin.
* May be unneccessary or even interfere
* with valid usages, need more feedback. */
hnull = CreateFile("NUL", GENERIC_READ|GENERIC_WRITE,
0, &sec, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, NULL);
if (hnull == INVALID_HANDLE_VALUE)
goto fail;
start.hStdInput = hnull;
start.hStdOutput = hchild;
start.hStdError = hchild;
}
else {
start.hStdInput = hchild;
}
if (CreateProcess(NULL,
cmd,
NULL, NULL, TRUE, 0,
NULL, NULL, &start, &procinfo) == 0)
goto fail;
if (flags & PIO_F_READ) {
os_handle = hread;
CloseHandle(hwrite);
}
else {
os_handle = hwrite;
CloseHandle(hread);
}
Parrot_str_free_cstring(cmd);
CloseHandle(procinfo.hThread);
*pid = (INTVAL)procinfo.hProcess;
return os_handle;
fail:
if (cmd != NULL)
Parrot_str_free_cstring(cmd);
if (hnull != INVALID_HANDLE_VALUE)
CloseHandle(hnull);
if (hread != INVALID_HANDLE_VALUE)
CloseHandle(hread);
if (hwrite != INVALID_HANDLE_VALUE)
CloseHandle(hwrite);
if (hchild != INVALID_HANDLE_VALUE)
CloseHandle(hchild);
if (procinfo.hThread != INVALID_HANDLE_VALUE)
CloseHandle(procinfo.hThread);
if (procinfo.hProcess != INVALID_HANDLE_VALUE)
CloseHandle(procinfo.hProcess);
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_UNIMPLEMENTED,
"pipe open error");
}
/*
=item C<INTVAL Parrot_io_pipe_win32(PARROT_INTERP, PIOHANDLE *reader, PIOHANDLE
*writer)>
Uses CreatePipe() to create a matched pair of pipe handles. Returns 0 on
success, -1 on failure.
=cut
*/
PARROT_WARN_UNUSED_RESULT
PARROT_CAN_RETURN_NULL
INTVAL
Parrot_io_pipe_win32(SHIM_INTERP, ARGMOD(PIOHANDLE *reader), ARGMOD(PIOHANDLE *writer))
{
ASSERT_ARGS(Parrot_io_pipe_win32)
return CreatePipe(reader, writer, NULL, 0) ? 0 : -1;
}
#endif /* PIO_OS_WIN32 */
/*
=back
=head1 SEE ALSO
F<src/io/unix.c>,
F<src/io/stdio.c>,
F<src/io/io.c>,
F<src/io/io_private.h>.
F<include/parrot/io_win32.h>.
=cut
*/
/*
* Local variables:
* c-file-style: "parrot"
* End:
* vim: expandtab shiftwidth=4 cinoptions='\:2=2' :
*/