| 1 | package Proc::Application::Daemon; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | Proc::Application::Daemon - daemon class based on Proc::Application; | ||
| 6 | |||
| 7 | =head1 SYNOPSIS | ||
| 8 | |||
| 9 | package Program; | ||
| 10 | use Proc::Application::Daemon; | ||
| 11 | use base qw(Proc::Application::Daemon); | ||
| 12 | sub handler | ||
| 13 | { | ||
| 14 | my ( $his, $clientSocket ) = @_; | ||
| 15 | $this->socket->print ( 'Done' ); | ||
| 16 | $this->log->warning ( 'warning' ); | ||
| 17 | die "Error"; | ||
| 18 | } | ||
| 19 | package main; | ||
| 20 | Program->new->run(); | ||
| 21 | |||
| 22 | |||
| 23 | =head1 DESCRIPTION | ||
| 24 | |||
| 25 | daemon class based on Proc::Application; | ||
| 26 | |||
| 27 | =cut | ||
| 28 | |||
| 29 | use strict; | ||
| 30 | use Proc::Application; | ||
| 31 | use base qw(Proc::Application); | ||
| 32 | use POSIX; | ||
| 33 | use Errno; | ||
| 34 | |||
| 35 | use constant FORK_COUNT => 10; | ||
| 36 | |||
| 37 | =head2 new | ||
| 38 | |||
| 39 | A construtror, setup childs and childCount | ||
| 40 | |||
| 41 | =cut | ||
| 42 | |||
| 43 | sub new | ||
| 44 | { | ||
| 45 | my $class = shift; | ||
| 46 | my $this = $class->SUPER::new ( @_ ); | ||
| 47 | $this->{childs} ||= {}; | ||
| 48 | $this->{childCount} ||= 0; | ||
| 49 | $this; | ||
| 50 | } | ||
| 51 | |||
| 52 | =head2 options | ||
| 53 | |||
| 54 | Add mode (fork|single|prefork=[count]) and socket (domain=unix|inet|ssl:all_io::socket:: parameter) options | ||
| 55 | |||
| 56 | =cut | ||
| 57 | |||
| 58 | sub options | ||
| 59 | { | ||
| 60 | my $this = shift; | ||
| 61 | my $options = $this->SUPER::options(); | ||
| 62 | $options->{mode} = { template => 'mode=s', #fork|prefork|single | =count | ||
| 63 | description => 'fork|single|prefork=count', | ||
| 64 | default => 'fork', | ||
| 65 | action => sub { $this->processModeParameter ( @_ ) } }; | ||
| 66 | $options->{socket} = { template => 'socket=s', | ||
| 67 | description => 'parameters for init socket', | ||
| 68 | priority => 8, | ||
| 69 | action => sub { $this->processSocketCreate ( @_ ) } }; | ||
| 70 | $options; | ||
| 71 | } | ||
| 72 | |||
| 73 | =head2 processSocketCreate | ||
| 74 | |||
| 75 | Handler for socket option. Create new socket and store it to $object->{socket} | ||
| 76 | |||
| 77 | =cut | ||
| 78 | |||
| 79 | sub processSocketCreate | ||
| 80 | { | ||
| 81 | my ( $this, $option, $params ) = @_; | ||
| 82 | my %params = $this->_decodeOption ( $params ); | ||
| 83 | my $socketDomain = delete $params{domain} || die "Your must setup domain parameter for socket"; | ||
| 84 | my $socketClass = 'IO::Socket::' . | ||
| 85 | ( { inet => 'INET', unix => 'UNIX', ssl => 'SSL' }->{ $socketDomain } || die "Error socket domain: $socketDomain" ); | ||
| 86 | eval "use $socketClass;"; die $@ if $@; | ||
| 87 | $this->{mainSocket} = $socketClass->new ( %params ) || die "Can't create socket: $!"; | ||
| 88 | } | ||
| 89 | |||
| 90 | =head2 processModeParameter | ||
| 91 | |||
| 92 | Child for 'prefork=count' mode and setup preforkCount option | ||
| 93 | |||
| 94 | =cut | ||
| 95 | |||
| 96 | sub processModeParameter | ||
| 97 | { | ||
| 98 | my ( $this, $option, $value ) = @_; | ||
| 99 | my ( $mode, $count ) = split /=/, $value; | ||
| 100 | $mode eq 'prefork' || return; | ||
| 101 | $this->{options}->{mode} = $mode; | ||
| 102 | $count ||= FORK_COUNT; | ||
| 103 | $count =~ /^\d+$/ || die "Your must setup numeric value for prefork childs count"; | ||
| 104 | $this->{options}->{preforkcount} = $count; | ||
| 105 | } | ||
| 106 | |||
| 107 | =head2 socket | ||
| 108 | |||
| 109 | Return a client socket from $object->{socket} | ||
| 110 | |||
| 111 | =cut | ||
| 112 | |||
| 113 | sub socket | ||
| 114 | { | ||
| 115 | my $this = shift; | ||
| 116 | $this->{socket}; | ||
| 117 | } | ||
| 118 | |||
| 119 | =head2 mainSocket | ||
| 120 | |||
| 121 | Return a main daemon socket from $object->{socket} | ||
| 122 | |||
| 123 | =cut | ||
| 124 | |||
| 125 | sub mainSocket | ||
| 126 | { | ||
| 127 | my $this = shift; | ||
| 128 | $this->{mainSocket}; | ||
| 129 | } | ||
| 130 | |||
| 131 | =head2 done | ||
| 132 | |||
| 133 | You must return 'true' for end main loop | ||
| 134 | |||
| 135 | =cut | ||
| 136 | |||
| 137 | sub done | ||
| 138 | { | ||
| 139 | 0; | ||
| 140 | } | ||
| 141 | |||
| 142 | =head2 mainHandler | ||
| 143 | |||
| 144 | Call handler() method with $cliendSocket atrument, log the errors of execution and close client socket at exit | ||
| 145 | |||
| 146 | =cut | ||
| 147 | |||
| 148 | sub mainHandler | ||
| 149 | { | ||
| 150 | my ( $this, $childSocket ) = @_; | ||
| 151 | eval { $this->handler () }; | ||
| 152 | $this->log->warning ( $@ ) if $@; | ||
| 153 | $childSocket && $childSocket->close(); | ||
| 154 | } | ||
| 155 | |||
| 156 | =head2 handler | ||
| 157 | |||
| 158 | Real work method, get $this and $clientSocket argumentds. | ||
| 159 | |||
| 160 | =cut | ||
| 161 | |||
| 162 | sub handler | ||
| 163 | { | ||
| 164 | my $this = shift; | ||
| 165 | 1; | ||
| 166 | } | ||
| 167 | |||
| 168 | =head2 childs | ||
| 169 | |||
| 170 | Return a hash reference of childs with keys with pids | ||
| 171 | |||
| 172 | =cut | ||
| 173 | |||
| 174 | sub childs | ||
| 175 | { | ||
| 176 | my $this = shift; | ||
| 177 | $this->{childs}; | ||
| 178 | } | ||
| 179 | |||
| 180 | =head2 processSigChild | ||
| 181 | |||
| 182 | SIGCHLD processor. Get a child pid by waitpid() and delete it from childs() | ||
| 183 | |||
| 184 | =cut | ||
| 185 | |||
| 186 | sub processSigChild | ||
| 187 | { | ||
| 188 | my $this = shift || return; | ||
| 189 | |||
| 190 | while ( my $pid = waitpid ( -1, WNOHANG ) ) | ||
| 191 | { | ||
| 192 | if ( $pid == -1 ) # process errors | ||
| 193 | { | ||
| 194 | next if $! == Errno::EINTR; | ||
| 195 | last if $! == Errno::ECHILD; | ||
| 196 | $this->log->error ( "waitpid() error: $!" ); | ||
| 197 | last; | ||
| 198 | } | ||
| 199 | |||
| 200 | #$this->done && last; | ||
| 201 | $this->{childCount}-- if $this->{childCount}; | ||
| 202 | delete $this->{childs}->{ $pid } || $this->log->warning ( "Unknown child $pid" ); | ||
| 203 | } | ||
| 204 | |||
| 205 | $SIG{CHLD} = sub { $this || return; $this->processSigChild }; | ||
| 206 | } | ||
| 207 | |||
| 208 | =head2 forkFunction | ||
| 209 | |||
| 210 | Get three code refs ( for run at parent, child and error fork sitiations ), fork process and | ||
| 211 | execute parameters functions. Store pid of new process at childs(), exit(0) at child after | ||
| 212 | execute of child function, log fork error and sleep(1) after fork error. | ||
| 213 | funtions | ||
| 214 | |||
| 215 | =cut | ||
| 216 | |||
| 217 | sub forkFunction | ||
| 218 | { | ||
| 219 | my ( $this, $parentFunction, $childFunction, $errorFunction ) = @_; | ||
| 220 | if ( my $pid = fork() ) | ||
| 221 | { | ||
| 222 | $this->{childs}->{ $pid } = 1; | ||
| 223 | &$parentFunction() if $parentFunction; | ||
| 224 | } | ||
| 225 | elsif ( defined $pid ) | ||
| 226 | { | ||
| 227 | &$childFunction() if $childFunction; | ||
| 228 | exit ( 0 ); | ||
| 229 | } | ||
| 230 | else | ||
| 231 | { | ||
| 232 | $this->log->error ( "fock failed(): $!" ); | ||
| 233 | &$errorFunction() if &$errorFunction(); | ||
| 234 | sleep ( 1 ); | ||
| 235 | } | ||
| 236 | } | ||
| 237 | |||
| 238 | =head2 threadFunction | ||
| 239 | |||
| 240 | =cut | ||
| 241 | |||
| 242 | sub threadFunction | ||
| 243 | { | ||
| 244 | my ( $this, $parentFunction, $childFunction, $errorFunction ) = @_; | ||
| 245 | if ( my $pid = fork() ) | ||
| 246 | { | ||
| 247 | $this->{childs}->{ $pid } = 1; | ||
| 248 | &$parentFunction() if $parentFunction; | ||
| 249 | } | ||
| 250 | elsif ( defined $pid ) | ||
| 251 | { | ||
| 252 | &$childFunction() if $childFunction; | ||
| 253 | exit ( 0 ); | ||
| 254 | } | ||
| 255 | else | ||
| 256 | { | ||
| 257 | $this->log->error ( "fock failed(): $!" ); | ||
| 258 | &$errorFunction() if &$errorFunction(); | ||
| 259 | sleep ( 1 ); | ||
| 260 | } | ||
| 261 | } | ||
| 262 | |||
| 263 | =head2 realMain | ||
| 264 | |||
| 265 | Main loop. accept() the new connection, and fork (if fork more) and pass execution to mainHandler | ||
| 266 | |||
| 267 | =cut | ||
| 268 | |||
| 269 | sub realMain | ||
| 270 | { | ||
| 271 | my $this = shift; | ||
| 272 | my $mode = $this->{options}->{mode}; | ||
| 273 | while ( ! $this->done() ) | ||
| 274 | { | ||
| 275 | my $mainSocket = $this->mainSocket(); | ||
| 276 | |||
| 277 | my $childSocket = $this->{socket} = $mainSocket->accept; | ||
| 278 | |||
| 279 | unless ( $childSocket ) | ||
| 280 | { | ||
| 281 | $this->log->error ( "accept() failed: $!" ); | ||
| 282 | next; | ||
| 283 | } | ||
| 284 | |||
| 285 | if ( $mode eq 'fork' ) | ||
| 286 | { | ||
| 287 | $this->forkFunction ( sub { $childSocket->close() }, sub { $mainSocket->close(); $this->mainHandler (); } ); | ||
| 288 | } | ||
| 289 | elsif ( $mode eq 'thread' ) | ||
| 290 | { | ||
| 291 | $this->threadFunction ( sub { $childSocket->close() }, sub { $mainSocket->close(); $this->mainHandler (); } ); | ||
| 292 | } | ||
| 293 | elsif ( $mode eq 'single' || $mode eq 'prefork' || $mode eq 'threadpool' ) | ||
| 294 | { | ||
| 295 | $this->mainHandler ( $childSocket ); | ||
| 296 | } | ||
| 297 | } | ||
| 298 | } | ||
| 299 | |||
| 300 | =head2 main | ||
| 301 | |||
| 302 | Prefork processes if prefork mode, loop up for preforked childs count and call realMain() | ||
| 303 | |||
| 304 | =cut | ||
| 305 | |||
| 306 | sub main | ||
| 307 | { | ||
| 308 | my $this = shift || return; | ||
| 309 | my $mode = $this->{options}->{mode}; | ||
| 310 | $SIG{CHLD} = sub { $this || return; $this->processSigChild } if $mode =~ /fork/; | ||
| 311 | $this->mainSocket || die "You must setup socket parameter"; | ||
| 312 | $this->realMain () unless $mode eq 'prefork' || $mode eq 'threadpool'; | ||
| 313 | if ( $mode eq 'prefork' ) | ||
| 314 | { | ||
| 315 | while ( ! $this->done ) | ||
| 316 | { | ||
| 317 | while ( $this->{childCount} < $this->{options}->{preforkcount} ) | ||
| 318 | { | ||
| 319 | $this->forkFunction ( sub { $this->{childCount}++ }, sub { $this->realMain(); } ); | ||
| 320 | } | ||
| 321 | sleep ( 1 ); | ||
| 322 | } | ||
| 323 | } | ||
| 324 | else # threadpool | ||
| 325 | { | ||
| 326 | while ( ! $this->done ) | ||
| 327 | { | ||
| 328 | while ( $this->{threadCount} < $this->{options}->{threadpoolcount} ) | ||
| 329 | { | ||
| 330 | $this->threadFunction ( sub { $this->{threadCount}++ }, sub { $this->realMain(); } ); | ||
| 331 | } | ||
| 332 | sleep ( 1 ); | ||
| 333 | } | ||
| 334 | |||
| 335 | } | ||
| 336 | } | ||
| 337 | |||
| 338 | =head2 DESTROY | ||
| 339 | |||
| 340 | Send TERM signal to all childs and call parent DESTROY() | ||
| 341 | |||
| 342 | =cut | ||
| 343 | |||
| 344 | sub DESTROY | ||
| 345 | { | ||
| 346 | my $this = shift; | ||
| 347 | foreach my $pid ( keys %{ $this->{childs} } ) | ||
| 348 | { | ||
| 349 | kill TERM => $pid; | ||
| 350 | } | ||
| 351 | $this->SUPER::DESTROY(); | ||
| 352 | } | ||
| 353 | |||
| 354 | =head2 Log Debug Error Fatal Run Options | ||
| 355 | |||
| 356 | for compatibility with Net::Daemon | ||
| 357 | |||
| 358 | =cut | ||
| 359 | |||
| 360 | sub Log | ||
| 361 | { | ||
| 362 | my ( $this, $level, $message, @args ) = @_; | ||
| 363 | # warn "depricated call Log() from " . ( join ' ', caller() ) . "\n"; | ||
| 364 | $message = sprintf ( $message, @args ) if @args; | ||
| 365 | $this->log->$level ( $message ); | ||
| 366 | } | ||
| 367 | |||
| 368 | sub Debug | ||
| 369 | { | ||
| 370 | my ( $this, $message, @args ) = @_; | ||
| 371 | # warn "depricated call Log() from " . ( join ' ', caller() ) . "\n"; | ||
| 372 | $message = sprintf ( $message, @args ) if @args; | ||
| 373 | $this->log->error ( $message ); | ||
| 374 | } | ||
| 375 | |||
| 376 | sub Fatal | ||
| 377 | { | ||
| 378 | my ( $this, $message, @args ) = @_; | ||
| 379 | # warn "depricated call Log() from " . ( join ' ', caller() ) . "\n"; | ||
| 380 | $message = sprintf ( $message, @args ) if @args; | ||
| 381 | $this->log->error ( $message ); | ||
| 382 | die $message; | ||
| 383 | } | ||
| 384 | |||
| 385 | sub Run | ||
| 386 | { | ||
| 387 | my $this = shift; | ||
| 388 | # warn "depricated call Log() from " . ( join ' ', caller() ); | ||
| 389 | $this->handler (); | ||
| 390 | } | ||
| 391 | |||
| 392 | sub Options | ||
| 393 | { | ||
| 394 | my $this = shift; | ||
| 395 | # warn "depricated call Log() from " . ( join ' ', caller() ); | ||
| 396 | $this->options ( @_ ); | ||
| 397 | } | ||
| 398 | |||
| 399 | sub Bind | ||
| 400 | { | ||
| 401 | my $this = shift; | ||
| 402 | # warn "depricated call Log() from " . ( join ' ', caller() ); | ||
| 403 | $this->run ( @_ ); | ||
| 404 | } | ||
| 405 | |||
| 406 | 1; |