From bd3e033e08555bb6123f927b7bf04afcec6f93c4 Mon Sep 17 00:00:00 2001 From: Laszlo Ersek Date: Fri, 14 Jul 2023 15:22:12 +0200 Subject: [PATCH] lib: introduce guestfs_int_make_pid_path() Introduce a small function for creating pathnames for PID files. guestfs_int_make_pid_path() is something of an amalgamation of guestfs_int_make_temp_path() [1] and guestfs_int_create_socketname() [2]: - it creates a pathname under sockdir, like [2], - it uses the handle's unique counter, like [1], - it takes a name like both [1] and [2], but the name is not size-limited like in [2], plus we hardcode the suffix from [1] as ".pid". Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2184967 Signed-off-by: Laszlo Ersek Reviewed-by: Richard W.M. Jones Message-Id: <20230714132213.96616-7-lersek@redhat.com> --- lib/guestfs-internal.h | 1 + lib/tmpdirs.c | 15 +++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/lib/guestfs-internal.h b/lib/guestfs-internal.h index 2ee16ea1e..9ba4d4ad4 100644 --- a/lib/guestfs-internal.h +++ b/lib/guestfs-internal.h @@ -669,6 +669,7 @@ extern int guestfs_int_lazy_make_tmpdir (guestfs_h *g); extern int guestfs_int_lazy_make_sockdir (guestfs_h *g); extern char *guestfs_int_make_temp_path (guestfs_h *g, const char *name, const char *extension); extern int guestfs_int_create_socketname (guestfs_h *g, const char *filename, char (*sockname)[UNIX_PATH_MAX]); +extern char *guestfs_int_make_pid_path (guestfs_h *g, const char *name); extern char *guestfs_int_lazy_make_supermin_appliance_dir (guestfs_h *g); extern void guestfs_int_remove_tmpdir (guestfs_h *g); extern void guestfs_int_remove_sockdir (guestfs_h *g); diff --git a/lib/tmpdirs.c b/lib/tmpdirs.c index 24adf98da..22b8f54b0 100644 --- a/lib/tmpdirs.c +++ b/lib/tmpdirs.c @@ -279,6 +279,21 @@ guestfs_int_create_socketname (guestfs_h *g, const char *filename, return 0; } +/** + * Generate unique paths for PID files. + * + * Returns a unique path or NULL on error. On success, the pathname points + * under sockdir and not tmpdir; daemons that write PID files after dropping + * privileges may not have access to tmpdir. + */ +char * +guestfs_int_make_pid_path (guestfs_h *g, const char *name) +{ + if (guestfs_int_lazy_make_sockdir (g) < 0) + return NULL; + return safe_asprintf (g, "%s/%s%d.pid", g->sockdir, name, ++g->unique); +} + /** * Create the supermin appliance directory under cachedir, if it does * not exist.