diff --git a/config.h b/config.h new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/meson.build b/meson.build index 217500ef0156641262042d29fb8bee29e7d599e6..4e652b1a442de3983d1f3f2eca878c5524ab5910 100644 --- a/meson.build +++ b/meson.build @@ -95,6 +95,10 @@ project_warning_cflags = [ '-Wsign-compare', ] +project_include_dirs = include_directories( + '.', + 'subprojects', +) c_compiler = meson.get_compiler('c') supported_warning_cflags = c_compiler.get_supported_arguments(warning_cflags) @@ -171,6 +175,8 @@ executable( 'src/bwrap-lock.h', 'src/glib-backports.c', 'src/glib-backports.h', + 'src/flatpak-utils-base.c', + 'src/flatpak-utils-base-private.h', 'src/flatpak-utils.c', 'src/flatpak-utils-private.h', 'src/utils.c', @@ -184,6 +190,7 @@ executable( dependency('gio-unix-2.0', required : true), subproject('libglnx').get_variable('libglnx_dep'), ], + include_directories : project_include_dirs, install : true, install_dir : get_option('bindir'), build_rpath : '${ORIGIN}/../' + get_option('libdir'), @@ -203,6 +210,8 @@ executable( 'src/flatpak-bwrap-private.h', 'src/flatpak-run.c', 'src/flatpak-run-private.h', + 'src/flatpak-utils-base.c', + 'src/flatpak-utils-base-private.h', 'src/flatpak-utils.c', 'src/flatpak-utils-private.h', 'src/utils.c', @@ -226,6 +235,7 @@ executable( dependency('xau', required : true), subproject('libglnx').get_variable('libglnx_dep'), ], + include_directories : project_include_dirs, install : true, install_dir : get_option('bindir'), build_rpath : '${ORIGIN}/../' + get_option('libdir'), @@ -237,6 +247,7 @@ executable( sources : [ 'src/try-setlocale.c', ], + include_directories : project_include_dirs, install : true, install_dir : get_option('bindir'), ) diff --git a/src/bwrap-lock.h b/src/bwrap-lock.h index f841635fcaa1576ff902b6652705fa3ccdd7dfb1..562d28e5eb01dea1281d12deb1e8dfe161c4cc18 100644 --- a/src/bwrap-lock.h +++ b/src/bwrap-lock.h @@ -23,7 +23,7 @@ #include <glib-object.h> #include "glib-backports.h" -#include "libglnx.h" +#include "libglnx/libglnx.h" /** * PvBwrapLockFlags: diff --git a/src/bwrap.h b/src/bwrap.h index 492d89670267ff3d3869f4faf8fbc3352c05162d..792db2a7e2c6b7653e949de7b021d85021f3d1c2 100644 --- a/src/bwrap.h +++ b/src/bwrap.h @@ -22,7 +22,7 @@ #include <glib.h> #include <glib-object.h> -#include "libglnx.h" +#include "libglnx/libglnx.h" #include "flatpak-bwrap-private.h" #include "glib-backports.h" diff --git a/src/flatpak-bwrap-private.h b/src/flatpak-bwrap-private.h index 2a633d30cd609b870950f287cbfac8b79232c8a3..98cd82ebe827ed6d69570654aeed53553824bdb1 100644 --- a/src/flatpak-bwrap-private.h +++ b/src/flatpak-bwrap-private.h @@ -1,4 +1,7 @@ /* + * Taken from Flatpak + * Last updated: Flatpak 1.6.1 + * * Copyright © 2014-2018 Red Hat, Inc * * This program is free software; you can redistribute it and/or @@ -48,7 +51,7 @@ void flatpak_bwrap_add_noinherit_fd (FlatpakBwrap *bwrap, void flatpak_bwrap_add_fd (FlatpakBwrap *bwrap, int fd); void flatpak_bwrap_add_args (FlatpakBwrap *bwrap, - ...); + ...) G_GNUC_NULL_TERMINATED; void flatpak_bwrap_add_arg_printf (FlatpakBwrap *bwrap, const char *format, ...) G_GNUC_PRINTF (2, 3); @@ -80,7 +83,8 @@ gboolean flatpak_bwrap_bundle_args (FlatpakBwrap *bwrap, GError **error); void flatpak_bwrap_child_setup_cb (gpointer user_data); - +void flatpak_bwrap_child_setup (GArray *fd_array, + gboolean close_fd_workaround); G_DEFINE_AUTOPTR_CLEANUP_FUNC (FlatpakBwrap, flatpak_bwrap_free) diff --git a/src/flatpak-bwrap.c b/src/flatpak-bwrap.c index 626e8eaa060393f43c0c589171cdbf005c9704ce..cdfc7ff2c17e783d1f350b4d13472dbf8d13c710 100644 --- a/src/flatpak-bwrap.c +++ b/src/flatpak-bwrap.c @@ -1,4 +1,7 @@ /* + * Taken from Flatpak + * Last updated: Flatpak 1.6.1 + * * Copyright © 2014-2018 Red Hat, Inc * * This program is free software; you can redistribute it and/or @@ -18,6 +21,8 @@ * Alexander Larsson <alexl@redhat.com> */ +#include "config.h" + #include <string.h> #include <fcntl.h> #include <stdio.h> @@ -31,10 +36,11 @@ #include <gio/gunixfdlist.h> #include <gio/gio.h> -#include "libglnx.h" +#include "libglnx/libglnx.h" #include "flatpak-bwrap-private.h" #include "flatpak-utils-private.h" +#include "flatpak-utils-base-private.h" #include "glib-backports.h" @@ -322,13 +328,15 @@ flatpak_bwrap_bundle_args (FlatpakBwrap *bwrap, return TRUE; } -/* Unset FD_CLOEXEC on the array of fds passed in @user_data */ void -flatpak_bwrap_child_setup_cb (gpointer user_data) +flatpak_bwrap_child_setup (GArray *fd_array, + gboolean close_fd_workaround) { - GArray *fd_array = user_data; int i; + if (close_fd_workaround) + flatpak_close_fds_workaround (3); + /* If no fd_array was specified, don't care. */ if (fd_array == NULL) return; @@ -348,3 +356,12 @@ flatpak_bwrap_child_setup_cb (gpointer user_data) fcntl (fd, F_SETFD, 0); } } + +/* Unset FD_CLOEXEC on the array of fds passed in @user_data */ +void +flatpak_bwrap_child_setup_cb (gpointer user_data) +{ + GArray *fd_array = user_data; + + flatpak_bwrap_child_setup (fd_array, TRUE); +} diff --git a/src/flatpak-run-private.h b/src/flatpak-run-private.h index 78b5e372efd52fbf838e156bc4fa7894fc141078..24f1a8fd77b06fed406050c2cba0c7bb7dc44868 100644 --- a/src/flatpak-run-private.h +++ b/src/flatpak-run-private.h @@ -1,33 +1,39 @@ /* * Cut-down version of common/flatpak-run-private.h from Flatpak + * Last updated: Flatpak 1.6.1 * * Copyright © 2017-2019 Collabora Ltd. * Copyright © 2014-2019 Red Hat, Inc * * SPDX-License-Identifier: LGPL-2.1-or-later * - * This library is free software; you can redistribute it and/or + * 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 + * 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/>. + * 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 <alexl@redhat.com> */ -#pragma once +#ifndef __FLATPAK_RUN_H__ +#define __FLATPAK_RUN_H__ #include <glib.h> #include <glib-object.h> -#include "libglnx.h" +#include "libglnx/libglnx.h" #include "flatpak-bwrap-private.h" +#include "flatpak-utils-private.h" #include "glib-backports.h" void flatpak_run_add_x11_args (FlatpakBwrap *bwrap, @@ -36,3 +42,4 @@ gboolean flatpak_run_add_wayland_args (FlatpakBwrap *bwrap); void flatpak_run_add_pulseaudio_args (FlatpakBwrap *bwrap); gboolean flatpak_run_add_system_dbus_args (FlatpakBwrap *app_bwrap); gboolean flatpak_run_add_session_dbus_args (FlatpakBwrap *app_bwrap); +#endif /* __FLATPAK_RUN_H__ */ diff --git a/src/flatpak-run.c b/src/flatpak-run.c index 20c4fbcd997d5e4c8d31ca19efd349c0a4e46776..94a20e25ee64917ddfe8323f8fd7086aff9db868 100644 --- a/src/flatpak-run.c +++ b/src/flatpak-run.c @@ -1,32 +1,40 @@ /* * Cut-down version of common/flatpak-run.c from Flatpak + * Last updated: Flatpak 1.6.1 * * Copyright © 2017-2019 Collabora Ltd. * Copyright © 2014-2019 Red Hat, Inc * * SPDX-License-Identifier: LGPL-2.1-or-later * - * This library is free software; you can redistribute it and/or + * 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 + * 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/>. + * 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 <alexl@redhat.com> */ +#include "config.h" + #include "flatpak-run-private.h" #include <sys/utsname.h> #include <X11/Xauth.h> #include <gio/gio.h> +#include "libglnx/libglnx.h" +#include "flatpak-utils-base-private.h" #include "flatpak-utils-private.h" /* In Flatpak this is optional, in pressure-vessel not so much */ @@ -125,7 +133,7 @@ write_xauth (char *number, FILE *output) fclose (f); } -#endif +#endif /* ENABLE_XAUTH */ void flatpak_run_add_x11_args (FlatpakBwrap *bwrap, @@ -171,7 +179,7 @@ flatpak_run_add_x11_args (FlatpakBwrap *bwrap, #ifdef ENABLE_XAUTH g_auto(GLnxTmpfile) xauth_tmpf = { 0, }; - if (glnx_open_anonymous_tmpfile (O_RDWR | O_CLOEXEC, &xauth_tmpf, NULL)) + if (glnx_open_anonymous_tmpfile_full (O_RDWR | O_CLOEXEC, "/tmp", &xauth_tmpf, NULL)) { FILE *output = fdopen (xauth_tmpf.fd, "wb"); if (output != NULL) @@ -417,12 +425,9 @@ gboolean flatpak_run_add_session_dbus_args (FlatpakBwrap *app_bwrap) { const char *dbus_address = g_getenv ("DBUS_SESSION_BUS_ADDRESS"); - char *dbus_session_socket = NULL; + g_autofree char *dbus_session_socket = NULL; g_autofree char *sandbox_socket_path = g_strdup_printf ("/run/user/%d/bus", getuid ()); g_autofree char *sandbox_dbus_address = g_strdup_printf ("unix:path=/run/user/%d/bus", getuid ()); - g_autofree char *user_runtime_dir = flatpak_get_real_xdg_runtime_dir (); - - /* FIXME: upstream the use of user_runtime_dir to Flatpak */ if (dbus_address != NULL) { @@ -430,6 +435,7 @@ flatpak_run_add_session_dbus_args (FlatpakBwrap *app_bwrap) } else { + g_autofree char *user_runtime_dir = flatpak_get_real_xdg_runtime_dir (); struct stat statbuf; dbus_session_socket = g_build_filename (user_runtime_dir, "bus", NULL); diff --git a/src/flatpak-utils-base-private.h b/src/flatpak-utils-base-private.h new file mode 100644 index 0000000000000000000000000000000000000000..751686b7b13924bfc90f537e797de5ccdae240e4 --- /dev/null +++ b/src/flatpak-utils-base-private.h @@ -0,0 +1,36 @@ +/* + * Taken from Flatpak, last updated: Flatpak 1.6.1 + * Copyright © 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 <alexl@redhat.com> + */ + +#ifndef __FLATPAK_UTILS_BASE_H__ +#define __FLATPAK_UTILS_BASE_H__ + +#include <glib.h> + +char *flatpak_get_timezone (void); + +char * flatpak_readlink (const char *path, + GError **error); +char * flatpak_resolve_link (const char *path, + GError **error); +char * flatpak_canonicalize_filename (const char *path); +void flatpak_close_fds_workaround (int start_fd); + +#endif /* __FLATPAK_UTILS_BASE_H__ */ diff --git a/src/flatpak-utils-base.c b/src/flatpak-utils-base.c new file mode 100644 index 0000000000000000000000000000000000000000..23ec448b72e3ee390613a233064f5eb6c681d70e --- /dev/null +++ b/src/flatpak-utils-base.c @@ -0,0 +1,117 @@ +/* + * Taken from Flatpak, last updated: Flatpak 1.6.1 + * Copyright © 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 <alexl@redhat.com> + */ + +#include "config.h" + +#include "flatpak-utils-base-private.h" + +#include <stdlib.h> +#include <string.h> + +#include <gio/gio.h> +#include "libglnx/libglnx.h" + +char * +flatpak_get_timezone (void) +{ + g_autofree gchar *symlink = NULL; + gchar *etc_timezone = NULL; + const gchar *tzdir; + + tzdir = getenv ("TZDIR"); + if (tzdir == NULL) + tzdir = "/usr/share/zoneinfo"; + + symlink = flatpak_resolve_link ("/etc/localtime", NULL); + if (symlink != NULL) + { + /* Resolve relative path */ + g_autofree gchar *canonical = flatpak_canonicalize_filename (symlink); + char *canonical_suffix; + + /* Strip the prefix and slashes if possible. */ + if (g_str_has_prefix (canonical, tzdir)) + { + canonical_suffix = canonical + strlen (tzdir); + while (*canonical_suffix == '/') + canonical_suffix++; + + return g_strdup (canonical_suffix); + } + } + + if (g_file_get_contents ("/etc/timezone", &etc_timezone, + NULL, NULL)) + { + g_strchomp (etc_timezone); + return etc_timezone; + } + + /* Final fall-back is UTC */ + return g_strdup ("UTC"); +} + +char * +flatpak_readlink (const char *path, + GError **error) +{ + return glnx_readlinkat_malloc (-1, path, NULL, error); +} + +char * +flatpak_resolve_link (const char *path, + GError **error) +{ + g_autofree char *link = flatpak_readlink (path, error); + g_autofree char *dirname = NULL; + + if (link == NULL) + return NULL; + + if (g_path_is_absolute (link)) + return g_steal_pointer (&link); + + dirname = g_path_get_dirname (path); + return g_build_filename (dirname, link, NULL); +} + +char * +flatpak_canonicalize_filename (const char *path) +{ + g_autoptr(GFile) file = g_file_new_for_path (path); + return g_file_get_path (file); +} + +/* There is a dead-lock in glib versions before 2.60 when it closes + * the fds. See: https://gitlab.gnome.org/GNOME/glib/merge_requests/490 + * This was hitting the test-suite a lot, so we work around it by using + * the G_SPAWN_LEAVE_DESCRIPTORS_OPEN/G_SUBPROCESS_FLAGS_INHERIT_FDS flag + * and setting CLOEXEC ourselves. + */ +void +flatpak_close_fds_workaround (int start_fd) +{ + int max_open_fds = sysconf (_SC_OPEN_MAX); + int fd; + + for (fd = start_fd; fd < max_open_fds; fd++) + fcntl (fd, F_SETFD, FD_CLOEXEC); +} diff --git a/src/flatpak-utils-private.h b/src/flatpak-utils-private.h index 736cca1a7248d0cb360797f06c97ebdbfacd7b40..612a71e357ed3a0c0b712e34894edeb9c7658324 100644 --- a/src/flatpak-utils-private.h +++ b/src/flatpak-utils-private.h @@ -23,7 +23,7 @@ #ifndef __FLATPAK_UTILS_H__ #define __FLATPAK_UTILS_H__ -#include "libglnx.h" +#include "libglnx/libglnx.h" /* https://github.com/GNOME/libglnx/pull/38 * Note by using #define rather than wrapping via a static inline, we @@ -37,17 +37,10 @@ * it as a path into the sandbox */ char * flatpak_get_real_xdg_runtime_dir (void); -char *flatpak_get_timezone (void); char * flatpak_quote_argv (const char *argv[], gssize len); -char * flatpak_readlink (const char *path, - GError **error); -char * flatpak_resolve_link (const char *path, - GError **error); -char * flatpak_canonicalize_filename (const char *path); - gboolean flatpak_buffer_to_sealed_memfd_or_tmpfile (GLnxTmpfile *tmpf, const char *name, const char *str, diff --git a/src/flatpak-utils.c b/src/flatpak-utils.c index 04112cdac1074a85614f1a5f6ef2a70078d83d9e..d796878008461472273a2f325a7540905fd6fb3c 100644 --- a/src/flatpak-utils.c +++ b/src/flatpak-utils.c @@ -1,7 +1,8 @@ /* * A cut-down version of common/flatpak-utils from Flatpak + * Last updated: Flatpak 1.6.1 * - * Copyright © 2014 Red Hat, Inc + * 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 @@ -20,7 +21,7 @@ * Alexander Larsson <alexl@redhat.com> */ -#include "flatpak-utils-private.h" +#include "config.h" #include <string.h> #include <stdlib.h> @@ -37,6 +38,10 @@ #include <sys/ioctl.h> #include <termios.h> +#include <glib.h> +#include "flatpak-utils-base-private.h" +#include "flatpak-utils-private.h" + /* To keep this more similar to the original file, we explicitly disable * this warning rather than fixing it */ #pragma GCC diagnostic ignored "-Wshadow" @@ -51,46 +56,6 @@ flatpak_get_real_xdg_runtime_dir (void) return realpath (g_get_user_runtime_dir (), NULL); } -char * -flatpak_get_timezone (void) -{ - g_autofree gchar *symlink = NULL; - gchar *etc_timezone = NULL; - const gchar *tzdir; - - tzdir = getenv ("TZDIR"); - if (tzdir == NULL) - tzdir = "/usr/share/zoneinfo"; - - symlink = flatpak_resolve_link ("/etc/localtime", NULL); - if (symlink != NULL) - { - /* Resolve relative path */ - g_autofree gchar *canonical = flatpak_canonicalize_filename (symlink); - char *canonical_suffix; - - /* Strip the prefix and slashes if possible. */ - if (g_str_has_prefix (canonical, tzdir)) - { - canonical_suffix = canonical + strlen (tzdir); - while (*canonical_suffix == '/') - canonical_suffix++; - - return g_strdup (canonical_suffix); - } - } - - if (g_file_get_contents ("/etc/timezeone", &etc_timezone, - NULL, NULL)) - { - g_strchomp (etc_timezone); - return etc_timezone; - } - - /* Final fall-back is UTC */ - return g_strdup ("UTC"); -} - static gboolean needs_quoting (const char *arg) { @@ -134,37 +99,6 @@ flatpak_quote_argv (const char *argv[], return g_string_free (res, FALSE); } -char * -flatpak_readlink (const char *path, - GError **error) -{ - return glnx_readlinkat_malloc (-1, path, NULL, error); -} - -char * -flatpak_resolve_link (const char *path, - GError **error) -{ - g_autofree char *link = flatpak_readlink (path, error); - g_autofree char *dirname = NULL; - - if (link == NULL) - return NULL; - - if (g_path_is_absolute (link)) - return g_steal_pointer (&link); - - dirname = g_path_get_dirname (path); - return g_build_filename (dirname, link, NULL); -} - -char * -flatpak_canonicalize_filename (const char *path) -{ - g_autoptr(GFile) file = g_file_new_for_path (path); - return g_file_get_path (file); -} - /* If memfd_create() is available, generate a sealed memfd with contents of * @str. Otherwise use an O_TMPFILE @tmpf in anonymous mode, write @str to * @tmpf, and lseek() back to the start. See also similar uses in e.g. diff --git a/src/utils.c b/src/utils.c index 1076a78ecbf001d3732a7f59be449f477815808f..40f171ea8e5e6a6a3b944886eb5e19210a1c28f8 100644 --- a/src/utils.c +++ b/src/utils.c @@ -26,10 +26,11 @@ #include <glib/gstdio.h> #include <gio/gio.h> -#include "libglnx.h" +#include "libglnx/libglnx.h" #include "glib-backports.h" #include "flatpak-bwrap-private.h" +#include "flatpak-utils-base-private.h" #include "flatpak-utils-private.h" /** diff --git a/src/with-lock.c b/src/with-lock.c index 0d4d2cbce89b284ffa8edf093247a60d74288d0d..cf185dc7de556b422d2bf0c151ee7c0d93ec3f49 100644 --- a/src/with-lock.c +++ b/src/with-lock.c @@ -35,7 +35,7 @@ #include <gio/gio.h> #include "glib-backports.h" -#include "libglnx.h" +#include "libglnx/libglnx.h" #include "bwrap-lock.h" #include "utils.h" diff --git a/src/wrap-interactive.h b/src/wrap-interactive.h index ad01ce1fd5b9bcacf31f734422ebc06d5dc71f34..2170f590257c350fe0f565d10089fcefa343d437 100644 --- a/src/wrap-interactive.h +++ b/src/wrap-interactive.h @@ -22,7 +22,7 @@ #include <glib.h> #include <glib-object.h> -#include "libglnx.h" +#include "libglnx/libglnx.h" #include "flatpak-bwrap-private.h" #include "glib-backports.h" diff --git a/src/wrap.c b/src/wrap.c index 3bfed3672ba38759b2627209a059db37c56aca76..c1f727a38af77e2f3f1202c60d1f828f72422218 100644 --- a/src/wrap.c +++ b/src/wrap.c @@ -33,7 +33,7 @@ /* Include these before steam-runtime-tools.h so that their backport of * G_DEFINE_AUTOPTR_CLEANUP_FUNC will be visible to it */ #include "glib-backports.h" -#include "libglnx.h" +#include "libglnx/libglnx.h" #include <steam-runtime-tools/steam-runtime-tools.h> @@ -41,6 +41,7 @@ #include "bwrap-lock.h" #include "flatpak-bwrap-private.h" #include "flatpak-run-private.h" +#include "flatpak-utils-base-private.h" #include "flatpak-utils-private.h" #include "utils.h" #include "wrap-interactive.h"