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 <lersek@redhat.com>
Reviewed-by: Richard W.M. Jones <rjones@redhat.com>
Message-Id: <20230714132213.96616-7-lersek@redhat.com>
This commit is contained in:
Laszlo Ersek
2023-07-14 15:22:12 +02:00
parent 0b2ad40a09
commit bd3e033e08
2 changed files with 16 additions and 0 deletions

View File

@@ -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);

View File

@@ -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.