| 1 | package Ereshkigal::App::Command::start; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use 5.006; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use Ereshkigal::App -command; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use Ereshkigal (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use Net::Server::Daemonize qw( daemonize ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | =head1 NAME | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | Ereshkigal::App::Command::start - Start the manager and all the configured kur instances. | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | =head1 VERSION | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | Version 0.0.1 | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | =cut | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 2 | our $VERSION = '0.0.1'; | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | ereshkigal start | ||||||||||||||||||||||||||||
| 25 | ereshkigal start --foreground | ||||||||||||||||||||||||||||
| 26 | ereshkigal start --config /usr/local/etc/ereshkigal.toml | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | Starts the manager and every kur the config defines. The manager reads the | ||||||||||||||||||||||||||||
| 31 | config, brings up a kur process per instance, and listens on its unix | ||||||||||||||||||||||||||||
| 32 | socket. Each kur builds its firewall setup and re-bans whatever its tablet | ||||||||||||||||||||||||||||
| 33 | was carrying, so bans that had not run out survive the restart; anything that | ||||||||||||||||||||||||||||
| 34 | expired while it was down is unbanned instead, in case the firewall still had | ||||||||||||||||||||||||||||
| 35 | the rule. A kur that dies is restarted with a backoff, doubling to a cap of a | ||||||||||||||||||||||||||||
| 36 | minute. | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | It daemonizes unless C<--foreground> is given. Either way it refuses to start | ||||||||||||||||||||||||||||
| 39 | over a manager that is already running, and waits briefly first for a | ||||||||||||||||||||||||||||
| 40 | previous one still shutting down, so restart style invocations do not race. | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | Building firewall state means this generally has to run as root. | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | =head1 METHODS | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | The App::Cmd hooks this subcommand supplies... abstract, description, | ||||||||||||||||||||||||||||
| 47 | usage_desc, opt_spec, validate_args, and execute. | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | =cut | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 1 | ▶ | sub abstract { return 'start the manager and all the configured kur instances' } | ||||||||||||||||||||||||||
|
Subroutine
abstract: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 4 | ▶ | sub description { | ||||||||||||||||||||||||||
|
Subroutine
description: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 54 | 4 | return <<'DESCRIPTION'; | |||||||||||||||||||||||||||
| 55 | Start the manager and every kur the config defines. | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | The manager reads the config, brings up a kur process per instance, and | ||||||||||||||||||||||||||||
| 58 | listens on its unix socket. Each kur builds its firewall setup and re-bans | ||||||||||||||||||||||||||||
| 59 | whatever its tablet was carrying, so bans that had not run out survive the | ||||||||||||||||||||||||||||
| 60 | restart. Anything whose sentence expired while it was down is unbanned | ||||||||||||||||||||||||||||
| 61 | instead, in case the firewall still had the rule. | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | A kur that dies is restarted with a backoff, doubling to a cap of a | ||||||||||||||||||||||||||||
| 64 | minute. | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | It daemonizes unless -f is given. Either way it refuses to start over a | ||||||||||||||||||||||||||||
| 67 | manager that is already running, and waits briefly first for a previous | ||||||||||||||||||||||||||||
| 68 | one that is still shutting down, so "restart" style invocations do not | ||||||||||||||||||||||||||||
| 69 | race. | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | This needs to be able to build firewall state, which in practice means | ||||||||||||||||||||||||||||
| 72 | root. | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | ereshkigal start | ||||||||||||||||||||||||||||
| 75 | ereshkigal start -f --config ./ereshkigal.toml # foreground, for a look | ||||||||||||||||||||||||||||
| 76 | DESCRIPTION | ||||||||||||||||||||||||||||
| 77 | } ## end sub description | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 5 | ▶ | sub usage_desc { return '%c start %o'; } | ||||||||||||||||||||||||||
|
Subroutine
usage_desc: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 5 | ▶ | sub opt_spec { | ||||||||||||||||||||||||||
|
Subroutine
opt_spec: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 82 | return ( | ||||||||||||||||||||||||||||
| 83 | 5 | [ 'config=s', 'path of the config file', { default => '/usr/local/etc/ereshkigal.toml' } ], | |||||||||||||||||||||||||||
| 84 | [ 'foreground|f', 'do not daemonize' ], | ||||||||||||||||||||||||||||
| 85 | ); | ||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 1 | ▶ | sub validate_args { | ||||||||||||||||||||||||||
|
Subroutine
validate_args: called CC=2 SCAR=7.4
Pod
documented
| |||||||||||||||||||||||||||||
| 89 | 1 | my ( $self, $opt, $args ) = @_; | |||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 1 | ▶ | if ( @{$args} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | 1 | $self->usage_error('start does not take any args'); | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 0 | return; | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 0 | ▶ | sub execute { | ||||||||||||||||||||||||||
|
Subroutine
execute: not called CC=10 SCAR=47.0
Pod
documented
| |||||||||||||||||||||||||||||
| 99 | 0 | my ( $self, $opt, $args ) = @_; | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 0 | my $ereshkigal = Ereshkigal->new( 'config' => $opt->config ); | |||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | # A previous manager may still be tearing down. `ereshkigal stop`, and thus | ||||||||||||||||||||||||||||
| 104 | # `service ereshkigal restart`, returns as soon as the shutdown is requested, | ||||||||||||||||||||||||||||
| 105 | # so the following start can race the old process. The manager unlinks its | ||||||||||||||||||||||||||||
| 106 | # PID file only once it has fully exited, so wait briefly for a live PID to | ||||||||||||||||||||||||||||
| 107 | # clear rather than letting daemonize() abort with | ||||||||||||||||||||||||||||
| 108 | # "Pid_file already exists for running process". | ||||||||||||||||||||||||||||
| 109 | 0 | $self->_wait_for_pid_clear( $ereshkigal->pid_path ); | |||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 0 | ▶ | if ( $opt->foreground ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | # the daemonize branch gets this from daemonize() itself, so make sure | ||||||||||||||||||||||||||||
| 113 | # the foreground branch does not clobber the PID file of a live manager | ||||||||||||||||||||||||||||
| 114 | 0 | ▶ | if ( -e $ereshkigal->pid_path && open( my $old_pid_fh, '<', $ereshkigal->pid_path ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
-e $ereshkigal->pid_path and open my $old_pid_fh, "<", $ereshkigal->pid_path
MC/DC0%0/2
-e $ereshkigal->pid_path and open my $old_pid_fh, "<", $ereshkigal->pid_path
-e $ereshkigal->pid_path
open my $old_pid_fh, "<", $ereshkigal->pid_path
Truth table0%0/3
A and B
A= -e $ereshkigal->pid_pathB= open my $old_pid_fh, "<", $ereshkigal->pid_path
| |||||||||||||||||||||||||||||
| 115 | 0 | my $old_pid = <$old_pid_fh>; | |||||||||||||||||||||||||||
| 116 | 0 | close($old_pid_fh); | |||||||||||||||||||||||||||
| 117 | 0 | ▶ | if ( defined($old_pid) && $old_pid =~ /^\s*([0-9]+)\s*$/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $old_pid and $old_pid =~ /^\s*([0-9]+)\s*$/
MC/DC0%0/2
defined $old_pid and $old_pid =~ /^\s*([0-9]+)\s*$/
defined $old_pid
$old_pid =~ /^\s*([0-9]+)\s*$/
Truth table0%0/3
A and B
A= defined $old_pidB= $old_pid =~ /^\s*([0-9]+)\s*$/
| |||||||||||||||||||||||||||||
| 118 | 0 | $old_pid = $1; | |||||||||||||||||||||||||||
| 119 | # kill 0 is true while the process exists, and EPERM means it | ||||||||||||||||||||||||||||
| 120 | # exists but is not ours to signal | ||||||||||||||||||||||||||||
| 121 | 0 | ▶ | if ( kill( 0, $old_pid ) || $!{EPERM} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
kill 0, $old_pid or $!{"EPERM"}
MC/DC0%0/2
kill 0, $old_pid or $!{"EPERM"}
kill 0, $old_pid
$!{"EPERM"}
Truth table0%0/3
A or B
A= kill 0, $old_pidB= $!{"EPERM"}
| |||||||||||||||||||||||||||||
| 122 | 0 | die( 'A manager already appears to be running with PID ' | |||||||||||||||||||||||||||
| 123 | . $old_pid | ||||||||||||||||||||||||||||
| 124 | . ' per the PID file "' | ||||||||||||||||||||||||||||
| 125 | . $ereshkigal->pid_path | ||||||||||||||||||||||||||||
| 126 | . '"' ); | ||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | } ## end if ( defined($old_pid) && $old_pid =~ /^\s*([0-9]+)\s*$/) | ||||||||||||||||||||||||||||
| 129 | } ## end if ( -e $ereshkigal->pid_path && open( my ...)) | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 0 | ▶ | open( my $pid_fh, '>', $ereshkigal->pid_path ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | || die( 'Failed to open the PID file "' . $ereshkigal->pid_path . '"... ' . $! ); | ||||||||||||||||||||||||||||
| 133 | 0 | print $pid_fh $$; | |||||||||||||||||||||||||||
| 134 | 0 | close($pid_fh); | |||||||||||||||||||||||||||
| 135 | } else { | ||||||||||||||||||||||||||||
| 136 | 0 | daemonize( $>, ( split( /\s+/, $) ) )[0], $ereshkigal->pid_path ); | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 0 | $ereshkigal->start_server; | |||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 0 | ▶ | unlink( $ereshkigal->pid_path ) if -e $ereshkigal->pid_path; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 0 | return; | |||||||||||||||||||||||||||
| 144 | } ## end sub execute | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | # Waits, briefly, for a previous manager to finish going away, so that a | ||||||||||||||||||||||||||||
| 147 | # restart does not race its own predecessor. This exists because | ||||||||||||||||||||||||||||
| 148 | # "ereshkigal stop" returns as soon as the shutdown has been requested | ||||||||||||||||||||||||||||
| 149 | # rather than once it has finished, which means "service ereshkigal | ||||||||||||||||||||||||||||
| 150 | # restart", and anything else that stops and immediately starts, can reach | ||||||||||||||||||||||||||||
| 151 | # start while the old manager is still tearing its kurs down. Without this | ||||||||||||||||||||||||||||
| 152 | # the start aborts with daemonize's "Pid_file already exists for running | ||||||||||||||||||||||||||||
| 153 | # process" for a manager that was a half second from being gone. | ||||||||||||||||||||||||||||
| 154 | # | ||||||||||||||||||||||||||||
| 155 | # It polls rather than watching for anything, up to forty times with a | ||||||||||||||||||||||||||||
| 156 | # quarter second between, so a little over ten seconds in the worst case. It | ||||||||||||||||||||||||||||
| 157 | # returns the moment any of four things is true... the PID file is gone, it | ||||||||||||||||||||||||||||
| 158 | # cannot be opened, it holds nothing that looks like a PID, or the PID it | ||||||||||||||||||||||||||||
| 159 | # holds names a process that is neither running nor merely unsignalable by | ||||||||||||||||||||||||||||
| 160 | # us. The first three are all a stale PID file, which daemonize happily | ||||||||||||||||||||||||||||
| 161 | # reclaims on its own. | ||||||||||||||||||||||||||||
| 162 | # | ||||||||||||||||||||||||||||
| 163 | # Deliberately does not decide anything. If a live manager is still there | ||||||||||||||||||||||||||||
| 164 | # when the polling runs out it returns anyway and leaves refusing the start | ||||||||||||||||||||||||||||
| 165 | # to daemonize, or to the foreground branch's own liveness check... this only | ||||||||||||||||||||||||||||
| 166 | # ever buys time, and never turns a running manager into a startable one. | ||||||||||||||||||||||||||||
| 167 | # | ||||||||||||||||||||||||||||
| 168 | # Args, one, positional and required... | ||||||||||||||||||||||||||||
| 169 | # | ||||||||||||||||||||||||||||
| 170 | # $pid_path :: Full path of the manager PID file to watch, in practice | ||||||||||||||||||||||||||||
| 171 | # always $ereshkigal->pid_path. It need not exist... a | ||||||||||||||||||||||||||||
| 172 | # missing file is the state being waited for and returns at | ||||||||||||||||||||||||||||
| 173 | # once. | ||||||||||||||||||||||||||||
| 174 | # | ||||||||||||||||||||||||||||
| 175 | # Returns nothing meaningful, an empty return. There is no telling from the | ||||||||||||||||||||||||||||
| 176 | # return whether the wait succeeded or timed out, deliberately, as the | ||||||||||||||||||||||||||||
| 177 | # caller does not act on it either way. | ||||||||||||||||||||||||||||
| 178 | # | ||||||||||||||||||||||||||||
| 179 | # $self->_wait_for_pid_clear( $ereshkigal->pid_path ); | ||||||||||||||||||||||||||||
| 180 | # # returns at once if nothing is running, or after up to ~10s if a | ||||||||||||||||||||||||||||
| 181 | # # previous manager is still shutting down | ||||||||||||||||||||||||||||
| 182 | 0 | ▶ | sub _wait_for_pid_clear { | ||||||||||||||||||||||||||
|
Subroutine
_wait_for_pid_clear: not called CC=8 SCAR=42.8
| |||||||||||||||||||||||||||||
| 183 | 0 | my ( $self, $pid_path ) = @_; | |||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | 0 | for ( 1 .. 40 ) { | |||||||||||||||||||||||||||
| 186 | 0 | ▶ | last if !-e $pid_path; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | 0 | ▶ | open( my $pid_fh, '<', $pid_path ) or last; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 189 | 0 | my $pid = <$pid_fh>; | |||||||||||||||||||||||||||
| 190 | 0 | close($pid_fh); | |||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | 0 | ▶ | last if !defined($pid) || $pid !~ /^\s*([0-9]+)\s*$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $pid and $pid =~ /^\s*([0-9]+)\s*$/
MC/DC0%0/2
defined $pid and $pid =~ /^\s*([0-9]+)\s*$/
defined $pid
$pid =~ /^\s*([0-9]+)\s*$/
Truth table0%0/3
A and B
A= defined $pidB= $pid =~ /^\s*([0-9]+)\s*$/
| |||||||||||||||||||||||||||||
| 193 | 0 | $pid = $1; | |||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | # kill 0 is true while the process exists, and EPERM means it exists | ||||||||||||||||||||||||||||
| 196 | # but is not ours to signal; once both go false the old manager is | ||||||||||||||||||||||||||||
| 197 | # gone. matching the foreground branch, which checks EPERM the same | ||||||||||||||||||||||||||||
| 198 | # way, so a manager owned by another user is still waited on. | ||||||||||||||||||||||||||||
| 199 | 0 | ▶ | last if !kill( 0, $pid ) && !$!{EPERM}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
kill 0, $pid or $!{"EPERM"}
MC/DC0%0/2
kill 0, $pid or $!{"EPERM"}
kill 0, $pid
$!{"EPERM"}
Truth table0%0/3
A or B
A= kill 0, $pidB= $!{"EPERM"}
| |||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | # 0.25s nap without pulling in Time::HiRes. | ||||||||||||||||||||||||||||
| 202 | 0 | select( undef, undef, undef, 0.25 ); ## no critic (BuiltinFunctions::ProhibitSleepViaSelect) | |||||||||||||||||||||||||||
| 203 | } ## end for ( 1 .. 40 ) | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | 0 | return; | |||||||||||||||||||||||||||
| 206 | } ## end sub _wait_for_pid_clear | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | 2 | 1; | |||||||||||||||||||||||||||