1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
4 * Copyright (C) 2002, 2003, 2004 Red Hat, Inc.
5 * Copyright (C) 2003 CodeFactory AB
6 * Copyright (C) 2005 Novell, Inc.
8 * Licensed under the Academic Free License version 2.1
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
28 #include "dbus-spawn.h"
29 #include "dbus-sysdeps.h"
30 #include "dbus-test.h"
33 get_test_exec (const char *exe,
34 DBusString *scratch_space)
36 const char *dbus_test_exec;
38 dbus_test_exec = _dbus_getenv ("DBUS_TEST_EXEC");
40 if (dbus_test_exec == NULL)
43 if (!_dbus_string_init (scratch_space))
46 if (!_dbus_string_append_printf (scratch_space, "%s/%s%s",
47 dbus_test_exec, exe, DBUS_EXEEXT))
49 _dbus_string_free (scratch_space);
53 return _dbus_string_get_data (scratch_space);
57 check_spawn_nonexistent (void *data)
59 static const char arg_does_not_exist[] = "/this/does/not/exist/32542sdgafgafdg";
61 const char *argv[4] = { NULL, NULL, NULL, NULL };
62 DBusBabysitter *sitter = NULL;
63 DBusError error = DBUS_ERROR_INIT;
65 /*** Test launching nonexistent binary */
67 argv[0] = arg_does_not_exist;
68 if (_dbus_spawn_async_with_babysitter (&sitter, "spawn_nonexistent",
69 (char * const *) argv,
70 NULL, DBUS_SPAWN_NONE, NULL, NULL,
73 _dbus_babysitter_block_for_child_exit (sitter);
74 _dbus_babysitter_set_child_exit_error (sitter, &error);
78 _dbus_babysitter_unref (sitter);
80 if (!dbus_error_is_set (&error))
82 _dbus_warn ("Did not get an error launching nonexistent executable");
86 if (!(dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY) ||
87 dbus_error_has_name (&error, DBUS_ERROR_SPAWN_EXEC_FAILED)))
89 _dbus_warn ("Not expecting error when launching nonexistent executable: %s: %s",
90 error.name, error.message);
91 dbus_error_free (&error);
95 dbus_error_free (&error);
101 check_spawn_segfault (void *data)
103 char *argv[4] = { NULL, NULL, NULL, NULL };
104 DBusBabysitter *sitter = NULL;
105 DBusError error = DBUS_ERROR_INIT;
108 /*** Test launching segfault binary */
110 argv[0] = get_test_exec ("test-segfault", &argv0);
114 /* OOM was simulated or DBUS_TEST_EXEC was unset; either is OK */
118 if (_dbus_spawn_async_with_babysitter (&sitter, "spawn_segfault", argv,
119 NULL, DBUS_SPAWN_NONE, NULL, NULL,
122 _dbus_babysitter_block_for_child_exit (sitter);
123 _dbus_babysitter_set_child_exit_error (sitter, &error);
126 _dbus_string_free (&argv0);
129 _dbus_babysitter_unref (sitter);
131 if (!dbus_error_is_set (&error))
133 _dbus_warn ("Did not get an error launching segfaulting binary");
137 if (!(dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY) ||
139 dbus_error_has_name (&error, DBUS_ERROR_SPAWN_CHILD_EXITED)))
141 dbus_error_has_name (&error, DBUS_ERROR_SPAWN_CHILD_SIGNALED)))
144 _dbus_warn ("Not expecting error when launching segfaulting executable: %s: %s",
145 error.name, error.message);
146 dbus_error_free (&error);
150 dbus_error_free (&error);
156 check_spawn_exit (void *data)
158 char *argv[4] = { NULL, NULL, NULL, NULL };
159 DBusBabysitter *sitter = NULL;
160 DBusError error = DBUS_ERROR_INIT;
163 /*** Test launching exit failure binary */
165 argv[0] = get_test_exec ("test-exit", &argv0);
169 /* OOM was simulated or DBUS_TEST_EXEC was unset; either is OK */
173 if (_dbus_spawn_async_with_babysitter (&sitter, "spawn_exit", argv,
174 NULL, DBUS_SPAWN_NONE, NULL, NULL,
177 _dbus_babysitter_block_for_child_exit (sitter);
178 _dbus_babysitter_set_child_exit_error (sitter, &error);
181 _dbus_string_free (&argv0);
184 _dbus_babysitter_unref (sitter);
186 if (!dbus_error_is_set (&error))
188 _dbus_warn ("Did not get an error launching binary that exited with failure code");
192 if (!(dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY) ||
193 dbus_error_has_name (&error, DBUS_ERROR_SPAWN_CHILD_EXITED)))
195 _dbus_warn ("Not expecting error when launching exiting executable: %s: %s",
196 error.name, error.message);
197 dbus_error_free (&error);
201 dbus_error_free (&error);
207 check_spawn_and_kill (void *data)
209 char *argv[4] = { NULL, NULL, NULL, NULL };
210 DBusBabysitter *sitter = NULL;
211 DBusError error = DBUS_ERROR_INIT;
214 /*** Test launching sleeping binary then killing it */
216 argv[0] = get_test_exec ("test-sleep-forever", &argv0);
220 /* OOM was simulated or DBUS_TEST_EXEC was unset; either is OK */
224 if (_dbus_spawn_async_with_babysitter (&sitter, "spawn_and_kill", argv,
225 NULL, DBUS_SPAWN_NONE, NULL, NULL,
228 _dbus_babysitter_kill_child (sitter);
230 _dbus_babysitter_block_for_child_exit (sitter);
232 _dbus_babysitter_set_child_exit_error (sitter, &error);
235 _dbus_string_free (&argv0);
238 _dbus_babysitter_unref (sitter);
240 if (!dbus_error_is_set (&error))
242 _dbus_warn ("Did not get an error after killing spawned binary");
246 if (!(dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY) ||
248 dbus_error_has_name (&error, DBUS_ERROR_SPAWN_CHILD_EXITED)))
250 dbus_error_has_name (&error, DBUS_ERROR_SPAWN_CHILD_SIGNALED)))
253 _dbus_warn ("Not expecting error when killing executable: %s: %s",
254 error.name, error.message);
255 dbus_error_free (&error);
259 dbus_error_free (&error);
265 _dbus_spawn_test (const char *test_data_dir)
267 if (!_dbus_test_oom_handling ("spawn_nonexistent",
268 check_spawn_nonexistent,
272 if (!_dbus_test_oom_handling ("spawn_segfault",
273 check_spawn_segfault,
277 if (!_dbus_test_oom_handling ("spawn_exit",
282 if (!_dbus_test_oom_handling ("spawn_and_kill",
283 check_spawn_and_kill,