[Libguestfs] [libguestfs PATCH v2 6/7] lib: introduce guestfs_int_make_pid_path()

Laszlo Ersek lersek at redhat.com
Fri Jul 14 13:22:12 UTC 2023


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 <lersek at redhat.com>
Reviewed-by: Richard W.M. Jones <rjones at redhat.com>
---

Notes:
    v2:
    
    - pick up Rich's R-b

 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 2ee16ea1e75a..9ba4d4ad46cf 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 24adf98daee0..22b8f54b0693 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.



More information about the Libguestfs mailing list