]>
git.proxmox.com Git - pve-container.git/blob - src/PVE/LXC/Monitor.pm
3 package PVE
::LXC
::Monitor
;
9 use Socket
qw(SOCK_STREAM);
10 use POSIX
qw(NAME_MAX);
24 my $LXC_MSG_SIZE = length(pack('I! Z'.(NAME_MAX
+1).' x![I] I', 0, "", 0));
25 # Unpack an lxc_msg struct.
26 my sub _unpack_lxc_msg
($) {
30 # lxc_msg_type_t type;
31 # char name[NAME_MAX+1];
35 my ($type, $name, $value) = unpack('I!Z'.(NAME_MAX
+1).'I!', $packet);
39 } elsif ($type == 1) {
41 } elsif ($type == 2) {
44 warn "unsupported lxc message type $type received\n";
48 return ($type, $name, $value);
51 # Opens the monitor socket
54 sub get_monitor_socket
{
55 my $socket = IO
::Socket
::UNIX-
>new(
56 Type
=> SOCK_STREAM
(),
57 # assumes that lxcpath is '/var/lib/lxc', the hex part is a hash of the lxcpath
58 Peer
=> "\0lxc/ad055575fe28ddd5//var/lib/lxc",
60 if (!defined($socket)) {
61 die "failed to connect to monitor socket: $!\n";
67 # Read an lxc message from a socket.
69 # Returns undef on EOF
70 # Otherwise returns a (type, vmid, value) tuple.
72 # The returned 'type' currently can be 'STATE', 'PRIORITY' or 'EXITSTATUS'.
73 sub read_lxc_message
($) {
77 my $got = recv($socket, $msg, $LXC_MSG_SIZE, 0)
78 // die "failed to read from state socket: $!\n";
80 if (length($msg) == 0) {
84 die "short read on state socket ($LXC_MSG_SIZE != ".length($msg).")\n"
85 if length($msg) != $LXC_MSG_SIZE;
87 my ($type, $name, $value) = _unpack_lxc_msg
($msg);
89 return ($type, $name, $value);