AnyEvent
view release on metacpan or search on metacpan
lib/AnyEvent/Util.pm view on Meta::CPAN
Example: close all fds except 0, 1, 2.
close_all_fds_except 0, 2, 1;
=cut
sub close_all_fds_except {
my %except; @except{@_} = ();
require POSIX unless $POSIX::VERSION;
# some OSes have a usable /dev/fd, sadly, very few
if ($^O =~ /(freebsd|cygwin|linux)/) {
# netbsd, openbsd, solaris have a broken /dev/fd
my $dir;
if (opendir $dir, "/dev/fd" or opendir $dir, "/proc/self/fd") {
my @fds = sort { $a <=> $b } grep /^\d+$/, readdir $dir;
# broken OS's have device nodes for 0..63 usually, solaris 0..255
if (@fds < 20 or "@fds" ne join " ", 0..$#fds) {
# assume the fds array is valid now
exists $except{$_} or POSIX::close ($_)
for @fds;
return;
}
}
}
my $fd_max = eval { POSIX::sysconf (POSIX::_SC_OPEN_MAX ()) - 1 } || 1023;
exists $except{$_} or POSIX::close ($_)
for 0..$fd_max;
}
=item $cv = run_cmd $cmd, key => value...
Run a given external command, potentially redirecting file descriptors and
return a condition variable that gets sent the exit status (like C<$?>)
when the program exits I<and> all redirected file descriptors have been
exhausted.
The C<$cmd> is either a single string, which is then passed to a shell, or
an arrayref, which is passed to the C<execvp> function (the first array
element is used both for the executable name and argv[0]).
The key-value pairs can be:
=over 4
=item ">" => $filename
Redirects program standard output into the specified filename, similar to C<<
>filename >> in the shell.
=item ">" => \$data
Appends program standard output to the referenced scalar. The condvar will
not be signalled before EOF or an error is signalled.
Specifying the same scalar in multiple ">" pairs is allowed, e.g. to
redirect both stdout and stderr into the same scalar:
">" => \$output,
"2>" => \$output,
=item ">" => $filehandle
Redirects program standard output to the given filehandle (or actually its
underlying file descriptor).
=item ">" => $callback->($data)
Calls the given callback each time standard output receives some data,
passing it the data received. On EOF or error, the callback will be
invoked once without any arguments.
The condvar will not be signalled before EOF or an error is signalled.
=item "fd>" => $see_above
Like ">", but redirects the specified fd number instead.
=item "<" => $see_above
The same, but redirects the program's standard input instead. The same
forms as for ">" are allowed.
In the callback form, the callback is supposed to return data to be
written, or the empty list or C<undef> or a zero-length scalar to signal
EOF.
Similarly, either the write data must be exhausted or an error is to be
signalled before the condvar is signalled, for both string-reference and
callback forms.
=item "fd<" => $see_above
Like "<", but redirects the specified file descriptor instead.
=item on_prepare => $cb
Specify a callback that is executed just before the command is C<exec>'ed,
in the child process. Be careful not to use any event handling or other
services not available in the child.
This can be useful to set up the environment in special ways, such as
changing the priority of the command or manipulating signal handlers (e.g.
setting C<SIGINT> to C<IGNORE>).
=item close_all => $boolean
When C<close_all> is enabled (default is disabled), then all extra file
descriptors will be closed, except the ones that were redirected and C<0>,
C<1> and C<2>.
See C<close_all_fds_except> for more details.
=item '$$' => \$pid
A reference to a scalar which will receive the PID of the newly-created
subprocess after C<run_cmd> returns.
( run in 0.432 second using v1.01-cache-2.11-cpan-39bf76dae61 )