From patchwork Fri Jun 3 10:59:08 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: dbugnar X-Patchwork-Id: 629764 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2001:1868:205::9]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3rLh3z2r51z9t7s for ; Fri, 3 Jun 2016 21:01:35 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b=JGV5ip/u; dkim-atps=neutral Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1b8mpR-0005jW-DB; Fri, 03 Jun 2016 10:59:49 +0000 Received: from mail-wm0-x241.google.com ([2a00:1450:400c:c09::241]) by bombadil.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1b8mpP-0005dz-LB for lede-dev@lists.infradead.org; Fri, 03 Jun 2016 10:59:48 +0000 Received: by mail-wm0-x241.google.com with SMTP id a20so11129430wma.3 for ; Fri, 03 Jun 2016 03:59:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=P8IBaKAXfzCAH/0aKgI9AM3i5ksRe/cbrKOfs2P7J2U=; b=JGV5ip/udTAkoQFkdTE37nhuuZXQu8SkGL59cl0X0h83WGFmeR9ganuqob2JSXSBSt DNBGAo6mfUsmemFM+Miyi4vQO0vChSYOnzsm+i8E89frl6juDweeyPQEVLfQEz7zQbZ3 7pbK1LeeX1JbxREIjdPksFMvDUx2PKuE/7FELeIAWQu3YcmoH4cbrpPXq0AlPuGCf3zY pW34WTk1focprUJaL0Yr21lV0gG0UctyO7qi/yLEyeDTJ3g1Mzlje55bKzOUfgxVGL2p nwYL6SFO5W23toMEN6tC/pID1W4YdP9C/tTrF5V4MDKTmZDkCqMH+VokjYTYJ8SklSQQ DJHA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=P8IBaKAXfzCAH/0aKgI9AM3i5ksRe/cbrKOfs2P7J2U=; b=lglIuiA4pzBLmAtJoTB3JpYCja49fVWvjE+030VYoUOpxuZ76H1PVgo8OrTyUmXoHZ jwIYTNsa19h8HV20IzZlrH09u4pS9NbiqZ0JadgMy35Qh6ekLGsHQxHm1cNqf+9cmnF5 JKUNFW8ar7DpglthSbPs8EOVUA9QwnHI16ZwWIiW1BUNYK54sJ3xXZhDg5JWiIPPLO9E rzLIQmOKleq0Qhuiwl29Gg2S4TnwoAmukFfuVkdeGLMcmstSauE8NSFsh1O94jaCpO1D M4A+RtA7b+KXWoR1dFpAT4ulNFCIIlrc/IghdjuPXAbXx3upqvIJt+wwlWLOg1+ETusm QHhg== X-Gm-Message-State: ALyK8tKxjxr8rwVhmoUI/kxd2Ucwk41V9z3fy8Epoin496dqfbl8NDN4NP8Rj6DUNkkpPg== X-Received: by 10.28.158.8 with SMTP id h8mr3672021wme.1.1464951565855; Fri, 03 Jun 2016 03:59:25 -0700 (PDT) Received: from localhost.localdomain ([5.2.198.78]) by smtp.gmail.com with ESMTPSA id f1sm5824355wmf.22.2016.06.03.03.59.24 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 03 Jun 2016 03:59:25 -0700 (PDT) From: Dan Bugnar To: lede-dev@lists.infradead.org Date: Fri, 3 Jun 2016 13:59:08 +0300 Message-Id: <1464951549-4152-2-git-send-email-danutbug@gmail.com> X-Mailer: git-send-email 2.8.1 In-Reply-To: <1464951549-4152-1-git-send-email-danutbug@gmail.com> References: <1464951549-4152-1-git-send-email-danutbug@gmail.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20160603_035947_854632_B67D9CB8 X-CRM114-Status: GOOD ( 15.34 ) X-Spam-Score: -2.7 (--) X-Spam-Report: SpamAssassin version 3.4.0 on bombadil.infradead.org summary: Content analysis details: (-2.7 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.7 RCVD_IN_DNSWL_LOW RBL: Sender listed at http://www.dnswl.org/, low trust [2a00:1450:400c:c09:0:0:0:241 listed in] [list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider (danutbug[at]gmail.com) -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% [score: 0.0000] -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid Subject: [LEDE-DEV] [PATCH 2/3] [ubox] syslog: change log buffer size X-BeenThere: lede-dev@lists.infradead.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Dan Bugnar MIME-Version: 1.0 Sender: "Lede-dev" Errors-To: lede-dev-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org Change the log buffer size and copy the messages. Copy the messages from the oldest to newest using the log_list. First time we calculate the size of the all entries. The log_entries_size calculates the size indifferent of the positions of the newest and oldest. If we want to increase the log buffer size is simple, we just copy from the oldest message to the newest all the messages into the new buffer. In case we shrink the buffer we go over messages and decrease the entries size with the size of each message until the entries size is less or equal to the new size we allocate the new buffer. Signed-off-by: Dan Bugnar --- log/syslog.c | 36 +++++++++++++++++++++++++++--------- log/syslog.h | 2 +- 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/log/syslog.c b/log/syslog.c index 2042e93..e24c678 100644 --- a/log/syslog.c +++ b/log/syslog.c @@ -42,7 +42,7 @@ #define PAD(x) (x % 4) ? (((x) - (x % 4)) + 4) : (x) static char *log_dev = LOG_DEFAULT_SOCKET; -static int log_size = LOG_DEFAULT_SIZE; +static int log_size = 0; static struct log_head *log, *log_end, *oldest, *newest; static int current_id = 0; static regex_t pat_prio; @@ -213,6 +213,15 @@ syslog_open(void) return 0; } +static inline int +log_entries_size() +{ + if (newest > oldest) + return (newest - oldest); + else + return (log_end - oldest + newest - log); +} + struct log_head* log_list(int count, struct log_head *h) { @@ -237,26 +246,38 @@ log_list(int count, struct log_head *h) } int -log_buffer_init(int size) +log_buffer_reinit(int size) { + if (size <= 0) + size = LOG_DEFAULT_SIZE; + if (log_size == size) + return 0; + struct log_head *_log = malloc(size); if (!_log) { + oldest = newest = log = NULL; fprintf(stderr, "Failed to initialize log buffer with size %d\n", log_size); return -1; } memset(_log, 0, size); - if (log && ((log_size + sizeof(struct log_head)) < size)) { + if (log) { struct log_head *start = _log; struct log_head *end = ((void*) _log) + size; struct log_head *l; + int entries_size = log_entries_size(); l = log_list(0, NULL); while ((start < end) && l && l->size) { - memcpy(start, l, PAD(sizeof(struct log_head) + l->size)); - start = (struct log_head *) &l->data[PAD(l->size)]; + int copy_len = PAD(sizeof(struct log_head)) + l->size; + if (entries_size < size) { + memcpy(start, l, copy_len); + start = (struct log_head *) &start->data[l->size]; + } else{ + entries_size -= copy_len; + } l = log_list(0, l); } free(log); @@ -276,13 +297,10 @@ log_buffer_init(int size) void log_init(int _log_size) { - if (_log_size > 0) - log_size = _log_size; - regcomp(&pat_prio, "^<([0-9]*)>(.*)", REG_EXTENDED); regcomp(&pat_tstamp, "^\[[ 0]*([0-9]*).([0-9]*)] (.*)", REG_EXTENDED); - if (log_buffer_init(log_size)) { + if (log_buffer_reinit(_log_size)) { fprintf(stderr, "Failed to allocate log memory\n"); exit(-1); } diff --git a/log/syslog.h b/log/syslog.h index 81a039f..ed5a41b 100644 --- a/log/syslog.h +++ b/log/syslog.h @@ -35,7 +35,7 @@ void log_shutdown(void); typedef void (*log_list_cb)(struct log_head *h); struct log_head* log_list(int count, struct log_head *h); -int log_buffer_init(int size); +int log_buffer_reinit(int size); void log_add(char *buf, int size, int source); void ubus_notify_log(struct log_head *l);