Created
March 3, 2016 17:36
-
-
Save dfaerch/ce1fd418567ec7f84b52 to your computer and use it in GitHub Desktop.
Example of Parallel::ForkManager, with setuid() and with returning data to parent, using /dev/shm for temp storage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
use strict; | |
use warnings; | |
use Parallel::ForkManager 1.7.6; | |
use Data::Dumper; | |
use POSIX qw(setuid setgid); | |
use File::Temp; | |
# We make a temp tmp dir in memory (/dev/shm/) instead of built in /tmp/ (for speedy access) | |
my $tmpdir = mkdtemp("/dev/shm/$0.XXXXXXXXX") ; | |
# Make perms like /tmp/ | |
chmod oct(1777), $tmpdir; | |
# INIT ForkManager to 10 forks and our custom tmp dir | |
my $pm = new Parallel::ForkManager(10, $tmpdir); | |
##-- Setup a callback for when a child finishes | |
$pm->run_on_finish( sub { | |
my ($pid, $exit_code, $ident, $exit_signal, $core_dump, $data_structure_reference) = @_; | |
print "\n** $ident just finished ". | |
"with PID $pid and exit code: $exit_code\n"; | |
print Dumper($data_structure_reference); | |
}); | |
##-- Main loop | |
foreach ((1,2,3,4,5,6)) { | |
$pm->start("Proc $_") and next; | |
## -- We are now the child process | |
setuid(33); | |
setgid(33); | |
# Store some data to return | |
my %res; | |
$res{$_} = " pid $$"; | |
$res{'id'} = `id`; | |
$pm->finish(0,\%res); | |
} | |
$pm->wait_all_children; | |
rmdir $tmpdir; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment