| 1 | package Daemonise::Plugin::Daemon; | ||
| 2 | |||
| 3 | use Mouse::Role; | ||
| 4 | |||
| 5 | # ABSTRACT: Daemonise plugin handling PID file and forking | ||
| 6 | |||
| 7 | use POSIX qw(strftime SIGTERM SIG_BLOCK SIG_UNBLOCK); | ||
| 8 | |||
| 9 | |||
| 10 | has 'user' => ( | ||
| 11 | is => 'rw', | ||
| 12 | default => sub { 'root' }, | ||
| 13 | ); | ||
| 14 | |||
| 15 | |||
| 16 | has 'uid' => ( | ||
| 17 | is => 'rw', | ||
| 18 | isa => 'Int', | ||
| 19 | ); | ||
| 20 | |||
| 21 | |||
| 22 | has 'group' => ( | ||
| 23 | is => 'rw', | ||
| 24 | default => sub { 'root' }, | ||
| 25 | ); | ||
| 26 | |||
| 27 | |||
| 28 | has 'gid' => (is => 'rw'); | ||
| 29 | |||
| 30 | |||
| 31 | has 'pid_file' => ( | ||
| 32 | is => 'rw', | ||
| 33 | predicate => 'has_pid_file', | ||
| 34 | ); | ||
| 35 | |||
| 36 | |||
| 37 | has 'running' => ( | ||
| 38 | is => 'rw', | ||
| 39 | predicate => 'is_running', | ||
| 40 | ); | ||
| 41 | |||
| 42 | |||
| 43 | has 'phase' => ( | ||
| 44 | is => 'rw', | ||
| 45 | default => sub { 'starting' }, | ||
| 46 | ); | ||
| 47 | |||
| 48 | |||
| 49 | has 'logfile' => ( | ||
| 50 | is => 'rw', | ||
| 51 | predicate => 'has_logfile', | ||
| 52 | ); | ||
| 53 | |||
| 54 | |||
| 55 | has 'foreground' => ( | ||
| 56 | is => 'rw', | ||
| 57 | isa => 'Bool', | ||
| 58 | lazy => 1, | ||
| 59 | default => sub { 0 }, | ||
| 60 | ); | ||
| 61 | |||
| 62 | |||
| 63 | has 'loops' => ( | ||
| 64 | is => 'rw', | ||
| 65 | isa => 'Bool', | ||
| 66 | lazy => 1, | ||
| 67 | default => sub { 1 }, | ||
| 68 | ); | ||
| 69 | |||
| 70 | |||
| 71 | has 'pid_dir' => ( | ||
| 72 | is => 'rw', | ||
| 73 | isa => 'Str', | ||
| 74 | lazy => 1, | ||
| 75 | default => sub { '/var/run/bunny' }, | ||
| 76 | ); | ||
| 77 | |||
| 78 | |||
| 79 | has 'bin_dir' => ( | ||
| 80 | is => 'rw', | ||
| 81 | isa => 'Str', | ||
| 82 | lazy => 1, | ||
| 83 | default => sub { '/usr/local/bunny' }, | ||
| 84 | ); | ||
| 85 | |||
| 86 | |||
| 87 | has 'interval' => ( | ||
| 88 | is => 'rw', | ||
| 89 | isa => 'Int', | ||
| 90 | lazy => 1, | ||
| 91 | default => sub { 5 }, | ||
| 92 | ); | ||
| 93 | |||
| 94 | |||
| 95 | after 'configure' => sub { | ||
| 96 | my ($self, $reconfig) = @_; | ||
| 97 | |||
| 98 | $self->log("configuring Daemon plugin") if $self->debug; | ||
| 99 | |||
| 100 | unless (exists $self->config->{daemon} | ||
| 101 | and ref $self->config->{daemon} eq 'HASH') | ||
| 102 | { | ||
| 103 | warn "'daemon' plugin config section missing!"; | ||
| 104 | return; | ||
| 105 | } | ||
| 106 | |||
| 107 | foreach my $key (keys %{ $self->config->{daemon} }) { | ||
| 108 | my $val = $self->config->{daemon}->{$key}; | ||
| 109 | |||
| 110 | # set properties if they exist but don't die if they don't | ||
| 111 | eval { $self->$key($val) }; | ||
| 112 | warn "$key not defined as property! $@" | ||
| 113 | if ($@ && $self->debug); | ||
| 114 | } | ||
| 115 | |||
| 116 | return; | ||
| 117 | }; | ||
| 118 | |||
| 119 | |||
| 120 | around 'log' => sub { | ||
| 121 | my ($orig, $self, $msg) = @_; | ||
| 122 | |||
| 123 | $self->$orig($msg); | ||
| 124 | |||
| 125 | return unless ($self->has_logfile); | ||
| 126 | |||
| 127 | open(my $log_file, '>>', $self->logfile) | ||
| 128 | or confess "Could not open File (" . $self->logfile . "): $@"; | ||
| 129 | my $now = strftime "%F %T", localtime; | ||
| 130 | print $log_file "${now} " | ||
| 131 | . $self->hostname . ' ' | ||
| 132 | . $self->name | ||
| 133 | . "[$$]: $msg\n"; | ||
| 134 | close $log_file; | ||
| 135 | |||
| 136 | return; | ||
| 137 | }; | ||
| 138 | |||
| 139 | |||
| 140 | before 'stop' => sub { | ||
| 141 | my ($self) = @_; | ||
| 142 | |||
| 143 | unlink $self->pid_file if $self->has_pid_file; | ||
| 144 | |||
| 145 | return; | ||
| 146 | }; | ||
| 147 | |||
| 148 | |||
| 149 | around 'start' => sub { | ||
| 150 | my ($orig, $self, $code) = @_; | ||
| 151 | |||
| 152 | # check whether we have a CODEREF | ||
| 153 | unless (ref $code eq 'CODE') { | ||
| 154 | $self->log("first argument of start() must be a CODEREF! existing..."); | ||
| 155 | $self->stop; | ||
| 156 | } | ||
| 157 | |||
| 158 | # call original method first | ||
| 159 | $self->$orig($code); | ||
| 160 | |||
| 161 | $self->daemonise; | ||
| 162 | |||
| 163 | # we need to reconfigure, because we are in the child and some plugins | ||
| 164 | # start connections in the configure stage | ||
| 165 | $self->configure; | ||
| 166 | |||
| 167 | # graph that startup was ok and that we are running now | ||
| 168 | $self->graph('hase.' . $self->name, 'running', 1) if $self->can('graph'); | ||
| 169 | |||
| 170 | if ($self->loops) { | ||
| 171 | while (1) { | ||
| 172 | { $code->(); } | ||
| 173 | } | ||
| 174 | } | ||
| 175 | else { | ||
| 176 | $code->(); | ||
| 177 | $self->stop; | ||
| 178 | } | ||
| 179 | |||
| 180 | return; | ||
| 181 | }; | ||
| 182 | |||
| 183 | |||
| 184 | sub dont_loop { $_[0]->loops(0); return; } ## no critic | ||
| 185 | sub loop { $_[0]->loops(1); return; } ## no critic | ||
| 186 | |||
| 187 | |||
| 188 | sub check_pid_file { | ||
| 189 | my ($self) = @_; | ||
| 190 | |||
| 191 | ### no pid_file = return success | ||
| 192 | return 1 unless -e $self->pid_file; | ||
| 193 | |||
| 194 | ### get the currently listed pid | ||
| 195 | open(my $pid_file, '<', $self->pid_file) | ||
| 196 | or die "Couldn't open existant pid_file \"" | ||
| 197 | . $self->pid_file | ||
| 198 | . "\" [$!]\n"; | ||
| 199 | my $pid = <$pid_file>; | ||
| 200 | close $pid_file; | ||
| 201 | |||
| 202 | $pid = | ||
| 203 | $pid =~ /^(\d{1,10})/ | ||
| 204 | ? $1 | ||
| 205 | : die "Couldn't find pid in existing pid_file"; | ||
| 206 | |||
| 207 | my $exists; | ||
| 208 | |||
| 209 | ### try a proc file system | ||
| 210 | if (-d '/proc') { | ||
| 211 | $exists = -e "/proc/$pid"; | ||
| 212 | |||
| 213 | ### try ps | ||
| 214 | #}elsif( -x '/bin/ps' ){ # not as portable | ||
| 215 | # the ps command itself really isn't portable | ||
| 216 | # this follows BSD syntax ps (BSD's and linux) | ||
| 217 | # this will fail on Unix98 syntax ps (Solaris, etc) | ||
| 218 | } | ||
| 219 | elsif (`ps p $$ | grep -v 'PID'` =~ /^\s*$$\s+.*$/) { | ||
| 220 | |||
| 221 | # can I play ps on myself ? | ||
| 222 | $exists = `ps p $pid | grep -v 'PID'`; | ||
| 223 | |||
| 224 | } | ||
| 225 | |||
| 226 | ### running process exists, ouch | ||
| 227 | if ($exists) { | ||
| 228 | |||
| 229 | if ($pid == $$) { | ||
| 230 | warn "Pid_file created by this same process. Doing nothing.\n"; | ||
| 231 | return 1; | ||
| 232 | } | ||
| 233 | else { | ||
| 234 | if ($self->phase eq 'status') { | ||
| 235 | $self->running($pid); | ||
| 236 | return; | ||
| 237 | } | ||
| 238 | else { | ||
| 239 | die | ||
| 240 | "Pid_file already exists for running process ($pid)... aborting\n"; | ||
| 241 | } | ||
| 242 | } | ||
| 243 | } | ||
| 244 | else { | ||
| 245 | ### remove the pid_file | ||
| 246 | |||
| 247 | warn "Pid_file \"" | ||
| 248 | . $self->pid_file | ||
| 249 | . "\" already exists. Overwriting!\n"; | ||
| 250 | unlink $self->pid_file | ||
| 251 | || die "Couldn't remove pid_file \"" | ||
| 252 | . $self->pid_file | ||
| 253 | . "\" [$!]\n"; | ||
| 254 | |||
| 255 | return 1; | ||
| 256 | } | ||
| 257 | } | ||
| 258 | |||
| 259 | |||
| 260 | sub daemonise { | ||
| 261 | my ($self) = @_; | ||
| 262 | |||
| 263 | if ($self->foreground) { | ||
| 264 | $self->running($$); | ||
| 265 | $self->log('staying in foreground'); | ||
| 266 | return; | ||
| 267 | } | ||
| 268 | |||
| 269 | $self->phase('starting'); | ||
| 270 | $self->check_pid_file if $self->has_pid_file; | ||
| 271 | |||
| 272 | $self->uid($self->_get_uid); | ||
| 273 | $self->gid($self->_get_gid); | ||
| 274 | $self->gid((split /\s+/, $self->gid)[0]); | ||
| 275 | |||
| 276 | # turn off logging to STDOUT when running in background | ||
| 277 | $self->print_log(0); | ||
| 278 | |||
| 279 | my $pid = $self->async; | ||
| 280 | |||
| 281 | ### parent process should do the pid file and exit | ||
| 282 | if ($pid) { | ||
| 283 | $pid && exit; | ||
| 284 | ### child process will continue on | ||
| 285 | } | ||
| 286 | else { | ||
| 287 | $self->_create_pid_file if $self->has_pid_file; | ||
| 288 | |||
| 289 | ### make sure we can remove the file later | ||
| 290 | chown($self->uid, $self->gid, $self->pid_file) | ||
| 291 | if $self->has_pid_file; | ||
| 292 | |||
| 293 | ### become another user and group | ||
| 294 | $self->_set_user; | ||
| 295 | |||
| 296 | ### close all input/output and separate | ||
| 297 | ### from the parent process group | ||
| 298 | open(STDIN, '<', '/dev/null') | ||
| 299 | or die "Can't open STDIN from /dev/null: [$!]"; | ||
| 300 | |||
| 301 | $self->stdout_redirect; | ||
| 302 | |||
| 303 | ### Change to root dir to avoid locking a mounted file system | ||
| 304 | chdir '/' or die "Can't chdir to \"/\": [$!]"; | ||
| 305 | |||
| 306 | ### Turn process into session leader, and ensure no controlling terminal | ||
| 307 | POSIX::setsid(); | ||
| 308 | |||
| 309 | ### install signal handlers to make sure we shut down gracefully | ||
| 310 | $SIG{QUIT} = sub { $self->stop }; ## no critic | ||
| 311 | $SIG{TERM} = sub { $self->stop }; ## no critic | ||
| 312 | $SIG{INT} = sub { $self->stop }; ## no critic | ||
| 313 | |||
| 314 | $self->log("daemon started"); | ||
| 315 | |||
| 316 | return 1; | ||
| 317 | } | ||
| 318 | |||
| 319 | return; | ||
| 320 | } | ||
| 321 | |||
| 322 | |||
| 323 | around 'stdout_redirect' => sub { | ||
| 324 | my ($orig, $self) = @_; | ||
| 325 | |||
| 326 | if ($self->has_logfile) { | ||
| 327 | my $logfile = $self->logfile; | ||
| 328 | open(STDOUT, '>>', $logfile) | ||
| 329 | or die "Can't redirect STDOUT to $logfile: [$!]"; | ||
| 330 | open(STDERR, '>>', '&STDOUT') | ||
| 331 | or die "Can't redirect STDERR to STDOUT: [$!]"; | ||
| 332 | } | ||
| 333 | else { | ||
| 334 | open(STDOUT, '>', '/dev/null') | ||
| 335 | or die "Can't redirect STDOUT to /dev/null: [$!]"; | ||
| 336 | open(STDERR, '>', '&STDOUT') | ||
| 337 | or die "Can't redirect STDERR to STDOUT: [$!]"; | ||
| 338 | } | ||
| 339 | |||
| 340 | return; | ||
| 341 | }; | ||
| 342 | |||
| 343 | |||
| 344 | sub status { | ||
| 345 | my ($self) = @_; | ||
| 346 | |||
| 347 | $self->phase('status'); | ||
| 348 | $self->check_pid_file(); | ||
| 349 | |||
| 350 | if ($self->is_running) { | ||
| 351 | return $self->running . ' with PID file ' . $self->pid_file; | ||
| 352 | } | ||
| 353 | |||
| 354 | return; | ||
| 355 | } | ||
| 356 | |||
| 357 | sub _get_uid { | ||
| 358 | my ($self) = @_; | ||
| 359 | |||
| 360 | my $uid = undef; | ||
| 361 | if ($self->user =~ /^\d+$/) { | ||
| 362 | $uid = $self->user; | ||
| 363 | } | ||
| 364 | else { | ||
| 365 | $uid = getpwnam($self->user); | ||
| 366 | } | ||
| 367 | |||
| 368 | die 'No such user "' . $self->user . '"' unless defined $uid; | ||
| 369 | |||
| 370 | return $uid; | ||
| 371 | } | ||
| 372 | |||
| 373 | sub _get_gid { | ||
| 374 | my ($self) = @_; | ||
| 375 | |||
| 376 | my @gid = (); | ||
| 377 | foreach my $group (split(/[, ]+/, join(" ", $self->group))) { | ||
| 378 | if ($group =~ /^\d+$/) { | ||
| 379 | push @gid, $group; | ||
| 380 | } | ||
| 381 | else { | ||
| 382 | my $id = getgrnam($group); | ||
| 383 | die "No such group \"$group\"" unless defined $id; | ||
| 384 | push @gid, $id; | ||
| 385 | } | ||
| 386 | } | ||
| 387 | |||
| 388 | die "No group found in arguments." unless @gid; | ||
| 389 | |||
| 390 | return join(" ", $gid[0], @gid); | ||
| 391 | } | ||
| 392 | |||
| 393 | sub _create_pid_file { | ||
| 394 | my ($self) = @_; | ||
| 395 | |||
| 396 | # child should also know its PID | ||
| 397 | $self->running($$); | ||
| 398 | |||
| 399 | ### see if the pid_file is already there | ||
| 400 | $self->check_pid_file; | ||
| 401 | |||
| 402 | open(my $pid_file, '>', $self->pid_file) | ||
| 403 | or die "Couldn't open pid file \"" . $self->pid_file . "\" [$!].\n"; | ||
| 404 | print $pid_file "$$\n"; | ||
| 405 | close $pid_file; | ||
| 406 | |||
| 407 | die "Pid_file \"" . $self->pid_file . "\" not created.\n" | ||
| 408 | unless -e $self->pid_file; | ||
| 409 | |||
| 410 | return 1; | ||
| 411 | } | ||
| 412 | |||
| 413 | sub _set_user { | ||
| 414 | my ($self) = @_; | ||
| 415 | |||
| 416 | $self->_set_gid || return; | ||
| 417 | $self->_set_uid || return; | ||
| 418 | |||
| 419 | return 1; | ||
| 420 | } | ||
| 421 | |||
| 422 | sub _set_uid { | ||
| 423 | my ($self) = @_; | ||
| 424 | |||
| 425 | my $uid = $self->_get_uid; | ||
| 426 | |||
| 427 | POSIX::setuid($uid); | ||
| 428 | |||
| 429 | # check $> also (rt #21262) | ||
| 430 | if ($< != $uid || $> != $uid) { | ||
| 431 | |||
| 432 | # try again - needed by some 5.8.0 linux systems (rt #13450) | ||
| 433 | local $< = local $> = $uid; | ||
| 434 | if ($< != $uid) { | ||
| 435 | die "Couldn't become uid \"$uid\": $!\n"; | ||
| 436 | } | ||
| 437 | } | ||
| 438 | |||
| 439 | return 1; | ||
| 440 | } | ||
| 441 | |||
| 442 | sub _set_gid { | ||
| 443 | my ($self) = @_; | ||
| 444 | |||
| 445 | my $gids = $self->_get_gid; | ||
| 446 | my $gid = (split /\s+/, $gids)[0]; | ||
| 447 | |||
| 448 | # store all the gids - this is really sort of optional | ||
| 449 | eval { local $) = $gids }; | ||
| 450 | |||
| 451 | POSIX::setgid($gid); | ||
| 452 | |||
| 453 | # look for any valid gid in the list | ||
| 454 | if (!grep { $gid == $_ } split /\s+/, $() { | ||
| 455 | die "Couldn't become gid \"$gid\": $!\n"; | ||
| 456 | } | ||
| 457 | |||
| 458 | return 1; | ||
| 459 | } | ||
| 460 | |||
| 461 | 1; | ||
| 462 | |||
| 463 | __END__ |