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.133.124]) by passt.top (Postfix) with ESMTP id 700DF5A0082 for ; Wed, 8 Feb 2023 18:48:49 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1675878528; 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=MVIRbtfOuO2cwr7J138JRCX/KOFTwymYd3QsOudLmxg=; b=PBmgMrnsKsHC3F3mzJACxBNLOXyWbLImUHnBBPKto36NhknkqRmVW3BUwDFIP0AGugLTrp esTojwtO5+g/fSsmFVFn7BZPCuJlJ5hg5FiDsWAyLl+P8aNArHE23SqAYJwUmmdyJ0AhPA umU8xjgy6qAVLYTBzOdSai1tf97Wp4w= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-16-2hp0Vl9jNICk6rQ5uGgK2A-1; Wed, 08 Feb 2023 12:48:39 -0500 X-MC-Unique: 2hp0Vl9jNICk6rQ5uGgK2A-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 4B20C3815EE6; Wed, 8 Feb 2023 17:48:39 +0000 (UTC) Received: from vhost3.router.laine.org (unknown [10.2.17.81]) by smtp.corp.redhat.com (Postfix) with ESMTP id 2F8D12166B29; Wed, 8 Feb 2023 17:48:39 +0000 (UTC) From: Laine Stump To: passt-dev@passt.top, laine@laine.org Subject: [PATCH v2 1/9] log to stderr until process is daemonized, even if a logfile is set Date: Wed, 8 Feb 2023 12:48:30 -0500 Message-Id: <20230208174838.1680517-2-laine@redhat.com> In-Reply-To: <20230208174838.1680517-1-laine@redhat.com> References: <20230208174838.1680517-1-laine@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.6 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: SPUHUMQ3ZRNFJILJ4R2HSL2VAONLISM2 X-Message-ID-Hash: SPUHUMQ3ZRNFJILJ4R2HSL2VAONLISM2 X-MailFrom: laine@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.3 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: Signed-off-by: Laine Stump --- log.c | 14 +++++++++++++- log.h | 1 + passt.c | 6 ++++-- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/log.c b/log.c index 468c730..0ab0adf 100644 --- a/log.c +++ b/log.c @@ -34,6 +34,7 @@ static int log_sock = -1; /* Optional socket to system logger */ static char log_ident[BUFSIZ]; /* Identifier string for openlog() */ static int log_mask; /* Current log priority mask */ static int log_opt; /* Options for openlog() */ +static int log_daemon_mode = false; /* true once process is daemonized */ static int log_file = -1; /* Optional log file descriptor */ static size_t log_size; /* Maximum log file size in bytes */ @@ -67,7 +68,8 @@ void name(const char *format, ...) { \ } \ \ if ((setlogmask(0) & LOG_MASK(LOG_DEBUG) || \ - setlogmask(0) == LOG_MASK(LOG_EMERG)) && log_file == -1) { \ + setlogmask(0) == LOG_MASK(LOG_EMERG)) \ + && (log_file == -1 || !log_daemon_mode)) { \ va_start(args, format); \ (void)vfprintf(stderr, format, args); \ va_end(args); \ @@ -91,6 +93,16 @@ logfn(warn, LOG_WARNING) logfn(info, LOG_INFO) logfn(debug, LOG_DEBUG) +/** + * log_go_daemon() - tell logging subsystem that the process has been + * been daemonized, so it stop logging to stderr + * if appropriate. + */ +void log_go_daemon(void) +{ + log_daemon_mode = true; +} + /** * trace_init() - Set log_trace depending on trace (debug) mode * @enable: Tracing debug mode enabled if non-zero diff --git a/log.h b/log.h index 987dc17..a57c777 100644 --- a/log.h +++ b/log.h @@ -25,6 +25,7 @@ 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 log_go_daemon(void); 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.c b/passt.c index 8b2c50d..cf010e8 100644 --- a/passt.c +++ b/passt.c @@ -296,10 +296,12 @@ int main(int argc, char **argv) exit(EXIT_FAILURE); } - if (!c.foreground) + if (!c.foreground) { __daemon(pidfile_fd, devnull_fd); - else + log_go_daemon(); + } else { write_pidfile(pidfile_fd, getpid()); + } isolate_postfork(&c); -- 2.39.1