ReslirpTunnel.pm

filter: Statementstmt 83.3% 10 / 12 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 87.5% 14 / 16 CC 1 SCAR 0.7
CC
1
Cov
83%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« ReslirpTunnel/RPC.pm ↑ Summary
1 package App::ReslirpTunnel;
2  
3 our $VERSION = '0.06';
4  
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 0 ▶use Data::Validate::Domain qw(is_hostname);
Subroutine BEGIN: called CC=1 SCAR=2.6
10 use Data::Validate::IP qw(is_ipv4);
11 use Path::Tiny;
12 use File::XDG;
13 use POSIX;
14 use Net::OpenSSH;
15 use JSON::PP;
16  
17 use parent 'App::ReslirpTunnel::Logger';
18  
19 use App::ReslirpTunnel::Butler;
20 use App::ReslirpTunnel::Loop;
21  
22 sub new {
23 my ($class, %args) = @_;
24 my $self = { args => \%args };
25 bless $self, $class;
26 return $self;
27 }
28  
29 sub go {
30 my $self = shift;
31  
32 eval {
33 $self->_init_xdg;
34 $self->_init_time;
35 $self->_init_logger;
36 $self->_log(info => "Starting ReslirpTunnel");
37 $self->_set_signal_handlers;
38 $self->_init_config;
39 $self->_init_butler;
40 $self->_init_ssh;
41 $self->_send_to_background;
42 $self->_init_tap_device;
43 $self->_init_reslirp;
44 $self->_init_loop;
45 $self->_config_forward_dns;
46 $self->_config_net_mappings;
47 $self->_init_dnsmasq;
48 $self->_init_resolver_rules;
49 $self->_init_routes;
50 $self->_wait_for_something;
51 $self->_log(info => "Terminating ReslirpTunnel");
52 };
53 if ($@) {
54 die "Something went wrong: $@\n";
55 }
56 $self->_kill_everything;
57 }
58  
59 sub _init_xdg {
60 my $self = shift;
61 my $app_name = $self->{args}{app_name} or die "App name missing, unable to initialize XDG helper";
62 $self->{xdg} = File::XDG->new(name => $app_name, path_class => 'Path::Tiny');
63 }
64  
65 sub _init_time {
66 my $self = shift;
67 $self->{timestamp} = POSIX::strftime("%Y%m%dT%H%M%SZ", gmtime);
68 }
69  
70 sub _init_logger {
71 my $self = shift;
72 my $level = $self->{args}{log_level};
73 my $log_to_stderr = $self->{args}{log_to_stderr};
74 my $fn = $self->{args}{log_file};
75 unless (defined $fn) {
76 my $parent_dir = $self->{xdg}->state_home->child('logs')->mkdir;
77 $fn = $parent_dir->child($self->{timestamp}.".reslirp-tunnel.log");
78 eval {
79 my $sl = $parent_dir->child('latest.reslirp-tunnel.log');
80 unlink $sl if -l $sl;
81 symlink $fn, $sl;
82 };
83 }
84 $self->SUPER::_init_logger(log_level => $level, log_to_stderr => $log_to_stderr, log_file => $fn);
85 }
86  
87 sub _set_signal_handlers {
88 my $self = shift;
89 my $signal_count = 0;
90 $self->{signal_count_ref} = \$signal_count;
91 $self->{signal_handler} = sub {
92 $signal_count++;
93 $self->_log(info => "Signal received, count: $signal_count");
94 };
95  
96 $SIG{INT} = $self->{signal_handler};
97 $SIG{TERM} = $self->{signal_handler};
98 }
99  
100 sub _init_config {
101 my $self = shift;
102 my $args = $self->{args};
103  
104 $self->{run_in_foreground} = $args->{run_in_foreground} // 0;
105 $self->{dont_close_stdio} = $args->{dont_close_stdio} // 0;
106  
107 $self->{ssh_host} = $args->{ssh_host};
108 $self->{ssh_port} = $args->{ssh_port};
109 $self->{ssh_user} = $args->{ssh_user};
110  
111 $self->{remote_network} = $args->{remote_network} // '10.0.2.0';
112 is_ipv4($self->{remote_network}) or $self->_die("Invalid remote network address, $self->{remote_network}");
113  
114 $self->{remote_netmask} = $self->_parse_netmask($args->{remote_netmask} // 24);
115 $self->{remote_dns} = $self->_parse_ip($args->{remote_dns});
116 $self->{remote_gw} = $self->_parse_ip($args->{remote_gw});
117 $self->{local_ip} = $self->_parse_ip($args->{local_ip});
118 }
119  
120 sub _parse_netmask {
121 my ($self, $netmask) = @_;
122 ($netmask =~ /^\d+$/ && $netmask >= 1 && $netmask <= 31) or $self->_die("Invalid netmask", $netmask);
123 return $netmask;
124 }
125  
126 sub _parse_ip {
127 my ($self, $ip) = @_;
128 my $network = $self->{remote_network};
129 my $netmask = $self->{remote_netmask};
130 if ($ip =~ /^\d+$/) {
131 $ip = $network =~ s/\d+$/$ip/r;
132 }
133 is_ipv4($ip) or $self->_die("Invalid IP address", $ip);
134  
135 my $ip_int = __ip_to_int($ip);
136 my $net_int = __ip_to_int($network);
137 my $bitmask = ~0 << (32 - $netmask);
138  
139 unless (($ip_int & $bitmask) == ($ip_int & $bitmask)) {
140 $self->_die("IP address $ip is not inside remote network $network/$netmask");
141 }
142  
143 return $ip;
144 }
145  
146 sub __ip_to_int {
147 my $ip = shift;
148 return unpack("N", pack("C*", split(/\./, $ip)));
149 }
150  
151 sub _init_butler {
152 my $self = shift;
153 my $butler = $self->{butler} = App::ReslirpTunnel::Butler->new(dont_close_stdio => $self->{dont_close_stdio},
154 log_level => $self->{log_level},
155 log_to_stderr => $self->{log_to_stderr},
156 log_file => $self->{log_file});
157  
158 $butler->start or $self->_die("Failed to start butler");
159 $butler->hello
160 or $self->_die("Failed to say hello to butler");
161 $self->_log(info => "Elevated slave process started and ready");
162 }
163  
164 sub _send_to_background {
165 my $self = shift;
166 return if $self->{run_in_foreground};
167  
168 $self->_log(info => "Moving to background");
169 POSIX::setsid();
170  
171 my $pid = fork // $self->_die("Unable to move process into the background", $!);
172 if ($pid == 0) {
173 $SIG{INT} = $self->{signal_handler};
174 $SIG{TERM} = $self->{signal_handler};
175  
176 unless ($self->{dont_close_stdio}) {
177 open STDIN, '<', '/dev/null';
178 open STDOUT, '>', '/dev/null';
179 open STDERR, '>', '/dev/null' unless $self->{log_to_stderr};
180 }
181  
182 $self->{log_prefix} = "ReslirpTunnel::Child";
183  
184 return 1; # Return in the child!!!
185 }
186 else {
187 eval {
188 syswrite STDERR, "$0 moved to background, PID: $pid\n";
189 $self->_log(debug => "First process exiting");
190 };
191  
192 POSIX::_exit(0);
193 }
194 }
195  
196 sub _init_ssh {
197 my $self = shift;
198 my $host = $self->{ssh_host} // $self->_die("No remote host specified");
199 my $port = $self->{ssh_port};
200 my $user = $self->{ssh_user};
201 my $cmd = $self->{args}{ssh_command};
202 my $more_args = $self->{args}{more_ssh_args};
203 my @args = (host => $host);
204 push @args, (port => $port) if defined $port;
205 push @args, (user => $user) if defined $user;
206 push @args, (ssh_cmd => $cmd) if defined $cmd;
207 push @args, (master_opts => $more_args) if defined $more_args;
208 $self->{ssh} = my $ssh = Net::OpenSSH->new(@args);
209 $ssh->error and
210 $self->_die("Unable to connect to remote host", $ssh->error);
211 $self->{remote_os} = $self->{args}{remote_os} // $self->_autodetect_remote_os //
212 $self->_die("No remote OS specified and unable to autodetect it");
213 $self->{remote_shell} = $self->{args}{remote_shell} // $self->_autodetect_remote_shell //
214 $self->_die("No remote shell specified and unable to autodetect it");
215  
216 $self->{quoting_backend} = (($self->{remote_shell} eq 'windows') ? 'MSWin' : 'ksh');
217 my $ssh_master_pid = $self->{ssh}->get_master_pid;
218 $self->_log(debug => "SSH master PID", $ssh_master_pid);
219 $self->{ssh_master_pid} = $ssh_master_pid;
220 }
221  
222 sub _autodetect_remote_os {
223 my $self = shift;
224 my $ssh = $self->{ssh};
225 my $out = $ssh->capture('echo %COMSPEC%');
226 my $looks_like_unix = $out =~ /^\%COMSPEC\%$/m;
227 if ($looks_like_unix) {
228 $self->_log(debug => "Looks like a Unix-like system, let's check it further...");
229 my $uname = lc $ssh->capture('uname -s');
230 if ($uname =~ /^(Linux|Darwin|FreeBSD|OpenBSD|NetBSD|DragonFly|MidnightBSD|AIX|HP-UX|SunOS|IRIX|OSF1|SCO_SV|QNX)$/i) {
231 $self->_log(info => "Remote OS identified as Linux/UNIX ($1)");
232 return 'unix';
233 }
234 }
235 else {
236 $self->_log(debug => "Looks like Windows, let's check it further...");
237 my $ver = $ssh->capture('ver');
238 if ($ver =~ /^(Microsoft Windows \[Version.*\])/m) {
239 $self->_log(info => "Remote OS identified as Windows ($1)");
240 return 'windows';
241 }
242 }
243 $self->_warn("Unable to autodetect remote OS");
244 return;
245 }
246  
247 sub _autodetect_remote_shell {
248 my $self = shift;
249 if ($self->{remote_os} eq 'windows') {
250 return $self->{remote_shell} = 'windows';
251 }
252 my $ssh = $self->{ssh};
253 my $out = $ssh->capture('echo $SHELL') or return "sh";
254 chomp($out);
255 return Path::Tiny->new($out)->basename;
256 }
257  
258 sub _init_tap_device {
259 my $self = shift;
260 my $butler = $self->{butler};
261 my $device = $self->{tap_device} = $self->{args}{device} // $self->_find_unused_tap_device;
262 $self->{tap_fh} = $butler->create_tap($device);
263 $butler->device_up($device)
264 or $self->_die("Failed to bring up tap device $device");
265  
266 my $host = $self->{local_ip};
267 my $mask = $self->{remote_netmask};
268 $butler->device_addr_add($device, $host, $mask)
269 or $self->_die("Failed to add address $host/$mask to tap device $device");
270 $self->_log(info => "Tap device $device created and configured");
271 1;
272 }
273  
274 sub _init_reslirp {
275 my $self = shift;
276 my $ssh = $self->{ssh};
277 my $cmd = $self->{reslirp_command} = $self->{args}{reslirp_command} // $self->_autodetect_reslirp_command;
278 my @args = @{$self->{args}{more_reslirp_args}};
279 $self->_log(info => "Starting remote reSLIRP process");
280 $self->_log(debug => "Remote command: $cmd @args");
281 my ($socket, undef, $stderr, $pid) = $ssh->open_ex({stderr_pipe => 1,
282 stdinout_socket => 1,
283 quote_args => 1,
284 remote_shell => $self->{quoting_backend}},
285 $cmd, @args);
286 $self->{reslirp_socket} = $socket;
287 $self->{reslirp_stderr} = $stderr;
288 $self->{reslirp_pid} = $pid;
289 $pid or $self->_die("Failed to start reSLIRP process");
290 $self->_log(info => "reSLIRP process started");
291 }
292  
293 sub _autodetect_reslirp_command {
294 my $self = shift;
295 if ($self->{remote_os} eq 'windows') {
296 return 'C:\Program Files\reSLIRP\reslirp.exe';
297 }
298 return "reslirp";
299 }
300  
301 sub _find_unused_tap_device {
302 my $self = shift;
303 my $n = 0;
304 for my $n (0..100) {
305 my $device = "tap$n";
306 -e "/dev/$device" or return $device;
307 }
308 $self->_die("Unable to find an unused tap device");
309 }
310  
311 sub _config_forward_dns {
312 my $self = shift;
313 $self->{forward_dns} //= {};
314 $self->{forward_ipv4} //= {};
315 $self->_config_forward_dns_ssh;
316 }
317  
318 sub _config_forward_dns_ssh {
319 my $self = shift;
320 for my $record (@{$self->{args}{forward_dns_ssh}}) {
321 $self->_log(debug => "Retrieving iface DNS servers using remote shell");
322 my $domain = $record->{domain};
323 my $iface = $record->{iface};
324 my $method = "_resolve_remote_iface_dns__". (($self->{remote_os} eq 'windows') ? 'windows' : 'unix');
325 if (my @addrs = $self->$method($iface)) {
326 $self->_log(debug => "DNS servers for remote iface $iface", join(", ", @addrs));
327 for my $addr (@addrs) {
328 push @{$self->{forward_dns}{$domain} //= []}, $addr;
329 $self->{forward_ipv4}{"$addr/32"} = 1;
330 }
331 }
332 else {
333 $self->_warn("Failed to retrieve DNS servers using remote shell, ignoring domain", $record->{domain});
334 }
335 }
336 }
337  
338 sub _resolve_remote_iface_dns__unix {
339 my $self = shift;
340 $self->_warn('Retrieving by iface DNS servers using the shell on remote Unix hosts is not implemented yet');
341 ()
342 }
343  
344 sub _resolve_remote_iface_dns__windows {
345 my ($self, $iface) = @_;
346 my $ssh = $self->{ssh};
347  
348 my $out = $ssh->capture({remote_shell=> 'MSWin'}, 'powershell', '-Command', "Get-DnsClientServerAddress | ConvertTo-Json");
349 my @addrs;
350 eval {
351 for my $record (@{JSON::PP::decode_json($out)}) {
352 if ($record->{InterfaceAlias} eq $iface and
353 $record->{AddressFamily} eq '2') {
354 push @addrs, @{$record->{ServerAddresses}};
355 }
356 }
357 };
358 unless (@addrs) {
359 $self->_warn("Failed to parse JSON output from DnsClientServerAddress", $@);
360 $self->_log(debug => "Output was", $out);
361 }
362 return @addrs;
363 }
364  
365  
366 sub _config_net_mappings {
367 my $self = shift;
368 $self->{net_mapping} //= {};
369 $self->{forward_ipv4} //= {};
370 $self->_config_net_mappings_net;
371 $self->_config_net_mappings_direct;
372 $self->_config_net_mappings_local;
373 $self->_config_net_mappings_dns;
374 $self->_config_net_mappings_ssh;
375 }
376  
377 sub _config_net_mappings_net {
378 my $self = shift;
379 for my $record (@{$self->{args}{route_nets}}) {
380 my $addr = $record->{addr};
381 my $mask = $record->{mask};
382 if ($self->_validate_ipv4($addr) and $self->_validate_netmask($mask)) {
383 $self->{forward_ipv4}{"$addr/$mask"} = 1;
384 }
385 else {
386 $self->_warn("Ignoring invalid network", "$addr/$mask");
387 }
388 }
389 }
390  
391 sub _config_net_mappings_direct {
392 my $self = shift;
393 for my $record (@{$self->{args}{route_hosts}}) {
394 my $addrs = $record->{addrs} // [];
395 $self->{forward_ipv4}{"$_/32"} = 1 for @$addrs;
396 if (defined (my $host = $record->{host})) {
397 if ($self->_validate_domain_name($host)) {
398 push @{$self->{net_mapping}{$host} //= []}, @$addrs;
399 }
400 else {
401 $self->_warn("Ignoring host with invalid name", $host);
402 }
403 }
404 }
405 }
406  
407 sub _config_net_mappings_local {
408 my $self = shift;
409 for my $host (@{$self->{args}{route_hosts_local}}) {
410 my $addr;
411 if (is_ipv4($host)) {
412 $self->{forward_ipv4}{"$host/32"} = 1;
413 }
414 elsif ($self->_validate_domain_name($host)) {
415 my $good;
416 my ($err, @records) = Socket::getaddrinfo($host);
417 unless ($err) {
418 for my $record (@records) {
419 if ($record->{family} == AF_INET) {
420 my (undef, $packed_ip) = sockaddr_in($record->{addr});
421 my $addr = inet_ntoa($packed_ip);
422 push @{$self->{net_mapping}{$host} //= []}, $addr;
423 $self->{forward_ipv4}{"$addr/32"} = 1;
424 $good = 1;
425 }
426 }
427 }
428 $good or $self->_warn("Failed to resolve host, ignoring it", $host);
429 }
430 else {
431 $self->_warn("Ignoring host with invalid name", $host);
432 }
433 }
434 }
435  
436 sub _validate_ipv4 {
437 my ($self, $ipv4) = @_;
438 is_ipv4($ipv4) and return 1;
439 $self->_log(debug => "Bad IPv4", $ipv4);
440 return undef;
441 }
442  
443 sub _validate_netmask {
444 my ($self, $mask) = @_;
445 $mask =~ /\d+/ and $mask >= 1 and $mask <= 32 and return 1;
446 $self->_log(debug => "Bad netmask", $mask);
447 return undef;
448 }
449  
450 sub _validate_domain_name {
451 my ($self, $domain) = @_;
452 is_hostname($domain, {'domain_private_tld' => 1}) and return 1;
453 $self->_log(debug => "Bad domain", $domain);
454 return undef;
455 }
456  
457 sub _config_net_mappings_dns {
458 my $self = shift;
459 my $route_hosts = $self->{args}{route_hosts_dns};
460 if (@$route_hosts) {
461 my $dns = Net::DNS::Resolver->new(nameservers => [$self->{remote_dns}], recurse => 1);
462 for my $host (@$route_hosts) {
463 if ($self->_validate_domain_name($host)) {
464 my $good;
465 $self->_log(debug => "Resolving $host using remote DNS");
466 my $query = $dns->query($host, 'A');
467 if ($query) {
468 for my $rr ($query->answer) {
469 if ($rr->type eq 'A') {
470 my $addr = $rr->address;
471 push @{$self->{net_mapping}{$host} //= []}, $addr;
472 $self->{forward_ipv4}{"$addr/32"} = 1;
473 $good = 1;
474 }
475 }
476 }
477 $good or $self->_warn("Failed to resolve host using remote DNS, ignoring it", $host);
478 }
479 else {
480 $self->_warn("Ignoring host with invalid name", $host);
481 }
482 }
483 }
484 }
485  
486 sub _config_net_mappings_ssh {
487 my $self = shift;
488 my $route_hosts = $self->{args}{route_hosts_ssh};
489 for my $host (@$route_hosts) {
490 if ($self->_validate_domain_name($host)) {
491 $self->_log(debug => "Resolving $host using remote shell");
492 my $method = "_resolve_remote_host_with_shell__" . (($self->{remote_os} eq 'windows') ? 'windows' : 'unix');
493 my @addrs = $self->$method($host);
494 for my $addr (@addrs) {
495 push @{$self->{net_mapping}{$host} //= []}, $addr;
496 $self->{forward_ipv4}{"$addr/32"} = 1;
497 }
498 @addrs or $self->_warn("Failed to resolve host using remote DNS, ignoring it", $host);
499 }
500 else {
501 $self->_warn("Ignoring host with invalid name", $host);
502 }
503 }
504 }
505  
506 sub _resolve_remote_host_with_shell__unix {
507 my $self = shift;
508 $self->_warn('Resolving using the shell on remote Unix hosts is not implemented yet');
509 ()
510 }
511  
512 sub _resolve_remote_host_with_shell__windows {
513 my ($self, $host) = @_;
514 my $ssh = $self->{ssh};
515  
516 my $out = $ssh->capture({remote_shell=> 'MSWin'}, 'powershell', '-Command', "Resolve-DnsName $host | ConvertTo-Json");
517 my @addrs;
518 eval {
519 my $records = JSON::PP::decode_json($out);
520 my @names = $host;
521 for my $r (@$records) {
522 push @names, $r->{NameHost} if $r->{Type} == 5;
523 }
524 for my $r (@$records) {
525 push @addrs, $r->{IP4Address} if $r->{Type} == 1
526 }
527 return @addrs
528 };
529 unless (@addrs) {
530 $self->_warn("Failed to parse JSON output from Resolve-DnsName", $@);
531 $self->_log(debug => "Output was", $out);
532 }
533 return @addrs;
534 }
535  
536 sub _init_dnsmasq {
537 my $self = shift;
538 my $net_mapping = $self->{net_mapping};
539 my $forward_dns = $self->{forward_dns};
540  
541 if (%$net_mapping or %$forward_dns) {
542 $self->_log(info => "Starting dnsmasq");
543  
544 my $pid_parent_dir = $self->{xdg}->state_home->child('dnsmasq')->mkdir;
545 my $pid_fn = $pid_parent_dir->child($self->{timestamp}.".dnsmasq.pid");
546 my $latest_fn = $pid_parent_dir->child("latest.dnsmasq.pid");
547 unlink $latest_fn if -e $latest_fn;
548 symlink $pid_fn, $latest_fn;
549 my $log_fn = $self->{xdg}->state_home->child('logs')->mkdir->child($self->{timestamp}.".dnsmasq.log");
550 my $butler = $self->{butler};
551 my $user_name = $self->_get_user_name;
552 my $group_name = $self->_get_group_name;
553 my $pid = $self->{dnsmasq_pid} = $butler->start_dnsmasq(device => $self->{tap_device},
554 net_mapping => $net_mapping,
555 forward_dns => $forward_dns,
556 user => $user_name,
557 group => $group_name,
558 pid_fn => "$pid_fn",
559 log_fn => "$log_fn");
560 if ($pid) {
561 $self->_log(debug => "dnsmasq PID", $pid);
562 }
563 else {
564 $self->_warn("dnsmasq failed to start correctly, no PID found");
565 }
566 }
567 else {
568 $self->_log(debug => 'dnsmasq not required');
569 }
570 }
571  
572 sub _init_resolver_rules {
573 my $self = shift;
574 my @domains = ( keys(%{$self->{net_mapping}}),
575 keys(%{$self->{forward_dns}}) );
576 if(@domains) {
577 $self->_log(info => "Setting up resolver rules");
578 my $butler = $self->{butler};
579 my $device = $self->{tap_device};
580 my $local_ip = $self->{local_ip};
581 $butler->resolvectl_dns(device => $device, dns => $local_ip);
582 for my $domain (@domains) {
583 $butler->resolvectl_domain(device => $device, domain => $domain);
584 }
585 }
586 }
587  
588 sub _init_routes {
589 my $self = shift;
590 my $forward = $self->{forward_ipv4};
591 if (%$forward) {
592 $self->_log(info => "Setting up routes");
593 my $butler = $self->{butler};
594 for my $net (keys %$forward) {
595 $butler->route_add(net => $net, gw => $self->{remote_gw}, device => $self->{tap_device});
596 }
597 }
598 }
599  
600 sub _get_user_name {
601 my $self = shift;
602 my $user = getpwuid($<);
603 return $user if $user;
604  
605 $self->_warn("Failed to get user name, using 'nobody'");
606 return 'nobody';
607 }
608  
609 sub _get_group_name {
610 my $self = shift;
611 my $group = getgrgid($();
612 return $group if $group;
613  
614 $self->_warn("Failed to get group name, using 'nogroup'");
615 return 'nogroup'
616 }
617  
618 sub _init_loop {
619 my $self = shift;
620  
621 my $loop = App::ReslirpTunnel::Loop->new(log_level => $self->{log_level},
622 log_to_stderr => $self->{log_to_stderr},
623 log_file => $self->{log_file});
624  
625 my $pid = $loop->run($self->{tap_fh}, $self->{reslirp_socket}, $self->{reslirp_stderr})
626 //$self->_die("Failed to start IO loop process");
627  
628 $self->_log(info => "IO loop process started, PID: $pid");
629 $self->{loop_pid} = $pid;
630 }
631  
632 sub _find_process_by_pid {
633 my ($self, $pid) = @_;
634 for my $process (qw(reslirp loop dnsmasq)) {
635 my $process_pid = $self->{"${process}_pid"};
636 if (defined $process_pid) {
637 return $process if $self->{"${process}_pid"} == $pid;
638 }
639 }
640 return;
641 }
642  
643 sub _wait_for_something {
644 my $self = shift;
645 $self->_log(debug => "Waiting for some child to exit");
646 while (not ${$self->{signal_count_ref}}) {
647 my $kid = waitpid(-1, WNOHANG);
648 if ($kid <= 0) {
649 # $self->_log(debug => "waitpid", $kid);
650 $self->_log(debug => "waitpid failed", $!) if $kid < 0;
651 select undef, undef, undef, 5;
652 }
653 else {
654 $self->_log(debug => "process $kid exited, rc", $? >> 8);
655 for my $proc (qw(reslirp loop ssh_master)) {
656 my $proc_pid = $self->{"${proc}_pid"};
657 if (defined $proc_pid and $kid == $proc_pid) {
658 $self->_log(info => "Process $proc (PID: $kid) finished");
659 delete $self->{"${proc}_pid"};
660  
661 $self->{ssh}->master_exited if $proc eq 'ssh_master';
662 return;
663 }
664 }
665 $self->_warn("Unknown process with PID $kid finished");
666 }
667 }
668 }
669  
670 sub _kill_everything {
671 my $self = shift;
672 $self->_log(debug => "killing everything!");
673 my @signals = (0, 0, 15, 15, 15, 9, 9, 9);
674  
675 if (defined(my $ssh = $self->{ssh})) {
676 $ssh->disconnect;
677 delete $self->{ssh_master_pid};
678 }
679  
680 for my $process (qw(loop reslirp dnsmasq)) {
681 my $pid = $self->{"${process}_pid"} // next;
682 $self->_log(debug => "Waiting for process $process (PID: $pid) to finish");
683 if (kill(0 => $pid) > 0) {
684 for my $signal (@signals) {
685 my $kid = waitpid($pid, WNOHANG);
686 if ($kid == $pid) {
687 $self->_log(debug => "Process $process exited and captured", $?);
688 last;
689 }
690 sleep 1;
691 $self->_log(debug => "Sending signal $signal to process $pid");
692 kill $signal => $pid;
693 }
694 }
695 else {
696 $self->_log(debug => "Cannot send signals to process $pid");
697 last;
698 }
699 }
700 $self->_log(info => "All processes finished");
701 }
702  
703 1;
704 __END__
« ReslirpTunnel/RPC.pm ↑ Summary