|
1 /* GLIB - Library of useful routines for C programming |
|
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald |
|
3 * Portion Copyright © 2008-09 Nokia Corporation and/or its subsidiary(-ies). All rights reserved. |
|
4 * This library is free software; you can redistribute it and/or |
|
5 * modify it under the terms of the GNU Lesser General Public |
|
6 * License as published by the Free Software Foundation; either |
|
7 * version 2 of the License, or (at your option) any later version. |
|
8 * |
|
9 * This library is distributed in the hope that it will be useful, |
|
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
12 * Lesser General Public License for more details. |
|
13 * |
|
14 * You should have received a copy of the GNU Lesser General Public |
|
15 * License along with this library; if not, write to the |
|
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
|
17 * Boston, MA 02111-1307, USA. |
|
18 */ |
|
19 |
|
20 /* |
|
21 * Modified by the GLib Team and others 1997-2000. See the AUTHORS |
|
22 * file for a list of people on the GLib Team. See the ChangeLog |
|
23 * files for a list of changes. These files are distributed with |
|
24 * GLib at ftp://ftp.gtk.org/pub/gtk/. |
|
25 */ |
|
26 |
|
27 // This test case is a threaded test case. |
|
28 |
|
29 #include "config.h" |
|
30 #include <stdio.h> |
|
31 |
|
32 #include <sys/types.h> |
|
33 #ifdef HAVE_UNISTD_H |
|
34 #include <unistd.h> |
|
35 #endif |
|
36 #include <stdlib.h> |
|
37 |
|
38 #include <glib.h> |
|
39 |
|
40 #ifdef SYMBIAN |
|
41 #include "mrt2_glib2_test.h" |
|
42 #endif /*SYMBIAN*/ |
|
43 |
|
44 #define TEST_THREAD 1 |
|
45 |
|
46 #ifdef G_OS_WIN32 |
|
47 #include <windows.h> |
|
48 #endif |
|
49 |
|
50 #ifdef G_OS_WIN32 |
|
51 #define GPID_FORMAT "%p" |
|
52 #else |
|
53 #define GPID_FORMAT "%d" |
|
54 #endif |
|
55 |
|
56 GMainLoop *main_loop; |
|
57 gint alive; |
|
58 |
|
59 #if defined(G_OS_WIN32) || defined(SYMBIAN) |
|
60 char *argv0; |
|
61 #endif |
|
62 |
|
63 GPid |
|
64 get_a_child (gint ttl) |
|
65 { |
|
66 GPid pid; |
|
67 |
|
68 #ifdef G_OS_WIN32 |
|
69 STARTUPINFO si; |
|
70 PROCESS_INFORMATION pi; |
|
71 gchar *cmdline; |
|
72 |
|
73 memset (&si, 0, sizeof (si)); |
|
74 si.cb = sizeof (&si); |
|
75 memset (&pi, 0, sizeof (pi)); |
|
76 |
|
77 cmdline = g_strdup_printf( "child-test -c%d", ttl); |
|
78 |
|
79 if (!CreateProcess (argv0, cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) |
|
80 g_error ("CreateProcess failed: %s\n", g_win32_error_message (GetLastError ())); |
|
81 |
|
82 g_free(cmdline); |
|
83 |
|
84 CloseHandle (pi.hThread); |
|
85 pid = pi.hProcess; |
|
86 |
|
87 return pid; |
|
88 #elif defined(SYMBIAN) |
|
89 gchar *working_directory = NULL; |
|
90 gchar **envp = NULL; |
|
91 gpointer user_data = NULL; |
|
92 GError *error = NULL; |
|
93 GPid child_pid; |
|
94 |
|
95 GSpawnChildSetupFunc child_setup = NULL; |
|
96 |
|
97 int retVal = 0; |
|
98 |
|
99 int flags = G_SPAWN_FILE_AND_ARGV_ZERO|G_SPAWN_DO_NOT_REAP_CHILD; |
|
100 |
|
101 char **argv = NULL; |
|
102 argv = (char **)malloc(3*sizeof(char *)); |
|
103 argv[1] = (char *)malloc(10*sizeof(char)); |
|
104 argv[0] = argv0; |
|
105 sprintf(argv[1],"-c%d",ttl); |
|
106 argv[2] = NULL; |
|
107 g_assert(g_spawn_async(working_directory,argv,envp,flags,child_setup,user_data,&child_pid,&error)); |
|
108 return child_pid; |
|
109 |
|
110 #else |
|
111 pid = fork (); |
|
112 if (pid < 0) |
|
113 exit (1); |
|
114 |
|
115 if (pid > 0) |
|
116 return pid; |
|
117 |
|
118 sleep (ttl); |
|
119 _exit (0); |
|
120 #endif /* G_OS_WIN32 */ |
|
121 } |
|
122 |
|
123 gboolean |
|
124 child_watch_callback (GPid pid, gint status, gpointer data) |
|
125 { |
|
126 #ifdef VERBOSE |
|
127 gint ttl = GPOINTER_TO_INT (data); |
|
128 |
|
129 g_print ("child " GPID_FORMAT " (ttl %d) exited, status %d\n", pid, ttl, status); |
|
130 #endif |
|
131 |
|
132 g_spawn_close_pid (pid); |
|
133 |
|
134 if (--alive == 0) |
|
135 g_main_loop_quit (main_loop); |
|
136 |
|
137 return TRUE; |
|
138 } |
|
139 |
|
140 static gboolean |
|
141 quit_loop (gpointer data) |
|
142 { |
|
143 GMainLoop *main_loop = data; |
|
144 |
|
145 g_main_loop_quit (main_loop); |
|
146 |
|
147 return TRUE; |
|
148 } |
|
149 |
|
150 #ifdef TEST_THREAD |
|
151 static gpointer |
|
152 test_thread (gpointer data) |
|
153 { |
|
154 GMainLoop *new_main_loop; |
|
155 GSource *source; |
|
156 GPid pid = 0; |
|
157 gint ttl = GPOINTER_TO_INT (data); |
|
158 |
|
159 new_main_loop = g_main_loop_new (NULL, FALSE); |
|
160 |
|
161 pid = get_a_child (ttl); |
|
162 g_assert(pid != 0); |
|
163 source = g_child_watch_source_new (pid); |
|
164 g_source_set_callback (source, (GSourceFunc) child_watch_callback, data, NULL); |
|
165 g_source_attach (source, g_main_loop_get_context (new_main_loop)); |
|
166 g_source_unref (source); |
|
167 |
|
168 #ifdef VERBOSE |
|
169 g_print ("whee! created pid: " GPID_FORMAT " (ttl %d)\n", pid, ttl); |
|
170 #endif |
|
171 |
|
172 g_main_loop_run (new_main_loop); |
|
173 |
|
174 return NULL; |
|
175 } |
|
176 #endif |
|
177 |
|
178 int |
|
179 main (int argc, char *argv[]) |
|
180 { |
|
181 GPid pid; |
|
182 |
|
183 #if defined(SYMBIAN) && (defined(__WINS__) || defined(__WINSCW__)) |
|
184 |
|
185 testResultXml("child-test2"); |
|
186 return 0; |
|
187 |
|
188 #endif // EMULATOR |
|
189 #if defined(G_OS_WIN32) || defined(SYMBIAN) |
|
190 argv0 = argv[0]; |
|
191 |
|
192 if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'c') |
|
193 { |
|
194 int ttl = atoi (argv[1] + 2); |
|
195 usleep (ttl); |
|
196 exit (0); |
|
197 } |
|
198 #endif |
|
199 |
|
200 #ifdef SYMBIAN |
|
201 |
|
202 g_log_set_handler (NULL, G_LOG_FLAG_FATAL| G_LOG_FLAG_RECURSION | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG, &mrtLogHandler, NULL); |
|
203 g_set_print_handler(mrtPrintHandler); |
|
204 #endif /*SYMBIAN*/ |
|
205 |
|
206 /* Only run the test, if threads are enabled and a default thread |
|
207 * implementation is available. |
|
208 */ |
|
209 #if defined(G_THREADS_ENABLED) && ! defined(G_THREADS_IMPL_NONE) |
|
210 #ifdef TEST_THREAD |
|
211 g_thread_init (NULL); |
|
212 #endif |
|
213 |
|
214 main_loop = g_main_loop_new (NULL, FALSE); |
|
215 g_assert(main_loop != NULL); |
|
216 |
|
217 #ifdef G_OS_WIN32 |
|
218 system ("ipconfig /all"); |
|
219 #else |
|
220 system ("/bin/true"); |
|
221 #endif |
|
222 |
|
223 alive = 2; |
|
224 g_timeout_add (30000, quit_loop, main_loop); |
|
225 |
|
226 #ifdef TEST_THREAD |
|
227 g_thread_create (test_thread, GINT_TO_POINTER (10), FALSE, NULL); |
|
228 g_thread_create (test_thread, GINT_TO_POINTER (20), FALSE, NULL); |
|
229 #else |
|
230 pid = get_a_child (10); |
|
231 g_assert(pid != 0); |
|
232 g_child_watch_add (pid, (GChildWatchFunc) child_watch_callback, |
|
233 GINT_TO_POINTER (10)); |
|
234 pid = get_a_child (20); |
|
235 g_assert(pid != 0); |
|
236 g_child_watch_add (pid, (GChildWatchFunc) child_watch_callback, |
|
237 GINT_TO_POINTER (20)); |
|
238 #endif |
|
239 |
|
240 g_main_loop_run (main_loop); |
|
241 |
|
242 if (alive > 0) |
|
243 { |
|
244 g_warning ("%d children still alive\n", alive); |
|
245 g_assert(FALSE && "some children still alive"); |
|
246 return 1; |
|
247 } |
|
248 |
|
249 #endif |
|
250 |
|
251 #if SYMBIAN |
|
252 testResultXml("child-test2"); |
|
253 #endif |
|
254 |
|
255 return 0; |
|
256 } |