2016-05-16 15:35:01 +02:00
|
|
|
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
|
|
|
|
|
/* dbus-spawn-test.c
|
|
|
|
|
*
|
|
|
|
|
* Copyright (C) 2002, 2003, 2004 Red Hat, Inc.
|
|
|
|
|
* Copyright (C) 2003 CodeFactory AB
|
|
|
|
|
* Copyright (C) 2005 Novell, Inc.
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Academic Free License version 2.1
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program 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 General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
2018-09-28 15:53:40 +01:00
|
|
|
#include "dbus/dbus-internals.h"
|
|
|
|
|
#include "dbus/dbus-spawn.h"
|
|
|
|
|
#include "dbus/dbus-sysdeps.h"
|
|
|
|
|
#include "dbus/dbus-test.h"
|
2018-11-15 14:08:03 +00:00
|
|
|
#include "test/test-utils.h"
|
2016-05-16 15:35:01 +02:00
|
|
|
|
|
|
|
|
static char *
|
|
|
|
|
get_test_exec (const char *exe,
|
|
|
|
|
DBusString *scratch_space)
|
|
|
|
|
{
|
|
|
|
|
const char *dbus_test_exec;
|
|
|
|
|
|
|
|
|
|
dbus_test_exec = _dbus_getenv ("DBUS_TEST_EXEC");
|
|
|
|
|
|
|
|
|
|
if (dbus_test_exec == NULL)
|
2017-04-15 21:06:23 +01:00
|
|
|
return NULL;
|
2016-05-16 15:35:01 +02:00
|
|
|
|
|
|
|
|
if (!_dbus_string_init (scratch_space))
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
if (!_dbus_string_append_printf (scratch_space, "%s/%s%s",
|
|
|
|
|
dbus_test_exec, exe, DBUS_EXEEXT))
|
|
|
|
|
{
|
|
|
|
|
_dbus_string_free (scratch_space);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return _dbus_string_get_data (scratch_space);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static dbus_bool_t
|
2017-11-14 16:06:05 +00:00
|
|
|
check_spawn_nonexistent (void *data,
|
|
|
|
|
dbus_bool_t have_memory)
|
2016-05-16 15:35:01 +02:00
|
|
|
{
|
Be more const-correct
As a general design principle, strings that we aren't going to modify
should usually be const. When compiling with -Wwrite-strings, quoted
string constants are of type "const char *", causing compiler warnings
when they are assigned to char * variables.
Unfortunately, we need to add casts in a few places:
* _dbus_list_append(), _dbus_test_oom_handling() and similar generic
"user-data" APIs take a void *, not a const void *, so we have
to cast
* For historical reasons the execve() family of functions take a
(char * const *), i.e. a constant pointer to an array of mutable
strings, so again we have to cast
* _dbus_spawn_async_with_babysitter similarly takes a char **,
although we can make it a little more const-correct by making it
take (char * const *) like execve() does
This also incorporates a subsequent patch by Thomas Zimmermann to
put various string constants in static storage, which is a little
more efficient.
Signed-off-by: Simon McVittie <smcv@debian.org>
Reviewed-by: Thomas Zimmermann <tdz@users.sourceforge.net>
Bug: https://bugs.freedesktop.org/show_bug.cgi?id=97357
2016-10-07 17:41:01 +01:00
|
|
|
static const char arg_does_not_exist[] = "/this/does/not/exist/32542sdgafgafdg";
|
|
|
|
|
|
|
|
|
|
const char *argv[4] = { NULL, NULL, NULL, NULL };
|
2016-05-16 15:35:01 +02:00
|
|
|
DBusBabysitter *sitter = NULL;
|
|
|
|
|
DBusError error = DBUS_ERROR_INIT;
|
|
|
|
|
|
|
|
|
|
/*** Test launching nonexistent binary */
|
|
|
|
|
|
Be more const-correct
As a general design principle, strings that we aren't going to modify
should usually be const. When compiling with -Wwrite-strings, quoted
string constants are of type "const char *", causing compiler warnings
when they are assigned to char * variables.
Unfortunately, we need to add casts in a few places:
* _dbus_list_append(), _dbus_test_oom_handling() and similar generic
"user-data" APIs take a void *, not a const void *, so we have
to cast
* For historical reasons the execve() family of functions take a
(char * const *), i.e. a constant pointer to an array of mutable
strings, so again we have to cast
* _dbus_spawn_async_with_babysitter similarly takes a char **,
although we can make it a little more const-correct by making it
take (char * const *) like execve() does
This also incorporates a subsequent patch by Thomas Zimmermann to
put various string constants in static storage, which is a little
more efficient.
Signed-off-by: Simon McVittie <smcv@debian.org>
Reviewed-by: Thomas Zimmermann <tdz@users.sourceforge.net>
Bug: https://bugs.freedesktop.org/show_bug.cgi?id=97357
2016-10-07 17:41:01 +01:00
|
|
|
argv[0] = arg_does_not_exist;
|
|
|
|
|
if (_dbus_spawn_async_with_babysitter (&sitter, "spawn_nonexistent",
|
|
|
|
|
(char * const *) argv,
|
2016-07-21 10:21:35 +01:00
|
|
|
NULL, DBUS_SPAWN_NONE, NULL, NULL,
|
2016-05-16 15:35:01 +02:00
|
|
|
&error))
|
|
|
|
|
{
|
|
|
|
|
_dbus_babysitter_block_for_child_exit (sitter);
|
|
|
|
|
_dbus_babysitter_set_child_exit_error (sitter, &error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (sitter)
|
|
|
|
|
_dbus_babysitter_unref (sitter);
|
|
|
|
|
|
|
|
|
|
if (!dbus_error_is_set (&error))
|
|
|
|
|
{
|
2016-07-21 11:01:01 +01:00
|
|
|
_dbus_warn ("Did not get an error launching nonexistent executable");
|
2016-05-16 15:35:01 +02:00
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!(dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY) ||
|
|
|
|
|
dbus_error_has_name (&error, DBUS_ERROR_SPAWN_EXEC_FAILED)))
|
|
|
|
|
{
|
2016-07-21 11:01:01 +01:00
|
|
|
_dbus_warn ("Not expecting error when launching nonexistent executable: %s: %s",
|
2016-05-16 15:35:01 +02:00
|
|
|
error.name, error.message);
|
|
|
|
|
dbus_error_free (&error);
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dbus_error_free (&error);
|
|
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static dbus_bool_t
|
2017-11-14 16:06:05 +00:00
|
|
|
check_spawn_segfault (void *data,
|
|
|
|
|
dbus_bool_t have_memory)
|
2016-05-16 15:35:01 +02:00
|
|
|
{
|
|
|
|
|
char *argv[4] = { NULL, NULL, NULL, NULL };
|
|
|
|
|
DBusBabysitter *sitter = NULL;
|
|
|
|
|
DBusError error = DBUS_ERROR_INIT;
|
|
|
|
|
DBusString argv0;
|
|
|
|
|
|
|
|
|
|
/*** Test launching segfault binary */
|
|
|
|
|
|
|
|
|
|
argv[0] = get_test_exec ("test-segfault", &argv0);
|
|
|
|
|
|
|
|
|
|
if (argv[0] == NULL)
|
|
|
|
|
{
|
2017-04-15 21:06:23 +01:00
|
|
|
/* OOM was simulated or DBUS_TEST_EXEC was unset; either is OK */
|
2016-05-16 15:35:01 +02:00
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (_dbus_spawn_async_with_babysitter (&sitter, "spawn_segfault", argv,
|
2016-07-21 10:21:35 +01:00
|
|
|
NULL, DBUS_SPAWN_NONE, NULL, NULL,
|
2016-05-16 15:35:01 +02:00
|
|
|
&error))
|
|
|
|
|
{
|
|
|
|
|
_dbus_babysitter_block_for_child_exit (sitter);
|
|
|
|
|
_dbus_babysitter_set_child_exit_error (sitter, &error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_dbus_string_free (&argv0);
|
|
|
|
|
|
|
|
|
|
if (sitter)
|
|
|
|
|
_dbus_babysitter_unref (sitter);
|
|
|
|
|
|
|
|
|
|
if (!dbus_error_is_set (&error))
|
|
|
|
|
{
|
2016-07-21 11:01:01 +01:00
|
|
|
_dbus_warn ("Did not get an error launching segfaulting binary");
|
2016-05-16 15:35:01 +02:00
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!(dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY) ||
|
|
|
|
|
#ifdef DBUS_WIN
|
|
|
|
|
dbus_error_has_name (&error, DBUS_ERROR_SPAWN_CHILD_EXITED)))
|
|
|
|
|
#else
|
|
|
|
|
dbus_error_has_name (&error, DBUS_ERROR_SPAWN_CHILD_SIGNALED)))
|
|
|
|
|
#endif
|
|
|
|
|
{
|
2016-07-21 11:01:01 +01:00
|
|
|
_dbus_warn ("Not expecting error when launching segfaulting executable: %s: %s",
|
2016-05-16 15:35:01 +02:00
|
|
|
error.name, error.message);
|
|
|
|
|
dbus_error_free (&error);
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dbus_error_free (&error);
|
|
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static dbus_bool_t
|
2017-11-14 16:06:05 +00:00
|
|
|
check_spawn_exit (void *data,
|
|
|
|
|
dbus_bool_t have_memory)
|
2016-05-16 15:35:01 +02:00
|
|
|
{
|
|
|
|
|
char *argv[4] = { NULL, NULL, NULL, NULL };
|
|
|
|
|
DBusBabysitter *sitter = NULL;
|
|
|
|
|
DBusError error = DBUS_ERROR_INIT;
|
|
|
|
|
DBusString argv0;
|
|
|
|
|
|
|
|
|
|
/*** Test launching exit failure binary */
|
|
|
|
|
|
|
|
|
|
argv[0] = get_test_exec ("test-exit", &argv0);
|
|
|
|
|
|
|
|
|
|
if (argv[0] == NULL)
|
|
|
|
|
{
|
2017-04-15 21:06:23 +01:00
|
|
|
/* OOM was simulated or DBUS_TEST_EXEC was unset; either is OK */
|
2016-05-16 15:35:01 +02:00
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (_dbus_spawn_async_with_babysitter (&sitter, "spawn_exit", argv,
|
2016-07-21 10:21:35 +01:00
|
|
|
NULL, DBUS_SPAWN_NONE, NULL, NULL,
|
2016-05-16 15:35:01 +02:00
|
|
|
&error))
|
|
|
|
|
{
|
|
|
|
|
_dbus_babysitter_block_for_child_exit (sitter);
|
|
|
|
|
_dbus_babysitter_set_child_exit_error (sitter, &error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_dbus_string_free (&argv0);
|
|
|
|
|
|
|
|
|
|
if (sitter)
|
|
|
|
|
_dbus_babysitter_unref (sitter);
|
|
|
|
|
|
|
|
|
|
if (!dbus_error_is_set (&error))
|
|
|
|
|
{
|
2016-07-21 11:01:01 +01:00
|
|
|
_dbus_warn ("Did not get an error launching binary that exited with failure code");
|
2016-05-16 15:35:01 +02:00
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!(dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY) ||
|
|
|
|
|
dbus_error_has_name (&error, DBUS_ERROR_SPAWN_CHILD_EXITED)))
|
|
|
|
|
{
|
2016-07-21 11:01:01 +01:00
|
|
|
_dbus_warn ("Not expecting error when launching exiting executable: %s: %s",
|
2016-05-16 15:35:01 +02:00
|
|
|
error.name, error.message);
|
|
|
|
|
dbus_error_free (&error);
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dbus_error_free (&error);
|
|
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static dbus_bool_t
|
2017-11-14 16:06:05 +00:00
|
|
|
check_spawn_and_kill (void *data,
|
|
|
|
|
dbus_bool_t have_memory)
|
2016-05-16 15:35:01 +02:00
|
|
|
{
|
|
|
|
|
char *argv[4] = { NULL, NULL, NULL, NULL };
|
|
|
|
|
DBusBabysitter *sitter = NULL;
|
|
|
|
|
DBusError error = DBUS_ERROR_INIT;
|
|
|
|
|
DBusString argv0;
|
|
|
|
|
|
|
|
|
|
/*** Test launching sleeping binary then killing it */
|
|
|
|
|
|
|
|
|
|
argv[0] = get_test_exec ("test-sleep-forever", &argv0);
|
|
|
|
|
|
|
|
|
|
if (argv[0] == NULL)
|
|
|
|
|
{
|
2017-04-15 21:06:23 +01:00
|
|
|
/* OOM was simulated or DBUS_TEST_EXEC was unset; either is OK */
|
2016-05-16 15:35:01 +02:00
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (_dbus_spawn_async_with_babysitter (&sitter, "spawn_and_kill", argv,
|
2016-07-21 10:21:35 +01:00
|
|
|
NULL, DBUS_SPAWN_NONE, NULL, NULL,
|
2016-05-16 15:35:01 +02:00
|
|
|
&error))
|
|
|
|
|
{
|
|
|
|
|
_dbus_babysitter_kill_child (sitter);
|
|
|
|
|
|
|
|
|
|
_dbus_babysitter_block_for_child_exit (sitter);
|
|
|
|
|
|
|
|
|
|
_dbus_babysitter_set_child_exit_error (sitter, &error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_dbus_string_free (&argv0);
|
|
|
|
|
|
|
|
|
|
if (sitter)
|
|
|
|
|
_dbus_babysitter_unref (sitter);
|
|
|
|
|
|
|
|
|
|
if (!dbus_error_is_set (&error))
|
|
|
|
|
{
|
2016-07-21 11:01:01 +01:00
|
|
|
_dbus_warn ("Did not get an error after killing spawned binary");
|
2016-05-16 15:35:01 +02:00
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!(dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY) ||
|
|
|
|
|
#ifdef DBUS_WIN
|
|
|
|
|
dbus_error_has_name (&error, DBUS_ERROR_SPAWN_CHILD_EXITED)))
|
|
|
|
|
#else
|
|
|
|
|
dbus_error_has_name (&error, DBUS_ERROR_SPAWN_CHILD_SIGNALED)))
|
|
|
|
|
#endif
|
|
|
|
|
{
|
2016-07-21 11:01:01 +01:00
|
|
|
_dbus_warn ("Not expecting error when killing executable: %s: %s",
|
2016-05-16 15:35:01 +02:00
|
|
|
error.name, error.message);
|
|
|
|
|
dbus_error_free (&error);
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dbus_error_free (&error);
|
|
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-28 15:53:40 +01:00
|
|
|
static dbus_bool_t
|
2016-05-16 15:35:01 +02:00
|
|
|
_dbus_spawn_test (const char *test_data_dir)
|
|
|
|
|
{
|
|
|
|
|
if (!_dbus_test_oom_handling ("spawn_nonexistent",
|
|
|
|
|
check_spawn_nonexistent,
|
|
|
|
|
NULL))
|
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
|
|
if (!_dbus_test_oom_handling ("spawn_segfault",
|
|
|
|
|
check_spawn_segfault,
|
|
|
|
|
NULL))
|
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
|
|
if (!_dbus_test_oom_handling ("spawn_exit",
|
|
|
|
|
check_spawn_exit,
|
|
|
|
|
NULL))
|
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
|
|
if (!_dbus_test_oom_handling ("spawn_and_kill",
|
|
|
|
|
check_spawn_and_kill,
|
|
|
|
|
NULL))
|
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
2018-09-28 15:53:40 +01:00
|
|
|
|
|
|
|
|
static DBusTestCase test = { "spawn", _dbus_spawn_test };
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
main (int argc,
|
|
|
|
|
char **argv)
|
|
|
|
|
{
|
|
|
|
|
return _dbus_test_main (argc, argv, 1, &test,
|
|
|
|
|
DBUS_TEST_FLAGS_CHECK_MEMORY_LEAKS,
|
|
|
|
|
NULL, NULL);
|
|
|
|
|
}
|