Skip to content

Commit

Permalink
run: Avoid execve() when measuring test coverage
Browse files Browse the repository at this point in the history
Normally, we want to save a process and get better signal handling
by replacing the `flatpak run` process with bubblewrap.

However, when we're doing profiling or measuring coverage, we want to
exit cleanly so that profiling data can be recorded, which is done in
an atexit() hook. In this situation, bypass the execve() optimization;
instead, start bubblewrap in the background, immediately wait for it,
and propagate its exit status.

Signed-off-by: Simon McVittie <[email protected]>
  • Loading branch information
smcv authored and alexlarsson committed Jan 17, 2022
1 parent d567a1a commit 88a928e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ AM_DISTCHECK_CONFIGURE_FLAGS = \

coverage:
$(AM_V_GEN) $(MAKE) $(AM_MAKEFLAGS) lcov-clean
$(AM_V_GEN) $(MAKE) check
$(AM_V_GEN) FLATPAK_TEST_COVERAGE=1 $(MAKE) check
$(AM_V_GEN) $(MAKE) $(AM_MAKEFLAGS) genlcov

lcov-clean:
Expand Down
23 changes: 21 additions & 2 deletions common/flatpak-run.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/vfs.h>
#include <sys/wait.h>
#include <sys/personality.h>
#include <grp.h>
#include <unistd.h>
Expand Down Expand Up @@ -4489,15 +4490,17 @@ flatpak_run_app (FlatpakDecomposed *app_ref,
commandline = flatpak_quote_argv ((const char **) bwrap->argv->pdata, -1);
g_debug ("Running '%s'", commandline);

if ((flags & FLATPAK_RUN_FLAG_BACKGROUND) != 0)
if ((flags & (FLATPAK_RUN_FLAG_BACKGROUND)) != 0 ||
g_getenv ("FLATPAK_TEST_COVERAGE") != NULL)
{
GPid child_pid;
char pid_str[64];
g_autofree char *pid_path = NULL;
GSpawnFlags spawn_flags;

spawn_flags = G_SPAWN_SEARCH_PATH;
if (flags & FLATPAK_RUN_FLAG_DO_NOT_REAP)
if (flags & FLATPAK_RUN_FLAG_DO_NOT_REAP ||
(flags & FLATPAK_RUN_FLAG_BACKGROUND) == 0)
spawn_flags |= G_SPAWN_DO_NOT_REAP_CHILD;

/* We use LEAVE_DESCRIPTORS_OPEN to work around dead-lock, see flatpak_close_fds_workaround */
Expand All @@ -4521,6 +4524,22 @@ flatpak_run_app (FlatpakDecomposed *app_ref,
g_snprintf (pid_str, sizeof (pid_str), "%d", child_pid);
pid_path = g_build_filename (instance_id_host_dir, "pid", NULL);
g_file_set_contents (pid_path, pid_str, -1, NULL);

if ((flags & (FLATPAK_RUN_FLAG_BACKGROUND)) == 0)
{
int wait_status;

if (waitpid (child_pid, &wait_status, 0) != child_pid)
return glnx_throw_errno_prefix (error, "Failed to wait for child process");

if (WIFEXITED (wait_status))
exit (WEXITSTATUS (wait_status));

if (WIFSIGNALED (wait_status))
exit (128 + WTERMSIG (wait_status));

return glnx_throw (error, "Unknown wait status from waitpid(): %d", wait_status);
}
}
else
{
Expand Down

0 comments on commit 88a928e

Please sign in to comment.