public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
From: Jon Maloy <jmaloy@redhat.com>
To: sbrivio@redhat.com, david@gibson.dropbear.id.au,
	jmaloy@redhat.com, passt-dev@passt.top
Subject: [PATCH] util, passt: Close daemon-lifetime fds on exit to avoid Coverity warning
Date: Sun, 31 May 2026 16:13:24 -0400	[thread overview]
Message-ID: <20260531201324.1714921-1-jmaloy@redhat.com> (raw)

conf_open_files() opens four file descriptors (fd_tap_listen,
fd_repair_listen, pidfile_fd, fd_control_listen) that are held for
the entire daemon lifetime.  Because no close() call exists for them
anywhere, Coverity flags each as INCOMPLETE_DEALLOCATOR. This is
clearly a false positive, but we still want to get rid of this
warning.

We now register the execution context so that passt_exit() can use
to close these descriptors before calling _exit().  All exit paths
(signal handler, die(), die_perror()) funnel through passt_exit(),
so this covers all cases.

Signed-off-by: Jon Maloy <jmaloy@redhat.com>
---
 log.h   |  2 ++
 passt.c |  1 +
 util.c  | 22 ++++++++++++++++++++++
 3 files changed, 25 insertions(+)

diff --git a/log.h b/log.h
index 69cfb507..079f429c 100644
--- a/log.h
+++ b/log.h
@@ -63,7 +63,9 @@ extern bool debug_flag;
 /* This would make more sense in util.h, but because we use it in die(), that
  * would cause awkward circular reference problems.
  */
+struct ctx;
 void passt_exit(int status) __attribute__((noreturn));
+void passt_exit_set_ctx(struct ctx *c);
 
 #define LOGFILE_SIZE_DEFAULT		(1024 * 1024UL)
 #define LOGFILE_CUT_RATIO		30	/* When full, cut ~30% size */
diff --git a/passt.c b/passt.c
index b6fc12d4..ec6aa57a 100644
--- a/passt.c
+++ b/passt.c
@@ -392,6 +392,7 @@ int main(int argc, char **argv)
 	sock_probe_features(&c);
 
 	conf(&c, argc, argv);
+	passt_exit_set_ctx(&c);
 	trace_init(c.trace);
 
 	pasta_netns_quit_init(&c);
diff --git a/util.c b/util.c
index b64c29ed..f15b1f9a 100644
--- a/util.c
+++ b/util.c
@@ -1097,6 +1097,17 @@ void abort_with_msg(const char *fmt, ...)
 	abort();
 }
 
+static struct ctx *exit_cleanup_ctx;
+
+/**
+ * passt_exit_set_ctx() - Register context for cleanup on exit
+ * @c:		Execution context
+ */
+void passt_exit_set_ctx(struct ctx *c)
+{
+	exit_cleanup_ctx = c;
+}
+
 /**
  * passt_exit() - Perform vital cleanup and exit
  *
@@ -1108,6 +1119,17 @@ void abort_with_msg(const char *fmt, ...)
  */
 void passt_exit(int status)
 {
+	if (exit_cleanup_ctx) {
+		if (exit_cleanup_ctx->fd_tap_listen >= 0)
+			close(exit_cleanup_ctx->fd_tap_listen);
+		if (exit_cleanup_ctx->fd_repair_listen >= 0)
+			close(exit_cleanup_ctx->fd_repair_listen);
+		if (exit_cleanup_ctx->pidfile_fd >= 0)
+			close(exit_cleanup_ctx->pidfile_fd);
+		if (exit_cleanup_ctx->fd_control_listen >= 0)
+			close(exit_cleanup_ctx->fd_control_listen);
+	}
+
 	/* Make sure we don't leave the pcap file truncated */
 	if (pcap_fd != -1 && fsync(pcap_fd))
 		warn_perror("Failed to flush pcap file, it might be truncated");
-- 
2.52.0


             reply	other threads:[~2026-05-31 20:13 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-31 20:13 Jon Maloy [this message]
2026-06-02  1:28 ` David Gibson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260531201324.1714921-1-jmaloy@redhat.com \
    --to=jmaloy@redhat.com \
    --cc=david@gibson.dropbear.id.au \
    --cc=passt-dev@passt.top \
    --cc=sbrivio@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://passt.top/passt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for IMAP folder(s).