From patchwork Mon Jun 6 15:59:41 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wei Liu X-Patchwork-Id: 9158709 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id D249060573 for ; Mon, 6 Jun 2016 16:01:54 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C3BDD20700 for ; Mon, 6 Jun 2016 16:01:54 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id B8A0726B41; Mon, 6 Jun 2016 16:01:54 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.2 required=2.0 tests=BAYES_00, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 6B5CB20700 for ; Mon, 6 Jun 2016 16:01:54 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1b9wwR-0007LH-OT; Mon, 06 Jun 2016 15:59:51 +0000 Received: from mail6.bemta5.messagelabs.com ([195.245.231.135]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1b9wwQ-0007K7-4h for xen-devel@lists.xenproject.org; Mon, 06 Jun 2016 15:59:50 +0000 Received: from [85.158.139.211] by server-12.bemta-5.messagelabs.com id 6A/EC-32412-5FD95575; Mon, 06 Jun 2016 15:59:49 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFprAIsWRWlGSWpSXmKPExsXitHSDve6XuaH hBk3vLSy+b5nM5MDocfjDFZYAxijWzLyk/IoE1oyGv2/ZC/YIV/TfOMHawHiRr4uRk0NCwF9i +f79zCA2m4CyxM/OXjYQW0RAT6LpwHNGEJtZwEvix4oedhBbWMBH4vbMG2A2i4CKxN33a1hAb F4BZ4mHK7sYIWbKSZw//hNsJqeAi8T5vq9g9UJANXNvzYOyFSQ6ph9jgugVlDg58wkLxC4JiY MvXgD1cgDN4Zb4220/gZFvFpKqWUiqFjAyrWLUKE4tKkst0jU01UsqykzPKMlNzMzRNTQw1ct NLS5OTE/NSUwq1kvOz93ECAwpBiDYwdiw3fMQoyQHk5Io747+0HAhvqT8lMqMxOKM+KLSnNTi Q4wyHBxKErzGwBAVEixKTU+tSMvMAQY3TFqCg0dJhLcMJM1bXJCYW5yZDpE6xagoJc7rCJIQA ElklObBtcEi6hKjrJQwLyPQIUI8BalFuZklqPKvGMU5GJWEeflApvBk5pXATX8FtJgJaPGya8 Egi0sSEVJSDYxWyc+Uzi57XcrRdo1zn4lvQ4GtS6kj3y33SXdqbzt+LTS2i5+qv87qym3p855 HVA1CvqYHm0w+aTdRq+RKwqJNLzTWFf6SO7rdYV1/wxxp0z3zFxgH6Oo/nTiR74vMzODwLSwK zGwH0m5033+/1ytbKrlauGS1bg7/Ym0P3pd1Xdx/bumtdVZiKc5INNRiLipOBAD3SeJOowIAA A== X-Env-Sender: prvs=9584f9c1d=wei.liu2@citrix.com X-Msg-Ref: server-11.tower-206.messagelabs.com!1465228787!31248668!1 X-Originating-IP: [66.165.176.63] X-SpamReason: No, hits=0.0 required=7.0 tests=sa_preprocessor: VHJ1c3RlZCBJUDogNjYuMTY1LjE3Ni42MyA9PiAzMDYwNDg=\n, received_headers: No Received headers X-StarScan-Received: X-StarScan-Version: 8.46; banners=-,-,- X-VirusChecked: Checked Received: (qmail 64004 invoked from network); 6 Jun 2016 15:59:48 -0000 Received: from smtp02.citrix.com (HELO SMTP02.CITRIX.COM) (66.165.176.63) by server-11.tower-206.messagelabs.com with RC4-SHA encrypted SMTP; 6 Jun 2016 15:59:48 -0000 X-IronPort-AV: E=Sophos;i="5.26,427,1459814400"; d="scan'208";a="365531460" From: Wei Liu To: Xen-devel Date: Mon, 6 Jun 2016 16:59:41 +0100 Message-ID: <1465228781-22754-7-git-send-email-wei.liu2@citrix.com> X-Mailer: git-send-email 2.1.4 In-Reply-To: <1465228781-22754-1-git-send-email-wei.liu2@citrix.com> References: <1465228781-22754-1-git-send-email-wei.liu2@citrix.com> MIME-Version: 1.0 X-DLP: MIA1 Cc: Wei Liu , Ian Jackson Subject: [Xen-devel] [PATCH 6/6] xenconsoled: handle --log-backups 0 in logfile_rollover X-BeenThere: xen-devel@lists.xen.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xen.org Sender: "Xen-devel" X-Virus-Scanned: ClamAV using ClamSMTP We now allow user to configure the number of backups to keep. We need to handle when the number is set to 0. Check if number of backup is 0. If so, just unlink the file. Move the rotation to `else' branch so that we skip it altogether. Signed-off-by: Wei Liu --- tools/console/daemon/logfile.c | 63 +++++++++++++++++++++++++----------------- 1 file changed, 37 insertions(+), 26 deletions(-) diff --git a/tools/console/daemon/logfile.c b/tools/console/daemon/logfile.c index 3b95f84..fd29ba5 100644 --- a/tools/console/daemon/logfile.c +++ b/tools/console/daemon/logfile.c @@ -114,38 +114,49 @@ static int logfile_rollover(struct logfile *file) char *next = NULL; unsigned i; - if (asprintf(&next, "%s.%u", file->basepath, - log_backups) < 0) { - dolog(LOG_ERR, "Failed to asprintf %s.%u", - file->basepath, log_backups); - goto err; - } + if (log_backups == 0) { + if (unlink(file->basepath) < 0 && + errno != ENOENT) { + dolog(LOG_ERR, "Failed to unlink %s", + file->basepath); + goto err; + } + } else { + if (asprintf(&next, "%s.%u", file->basepath, + log_backups) < 0) { + dolog(LOG_ERR, "Failed to asprintf %s.%u", + file->basepath, log_backups); + goto err; + } - for (i = log_backups; i > 0; i--) { - if (i == 1) { - this = strdup(file->basepath); - if (!this) { - dolog(LOG_ERR, "Failed to strdup %s", - file->basepath); - goto err; + for (i = log_backups; i > 0; i--) { + if (i == 1) { + this = strdup(file->basepath); + if (!this) { + dolog(LOG_ERR, "Failed to strdup %s", + file->basepath); + goto err; + } + } else { + if (asprintf(&this, "%s.%u", file->basepath, + i-1) < 0) { + dolog(LOG_ERR, + "Failed to asprintf %s.%u", + file->basepath, i-1); + goto err; + } } - } else { - if (asprintf(&this, "%s.%u", file->basepath, i-1) < 0) { - dolog(LOG_ERR, "Failed to asprintf %s.%u", - file->basepath, i-1); + + if (rename(this, next) < 0 && errno != ENOENT) { + dolog(LOG_ERR, "Failed to rename %s to %s", + this, next); goto err; } - } - if (rename(this, next) < 0 && errno != ENOENT) { - dolog(LOG_ERR, "Failed to rename %s to %s", - this, next); - goto err; + free(next); + next = this; + this = NULL; } - - free(next); - next = this; - this = NULL; } new = logfile_entry_new(file->basepath, file->mode);