| 1 | package ControlFreak::Proxy; | ||||||||||||||||||||||||||||||||
| 2 | 8 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 3 | 8 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||
| 5 | 8 | ▶ | use AnyEvent::Util(); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | 8 | ▶ | use Carp; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 7 | 8 | ▶ | use ControlFreak::Util; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 8 | 8 | ▶ | use Fcntl qw(F_GETFD F_SETFD FD_CLOEXEC); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 9 | 8 | ▶ | use JSON::XS; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 10 | 8 | ▶ | use Object::Tiny qw{ name cmd pid is_running env auto }; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 11 | 8 | ▶ | use Params::Util qw{ _ARRAY _STRING }; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 12 | 8 | ▶ | use POSIX 'SIGTERM'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 13 | 8 | ▶ | use Scalar::Util(); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 14 | 8 | ▶ | use Try::Tiny; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||
| 16 | =pod | ||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||
| 18 | =head1 NAME | ||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||
| 20 | ControlFreak::Proxy - Delegate some control to an intermediary process. | ||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||
| 22 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||
| 24 | There are some cases where you want some services managed in a special way, | ||||||||||||||||||||||||||||||||
| 25 | and it makes no sense to implement this in C<ControlFreak> itself. | ||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||
| 27 | Indeed, one design trait of B<ControlFreak> is its absolute simplicity, we | ||||||||||||||||||||||||||||||||
| 28 | don't want to clutter it with features that are only rarely used or that | ||||||||||||||||||||||||||||||||
| 29 | could make the controller unstable. | ||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||
| 31 | One example of that is Memory Sharing. If you have 20 application processes | ||||||||||||||||||||||||||||||||
| 32 | running on one machine all having the same code running, there is a | ||||||||||||||||||||||||||||||||
| 33 | memory benefit into making sure the app is loaded in the parent process | ||||||||||||||||||||||||||||||||
| 34 | of all these applications. Indeed, it would allow all children to initially | ||||||||||||||||||||||||||||||||
| 35 | share parent code and thus potentially reduce the memory footprint of the | ||||||||||||||||||||||||||||||||
| 36 | application by quite a while, maybe. But, it's out of question for the | ||||||||||||||||||||||||||||||||
| 37 | C<controller> to load that code in its own memory. A better solution is to use | ||||||||||||||||||||||||||||||||
| 38 | a C<ControlFreak::Proxy> separate process that will: | ||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||
| 40 | =over 4 | ||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||
| 42 | =item * load the application code once and for all | ||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||
| 44 | =item * take commands from the main C<controller> (over pipes) | ||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||
| 46 | =item * fork children when instructed, that exec some user defined commands | ||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||
| 48 | =back | ||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||
| 50 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||
| 52 | $proxy = ControlFreak::Proxy->new( | ||||||||||||||||||||||||||||||||
| 53 | ctrl => $ctrl, | ||||||||||||||||||||||||||||||||
| 54 | cmd => '/usr/bin/cfk-share-mem-proxy.pl --preload Some::Module', | ||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||
| 56 | ); | ||||||||||||||||||||||||||||||||
| 57 | $proxy->add_service($svc); | ||||||||||||||||||||||||||||||||
| 58 | $proxy->destroy_service($svc); | ||||||||||||||||||||||||||||||||
| 59 | $proxy->run; | ||||||||||||||||||||||||||||||||
| 60 | $proxy->start_service($svc); | ||||||||||||||||||||||||||||||||
| 61 | $proxy->stop_service($svc); | ||||||||||||||||||||||||||||||||
| 62 | @list = $proxy->services; | ||||||||||||||||||||||||||||||||
| 63 | $proxy->shutdown; | ||||||||||||||||||||||||||||||||
| 64 | $proxy->is_running; | ||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||
| 66 | =head1 METHODS | ||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||
| 68 | =head2 new(%param) | ||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||
| 70 | =cut | ||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||
| 72 | 3 | ▶ | sub new { | ||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=6 SCAR=19.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 73 | 3 | my $class = shift; | |||||||||||||||||||||||||||||||
| 74 | 3 | my %param = @_; | |||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||
| 76 | 3 | my $ctrl = $param{ctrl}; | |||||||||||||||||||||||||||||||
| 77 | 3 | ▶ | unless ($ctrl) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 78 | 0 | warn "Proxy creation attempt without ctrl"; | |||||||||||||||||||||||||||||||
| 79 | 0 | return; | |||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||
| 82 | 3 | ▶ | unless ($param{name}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 83 | 0 | $ctrl->log->error("Proxy creation attempt without a name"); | |||||||||||||||||||||||||||||||
| 84 | 0 | return; | |||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||
| 87 | 3 | my $proxy = $class->SUPER::new(%param); | |||||||||||||||||||||||||||||||
| 88 | 3 | $proxy->{ctrl} = $ctrl; | |||||||||||||||||||||||||||||||
| 89 | 3 | $proxy->{servicemap} = {}; | |||||||||||||||||||||||||||||||
| 90 | 3 | ▶ | $proxy->{env} ||= {}; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$proxy->{"env"} ||= {}
MC/DC0%0/1
$proxy->{"env"} ||= {}
$proxy->{"env"}
Truth table50%1/2
A
A= $proxy->{"env"}
| |||||||||||||||||||||||||||||||||
| 91 | 3 | ▶ | unless (defined $param{auto}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 92 | 2 | $proxy->{auto} = 1; # proxy is 'auto' by default | |||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||
| 94 | 3 | ▶ | unless ($ctrl->add_proxy($proxy)) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 95 | 0 | $ctrl->log->error("A proxy by that name already exists"); | |||||||||||||||||||||||||||||||
| 96 | 0 | return; | |||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||
| 98 | 3 | Scalar::Util::weaken($proxy->{ctrl}); | |||||||||||||||||||||||||||||||
| 99 | 3 | return $proxy; | |||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||
| 102 | =head2 status_as_text | ||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||
| 104 | Returns the status of the proxy, including its eventual pid in one line of | ||||||||||||||||||||||||||||||||
| 105 | text, where the following fields are seperated with tabs: | ||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||
| 107 | =over 4 | ||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||
| 109 | =item * name | ||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||
| 111 | =item * status ('up' or 'down') | ||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||
| 113 | =item * pid, if proxy is up | ||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||
| 115 | =back | ||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||
| 117 | =cut | ||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||
| 119 | 0 | ▶ | sub status { | ||||||||||||||||||||||||||||||
|
Subroutine
status: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 120 | 0 | my $proxy = shift; | |||||||||||||||||||||||||||||||
| 121 | 0 | ▶ | return $proxy->is_running ? "up" : "down"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||
| 124 | 0 | ▶ | sub status_as_text { | ||||||||||||||||||||||||||||||
|
Subroutine
status_as_text: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 125 | 0 | my $proxy = shift; | |||||||||||||||||||||||||||||||
| 126 | 0 | ▶ | return join "\t", map { $proxy->$_ || "" } qw/name status pid/; | ||||||||||||||||||||||||||||||
|
Condition0%0/2
$proxy->$_ || ''
MC/DC0%0/1
$proxy->$_ || ''
$proxy->$_
Truth table0%0/2
A
A= $proxy->$_
| |||||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||
| 129 | 1 | ▶ | sub _err { ControlFreak::Util::error(@_) } | ||||||||||||||||||||||||||||||
|
Subroutine
_err: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||
| 131 | =head2 services | ||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||
| 133 | Returns a list of L<ControlFreak::Service> objects related to the | ||||||||||||||||||||||||||||||||
| 134 | proxy. | ||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||
| 136 | =cut | ||||||||||||||||||||||||||||||||
| 137 | 6 | ▶ | sub services { | ||||||||||||||||||||||||||||||
|
Subroutine
services: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 138 | 6 | my $proxy = shift; | |||||||||||||||||||||||||||||||
| 139 | 6 | return values %{ $proxy->{servicemap} }; | |||||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||
| 142 | =head2 add_service($svc) | ||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||
| 144 | Declares a service under the control of the proxy. | ||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||
| 146 | =cut | ||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||
| 148 | 3 | ▶ | sub add_service { | ||||||||||||||||||||||||||||||
|
Subroutine
add_service: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 149 | 3 | my $proxy = shift; | |||||||||||||||||||||||||||||||
| 150 | 3 | my $svc = shift; | |||||||||||||||||||||||||||||||
| 151 | 3 | $proxy->{servicemap}->{$svc->name} = $svc; | |||||||||||||||||||||||||||||||
| 152 | 3 | $svc->assign_proxy($proxy); | |||||||||||||||||||||||||||||||
| 153 | 3 | return 1; | |||||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||
| 156 | =head2 start_service | ||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||
| 158 | Given a L<ControlFreak::Service>, check that it is effectively | ||||||||||||||||||||||||||||||||
| 159 | under the control of a L<ControlFreak::Proxy> object and contact the | ||||||||||||||||||||||||||||||||
| 160 | later to instruct it to start the service on our behalf. | ||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||
| 162 | =cut | ||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||
| 164 | 5 | ▶ | sub start_service { | ||||||||||||||||||||||||||||||
|
Subroutine
start_service: called CC=5 SCAR=16.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 165 | 5 | my $proxy = shift; | |||||||||||||||||||||||||||||||
| 166 | 5 | my %param = @_; | |||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||
| 168 | 5 | my $svc = $param{service}; | |||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||
| 170 | 5 | my $name = $svc->name; | |||||||||||||||||||||||||||||||
| 171 | 5 | ▶ | unless ($svc->{proxy} && $svc->{proxy} eq $proxy) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$svc->{"proxy"} and $svc->{"proxy"} eq $proxy
MC/DC0%0/2
$svc->{"proxy"} and $svc->{"proxy"} eq $proxy
$svc->{"proxy"}
$svc->{"proxy"} eq $proxy
Truth table33%1/3
A and B
A= $svc->{"proxy"}B= $svc->{"proxy"} eq $proxy
| |||||||||||||||||||||||||||||||||
| 172 | 0 | return $proxy->_err( | |||||||||||||||||||||||||||||||
| 173 | %param, "Cannot start svc '$name': inappropriate proxy" | ||||||||||||||||||||||||||||||||
| 174 | ); | ||||||||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||||||
| 176 | 5 | ▶ | unless ($proxy->is_running) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 177 | 1 | ▶ | if ($proxy->auto) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 178 | 1 | $proxy->run; | |||||||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||||||
| 180 | else { | ||||||||||||||||||||||||||||||||
| 181 | 0 | return $proxy->_err( | |||||||||||||||||||||||||||||||
| 182 | %param, "Proxy is not running, not starting service '$name'" | ||||||||||||||||||||||||||||||||
| 183 | ); | ||||||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||||||
| 186 | 5 | my $hdl = $proxy->{command_hdl}; | |||||||||||||||||||||||||||||||
| 187 | 5 | my $descr = { | |||||||||||||||||||||||||||||||
| 188 | command => 'start', | ||||||||||||||||||||||||||||||||
| 189 | cmd => $svc->cmd, | ||||||||||||||||||||||||||||||||
| 190 | name => $svc->name, | ||||||||||||||||||||||||||||||||
| 191 | ignore_stderr => $svc->ignore_stderr, | ||||||||||||||||||||||||||||||||
| 192 | ignore_stdout => $svc->ignore_stdout, | ||||||||||||||||||||||||||||||||
| 193 | env => $svc->env, | ||||||||||||||||||||||||||||||||
| 194 | tie_stdin_to => $svc->tie_stdin_to, | ||||||||||||||||||||||||||||||||
| 195 | no_new_session => $svc->no_new_session, | ||||||||||||||||||||||||||||||||
| 196 | }; | ||||||||||||||||||||||||||||||||
| 197 | 5 | my $string = encode_json($descr); | |||||||||||||||||||||||||||||||
| 198 | 5 | $hdl->push_write("$string\n"); | |||||||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||||
| 201 | 1 | ▶ | sub stop_service { | ||||||||||||||||||||||||||||||
|
Subroutine
stop_service: called CC=3 SCAR=11.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 202 | 1 | my $proxy = shift; | |||||||||||||||||||||||||||||||
| 203 | 1 | my %param = @_; | |||||||||||||||||||||||||||||||
| 204 | 1 | my $svc = $param{service}; | |||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||
| 206 | 1 | my $pname = $proxy->name; | |||||||||||||||||||||||||||||||
| 207 | 1 | my $sname = $svc->name; | |||||||||||||||||||||||||||||||
| 208 | 1 | ▶ | return $proxy->_err(%param, "proxy '$pname' not running for '$sname'") | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 209 | unless $proxy->is_running; | ||||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||
| 211 | 1 | my $hdl = $proxy->{command_hdl}; | |||||||||||||||||||||||||||||||
| 212 | 1 | ▶ | unless ($hdl) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 213 | ## TODO: cleanup? | ||||||||||||||||||||||||||||||||
| 214 | 0 | $proxy->{ctrl}->log->error("proxy '$pname' is gone"); | |||||||||||||||||||||||||||||||
| 215 | 0 | return; | |||||||||||||||||||||||||||||||
| 216 | } | ||||||||||||||||||||||||||||||||
| 217 | 1 | my $descr = { | |||||||||||||||||||||||||||||||
| 218 | command => 'stop', | ||||||||||||||||||||||||||||||||
| 219 | name => $svc->name, | ||||||||||||||||||||||||||||||||
| 220 | }; | ||||||||||||||||||||||||||||||||
| 221 | 1 | my $string = encode_json($descr); | |||||||||||||||||||||||||||||||
| 222 | 1 | $hdl->push_write("$string\n"); | |||||||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||||||
| 225 | 0 | ▶ | sub unset { | ||||||||||||||||||||||||||||||
|
Subroutine
unset: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 226 | 0 | my $proxy = shift; | |||||||||||||||||||||||||||||||
| 227 | 0 | my $attr = shift; | |||||||||||||||||||||||||||||||
| 228 | 0 | $proxy->{$attr} = undef; | |||||||||||||||||||||||||||||||
| 229 | 0 | return 1; | |||||||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||||||
| 232 | 0 | ▶ | sub setup_environment { | ||||||||||||||||||||||||||||||
|
Subroutine
setup_environment: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 233 | 0 | my $proxy = shift; | |||||||||||||||||||||||||||||||
| 234 | 0 | my $env = $proxy->env; | |||||||||||||||||||||||||||||||
| 235 | 0 | ▶ | return unless $env; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 236 | 0 | ▶ | return unless ref $env eq 'HASH'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 237 | 0 | ▶ | while (my ($k, $v) = each %$env) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 238 | 0 | $ENV{$k} = $v; | |||||||||||||||||||||||||||||||
| 239 | } | ||||||||||||||||||||||||||||||||
| 240 | 0 | return 1; | |||||||||||||||||||||||||||||||
| 241 | } | ||||||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||||||
| 243 | 0 | ▶ | sub set_add_env { | ||||||||||||||||||||||||||||||
|
Subroutine
set_add_env: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 244 | 0 | my $proxy = shift; | |||||||||||||||||||||||||||||||
| 245 | 0 | ▶ | my $value = _STRING($_[0]) or return; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 246 | 0 | my ($key, $val) = split /=/, $value, 2; | |||||||||||||||||||||||||||||||
| 247 | 0 | $proxy->{ctrl}->log->debug( "Setting ENV{$key} to '$val'" ); | |||||||||||||||||||||||||||||||
| 248 | 0 | $proxy->add_env($key, $val); | |||||||||||||||||||||||||||||||
| 249 | } | ||||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||
| 251 | =head2 add_env($key => $value) | ||||||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||||||
| 253 | Adds an environment key, value pair to the proxy | ||||||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||||||
| 255 | =cut | ||||||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||||||
| 257 | 0 | ▶ | sub add_env { | ||||||||||||||||||||||||||||||
|
Subroutine
add_env: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 258 | 0 | my $proxy = shift; | |||||||||||||||||||||||||||||||
| 259 | 0 | my ($key, $value) = @_; | |||||||||||||||||||||||||||||||
| 260 | 0 | $proxy->env->{$key} = $value; | |||||||||||||||||||||||||||||||
| 261 | 0 | return 1; | |||||||||||||||||||||||||||||||
| 262 | } | ||||||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||||||
| 264 | =head2 clear_env() | ||||||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||||||
| 266 | Resets proxy environment to empty. | ||||||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||||||
| 268 | =cut | ||||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||||
| 270 | 0 | ▶ | sub clear_env { | ||||||||||||||||||||||||||||||
|
Subroutine
clear_env: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 271 | 0 | my $proxy = shift; | |||||||||||||||||||||||||||||||
| 272 | 0 | $proxy->{env} = {}; | |||||||||||||||||||||||||||||||
| 273 | } | ||||||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||||||
| 275 | 2 | ▶ | sub set_cmd { | ||||||||||||||||||||||||||||||
|
Subroutine
set_cmd: called CC=3 SCAR=12.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 276 | 2 | ▶ | my $value = (ref $_[1] ? _ARRAY($_[1]) : _STRING($_[1])) or return; | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 277 | 2 | shift->_set('cmd', $value); | |||||||||||||||||||||||||||||||
| 278 | } | ||||||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||||||
| 280 | 0 | ▶ | sub set_cmd_from_con { | ||||||||||||||||||||||||||||||
|
Subroutine
set_cmd_from_con: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 281 | 0 | my $proxy = shift; | |||||||||||||||||||||||||||||||
| 282 | 0 | my $value = shift; | |||||||||||||||||||||||||||||||
| 283 | 0 | ▶ | return $proxy->unset('cmd') unless defined $value; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 284 | 0 | ▶ | if ($value =~ /^\[/) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 285 | 0 | ▶ | $value = try { decode_json($value) } | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 286 | 0 | ▶ | catch { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 287 | 0 | my $error = $_; | |||||||||||||||||||||||||||||||
| 288 | 0 | $proxy->{ctrl}->log->error("Invalid JSON: $error"); | |||||||||||||||||||||||||||||||
| 289 | 0 | return; | |||||||||||||||||||||||||||||||
| 290 | 0 | }; | |||||||||||||||||||||||||||||||
| 291 | } | ||||||||||||||||||||||||||||||||
| 292 | 0 | return $proxy->set_cmd($value); | |||||||||||||||||||||||||||||||
| 293 | } | ||||||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||||||
| 295 | 0 | ▶ | sub set_desc { | ||||||||||||||||||||||||||||||
|
Subroutine
set_desc: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 296 | 0 | ▶ | my $value = _STRING($_[1]) or return; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 297 | 0 | $value =~ s/[\n\r\t\0]+//g; ## desc should be one line | |||||||||||||||||||||||||||||||
| 298 | 0 | shift->_set('desc', $value); | |||||||||||||||||||||||||||||||
| 299 | } | ||||||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||||||
| 301 | 0 | ▶ | sub set_noauto { | ||||||||||||||||||||||||||||||
|
Subroutine
set_noauto: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 302 | 0 | my $value = _STRING($_[1]); | |||||||||||||||||||||||||||||||
| 303 | 0 | ▶ | return unless defined $value; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 304 | 0 | shift->_set('auto', !$value); | |||||||||||||||||||||||||||||||
| 305 | } | ||||||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||||||
| 307 | 2 | ▶ | sub _set { | ||||||||||||||||||||||||||||||
|
Subroutine
_set: called CC=6 SCAR=18.4
| |||||||||||||||||||||||||||||||||
| 308 | 2 | my $proxy = shift; | |||||||||||||||||||||||||||||||
| 309 | 2 | my ($attr, $value) = @_; | |||||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||||||
| 311 | 2 | my $old = $proxy->$attr; | |||||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||||||
| 313 | 2 | ▶ | my $v = defined $value ? $value : "~"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 314 | 2 | local $Data::Dumper::Indent = 0; | |||||||||||||||||||||||||||||||
| 315 | 2 | local $Data::Dumper::Terse = 1; | |||||||||||||||||||||||||||||||
| 316 | 2 | ▶ | if (ref $v) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 317 | 0 | $v = Data::Dumper::Dumper($v); | |||||||||||||||||||||||||||||||
| 318 | } | ||||||||||||||||||||||||||||||||
| 319 | 2 | ▶ | if ($old) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 320 | 1 | ▶ | my $oldv = defined $old ? $old : "~"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 321 | 1 | ▶ | $oldv = Data::Dumper::Dumper($oldv) if ref $oldv; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 322 | 1 | $proxy->{ctrl}->log->debug( "Changing $attr from '$oldv' to '$v'" ); | |||||||||||||||||||||||||||||||
| 323 | } | ||||||||||||||||||||||||||||||||
| 324 | else { | ||||||||||||||||||||||||||||||||
| 325 | 1 | $proxy->{ctrl}->log->debug( "Setting $attr to '$v'" ); | |||||||||||||||||||||||||||||||
| 326 | } | ||||||||||||||||||||||||||||||||
| 327 | 2 | $proxy->{$attr} = $value; | |||||||||||||||||||||||||||||||
| 328 | 2 | return 1; | |||||||||||||||||||||||||||||||
| 329 | } | ||||||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||||||
| 332 | ## open in the proxy process before exec | ||||||||||||||||||||||||||||||||
| 333 | 0 | ▶ | sub prepare_child_fds { | ||||||||||||||||||||||||||||||
|
Subroutine
prepare_child_fds: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 334 | 0 | my $proxy = shift; | |||||||||||||||||||||||||||||||
| 335 | 0 | my ($cr, $sw, $lw) = @_; | |||||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||||||
| 337 | 0 | $proxy->no_close_on_exec($_) for ($cr, $sw, $lw); | |||||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||||||
| 339 | 0 | $ENV{_CFK_COMMAND_FD} = fileno $cr; | |||||||||||||||||||||||||||||||
| 340 | 0 | $ENV{_CFK_STATUS_FD} = fileno $sw; | |||||||||||||||||||||||||||||||
| 341 | 0 | $ENV{_CFK_LOG_FD} = fileno $lw; | |||||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||||||
| 343 | 0 | $proxy->write_sockets_to_env; | |||||||||||||||||||||||||||||||
| 344 | } | ||||||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||||||
| 346 | 0 | ▶ | sub write_sockets_to_env { | ||||||||||||||||||||||||||||||
|
Subroutine
write_sockets_to_env: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 347 | 0 | my $proxy = shift; | |||||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||||||
| 349 | 0 | my $ctrl = $proxy->{ctrl}; | |||||||||||||||||||||||||||||||
| 350 | 0 | for my $socket ($ctrl->sockets) { | |||||||||||||||||||||||||||||||
| 351 | 0 | ▶ | my $fh = $socket->fh or next; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||||||
| 353 | 0 | $proxy->no_close_on_exec($fh); | |||||||||||||||||||||||||||||||
| 354 | 0 | my $prefix = "_CFK_SOCK_"; | |||||||||||||||||||||||||||||||
| 355 | 0 | my $name = $prefix . $socket->name; | |||||||||||||||||||||||||||||||
| 356 | 0 | $ENV{$name} = fileno $fh; | |||||||||||||||||||||||||||||||
| 357 | } | ||||||||||||||||||||||||||||||||
| 358 | } | ||||||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||||||
| 360 | 0 | ▶ | sub no_close_on_exec { | ||||||||||||||||||||||||||||||
|
Subroutine
no_close_on_exec: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 361 | 0 | my $proxy = shift; | |||||||||||||||||||||||||||||||
| 362 | 0 | my $fh = shift; | |||||||||||||||||||||||||||||||
| 363 | 0 | my $flags = fcntl($fh, F_GETFD, 0); | |||||||||||||||||||||||||||||||
| 364 | 0 | fcntl($fh, F_SETFD, $flags & ~FD_CLOEXEC); | |||||||||||||||||||||||||||||||
| 365 | } | ||||||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||||||
| 367 | =head2 run | ||||||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||||||
| 369 | Runs the proxy command. | ||||||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||||||
| 371 | =cut | ||||||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||||||
| 373 | 5 | ▶ | sub run { | ||||||||||||||||||||||||||||||
|
Subroutine
run: called CC=4 SCAR=15.1
Pod
documented
| |||||||||||||||||||||||||||||||||
| 374 | 5 | my $proxy = shift; | |||||||||||||||||||||||||||||||
| 375 | 5 | my %param = @_; | |||||||||||||||||||||||||||||||
| 376 | 5 | ▶ | my $err = $param{err_cb} ||= sub {}; | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=1.2
Condition33%1/3
$param{"err_cb"} ||= sub {
}
MC/DC0%0/2
$param{"err_cb"} ||= sub {
}
$param{"err_cb"}
sub {
}
Truth table33%1/3
A ||= B
A= $param{"err_cb"}B= sub {
}
| |||||||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||||||
| 378 | 5 | my $name = $proxy->name; | |||||||||||||||||||||||||||||||
| 379 | 5 | ▶ | return $proxy->_err(%param, "Proxy '$name' can't run: no command") | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 380 | unless $proxy->cmd; | ||||||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||||||
| 382 | 4 | $proxy->{is_running} = 1; | |||||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||||||
| 384 | ## Command, Status and Log pipes | ||||||||||||||||||||||||||||||||
| 385 | 4 | my ($cr, $cw) = AnyEvent::Util::portable_pipe; | |||||||||||||||||||||||||||||||
| 386 | 4 | my ($sr, $sw) = AnyEvent::Util::portable_pipe; | |||||||||||||||||||||||||||||||
| 387 | 4 | my ($lr, $lw) = AnyEvent::Util::portable_pipe; | |||||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||||||
| 389 | 4 | AnyEvent::Util::fh_nonblocking($_, 1) for ($sr, $cw, $lr); | |||||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||||||
| 391 | 4 | my $cmd = $proxy->cmd; | |||||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||||||
| 393 | ## XXX redir std /dev/null | ||||||||||||||||||||||||||||||||
| 394 | $proxy->{proxy_cv} = AnyEvent::Util::run_cmd( | ||||||||||||||||||||||||||||||||
| 395 | $cmd, | ||||||||||||||||||||||||||||||||
| 396 | '$$' => \$proxy->{pid}, | ||||||||||||||||||||||||||||||||
| 397 | close_all => 0, | ||||||||||||||||||||||||||||||||
| 398 | 0 | ▶ | on_prepare => sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 399 | 0 | $proxy->setup_environment; | |||||||||||||||||||||||||||||||
| 400 | 0 | $proxy->prepare_child_fds($cr, $sw, $lw); | |||||||||||||||||||||||||||||||
| 401 | }, | ||||||||||||||||||||||||||||||||
| 402 | 4 | ); | |||||||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||||||
| 404 | 3 | ▶ | $proxy->{proxy_cv}->cb( sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=17.1
| |||||||||||||||||||||||||||||||||
| 405 | 3 | my $es = shift()->recv; | |||||||||||||||||||||||||||||||
| 406 | 3 | $proxy->{proxy_cv} = undef; | |||||||||||||||||||||||||||||||
| 407 | 3 | $proxy->{pid} = undef; | |||||||||||||||||||||||||||||||
| 408 | 3 | $proxy->{exit_status} = $es; | |||||||||||||||||||||||||||||||
| 409 | 3 | my $name = $proxy->name; | |||||||||||||||||||||||||||||||
| 410 | 3 | my $state; | |||||||||||||||||||||||||||||||
| 411 | 3 | ▶ | if (POSIX::WIFEXITED($es) && !POSIX::WEXITSTATUS($es)) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
POSIX::WIFEXITED($es) and not POSIX::WEXITSTATUS($es)
MC/DC50%1/2
POSIX::WIFEXITED($es) and not POSIX::WEXITSTATUS($es)
POSIX::WIFEXITED($es)
not POSIX::WEXITSTATUS($es)
Truth table66%2/3
A and not(B)
A= POSIX::WIFEXITED($es)B= not POSIX::WEXITSTATUS($es)
| |||||||||||||||||||||||||||||||||
| 412 | 1 | $proxy->{ctrl}->log->info("proxy '$name' exited"); | |||||||||||||||||||||||||||||||
| 413 | } | ||||||||||||||||||||||||||||||||
| 414 | ▶ | elsif (POSIX::WIFSIGNALED($es) && POSIX::WTERMSIG($es) == SIGTERM) { | |||||||||||||||||||||||||||||||
|
Condition33%1/3
POSIX::WIFSIGNALED($es) and POSIX::WTERMSIG($es) == 15
MC/DC0%0/2
POSIX::WIFSIGNALED($es) and POSIX::WTERMSIG($es) == 15
POSIX::WIFSIGNALED($es)
POSIX::WTERMSIG($es) == 15
Truth table33%1/3
A and B
A= POSIX::WIFSIGNALED($es)B= POSIX::WTERMSIG($es) == 15
| |||||||||||||||||||||||||||||||||
| 415 | 2 | $proxy->{ctrl}->log->info("proxy '$name' gracefully killed"); | |||||||||||||||||||||||||||||||
| 416 | } | ||||||||||||||||||||||||||||||||
| 417 | else { | ||||||||||||||||||||||||||||||||
| 418 | 0 | my $r = ControlFreak::Util::exit_reason($es); | |||||||||||||||||||||||||||||||
| 419 | $proxy->{ctrl}->log->info( | ||||||||||||||||||||||||||||||||
| 420 | 0 | "proxy '$name' abnormal termination " . $r | |||||||||||||||||||||||||||||||
| 421 | ); | ||||||||||||||||||||||||||||||||
| 422 | } | ||||||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||||||
| 424 | 3 | $proxy->has_stopped; | |||||||||||||||||||||||||||||||
| 425 | 4 | }); | |||||||||||||||||||||||||||||||
| 426 | 4 | close $cr; | |||||||||||||||||||||||||||||||
| 427 | 4 | close $sw; | |||||||||||||||||||||||||||||||
| 428 | 4 | close $lw; | |||||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||||||
| 430 | 4 | $proxy->{status_fh} = $sr; | |||||||||||||||||||||||||||||||
| 431 | 4 | $proxy->{log_fh} = $lr; | |||||||||||||||||||||||||||||||
| 432 | 4 | ▶ | $proxy->{status_cv} = AE::io $sr, 0, sub { $proxy->read_status }; | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 433 | 4 | ▶ | $proxy->{log_cv} = AE::io $lr, 0, sub { $proxy->read_log }; | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||||||
| 435 | $proxy->{command_hdl} = AnyEvent::Handle->new( | ||||||||||||||||||||||||||||||||
| 436 | fh => $cw, | ||||||||||||||||||||||||||||||||
| 437 | 0 | ▶ | on_error => sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||
| 438 | 0 | my ($h, $fatal, $message) = @_; | |||||||||||||||||||||||||||||||
| 439 | 0 | ▶ | $proxy->{ctrl}->log->error($message || "unknown proxy error"); | ||||||||||||||||||||||||||||||
|
Condition0%0/2
$message || 'unknown proxy error'
MC/DC0%0/1
$message || 'unknown proxy error'
$message
Truth table0%0/2
A
A= $message
| |||||||||||||||||||||||||||||||||
| 440 | 0 | ▶ | if ($fatal) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 441 | 0 | $proxy->{ctrl}->log->error("Proxy fatal error"); | |||||||||||||||||||||||||||||||
| 442 | 0 | $proxy->shutdown; | |||||||||||||||||||||||||||||||
| 443 | 0 | undef $h; | |||||||||||||||||||||||||||||||
| 444 | } | ||||||||||||||||||||||||||||||||
| 445 | }, | ||||||||||||||||||||||||||||||||
| 446 | 4 | ); | |||||||||||||||||||||||||||||||
| 447 | } | ||||||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||||||
| 449 | =head2 shutdown | ||||||||||||||||||||||||||||||||
| 450 | |||||||||||||||||||||||||||||||||
| 451 | Quits the proxy (and consequently stops all related services). | ||||||||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||||||
| 453 | =cut | ||||||||||||||||||||||||||||||||
| 454 | |||||||||||||||||||||||||||||||||
| 455 | 3 | ▶ | sub shutdown { | ||||||||||||||||||||||||||||||
|
Subroutine
shutdown: called CC=4 SCAR=14.8
Pod
documented
| |||||||||||||||||||||||||||||||||
| 456 | 3 | my $proxy = shift; | |||||||||||||||||||||||||||||||
| 457 | 3 | my %param = @_; | |||||||||||||||||||||||||||||||
| 458 | |||||||||||||||||||||||||||||||||
| 459 | 3 | ▶ | my $ok = $param{ok_cb} ||= sub {}; | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=1.2
Condition33%1/3
$param{"ok_cb"} ||= sub {
}
MC/DC0%0/2
$param{"ok_cb"} ||= sub {
}
$param{"ok_cb"}
sub {
}
Truth table33%1/3
A ||= B
A= $param{"ok_cb"}B= sub {
}
| |||||||||||||||||||||||||||||||||
| 460 | 3 | ▶ | my $err = $param{err_cb} ||= sub {}; | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
Condition33%1/3
$param{"err_cb"} ||= sub {
}
MC/DC0%0/2
$param{"err_cb"} ||= sub {
}
$param{"err_cb"}
sub {
}
Truth table33%1/3
A ||= B
A= $param{"err_cb"}B= sub {
}
| |||||||||||||||||||||||||||||||||
| 461 | |||||||||||||||||||||||||||||||||
| 462 | 3 | my $name = $proxy->name; | |||||||||||||||||||||||||||||||
| 463 | 3 | $proxy->{ctrl}->log->info("shutting down proxy '$name'"); | |||||||||||||||||||||||||||||||
| 464 | 3 | $proxy->{command_hdl} = undef; | |||||||||||||||||||||||||||||||
| 465 | |||||||||||||||||||||||||||||||||
| 466 | 3 | ▶ | if (my $pid = $proxy->pid) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 467 | 3 | kill 'TERM', $pid; | |||||||||||||||||||||||||||||||
| 468 | } | ||||||||||||||||||||||||||||||||
| 469 | ## eventually mark it has dead | ||||||||||||||||||||||||||||||||
| 470 | 0 | ▶ | $proxy->{shutdown_cv} = AE::timer 3, 0, sub { $proxy->has_stopped(1) }; | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||
| 471 | |||||||||||||||||||||||||||||||||
| 472 | 3 | $ok->(); | |||||||||||||||||||||||||||||||
| 473 | 3 | return 1; | |||||||||||||||||||||||||||||||
| 474 | } | ||||||||||||||||||||||||||||||||
| 475 | |||||||||||||||||||||||||||||||||
| 476 | 724 | ▶ | sub read_log { | ||||||||||||||||||||||||||||||
|
Subroutine
read_log: called CC=5 SCAR=16.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 477 | 724 | my $proxy = shift; | |||||||||||||||||||||||||||||||
| 478 | 724 | ▶ | my $log_fh = $proxy->{log_fh} or return; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 479 | 724 | my @logs; | |||||||||||||||||||||||||||||||
| 480 | 724 | ▶ | while (<$log_fh>) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 481 | 6 | push @logs, $_; | |||||||||||||||||||||||||||||||
| 482 | } | ||||||||||||||||||||||||||||||||
| 483 | 724 | for (@logs) { | |||||||||||||||||||||||||||||||
| 484 | 6 | ▶ | next unless $_; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 485 | 6 | $proxy->process_log($_); | |||||||||||||||||||||||||||||||
| 486 | } | ||||||||||||||||||||||||||||||||
| 487 | 724 | return; | |||||||||||||||||||||||||||||||
| 488 | } | ||||||||||||||||||||||||||||||||
| 489 | |||||||||||||||||||||||||||||||||
| 490 | 728 | ▶ | sub read_status { | ||||||||||||||||||||||||||||||
|
Subroutine
read_status: called CC=5 SCAR=16.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 491 | 728 | my $proxy = shift; | |||||||||||||||||||||||||||||||
| 492 | 728 | ▶ | my $status_fh = $proxy->{status_fh} or return; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 493 | 728 | my @statuses; | |||||||||||||||||||||||||||||||
| 494 | 728 | ▶ | while (<$status_fh>) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 495 | 8 | push @statuses, $_; | |||||||||||||||||||||||||||||||
| 496 | } | ||||||||||||||||||||||||||||||||
| 497 | 728 | for (@statuses) { | |||||||||||||||||||||||||||||||
| 498 | 8 | ▶ | next unless $_; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 499 | 8 | $proxy->process_status($_); | |||||||||||||||||||||||||||||||
| 500 | } | ||||||||||||||||||||||||||||||||
| 501 | 728 | return; | |||||||||||||||||||||||||||||||
| 502 | } | ||||||||||||||||||||||||||||||||
| 503 | |||||||||||||||||||||||||||||||||
| 504 | 8 | ▶ | sub process_status { | ||||||||||||||||||||||||||||||
|
Subroutine
process_status: called CC=9 SCAR=22.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 505 | 8 | my $proxy = shift; | |||||||||||||||||||||||||||||||
| 506 | 8 | my $json_data = shift; | |||||||||||||||||||||||||||||||
| 507 | |||||||||||||||||||||||||||||||||
| 508 | 8 | my $ctrl = $proxy->{ctrl}; | |||||||||||||||||||||||||||||||
| 509 | 8 | $ctrl->log->debug("Got a new status: $json_data"); | |||||||||||||||||||||||||||||||
| 510 | |||||||||||||||||||||||||||||||||
| 511 | 8 | my $data = decode_json($json_data); | |||||||||||||||||||||||||||||||
| 512 | |||||||||||||||||||||||||||||||||
| 513 | 8 | my $pname = $proxy->name; | |||||||||||||||||||||||||||||||
| 514 | 8 | ▶ | my $name = $data->{name} || ""; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$data->{'name'} || ''
MC/DC0%0/1
$data->{'name'} || ''
$data->{'name'}
Truth table50%1/2
A
A= $data->{'name'}
| |||||||||||||||||||||||||||||||||
| 515 | 8 | my $svc = $proxy->{servicemap}{$name}; | |||||||||||||||||||||||||||||||
| 516 | 8 | my $status = $data->{status}; | |||||||||||||||||||||||||||||||
| 517 | |||||||||||||||||||||||||||||||||
| 518 | 8 | ▶ | if ($status && $status eq 'started') { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
$status and $status eq "started"
MC/DC50%1/2
$status and $status eq "started"
$status
$status eq "started"
Truth table66%2/3
A and B
A= $statusB= $status eq "started"
| |||||||||||||||||||||||||||||||||
| 519 | 4 | my $pid = $data->{pid}; | |||||||||||||||||||||||||||||||
| 520 | 4 | ▶ | unless ($pid) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 521 | 0 | $ctrl->log->fatal("Started '$name' without pid!"); | |||||||||||||||||||||||||||||||
| 522 | } | ||||||||||||||||||||||||||||||||
| 523 | 4 | $svc->assign_pid( $pid ); | |||||||||||||||||||||||||||||||
| 524 | 4 | $svc->set_check_running_state_timer; | |||||||||||||||||||||||||||||||
| 525 | } | ||||||||||||||||||||||||||||||||
| 526 | ▶ | elsif ($status && $status eq 'stopped') { | |||||||||||||||||||||||||||||||
|
Condition33%1/3
$status and $status eq "stopped"
MC/DC0%0/2
$status and $status eq "stopped"
$status
$status eq "stopped"
Truth table33%1/3
A and B
A= $statusB= $status eq "stopped"
| |||||||||||||||||||||||||||||||||
| 527 | 4 | $svc->acknowledge_exit($data->{exit_status}); | |||||||||||||||||||||||||||||||
| 528 | 4 | ▶ | if ($proxy->auto) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 529 | 1 | my @up = grep { $_->is_up } $proxy->services; | |||||||||||||||||||||||||||||||
| 530 | 1 | ▶ | unless (@up) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 531 | 1 | $proxy->shutdown; | |||||||||||||||||||||||||||||||
| 532 | } | ||||||||||||||||||||||||||||||||
| 533 | } | ||||||||||||||||||||||||||||||||
| 534 | } | ||||||||||||||||||||||||||||||||
| 535 | else { | ||||||||||||||||||||||||||||||||
| 536 | 0 | $ctrl->log->fatal( "Unknown status '$status' sent to proxy '$pname'"); | |||||||||||||||||||||||||||||||
| 537 | } | ||||||||||||||||||||||||||||||||
| 538 | } | ||||||||||||||||||||||||||||||||
| 539 | |||||||||||||||||||||||||||||||||
| 540 | 6 | ▶ | sub process_log { | ||||||||||||||||||||||||||||||
|
Subroutine
process_log: called CC=5 SCAR=18.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 541 | 6 | my $proxy = shift; | |||||||||||||||||||||||||||||||
| 542 | 6 | my $log_data = shift; | |||||||||||||||||||||||||||||||
| 543 | |||||||||||||||||||||||||||||||||
| 544 | 6 | my $ctrl = $proxy->{ctrl}; | |||||||||||||||||||||||||||||||
| 545 | 6 | my ($type, $svcname, $msg) = split ':', $log_data, 3; | |||||||||||||||||||||||||||||||
| 546 | |||||||||||||||||||||||||||||||||
| 547 | 6 | ▶ | if ($svcname && $svcname eq '-') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$svcname and $svcname eq "-"
MC/DC0%0/2
$svcname and $svcname eq "-"
$svcname
$svcname eq "-"
Truth table33%1/3
A and B
A= $svcnameB= $svcname eq "-"
| |||||||||||||||||||||||||||||||||
| 548 | ## this is a proxy log | ||||||||||||||||||||||||||||||||
| 549 | 0 | $ctrl->log->proxy_log([ $type, $proxy, $msg ]); | |||||||||||||||||||||||||||||||
| 550 | 0 | return; | |||||||||||||||||||||||||||||||
| 551 | } | ||||||||||||||||||||||||||||||||
| 552 | 6 | my $svc = $ctrl->service($svcname); | |||||||||||||||||||||||||||||||
| 553 | 6 | ▶ | unless ($svc) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 554 | 6 | ▶ | $svcname ||= ""; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$svcname ||= ""
MC/DC0%0/1
$svcname ||= ""
$svcname
Truth table50%1/2
A
A= $svcname
| |||||||||||||||||||||||||||||||||
| 555 | 6 | chomp $msg; | |||||||||||||||||||||||||||||||
| 556 | 6 | $ctrl->log->error("Cannot find svc '$svcname' for proxy log. [$msg]"); | |||||||||||||||||||||||||||||||
| 557 | 6 | return; | |||||||||||||||||||||||||||||||
| 558 | } | ||||||||||||||||||||||||||||||||
| 559 | 0 | $ctrl->log->proxy_svc_log([ $type, $svc, $msg ]); | |||||||||||||||||||||||||||||||
| 560 | 0 | return; | |||||||||||||||||||||||||||||||
| 561 | } | ||||||||||||||||||||||||||||||||
| 562 | |||||||||||||||||||||||||||||||||
| 563 | =head2 has_stopped | ||||||||||||||||||||||||||||||||
| 564 | |||||||||||||||||||||||||||||||||
| 565 | Called when the proxy has exited. It performs a number | ||||||||||||||||||||||||||||||||
| 566 | of cleaning tasks. | ||||||||||||||||||||||||||||||||
| 567 | |||||||||||||||||||||||||||||||||
| 568 | =cut | ||||||||||||||||||||||||||||||||
| 569 | |||||||||||||||||||||||||||||||||
| 570 | 3 | ▶ | sub has_stopped { | ||||||||||||||||||||||||||||||
|
Subroutine
has_stopped: called CC=4 SCAR=14.2
Pod
documented
| |||||||||||||||||||||||||||||||||
| 571 | 3 | my $proxy = shift; | |||||||||||||||||||||||||||||||
| 572 | 3 | my $finally = shift; | |||||||||||||||||||||||||||||||
| 573 | |||||||||||||||||||||||||||||||||
| 574 | ## ignore if already dead | ||||||||||||||||||||||||||||||||
| 575 | 3 | ▶ | return unless $proxy->{is_running}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 576 | |||||||||||||||||||||||||||||||||
| 577 | ## cancel timer | ||||||||||||||||||||||||||||||||
| 578 | 3 | $proxy->{shutdown_cv} = undef; | |||||||||||||||||||||||||||||||
| 579 | |||||||||||||||||||||||||||||||||
| 580 | 3 | ▶ | if ($finally) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 581 | 0 | my $pname = $proxy->name; | |||||||||||||||||||||||||||||||
| 582 | 0 | $proxy->{ctrl}->log->warn("Proxy '$pname' didn't clean after itself?"); | |||||||||||||||||||||||||||||||
| 583 | } | ||||||||||||||||||||||||||||||||
| 584 | ## not running anymore, obviously | ||||||||||||||||||||||||||||||||
| 585 | 3 | $proxy->{is_running} = 0; | |||||||||||||||||||||||||||||||
| 586 | 3 | $proxy->{pid} = undef; | |||||||||||||||||||||||||||||||
| 587 | |||||||||||||||||||||||||||||||||
| 588 | 3 | $proxy->{proxy_cv} = undef; | |||||||||||||||||||||||||||||||
| 589 | 3 | $proxy->{status_cv} = undef; | |||||||||||||||||||||||||||||||
| 590 | 3 | $proxy->{status_fh} = undef; | |||||||||||||||||||||||||||||||
| 591 | 3 | $proxy->{log_cv} = undef; | |||||||||||||||||||||||||||||||
| 592 | 3 | $proxy->{log_fh} = undef; | |||||||||||||||||||||||||||||||
| 593 | |||||||||||||||||||||||||||||||||
| 594 | ## no matter what, clean the mess | ||||||||||||||||||||||||||||||||
| 595 | 3 | for my $svc ($proxy->services) { | |||||||||||||||||||||||||||||||
| 596 | 3 | $svc->has_stopped("proxy stopped"); | |||||||||||||||||||||||||||||||
| 597 | } | ||||||||||||||||||||||||||||||||
| 598 | } | ||||||||||||||||||||||||||||||||
| 599 | |||||||||||||||||||||||||||||||||
| 600 | 8 | 1; | |||||||||||||||||||||||||||||||