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