lib/App/FuguVM/QMP.pm

filter: Statementstmt 60.0% 3 / 5 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 2 / 2 Podpod n/a totaltotal 71.4% 5 / 7 CC 1 SCAR 1.4
CC
1
Cov
60%
BEGIN
1.3
BEGIN
1.0
CRAP 1.1
« lib/App/FuguVM/Config.pm ↑ Summary
1 # ex:ts=8 sw=4:
2 # $OpenBSD$
3 #
4 # Copyright (c) 2024 Dick Olsson <hi@senzilla.io>
5 #
6 # Permission to use, copy, modify, and distribute this software for any
7 # purpose with or without fee is hereby granted, provided that the above
8 # copyright notice and this permission notice appear in all copies.
9 #
10 # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  
18 1 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 package App::FuguVM::QMP;
21 our $VERSION = '0.3.1';
22  
23 0 ▶use Fugu::JSONSocket;
Subroutine BEGIN: called CC=1 SCAR=2.6
24  
25 # App::FuguVM::QMP - the QEMU Machine Protocol command set.
26 #
27 # The transport is Fugu::JSONSocket. This file holds only what is
28 # true of QMP: the greeting, the capabilities handshake, and the four
29 # commands that FuguVM uses to manage a VM's lifecycle.
30  
31 use constant READ_TIMEOUT => 10;
32  
33 sub new ( $class, $socket_path )
34 {
35 return bless {
36 socket => Fugu::JSONSocket->new(
37 path => $socket_path,
38 timeout => READ_TIMEOUT,
39 greeting => 1,
40 ),
41 }, $class;
42 }
43  
44 # $self->socket_path:
45 # Return the QMP socket path.
46 sub socket_path ($self)
47 {
48 return $self->{socket}->path;
49 }
50  
51 # $self->is_available:
52 # Report if the socket file is there. QEMU creates it at startup,
53 # so its absence means the VM has not started.
54 sub is_available ($self)
55 {
56 return $self->{socket}->exists;
57 }
58  
59 # $self->open_connection:
60 # Connect, read the greeting, and leave capabilities negotiation
61 # behind. QMP refuses every other command until qmp_capabilities
62 # succeeds. The method returns 1 on success and 0 on failure.
63 sub open_connection ($self)
64 {
65 return 1 if $self->{socket}->is_connected;
66  
67 $self->{socket}->connect or return 0;
68  
69 my $greeting = $self->{socket}->greeting;
70 if ( !defined $greeting || !exists $greeting->{QMP} ) {
71 $self->disconnect;
72 return 0;
73 }
74  
75 unless ( defined $self->run_command('qmp_capabilities') ) {
76 $self->disconnect;
77 return 0;
78 }
79  
80 return 1;
81 }
82  
83 sub disconnect ($self)
84 {
85 $self->{socket}->disconnect;
86 return $self;
87 }
88  
89 # $self->run_command($command, $arguments):
90 # Run one QMP command. The method returns the whole reply, so a
91 # caller can tell an error reply from a missing one.
92 sub run_command ( $self, $command, $arguments = undef )
93 {
94 my %message = ( execute => $command );
95 $message{arguments} = $arguments if defined $arguments;
96  
97 return $self->{socket}->request( \%message );
98 }
99  
100 # $self->query_status:
101 # Query the VM running status. The method returns a hashref with
102 # the 'running' and 'status' keys.
103 sub query_status ($self)
104 {
105 my $result = $self->run_command('query-status');
106 return if !defined $result || exists $result->{error};
107  
108 return $result->{return};
109 }
110  
111 # $self->is_running:
112 # Check if the VM runs now
113 sub is_running ($self)
114 {
115 my $status = $self->query_status;
116 return 0 if !defined $status;
117  
118 return $status->{running} ? 1 : 0;
119 }
120  
121 # $self->powerdown:
122 # Ask the guest for a controlled shutdown through ACPI
123 sub powerdown ($self)
124 {
125 my $result = $self->run_command('system_powerdown');
126  
127 return defined $result && !exists $result->{error};
128 }
129  
130 # $self->quit:
131 # Stop the QEMU process immediately
132 sub quit ($self)
133 {
134 my $result = $self->run_command('quit');
135 $self->disconnect;
136  
137 return defined $result && !exists $result->{error};
138 }
139  
140 1;
« lib/App/FuguVM/Config.pm ↑ Summary