This file is indexed.

/usr/share/perl5/ImVirt.pm is in libimvirt-perl 0.9.6-1.

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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# ImVirt - I'm virtualized?
#
# Authors:
#   Thomas Liske <liske@ibh.de>
#
# Copyright Holder:
#   2009 - 2012 (C) IBH IT-Service GmbH [http://www.ibh.de/]
#
# License:
#   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 package; if not, write to the Free Software
#   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
#

package ImVirt;

=head1 NAME

ImVirt - detects several virtualizations

=head1 SYNOPSIS

    use ImVirt;
    print imv_get(IMV_PROB_DEFAULT, imv_detect()),"\n";

=head1 DESCRIPTION

The C<ImVirt> package tries to detect if it is run in a
virtualization container. At least the following container
should be detected:

=over

=item ARAnyM

=item KVM

=item lguest

=item LXC

=item OpenVZ

=item QEMU

=item UML

=item VirtualBox

=item Virtual PC/Server

=item VMware

=item Xen

=back

=head1 DETECTION HEURISTIC

The detection is based on a heuristic - you should not trust the result at all.

ImVirt probes for different well-known characteristics of different
virtualization containers. Any characteristics found or not found are weighted
by their significance.

The result of the heuristic is a weighted tree. The leaves are the (not)
detected containers.

=head1 FUNCTIONS

The following functions should be used to retrieve the detected virtualization
containers:

=over 4

=item imv_get($prob)
Returns exactly one string describing the detected container. If the detected
container has a smaller match probability than $prob the string 'Unknown' is
returned.

=item imv_get_all()
Returns a hash any positive detected containers as keys and their corresponding
match probability as value.

=item imv_get_pos_results()
Returns a list of all possible results which might be returned by all detection
modules. The list entries might be appended by some additional data like version
numbers etc.

=back

=cut

use strict;
use warnings;
use Module::Find;
use List::Util qw(sum);
use Data::Dumper;

use constant {
    KV_POINTS		=> 'points',
    KV_SUBPRODS		=> 'prods',
    KV_PROB		=> 'prob',

    IMV_PHYSICAL	=> 'Physical',
    IMV_VIRTUAL		=> 'Virtual',
    IMV_EMULATOR	=> 'Emulator',
    IMV_CONTAINER	=> 'Container',
    IMV_UNKNOWN		=> 'Unknown',

    IMV_PROB_DEFAULT	=> 0.9,

    IMV_PTS_MINOR	=> 1,
    IMV_PTS_NORMAL	=> 3,
    IMV_PTS_MAJOR	=> 6,
    IMV_PTS_DRASTIC	=> 12,
};

require Exporter;
our @ISA = qw(Exporter);

our @EXPORT = qw(
    imv_detect
    imv_get
    imv_get_all
    imv_get_pos_results
    IMV_PROB_DEFAULT
    IMV_PHYSICAL
    IMV_VIRTUAL
    IMV_UNKNOWN
    IMV_PTS_MINOR
    IMV_PTS_NORMAL
    IMV_PTS_MAJOR
    IMV_PTS_DRASTIC
);

our $VERSION = '0.9.6';

sub get_libexecdir() {
    return '/usr/lib/imvirt';
}

my @vmds = ();
my $debug = 0;
my %detected = ();
my %rtree;

sub register_vmd($) {
    my $vmd = shift || return;

    push(@vmds, $vmd);
}

sub _rtree_vm($$$@) {
    my $dref = shift;
    my $cref = shift;
    my $pts = shift;

    foreach my $prod (keys %{$dref}) {
	my $href = ${$dref}{$prod};

	if(keys %{${$href}{KV_SUBPRODS}}) {
	    &_rtree_vm(${$href}{KV_SUBPRODS}, $cref, $pts + ${$href}{KV_POINTS}, @_, $prod);
	}
	else {
	    if(${$href}{KV_POINTS} > 0) {
		my $n = join(' ', @_, $prod);
		$n =~ s/^.*\|([^\|]+)/$1/ if($n =~ /\|/);
		${$cref}{$n} = $pts + ${$href}{KV_POINTS};
	    }
	}
    }
}

sub imv_detect() {
    imv_init() unless (@vmds);

    %detected = (ImVirt::IMV_PHYSICAL => {KV_POINTS => IMV_PTS_MINOR});

    foreach my $vmd (@vmds) {
	eval "${vmd}::detect(\\\%detected);";
	warn "Error in ${vmd}::detect(): $@\n" if $@;
    }

    %rtree = ();

    _rtree_vm(\%detected, \%rtree, 0);

    my $psum = sum grep { $_ > 0 } values %rtree;

    foreach my $prod (keys %rtree) {
	my $pts = $rtree{$prod};

	if($pts > 0) {
	    $rtree{$prod} = $pts/$psum;
	}
	else {
	    $rtree{$prod} = 0;
	}
    }

    debug(__PACKAGE__, "imvirt_detect():\n".Dumper(\%rtree));
}

sub inc_pts($$@) {
    debug(__PACKAGE__, 'inc_pts('.join(', ',@_).')');

    my $dref = shift;
    my $prop = shift;

    _change_pts($prop, $dref, @_);
}

sub dec_pts($$@) {
    debug(__PACKAGE__, 'dec_pts('.join(', ',@_).')');

    my $dref = shift;
    my $prop = shift;

    _change_pts(-$prop, $dref, @_);
}

sub _change_pts($\%@) {
    my $prop = shift;
    my $ref = shift;
    my $key = shift;

    my $href = ${$ref}{$key};
    unless($href) {
        $href = ${$ref}{$key} = {KV_POINTS => 0, KV_SUBPRODS => {}};
    }

    if($#_ != -1) {
	&_change_pts($prop, ${$href}{KV_SUBPRODS}, @_);
    }
    else {
	${$href}{KV_POINTS} += $prop;
    }
}
sub imv_get_all() {
    imv_detect() unless (%detected);

    return %rtree;
}

sub imv_get($) {
    imv_detect() unless (%detected);

    my $prob = shift;

    my @res = sort { $rtree{$b} > $rtree{$a} } keys %rtree;
    my $vm = shift @res;

    return $vm if(eval {
	my $m = (sum values %rtree)/($#res+2);
	debug(__PACKAGE__, "imv_get: m = $m");

	my $s = 0;
	foreach my $v (values %rtree) {
	    $s += ($v - $m)**2;
	}
	$s /=($#res+1);
	debug(__PACKAGE__, "imv_get: s² = $s");

	my $vm2 = shift @res;
	debug(__PACKAGE__, "imv_get: $rtree{$vm} - sqrt($s) > $rtree{$vm2}");
	$rtree{$vm} - sqrt($s) > $rtree{$vm2};
    });

    return $vm if($rtree{$vm} >= $prob);

    return IMV_UNKNOWN;
}

sub imv_get_pos_results {
    imv_init() unless (@vmds);

    my @pres;

    foreach my $vmd (@vmds) {
	eval "\@pres = (\@pres, ${vmd}::pres());";
    }

    my %pres = map { $_, 1 } map { s/^.*\|([^|]+)$/$1/; $_; } @pres;
    return sort {uc($a) cmp uc($b)} keys %pres;
}

sub set_debug($) {
    $debug = shift;
}

sub get_debug() {
    return $debug;
}

sub debug($$) {
    printf STDERR "%s: %s\n", @_ if($debug);
}

sub imv_init() {
    # autoload VMD modules
    foreach my $module (findsubmod ImVirt::VMD) {
	eval "use $module;";
	die "Error loading $module: $@\n" if $@;
    }
}

1;