File Coverage

blib/lib/Umlmgr/Uml.pm
Criterion Covered Total %
statement 43 118 36.4
branch 6 54 11.1
condition 0 14 0.0
subroutine 9 15 60.0
pod 0 8 0.0
total 58 209 27.7


line stmt bran cond sub pod time code
1             package Umlmgr::Uml;
2              
3 2     2   25005 use 5.010000;
  2         6  
  2         78  
4 2     2   12 use strict;
  2         3  
  2         60  
5 2     2   10 use warnings;
  2         3  
  2         60  
6 2     2   1283 use Config::IniFiles;
  2         38509  
  2         62  
7 2     2   1926 use IPC::Open3;
  2         5985  
  2         106  
8 2     2   2363 use Sys::Syslog;
  2         48014  
  2         145  
9 2     2   1075 use Umlmgr::Utils;
  2         4  
  2         2425  
10              
11             sub new {
12 3     3 0 725 my ($class, $config, %options) = @_;
13              
14 3         8 my $self = {};
15 3         4 eval {
16 3         33 $self->{config} = Config::IniFiles->new(
17             -file => $config
18             );
19             };
20 3 100       2910 return if (! $self->{config});
21 2         12 $self->{$_} = $options{$_} foreach (keys %options);
22 2         16 ($self->{umid}) = $config =~ m:([^/]+)\.uml$:;
23 2         15 Sys::Syslog::openlog('umlmgr', 'pid', 'daemon');
24 2         59 bless($self, $class);
25             }
26              
27             sub build_uml_cmd {
28 1     1 0 426 my ($self) = @_;
29              
30 1         2 my @cmd;
31              
32 1         7 push(@cmd, $self->{config}->val('env', 'kernel', 'linux'));
33              
34 1         27 foreach my $param ($self->{config}->Parameters('uml')) {
35 2 50       36 $self->{config}->val('uml', $param) or next;
36 2         35 push(@cmd, $param . '=' . $self->{config}->val('uml', $param));
37             }
38 1 50       16 if (!$self->{config}->val('uml', 'umid')) {
39 1         19 push(@cmd, "umid=$self->{umid}");
40             }
41 1 50       4 if (!$self->{config}->val('uml', 'con0')) {
42 1         16 push(@cmd, 'con0=fd:0,fd:1');
43             }
44 1 50       4 if (!$self->{config}->val('uml', 'con')) {
45 1         15 push(@cmd, 'con=pts');
46             }
47              
48 1         13 return @cmd;
49             }
50              
51             sub onboot {
52 0     0 0   my ($self) = @_;
53 0           $self->{config}->val('env', 'onboot')
54             }
55              
56             sub id {
57 0     0 0   my ($self) = @_;
58 0           $self->{config}->val('uml', 'umid', $self->{umid});
59             }
60              
61             sub start {
62 0     0 0   my ($self) = @_;
63              
64 0 0 0       if ($> == 0 && !$self->{user}) {
65 0           warn "No user defined, running vm as root is a bad idea\n";
66 0           return;
67             }
68              
69 0           my $pid = fork;
70 0 0         return if (!defined($pid));
71              
72 0 0         if ($pid) {
73 0           return 1; # we hope that's ok
74             } else {
75 0 0         if ($self->{user}) {
76 0 0         Umlmgr::Utils::become_user($self->{user}) or return;
77             }
78 0           my @cmd = $self->build_uml_cmd;
79 0           my $id = $self->id;
80 0           $self->{config} = undef;
81 0           Sys::Syslog::syslog('info', $id . ': Starting as ' . join(' ', @cmd));
82 0           my $upid = open3(my $in, my $out, my $err, @cmd);
83 0           while (my $line = <$out>) {
84 0           chomp($line);
85 0           Sys::Syslog::syslog('info', $id . " $line");
86             }
87 0           waitpid($upid, 0);
88 0           Sys::Syslog::syslog('info', $id . " terminated with exit status $?");
89 0           exit(0);
90             }
91             }
92              
93             sub stop {
94 0     0 0   my ($self) = @_;
95              
96 0 0 0       if ($> == 0 && !$self->{user}) {
97 0           warn "No user defined, running vm as root is a bad idea\n";
98 0           return;
99             }
100              
101 0           my $pid = fork;
102 0 0         if (!defined($pid)) { return }
  0            
103 0 0         if ($pid) {
104 0           waitpid($pid, 0);
105             } else {
106 0 0         if ($self->{user}) {
107 0 0         Umlmgr::Utils::become_user($self->{user}) or return;
108             }
109 0           Sys::Syslog::syslog('info', $self->id . ": sending halt");
110 0           system('uml_mconsole', $self->id, 'halt');
111 0           exit(0);
112             }
113             }
114              
115             sub status {
116 0     0 0   my ($self) = @_;
117              
118 0 0 0       if ($> == 0 && !$self->{user}) {
119 0           warn "No user defined, running vm as root is a bad idea\n";
120 0           return;
121             }
122              
123 0           my $pid = fork;
124 0 0         return if (!defined($pid));
125 0 0         if ($pid) {
126 0           waitpid($pid, 0);
127 0           return(($? >> 8) == 0);
128             } else {
129 0 0         if ($self->{user}) {
130 0 0         Umlmgr::Utils::become_user($self->{user}) or return;
131             }
132 0           my $opid = open3(my $in, my $out, my $err, 'uml_mconsole', $self->id, 'version');
133 0           waitpid($opid, 0);
134 0           exit($? >> 8);
135             }
136             }
137              
138             sub get_console {
139 0     0 0   my ($self, $console) = @_;
140            
141 0 0 0       if ($> == 0 && !$self->{user}) {
142 0           warn "No user defined, running vm as root is a bad idea\n";
143 0           return;
144             }
145 0           my $pid = fork;
146 0 0         return if (!defined($pid));
147 0 0         if ($pid) {
148 0           waitpid($pid, 0);
149 0           return(($? >> 8) == 0);
150             } else {
151 0 0         if ($self->{user}) {
152 0 0         Umlmgr::Utils::become_user($self->{user}) or return;
153             }
154 0           my $pts;
155              
156 0 0         if (open(my $hd,
  0            
157             join(' ', map { quotemeta($_) }
158             ('uml_mconsole', $self->id, 'config', $console)) . ' |')) {
159 0           my $line = <$hd>;
160 0   0       $line ||= '';
161 0           chomp $line;
162 0           ($pts) = $line =~ /^OK pts:(.*)/;
163 0           close($hd);
164             }
165 0 0         if ($pts) {
166 0           print "$pts\n";
167             } else {
168 0           warn "Can't find a valid console\n";
169             }
170             }
171             }
172              
173             # Preloaded methods go here.
174              
175             # Autoload methods go after =cut, and are processed by the autosplit program.
176              
177             1;
178             __END__