| 1 | #!/usr/bin/env perl | ||
| 2 | # vim: fdm=marker sw=4 et | ||
| 3 | # Documentation head {{{ | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | IO::Pty::HalfDuplex::PTrace - identify reads using syscall tracing | ||
| 8 | |||
| 9 | =head1 SYNOPSIS | ||
| 10 | |||
| 11 | IO::Pty::HalfDuplex->new(backend => 'PTrace') | ||
| 12 | |||
| 13 | =head1 CAVEATS | ||
| 14 | |||
| 15 | C<IO::Pty::HalfDuplex::PTrace> is extremely sensitive to OS and architecture; | ||
| 16 | currently it only works on FreeBSD i386 and amd64. | ||
| 17 | |||
| 18 | C<IO::Pty::HalfDuplex::PTrace> does not know about ABI emulations used by the | ||
| 19 | target, and will fail on anything compiled for a different ABI than Perl. | ||
| 20 | |||
| 21 | =head1 BUGS | ||
| 22 | |||
| 23 | See L<IO::Pty::HalfDuplex>. | ||
| 24 | |||
| 25 | =head1 COPYRIGHT AND LICENSE | ||
| 26 | |||
| 27 | Copyright 2008-2009 Stefan O'Rear. | ||
| 28 | |||
| 29 | This program is free software; you can redistribute it and/or modify it | ||
| 30 | under the same terms as Perl itself. | ||
| 31 | |||
| 32 | =cut | ||
| 33 | |||
| 34 | # }}} | ||
| 35 | # header {{{ | ||
| 36 | package IO::Pty::HalfDuplex::PTrace; | ||
| 37 | |||
| 38 | use strict; | ||
| 39 | use warnings; | ||
| 40 | use POSIX '_exit', ':sys_wait_h', 'tcsetpgrp'; | ||
| 41 | |||
| 42 | use base 'IO::Pty::HalfDuplex::Ptyish'; | ||
| 43 | |||
| 44 | BEGIN { | ||
| 45 | die "XS code for IO::Pty::HalfDuplex::PTrace not built." | ||
| 46 | unless __PACKAGE__->can('_fork_traced'); | ||
| 47 | } | ||
| 48 | |||
| 49 | # }}} | ||
| 50 | # _report_death {{{ | ||
| 51 | sub _report_death { | ||
| 52 | my $self = shift; | ||
| 53 | |||
| 54 | syswrite $self->{info_pipe}, "d" . | ||
| 55 | chr(WIFSIGNALED($?) ? WTERMSIG($?) : 0) . | ||
| 56 | chr(WIFEXITED($?) ? WEXITSTATUS($?) : 0); | ||
| 57 | |||
| 58 | # We got here by a fork, so we certainly have stale buffers | ||
| 59 | _exit 0; | ||
| 60 | } | ||
| 61 | # }}} | ||
| 62 | # control loop and startup {{{ | ||
| 63 | # Wait for, and process, commands | ||
| 64 | sub _shell_loop { | ||
| 65 | my $self = shift; | ||
| 66 | |||
| 67 | while(1) { | ||
| 68 | my $buf = ''; | ||
| 69 | sysread($self->{ctl_pipe}, $buf, 1) > 0 or die "read(ctl): $!"; | ||
| 70 | |||
| 71 | while (1) { | ||
| 72 | my $rin = ''; | ||
| 73 | vec($rin, 0, 1) = 1; | ||
| 74 | tcsetpgrp(0, $self->{pid}); | ||
| 75 | last unless select($rin, undef, undef, 0); | ||
| 76 | tcsetpgrp(0, $self->{slave_pid}); | ||
| 77 | |||
| 78 | _continue_to_next_read($self->{slave_pid}) | ||
| 79 | or $self->_report_death; | ||
| 80 | } | ||
| 81 | tcsetpgrp(0, $self->{slave_pid}); | ||
| 82 | |||
| 83 | syswrite($self->{info_pipe}, "r"); | ||
| 84 | } | ||
| 85 | } | ||
| 86 | |||
| 87 | # This routine is responsible for creating the proper environment for the | ||
| 88 | # slave to run in. | ||
| 89 | sub _shell_spawn { | ||
| 90 | my $self = shift; | ||
| 91 | |||
| 92 | $self->{slave_pid} = _fork_traced; | ||
| 93 | |||
| 94 | if ($self->{slave_pid} == -1) { | ||
| 95 | # XXX yucky interface, what can be sensibly done | ||
| 96 | # child died before first trap, probably exec failure | ||
| 97 | $self->_report_death; | ||
| 98 | } | ||
| 99 | |||
| 100 | unless ($self->{slave_pid}) { | ||
| 101 | exec(@{$self->{command}}); | ||
| 102 | die "exec: $!"; | ||
| 103 | } | ||
| 104 | |||
| 105 | tcsetpgrp(0, $self->{slave_pid}); | ||
| 106 | syswrite($self->{info_pipe}, pack('N', $self->{slave_pid})); | ||
| 107 | |||
| 108 | _continue_to_next_read $self->{slave_pid} | ||
| 109 | or $self->_report_death; | ||
| 110 | } | ||
| 111 | |||
| 112 | sub _shell { | ||
| 113 | my $self = shift; | ||
| 114 | %$self = ( | ||
| 115 | %$self, | ||
| 116 | pid => $$, | ||
| 117 | @_ | ||
| 118 | ); | ||
| 119 | |||
| 120 | $self->_shell_spawn(); | ||
| 121 | $self->_shell_loop(); | ||
| 122 | } | ||
| 123 | 1; | ||
| 124 | # }}} |