2 # Created by Ian Hickson to make exec() call if IO::Pipe more secure.
3 # Distributed under exactly the same licence terms as IO::Pipe.
5 package IO::SecurePipe;
12 my $do_spawn = $^O eq 'os2';
15 $0 =~ m/^(.*)$/os; # untaint $0 so that we can call it below:
16 exec { $1 } ($1, '--abort'); # do not call shutdown handlers
17 exit(); # exit (implicit in exec() actually)
24 my $pid = $do_spawn ? 0 : fork();
29 elsif(defined $pid) { # Child or spawn
31 my $io = $rw ? \*STDIN : \*STDOUT;
32 my ($mode, $save) = $rw ? "r" : "w";
35 $save = IO::Handle->new_from_fd($io, $mode);
37 fcntl(shift, Fcntl::F_SETFD(), 1) or croak "fcntl: $!";
38 $fh = $rw ? ${*$me}[0] : ${*$me}[1];
41 $fh = $rw ? $me->reader() : $me->writer(); # close the other end
43 bless $io, "IO::Handle";
44 $io->fdopen($fh, $mode);
48 $pid = eval { system 1, @_ }; # 1 == P_NOWAIT
51 $io->fdopen($save, $mode);
52 $save->close or croak "Cannot close $!";
53 croak "IO::Pipe: Cannot spawn-NOWAIT: $err" if not $pid or $pid < 0;
56 exec { $_[0] } @_ or # XXX change here
57 croak "IO::Pipe: Cannot exec: $!";
61 croak "IO::Pipe: Cannot fork: $!";