2 * Copyright (C) 2011 Red Hat, Inc.
4 * This work is provided "as is"; redistribution and modification
5 * in whole or in part, in any medium, physical or electronic is
6 * permitted without restriction.
8 * This work is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * In no event shall the authors or contributors be liable for any
13 * direct, indirect, incidental, special, exemplary, or consequential
14 * damages (including, but not limited to, procurement of substitute
15 * goods or services; loss of use, data, or profits; or business
16 * interruption) however caused and on any theory of liability, whether
17 * in contract, strict liability, or tort (including negligence or
18 * otherwise) arising in any way out of the use of this software, even
19 * if advised of the possibility of such damage.
21 * Author: Colin Walters <walters@verbum.org>
29 static char *echo_prog_path;
30 static char *echo_script_path;
34 gboolean child_exited;
37 } SpawnAsyncMultithreadedData;
40 on_child_exited (GPid pid,
44 SpawnAsyncMultithreadedData *data = datap;
46 data->child_exited = TRUE;
47 if (data->child_exited && data->stdout_done)
48 g_main_loop_quit (data->loop);
50 return G_SOURCE_REMOVE;
54 on_child_stdout (GIOChannel *channel,
55 GIOCondition condition,
61 SpawnAsyncMultithreadedData *data = datap;
63 if (condition & G_IO_IN)
66 status = g_io_channel_read_chars (channel, buf, sizeof (buf), &bytes_read, &error);
67 g_assert_no_error (error);
68 g_string_append_len (data->stdout_buf, buf, (gssize) bytes_read);
69 if (status == G_IO_STATUS_EOF)
70 data->stdout_done = TRUE;
72 if (condition & G_IO_HUP)
73 data->stdout_done = TRUE;
74 if (condition & G_IO_ERR)
75 g_error ("Error reading from child stdin");
77 if (data->child_exited && data->stdout_done)
78 g_main_loop_quit (data->loop);
80 return !data->stdout_done;
84 test_spawn_async (void)
91 GMainContext *context;
96 SpawnAsyncMultithreadedData data;
98 context = g_main_context_new ();
99 loop = g_main_loop_new (context, TRUE);
101 arg = g_strdup_printf ("thread %d", tnum);
103 argv = g_ptr_array_new ();
104 g_ptr_array_add (argv, echo_prog_path);
105 g_ptr_array_add (argv, arg);
106 g_ptr_array_add (argv, NULL);
108 g_spawn_async_with_pipes (NULL, (char**)argv->pdata, NULL, G_SPAWN_DO_NOT_REAP_CHILD, NULL, NULL, &pid, NULL,
109 &child_stdout_fd, NULL, &error);
110 g_assert_no_error (error);
111 g_ptr_array_free (argv, TRUE);
114 data.stdout_done = FALSE;
115 data.child_exited = FALSE;
116 data.stdout_buf = g_string_new (0);
118 source = g_child_watch_source_new (pid);
119 g_source_set_callback (source, (GSourceFunc)on_child_exited, &data, NULL);
120 g_source_attach (source, context);
121 g_source_unref (source);
123 channel = g_io_channel_unix_new (child_stdout_fd);
124 source = g_io_create_watch (channel, G_IO_IN | G_IO_HUP | G_IO_ERR);
125 g_source_set_callback (source, (GSourceFunc)on_child_stdout, &data, NULL);
126 g_source_attach (source, context);
127 g_source_unref (source);
129 g_main_loop_run (loop);
131 g_assert (data.child_exited);
132 g_assert (data.stdout_done);
133 g_assert_cmpstr (data.stdout_buf->str, ==, arg);
134 g_string_free (data.stdout_buf, TRUE);
136 g_io_channel_unref (channel);
137 g_main_context_unref (context);
138 g_main_loop_unref (loop);
144 test_spawn_sync (void)
147 GError *error = NULL;
153 arg = g_strdup_printf ("thread %d", tnum);
155 argv = g_ptr_array_new ();
156 g_ptr_array_add (argv, echo_prog_path);
157 g_ptr_array_add (argv, arg);
158 g_ptr_array_add (argv, NULL);
160 g_spawn_sync (NULL, (char**)argv->pdata, NULL, 0, NULL, NULL, &stdout_str, NULL, &estatus, &error);
161 g_assert_no_error (error);
162 g_assert_cmpstr (arg, ==, stdout_str);
165 g_ptr_array_free (argv, TRUE);
169 test_spawn_script (void)
171 GError *error = NULL;
176 argv = g_ptr_array_new ();
177 g_ptr_array_add (argv, echo_script_path);
178 g_ptr_array_add (argv, NULL);
180 g_spawn_sync (NULL, (char**)argv->pdata, NULL, 0, NULL, NULL, &stdout_str, NULL, &estatus, &error);
181 g_assert_no_error (error);
182 g_assert_cmpstr ("echo\n", ==, stdout_str);
184 g_ptr_array_free (argv, TRUE);
193 g_test_init (&argc, &argv, NULL);
195 dirname = g_path_get_dirname (argv[0]);
196 echo_prog_path = g_build_filename (dirname, "test-spawn-echo", NULL);
197 if (!g_file_test (echo_prog_path, G_FILE_TEST_EXISTS))
199 g_free (echo_prog_path);
200 echo_prog_path = g_build_filename (dirname, "lt-test-spawn-echo", NULL);
203 #define SRCDIR dirname
205 echo_script_path = g_build_filename (SRCDIR, "echo-script", NULL);
206 if (!g_file_test (echo_script_path, G_FILE_TEST_EXISTS))
210 tmp = g_path_get_dirname (dirname);
211 g_free (echo_script_path);
212 echo_script_path = g_build_filename (tmp, "echo-script", NULL);
217 g_assert (g_file_test (echo_prog_path, G_FILE_TEST_EXISTS));
218 g_assert (g_file_test (echo_script_path, G_FILE_TEST_EXISTS));
220 g_test_add_func ("/gthread/spawn-single-sync", test_spawn_sync);
221 g_test_add_func ("/gthread/spawn-single-async", test_spawn_async);
222 g_test_add_func ("/gthread/spawn-script", test_spawn_script);