Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Daemon crashes the system

by jjimmy (Initiate)
on Nov 02, 2011 at 12:06 UTC ( [id://935367]=perlquestion: print w/replies, xml ) Need Help??

jjimmy has asked for the wisdom of the Perl Monks concerning the following question:

Hi, Hi, I have a problem when I turn the daemon and will perform several times a command by php script, a script crashes the system.
Throws an error: fork() failed: Cannot allocate memory at Frontier/Daemon/Forking.pm line 40.

Maybe too many connections at once in php script?

Daemon:
use warnings; use strict; use Frontier::Daemon::Forking; use Crypt::XXTEA; use Cfg::Config; use Unix::PasswdFile; use MIME::Base64; use constant DEMON_PASS => $Cfg::Config{demonpass}; use constant DEMON_PORT => $Cfg::Config{demonport}; print "Uruchamianie demona.\n"; my $demon = Frontier::Daemon::Forking->new( methods => { sprawdz => \&sprawdz, instaluj => \&instaluj, start => \&start, stop => \&stop, konsola => \&konsola, send => \&send, ftp => \&ftp, reinstall => \&reinstall, delete => \&delete, status => \&status, }, LocalPort => DEMON_PORT, LocalAddr => '0.0.0.0', ) or die "Nie mozna uruchomic demona, sprobuj za chwile.\nKod bledu: $ +!"; sub decrypt_params { my @params; foreach my $param (@_) { $param = decode_base64($param); $param = Crypt::XXTEA::decrypt($param,DEMON_PASS); $param = decode_base64($param); return $param; push(@params,$param); } return @params; } sub sprawdz { my ($spr) = @_; my $sprawdz = decrypt_params($spr); if($spr ne 'spr') { return 1; } return 0; } sub instaluj { my($katalogins, $loginftp, $hasloftp) = @_; decrypt_params($katalogins); decrypt_params($loginftp); decrypt_params($hasloftp); system("screen -A -m -d -S c".$loginftp." cp -r ".$katalogins." /home/ +srv".$loginftp."/;"); system("useradd -d /home/srv".$loginftp."/ ".$loginftp.""); my $pw = new Unix::PasswdFile "/etc/passwd"; $pw->passwd("".$loginftp."", $pw->encpass("".$hasloftp."")); $pw->commit(); undef $pw; } sub start { my($katalog, $komenda, $loginftp) = @_; decrypt_params($katalog); decrypt_params($loginftp); decrypt_params($komenda); system("cd ".$katalog.";screen -A -m -d -L -S srv".$loginftp." ".$kome +nda.";"); } sub stop { my($loginftp) = @_; decrypt_params($loginftp); system("kill -3 `screen -list | grep srv".$loginftp." | cut -d . -f1`; +"); system("screen -wipe;"); } sub konsola { my ($loginftp) = @_; decrypt_params($loginftp); system("cd /home/srv".$loginftp.";rm screenlog;tail -n 200 screenlog.0 + > screenlog;"); } sub send { my ($loginftp, $komenda) = @_; decrypt_params($loginftp); decrypt_params($komenda); system("screen -A -m -d -S tmp".$loginftp." screen -x srv".$loginftp." +;"); sleep 1; system("screen -s srv".$loginftp." -X stuff '".$komenda." ';"); system("kill -9 `screen -list | grep tmp".$loginftp." | cut -d . -f1`; +"); } sub ftp { my($loginftp, $hasloftp) = @_; decrypt_params($loginftp); decrypt_params($hasloftp); my $pw = new Unix::PasswdFile "/etc/passwd"; $pw->passwd("".$loginftp."", $pw->encpass("".$hasloftp."")); $pw->commit(); undef $pw; } sub reinstall { my($loginftp, $katalogins) = @_; decrypt_params($katalogins); decrypt_params($loginftp); system("rm -r /home/srv".$loginftp."/ && cp -R ".$katalogins." /home/s +rv".$loginftp."/;"); } sub delete { my($loginftp) = @_; decrypt_params($loginftp); system("rm -r /home/srv".$loginftp."/;"); } sub status { my($screen) = @_; decrypt_params($screen); my $stat = `screen -list | grep $screen`; if ( $stat =~ /^\s*$/ ) { return 0; } else { return 1; } }

Replies are listed 'Best First'.
Re: Daemon crashes the system
by roboticus (Chancellor) on Nov 02, 2011 at 12:16 UTC

    jjimmy:

    Since fork is complaining about not being able to allocate memory, then I'd look at memory consumption of the various programs running, rather than the number of connections. Also, if you're suspecting a PHP program, you might want to post your question & PHP script to an appropriate PHP forum.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Re: Daemon crashes the system
by RMGir (Prior) on Nov 02, 2011 at 12:21 UTC
    Shouldn't you be installing a $SIG{CHLD} handler? (Or setting $SIG{CHLD}='IGNORE' -- see perlipc)

    It doesn't look like Frontier::Daemon::Forking does that for you, so the child processes are probably staying around as zombies.


    Mike
Re: Daemon crashes the system
by jjimmy (Initiate) on Nov 02, 2011 at 12:23 UTC
    RMGir, and what i do now? Sorry, but my english is weak
      Don't apologize - your English is far superior to my Polish (thank you, Google Translate, for helping me identify that as Polish :) )

      I think all you need to do is add the following lines just below your last "use" statement:

      use Config; # copied from perldoc Config my %sig_num; my @sig_name; unless($Config{sig_name} && $Config{sig_num}) { die "No sigs?"; } else { my @names = split ' ', $Config{sig_name}; @sig_num{@names} = split ' ', $Config{sig_num}; foreach (@names) { $sig_name[$sig_num{$_}] ||= $_; } } # ignore child signals, automatically reaping child processes $SIG{$sig_num{CHLD}} = 'IGNORE';
      Of course, you'll have to test this, but I think it might solve your issue.

      Mike
        RMGir, your way isn't working.
      Where i have to add?: $SIG{CHLD} = 'IGNORE';

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://935367]
Approved by planetscape
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (2)
As of 2024-04-20 03:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found