From nobody Fri Dec 29 08:33:13 2023 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4T1dtd4xS4z55cxg; Fri, 29 Dec 2023 08:33:13 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4T1dtd4NWzz4kty; Fri, 29 Dec 2023 08:33:13 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1703838793; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=FNPdiwfE/Zuzl4eH2Zeee1nfHMG+FPODwKa3BlNyTWQ=; b=kjSDfZsjnhFlo6zVS5YQt1axhOe9E5Lg39pu9RtJJJnxAB9OO+ZdmOOpyGUnJVg87v2EDw Wj642pq9WlsG0G0w+mX1QiJthiwPBKuCOHCOEM3i0BouCtUjA2Mm4SFwx05fR9xX/geREi Hlxe9F8nqEcpmPGXyPJ3jgAwyocPm1Yg04LCYXFdscjrz0trqmkCxPOIqEG2kPhWVHZACU VLbNu6LGSL6t8C5yrh+1Aad7+Tg24I0Re+hSlKoi19sYB1E4kVK5ggcZxIn6LJd5l9Nr3K bZdTBqb/mMclF/n0qOyG+qBvW8l9Kdcvj8m5kEKTpWTGIe5mo+cLT5/10jU9aA== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1703838793; a=rsa-sha256; cv=none; b=pqhrQMUhs7NHjv7B94BznbdgSX5S5p5QJuxwKJ+fG5y7TvXmKrPMvMkEwm9+DyC8AMwbya MjAluxw1ERheamotiRGR2PF1g9yi7GoUse9xSft/Tp5F+AgvKR6EUGb8+R3gFlQTp1ZxGP kJZJ13Z5rIKcTmWsjYBjfzeNzDoJwGbKOnig0C+QSeqNNKymLvdA0qe5jpjlfvk6rk4M0l AYW4USsQCNQvJ/6VaTGEDRWHPzGlKaNZrDTHn246MMcH9MFbUoKNXxg8ASG0yWFefDeq0Q bOzCEN4CZd5tSWk2GqiCsoZewpr2H0mLeXNDzz7miI737B4llyWoQ8NnXehTJw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1703838793; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=FNPdiwfE/Zuzl4eH2Zeee1nfHMG+FPODwKa3BlNyTWQ=; b=LywP7e+fMqY9/09MRa3IaeNQuw9TtPelrt52y9fxlDfwKbT71gWyTzBwwRZqcXogzD59zz ts9DPAnxMyBv5cYao55kBuDoafdgZg6Il1H0iwAT7qtTU7+yYycnJKWZVwytbGdIqugQZy MKd6/wjSnTTLtCmXvsmLg3aQ/l1GC1GimAkkL5Ro82sHB7sIHS/EtV/DAdEiF4tI4Mtkfj GZSOnmIxRj52VAfgNFL4/PQ/Y9S3ipNKpUS4fhrNHVjdQZohPoxA+83VdhP8xVRzyHxWlf P9tmgefiZqxfuXO/x94foGE8LgsfKh3Agpzg8TMAqk2SKL38B/2uDpkWrva8Iw== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4T1dtd3Rclzysk; Fri, 29 Dec 2023 08:33:13 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 3BT8XDO7012484; Fri, 29 Dec 2023 08:33:13 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 3BT8XDRF012481; Fri, 29 Dec 2023 08:33:13 GMT (envelope-from git) Date: Fri, 29 Dec 2023 08:33:13 GMT Message-Id: <202312290833.3BT8XDRF012481@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Xin LI Subject: git: 61174ad88e33 - main - newsyslog(8): Add support of specifying compression method in configuration file. List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: delphij X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 61174ad88e33b6162fa1c9ee37d7f227e0bbf931 Auto-Submitted: auto-generated The branch main has been updated by delphij: URL: https://cgit.FreeBSD.org/src/commit/?id=61174ad88e33b6162fa1c9ee37d7f227e0bbf931 commit 61174ad88e33b6162fa1c9ee37d7f227e0bbf931 Author: Xin LI AuthorDate: 2023-12-25 07:47:36 +0000 Commit: Xin LI CommitDate: 2023-12-29 08:32:26 +0000 newsyslog(8): Add support of specifying compression method in configuration file. Administrators can now specify a global compression method directly at the beginning of the newsyslog.conf file, for example: none Relnotes: yes Reviewed by: dvl MFC after: 1 week Differential Revision: https://reviews.freebsd.org/D43174 --- usr.sbin/newsyslog/newsyslog.c | 97 +++++++++++++++++++++++++++++++------ usr.sbin/newsyslog/newsyslog.conf.5 | 23 +++++++-- 2 files changed, 99 insertions(+), 21 deletions(-) diff --git a/usr.sbin/newsyslog/newsyslog.c b/usr.sbin/newsyslog/newsyslog.c index a08e7b903707..9f9185c1ad20 100644 --- a/usr.sbin/newsyslog/newsyslog.c +++ b/usr.sbin/newsyslog/newsyslog.c @@ -74,6 +74,7 @@ #include #include #include +#include #include #include #include @@ -124,6 +125,7 @@ enum compress_types_enum { #define DEFAULT_MARKER "" #define DEBUG_MARKER "" #define INCLUDE_MARKER "" +#define COMPRESS_MARKER "" #define DEFAULT_TIMEFNAME_FMT "%Y%m%dT%H%M%S" #define MAX_OLDLOGS 65536 /* Default maximum number of old logfiles */ @@ -248,6 +250,8 @@ static char *archdirname; /* Directory path to old logfiles archive */ static char *destdir = NULL; /* Directory to treat at root for logs */ static const char *conf; /* Configuration file to use */ static enum compress_types_enum compress_type_override = COMPRESS_LEGACY; /* Compression type */ +static bool compress_type_set = false; +static bool compress_type_seen = false; struct ptime_data *dbg_timenow; /* A "timenow" value set via -D option */ static struct ptime_data *timenow; /* The time to use for checking at-fields */ @@ -505,6 +509,37 @@ free_clist(struct cflist *list) list = NULL; } +static bool +parse_compression_type(const char *str, enum compress_types_enum *type) +{ + int i; + + for (i = 0; i < COMPRESS_TYPES; i++) { + if (strcasecmp(str, compress_type[i].name) == 0) { + *type = i; + break; + } + } + if (i == COMPRESS_TYPES) { + if (strcasecmp(str, "legacy") == 0) + compress_type_override = COMPRESS_LEGACY; + else { + return (false); + } + } + return (true); +} + +static const char * +compression_type_name(enum compress_types_enum type) +{ + + if (type == COMPRESS_LEGACY) + return ("legacy"); + else + return (compress_type[type].name); +} + static fk_entry do_entry(struct conf_entry * ent) { @@ -628,8 +663,13 @@ do_entry(struct conf_entry * ent) if (ent->rotate && !norotate) { if (temp_reason[0] != '\0') ent->r_reason = strdup(temp_reason); - if (verbose) - printf("--> trimming log....\n"); + if (verbose) { + if (ent->compress == COMPRESS_NONE) + printf("--> trimming log....\n"); + else + printf("--> trimming log and compressing with %s....\n", + compression_type_name(ent->compress)); + } if (noaction && !verbose) printf("%s <%d%s>: trimming\n", ent->log, ent->numlogs, @@ -647,7 +687,7 @@ do_entry(struct conf_entry * ent) static void parse_args(int argc, char **argv) { - int ch, i; + int ch; char *p; timenow = ptime_init(NULL); @@ -667,20 +707,11 @@ parse_args(int argc, char **argv) archdirname = optarg; break; case 'c': - for (i = 0; i < COMPRESS_TYPES; i++) { - if (strcmp(optarg, compress_type[i].name) == 0) { - compress_type_override = i; - break; - } - } - if (i == COMPRESS_TYPES) { - if (strcmp(optarg, "legacy") == 0) - compress_type_override = COMPRESS_LEGACY; - else { - warnx("Unrecognized compression method '%s'.", optarg); - usage(); - } + if (!parse_compression_type(optarg, &compress_type_override)) { + warnx("Unrecognized compression method '%s'.", optarg); + usage(); } + compress_type_set = true; break; case 'd': destdir = optarg; @@ -1187,6 +1218,36 @@ parse_file(FILE *cf, struct cflist *work_p, struct cflist *glob_p, } else add_to_queue(q, inclist); continue; + } else if (strcasecmp(COMPRESS_MARKER, q) == 0) { + enum compress_types_enum result; + + if (verbose) + printf("Found: %s", errline); + q = parse = missing_field(sob(parse + 1), errline); + parse = son(parse); + if (!*parse) + warnx("compress line specifies no option:\n%s", + errline); + else { + *parse = '\0'; + if (parse_compression_type(q, &result)) { + if (compress_type_set) { + warnx("Ignoring compress line " + "option '%s', using '%s' instead", + q, + compression_type_name(compress_type_override)); + } else { + if (compress_type_seen) + warnx("Compress type should appear before all log files:\n%s", + errline); + compress_type_override = result; + compress_type_set = true; + } + } else { + warnx("Bad compress option '%s'", q); + }; + } + continue; } #define badline(msg, ...) do { \ @@ -1357,6 +1418,7 @@ no_trimat: working->compress = COMPRESS_BZIP2; else working->compress = compress_type_override; + compress_type_seen = true; break; case 'n': working->flags |= CE_NOSIGNAL; @@ -1381,18 +1443,21 @@ no_trimat: working->compress = COMPRESS_XZ; else working->compress = compress_type_override; + compress_type_seen = true; break; case 'y': if (compress_type_override == COMPRESS_LEGACY) working->compress = COMPRESS_ZSTD; else working->compress = compress_type_override; + compress_type_seen = true; break; case 'z': if (compress_type_override == COMPRESS_LEGACY) working->compress = COMPRESS_GZIP; else working->compress = compress_type_override; + compress_type_seen = true; break; case '-': break; diff --git a/usr.sbin/newsyslog/newsyslog.conf.5 b/usr.sbin/newsyslog/newsyslog.conf.5 index a53af5b38319..120c5a0cb7cc 100644 --- a/usr.sbin/newsyslog/newsyslog.conf.5 +++ b/usr.sbin/newsyslog/newsyslog.conf.5 @@ -18,7 +18,7 @@ .\" the suitability of this software for any purpose. It is .\" provided "as is" without express or implied warranty. .\" -.Dd December 22, 2023 +.Dd December 25, 2023 .Dt NEWSYSLOG.CONF 5 .Os .Sh NAME @@ -67,11 +67,24 @@ The fields of the configuration file are as follows: .Bl -tag -width indent .It Ar logfile_name Name of the system log file to be archived, -or one of the literal strings -.Dq Aq Li default , +or one of the special strings +.Dq Li , +.Dq Li , or -.Dq Aq Li include . -The special default entry will only be used if a log file +.Dq Li . +The entry, +which should be placed at the beginning of the +.Nm +configuration file, +sets the global compress method. +This method is applied when a log file is flagged as +compressible, +which has the same effect of passing a compress method to the +.Fl c +option on the +.Xr newsyslog 8 +command line. +The special entry will only be used if a log file name is given as a command line argument to .Xr newsyslog 8 , and if that log file name is not matched by any other