From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id 2E87E602D5 for ; Tue, 8 Sep 2020 13:58:50 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 1F8BC994A for ; Tue, 8 Sep 2020 13:58:50 +0200 (CEST) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [212.186.127.180]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS id 2BE8D9941 for ; Tue, 8 Sep 2020 13:58:49 +0200 (CEST) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id E4B3C44A92 for ; Tue, 8 Sep 2020 13:58:48 +0200 (CEST) From: Fabian Ebner To: pve-devel@lists.proxmox.com Cc: Wolfgang Bumiller Date: Tue, 8 Sep 2020 13:58:42 +0200 Message-Id: <20200908115843.345-1-f.ebner@proxmox.com> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL -0.040 Adjusted score from AWL reputation of From: address KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [config.pm, monitor.pm, create.pm, migrate.pm, tools.pm, setup.pm] Subject: [pve-devel] [PATCH v2 container 1/2] Add module for reading state changes from monitor socket X-BeenThere: pve-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox VE development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 08 Sep 2020 11:58:50 -0000 Will be used to monitor state changes on container startup. Co-developed-by: Wolfgang Bumiller Signed-off-by: Fabian Ebner --- New in v2. I hard-coded the name of the abstract UNIX socket instead of trying to re-implement lxc/monitor.c's lxc_monitor_sock_name function. For us, is it true that the lxcpath is always '/varl/lib/lxc'? Otherwise this won't always work. src/PVE/LXC/Makefile | 1 + src/PVE/LXC/Monitor.pm | 92 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+) create mode 100644 src/PVE/LXC/Monitor.pm diff --git a/src/PVE/LXC/Makefile b/src/PVE/LXC/Makefile index f4f4dc1..a190260 100644 --- a/src/PVE/LXC/Makefile +++ b/src/PVE/LXC/Makefile @@ -4,6 +4,7 @@ SOURCES= \ Config.pm \ Create.pm \ Migrate.pm \ + Monitor.pm \ Setup.pm \ Tools.pm diff --git a/src/PVE/LXC/Monitor.pm b/src/PVE/LXC/Monitor.pm new file mode 100644 index 0000000..3a592b5 --- /dev/null +++ b/src/PVE/LXC/Monitor.pm @@ -0,0 +1,92 @@ +# LXC monitor socket + +package PVE::LXC::Monitor; + +use strict; +use warnings; + +use IO::Socket::UNIX; +use Socket qw(SOCK_STREAM); +use POSIX qw(NAME_MAX); + +use constant { + STATE_STOPPED => 0, + STATE_STARTING => 1, + STATE_RUNNING => 2, + STATE_STOPPING => 3, + STATE_ABORTING => 4, + STATE_FREEZING => 5, + STATE_FROZEN => 6, + STATE_THAWED => 7, + MAX_STATE => 8, +}; + +my $LXC_MSG_SIZE = length(pack('I! Z'.(NAME_MAX+1).' x![I] I', 0, "", 0)); +# Unpack an lxc_msg struct. +my sub _unpack_lxc_msg($) { + my ($packet) = @_; + + # struct lxc_msg { + # lxc_msg_type_t type; + # char name[NAME_MAX+1]; + # int value; + # }; + + my ($type, $name, $value) = unpack('I!Z'.(NAME_MAX+1).'I!', $packet); + + if ($type == 0) { + $type = 'STATE'; + } elsif ($type == 1) { + $type = 'PRIORITY'; + } elsif ($type == 2) { + $type = 'EXITCODE'; + } else { + warn "unsupported lxc message type $type received\n"; + $type = undef; + } + + return ($type, $name, $value); +} + +# Opens the monitor socket +# +# Dies on errors +sub get_monitor_socket { + my $socket = IO::Socket::UNIX->new( + Type => SOCK_STREAM(), + # assumes that lxcpath is '/var/lib/lxc', the hex part is a hash of the lxcpath + Peer => "\0lxc/ad055575fe28ddd5//var/lib/lxc", + ); + if (!defined($socket)) { + die "failed to connect to monitor socket: $!\n"; + } + + return $socket; +} + +# Read an lxc message from a socket. +# +# Returns undef on EOF +# Otherwise returns a (type, vmid, value) tuple. +# +# The returned 'type' currently can be 'STATE', 'PRIORITY' or 'EXITSTATUS'. +sub read_lxc_message($) { + my ($socket) = @_; + + my $msg; + my $got = recv($socket, $msg, $LXC_MSG_SIZE, 0) + // die "failed to read from state socket: $!\n"; + + if (length($msg) == 0) { + return undef; + } + + die "short read on state socket ($LXC_MSG_SIZE != ".length($msg).")\n" + if length($msg) != $LXC_MSG_SIZE; + + my ($type, $name, $value) = _unpack_lxc_msg($msg); + + return ($type, $name, $value); +} + +1; -- 2.20.1