Skip to content

Commit

Permalink
common: split Wayland socket setup to separate file
Browse files Browse the repository at this point in the history
More complicated setup logic will be added next commit.
  • Loading branch information
emersion authored and alexlarsson committed Aug 24, 2023
1 parent 522dd8b commit b4822e2
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 45 deletions.
2 changes: 2 additions & 0 deletions common/Makefile.am.inc
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ libflatpak_common_la_SOURCES = \
common/flatpak-run-pulseaudio.c \
common/flatpak-run-sockets-private.h \
common/flatpak-run-sockets.c \
common/flatpak-run-wayland-private.h \
common/flatpak-run-wayland.c \
common/flatpak-run-x11-private.h \
common/flatpak-run-x11.c \
common/flatpak-syscalls-private.h \
Expand Down
46 changes: 1 addition & 45 deletions common/flatpak-run-sockets.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,54 +26,10 @@

#include "flatpak-run-cups-private.h"
#include "flatpak-run-pulseaudio-private.h"
#include "flatpak-run-wayland-private.h"
#include "flatpak-run-x11-private.h"
#include "flatpak-utils-private.h"

/**
* flatpak_run_add_wayland_args:
*
* Returns: %TRUE if a Wayland socket was found.
*/
static gboolean
flatpak_run_add_wayland_args (FlatpakBwrap *bwrap)
{
const char *wayland_display;
g_autofree char *user_runtime_dir = flatpak_get_real_xdg_runtime_dir ();
g_autofree char *wayland_socket = NULL;
g_autofree char *sandbox_wayland_socket = NULL;
gboolean res = FALSE;
struct stat statbuf;

wayland_display = g_getenv ("WAYLAND_DISPLAY");
if (!wayland_display)
wayland_display = "wayland-0";

if (wayland_display[0] == '/')
wayland_socket = g_strdup (wayland_display);
else
wayland_socket = g_build_filename (user_runtime_dir, wayland_display, NULL);

if (!g_str_has_prefix (wayland_display, "wayland-") ||
strchr (wayland_display, '/') != NULL)
{
wayland_display = "wayland-0";
flatpak_bwrap_set_env (bwrap, "WAYLAND_DISPLAY", wayland_display, TRUE);
}

sandbox_wayland_socket = g_strdup_printf ("/run/flatpak/%s", wayland_display);

if (stat (wayland_socket, &statbuf) == 0 &&
(statbuf.st_mode & S_IFMT) == S_IFSOCK)
{
res = TRUE;
flatpak_bwrap_add_args (bwrap,
"--ro-bind", wayland_socket, sandbox_wayland_socket,
NULL);
flatpak_bwrap_add_runtime_dir_member (bwrap, wayland_display);
}
return res;
}

static void
flatpak_run_add_gssproxy_args (FlatpakBwrap *bwrap)
{
Expand Down
34 changes: 34 additions & 0 deletions common/flatpak-run-wayland-private.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright © 2014 Red Hat, Inc
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
* Authors:
* Alexander Larsson <[email protected]>
*/

#pragma once

#include "libglnx.h"

#include "flatpak-bwrap-private.h"
#include "flatpak-common-types-private.h"
#include "flatpak-context-private.h"

G_BEGIN_DECLS

gboolean
flatpak_run_add_wayland_args (FlatpakBwrap *bwrap);

G_END_DECLS
69 changes: 69 additions & 0 deletions common/flatpak-run-wayland.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/* vi:set et sw=2 sts=2 cin cino=t0,f0,(0,{s,>2s,n-s,^-s,e-s:
* Copyright © 2014-2019 Red Hat, Inc
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
* Authors:
* Alexander Larsson <[email protected]>
*/

#include "config.h"
#include "flatpak-run-wayland-private.h"

#include "flatpak-utils-private.h"

/**
* flatpak_run_add_wayland_args:
*
* Returns: %TRUE if a Wayland socket was found.
*/
gboolean
flatpak_run_add_wayland_args (FlatpakBwrap *bwrap)
{
const char *wayland_display;
g_autofree char *user_runtime_dir = flatpak_get_real_xdg_runtime_dir ();
g_autofree char *wayland_socket = NULL;
g_autofree char *sandbox_wayland_socket = NULL;
gboolean res = FALSE;
struct stat statbuf;

wayland_display = g_getenv ("WAYLAND_DISPLAY");
if (!wayland_display)
wayland_display = "wayland-0";

if (wayland_display[0] == '/')
wayland_socket = g_strdup (wayland_display);
else
wayland_socket = g_build_filename (user_runtime_dir, wayland_display, NULL);

if (!g_str_has_prefix (wayland_display, "wayland-") ||
strchr (wayland_display, '/') != NULL)
{
wayland_display = "wayland-0";
flatpak_bwrap_set_env (bwrap, "WAYLAND_DISPLAY", wayland_display, TRUE);
}

sandbox_wayland_socket = g_strdup_printf ("/run/flatpak/%s", wayland_display);

if (stat (wayland_socket, &statbuf) == 0 &&
(statbuf.st_mode & S_IFMT) == S_IFSOCK)
{
res = TRUE;
flatpak_bwrap_add_args (bwrap,
"--ro-bind", wayland_socket, sandbox_wayland_socket,
NULL);
flatpak_bwrap_add_runtime_dir_member (bwrap, wayland_display);
}
return res;
}
1 change: 1 addition & 0 deletions common/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ sources = [
'flatpak-run-dbus.c',
'flatpak-run-pulseaudio.c',
'flatpak-run-sockets.c',
'flatpak-run-wayland.c',
'flatpak-run-x11.c',
'flatpak-transaction.c',
'flatpak-utils-http.c',
Expand Down

0 comments on commit b4822e2

Please sign in to comment.