| 1 | # -*- perl -*- | ||
| 2 | |||
| 3 | # | ||
| 4 | # Author: Slaven Rezic | ||
| 5 | # | ||
| 6 | # Copyright (C) 2018 Slaven Rezic. All rights reserved. | ||
| 7 | # This package is free software; you can redistribute it and/or | ||
| 8 | # modify it under the same terms as Perl itself. | ||
| 9 | # | ||
| 10 | # Mail: slaven@rezic.de | ||
| 11 | # WWW: http://www.rezic.de/eserte/ | ||
| 12 | # | ||
| 13 | |||
| 14 | package Doit::XTermRPC; | ||
| 15 | |||
| 16 | use strict; | ||
| 17 | use warnings; | ||
| 18 | our $VERSION = '0.01'; | ||
| 19 | |||
| 20 | our @ISA = ('Doit::_AnyRPCImpl'); | ||
| 21 | |||
| 22 | use Doit::Log; | ||
| 23 | |||
| 24 | my $socket_count = 0; | ||
| 25 | |||
| 26 | sub do_connect { | ||
| 27 | my($class, %opts) = @_; | ||
| 28 | my $term_prog = delete $opts{term_prog} || 'xterm'; | ||
| 29 | my @term_opts = @{ delete $opts{term_opts} || [] }; | ||
| 30 | my @term_run = @{ delete $opts{term_run} || ['-e', \"prog_and_args"] }; | ||
| 31 | my $dry_run = delete $opts{dry_run}; | ||
| 32 | my $debug = delete $opts{debug}; | ||
| 33 | my @components = @{ delete $opts{components} || [] }; | ||
| 34 | my $perl = delete $opts{perl} || $^X; | ||
| 35 | error "Unhandled options: " . join(" ", %opts) if %opts; | ||
| 36 | |||
| 37 | my $self = bless { }, $class; | ||
| 38 | |||
| 39 | require File::Basename; | ||
| 40 | require File::Spec; | ||
| 41 | require IPC::Open2; | ||
| 42 | require POSIX; | ||
| 43 | |||
| 44 | # Socket pathname, make it possible to find out | ||
| 45 | # old outdated sockets easily by including a | ||
| 46 | # timestamp. Also need to maintain a $socket_count, | ||
| 47 | # if the same script opens multiple sockets quickly. | ||
| 48 | my $sock_path = "/tmp/." . join(".", "doit", "xterm", POSIX::strftime("%Y%m%d_%H%M%S", gmtime), $<, $$, (++$socket_count)) . ".sock"; | ||
| 49 | |||
| 50 | # On linux use Linux Abstract Namespace Sockets --- | ||
| 51 | # invisible and automatically cleaned up. See man 7 unix. | ||
| 52 | my $LANS_PREFIX = $class->_can_LANS ? '\0' : ''; | ||
| 53 | |||
| 54 | # Run the server | ||
| 55 | my @cmd_worker = | ||
| 56 | ( | ||
| 57 | $perl, "-I".File::Spec->rel2abs(File::Basename::dirname(__FILE__)."/.."), "-I".File::Spec->rel2abs(File::Basename::dirname($0)), "-e", | ||
| 58 | Doit::_ScriptTools::self_require() . | ||
| 59 | q{my $d = Doit->init; } . | ||
| 60 | Doit::_ScriptTools::add_components(@components) . | ||
| 61 | q{Doit::RPC::Server->new($d, "} . $LANS_PREFIX . $sock_path . q{", excl => 1, debug => } . ($debug?1:0) . q{)->run();} . | ||
| 62 | ($LANS_PREFIX ? '' : q<END { unlink "> . $sock_path . q<" }>), # cleanup socket file, except if Linux Abstract Namespace Sockets are used | ||
| 63 | "--", ($dry_run? "--dry-run" : ()) | ||
| 64 | ); | ||
| 65 | my @full_cmd_worker = ($term_prog, @term_opts); | ||
| 66 | for my $arg (@term_run) { | ||
| 67 | if (ref $arg) { | ||
| 68 | if ($$arg eq 'prog_and_args') { | ||
| 69 | push @full_cmd_worker, @cmd_worker; | ||
| 70 | } else { | ||
| 71 | error "Invalid reference $arg"; | ||
| 72 | } | ||
| 73 | } else { | ||
| 74 | push @full_cmd_worker, $arg; | ||
| 75 | } | ||
| 76 | } | ||
| 77 | |||
| 78 | my $worker_pid = fork; | ||
| 79 | if (!defined $worker_pid) { | ||
| 80 | error "fork failed: $!"; | ||
| 81 | } elsif ($worker_pid == 0) { | ||
| 82 | info "worker perl cmd: @full_cmd_worker\n" if $debug; | ||
| 83 | no warnings qw(syntax exec); # "Statement unlikely to be reached", different warning categories in different perls | ||
| 84 | exec @full_cmd_worker; | ||
| 85 | error "Failed to run '@full_cmd_worker': $!"; | ||
| 86 | } | ||
| 87 | |||
| 88 | # Run the client | ||
| 89 | my($in, $out); | ||
| 90 | my @cmd_comm = ($perl, "-I".File::Spec->rel2abs(File::Basename::dirname(__FILE__)."/.."), "-MDoit", "-e", | ||
| 91 | q{Doit::Comm->comm_to_sock("} . $LANS_PREFIX . $sock_path . q{", debug => shift)}, !!$debug); | ||
| 92 | warn "comm perl cmd: @cmd_comm\n" if $debug; | ||
| 93 | my $comm_pid = IPC::Open2::open2($out, $in, @cmd_comm); | ||
| 94 | $self->{rpc} = Doit::RPC::Client->new($out, $in, label => "xterm:", debug => $debug); | ||
| 95 | |||
| 96 | $self; | ||
| 97 | } | ||
| 98 | |||
| 99 | sub DESTROY { } | ||
| 100 | |||
| 101 | 1; | ||
| 102 | |||
| 103 | __END__ |