From nobody Sun Feb 8 14:22:32 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 170.10.129.124 as permitted sender) client-ip=170.10.129.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1662639938; cv=none; d=zohomail.com; s=zohoarc; b=d95x1fWNP/anoRCVnjvEWvgA55qhZD5p10FON9VEngb8BF8s0ztZh5OJf3Hlt5FytEFcqH8VCeHV20w+G28Q+BpIVuvh0o7nSziq4GrzDsoSKxji+hl9LnShfhmW6JyjWuNGb1LZqe+xE/hYu/IEepQr8x04utcfiaflmFoClrE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1662639938; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=Aub3hE60pC3vjnyPG8KaZZXqcRl0u+hX9ZR5sh0tGYg=; b=MbWVMdij+7KYZD99lijKcSuBTKytNgK9HPLOeHBS2GYNK6nNWHFi1DgxaOwBego7O6zbAZB47CLH3LlwMYPZTYrEnHf7Ih9zavwChRKhVjlSRYkQWwr7LZjFuflQ3dyCLChnAdQWrnYzle9WC+dHily7tSIQNxs14t/ByMlyQV8= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by mx.zohomail.com with SMTPS id 1662639938383390.1254182420064; Thu, 8 Sep 2022 05:25:38 -0700 (PDT) Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-363-yJCvgnPON4KWPF8i8lVE-A-1; Thu, 08 Sep 2022 08:25:33 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 41733294EDE4; Thu, 8 Sep 2022 12:25:10 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (unknown [10.30.29.100]) by smtp.corp.redhat.com (Postfix) with ESMTP id 2150A945DB; Thu, 8 Sep 2022 12:25:10 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (localhost [IPv6:::1]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id E1AFD1946A4A; Thu, 8 Sep 2022 12:25:09 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id B32F01946A41 for ; Thu, 8 Sep 2022 12:25:08 +0000 (UTC) Received: by smtp.corp.redhat.com (Postfix) id A893C2166B2B; Thu, 8 Sep 2022 12:25:08 +0000 (UTC) Received: from speedmetal.lan (unknown [10.40.208.30]) by smtp.corp.redhat.com (Postfix) with ESMTP id F40062166B29 for ; Thu, 8 Sep 2022 12:25:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1662639937; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=Aub3hE60pC3vjnyPG8KaZZXqcRl0u+hX9ZR5sh0tGYg=; b=XGiE5lB2A0IF3z3Wfh6LIaZNkCJTBG1Ao8aD5MKb1eVjLaONDs08YsFPLaAOLn3PDyRt4d XJabBfyl3EofAhCVSACU+srqVufFSt+Lsal1WnmpWNmIEXm1uXlZYj0rd/zbRgztQzoYXB akr5i4c8JIyYTiGbKV14g3q937vn9A8= X-MC-Unique: yJCvgnPON4KWPF8i8lVE-A-1 X-Original-To: libvir-list@listman.corp.redhat.com From: Peter Krempa To: libvir-list@redhat.com Subject: [PATCH 2/5] lxc: process: Rework reading errors from the log file Date: Thu, 8 Sep 2022 14:25:01 +0200 Message-Id: <84ca109e44d079f9fc6f03dc948b94f0a7a1df80.1662639767.git.pkrempa@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.78 on 10.11.54.6 X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libvir-list-bounces@redhat.com Sender: "libvir-list" X-Scanned-By: MIMEDefang 2.79 on 10.11.54.5 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1662639938689100004 Content-Type: text/plain; charset="utf-8" Introduce 'virLXCProcessReportStartupLogError' which simplifies the error handling on startup of the LXC process when reading of the error log is needed. This function has unusual return value semantics but it helps to make the callers simpler. This patch also removes 2 1k stack'd buffers from virLXCProcessStart. Signed-off-by: Peter Krempa --- src/lxc/lxc_process.c | 90 ++++++++++++++++++++++++++----------------- 1 file changed, 55 insertions(+), 35 deletions(-) diff --git a/src/lxc/lxc_process.c b/src/lxc/lxc_process.c index 39dcf53c67..2a753ae1da 100644 --- a/src/lxc/lxc_process.c +++ b/src/lxc/lxc_process.c @@ -1096,6 +1096,43 @@ virLXCProcessReadLogOutput(virDomainObj *vm, } +/** + * virLXCProcessReportStartupLogError: + * @vm: domain object + * @logfile: path to the VM logfile + * @pos: position in @logfile to look for errors + * + * Looks for the error message from the LXC container process. + * Returns: + * -1: - When reading of error message failed. Reports appropriate error + * - When successfully read a non-empty error message. Reports an erro= r with + * the following message + * 'guest failed to start: ' with the error from the log appended + * + * 0: - When reading the error was successful but the error log was empty. + */ +static int +virLXCProcessReportStartupLogError(virDomainObj *vm, + char *logfile, + off_t pos) +{ + size_t buflen =3D 1024; + g_autofree char *errbuf =3D g_new0(char, buflen); + int rc; + + if ((rc =3D virLXCProcessReadLogOutput(vm, logfile, pos, errbuf, bufle= n)) < 0) + return -1; + + if (rc =3D=3D 0) + return 0; + + virReportError(VIR_ERR_INTERNAL_ERROR, + _("guest failed to start: %s"), errbuf); + + return -1; +} + + static int virLXCProcessEnsureRootFS(virDomainObj *vm) { @@ -1151,7 +1188,6 @@ int virLXCProcessStart(virLXCDriver * driver, g_auto(GStrv) veths =3D NULL; int handshakefds[4] =3D { -1, -1, -1, -1 }; /* two pipes */ off_t pos =3D -1; - char ebuf[1024]; g_autofree char *timestamp =3D NULL; int nsInheritFDs[VIR_LXC_DOMAIN_NAMESPACE_LAST]; g_autoptr(virCommand) cmd =3D NULL; @@ -1368,28 +1404,28 @@ int virLXCProcessStart(virLXCDriver * driver, goto cleanup; if (status !=3D 0) { - if (virLXCProcessReadLogOutput(vm, logfile, pos, ebuf, - sizeof(ebuf)) <=3D 0) { - if (WIFEXITED(status)) - g_snprintf(ebuf, sizeof(ebuf), _("unexpected exit status %= d"), + if (virLXCProcessReportStartupLogError(vm, logfile, pos) < 0) + goto cleanup; + + /* In case there isn't an error in the logs report one based on th= e exit status */ + if (WIFEXITED(status)) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("guest failed to start: unexpected exit statu= s %d"), WEXITSTATUS(status)); - else - g_snprintf(ebuf, sizeof(ebuf), "%s", _("terminated abnorma= lly")); + } else { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("guest failed to start: terminated abnormally= ")); } - virReportError(VIR_ERR_INTERNAL_ERROR, - _("guest failed to start: %s"), ebuf); goto cleanup; } /* It has started running, so get its pid */ if ((r =3D virPidFileReadPath(pidfile, &vm->pid)) < 0) { - if (virLXCProcessReadLogOutput(vm, logfile, pos, ebuf, sizeof(ebuf= )) > 0) - virReportError(VIR_ERR_INTERNAL_ERROR, - _("guest failed to start: %s"), ebuf); - else - virReportSystemError(-r, - _("Failed to read pid file %s"), - pidfile); + if (virLXCProcessReportStartupLogError(vm, logfile, pos) < 0) + goto cleanup; + + /* In case there isn't an error in the logs report that we failed = to read the pidfile */ + virReportSystemError(-r, _("Failed to read pid file %s"), pidfile); goto cleanup; } @@ -1422,13 +1458,7 @@ int virLXCProcessStart(virLXCDriver * driver, /* The first synchronization point is when the controller creates CGro= ups. */ if (lxcContainerWaitForContinue(handshakefds[0]) < 0) { - char out[1024]; - - if (!(virLXCProcessReadLogOutput(vm, logfile, pos, out, 1024) < 0)= ) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("guest failed to start: %s"), out); - } - + virLXCProcessReportStartupLogError(vm, logfile, pos); goto cleanup; } @@ -1461,13 +1491,7 @@ int virLXCProcessStart(virLXCDriver * driver, /* The second synchronization point is when the controller finished * creating the container. */ if (lxcContainerWaitForContinue(handshakefds[0]) < 0) { - char out[1024]; - - if (!(virLXCProcessReadLogOutput(vm, logfile, pos, out, 1024) < 0)= ) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("guest failed to start: %s"), out); - } - + virLXCProcessReportStartupLogError(vm, logfile, pos); goto cleanup; } @@ -1476,11 +1500,7 @@ int virLXCProcessStart(virLXCDriver * driver, /* Intentionally overwrite the real monitor error message, * since a better one is almost always found in the logs */ - if (virLXCProcessReadLogOutput(vm, logfile, pos, ebuf, sizeof(ebuf= )) > 0) { - virResetLastError(); - virReportError(VIR_ERR_INTERNAL_ERROR, - _("guest failed to start: %s"), ebuf); - } + virLXCProcessReportStartupLogError(vm, logfile, pos); goto cleanup; } --=20 2.37.1