forked from Perl/perl5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
doio.c
3501 lines (3211 loc) · 107 KB
/
doio.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
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* doio.c
*
* Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
* 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 by Larry Wall and others
*
* You may distribute under the terms of either the GNU General Public
* License or the Artistic License, as specified in the README file.
*
*/
/*
* Far below them they saw the white waters pour into a foaming bowl, and
* then swirl darkly about a deep oval basin in the rocks, until they found
* their way out again through a narrow gate, and flowed away, fuming and
* chattering, into calmer and more level reaches.
*
* [p.684 of _The Lord of the Rings_, IV/vi: "The Forbidden Pool"]
*/
/* This file contains functions that do the actual I/O on behalf of ops.
* For example, pp_print() calls the do_print() function in this file for
* each argument needing printing.
*/
#include "EXTERN.h"
#define PERL_IN_DOIO_C
#include "perl.h"
#if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
#ifndef HAS_SEM
#include <sys/ipc.h>
#endif
#ifdef HAS_MSG
#include <sys/msg.h>
#endif
#ifdef HAS_SHM
#include <sys/shm.h>
# ifndef HAS_SHMAT_PROTOTYPE
extern Shmat_t shmat (int, char *, int);
# endif
#endif
#endif
#ifdef I_UTIME
# if defined(_MSC_VER) || defined(__MINGW32__)
# include <sys/utime.h>
# else
# include <utime.h>
# endif
#endif
#ifdef O_EXCL
# define OPEN_EXCL O_EXCL
#else
# define OPEN_EXCL 0
#endif
#define PERL_MODE_MAX 8
#define PERL_FLAGS_MAX 10
#include <signal.h>
void
Perl_setfd_cloexec(int fd)
{
assert(fd >= 0);
#if defined(HAS_FCNTL) && defined(F_SETFD) && defined(FD_CLOEXEC)
(void) fcntl(fd, F_SETFD, FD_CLOEXEC);
#elif !defined(DEBUGGING)
PERL_UNUSED_ARG(fd);
#endif
}
void
Perl_setfd_inhexec(int fd)
{
assert(fd >= 0);
#if defined(HAS_FCNTL) && defined(F_SETFD) && defined(FD_CLOEXEC)
(void) fcntl(fd, F_SETFD, 0);
#elif !defined(DEBUGGING)
PERL_UNUSED_ARG(fd);
#endif
}
void
Perl_setfd_cloexec_for_nonsysfd(pTHX_ int fd)
{
assert(fd >= 0);
if(fd > PL_maxsysfd)
setfd_cloexec(fd);
}
void
Perl_setfd_inhexec_for_sysfd(pTHX_ int fd)
{
assert(fd >= 0);
if(fd <= PL_maxsysfd)
setfd_inhexec(fd);
}
void
Perl_setfd_cloexec_or_inhexec_by_sysfdness(pTHX_ int fd)
{
assert(fd >= 0);
if(fd <= PL_maxsysfd)
setfd_inhexec(fd);
else
setfd_cloexec(fd);
}
#define DO_GENOPEN_THEN_CLOEXEC(GENOPEN_NORMAL, GENSETFD_CLOEXEC) \
do { \
int res = (GENOPEN_NORMAL); \
if(LIKELY(res != -1)) GENSETFD_CLOEXEC; \
return res; \
} while(0)
#if defined(HAS_FCNTL) && defined(F_SETFD) && defined(FD_CLOEXEC) && \
defined(F_GETFD)
enum { CLOEXEC_EXPERIMENT = 0, CLOEXEC_AT_OPEN, CLOEXEC_AFTER_OPEN };
# define DO_GENOPEN_EXPERIMENTING_CLOEXEC(strategy, TESTFD, GENOPEN_CLOEXEC, \
GENOPEN_NORMAL, GENSETFD_CLOEXEC) \
do { \
switch (strategy) { \
case CLOEXEC_EXPERIMENT: default: { \
int res = (GENOPEN_CLOEXEC), eno; \
if (LIKELY(res != -1)) { \
int fdflags = fcntl((TESTFD), F_GETFD); \
if (LIKELY(fdflags != -1) && \
LIKELY(fdflags & FD_CLOEXEC)) { \
strategy = CLOEXEC_AT_OPEN; \
} else { \
strategy = CLOEXEC_AFTER_OPEN; \
GENSETFD_CLOEXEC; \
} \
} else if (UNLIKELY((eno = errno) == EINVAL || \
eno == ENOSYS)) { \
res = (GENOPEN_NORMAL); \
if (LIKELY(res != -1)) { \
strategy = CLOEXEC_AFTER_OPEN; \
GENSETFD_CLOEXEC; \
} else if (!LIKELY((eno = errno) == EINVAL || \
eno == ENOSYS)) { \
strategy = CLOEXEC_AFTER_OPEN; \
} \
} \
return res; \
} \
case CLOEXEC_AT_OPEN: \
return (GENOPEN_CLOEXEC); \
case CLOEXEC_AFTER_OPEN: \
DO_GENOPEN_THEN_CLOEXEC(GENOPEN_NORMAL, GENSETFD_CLOEXEC); \
} \
} while(0)
#else
# define DO_GENOPEN_EXPERIMENTING_CLOEXEC(strategy, TESTFD, GENOPEN_CLOEXEC, \
GENOPEN_NORMAL, GENSETFD_CLOEXEC) \
DO_GENOPEN_THEN_CLOEXEC(GENOPEN_NORMAL, GENSETFD_CLOEXEC)
#endif
#define DO_ONEOPEN_THEN_CLOEXEC(ONEOPEN_NORMAL) \
do { \
int fd; \
DO_GENOPEN_THEN_CLOEXEC(fd = (ONEOPEN_NORMAL), \
setfd_cloexec(fd)); \
} while(0)
#define DO_ONEOPEN_EXPERIMENTING_CLOEXEC(strategy, \
ONEOPEN_CLOEXEC, ONEOPEN_NORMAL) \
do { \
int fd; \
DO_GENOPEN_EXPERIMENTING_CLOEXEC(strategy, \
fd, \
fd = (ONEOPEN_CLOEXEC), \
fd = (ONEOPEN_NORMAL), setfd_cloexec(fd)); \
} while(0)
#define DO_PIPESETFD_CLOEXEC(PIPEFD) \
do { \
setfd_cloexec((PIPEFD)[0]); \
setfd_cloexec((PIPEFD)[1]); \
} while(0)
#define DO_PIPEOPEN_THEN_CLOEXEC(PIPEFD, PIPEOPEN_NORMAL) \
DO_GENOPEN_THEN_CLOEXEC(PIPEOPEN_NORMAL, DO_PIPESETFD_CLOEXEC(PIPEFD))
#define DO_PIPEOPEN_EXPERIMENTING_CLOEXEC(strategy, PIPEFD, PIPEOPEN_CLOEXEC, \
PIPEOPEN_NORMAL) \
DO_GENOPEN_EXPERIMENTING_CLOEXEC(strategy, \
(PIPEFD)[0], PIPEOPEN_CLOEXEC, \
PIPEOPEN_NORMAL, DO_PIPESETFD_CLOEXEC(PIPEFD))
int
Perl_PerlLIO_dup_cloexec(pTHX_ int oldfd)
{
#if !defined(PERL_IMPLICIT_SYS) && defined(F_DUPFD_CLOEXEC)
/*
* struct IPerlLIO doesn't cover fcntl(), and there's no clear way
* to extend it, so for the time being this just isn't available on
* PERL_IMPLICIT_SYS builds.
*/
DO_ONEOPEN_EXPERIMENTING_CLOEXEC(
PL_strategy_dup,
fcntl(oldfd, F_DUPFD_CLOEXEC, 0),
PerlLIO_dup(oldfd));
#else
DO_ONEOPEN_THEN_CLOEXEC(PerlLIO_dup(oldfd));
#endif
}
int
Perl_PerlLIO_dup2_cloexec(pTHX_ int oldfd, int newfd)
{
#if !defined(PERL_IMPLICIT_SYS) && defined(HAS_DUP3) && defined(O_CLOEXEC)
/*
* struct IPerlLIO doesn't cover dup3(), and there's no clear way
* to extend it, so for the time being this just isn't available on
* PERL_IMPLICIT_SYS builds.
*/
DO_ONEOPEN_EXPERIMENTING_CLOEXEC(
PL_strategy_dup2,
dup3(oldfd, newfd, O_CLOEXEC),
PerlLIO_dup2(oldfd, newfd));
#else
DO_ONEOPEN_THEN_CLOEXEC(PerlLIO_dup2(oldfd, newfd));
#endif
}
#if defined(OEMVS)
#if (__CHARSET_LIB == 1)
# include <stdio.h>
# include <stdlib.h>
static int setccsid(int fd, int ccsid)
{
attrib_t attr;
int rc;
memset(&attr, 0, sizeof(attr));
attr.att_filetagchg = 1;
attr.att_filetag.ft_ccsid = ccsid;
attr.att_filetag.ft_txtflag = 1;
rc = __fchattr(fd, &attr, sizeof(attr));
return rc;
}
static void updateccsid(int fd, const char* path, int oflag, int perm)
{
int rc;
if (oflag & O_CREAT) {
rc = setccsid(fd, 819);
}
}
int asciiopen(const char* path, int oflag)
{
int rc;
int fd = open(path, oflag);
if (fd == -1) {
return fd;
}
updateccsid(fd, path, oflag, -1);
return fd;
}
int asciiopen3(const char* path, int oflag, int perm)
{
int rc;
int fd = open(path, oflag, perm);
if (fd == -1) {
return fd;
}
updateccsid(fd, path, oflag, perm);
return fd;
}
#endif
#endif
int
Perl_PerlLIO_open_cloexec(pTHX_ const char *file, int flag)
{
PERL_ARGS_ASSERT_PERLLIO_OPEN_CLOEXEC;
#if defined(O_CLOEXEC)
DO_ONEOPEN_EXPERIMENTING_CLOEXEC(
PL_strategy_open,
PerlLIO_open(file, flag | O_CLOEXEC),
PerlLIO_open(file, flag));
#else
DO_ONEOPEN_THEN_CLOEXEC(PerlLIO_open(file, flag));
#endif
}
int
Perl_PerlLIO_open3_cloexec(pTHX_ const char *file, int flag, int perm)
{
PERL_ARGS_ASSERT_PERLLIO_OPEN3_CLOEXEC;
#if defined(O_CLOEXEC)
DO_ONEOPEN_EXPERIMENTING_CLOEXEC(
PL_strategy_open3,
PerlLIO_open3(file, flag | O_CLOEXEC, perm),
PerlLIO_open3(file, flag, perm));
#else
DO_ONEOPEN_THEN_CLOEXEC(PerlLIO_open3(file, flag, perm));
#endif
}
#if defined(OEMVS)
#if (__CHARSET_LIB == 1)
#define TEMP_CCSID 819
#endif
static int Internal_Perl_my_mkstemp_cloexec(char *templte)
{
PERL_ARGS_ASSERT_MY_MKSTEMP_CLOEXEC;
# if defined(O_CLOEXEC)
DO_ONEOPEN_EXPERIMENTING_CLOEXEC(
PL_strategy_mkstemp,
Perl_my_mkostemp(templte, O_CLOEXEC),
Perl_my_mkstemp(templte));
# else
DO_ONEOPEN_THEN_CLOEXEC(Perl_my_mkstemp(templte));
# endif
}
int
Perl_my_mkstemp_cloexec(char *templte)
{
int tempfd = Internal_Perl_my_mkstemp_cloexec(templte);
# if defined(TEMP_CCSID)
setccsid(tempfd, TEMP_CCSID);
# endif
return tempfd;
}
# else /* Below is ! OEMVS */
int
Perl_my_mkstemp_cloexec(char *templte)
{
PERL_ARGS_ASSERT_MY_MKSTEMP_CLOEXEC;
# if defined(O_CLOEXEC)
DO_ONEOPEN_EXPERIMENTING_CLOEXEC(
PL_strategy_mkstemp,
Perl_my_mkostemp(templte, O_CLOEXEC),
Perl_my_mkstemp(templte));
# else
DO_ONEOPEN_THEN_CLOEXEC(Perl_my_mkstemp(templte));
# endif
}
#endif
int
Perl_my_mkostemp_cloexec(char *templte, int flags)
{
PERL_ARGS_ASSERT_MY_MKOSTEMP_CLOEXEC;
#if defined(O_CLOEXEC)
DO_ONEOPEN_EXPERIMENTING_CLOEXEC(
PL_strategy_mkstemp,
Perl_my_mkostemp(templte, flags | O_CLOEXEC),
Perl_my_mkostemp(templte, flags));
#else
DO_ONEOPEN_THEN_CLOEXEC(Perl_my_mkostemp(templte, flags));
#endif
}
#ifdef HAS_PIPE
int
Perl_PerlProc_pipe_cloexec(pTHX_ int *pipefd)
{
PERL_ARGS_ASSERT_PERLPROC_PIPE_CLOEXEC;
/*
* struct IPerlProc doesn't cover pipe2(), and there's no clear way
* to extend it, so for the time being this just isn't available on
* PERL_IMPLICIT_SYS builds.
*/
# if !defined(PERL_IMPLICIT_SYS) && defined(HAS_PIPE2) && defined(O_CLOEXEC)
DO_PIPEOPEN_EXPERIMENTING_CLOEXEC(PL_strategy_pipe, pipefd,
pipe2(pipefd, O_CLOEXEC),
PerlProc_pipe(pipefd));
# else
DO_PIPEOPEN_THEN_CLOEXEC(pipefd, PerlProc_pipe(pipefd));
# endif
}
#endif
#ifdef HAS_SOCKET
int
Perl_PerlSock_socket_cloexec(pTHX_ int domain, int type, int protocol)
{
# if defined(SOCK_CLOEXEC)
DO_ONEOPEN_EXPERIMENTING_CLOEXEC(
PL_strategy_socket,
PerlSock_socket(domain, type | SOCK_CLOEXEC, protocol),
PerlSock_socket(domain, type, protocol));
# else
DO_ONEOPEN_THEN_CLOEXEC(PerlSock_socket(domain, type, protocol));
# endif
}
int
Perl_PerlSock_accept_cloexec(pTHX_ int listenfd, struct sockaddr *addr,
Sock_size_t *addrlen)
{
# if !defined(PERL_IMPLICIT_SYS) && \
defined(HAS_ACCEPT4) && defined(SOCK_CLOEXEC)
/*
* struct IPerlSock doesn't cover accept4(), and there's no clear
* way to extend it, so for the time being this just isn't available
* on PERL_IMPLICIT_SYS builds.
*/
DO_ONEOPEN_EXPERIMENTING_CLOEXEC(
PL_strategy_accept,
accept4(listenfd, addr, addrlen, SOCK_CLOEXEC),
PerlSock_accept(listenfd, addr, addrlen));
# else
DO_ONEOPEN_THEN_CLOEXEC(PerlSock_accept(listenfd, addr, addrlen));
# endif
}
#endif
#if defined (HAS_SOCKETPAIR) || \
(defined (HAS_SOCKET) && defined(SOCK_DGRAM) && \
defined(AF_INET) && defined(PF_INET))
int
Perl_PerlSock_socketpair_cloexec(pTHX_ int domain, int type, int protocol,
int *pairfd)
{
PERL_ARGS_ASSERT_PERLSOCK_SOCKETPAIR_CLOEXEC;
# ifdef SOCK_CLOEXEC
DO_PIPEOPEN_EXPERIMENTING_CLOEXEC(PL_strategy_socketpair, pairfd,
PerlSock_socketpair(domain, type | SOCK_CLOEXEC, protocol, pairfd),
PerlSock_socketpair(domain, type, protocol, pairfd));
# else
DO_PIPEOPEN_THEN_CLOEXEC(pairfd,
PerlSock_socketpair(domain, type, protocol, pairfd));
# endif
}
#endif
static IO *
S_openn_setup(pTHX_ GV *gv, char *mode, PerlIO **saveifp, PerlIO **saveofp,
int *savefd, char *savetype)
{
IO * const io = GvIOn(gv);
PERL_ARGS_ASSERT_OPENN_SETUP;
*saveifp = NULL;
*saveofp = NULL;
*savefd = -1;
*savetype = IoTYPE_CLOSED;
Zero(mode,sizeof(mode),char);
PL_forkprocess = 1; /* assume true if no fork */
/* If currently open - close before we re-open */
if (IoIFP(io)) {
if (IoTYPE(io) == IoTYPE_STD) {
/* This is a clone of one of STD* handles */
}
else {
const int old_fd = PerlIO_fileno(IoIFP(io));
if (inRANGE(old_fd, 0, PL_maxsysfd)) {
/* This is one of the original STD* handles */
*saveifp = IoIFP(io);
*saveofp = IoOFP(io);
*savetype = IoTYPE(io);
*savefd = old_fd;
}
else {
int result;
if (IoTYPE(io) == IoTYPE_PIPE)
result = PerlProc_pclose(IoIFP(io));
else if (IoIFP(io) != IoOFP(io)) {
if (IoOFP(io)) {
result = PerlIO_close(IoOFP(io));
PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
}
else
result = PerlIO_close(IoIFP(io));
}
else
result = PerlIO_close(IoIFP(io));
if (result == EOF && old_fd > PL_maxsysfd) {
/* Why is this not Perl_warn*() call ? */
PerlIO_printf(Perl_error_log,
"Warning: unable to close filehandle %" HEKf
" properly.\n",
HEKfARG(GvENAME_HEK(gv))
);
}
}
}
IoOFP(io) = IoIFP(io) = NULL;
}
return io;
}
bool
Perl_do_openn(pTHX_ GV *gv, const char *oname, I32 len, int as_raw,
int rawmode, int rawperm, PerlIO *supplied_fp, SV **svp,
I32 num_svs)
{
PERL_ARGS_ASSERT_DO_OPENN;
if (as_raw) {
/* sysopen style args, i.e. integer mode and permissions */
if (num_svs != 0) {
Perl_croak(aTHX_ "panic: sysopen with multiple args, num_svs=%ld",
(long) num_svs);
}
return do_open_raw(gv, oname, len, rawmode, rawperm, NULL);
}
return do_open6(gv, oname, len, supplied_fp, svp, num_svs);
}
bool
Perl_do_open_raw(pTHX_ GV *gv, const char *oname, STRLEN len,
int rawmode, int rawperm, Stat_t *statbufp)
{
PerlIO *saveifp;
PerlIO *saveofp;
int savefd;
char savetype;
char mode[PERL_MODE_MAX]; /* file mode ("r\0", "rb\0", "ab\0" etc.) */
IO * const io = openn_setup(gv, mode, &saveifp, &saveofp, &savefd, &savetype);
int writing = 0;
PerlIO *fp;
PERL_ARGS_ASSERT_DO_OPEN_RAW;
/* For ease of blame back to 5.000, keep the existing indenting. */
{
/* sysopen style args, i.e. integer mode and permissions */
STRLEN ix = 0;
const int appendtrunc =
0
#ifdef O_APPEND /* Not fully portable. */
|O_APPEND
#endif
#ifdef O_TRUNC /* Not fully portable. */
|O_TRUNC
#endif
;
const int modifyingmode = O_WRONLY|O_RDWR|O_CREAT|appendtrunc;
int ismodifying;
SV *namesv;
/* It's not always
O_RDONLY 0
O_WRONLY 1
O_RDWR 2
It might be (in OS/390 and Mac OS Classic it is)
O_WRONLY 1
O_RDONLY 2
O_RDWR 3
This means that simple & with O_RDWR would look
like O_RDONLY is present. Therefore we have to
be more careful.
*/
if ((ismodifying = (rawmode & modifyingmode))) {
if ((ismodifying & O_WRONLY) == O_WRONLY ||
(ismodifying & O_RDWR) == O_RDWR ||
(ismodifying & (O_CREAT|appendtrunc)))
TAINT_PROPER("sysopen");
}
mode[ix++] = IoTYPE_NUMERIC; /* Marker to openn to use numeric "sysopen" */
#if defined(USE_64_BIT_RAWIO) && defined(O_LARGEFILE)
rawmode |= O_LARGEFILE; /* Transparently largefiley. */
#endif
IoTYPE(io) = PerlIO_intmode2str(rawmode, &mode[ix], &writing);
namesv = newSVpvn_flags(oname, len, SVs_TEMP);
fp = PerlIO_openn(aTHX_ NULL, mode, -1, rawmode, rawperm, NULL, 1, &namesv);
}
return openn_cleanup(gv, io, fp, mode, oname, saveifp, saveofp, savefd,
savetype, writing, 0, NULL, statbufp);
}
bool
Perl_do_open6(pTHX_ GV *gv, const char *oname, STRLEN len,
PerlIO *supplied_fp, SV **svp, U32 num_svs)
{
PerlIO *saveifp;
PerlIO *saveofp;
int savefd;
char savetype;
char mode[PERL_MODE_MAX]; /* file mode ("r\0", "rb\0", "ab\0" etc.) */
IO * const io = openn_setup(gv, mode, &saveifp, &saveofp, &savefd, &savetype);
int writing = 0;
PerlIO *fp;
bool was_fdopen = FALSE;
char *type = NULL;
PERL_ARGS_ASSERT_DO_OPEN6;
/* For ease of blame back to 5.000, keep the existing indenting. */
{
/* Regular (non-sys) open */
char *name;
STRLEN olen = len;
char *tend;
int dodup = 0;
bool in_raw = 0, in_crlf = 0, out_raw = 0, out_crlf = 0;
/* Collect default raw/crlf info from the op */
if (PL_op && PL_op->op_type == OP_OPEN) {
/* set up IO layers */
const U8 flags = PL_op->op_private;
in_raw = (flags & OPpOPEN_IN_RAW);
in_crlf = (flags & OPpOPEN_IN_CRLF);
out_raw = (flags & OPpOPEN_OUT_RAW);
out_crlf = (flags & OPpOPEN_OUT_CRLF);
}
type = savepvn(oname, len);
tend = type+len;
SAVEFREEPV(type);
/* Lose leading and trailing white space */
while (isSPACE(*type))
type++;
while (tend > type && isSPACE(tend[-1]))
*--tend = '\0';
if (num_svs) {
const char *p;
STRLEN nlen = 0;
/* New style explicit name, type is just mode and layer info */
#ifdef USE_STDIO
if (SvROK(*svp) && !memchr(oname, '&', len)) {
if (ckWARN(WARN_IO))
Perl_warner(aTHX_ packWARN(WARN_IO),
"Can't open a reference");
SETERRNO(EINVAL, LIB_INVARG);
fp = NULL;
goto say_false;
}
#endif /* USE_STDIO */
p = (SvOK(*svp) || SvGMAGICAL(*svp)) ? SvPV(*svp, nlen) : NULL;
if (p && !IS_SAFE_PATHNAME(p, nlen, "open")) {
fp = NULL;
goto say_false;
}
name = p ? savepvn(p, nlen) : savepvs("");
SAVEFREEPV(name);
}
else {
name = type;
len = tend-type;
}
IoTYPE(io) = *type;
if ((*type == IoTYPE_RDWR) && /* scary */
(*(type+1) == IoTYPE_RDONLY || *(type+1) == IoTYPE_WRONLY) &&
((!num_svs || (tend > type+1 && tend[-1] != IoTYPE_PIPE)))) {
TAINT_PROPER("open");
mode[1] = *type++;
writing = 1;
}
if (*type == IoTYPE_PIPE) {
if (num_svs) {
if (type[1] != IoTYPE_STD) {
unknown_open_mode:
Perl_croak(aTHX_ "Unknown open() mode '%.*s'", (int)olen, oname);
}
type++;
}
do {
type++;
} while (isSPACE(*type));
if (!num_svs) {
name = type;
len = tend-type;
}
if (*name == '\0') {
/* command is missing 19990114 */
if (ckWARN(WARN_PIPE))
Perl_warner(aTHX_ packWARN(WARN_PIPE), "Missing command in piped open");
errno = EPIPE;
fp = NULL;
goto say_false;
}
if (!(*name == '-' && name[1] == '\0') || num_svs)
TAINT_ENV();
TAINT_PROPER("piped open");
if (!num_svs && name[len-1] == '|') {
name[--len] = '\0' ;
if (ckWARN(WARN_PIPE))
Perl_warner(aTHX_ packWARN(WARN_PIPE), "Can't open bidirectional pipe");
}
mode[0] = 'w';
writing = 1;
if (out_raw)
mode[1] = 'b';
else if (out_crlf)
mode[1] = 't';
if (num_svs > 1) {
fp = PerlProc_popen_list(mode, num_svs, svp);
}
else {
fp = PerlProc_popen(name,mode);
}
if (num_svs) {
if (*type) {
if (PerlIO_apply_layers(aTHX_ fp, mode, type) != 0) {
fp = NULL;
goto say_false;
}
}
}
} /* IoTYPE_PIPE */
else if (*type == IoTYPE_WRONLY) {
TAINT_PROPER("open");
type++;
if (*type == IoTYPE_WRONLY) {
/* Two IoTYPE_WRONLYs in a row make for an IoTYPE_APPEND. */
mode[0] = IoTYPE(io) = IoTYPE_APPEND;
type++;
}
else {
mode[0] = 'w';
}
writing = 1;
if (out_raw)
mode[1] = 'b';
else if (out_crlf)
mode[1] = 't';
if (*type == '&') {
duplicity:
dodup = PERLIO_DUP_FD;
type++;
if (*type == '=') {
dodup = 0;
type++;
}
if (!num_svs && !*type && supplied_fp) {
/* "<+&" etc. is used by typemaps */
fp = supplied_fp;
}
else {
PerlIO *that_fp = NULL;
int wanted_fd;
UV uv;
if (num_svs > 1) {
/* diag_listed_as: More than one argument to '%s' open */
Perl_croak(aTHX_ "More than one argument to '%c&' open",IoTYPE(io));
}
while (isSPACE(*type))
type++;
if (num_svs && (
SvIOK(*svp)
|| (SvPOKp(*svp) && looks_like_number(*svp))
)) {
wanted_fd = SvUV(*svp);
num_svs = 0;
}
else if (isDIGIT(*type)
&& grok_atoUV(type, &uv, NULL)
&& uv <= INT_MAX
) {
wanted_fd = (int)uv;
}
else {
const IO* thatio;
if (num_svs) {
thatio = sv_2io(*svp);
}
else {
GV * const thatgv = gv_fetchpvn_flags(type, tend - type,
0, SVt_PVIO);
thatio = GvIO(thatgv);
}
if (!thatio) {
#ifdef EINVAL
SETERRNO(EINVAL,SS_IVCHAN);
#endif
fp = NULL;
goto say_false;
}
if ((that_fp = IoIFP(thatio))) {
/* Flush stdio buffer before dup. --mjd
* Unfortunately SEEK_CURing 0 seems to
* be optimized away on most platforms;
* only Solaris and Linux seem to flush
* on that. --jhi */
/* On the other hand, do all platforms
* take gracefully to flushing a read-only
* filehandle? Perhaps we should do
* fsetpos(src)+fgetpos(dst)? --nik */
PerlIO_flush(that_fp);
wanted_fd = PerlIO_fileno(that_fp);
/* When dup()ing STDIN, STDOUT or STDERR
* explicitly set appropriate access mode */
if (that_fp == PerlIO_stdout()
|| that_fp == PerlIO_stderr())
IoTYPE(io) = IoTYPE_WRONLY;
else if (that_fp == PerlIO_stdin())
IoTYPE(io) = IoTYPE_RDONLY;
/* When dup()ing a socket, say result is
* one as well */
else if (IoTYPE(thatio) == IoTYPE_SOCKET)
IoTYPE(io) = IoTYPE_SOCKET;
}
else {
SETERRNO(EBADF, RMS_IFI);
fp = NULL;
goto say_false;
}
}
if (!num_svs)
type = NULL;
if (that_fp) {
fp = PerlIO_fdupopen(aTHX_ that_fp, NULL, dodup);
}
else {
if (dodup)
wanted_fd = PerlLIO_dup_cloexec(wanted_fd);
else
was_fdopen = TRUE;
if (!(fp = PerlIO_openn(aTHX_ type,mode,wanted_fd,0,0,NULL,num_svs,svp))) {
if (dodup && wanted_fd >= 0)
PerlLIO_close(wanted_fd);
}
}
}
} /* & */
else {
while (isSPACE(*type))
type++;
if (*type == IoTYPE_STD && (!type[1] || isSPACE(type[1]) || type[1] == ':')) {
type++;
fp = PerlIO_stdout();
IoTYPE(io) = IoTYPE_STD;
if (num_svs > 1) {
/* diag_listed_as: More than one argument to '%s' open */
Perl_croak(aTHX_ "More than one argument to '>%c' open",IoTYPE_STD);
}
}
else {
if (num_svs) {
fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp);
}
else {
SV *namesv = newSVpvn_flags(type, tend - type, SVs_TEMP);
type = NULL;
fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,1,&namesv);
}
}
} /* !& */
if (!fp && type && *type && *type != ':' && !isIDFIRST(*type))
goto unknown_open_mode;
} /* IoTYPE_WRONLY */
else if (*type == IoTYPE_RDONLY) {
do {
type++;
} while (isSPACE(*type));
mode[0] = 'r';
if (in_raw)
mode[1] = 'b';
else if (in_crlf)
mode[1] = 't';
if (*type == '&') {
goto duplicity;
}
if (*type == IoTYPE_STD && (!type[1] || isSPACE(type[1]) || type[1] == ':')) {
type++;
fp = PerlIO_stdin();
IoTYPE(io) = IoTYPE_STD;
if (num_svs > 1) {
/* diag_listed_as: More than one argument to '%s' open */
Perl_croak(aTHX_ "More than one argument to '<%c' open",IoTYPE_STD);
}
}
else {
if (num_svs) {
fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp);
}
else {
SV *namesv = newSVpvn_flags(type, tend - type, SVs_TEMP);
type = NULL;
fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,1,&namesv);
}
}
if (!fp && type && *type && *type != ':' && !isIDFIRST(*type))
goto unknown_open_mode;
} /* IoTYPE_RDONLY */
else if ((num_svs && /* '-|...' or '...|' */
type[0] == IoTYPE_STD && type[1] == IoTYPE_PIPE) ||
(!num_svs && tend > type+1 && tend[-1] == IoTYPE_PIPE)) {
if (num_svs) {
type += 2; /* skip over '-|' */
}
else {
*--tend = '\0';
while (tend > type && isSPACE(tend[-1]))
*--tend = '\0';
for (; isSPACE(*type); type++)
;
name = type;
len = tend-type;
}
if (*name == '\0') {
/* command is missing 19990114 */
if (ckWARN(WARN_PIPE))
Perl_warner(aTHX_ packWARN(WARN_PIPE), "Missing command in piped open");
errno = EPIPE;
fp = NULL;
goto say_false;
}
if (!(*name == '-' && name[1] == '\0') || num_svs)
TAINT_ENV();
TAINT_PROPER("piped open");
mode[0] = 'r';
if (in_raw)
mode[1] = 'b';
else if (in_crlf)
mode[1] = 't';
if (num_svs > 1) {
fp = PerlProc_popen_list(mode,num_svs,svp);
}
else {
fp = PerlProc_popen(name,mode);
}
IoTYPE(io) = IoTYPE_PIPE;
if (num_svs) {
while (isSPACE(*type))
type++;
if (*type) {
if (PerlIO_apply_layers(aTHX_ fp, mode, type) != 0) {
fp = NULL;
goto say_false;
}
}
}
}
else { /* layer(Args) */
if (num_svs)
goto unknown_open_mode;
name = type;
IoTYPE(io) = IoTYPE_RDONLY;
for (; isSPACE(*name); name++)
;
mode[0] = 'r';
if (in_raw)
mode[1] = 'b';
else if (in_crlf)
mode[1] = 't';
if (*name == '-' && name[1] == '\0') {
fp = PerlIO_stdin();
IoTYPE(io) = IoTYPE_STD;
}
else {
if (num_svs) {
fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp);
}
else {
SV *namesv = newSVpvn_flags(type, tend - type, SVs_TEMP);
type = NULL;
fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,1,&namesv);
}
}
}
}
say_false:
return openn_cleanup(gv, io, fp, mode, oname, saveifp, saveofp, savefd,
savetype, writing, was_fdopen, type, NULL);
}
/* Yes, this is ugly, but it's private, and I don't see a cleaner way to
simplify the two-headed public interface of do_openn. */
static bool
S_openn_cleanup(pTHX_ GV *gv, IO *io, PerlIO *fp, char *mode, const char *oname,
PerlIO *saveifp, PerlIO *saveofp, int savefd, char savetype,
int writing, bool was_fdopen, const char *type, Stat_t *statbufp)
{
int fd;
Stat_t statbuf;
PERL_ARGS_ASSERT_OPENN_CLEANUP;
Zero(&statbuf, 1, Stat_t);
if (!fp) {
if (IoTYPE(io) == IoTYPE_RDONLY && ckWARN(WARN_NEWLINE)