| •1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
# ttyon - run command controlled by fake terminal |
| 4 |
# |
| 5 |
# Copyright 2021, Nicholas Guriev <[email protected]> |
| 6 |
# |
| 7 |
# Permission is hereby granted, free of charge, to any person obtaining a |
| 8 |
# copy of this software and associated documentation files (the "Software"), |
| 9 |
# to deal in the Software without restriction, including without limitation |
| 10 |
# the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 11 |
# and/or sell copies of the Software, and to permit persons to whom the |
| 12 |
# Software is furnished to do so, subject to the following conditions: |
| 13 |
# |
| 14 |
# The above copyright notice and this permission notice shall be included |
| 15 |
# in all copies or substantial portions of the Software. |
| 16 |
# |
| 17 |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 18 |
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 20 |
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR |
| 21 |
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 22 |
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 23 |
# OTHER DEALINGS IN THE SOFTWARE. |
| 24 |
|
| 25 |
=head1 NAME |
| 26 |
|
| 27 |
ttyon - run command controlled by fake terminal |
| 28 |
|
| 29 |
=head1 SYNOPSIS |
| 30 |
|
| 31 |
B<ttyon> [B<-a> I<NAME>] [B<-fs>] [I<COMMAND>...] |
| 32 |
|
| 33 |
=head1 DESCRIPTION |
| 34 |
|
| 35 |
Simple script that tries to open a fake controlling terminal and executes |
| 36 |
passed command. The script is primarily intended for running automated tests. |
| 37 |
|
| 38 |
If no command is specified, current Shell is executed. |
| 39 |
|
| 40 |
Options: |
| 41 |
|
| 42 |
=over |
| 43 |
|
| 44 |
=item B<-a> |
| 45 |
|
| 46 |
Override the zeroth argument of the command. |
| 47 |
|
| 48 |
=item B<-f> |
| 49 |
|
| 50 |
Never avoid wasted forks. |
| 51 |
|
| 52 |
=item B<-s> |
| 53 |
|
| 54 |
Connect standard streams (numbered 0, 1, and 2) to the opened terminal. |
| 55 |
|
| 56 |
=back |
| 57 |
|
| 58 |
=head1 AUTHOR |
| 59 |
|
| 60 |
Written by Nicholas Guriev on New Year's holidays 2021. |
| 61 |
|
| 62 |
=head1 SEE ALSO |
| 63 |
|
| 64 |
pts(4), ioctl_tty(2) |
| 65 |
|
| 66 |
=cut |
| 67 |
|
| 68 |
|
| 69 |
use strict; |
| 70 |
use warnings; |
| 71 |
|
| 72 |
use Getopt::Std; |
| 73 |
use Fcntl; |
| 74 |
use POSIX (); |
| 75 |
BEGIN { require "sys/ioctl.ph" } |
| 76 |
|
| 77 |
sub do_switch { |
| 78 |
my $child = fork; |
| 79 |
die $! unless defined $child; |
| 80 |
if ($child) { |
| 81 |
waitpid $child, 0 or die $!; |
| 82 |
kill $? & 127, $$ if $? & 127; |
| 83 |
POSIX::_exit $? >> 8; # Perl's cleaning causes unwanted SIGHUP. |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
# Some constants from ioctl.ph have incorrect value due to missing %sizeof hash. |
| 88 |
# Apply a workaround suggested by Celelibi at https://bugs.debian.org/190887#26 |
| 89 |
our %sizeof = (int => length pack "i!", 0); |
| 90 |
|
| 91 |
$Getopt::Std::STANDARD_HELP_VERSION = 1; |
| 92 |
our ($opt_a, $opt_f, $opt_s); |
| 93 |
getopts("a:fs") or exit 1; |
| 94 |
|
| 95 |
my $term; |
| 96 |
if ($opt_f or not open $term, "+<", "/dev/tty") { |
| 97 |
# Become a session leader. |
| 98 |
if ($opt_f or POSIX::setsid() < 0) { |
| 99 |
do_switch; |
| 100 |
POSIX::setsid() > 0 or die $!; |
| 101 |
} |
| 102 |
|
| 103 |
# Open a new terminal pair trough a multiplexor and treat its descriptor as |
| 104 |
# system file to not auto-close it by master process. |
| 105 |
local $^F = 3; |
| 106 |
open $term, "+<", "/dev/ptmx" or die $!; |
| 107 |
|
| 108 |
# Equivalent of unlockpt(3) which is not provided by the POSIX package. |
| 109 |
my $buf = pack "i!", 0; |
| 110 |
ioctl $term, TIOCSPTLCK, $buf or die $!; |
| 111 |
|
| 112 |
# Set the new terminal as controlling of us. |
| 113 |
ioctl $term, TIOCSCTTY, 0 or die $!; |
| 114 |
} |
| 115 |
do_switch if $opt_f; |
| 116 |
|
| 117 |
if ($opt_s and $term) { |
| 118 |
for my $stream (*STDIN, *STDOUT, *STDERR) { |
| 119 |
# Reopen via an equivalent of ptsname(3). |
| 120 |
close $stream or die $!; |
| 121 |
my $fd = ioctl $term, TIOCGPTPEER, O_RDWR or die $!; |
| 122 |
open $stream, "+<&=", $fd or die $!; |
| 123 |
} |
| 124 |
} |
| 125 |
close $term or die $! if $opt_f; |
| 126 |
|
| 127 |
unless (@ARGV) { |
| 128 |
push @ARGV, $ENV{SHELL} || "/bin/sh"; |
| 129 |
} |
| 130 |
exec { $opt_a or $ARGV[0] } @ARGV or die $!; |