This file is indexed.

/usr/share/perl5/Sbuild/ChrootAutopkgtest.pm is in libsbuild-perl 0.75.0-1ubuntu1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#
# Chroot.pm: chroot library for sbuild
# Copyright © 2005      Ryan Murray <rmurray@debian.org>
# Copyright © 2005-2008 Roger Leigh <rleigh@debian.org>
# Copyright © 2008      Simon McVittie <smcv@debian.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see
# <http://www.gnu.org/licenses/>.
#
#######################################################################

package Sbuild::ChrootAutopkgtest;

use strict;
use warnings;

use POSIX qw(setsid);
use Sbuild qw(shellescape);

BEGIN {
    use Exporter ();
    use Sbuild::Chroot;
    our (@ISA, @EXPORT);

    @ISA = qw(Exporter Sbuild::Chroot);

    @EXPORT = qw();
}

sub new {
    my $class = shift;
    my $conf = shift;
    my $chroot_id = shift;

    my $self = $class->SUPER::new($conf, $chroot_id);
    bless($self, $class);

    $self->set('Autopkgtest Pipe In', undef);
    $self->set('Autopkgtest Pipe Out', undef);
    $self->set('Autopkgtest Virt PID', undef);

    return $self;
}

sub begin_session {
    my $self = shift;

    # We are manually setting up bidirectional communication with autopkgtest
    # instead of using IPC::Open2 because we must call setsid() from the
    # child.
    #
    # Calling setsid() is necessary to place autopkgtest into a new process
    # group and thus prevent it from receiving for example a Ctrl+C that can be
    # sent by the user from a terminal. If autopkgtest would receive the
    # SIGINT, then it would close the session immediately without us being able
    # to do anything about it. Instead, we want to close the session later
    # ourselves.
    pipe(my $prnt_out, my $chld_in);
    pipe(my $chld_out, my $prnt_in);

    my $pid = fork();
    if (!defined $pid) {
	die "Cannot fork: $!";
    } elsif ($pid == 0) {
	# child
	close($chld_in);
	close($chld_out);

	# redirect stdin
	open(STDIN, '<&', $prnt_out)
	    or die "Can't redirect stdin\n";

	# redirect stdout
	open(STDOUT, '>&', $prnt_in)
	    or die "Can't redirect stdout\n";

	# put process into new group
	setsid();

	my @command = ($self->get_conf('AUTOPKGTEST_VIRT_SERVER'),
	    @{$self->get_conf('AUTOPKGTEST_VIRT_SERVER_OPTIONS')});
	exec { $self->get_conf('AUTOPKGTEST_VIRT_SERVER') } @command;
	die "Failed to exec $self->get_conf('AUTOPKGTEST_VIRT_SERVER'): $!";
    }
    close($prnt_out);
    close($prnt_in);

    # We must enable autoflushing for the stdin of the child process or
    # otherwise the commands we write will never reach the child.
    $chld_in->autoflush(1);

    if (!$pid) {
	print STDERR "Chroot setup failed\n";
	return 0;
    }

    my $status = <$chld_out>;

    if (!defined $status) {
	print STDERR "Undefined chroot status\n";
	return 0;
    }

    chomp $status;

    if (! defined $status || $status ne "ok") {
	print STDERR "autopkgtest-virt server returned unexpected value: $status\n";
	kill 'KILL', $pid;
	return 0;
    }

    print $chld_in "open\n";

    $status = <$chld_out>;

    if (!defined $status) {
	print STDERR "Undefined return value after 'open'\n";
	return 0;
    }

    chomp $status;

    my $autopkgtest_session;
    if ($status =~ /^ok (.*)$/) {
	$autopkgtest_session = $1;
	$self->set('Session ID', $autopkgtest_session);
    } else {
	print STDERR "autopkgtest-virt server: cannot open: $status\n";
	kill 'KILL', $pid;
	return 0;
    }

    print STDERR "Setting up chroot with session id $autopkgtest_session\n"
	if $self->get_conf('DEBUG');

    print $chld_in "capabilities\n";

    chomp ($status = <$chld_out>);

    my @capabilities;
    if ($status =~ /^ok (.*)$/) {
	@capabilities = split /\s+/, $1;
    } else {
	print STDERR "autopkgtest-virt server: cannot capabilities: $status\n";
	kill 'KILL', $pid;
	return 0;
    }

    if (! grep {$_ eq "root-on-testbed"} @capabilities) {
	print STDERR "autopkgtest-virt server: capability root-on-testbed missing\n";
	kill 'KILL', $pid;
	return 0;
    }

    # TODO: also test "revert" capability

    print $chld_in "print-execute-command\n";

    chomp ($status = <$chld_out>);

    my $exec_cmd;
    if ($status =~ /^ok (.*)$/) {
	$exec_cmd = $1;
    } else {
	print STDERR "autopkgtest-virt server: cannot print-execute-command: $status\n";
	kill 'KILL', $pid;
	return 0;
    }

    my @exec_args = split /,/, $exec_cmd;

    @exec_args = map { s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg; $_ } @exec_args;

    $self->set('Location', '/autopkgtest-virt-dummy-location');
    $self->set('Autopkgtest Pipe In', $chld_in);
    $self->set('Autopkgtest Pipe Out', $chld_out);
    $self->set('Autopkgtest Virt PID', $pid);
    $self->set('Autopkgtest Exec Command', \@exec_args);

    return 0 if !$self->_setup_options();

    return 1;
}

sub end_session {
    my $self = shift;

    return if $self->get('Session ID') eq "";

    print STDERR "Cleaning up chroot (session id " . $self->get('Session ID') . ")\n"
	if $self->get_conf('DEBUG');

    my $chld_in = $self->get('Autopkgtest Pipe In');
    my $chld_out = $self->get('Autopkgtest Pipe Out');
    my $pid = $self->get('Autopkgtest Virt PID');

    print $chld_in "close\n";

    my $status = <$chld_out>;

    if (!defined $status) {
	print STDERR "Undefined return value after 'close'\n";
	return 0;
    }

    chomp $status;

    if ($status ne "ok") {
	print STDERR "autopkgtest-virt server: cannot close: $status\n";
	return 0;
    }

    print $chld_in "quit\n";

    waitpid $pid, 0;

    if ($?) {
	my $child_exit_status = $? >> 8;
	print STDERR "autopkgtest-virt quit with exit status $child_exit_status\n";
	return 0;
    }

    close($chld_in);
    close($chld_out);

    $self->set('Autopkgtest Pipe In', undef);
    $self->set('Autopkgtest Pipe Out', undef);
    $self->set('Autopkgtest Virt PID', undef);

    return 1;
}

sub get_command_internal {
    my $self = shift;
    my $options = shift;

    # Command to run. If I have a string, use it. Otherwise use the list-ref
    my $command = $options->{'INTCOMMAND_STR'} // $options->{'INTCOMMAND'};

    my $user = $options->{'USER'};          # User to run command under
    my $dir;                                # Directory to use (optional)
    $dir = $self->get('Defaults')->{'DIR'} if
	(defined($self->get('Defaults')) &&
	 defined($self->get('Defaults')->{'DIR'}));
    $dir = $options->{'DIR'} if
	defined($options->{'DIR'}) && $options->{'DIR'};

    if (!defined $user || $user eq "") {
	$user = $self->get_conf('USERNAME');
    }

    my @cmdline = ();

    @cmdline = @{$self->get('Autopkgtest Exec Command')};

    if ($user ne "root") {
	push @cmdline, "/sbin/runuser", '-u', $user, '--';
    }

    if (defined($dir)) {
	my $shelldir = shellescape $dir;
	push @cmdline, 'sh', '-c', "cd $shelldir && exec \"\$@\"", 'exec';
    } else {
	$dir = '/';
    }

    if (ref $command) {
        push @cmdline, @$command;
    } else {
        push @cmdline, ('/bin/sh', '-c', $command);
        $command = [split(/\s+/, $command)];
    }

    $options->{'USER'} = $user;
    $options->{'COMMAND'} = $command;
    $options->{'EXPCOMMAND'} = \@cmdline;
    $options->{'CHDIR'} = undef;
    $options->{'DIR'} = $dir;
}

1;