From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail.ozlabs.org (gandalf.ozlabs.org [150.107.74.76]) by passt.top (Postfix) with ESMTPS id 201045A004F for ; Mon, 29 Jul 2024 06:23:06 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202312; t=1722226981; bh=i+WgEnTQPjpBeOVXogGcnBbO9OlRshqQKD9Qppd607k=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=HCvFErvREbYzrVZMVwP8NftaChNi8BV1gUHEANMchA8QkTOTvTDbqKjIsT0bRsMD2 RIj5utWOZIXeV4NY+2ekiZO9VHzzkHtIdYWm8gQOScDBpWZ9v05CLTHVL/RnRSYr3B aRI8sgx58uEKdP8PMdJkcTKYLkGB4FOccZKPJCKXSwrkgCVmVzcI89/KZ+kAKmzhfm RjWtzDu2vC9TjMH+FjYZzOXScjVGf4ooGu5yqKs9YuJ8DXD0VdKyPsxbbe3lUFAYm6 jV29fEqgVXY8F44Z4KTRuAcD/akW8Nj4uWO6kztKr7mtQgxetmtJsL7iOrbySFRKsS O0EIslJgJjXEA== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4WXQFd6z0bz4x42; Mon, 29 Jul 2024 14:23:01 +1000 (AEST) From: David Gibson To: passt-dev@passt.top, Stefano Brivio Subject: [PATCH 1/2] log: Make logfile_write() private Date: Mon, 29 Jul 2024 14:22:59 +1000 Message-ID: <20240729042300.280684-2-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240729042300.280684-1-david@gibson.dropbear.id.au> References: <20240729042300.280684-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: ZLMD6TVKPLOJ5J2TCHXLGFCTMTESUYWH X-Message-ID-Hash: ZLMD6TVKPLOJ5J2TCHXLGFCTMTESUYWH X-MailFrom: dgibson@gandalf.ozlabs.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: David Gibson X-Mailman-Version: 3.3.8 Precedence: list List-Id: Development discussion and patches for passt Archived-At: Archived-At: List-Archive: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: logfile_write() is not used outside log.c, nor should it be. It should only be used externall via the general logging functions. Make it static in log.c. To avoid forward declarations this requires moving a bunch of functions earlier in the file. Signed-off-by: David Gibson --- log.c | 341 +++++++++++++++++++++++++++++----------------------------- log.h | 1 - 2 files changed, 171 insertions(+), 171 deletions(-) diff --git a/log.c b/log.c index 0fb25b77..eb3a780a 100644 --- a/log.c +++ b/log.c @@ -55,6 +55,177 @@ bool log_runtime; /* Daemonised, or ready in foreground */ (timespec_diff_us((x), &log_start) / 1000000LL), \ (timespec_diff_us((x), &log_start) / 100LL) +/* Prefixes for log file messages, indexed by priority */ +const char *logfile_prefix[] = { + NULL, NULL, NULL, /* Unused: LOG_EMERG, LOG_ALERT, LOG_CRIT */ + "ERROR: ", + "WARNING: ", + NULL, /* Unused: LOG_NOTICE */ + "info: ", + " ", /* LOG_DEBUG */ +}; + +#ifdef FALLOC_FL_COLLAPSE_RANGE +/** + * logfile_rotate_fallocate() - Write header, set log_written after fallocate() + * @fd: Log file descriptor + * @now: Current timestamp + * + * #syscalls lseek ppc64le:_llseek ppc64:_llseek arm:_llseek + */ +static void logfile_rotate_fallocate(int fd, const struct timespec *now) +{ + char buf[BUFSIZ]; + const char *nl; + int n; + + if (lseek(fd, 0, SEEK_SET) == -1) + return; + if (read(fd, buf, BUFSIZ) == -1) + return; + + n = snprintf(buf, BUFSIZ, "%s - log truncated at ", log_header); + n += snprintf(buf + n, BUFSIZ - n, logtime_fmt_and_arg(now)); + + /* Avoid partial lines by padding the header with spaces */ + nl = memchr(buf + n + 1, '\n', BUFSIZ - n - 1); + if (nl) + memset(buf + n, ' ', nl - (buf + n)); + + if (lseek(fd, 0, SEEK_SET) == -1) + return; + if (write(fd, buf, BUFSIZ) == -1) + return; + + log_written -= log_cut_size; +} +#endif /* FALLOC_FL_COLLAPSE_RANGE */ + +/** + * logfile_rotate_move() - Fallback: move recent entries toward start, then cut + * @fd: Log file descriptor + * @now: Current timestamp + * + * #syscalls lseek ppc64le:_llseek ppc64:_llseek arm:_llseek + * #syscalls ftruncate + */ +static void logfile_rotate_move(int fd, const struct timespec *now) +{ + int header_len, write_offset, end, discard, n; + char buf[BUFSIZ]; + const char *nl; + + header_len = snprintf(buf, BUFSIZ, "%s - log truncated at ", + log_header); + header_len += snprintf(buf + header_len, BUFSIZ - header_len, + logtime_fmt_and_arg(now)); + + if (lseek(fd, 0, SEEK_SET) == -1) + return; + if (write(fd, buf, header_len) == -1) + return; + + end = write_offset = header_len; + discard = log_cut_size + header_len; + + /* Try to cut cleanly at newline */ + if (lseek(fd, discard, SEEK_SET) == -1) + goto out; + if ((n = read(fd, buf, BUFSIZ)) <= 0) + goto out; + if ((nl = memchr(buf, '\n', n))) + discard += (nl - buf) + 1; + + /* Go to first block to be moved */ + if (lseek(fd, discard, SEEK_SET) == -1) + goto out; + + while ((n = read(fd, buf, BUFSIZ)) > 0) { + end = header_len; + + if (lseek(fd, write_offset, SEEK_SET) == -1) + goto out; + if ((n = write(fd, buf, n)) == -1) + goto out; + write_offset += n; + + if ((n = lseek(fd, 0, SEEK_CUR)) == -1) + goto out; + + if (lseek(fd, discard - header_len, SEEK_CUR) == -1) + goto out; + + end = n; + } + +out: + if (ftruncate(fd, end)) + return; + + log_written = end; +} + +/** + * logfile_rotate() - "Rotate" log file once it's full + * @fd: Log file descriptor + * @now: Current timestamp + * + * Return: 0 on success, negative error code on failure + * + * #syscalls fcntl + * + * fallocate() passed as EXTRA_SYSCALL only if FALLOC_FL_COLLAPSE_RANGE is there + */ +static int logfile_rotate(int fd, const struct timespec *now) +{ + if (fcntl(fd, F_SETFL, O_RDWR /* Drop O_APPEND: explicit lseek() */)) + return -errno; + +#ifdef FALLOC_FL_COLLAPSE_RANGE + /* Only for Linux >= 3.15, extent-based ext4 or XFS, glibc >= 2.18 */ + if (!fallocate(fd, FALLOC_FL_COLLAPSE_RANGE, 0, log_cut_size)) + logfile_rotate_fallocate(fd, now); + else +#endif + logfile_rotate_move(fd, now); + + if (fcntl(fd, F_SETFL, O_RDWR | O_APPEND)) + return -errno; + + return 0; +} + +/** + * logfile_write() - Write entry to log file, trigger rotation if full + * @newline: Append newline at the end of the message, if missing + * @pri: Facility and level map, same as priority for vsyslog() + * @format: Same as vsyslog() format + * @ap: Same as vsyslog() ap + */ +static void logfile_write(bool newline, int pri, const char *format, va_list ap) +{ + struct timespec now; + char buf[BUFSIZ]; + int n; + + if (clock_gettime(CLOCK_MONOTONIC, &now)) + return; + + n = snprintf(buf, BUFSIZ, logtime_fmt_and_arg(&now)); + n += snprintf(buf + n, BUFSIZ - n, ": %s", logfile_prefix[pri]); + + n += vsnprintf(buf + n, BUFSIZ - n, format, ap); + + if (newline && format[strlen(format)] != '\n') + n += snprintf(buf + n, BUFSIZ - n, "\n"); + + if ((log_written + n >= log_size) && logfile_rotate(log_file, &now)) + return; + + if ((n = write(log_file, buf, n)) >= 0) + log_written += n; +} + /** * vlogmsg() - Print or send messages to log or output files as configured * @newline: Append newline at the end of the message, if missing @@ -125,16 +296,6 @@ void logmsg_perror(int pri, const char *format, ...) logmsg(true, pri, ": %s", strerror(errno_copy)); } -/* Prefixes for log file messages, indexed by priority */ -const char *logfile_prefix[] = { - NULL, NULL, NULL, /* Unused: LOG_EMERG, LOG_ALERT, LOG_CRIT */ - "ERROR: ", - "WARNING: ", - NULL, /* Unused: LOG_NOTICE */ - "info: ", - " ", /* LOG_DEBUG */ -}; - /** * trace_init() - Set log_trace depending on trace (debug) mode * @enable: Tracing debug mode enabled if non-zero @@ -239,163 +400,3 @@ void logfile_init(const char *name, const char *path, size_t size) log_cut_size = ROUND_UP(log_size * LOGFILE_CUT_RATIO / 100, PAGE_SIZE); } -#ifdef FALLOC_FL_COLLAPSE_RANGE -/** - * logfile_rotate_fallocate() - Write header, set log_written after fallocate() - * @fd: Log file descriptor - * @now: Current timestamp - * - * #syscalls lseek ppc64le:_llseek ppc64:_llseek arm:_llseek - */ -static void logfile_rotate_fallocate(int fd, const struct timespec *now) -{ - char buf[BUFSIZ]; - const char *nl; - int n; - - if (lseek(fd, 0, SEEK_SET) == -1) - return; - if (read(fd, buf, BUFSIZ) == -1) - return; - - n = snprintf(buf, BUFSIZ, "%s - log truncated at ", log_header); - n += snprintf(buf + n, BUFSIZ - n, logtime_fmt_and_arg(now)); - - /* Avoid partial lines by padding the header with spaces */ - nl = memchr(buf + n + 1, '\n', BUFSIZ - n - 1); - if (nl) - memset(buf + n, ' ', nl - (buf + n)); - - if (lseek(fd, 0, SEEK_SET) == -1) - return; - if (write(fd, buf, BUFSIZ) == -1) - return; - - log_written -= log_cut_size; -} -#endif /* FALLOC_FL_COLLAPSE_RANGE */ - -/** - * logfile_rotate_move() - Fallback: move recent entries toward start, then cut - * @fd: Log file descriptor - * @now: Current timestamp - * - * #syscalls lseek ppc64le:_llseek ppc64:_llseek arm:_llseek - * #syscalls ftruncate - */ -static void logfile_rotate_move(int fd, const struct timespec *now) -{ - int header_len, write_offset, end, discard, n; - char buf[BUFSIZ]; - const char *nl; - - header_len = snprintf(buf, BUFSIZ, "%s - log truncated at ", - log_header); - header_len += snprintf(buf + header_len, BUFSIZ - header_len, - logtime_fmt_and_arg(now)); - - if (lseek(fd, 0, SEEK_SET) == -1) - return; - if (write(fd, buf, header_len) == -1) - return; - - end = write_offset = header_len; - discard = log_cut_size + header_len; - - /* Try to cut cleanly at newline */ - if (lseek(fd, discard, SEEK_SET) == -1) - goto out; - if ((n = read(fd, buf, BUFSIZ)) <= 0) - goto out; - if ((nl = memchr(buf, '\n', n))) - discard += (nl - buf) + 1; - - /* Go to first block to be moved */ - if (lseek(fd, discard, SEEK_SET) == -1) - goto out; - - while ((n = read(fd, buf, BUFSIZ)) > 0) { - end = header_len; - - if (lseek(fd, write_offset, SEEK_SET) == -1) - goto out; - if ((n = write(fd, buf, n)) == -1) - goto out; - write_offset += n; - - if ((n = lseek(fd, 0, SEEK_CUR)) == -1) - goto out; - - if (lseek(fd, discard - header_len, SEEK_CUR) == -1) - goto out; - - end = n; - } - -out: - if (ftruncate(fd, end)) - return; - - log_written = end; -} - -/** - * logfile_rotate() - "Rotate" log file once it's full - * @fd: Log file descriptor - * @now: Current timestamp - * - * Return: 0 on success, negative error code on failure - * - * #syscalls fcntl - * - * fallocate() passed as EXTRA_SYSCALL only if FALLOC_FL_COLLAPSE_RANGE is there - */ -static int logfile_rotate(int fd, const struct timespec *now) -{ - if (fcntl(fd, F_SETFL, O_RDWR /* Drop O_APPEND: explicit lseek() */)) - return -errno; - -#ifdef FALLOC_FL_COLLAPSE_RANGE - /* Only for Linux >= 3.15, extent-based ext4 or XFS, glibc >= 2.18 */ - if (!fallocate(fd, FALLOC_FL_COLLAPSE_RANGE, 0, log_cut_size)) - logfile_rotate_fallocate(fd, now); - else -#endif - logfile_rotate_move(fd, now); - - if (fcntl(fd, F_SETFL, O_RDWR | O_APPEND)) - return -errno; - - return 0; -} - -/** - * logfile_write() - Write entry to log file, trigger rotation if full - * @newline: Append newline at the end of the message, if missing - * @pri: Facility and level map, same as priority for vsyslog() - * @format: Same as vsyslog() format - * @ap: Same as vsyslog() ap - */ -void logfile_write(bool newline, int pri, const char *format, va_list ap) -{ - struct timespec now; - char buf[BUFSIZ]; - int n; - - if (clock_gettime(CLOCK_MONOTONIC, &now)) - return; - - n = snprintf(buf, BUFSIZ, logtime_fmt_and_arg(&now)); - n += snprintf(buf + n, BUFSIZ - n, ": %s", logfile_prefix[pri]); - - n += vsnprintf(buf + n, BUFSIZ - n, format, ap); - - if (newline && format[strlen(format)] != '\n') - n += snprintf(buf + n, BUFSIZ - n, "\n"); - - if ((log_written + n >= log_size) && logfile_rotate(log_file, &now)) - return; - - if ((n = write(log_file, buf, n)) >= 0) - log_written += n; -} diff --git a/log.h b/log.h index e03199c6..5cb16d6f 100644 --- a/log.h +++ b/log.h @@ -56,7 +56,6 @@ void trace_init(int enable); void __openlog(const char *ident, int option, int facility); void logfile_init(const char *name, const char *path, size_t size); void passt_vsyslog(bool newline, int pri, const char *format, va_list ap); -void logfile_write(bool newline, int pri, const char *format, va_list ap); void __setlogmask(int mask); #endif /* LOG_H */ -- 2.45.2