From nobody Fri Apr 19 03:48:25 2024 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=fail(p=quarantine dis=quarantine) header.from=virtuozzo.com ARC-Seal: i=1; a=rsa-sha256; t=1669023046; cv=none; d=zohomail.com; s=zohoarc; b=ZTAQbQ1G+TTKid26t9+Gh9ipPXOdo4bDefpnbP+JM1K1JK7xBBfbDHW15l6hxyVtGk0uXJbyXNqXgNh7nZoUyG5cgSuItT4xIO+Uzy3YJeE2ps8AFU0xaZdYYdnI1PkIidoX8EIRvw5u6qabfuFHrBFXn4bVVNUmJ+tF7C1Iqq8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1669023046; h=Content-Type:Content-Transfer-Encoding:Cc: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=54e1K2VxmB+CDYEGjeECE9g+h/3PqpC2AMzNztQOWEY=; b=ZlMBhmC/4FuS/D/SIF2JI0Iujd+emsn1Wr2VDKgvLurC4MRlX2A/wYoupaHI6Nh0in7W2IowfeYGJMOrY0fq0RnltzLq0kBQvn/deqB9AwWpZLJGSH2K3caRbF8De2fqMxfwGFKsTfhRvnPpIPwd1zC4+xBBWfwIHgpyx0i2SYo= 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=fail header.from= (p=quarantine dis=quarantine) 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 1669023046040173.70159428085958; Mon, 21 Nov 2022 01:30:46 -0800 (PST) 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-227-CvdfEPbiO-KQ1Pn7DNoS8w-1; Mon, 21 Nov 2022 04:30:40 -0500 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id F11753C0E46B; Mon, 21 Nov 2022 09:30:37 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com [10.30.29.100]) by smtp.corp.redhat.com (Postfix) with ESMTP id 44FA840C6E14; Mon, 21 Nov 2022 09:30:36 +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 F2A341946588; Mon, 21 Nov 2022 09:30:35 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id 797BA1946587 for ; Mon, 21 Nov 2022 09:30:34 +0000 (UTC) Received: by smtp.corp.redhat.com (Postfix) id 3EEEE40C2143; Mon, 21 Nov 2022 09:30:34 +0000 (UTC) Received: from mimecast-mx02.redhat.com (mimecast06.extmail.prod.ext.rdu2.redhat.com [10.11.55.22]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 36D1340C2087 for ; Mon, 21 Nov 2022 09:30:34 +0000 (UTC) Received: from us-smtp-1.mimecast.com (us-smtp-1.mimecast.com [207.211.31.81]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 18492185A794 for ; Mon, 21 Nov 2022 09:30:34 +0000 (UTC) Received: from relay.virtuozzo.com (relay.virtuozzo.com [130.117.225.111]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-645-tmx8UZswNSGsDa9aqdpRjg-1; Mon, 21 Nov 2022 04:30:32 -0500 Received: from [192.168.16.72] (helo=dv.sw.ru) by relay.virtuozzo.com with esmtp (Exim 4.95) (envelope-from ) id 1ox385-00109T-Ls; Mon, 21 Nov 2022 10:30:17 +0100 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1669023044; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc: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=54e1K2VxmB+CDYEGjeECE9g+h/3PqpC2AMzNztQOWEY=; b=CTLdeavovbgz2R9aA4AQyCBu5MQJdcxf9LK8+3/j9r7nFDalewMKO6qQsUhKvnZ0MuVtX8 moAg8uLBxzYYB3UlmRIkqkQpfl2I8yp8M5DoKjiod7oHZx8lL0X+rVNyaQUIQPvF2i1CeH vxvtKO3YLlYs8QncJlTswwCI6EGmzKE= X-MC-Unique: CvdfEPbiO-KQ1Pn7DNoS8w-1 X-Original-To: libvir-list@listman.corp.redhat.com X-MC-Unique: tmx8UZswNSGsDa9aqdpRjg-1 From: Oleg Vasilev To: libvir-list@redhat.com Subject: [PATCH 1/2] logging: refactor to store config inside log handler Date: Mon, 21 Nov 2022 15:29:56 +0600 Message-Id: <20221121092957.11955-2-oleg.vasilev@virtuozzo.com> In-Reply-To: <20221121092957.11955-1-oleg.vasilev@virtuozzo.com> References: <20221121092957.11955-1-oleg.vasilev@virtuozzo.com> MIME-Version: 1.0 X-Mimecast-Impersonation-Protect: Policy=CLT - Impersonation Protection Definition; Similar Internal Domain=false; Similar Monitored External Domain=false; Custom External Domain=false; Mimecast External Domain=false; Newly Observed Domain=false; Internal User Name=false; Custom Display Name List=false; Reply-to Address Mismatch=false; Targeted Threat Dictionary=false; Mimecast Threat Dictionary=false; Custom Threat Dictionary=false X-Scanned-By: MIMEDefang 3.1 on 10.11.54.1 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: , Cc: Oleg Vasilev , "Denis V . Lunev" Errors-To: libvir-list-bounces@redhat.com Sender: "libvir-list" X-Scanned-By: MIMEDefang 3.1 on 10.11.54.2 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: 1669023047574100003 Content-Type: text/plain; charset="utf-8"; x-default="true" Signed-off-by: Oleg Vasilev --- src/logging/log_daemon.c | 6 ++---- src/logging/log_handler.c | 29 ++++++++++++----------------- src/logging/log_handler.h | 17 ++++++++--------- 3 files changed, 22 insertions(+), 30 deletions(-) diff --git a/src/logging/log_daemon.c b/src/logging/log_daemon.c index 00fc4148fd..294c30aaa8 100644 --- a/src/logging/log_daemon.c +++ b/src/logging/log_daemon.c @@ -145,8 +145,7 @@ virLogDaemonNew(virLogDaemonConfig *config, bool privil= eged) g_clear_pointer(&srv, virObjectUnref); =20 if (!(logd->handler =3D virLogHandlerNew(privileged, - config->max_size, - config->max_backups, + config, virLogDaemonInhibitor, logd))) goto error; @@ -231,8 +230,7 @@ virLogDaemonNewPostExecRestart(virJSONValue *object, bo= ol privileged, =20 if (!(logd->handler =3D virLogHandlerNewPostExecRestart(child, privileged, - config->max_size, - config->max_back= ups, + config, virLogDaemonInhi= bitor, logd))) goto error; diff --git a/src/logging/log_handler.c b/src/logging/log_handler.c index bf1e3f35c5..7342404b00 100644 --- a/src/logging/log_handler.c +++ b/src/logging/log_handler.c @@ -58,8 +58,7 @@ struct _virLogHandler { virObjectLockable parent; =20 bool privileged; - size_t max_size; - size_t max_backups; + virLogDaemonConfig *config; =20 virLogHandlerLogFile **files; size_t nfiles; @@ -185,8 +184,7 @@ virLogHandlerDomainLogFileEvent(int watch, =20 virLogHandler * virLogHandlerNew(bool privileged, - size_t max_size, - size_t max_backups, + virLogDaemonConfig *config, virLogHandlerShutdownInhibitor inhibitor, void *opaque) { @@ -199,8 +197,7 @@ virLogHandlerNew(bool privileged, return NULL; =20 handler->privileged =3D privileged; - handler->max_size =3D max_size; - handler->max_backups =3D max_backups; + handler->config =3D config; handler->inhibitor =3D inhibitor; handler->opaque =3D opaque; =20 @@ -253,8 +250,8 @@ virLogHandlerLogFilePostExecRestart(virLogHandler *hand= ler, } =20 if ((file->file =3D virRotatingFileWriterNew(path, - handler->max_size, - handler->max_backups, + handler->config->max_size, + handler->config->max_backup= s, false, DEFAULT_MODE)) =3D=3D NULL) goto error; @@ -282,8 +279,7 @@ virLogHandlerLogFilePostExecRestart(virLogHandler *hand= ler, virLogHandler * virLogHandlerNewPostExecRestart(virJSONValue *object, bool privileged, - size_t max_size, - size_t max_backups, + virLogDaemonConfig *config, virLogHandlerShutdownInhibitor inhibitor, void *opaque) { @@ -292,8 +288,7 @@ virLogHandlerNewPostExecRestart(virJSONValue *object, size_t i; =20 if (!(handler =3D virLogHandlerNew(privileged, - max_size, - max_backups, + config, inhibitor, opaque))) return NULL; @@ -393,8 +388,8 @@ virLogHandlerDomainOpenLogFile(virLogHandler *handler, file->domname =3D g_strdup(domname); =20 if ((file->file =3D virRotatingFileWriterNew(path, - handler->max_size, - handler->max_backups, + handler->config->max_size, + handler->config->max_backup= s, trunc, DEFAULT_MODE)) =3D=3D NULL) goto error; @@ -525,7 +520,7 @@ virLogHandlerDomainReadLogFile(virLogHandler *handler, =20 virObjectLock(handler); =20 - if (!(file =3D virRotatingFileReaderNew(path, handler->max_backups))) + if (!(file =3D virRotatingFileReaderNew(path, handler->config->max_bac= kups))) goto error; =20 if (virRotatingFileReaderSeek(file, inode, offset) < 0) @@ -579,8 +574,8 @@ virLogHandlerDomainAppendLogFile(virLogHandler *handler, =20 if (!writer) { if (!(newwriter =3D virRotatingFileWriterNew(path, - handler->max_size, - handler->max_backups, + handler->config->max_si= ze, + handler->config->max_ba= ckups, false, DEFAULT_MODE))) goto cleanup; diff --git a/src/logging/log_handler.h b/src/logging/log_handler.h index 099378b361..c9af033cd3 100644 --- a/src/logging/log_handler.h +++ b/src/logging/log_handler.h @@ -22,6 +22,7 @@ =20 #include "internal.h" #include "virjson.h" +#include "log_daemon_config.h" =20 typedef struct _virLogHandler virLogHandler; =20 @@ -30,16 +31,14 @@ typedef void (*virLogHandlerShutdownInhibitor)(bool inh= ibit, void *opaque); =20 virLogHandler *virLogHandlerNew(bool privileged, - size_t max_size, - size_t max_backups, - virLogHandlerShutdownInhibitor inhibitor, - void *opaque); + virLogDaemonConfig *config, + virLogHandlerShutdownInhibitor inhibitor, + void *opaque); virLogHandler *virLogHandlerNewPostExecRestart(virJSONValue *child, - bool privileged, - size_t max_size, - size_t max_backups, - virLogHandlerShutdownInhi= bitor inhibitor, - void *opaque); + bool privileged, + virLogDaemonConfig *config, + virLogHandlerShutdownInhibi= tor inhibitor, + void *opaque); =20 void virLogHandlerFree(virLogHandler *handler); =20 --=20 2.38.1 From nobody Fri Apr 19 03:48:25 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) client-ip=170.10.133.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.133.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=fail(p=quarantine dis=quarantine) header.from=virtuozzo.com ARC-Seal: i=1; a=rsa-sha256; t=1669023044; cv=none; d=zohomail.com; s=zohoarc; b=JGYoN4Wv6+1kDHtu7ny47roZqCAbPcMNTWy4rAA0+1u8qoTor4Qhpw1jM+fcY5e4a15ZHLQ7Pk1Qs395r2h+jxKiNMjhPKPvBAPCvTtZfKQO87Rk7k0UmdgkcI7sEXCyfwkVaK6aVExVVPfRaUK82XsxP8ytkurBCDmYtr9Ae0M= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1669023044; h=Content-Type:Content-Transfer-Encoding:Cc: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=3y/7BrP1Da6QydOCiCRx0PGljXtOwXCvZRxRmiMcyp0=; b=YuOXLhtjnZMYEIVw8J91HR+wUoXxNRy3+6iAg4cK7BApEz+kRLUw2Ek7uEDZ2HY7tf8as92x1F86lqHpt7oMByJ0HsQku8R5KxVNG+3ikH4v3u63/CIdWib3mbesVMEVC6PvTLU88GqzbDk3Wdv62umhl5pE8XWdCsxXbkPJQ0s= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=fail header.from= (p=quarantine dis=quarantine) Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by mx.zohomail.com with SMTPS id 1669023044536910.808370151985; Mon, 21 Nov 2022 01:30:44 -0800 (PST) Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-326-kkfGw2PsMo2RPTJRgq1TJQ-1; Mon, 21 Nov 2022 04:30:40 -0500 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id EB12D80601A; Mon, 21 Nov 2022 09:30:37 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com [10.30.29.100]) by smtp.corp.redhat.com (Postfix) with ESMTP id 45C1CC15BB3; Mon, 21 Nov 2022 09:30:36 +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 055CB194658D; Mon, 21 Nov 2022 09:30:36 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id 168D91946587 for ; Mon, 21 Nov 2022 09:30:35 +0000 (UTC) Received: by smtp.corp.redhat.com (Postfix) id 0BEE940C83AA; Mon, 21 Nov 2022 09:30:35 +0000 (UTC) Received: from mimecast-mx02.redhat.com (mimecast07.extmail.prod.ext.rdu2.redhat.com [10.11.55.23]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 03B3C40C2143 for ; Mon, 21 Nov 2022 09:30:34 +0000 (UTC) Received: from us-smtp-1.mimecast.com (us-smtp-delivery-1.mimecast.com [205.139.110.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id DA2433C0E443 for ; Mon, 21 Nov 2022 09:30:34 +0000 (UTC) Received: from relay.virtuozzo.com (relay.virtuozzo.com [130.117.225.111]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-94-hkrnMmeMMp2CqKoov2mW7Q-1; Mon, 21 Nov 2022 04:30:32 -0500 Received: from [192.168.16.72] (helo=dv.sw.ru) by relay.virtuozzo.com with esmtp (Exim 4.95) (envelope-from ) id 1ox386-00109T-Fk; Mon, 21 Nov 2022 10:30:18 +0100 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1669023043; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc: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=3y/7BrP1Da6QydOCiCRx0PGljXtOwXCvZRxRmiMcyp0=; b=bXpdBym2VKW0YoY7e0YvqIUpaYfv2Z63GdutDNIIiUiylot1C935v19E6qCdz1ib/+mUTC vWbnUdndWorKUk6H2yP9gJbREM962ppRQXu3KaVXNuKEYqXKrU2Qjqm0ih6aOR1R2oBNCN tQHz++8C4hPJn8rKQxQfvp9nE6HjLno= X-MC-Unique: kkfGw2PsMo2RPTJRgq1TJQ-1 X-Original-To: libvir-list@listman.corp.redhat.com X-MC-Unique: hkrnMmeMMp2CqKoov2mW7Q-1 From: Oleg Vasilev To: libvir-list@redhat.com Subject: [PATCH 2/2] logging: add log cleanup for obsolete domains Date: Mon, 21 Nov 2022 15:29:57 +0600 Message-Id: <20221121092957.11955-3-oleg.vasilev@virtuozzo.com> In-Reply-To: <20221121092957.11955-1-oleg.vasilev@virtuozzo.com> References: <20221121092957.11955-1-oleg.vasilev@virtuozzo.com> MIME-Version: 1.0 X-Mimecast-Impersonation-Protect: Policy=CLT - Impersonation Protection Definition; Similar Internal Domain=false; Similar Monitored External Domain=false; Custom External Domain=false; Mimecast External Domain=false; Newly Observed Domain=false; Internal User Name=false; Custom Display Name List=false; Reply-to Address Mismatch=false; Targeted Threat Dictionary=false; Mimecast Threat Dictionary=false; Custom Threat Dictionary=false X-Scanned-By: MIMEDefang 3.1 on 10.11.54.1 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: , Cc: Oleg Vasilev , "Denis V . Lunev" Errors-To: libvir-list-bounces@redhat.com Sender: "libvir-list" X-Scanned-By: MIMEDefang 3.1 on 10.11.54.8 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: 1669023045653100001 Content-Type: text/plain; charset="utf-8"; x-default="true" Before, logs from deleted machines have been piling up, since there were no garbadge collection mechanism. Now virtlogd can be configured to periodically scan log folder for orphan logs with no recent modfications and delete it. Signed-off-by: Oleg Vasilev --- src/logging/log_daemon_config.c | 9 +++ src/logging/log_daemon_config.h | 3 + src/logging/log_handler.c | 108 +++++++++++++++++++++++++++++++ src/logging/test_virtlogd.aug.in | 2 + src/logging/virtlogd.aug | 2 + src/logging/virtlogd.conf | 7 ++ 6 files changed, 131 insertions(+) diff --git a/src/logging/log_daemon_config.c b/src/logging/log_daemon_confi= g.c index 4436745488..248bd927d3 100644 --- a/src/logging/log_daemon_config.c +++ b/src/logging/log_daemon_config.c @@ -28,6 +28,7 @@ #include "virutil.h" =20 #define VIR_FROM_THIS VIR_FROM_CONF +#define DEFAULT_LOG_ROOT LOCALSTATEDIR "/log/libvirt/" =20 VIR_LOG_INIT("logging.log_daemon_config"); =20 @@ -60,6 +61,7 @@ virLogDaemonConfigNew(bool privileged G_GNUC_UNUSED) data->admin_max_clients =3D 5000; data->max_size =3D 1024 * 1024 * 2; data->max_backups =3D 3; + data->max_age_days =3D 0; =20 return data; } @@ -72,6 +74,7 @@ virLogDaemonConfigFree(virLogDaemonConfig *data) =20 g_free(data->log_filters); g_free(data->log_outputs); + g_free(data->log_root); =20 g_free(data); } @@ -94,6 +97,12 @@ virLogDaemonConfigLoadOptions(virLogDaemonConfig *data, return -1; if (virConfGetValueSizeT(conf, "max_backups", &data->max_backups) < 0) return -1; + if (virConfGetValueSizeT(conf, "max_age_days", &data->max_age_days) < = 0) + return -1; + if (virConfGetValueString(conf, "log_root", &data->log_root) < 0) + return -1; + if (!data->log_root) + data->log_root =3D g_strdup(DEFAULT_LOG_ROOT); =20 return 0; } diff --git a/src/logging/log_daemon_config.h b/src/logging/log_daemon_confi= g.h index 2ab0f67c96..43922feedf 100644 --- a/src/logging/log_daemon_config.h +++ b/src/logging/log_daemon_config.h @@ -33,6 +33,9 @@ struct _virLogDaemonConfig { =20 size_t max_backups; size_t max_size; + + char *log_root; + size_t max_age_days; }; =20 =20 diff --git a/src/logging/log_handler.c b/src/logging/log_handler.c index 7342404b00..2690d7519b 100644 --- a/src/logging/log_handler.c +++ b/src/logging/log_handler.c @@ -33,6 +33,7 @@ #include #include #include +#include =20 #include "configmake.h" =20 @@ -42,6 +43,10 @@ VIR_LOG_INIT("logging.log_handler"); =20 #define DEFAULT_MODE 0600 =20 +/* Cleanup log root (/var/log/libvirt) and all subfolders (e.g. /var/log/l= ibvirt/qemu) */ +#define CLEANUP_OBSOLETE_LOG_DEPTH 1 +#define CLEANUP_OBSOLETE_LOG_TIMEOUT_MS (24 * 3600 * 1000) /* One day */ + typedef struct _virLogHandlerLogFile virLogHandlerLogFile; struct _virLogHandlerLogFile { virRotatingFileWriter *file; @@ -60,6 +65,8 @@ struct _virLogHandler { bool privileged; virLogDaemonConfig *config; =20 + int cleanup_log_timer; + virLogHandlerLogFile **files; size_t nfiles; =20 @@ -81,6 +88,93 @@ virLogHandlerOnceInit(void) =20 VIR_ONCE_GLOBAL_INIT(virLogHandler); =20 +static void +virLogHandlerMaybeCleanupObsoleteLog(virLogHandler *handler, + const char* path) { + size_t i; + bool remove =3D true; + + if (fnmatch("*.log*", path, 0)) + return; + + virObjectLock(handler); + for (i =3D 0; i < handler->nfiles; i++) { + virLogHandlerLogFile *file =3D handler->files[i]; + if (STRPREFIX(path, virRotatingFileWriterGetPath(file->file))) { + remove =3D false; + break; + } + } + virObjectUnlock(handler); + + if (!remove) + return; + + if (unlink(path) < 0) { + virReportSystemError(errno, _("Unable to delete %s"), path); + } +} + +static void +virLogHandlerCleanupObsoleteLogsFolder(virLogHandler *handler, + time_t oldest_to_keep, + const char *path, + int depth_left) +{ + DIR *dir; + struct dirent *entry; + char *newpath; + struct stat sb; + + if (virDirOpenIfExists(&dir, path) < 0) + return; + + while (virDirRead(dir, &entry, path) > 0) { + if (STREQ(entry->d_name, ".")) + continue; + if (STREQ(entry->d_name, "..")) + continue; + + newpath =3D g_strdup_printf("%s/%s", path, entry->d_name); + + if (stat(newpath, &sb) < 0) { + virReportSystemError(errno, _("Unable to stat %s"), newpath); + goto next; + } + + if (S_ISDIR(sb.st_mode)) { + if (depth_left > 0) + virLogHandlerCleanupObsoleteLogsFolder(handler, oldest_to_= keep, newpath, depth_left - 1); + goto next; + } + + if (!S_ISREG(sb.st_mode)) { + goto next; + } + + if (sb.st_mtim.tv_sec > oldest_to_keep) { + goto next; + } + + virLogHandlerMaybeCleanupObsoleteLog(handler, newpath); + + next: + VIR_FREE(newpath); + } + + virDirClose(dir); +} + +static void +virLogHandlerCleanupObsoleteLogs(int timer G_GNUC_UNUSED, void* opaque) +{ + virLogHandler *handler =3D opaque; + time_t oldest_to_keep =3D time(NULL) - 3600 * 24 * handler->config->ma= x_age_days; + const char *log_root =3D handler->config->log_root; + + virLogHandlerCleanupObsoleteLogsFolder(handler, oldest_to_keep, log_ro= ot, CLEANUP_OBSOLETE_LOG_DEPTH); +} + =20 static void virLogHandlerLogFileFree(virLogHandlerLogFile *file) @@ -201,7 +295,19 @@ virLogHandlerNew(bool privileged, handler->inhibitor =3D inhibitor; handler->opaque =3D opaque; =20 + if (config->max_age_days > 0) { + handler->cleanup_log_timer =3D virEventAddTimeout(CLEANUP_OBSOLETE= _LOG_TIMEOUT_MS, + virLogHandlerC= leanupObsoleteLogs, + handler, NULL); + if (handler->cleanup_log_timer < 0) + goto error; + } + return handler; + + error: + virObjectUnref(handler); + return NULL; } =20 =20 @@ -344,6 +450,8 @@ virLogHandlerDispose(void *obj) virLogHandlerLogFileFree(handler->files[i]); } g_free(handler->files); + if (handler->cleanup_log_timer !=3D 0) + virEventRemoveTimeout(handler->cleanup_log_timer); } =20 =20 diff --git a/src/logging/test_virtlogd.aug.in b/src/logging/test_virtlogd.a= ug.in index cd5b0d91f8..8dfad39506 100644 --- a/src/logging/test_virtlogd.aug.in +++ b/src/logging/test_virtlogd.aug.in @@ -9,3 +9,5 @@ module Test_virtlogd =3D { "admin_max_clients" =3D "5" } { "max_size" =3D "2097152" } { "max_backups" =3D "3" } + { "max_age_days" =3D "0" } + { "log_root" =3D "/var/log/libvirt" } diff --git a/src/logging/virtlogd.aug b/src/logging/virtlogd.aug index 0f1b290c72..bdf61dea6e 100644 --- a/src/logging/virtlogd.aug +++ b/src/logging/virtlogd.aug @@ -31,6 +31,8 @@ module Virtlogd =3D | int_entry "admin_max_clients" | int_entry "max_size" | int_entry "max_backups" + | int_entry "max_age_days" + | str_entry "log_root" =20 (* Each entry in the config is one of the following three ... *) let entry =3D logging_entry diff --git a/src/logging/virtlogd.conf b/src/logging/virtlogd.conf index c53a1112bd..d88ce31327 100644 --- a/src/logging/virtlogd.conf +++ b/src/logging/virtlogd.conf @@ -101,3 +101,10 @@ # Maximum number of backup files to keep. Defaults to 3, # not including the primary active file #max_backups =3D 3 + +# Maximum age for log files to live after the last modification. +# Defaults to 0, which means "forever". +#max_age_days =3D 0 + +# Root of all logs managed by virtlogd. Used to GC logs from obsolete mach= ines. +#log_root =3D "/var/log/libvirt" --=20 2.38.1