Proc-SyncExec-1.01/0000755000175000017500000000000010201150620015466 5ustar roderickroderick00000000000000Proc-SyncExec-1.01/SyncExec.pm0000644000175000017500000001742610200663575017600 0ustar roderickroderick00000000000000# $Id: SyncExec.pm,v 1.5 2005/02/04 12:15:57 roderick Exp $ # # Copyright (c) 1997 Roderick Schertler. All rights reserved. This # program is free software; you can redistribute it and/or modify it # under the same terms as Perl itself. =head1 NAME Proc::SyncExec - Spawn processes but report exec() errors =head1 SYNOPSIS # Normal-looking piped opens which properly report exec() errors in $!: sync_open WRITER_FH, "|command -with args" or die $!; sync_open READER_FH, "command -with args|" or die $!; # Synchronized fork/exec which reports exec errors in $!: $pid = sync_exec $command, @arg; $pid = sync_exec $code_ref, $cmd, @arg; # run code after fork in kid # fork() which retries if it fails, then croaks() if it still fails. $pid = fork_retry; $pid = fork_retry 100; # retry 100 times rather than 5 $pid = fork_retry 100, 2; # sleep 2 rather than 5 seconds between # A couple of interfaces similar to sync_open() but which let you # avoid the shell: $pid = sync_fhpopen_noshell READERFH, 'r', @command; $pid = sync_fhpopen_noshell WRITERFH, 'w', @command; $fh = sync_popen_noshell 'r', @command_which_outputs; $fh = sync_popen_noshell 'w', @command_which_inputs; ($fh, $pid) = sync_popen_noshell 'r', @command_which_outputs; ($fh, $pid)= sync_popen_noshell 'w', @command_which_inputs; =head1 DESCRIPTION This module contains functions for synchronized process spawning with full error return. If the child's exec() call fails the reason for the failure is reported back to the parent. These functions will croak() if they encounter an unexpected system error, such as a pipe() failure or a repeated fork() failure. Nothing is exported by default. =over =cut #'; package Proc::SyncExec; use strict; use vars qw($VERSION @ISA @EXPORT_OK); use Carp qw(croak); use Exporter (); use Fcntl qw(F_SETFD); use POSIX qw(EINTR); use Symbol qw(gensym qualify_to_ref); $VERSION = '1.01'; @ISA = qw(Exporter); @EXPORT_OK = qw(fork_retry sync_exec sync_fhpopen_noshell sync_popen_noshell sync_open); =item B [I [I]] This function runs fork() until it succeeds or until I (default 5) attempts have been made, sleeping I seconds (default 5) between attempts. If the last fork() fails B croak()s. =cut sub fork_retry { @_ > 2 and croak "Usage: fork_retry max_retries=5 sleep_between=5"; my ($max_retries, $sleep) = @_; my ($retries, $kid); $max_retries = 5 if !defined $max_retries or $max_retries < 0; $sleep = 5 if !defined $sleep or $sleep < 0; $retries = 0; while (!defined($kid = fork)) { croak "Can't fork: $!" if $retries++ >= $max_retries; sleep $sleep; } return $kid; } =item B [I] I... This function is similar to a fork()/exec() sequence but with a few twists. B does not return until after the fork()ed child has already performed its exec(). The synchronization this provides is useful in some unusual circumstances. Normally the pid of the child process is returned. However, if the child fails its exec() B returns undef and sets $! to the reason for the child's exec() failure. Since the @cmd array is passed directly to Perl's exec() Perl might choose to invoke the command via the shell if @cmd contains only one element and it looks like it needs a shell to interpret it. If this happens the return value of B only indicates whether the exec() of the shell worked. The optional initial I argument must be a code reference. If it is present it is run in the child just before exec() is called. You can use this to set up redirections or whatever. If I returns false no exec is performed, instead a failure is returned using the current $! value (or EINTR if $! is 0). If the fork() fails or if there is some other unexpected system error B croak()s rather than returning. =cut sub sync_exec { my $code = (@_ && ref $_[0] eq 'CODE') ? shift : undef; @_ or croak 'Usage: sync_exec [code] cmd [arg]...'; my @cmd = @_; my ($reader, $writer) = (gensym, gensym); pipe $reader, $writer or croak "Can't pipe(): $!"; my $pid = fork_retry; if (!$pid) { my $ok = 1; $ok = close $reader if $ok; $ok = fcntl $writer, F_SETFD, 1 if $ok; $ok = &$code() if $ok && $code; $^W = 0; # turn off "Can't exec" message if (!$ok or !exec @cmd) { select $writer; $| = 1; print $!+0; POSIX::_exit 1; } } close $writer or croak "Error closing parent's write pipe: $!"; my ($nread, $buf); while (1) { $nread = sysread $reader, $buf, 16; last if defined $nread; next if $! == EINTR; croak "Error reading from pipe: $!"; } close $reader or croak "Error closing parent's read pipe: $!"; if ($nread) { while (waitpid($pid, 0) == -1) { next if $! == EINTR; croak "Error waiting for child: $!"; } $pid = undef; $! = $buf+0 || EINTR; } return $pid; } =item B I I I [I]... This is a popen() but it never invokes the shell and it uses sync_exec() under the covers. See L. The I is either C<'r'> to read from the process or C<'w'> to write to it. The return value is the pid of the forked process. =cut sub sync_fhpopen_noshell { @_ >= 3 or croak 'Usage: sync_fhpopen_noshell fh type cmd...'; my $fh_parent = qualify_to_ref shift, caller; my ($type, @cmd) = @_; my ($fh_child, $fh_dup_to, $fh_dup_type, $result); $fh_child = gensym; if ($type eq 'w') { $result = pipe $fh_child, $fh_parent; $fh_dup_to = \*STDIN; $fh_dup_type = '<&'; } elsif ($type eq 'r') { $result = pipe $fh_parent, $fh_child; $fh_dup_to = \*STDOUT; $fh_dup_type = '>&'; } else { croak "Invalid popen type `$type'"; } $result or croak "Can't pipe(): $!"; $result = sync_exec sub { close $fh_parent and open $fh_dup_to, $fh_dup_type . fileno $fh_child and close $fh_child }, @cmd; my $errno = $!; close $fh_child or croak "Error closing parent pipe: $!"; $! = $errno; return $result; } =item B I I I... This is like B, but you don't have to supply the filehandle. If called in an array context the return value is a list consisting of the filehandle and the PID of the child. In a scalar context only the filehandle is returned. =cut #' sub sync_popen_noshell { @_ >= 2 or croak 'Usage: sync_popen_noshell type cmd...'; my ($type, @cmd) = @_; my $fh = gensym; my $pid = sync_fhpopen_noshell $fh, $type, @cmd or return; wantarray ? ($fh, $pid) : $fh; } =item B I [I] This is like a Perl open() except that if a pipe is involved and the implied exec() fails sync_open() fails with $! set appropriately. See L. Like B, B croak()s if there is an unexpected system error (such as a failed pipe()). Also like B, if you use a command which Perl needs to use the shell to interpret you'll only know if the exec of the shell worked. Use B or B to be sure that this doesn't happen. =cut sub sync_open { @_ == 1 or @_ == 2 or croak 'Usage: sync_open fh [open-spec]'; my $fh = qualify_to_ref shift, caller; my $cmd = @_ ? shift : $fh; my $type; $cmd =~ s/^\s+//; $cmd =~ s/\s+$//; if ($cmd =~ s/^\|//) { if (substr($cmd, -1) eq '|') { croak "Can't do bidirectional pipe"; } $type = 'w'; } elsif ($cmd =~ s/\|$//) { $type = 'r'; } else { # Not a pipe, just do a regular open. return open $fh, $cmd; } return sync_fhpopen_noshell $fh, $type, $cmd; } 1 __END__ =back =head1 AUTHOR Roderick Schertler > =head1 SEE ALSO perl(1). =cut Proc-SyncExec-1.01/MANIFEST0000664000175000017500000000020710201150752016626 0ustar roderickroderick00000000000000 $Id: MANIFEST,v 1.2 2005/02/05 14:02:18 roderick Exp $ Changes MANIFEST MANIFEST.SKIP META.yml Makefile.PL README SyncExec.pm test.t Proc-SyncExec-1.01/test.t0000644000175000017500000000303607163263307016660 0ustar roderickroderick00000000000000#!perl -w use strict; # $Id: test.t,v 1.3 2000/09/24 02:28:23 roderick Exp $ # # Copyright (c) 2000 Roderick Schertler. All rights reserved. This # program is free software; you can redistribute it and/or modify it # under the same terms as Perl itself. my ($Expect, $Got, $Exit, $Parent_pid); BEGIN { $Expect = 16; $Got = 0; $Exit = 0; $Parent_pid = $$; $| = 1; print "1..$Expect\n"; } sub ok { my ($result, @info) = @_; $Got++; if ($result) { print "ok $Got\n"; } else { $Exit = 1; my ($pkg, $file, $line) = caller; print "not ok $Got at $file:$line", @info ? (' # ', @info) : (), "\n"; } } END { if ($$ == $Parent_pid) { ok $Got+1 == $Expect, "wrong number of tests, got ", 1+$Got, " not $Expect"; $? = $Exit; } } use Proc::SyncExec qw(fork_retry sync_exec sync_open); use POSIX qw(EACCES ENOENT); my ($fh, $pid, $s, $r, @l); eval { fork_retry or exit }; ok $@ eq '', $@; $pid = sync_exec 'this better not exist', 23; ok !defined $pid, $pid; ok $! == ENOENT, $!; $pid = sync_exec '/'; ok !defined $pid, $pid; ok $! == EACCES, $!; $pid = sync_exec 'true; exit 23'; ok $pid, $!; $r = waitpid $pid, 0; ok $r == $pid, $r; ok $? == 23 * 256, $?; close READ; # squelch used only once warning $pid = sync_open *READ, 'this-better-not-exist-either foo|'; ok !defined $pid, $pid; ok $! == ENOENT, $!; $pid = sync_open *WRITE, "|$^X -we 'exit '"; ok $pid, $!; $s = 23; ok print(WRITE $s), $!; ok close(WRITE), $!; ok waitpid($pid, 0) == $pid, $!; ok $? == $s * 256, $?; Proc-SyncExec-1.01/META.yml0000644000175000017500000000045510201150620016743 0ustar roderickroderick00000000000000# http://module-build.sourceforge.net/META-spec.html #XXXXXXX This is a prototype!!! It will change in the future!!! XXXXX# name: Proc-SyncExec version: 1.01 version_from: SyncExec.pm installdirs: site requires: distribution_type: module generated_by: ExtUtils::MakeMaker version 6.17 Proc-SyncExec-1.01/MANIFEST.SKIP0000644000175000017500000000012106374361673017412 0ustar roderickroderick00000000000000# $Id: MANIFEST.SKIP,v 1.1 1997/08/13 16:41:31 roderick Exp $ ,v$ ~$ ^Makefile$ Proc-SyncExec-1.01/Changes0000644000175000017500000000065510200663574017007 0ustar roderickroderick00000000000000Revision history for Perl extension Proc::SyncExec. 1.01 Fri Feb 4 07:00:07 EST 2005 - Oops, sync_popen_noshell() always returned true. - Oops, sync_*open*() would have allowed a failed close() to clobber a failed exec()'s $!. 1.00 Sat Sep 23 22:26:04 EDT 2000 - Add a synopsis and tests. 0.01 Wed Aug 13 12:41:00 EDT 1997 - Initial version. $Id: Changes,v 1.3 2005/02/04 12:15:56 roderick Exp $ Proc-SyncExec-1.01/README0000644000175000017500000000120707330261571016367 0ustar roderickroderick00000000000000This distribution contains the Proc::SyncExec module. To build this module, run: perl Makefile.PL make make test make install For more detailed instructions see "man perlmodinstall", "perldoc perlmodinstall", or http://www.cpan.org/modules/INSTALL.html. The documentation is embeded in the module, use perldoc SyncExec.pm to read it before installation. Roderick Schertler Copyright (c) 1997 Roderick Schertler. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. $Id: README,v 1.3 2001/07/27 12:50:01 roderick Exp $ Proc-SyncExec-1.01/Makefile.PL0000664000175000017500000000111210201150752017443 0ustar roderickroderick00000000000000# $Id: Makefile.PL,v 1.2 2005/02/05 14:02:18 roderick Exp $ # # Copyright (c) 1997 Roderick Schertler. All rights reserved. This # program is free software; you can redistribute it and/or modify it # under the same terms as Perl itself. use ExtUtils::MakeMaker; WriteMakefile( NAME => 'Proc::SyncExec', VERSION_FROM => 'SyncExec.pm', dist => { COMPRESS => 'gzip --best', DIST_CP => 'ln', PREOP => '$(MAKE) ci', SUFFIX => 'gz', CI => 'cvs commit', RCS_LABEL => 'cvs -q tag -F v$(VERSION_SYM)', }, test => { TESTS => '*.t' }, );