From patchwork Fri Nov 13 14:18:23 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: "Doebel, Bjoern" X-Patchwork-Id: 11903565 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id BDF48138B for ; Fri, 13 Nov 2020 14:22:45 +0000 (UTC) Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 80C9222226 for ; Fri, 13 Nov 2020 14:22:45 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=amazon.de header.i=@amazon.de header.b="rG5BmM2/" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 80C9222226 Authentication-Results: mail.kernel.org; dmarc=fail (p=quarantine dis=none) header.from=amazon.de Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=xen-devel-bounces@lists.xenproject.org Received: from list by lists.xenproject.org with outflank-mailman.26515.54925 (Exim 4.92) (envelope-from ) id 1kdZwl-0000Vm-Lt; Fri, 13 Nov 2020 14:21:03 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 26515.54925; Fri, 13 Nov 2020 14:21:03 +0000 X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1kdZwl-0000Vf-IE; Fri, 13 Nov 2020 14:21:03 +0000 Received: by outflank-mailman (input) for mailman id 26515; Fri, 13 Nov 2020 14:19:25 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1kdZvA-0008EU-NP for xen-devel@lists.xenproject.org; Fri, 13 Nov 2020 14:19:25 +0000 Received: from smtp-fw-6002.amazon.com (unknown [52.95.49.90]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 21da5738-23df-4304-a15c-d31f7b61fd64; Fri, 13 Nov 2020 14:19:23 +0000 (UTC) Received: from iad12-co-svc-p1-lb1-vlan3.amazon.com (HELO email-inbound-relay-2c-4e7c8266.us-west-2.amazon.com) ([10.43.8.6]) by smtp-border-fw-out-6002.iad6.amazon.com with ESMTP; 13 Nov 2020 14:19:01 +0000 Received: from EX13D37EUB004.ant.amazon.com (pdx1-ws-svc-p6-lb9-vlan2.pdx.amazon.com [10.236.137.194]) by email-inbound-relay-2c-4e7c8266.us-west-2.amazon.com (Postfix) with ESMTPS id F3D6BA0819; Fri, 13 Nov 2020 14:19:00 +0000 (UTC) Received: from EX13MTAUEA001.ant.amazon.com (10.43.61.82) by EX13D37EUB004.ant.amazon.com (10.43.166.187) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Fri, 13 Nov 2020 14:18:59 +0000 Received: from dev-dsk-doebel-2a-b41c32f5.us-west-2.amazon.com (172.19.225.92) by mail-relay.amazon.com (10.43.61.243) with Microsoft SMTP Server id 15.0.1497.2 via Frontend Transport; Fri, 13 Nov 2020 14:18:58 +0000 Received: by dev-dsk-doebel-2a-b41c32f5.us-west-2.amazon.com (Postfix, from userid 3160037) id 1FE8EA27B6; Fri, 13 Nov 2020 14:18:57 +0000 (UTC) Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1kdZvA-0008EU-NP for xen-devel@lists.xenproject.org; Fri, 13 Nov 2020 14:19:25 +0000 X-Inumbo-ID: 21da5738-23df-4304-a15c-d31f7b61fd64 Received: from smtp-fw-6002.amazon.com (unknown [52.95.49.90]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 21da5738-23df-4304-a15c-d31f7b61fd64; Fri, 13 Nov 2020 14:19:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.de; i=@amazon.de; q=dns/txt; s=amazon201209; t=1605277163; x=1636813163; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=uEC5EXOc4YcDJzP4U01LmoW19H30dbThY1jdb+RYew8=; b=rG5BmM2/12ebjmGn0qKQtjHPY3qSIbT8sydr905NI8H8upBsgWq+GNqY SdQpZ9iWwx/X2J4N5M082D9c5CPFAFHmKSvSfOUuEzxX9YiJlot7NBK1l 4nvbhDCzTZjoxmapaEsMiu6rT/Ytei4VA+E5z3Vo9PLLcXOMDLfMnda1Z c=; X-IronPort-AV: E=Sophos;i="5.77,475,1596499200"; d="scan'208";a="64882752" Received: from iad12-co-svc-p1-lb1-vlan3.amazon.com (HELO email-inbound-relay-2c-4e7c8266.us-west-2.amazon.com) ([10.43.8.6]) by smtp-border-fw-out-6002.iad6.amazon.com with ESMTP; 13 Nov 2020 14:19:01 +0000 Received: from EX13D37EUB004.ant.amazon.com (pdx1-ws-svc-p6-lb9-vlan2.pdx.amazon.com [10.236.137.194]) by email-inbound-relay-2c-4e7c8266.us-west-2.amazon.com (Postfix) with ESMTPS id F3D6BA0819; Fri, 13 Nov 2020 14:19:00 +0000 (UTC) Received: from EX13MTAUEA001.ant.amazon.com (10.43.61.82) by EX13D37EUB004.ant.amazon.com (10.43.166.187) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Fri, 13 Nov 2020 14:18:59 +0000 Received: from dev-dsk-doebel-2a-b41c32f5.us-west-2.amazon.com (172.19.225.92) by mail-relay.amazon.com (10.43.61.243) with Microsoft SMTP Server id 15.0.1497.2 via Frontend Transport; Fri, 13 Nov 2020 14:18:58 +0000 Received: by dev-dsk-doebel-2a-b41c32f5.us-west-2.amazon.com (Postfix, from userid 3160037) id 1FE8EA27B6; Fri, 13 Nov 2020 14:18:57 +0000 (UTC) From: Bjoern Doebel To: CC: Julien Grall , Eslam Elnikety , Bjoern Doebel , Ian Jackson , Wei Liu Subject: [XEN PATCH] tools/xenstore: Log xenstored build ID on startup Date: Fri, 13 Nov 2020 14:18:23 +0000 Message-ID: <20201113141823.58712-1-doebel@amazon.de> X-Mailer: git-send-email 2.16.6 MIME-Version: 1.0 Precedence: Bulk Right now we do not have a mechanism to determine the version of the currently running xenstored at runtime. As xenstored runs throughout the lifetime of a Xen host, this may lead to problems when newer user space builds are staged. Then, the running xenstored will no longer match the version of the installed xenstored. To allow users to always identify the running version of xenstored, add a linker-generated unique build ID to every xenstored build. Add functionality to log this build ID into a file upon service startup. Signed-off-by: Bjoern Doebel Reviewed-by: Martin Mazein Reviewed-by: Paul Durrant --- tools/hotplug/Linux/launch-xenstore.in | 2 +- tools/xenstore/Makefile | 4 +++ tools/xenstore/buildid_symbols.ld | 10 +++++++ tools/xenstore/xenstored_core.c | 8 ++++++ tools/xenstore/xenstored_core.h | 3 ++ tools/xenstore/xenstored_minios.c | 4 +++ tools/xenstore/xenstored_posix.c | 52 ++++++++++++++++++++++++++++++++++ 7 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 tools/xenstore/buildid_symbols.ld diff --git a/tools/hotplug/Linux/launch-xenstore.in b/tools/hotplug/Linux/launch-xenstore.in index 991dec8d25..a6f2254030 100644 --- a/tools/hotplug/Linux/launch-xenstore.in +++ b/tools/hotplug/Linux/launch-xenstore.in @@ -62,7 +62,7 @@ test -f @CONFIG_DIR@/@CONFIG_LEAF_DIR@/xencommons && . @CONFIG_DIR@/@CONFIG_LEAF } echo -n Starting $XENSTORED... - $XENSTORED --pid-file @XEN_RUN_DIR@/xenstored.pid $XENSTORED_ARGS + $XENSTORED --pid-file @XEN_RUN_DIR@/xenstored.pid --buildid-file @XEN_RUN_DIR@/xenstored.buildid $XENSTORED_ARGS systemd-notify --booted 2>/dev/null || timeout_xenstore $XENSTORED || exit 1 diff --git a/tools/xenstore/Makefile b/tools/xenstore/Makefile index 9a0f0d012d..c63350980b 100644 --- a/tools/xenstore/Makefile +++ b/tools/xenstore/Makefile @@ -66,6 +66,10 @@ $(XENSTORED_OBJS): CFLAGS += $(SYSTEMD_CFLAGS) xenstored: LDFLAGS += $(SYSTEMD_LIBS) endif +# xenstored: enforce creation of a buildID section and use a linker +# script to add additional symbols around that section +xenstored: LDFLAGS += -Wl,--build-id=sha1 -Wl,-T,buildid_symbols.ld + $(XENSTORED_OBJS): CFLAGS += $(CFLAGS_libxengnttab) xenstored: $(XENSTORED_OBJS) diff --git a/tools/xenstore/buildid_symbols.ld b/tools/xenstore/buildid_symbols.ld new file mode 100644 index 0000000000..d74024c4e9 --- /dev/null +++ b/tools/xenstore/buildid_symbols.ld @@ -0,0 +1,10 @@ +SECTIONS +{ + __buildid_note_section = . ; + .note.gnu.build-id : + { + *(.note.gnu.build-id) + } + __buildid_end = . ; +} +INSERT AFTER .data diff --git a/tools/xenstore/xenstored_core.c b/tools/xenstore/xenstored_core.c index b4be374d3f..c6f107bdd9 100644 --- a/tools/xenstore/xenstored_core.c +++ b/tools/xenstore/xenstored_core.c @@ -1844,6 +1844,7 @@ static void usage(void) static struct option options[] = { + { "buildid-file", 1, NULL, 'B' }, { "no-domain-init", 0, NULL, 'D' }, { "entry-nb", 1, NULL, 'E' }, { "pid-file", 1, NULL, 'F' }, @@ -1875,12 +1876,16 @@ int main(int argc, char *argv[]) bool outputpid = false; bool no_domain_init = false; const char *pidfile = NULL; + const char *buildid_file = NULL; int timeout; while ((opt = getopt_long(argc, argv, "DE:F:HNPS:t:T:RVW:", options, NULL)) != -1) { switch (opt) { + case 'B': + buildid_file = optarg; + break; case 'D': no_domain_init = true; break; @@ -1948,6 +1953,9 @@ int main(int argc, char *argv[]) if (pidfile) write_pidfile(pidfile); + if (buildid_file) + write_buildid_file(buildid_file); + /* Talloc leak reports go to stderr, which is closed if we fork. */ if (!dofork) talloc_enable_leak_report_full(); diff --git a/tools/xenstore/xenstored_core.h b/tools/xenstore/xenstored_core.h index 1df6ad94ab..712280626c 100644 --- a/tools/xenstore/xenstored_core.h +++ b/tools/xenstore/xenstored_core.h @@ -193,6 +193,9 @@ void xenbus_notify_running(void); /* Write out the pidfile */ void write_pidfile(const char *pidfile); +/* Write the buildid file */ +void write_buildid_file(const char *buildidfile); + /* Fork but do not close terminal FDs */ void daemonize(void); /* Close stdin/stdout/stderr to complete daemonize */ diff --git a/tools/xenstore/xenstored_minios.c b/tools/xenstore/xenstored_minios.c index c94493e52a..ef1151aee4 100644 --- a/tools/xenstore/xenstored_minios.c +++ b/tools/xenstore/xenstored_minios.c @@ -24,6 +24,10 @@ void write_pidfile(const char *pidfile) { } +void write_buildid_file(const char *buildid_file) +{ +} + void daemonize(void) { } diff --git a/tools/xenstore/xenstored_posix.c b/tools/xenstore/xenstored_posix.c index 1f9603fea2..ec017611d6 100644 --- a/tools/xenstore/xenstored_posix.c +++ b/tools/xenstore/xenstored_posix.c @@ -20,6 +20,7 @@ #include #include #include +#include #include #include @@ -48,6 +49,57 @@ void write_pidfile(const char *pidfile) close(fd); } +/* + * We don't have a working elf.h available here, so let's define our very own + * data structs and accessor macros for ELF notes. + * + * https://docs.oracle.com/cd/E23824_01/html/819-0690/chapter6-18048.html: + * For 64–bit objects and 32–bit objects, each entry is an array of 4-byte + * words in the format of the target processor. + */ +typedef struct +{ + uint32_t namesz; + uint32_t descsz; + uint32_t type; +} elf_note_hdr; + +/* ELF Note accessors, copied from Xen's elf.h */ +#define ELFNOTE_ALIGN(_n_) (((_n_)+3)&~3) +#define ELFNOTE_NAME(_n_) ((char*)(_n_) + sizeof(*(_n_))) +#define ELFNOTE_DESC(_n_) (ELFNOTE_NAME(_n_) + ELFNOTE_ALIGN((_n_)->namesz)) +/* GNU LD: type == note (NT_GNU_BUILD_ID as in + * https://sourceware.org/ml/binutils/2007-07/msg00012.html)*/ +#define NT_GNU_BUILD_ID 3 + + +void write_buildid_file(const char *buildid_file) +{ + unsigned int i = 0; + FILE *fdesc; + extern elf_note_hdr __buildid_note_section; + unsigned int id_length = __buildid_note_section.descsz; + char* desc = ELFNOTE_DESC(&__buildid_note_section); + + if (__buildid_note_section.type != NT_GNU_BUILD_ID) + barf("Expected GNU_BUILDID note, but found type '%d'", + __buildid_note_section.type); + + fdesc = fopen(buildid_file, "w+"); + if (!fdesc) + barf_perror("Error opening buildid file %s", buildid_file); + + /* We exit silently if daemon already running. */ + if (lockf(fileno(fdesc), F_TLOCK, 0) == -1) + exit(0); + + for (i = 0; i < id_length; ++i) + fprintf(fdesc, "%02x", (unsigned char)desc[i]); + fprintf(fdesc, "\n"); + + fclose(fdesc); +} + /* Stevens. */ void daemonize(void) {