Skip to content

Commit

Permalink
launch: add internal helper for socket paths creation
Browse files Browse the repository at this point in the history
Introduce an internal helper to create paths for sockets -- will be
useful for changing later the logic for placing sockets.
Futhermore, check that the length of sockets won't overflow the buffer
for their filenames.
  • Loading branch information
ptoscano committed Feb 3, 2016
1 parent 673a7a9 commit 4f7251f
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/guestfs-internal.h
Expand Up @@ -782,6 +782,7 @@ extern void guestfs_int_launch_send_progress (guestfs_h *g, int perdozen);
extern char *guestfs_int_appliance_command_line (guestfs_h *g, const char *appliance_dev, int flags);
#define APPLIANCE_COMMAND_LINE_IS_TCG 1
const char *guestfs_int_get_cpu_model (int kvm);
int guestfs_int_create_socketname (guestfs_h *g, const char *filename, char (*sockname)[UNIX_PATH_MAX]);
extern void guestfs_int_register_backend (const char *name, const struct backend_ops *);
extern int guestfs_int_set_backend (guestfs_h *g, const char *method);

Expand Down
4 changes: 3 additions & 1 deletion src/launch-direct.c
Expand Up @@ -295,7 +295,9 @@ launch_direct (guestfs_h *g, void *datav, const char *arg)
/* Using virtio-serial, we need to create a local Unix domain socket
* for qemu to connect to.
*/
snprintf (data->guestfsd_sock, sizeof data->guestfsd_sock, "%s/guestfsd.sock", g->tmpdir);
if (guestfs_int_create_socketname (g, "guestfsd.sock",
&data->guestfsd_sock) == -1)
goto cleanup0;

daemon_accept_sock = socket (AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0);
if (daemon_accept_sock == -1) {
Expand Down
10 changes: 6 additions & 4 deletions src/launch-libvirt.c
Expand Up @@ -395,8 +395,9 @@ launch_libvirt (guestfs_h *g, void *datav, const char *libvirt_uri)
/* Using virtio-serial, we need to create a local Unix domain socket
* for qemu to connect to.
*/
snprintf (data->guestfsd_path, sizeof data->guestfsd_path,
"%s/guestfsd.sock", g->tmpdir);
if (guestfs_int_create_socketname (g, "guestfsd.sock",
&data->guestfsd_path) == -1)
goto cleanup;

set_socket_create_context (g);

Expand All @@ -421,8 +422,9 @@ launch_libvirt (guestfs_h *g, void *datav, const char *libvirt_uri)
}

/* For the serial console. */
snprintf (data->console_path, sizeof data->console_path,
"%s/console.sock", g->tmpdir);
if (guestfs_int_create_socketname (g, "console.sock",
&data->console_path) == -1)
goto cleanup;

console_sock = socket (AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0);
if (console_sock == -1) {
Expand Down
17 changes: 17 additions & 0 deletions src/launch.c
Expand Up @@ -418,6 +418,23 @@ guestfs_int_get_cpu_model (int kvm)
#endif
}

/* Create the path for a socket with the selected filename in the
* tmpdir.
*/
int
guestfs_int_create_socketname (guestfs_h *g, const char *filename,
char (*sockpath)[UNIX_PATH_MAX])
{
if (strlen (g->tmpdir) + 1 + strlen (filename) > UNIX_PATH_MAX-1) {
error (g, _("socket path too long: %s/%s"), g->tmpdir, filename);
return -1;
}

snprintf (*sockpath, UNIX_PATH_MAX, "%s/%s", g->tmpdir, filename);

return 0;
}

/* glibc documents, but does not actually implement, a 'getumask(3)'
* call. This implements a thread-safe way to get the umask. Note
* this is only called when g->verbose is true and after g->tmpdir
Expand Down

0 comments on commit 4f7251f

Please sign in to comment.