From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by passt.top (Postfix) with ESMTP id AD2825A0271 for ; Tue, 6 Jun 2023 13:41:36 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1686051695; 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: in-reply-to:in-reply-to:references:references; bh=sihEBIdOyztLbUDUZ20Kxxo7yF5JRbIYp5eVw/p83n4=; b=NXlPDfPCRhSiIxiMonnv8iJT+9T81pkpq0vQt/rSSaRMBALagxe9A2l8Mp3KSmtdSGaadw B2ADkSvghgsi5olaEh95/vjSeydmC2vqXaSL3GtsZZGxC/0dmuEfqnCFOQ1Xfv+3dwHiGA MCLWgoNG9/rFQBnzg+o3AgjGO3SToMo= 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-655-PDHZ3E7nPyKK98e5O8kOMA-1; Tue, 06 Jun 2023 07:41:33 -0400 X-MC-Unique: PDHZ3E7nPyKK98e5O8kOMA-1 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 4E213811E78 for ; Tue, 6 Jun 2023 11:41:33 +0000 (UTC) Received: from localhost.localdomain (unknown [10.43.2.36]) by smtp.corp.redhat.com (Postfix) with ESMTP id EC46CC0448E for ; Tue, 6 Jun 2023 11:41:32 +0000 (UTC) From: Michal Privoznik To: passt-dev@passt.top Subject: [PATCH 2/2] conf, log: Introduce --log-fd option Date: Tue, 6 Jun 2023 13:41:30 +0200 Message-Id: <24c5ae1d4a44e6c3e32cc5e909dacd714b45e3b5.1686037337.git.mprivozn@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.8 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true Message-ID-Hash: S2D6YRXBIJHXFTP3XRD6SEYAOLMOXL62 X-Message-ID-Hash: S2D6YRXBIJHXFTP3XRD6SEYAOLMOXL62 X-MailFrom: mprivozn@redhat.com 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 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: So far, users/mgmt apps can use --log-file to specify log output. This has a downside though - they have to set up permissions so that passt/pasta can open the file. But we can do a bit better: allow mgmt apps pass an pre-opened FD and write all log messages into it. This then allows the log file to be readable by very few users (root:root even) or reside in a path which would be otherwise inaccessible. Signed-off-by: Michal Privoznik --- conf.c | 29 +++++++++++++++++++++++++---- log.c | 17 ++++++++++++----- log.h | 2 +- passt.1 | 5 +++++ 4 files changed, 43 insertions(+), 10 deletions(-) diff --git a/conf.c b/conf.c index ffff235..59d0a8e 100644 --- a/conf.c +++ b/conf.c @@ -744,6 +744,7 @@ static void usage(const char *name) info( " -e, --stderr Log to stderr too"); info( " default: log to system logger only if started from a TTY"); info( " -l, --log-file PATH Log (only) to given file"); + info( " --log-fd FD Log (only) to given file descriptor"); info( " --log-size BYTES Maximum size of log file"); info( " default: 1 MiB"); info( " --runas UID|UID:GID Run as given UID, GID, which can be"); @@ -1181,6 +1182,7 @@ void conf(struct ctx *c, int argc, char **argv) {"config-net", no_argument, NULL, 17 }, {"no-copy-routes", no_argument, NULL, 18 }, {"no-copy-addrs", no_argument, NULL, 19 }, + {"log-fd", required_argument, NULL, 20 }, { 0 }, }; struct get_bound_ports_ns_arg ns_ports_arg = { .c = c }; @@ -1192,7 +1194,7 @@ void conf(struct ctx *c, int argc, char **argv) struct in_addr *dns4 = c->ip4.dns; unsigned int ifi4 = 0, ifi6 = 0; const char *optstring; - int name, ret, b, i; + int name, ret, b, i, logfd = -1; size_t logsize = 0; uid_t uid; gid_t gid; @@ -1358,6 +1360,22 @@ void conf(struct ctx *c, int argc, char **argv) warn("--no-copy-addrs will be dropped soon"); c->no_copy_addrs = 1; + break; + case 20: + if (logfile) + die("Either --log-file or --log-fd"); + + if (c->force_stderr) + die("Can't log to both stderr and file"); + + if (logfd >= 0) + die("Multiple --log-fd options given"); + + errno = 0; + logfd = strtol(optarg, NULL, 0); + if (logfd < 0 || errno) + die("Invalid --log-fd: %s", optarg); + break; case 'd': if (c->debug) @@ -1369,7 +1387,7 @@ void conf(struct ctx *c, int argc, char **argv) c->debug = 1; break; case 'e': - if (logfile) + if (logfile || logfd >= 0) die("Can't log to both file and stderr"); if (c->force_stderr) @@ -1381,6 +1399,9 @@ void conf(struct ctx *c, int argc, char **argv) if (c->force_stderr) die("Can't log to both stderr and file"); + if (logfd >= 0) + die("Either --log-file or --log-fd"); + if (logfile) die("Multiple --log-file options given"); @@ -1659,9 +1680,9 @@ void conf(struct ctx *c, int argc, char **argv) conf_ugid(runas, &uid, &gid); - if (logfile) { + if (logfile || logfd >= 0) { logfile_init(c->mode == MODE_PASST ? "passt" : "pasta", - logfile, logsize); + logfile, logfd, logsize); } nl_sock_init(c, false); diff --git a/log.c b/log.c index 3a3d101..28929a7 100644 --- a/log.c +++ b/log.c @@ -174,9 +174,10 @@ void passt_vsyslog(int pri, const char *format, va_list ap) * logfile_init() - Open log file and write header with PID, version, path * @name: Identifier for header: passt or pasta * @path: Path to log file + * @logfd: Pre-opened log file descriptor (if >= 0) * @size: Maximum size of log file: log_cut_size is calculatd here */ -void logfile_init(const char *name, const char *path, size_t size) +void logfile_init(const char *name, const char *path, int logfd, size_t size) { char nl = '\n', exe[PATH_MAX] = { 0 }; int n; @@ -186,10 +187,16 @@ void logfile_init(const char *name, const char *path, size_t size) exit(EXIT_FAILURE); } - log_file = open(path, O_CREAT | O_TRUNC | O_APPEND | O_RDWR | O_CLOEXEC, - S_IRUSR | S_IWUSR); - if (log_file == -1) - die("Couldn't open log file %s: %s", path, strerror(errno)); + if (logfd >= 0) { + if (set_cloexec(logfd) < 0) + die("Could not set CLOEXEC flag on logfd", strerror(errno)); + log_file = logfd; + } else { + log_file = open(path, O_CREAT | O_TRUNC | O_APPEND | O_RDWR | O_CLOEXEC, + S_IRUSR | S_IWUSR); + if (log_file == -1) + die("Couldn't open log file %s: %s", path, strerror(errno)); + } log_size = size ? size : LOGFILE_SIZE_DEFAULT; diff --git a/log.h b/log.h index 3aab29d..d96359a 100644 --- a/log.h +++ b/log.h @@ -30,7 +30,7 @@ void trace_init(int enable); } while (0) void __openlog(const char *ident, int option, int facility); -void logfile_init(const char *name, const char *path, size_t size); +void logfile_init(const char *name, const char *path, int logfd, size_t size); void passt_vsyslog(int pri, const char *format, va_list ap); void logfile_write(int pri, const char *format, va_list ap); void __setlogmask(int mask); diff --git a/passt.1 b/passt.1 index 1ad4276..ee5abbb 100644 --- a/passt.1 +++ b/passt.1 @@ -101,6 +101,11 @@ terminal, and to both system logger and standard error otherwise. .BR \-l ", " \-\-log-file " " \fIPATH\fR Log to file \fIPATH\fR, not to standard error, and not to the system logger. +.TP +.BR \-l ", " \-\-log-fd " " \fIFD\fR +Log to pre-opened file descriptor \fIFD\fR, not to standard error, and not to +the system logger. + .TP .BR \-\-log-size " " \fISIZE\fR Limit log file size to \fISIZE\fR bytes. When the log file is full, make room -- 2.39.3