File: pid.pm

package info (click to toggle)
lxctl 0.3.1%2Bdebian-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch
  • size: 404 kB
  • ctags: 199
  • sloc: perl: 2,656; makefile: 35
file content (103 lines) | stat: -rw-r--r-- 1,954 bytes parent folder | download | duplicates (5)
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
package Lxctl::pid;

use strict;
use warnings;

my %options = ();

sub get_proc_name
{
	my ($self, $pid) = @_;

	open(my $file, '<', "/proc/$pid/comm")
		or die "Failed to open /proc/$pid/comm.\n\n";

	my $line = <$file>;
	chomp $line;

	return $line;
}

sub get_proc_container
{
	my ($self, $pid) = @_;

	open(my $file, '<', "/proc/$pid/cgroup")
		or die "Failed to open /proc/$pid/cgroup.\n\n";

	my $line = <$file>;
	$line =~ s/^\S+:\S+:\/(\S*)$/$1/;
	chomp $line;
	$line = "dom0 process" if $line eq "";

	return $line;
}

sub get_longest_string
{
	my ($self, @arr) = @_;

	my @tmp = sort { length($b) <=> length($a) } @arr;

	return length($tmp[0]);
}

sub do
{
	my $self = shift;

	my $pidlist = shift
		or die "What pid[s] should I check?\n\n";

	my @pids = ('PID');
	my @names = ('NAME');
	my @cts = ('CT_NAME');

	my @tmp_pids = split(/,/, $pidlist);
	for my $pid (@tmp_pids) {
		$pid =~ m/^\d+$/
			or die "$pid - that's not a pid. No-no-no-no, don't try to fool me.\n\n";
		
		push(@pids, $pid);
		push(@names, $self->get_proc_name($pid));
		push(@cts, $self->get_proc_container($pid));
	}

	my $max_pids = $self->get_longest_string(@pids);
	my $max_names = $self->get_longest_string(@names);
	my $max_cts = $self->get_longest_string(@cts);

	for (my $i=0; $i < scalar(@pids); $i++) {
		printf "  %".$max_pids."s  %".$max_names."s  %".$max_cts."s\n", $pids[$i], $names[$i], $cts[$i];
	}

	return;
}

sub new
{
	my $class = shift;
	my $self = {};
	bless $self, $class;

	return $self;
}

1;
__END__

=head1 AUTHOR

Anatoly Burtsev, E<lt>anatolyburtsev@yandex.ruE<gt>
Pavel Potapenkov, E<lt>ppotapenkov@gmail.comE<gt>
Vladimir Smirnov, E<lt>civil.over@gmail.comE<gt>

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2011 by Anatoly Burtsev, Pavel Potapenkov, Vladimir Smirnov

This library is free software; you can redistribute it and/or modify
it under the same terms of GPL v2 or later, or, at your opinion
under terms of artistic license.

=cut