D-Bus  1.11.6
dbus-spawn-test.c
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-spawn-test.c
3  *
4  * Copyright (C) 2002, 2003, 2004 Red Hat, Inc.
5  * Copyright (C) 2003 CodeFactory AB
6  * Copyright (C) 2005 Novell, Inc.
7  *
8  * Licensed under the Academic Free License version 2.1
9  *
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.
14  *
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.
19  *
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
23  *
24  */
25 #include <config.h>
26 
27 
28 #include "dbus-spawn.h"
29 #include "dbus-sysdeps.h"
30 #include "dbus-test.h"
31 
32 static char *
33 get_test_exec (const char *exe,
34  DBusString *scratch_space)
35 {
36  const char *dbus_test_exec;
37 
38  dbus_test_exec = _dbus_getenv ("DBUS_TEST_EXEC");
39 
40  if (dbus_test_exec == NULL)
41  dbus_test_exec = DBUS_TEST_EXEC;
42 
43  if (!_dbus_string_init (scratch_space))
44  return NULL;
45 
46  if (!_dbus_string_append_printf (scratch_space, "%s/%s%s",
47  dbus_test_exec, exe, DBUS_EXEEXT))
48  {
49  _dbus_string_free (scratch_space);
50  return NULL;
51  }
52 
53  return _dbus_string_get_data (scratch_space);
54 }
55 
56 static dbus_bool_t
57 check_spawn_nonexistent (void *data)
58 {
59  char *argv[4] = { NULL, NULL, NULL, NULL };
60  DBusBabysitter *sitter = NULL;
61  DBusError error = DBUS_ERROR_INIT;
62 
63  /*** Test launching nonexistent binary */
64 
65  argv[0] = "/this/does/not/exist/32542sdgafgafdg";
66  if (_dbus_spawn_async_with_babysitter (&sitter, "spawn_nonexistent", argv,
67  NULL, DBUS_SPAWN_NONE, NULL, NULL,
68  &error))
69  {
70  _dbus_babysitter_block_for_child_exit (sitter);
72  }
73 
74  if (sitter)
75  _dbus_babysitter_unref (sitter);
76 
77  if (!dbus_error_is_set (&error))
78  {
79  _dbus_warn ("Did not get an error launching nonexistent executable");
80  return FALSE;
81  }
82 
83  if (!(dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY) ||
85  {
86  _dbus_warn ("Not expecting error when launching nonexistent executable: %s: %s",
87  error.name, error.message);
88  dbus_error_free (&error);
89  return FALSE;
90  }
91 
92  dbus_error_free (&error);
93 
94  return TRUE;
95 }
96 
97 static dbus_bool_t
98 check_spawn_segfault (void *data)
99 {
100  char *argv[4] = { NULL, NULL, NULL, NULL };
101  DBusBabysitter *sitter = NULL;
102  DBusError error = DBUS_ERROR_INIT;
103  DBusString argv0;
104 
105  /*** Test launching segfault binary */
106 
107  argv[0] = get_test_exec ("test-segfault", &argv0);
108 
109  if (argv[0] == NULL)
110  {
111  /* OOM was simulated, never mind */
112  return TRUE;
113  }
114 
115  if (_dbus_spawn_async_with_babysitter (&sitter, "spawn_segfault", argv,
116  NULL, DBUS_SPAWN_NONE, NULL, NULL,
117  &error))
118  {
119  _dbus_babysitter_block_for_child_exit (sitter);
120  _dbus_babysitter_set_child_exit_error (sitter, &error);
121  }
122 
123  _dbus_string_free (&argv0);
124 
125  if (sitter)
126  _dbus_babysitter_unref (sitter);
127 
128  if (!dbus_error_is_set (&error))
129  {
130  _dbus_warn ("Did not get an error launching segfaulting binary");
131  return FALSE;
132  }
133 
134  if (!(dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY) ||
135 #ifdef DBUS_WIN
137 #else
139 #endif
140  {
141  _dbus_warn ("Not expecting error when launching segfaulting executable: %s: %s",
142  error.name, error.message);
143  dbus_error_free (&error);
144  return FALSE;
145  }
146 
147  dbus_error_free (&error);
148 
149  return TRUE;
150 }
151 
152 static dbus_bool_t
153 check_spawn_exit (void *data)
154 {
155  char *argv[4] = { NULL, NULL, NULL, NULL };
156  DBusBabysitter *sitter = NULL;
157  DBusError error = DBUS_ERROR_INIT;
158  DBusString argv0;
159 
160  /*** Test launching exit failure binary */
161 
162  argv[0] = get_test_exec ("test-exit", &argv0);
163 
164  if (argv[0] == NULL)
165  {
166  /* OOM was simulated, never mind */
167  return TRUE;
168  }
169 
170  if (_dbus_spawn_async_with_babysitter (&sitter, "spawn_exit", argv,
171  NULL, DBUS_SPAWN_NONE, NULL, NULL,
172  &error))
173  {
174  _dbus_babysitter_block_for_child_exit (sitter);
175  _dbus_babysitter_set_child_exit_error (sitter, &error);
176  }
177 
178  _dbus_string_free (&argv0);
179 
180  if (sitter)
181  _dbus_babysitter_unref (sitter);
182 
183  if (!dbus_error_is_set (&error))
184  {
185  _dbus_warn ("Did not get an error launching binary that exited with failure code");
186  return FALSE;
187  }
188 
189  if (!(dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY) ||
191  {
192  _dbus_warn ("Not expecting error when launching exiting executable: %s: %s",
193  error.name, error.message);
194  dbus_error_free (&error);
195  return FALSE;
196  }
197 
198  dbus_error_free (&error);
199 
200  return TRUE;
201 }
202 
203 static dbus_bool_t
204 check_spawn_and_kill (void *data)
205 {
206  char *argv[4] = { NULL, NULL, NULL, NULL };
207  DBusBabysitter *sitter = NULL;
208  DBusError error = DBUS_ERROR_INIT;
209  DBusString argv0;
210 
211  /*** Test launching sleeping binary then killing it */
212 
213  argv[0] = get_test_exec ("test-sleep-forever", &argv0);
214 
215  if (argv[0] == NULL)
216  {
217  /* OOM was simulated, never mind */
218  return TRUE;
219  }
220 
221  if (_dbus_spawn_async_with_babysitter (&sitter, "spawn_and_kill", argv,
222  NULL, DBUS_SPAWN_NONE, NULL, NULL,
223  &error))
224  {
226 
227  _dbus_babysitter_block_for_child_exit (sitter);
228 
229  _dbus_babysitter_set_child_exit_error (sitter, &error);
230  }
231 
232  _dbus_string_free (&argv0);
233 
234  if (sitter)
235  _dbus_babysitter_unref (sitter);
236 
237  if (!dbus_error_is_set (&error))
238  {
239  _dbus_warn ("Did not get an error after killing spawned binary");
240  return FALSE;
241  }
242 
243  if (!(dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY) ||
244 #ifdef DBUS_WIN
246 #else
248 #endif
249  {
250  _dbus_warn ("Not expecting error when killing executable: %s: %s",
251  error.name, error.message);
252  dbus_error_free (&error);
253  return FALSE;
254  }
255 
256  dbus_error_free (&error);
257 
258  return TRUE;
259 }
260 
262 _dbus_spawn_test (const char *test_data_dir)
263 {
264  if (!_dbus_test_oom_handling ("spawn_nonexistent",
265  check_spawn_nonexistent,
266  NULL))
267  return FALSE;
268 
269  if (!_dbus_test_oom_handling ("spawn_segfault",
270  check_spawn_segfault,
271  NULL))
272  return FALSE;
273 
274  if (!_dbus_test_oom_handling ("spawn_exit",
275  check_spawn_exit,
276  NULL))
277  return FALSE;
278 
279  if (!_dbus_test_oom_handling ("spawn_and_kill",
280  check_spawn_and_kill,
281  NULL))
282  return FALSE;
283 
284  return TRUE;
285 }
dbus_bool_t dbus_error_has_name(const DBusError *error, const char *name)
Checks whether the error is set and has the given name.
Definition: dbus-errors.c:302
const char * message
public error message field
Definition: dbus-errors.h:51
#define NULL
A null pointer, defined appropriately for C or C++.
#define DBUS_ERROR_SPAWN_EXEC_FAILED
While starting a new process, the exec() call failed.
#define DBUS_ERROR_SPAWN_CHILD_EXITED
While starting a new process, the child exited with a status code.
dbus_bool_t _dbus_spawn_async_with_babysitter(DBusBabysitter **sitter_p, const char *log_name, char **argv, char **env, DBusSpawnFlags flags, DBusSpawnChildSetupFunc child_setup, void *user_data, DBusError *error)
Spawns a new process.
Definition: dbus-spawn.c:1215
#define DBUS_ERROR_INIT
Expands to a suitable initializer for a DBusError on the stack.
Definition: dbus-errors.h:62
void dbus_error_free(DBusError *error)
Frees an error that&#39;s been set (or just initialized), then reinitializes the error as in dbus_error_i...
Definition: dbus-errors.c:211
dbus_bool_t _dbus_string_init(DBusString *str)
Initializes a string.
Definition: dbus-string.c:175
#define DBUS_ERROR_SPAWN_CHILD_SIGNALED
While starting a new process, the child exited on a signal.
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
Definition: dbus-types.h:35
void _dbus_babysitter_kill_child(DBusBabysitter *sitter)
Blocks until the babysitter process gives us the PID of the spawned grandchild, then kills the spawne...
Definition: dbus-spawn.c:684
Babysitter implementation details.
void _dbus_warn(const char *format,...)
Prints a warning message to stderr.
dbus_bool_t _dbus_string_append_printf(DBusString *str, const char *format,...)
Appends a printf-style formatted string to the DBusString.
Definition: dbus-string.c:1114
Object representing an exception.
Definition: dbus-errors.h:48
void _dbus_string_free(DBusString *str)
Frees a string created by _dbus_string_init().
Definition: dbus-string.c:259
#define TRUE
Expands to "1".
const char * name
public error name field
Definition: dbus-errors.h:50
void _dbus_babysitter_unref(DBusBabysitter *sitter)
Decrement the reference count on the babysitter object.
Definition: dbus-spawn.c:324
#define DBUS_ERROR_NO_MEMORY
There was not enough memory to complete an operation.
#define FALSE
Expands to "0".
void _dbus_babysitter_set_child_exit_error(DBusBabysitter *sitter, DBusError *error)
Sets the DBusError with an explanation of why the spawned child process exited (on a signal...
Definition: dbus-spawn.c:755
const char * _dbus_getenv(const char *varname)
Wrapper for getenv().
Definition: dbus-sysdeps.c:185
dbus_bool_t dbus_error_is_set(const DBusError *error)
Checks whether an error occurred (the error is set).
Definition: dbus-errors.c:329