From nobody Sun Dec 22 07:52:16 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) client-ip=8.43.85.245; envelope-from=devel-bounces@lists.libvirt.org; helo=lists.libvirt.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) smtp.mailfrom=devel-bounces@lists.libvirt.org; dmarc=fail(p=none dis=none) header.from=gmail.com Return-Path: Received: from lists.libvirt.org (lists.libvirt.org [8.43.85.245]) by mx.zohomail.com with SMTPS id 1734628149266595.5149768989447; Thu, 19 Dec 2024 09:09:09 -0800 (PST) Received: by lists.libvirt.org (Postfix, from userid 996) id 32AAE17CD; Thu, 19 Dec 2024 12:09:08 -0500 (EST) Received: from lists.libvirt.org (localhost [IPv6:::1]) by lists.libvirt.org (Postfix) with ESMTP id 52C68178D; Thu, 19 Dec 2024 12:08:06 -0500 (EST) Received: by lists.libvirt.org (Postfix, from userid 996) id 19A8E167B; Thu, 19 Dec 2024 12:08:01 -0500 (EST) Received: from mail-oi1-f178.google.com (mail-oi1-f178.google.com [209.85.167.178]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by lists.libvirt.org (Postfix) with ESMTPS id 1B7A81786 for ; Thu, 19 Dec 2024 12:08:00 -0500 (EST) Received: by mail-oi1-f178.google.com with SMTP id 5614622812f47-3eb98b3b63dso236972b6e.1 for ; Thu, 19 Dec 2024 09:08:00 -0800 (PST) Received: from home.. ([2603:8081:c640:1::1009]) by smtp.gmail.com with ESMTPSA id 46e09a7af769-71fc9807c0fsm325938a34.41.2024.12.19.09.07.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 Dec 2024 09:07:58 -0800 (PST) X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on lists.libvirt.org X-Spam-Level: X-Spam-Status: No, score=-1.7 required=5.0 tests=DKIM_ADSP_CUSTOM_MED, DKIM_INVALID,DKIM_SIGNED,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,RCVD_IN_VALIDITY_RPBL_BLOCKED, RCVD_IN_VALIDITY_SAFE_BLOCKED,SPF_HELO_NONE autolearn=unavailable autolearn_force=no version=3.4.4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1734628079; x=1735232879; darn=lists.libvirt.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=vLoipIr19aqgOYUoX04EOX9OlfX16/Rd2xB6gmj+nmI=; b=IQTP96wx+npAGGrOAJ9aL516YaHlqfWm+pl4Vs1CuQSqAh7zBomQRzkPV2pCLl/mlf VA5vsPjOApYIOj1JmNNwMOB1Vh49zo+kwbDDIUioVcNG0nuj02TgDHQXzo069RQDK4Es 1d2OSMVGN4hjNG9JQ+lfW/ZTCzI33q9ZQI31e51zaaxcNH1H+gmsTJDqNh9ALyGiknPt Ra8I4qe5I5/QcXcBX0uU+tZ8k4jYFeMlnq1ISEmw3DJ+MfigZHqyXp+Oe6sPMSRmZyjw 70+Uq12xU+ziC6RIOfsqpZr3HYqmHuTOq/iO+cu8Ng1zrDGJJn/yDyefdhwVrBk5oWyM Pdkg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734628079; x=1735232879; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=vLoipIr19aqgOYUoX04EOX9OlfX16/Rd2xB6gmj+nmI=; b=Q9DIT+aMaXrlmn5zsK+ndxp2JdV3ZsgvtToTLq/VN/FPDoWFbJ1/NK1zq6G6QYK26V jdIKoAaas6h6dv5CZUBXDxGpVSk0RAy5BTxCymNMsOYpuVuCD6FmDd9fcR/du89BurVe eg8K33arSG/eJJaBWeMfRCJFKN2/YgXL3jmf4PnoLtMC/p3pGZVgkLecjBj+I4hBSf9v glXxdvVIcfNOxQZS/3ZL1MB7cEV74nf71jfowcWvKMnxvugM+D3VKzXWeYApdfhoukdc 1VRkJwsyQ0F5fthWP3r9RI9EeVL/KhGyoS/t1/naADzP5FRWGSlsQ4rV1RgrFy7VCV17 CapA== X-Gm-Message-State: AOJu0Ywi0at8u4sjTezEGDDS3gI4yOCaGn2Ulej9FTLsmSzCQtqO9DBa /+F4XciXsGtDLNZUKkUvkTUS2fh4UgFOQJy/DtRE6Y7BbAyVKx9ZPfpzLtg/ X-Gm-Gg: ASbGncumbBRNjzjKx/9I9b0obQO70ftXvSE6OCw4eZu7GCZHKVi03cbDE5yDmwO2jHE mkQ6lsTf0xcQm+qRkwYpdJuDePgaLYA+81ju1VDkiKoMfYuAfFFID2R5S9J1KyguuVLLuog+DUR aDolhA2PnQ+T+WmmXsRrc/fo24NC2KWJ7gCyUhxjhZCK5gPZXUbQQXKIpNOdlhW0qAqX6hSugrT e3wHllULmKUJ4QRqA/4AdNWMTz0Hp2eClk8z1vi X-Google-Smtp-Source: AGHT+IEeuE8AAp+rjL10P1HNwR/M9kEWZiOk1ECJ6M90j5JcvGbkC7v7WpGObzX8xze19FacBVXw8g== X-Received: by 2002:a05:6808:e88:b0:3eb:5d13:f688 with SMTP id 5614622812f47-3ecdcd55d14mr2851900b6e.26.1734628078811; Thu, 19 Dec 2024 09:07:58 -0800 (PST) From: Praveen K Paladugu To: devel@lists.libvirt.org Subject: [PATCH v2 01/11] hypervisor: copy qemu log context mgmt to hypervisor Date: Thu, 19 Dec 2024 11:07:46 -0600 Message-ID: <20241219170756.30810-2-praveenkpaladugu@gmail.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241219170756.30810-1-praveenkpaladugu@gmail.com> References: <20241219170756.30810-1-praveenkpaladugu@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Message-ID-Hash: DM2Z3ZSPDLMSQGU6B3WZOTY2FZUYERH3 X-Message-ID-Hash: DM2Z3ZSPDLMSQGU6B3WZOTY2FZUYERH3 X-MailFrom: praveenkpaladugu@gmail.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-config-1; header-match-config-2; header-match-config-3; header-match-devel.lists.libvirt.org-0; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; suspicious-header CC: liuwe@microsoft.com X-Mailman-Version: 3.2.2 Precedence: list List-Id: Development discussions about the libvirt library & tools Archived-At: List-Archive: List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1734628151146116600 Content-Type: text/plain; charset="utf-8" Signed-off-by: Praveen K Paladugu --- po/POTFILES | 1 + src/hypervisor/domain_logcontext.c | 329 +++++++++++++++++++++++++++++ src/hypervisor/domain_logcontext.h | 41 ++++ 3 files changed, 371 insertions(+) create mode 100644 src/hypervisor/domain_logcontext.c create mode 100644 src/hypervisor/domain_logcontext.h diff --git a/po/POTFILES b/po/POTFILES index 3514aa3dca..1d3c3c0a81 100644 --- a/po/POTFILES +++ b/po/POTFILES @@ -94,6 +94,7 @@ src/hyperv/hyperv_util.c src/hyperv/hyperv_wmi.c src/hypervisor/domain_cgroup.c src/hypervisor/domain_driver.c +src/hypervisor/domain_logcontext.c src/hypervisor/domain_interface.c src/hypervisor/virhostdev.c src/interface/interface_backend_netcf.c diff --git a/src/hypervisor/domain_logcontext.c b/src/hypervisor/domain_log= context.c new file mode 100644 index 0000000000..7932cf7712 --- /dev/null +++ b/src/hypervisor/domain_logcontext.c @@ -0,0 +1,329 @@ +/* + * domain_logcontext.c: QEMU log context + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see + * . + */ + +#include + +#include "qemu_logcontext.h" +#include "viralloc.h" +#include "virlog.h" +#include "virstring.h" +#include "virutil.h" + +#include + +#define VIR_FROM_THIS VIR_FROM_QEMU + +VIR_LOG_INIT("qemu.qemu_logcontext"); + + +struct _qemuLogContext { + GObject parent; + + int writefd; + int readfd; /* Only used if manager =3D=3D NULL */ + off_t pos; + ino_t inode; /* Only used if manager !=3D NULL */ + char *path; + virLogManager *manager; +}; + +G_DEFINE_TYPE(qemuLogContext, qemu_log_context, G_TYPE_OBJECT); + +static void +qemuLogContextFinalize(GObject *obj); + + +static void +qemu_log_context_init(qemuLogContext *logctxt G_GNUC_UNUSED) +{ +} + + +static void +qemu_log_context_class_init(qemuLogContextClass *klass) +{ + GObjectClass *obj =3D G_OBJECT_CLASS(klass); + + obj->finalize =3D qemuLogContextFinalize; +} + + +static void +qemuLogContextFinalize(GObject *object) +{ + qemuLogContext *ctxt =3D QEMU_LOG_CONTEXT(object); + VIR_DEBUG("ctxt=3D%p", ctxt); + + virLogManagerFree(ctxt->manager); + VIR_FREE(ctxt->path); + VIR_FORCE_CLOSE(ctxt->writefd); + VIR_FORCE_CLOSE(ctxt->readfd); + G_OBJECT_CLASS(qemu_log_context_parent_class)->finalize(object); +} + + +qemuLogContext * +qemuLogContextNew(virQEMUDriver *driver, + virDomainObj *vm, + const char *basename) +{ + g_autoptr(virQEMUDriverConfig) cfg =3D virQEMUDriverGetConfig(driver); + qemuLogContext *ctxt =3D QEMU_LOG_CONTEXT(g_object_new(QEMU_TYPE_LOG_C= ONTEXT, NULL)); + + VIR_DEBUG("Context new %p stdioLogD=3D%d", ctxt, cfg->stdioLogD); + ctxt->writefd =3D -1; + ctxt->readfd =3D -1; + + ctxt->path =3D g_strdup_printf("%s/%s.log", cfg->logDir, basename); + + if (cfg->stdioLogD) { + ctxt->manager =3D virLogManagerNew(driver->privileged); + if (!ctxt->manager) + goto error; + + ctxt->writefd =3D virLogManagerDomainOpenLogFile(ctxt->manager, + "qemu", + vm->def->uuid, + vm->def->name, + ctxt->path, + 0, + &ctxt->inode, + &ctxt->pos); + if (ctxt->writefd < 0) + goto error; + } else { + if ((ctxt->writefd =3D open(ctxt->path, O_WRONLY | O_CREAT | O_APP= END, S_IRUSR | S_IWUSR)) < 0) { + virReportSystemError(errno, _("failed to create logfile %1$s"), + ctxt->path); + goto error; + } + if (virSetCloseExec(ctxt->writefd) < 0) { + virReportSystemError(errno, _("failed to set close-on-exec fla= g on %1$s"), + ctxt->path); + goto error; + } + + /* For unprivileged startup we must truncate the file since + * we can't rely on logrotate. We don't use O_TRUNC since + * it is better for SELinux policy if we truncate afterwards */ + if (!driver->privileged && + ftruncate(ctxt->writefd, 0) < 0) { + virReportSystemError(errno, _("failed to truncate %1$s"), + ctxt->path); + goto error; + } + + if ((ctxt->readfd =3D open(ctxt->path, O_RDONLY)) < 0) { + virReportSystemError(errno, _("failed to open logfile %1$s"), + ctxt->path); + goto error; + } + if (virSetCloseExec(ctxt->readfd) < 0) { + virReportSystemError(errno, _("failed to set close-on-exec fla= g on %1$s"), + ctxt->path); + goto error; + } + + if ((ctxt->pos =3D lseek(ctxt->writefd, 0, SEEK_END)) < 0) { + virReportSystemError(errno, _("failed to seek in log file %1$s= "), + ctxt->path); + goto error; + } + } + + return ctxt; + + error: + g_clear_object(&ctxt); + return NULL; +} + + +int +qemuLogContextWrite(qemuLogContext *ctxt, + const char *fmt, ...) +{ + va_list argptr; + g_autofree char *message =3D NULL; + int ret =3D -1; + + va_start(argptr, fmt); + + message =3D g_strdup_vprintf(fmt, argptr); + if (!ctxt->manager && + lseek(ctxt->writefd, 0, SEEK_END) < 0) { + virReportSystemError(errno, "%s", + _("Unable to seek to end of domain logfile")); + goto cleanup; + } + if (safewrite(ctxt->writefd, message, strlen(message)) < 0) { + virReportSystemError(errno, "%s", + _("Unable to write to domain logfile")); + goto cleanup; + } + + ret =3D 0; + + cleanup: + va_end(argptr); + return ret; +} + + +ssize_t +qemuLogContextRead(qemuLogContext *ctxt, + char **msg) +{ + char *buf; + size_t buflen; + + VIR_DEBUG("Context read %p manager=3D%p inode=3D%llu pos=3D%llu", + ctxt, ctxt->manager, + (unsigned long long)ctxt->inode, + (unsigned long long)ctxt->pos); + + if (ctxt->manager) { + buf =3D virLogManagerDomainReadLogFile(ctxt->manager, + ctxt->path, + ctxt->inode, + ctxt->pos, + 1024 * 128, + 0); + if (!buf) + return -1; + buflen =3D strlen(buf); + } else { + ssize_t got; + + buflen =3D 1024 * 128; + + /* Best effort jump to start of messages */ + ignore_value(lseek(ctxt->readfd, ctxt->pos, SEEK_SET)); + + buf =3D g_new0(char, buflen); + + got =3D saferead(ctxt->readfd, buf, buflen - 1); + if (got < 0) { + VIR_FREE(buf); + virReportSystemError(errno, "%s", + _("Unable to read from log file")); + return -1; + } + + buf[got] =3D '\0'; + + buf =3D g_renew(char, buf, got + 1); + buflen =3D got; + } + + *msg =3D buf; + + return buflen; +} + + +/** + * qemuLogContextFilter: Read and filter log for relevant messages + * @ctxt: the domain log context + * @msg: pointer to buffer to store the read messages in + * @max: maximum length of the message returned in @msg after filtering + * + * Reads log output from @ctxt and filters it. Skips messages not produced= by + * the target executable or irrelevant messages. If @max is not zero, @buf= will + * contain at most @max characters from the end of the log and @buf will s= tart + * after a new line if possible. + */ +int +qemuLogContextReadFiltered(qemuLogContext *ctxt, + char **msg, + size_t max) +{ + char *buf; + char *eol; + char *filter_next; + size_t skip; + ssize_t got; + + if ((got =3D qemuLogContextRead(ctxt, &buf)) < 0) + return -1; + + /* Filter out debug messages from intermediate libvirt process */ + filter_next =3D buf; + while ((eol =3D strchr(filter_next, '\n'))) { + *eol =3D '\0'; + if (virLogProbablyLogMessage(filter_next) || + strstr(filter_next, "char device redirected to")) { + skip =3D (eol + 1) - filter_next; + memmove(filter_next, eol + 1, buf + got - eol); + got -=3D skip; + } else { + filter_next =3D eol + 1; + *eol =3D '\n'; + } + } + + if (got > 0 && + buf[got - 1] =3D=3D '\n') { + buf[got - 1] =3D '\0'; + got--; + } + + if (max > 0 && got > max) { + skip =3D got - max; + + if (buf[skip - 1] !=3D '\n' && + (eol =3D strchr(buf + skip, '\n')) && + !virStringIsEmpty(eol + 1)) + skip =3D eol + 1 - buf; + + memmove(buf, buf + skip, got - skip + 1); + got -=3D skip; + } + + buf =3D g_renew(char, buf, got + 1); + *msg =3D buf; + return 0; +} + + +int +qemuLogContextGetWriteFD(qemuLogContext *ctxt) +{ + return ctxt->writefd; +} + + +void +qemuLogContextMarkPosition(qemuLogContext *ctxt) +{ + if (ctxt->manager) + virLogManagerDomainGetLogFilePosition(ctxt->manager, + ctxt->path, + 0, + &ctxt->inode, + &ctxt->pos); + else + ctxt->pos =3D lseek(ctxt->writefd, 0, SEEK_END); +} + + +virLogManager * +qemuLogContextGetManager(qemuLogContext *ctxt) +{ + return ctxt->manager; +} diff --git a/src/hypervisor/domain_logcontext.h b/src/hypervisor/domain_log= context.h new file mode 100644 index 0000000000..572ac1026e --- /dev/null +++ b/src/hypervisor/domain_logcontext.h @@ -0,0 +1,41 @@ +/* + * domain_logcontext.h: QEMU log context + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see + * . + */ + +#pragma once + +#include +#include "qemu_conf.h" +#include "logging/log_manager.h" + +#define QEMU_TYPE_LOG_CONTEXT qemu_log_context_get_type() +G_DECLARE_FINAL_TYPE(qemuLogContext, qemu_log_context, QEMU, LOG_CONTEXT, = GObject); + +qemuLogContext *qemuLogContextNew(virQEMUDriver *driver, + virDomainObj *vm, + const char *basename); +int qemuLogContextWrite(qemuLogContext *ctxt, + const char *fmt, ...) G_GNUC_PRINTF(2, 3); +ssize_t qemuLogContextRead(qemuLogContext *ctxt, + char **msg); +int qemuLogContextReadFiltered(qemuLogContext *ctxt, + char **msg, + size_t max); +int qemuLogContextGetWriteFD(qemuLogContext *ctxt); +void qemuLogContextMarkPosition(qemuLogContext *ctxt); + +virLogManager *qemuLogContextGetManager(qemuLogContext *ctxt); --=20 2.47.0