1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/glib/tsrc/BC/tests/child-test1.c Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,258 @@
1.4 +/* GLIB - Library of useful routines for C programming
1.5 + * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
1.6 + * Portion Copyright © 2008-09 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
1.7 + * This library is free software; you can redistribute it and/or
1.8 + * modify it under the terms of the GNU Lesser General Public
1.9 + * License as published by the Free Software Foundation; either
1.10 + * version 2 of the License, or (at your option) any later version.
1.11 + *
1.12 + * This library is distributed in the hope that it will be useful,
1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1.15 + * Lesser General Public License for more details.
1.16 + *
1.17 + * You should have received a copy of the GNU Lesser General Public
1.18 + * License along with this library; if not, write to the
1.19 + * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
1.20 + * Boston, MA 02111-1307, USA.
1.21 + */
1.22 +
1.23 +/*
1.24 + * Modified by the GLib Team and others 1997-2000. See the AUTHORS
1.25 + * file for a list of people on the GLib Team. See the ChangeLog
1.26 + * files for a list of changes. These files are distributed with
1.27 + * GLib at ftp://ftp.gtk.org/pub/gtk/.
1.28 + */
1.29 +
1.30 + //This test is a non-threaded test case.
1.31 +
1.32 +#include "config.h"
1.33 +#include <stdio.h>
1.34 +
1.35 +#include <sys/types.h>
1.36 +#ifdef HAVE_UNISTD_H
1.37 +#include <unistd.h>
1.38 +#endif
1.39 +#include <stdlib.h>
1.40 +
1.41 +#include <glib.h>
1.42 +
1.43 +#ifdef SYMBIAN
1.44 +#include "mrt2_glib2_test.h"
1.45 +#endif /*SYMBIAN*/
1.46 +
1.47 +#ifdef G_OS_WIN32
1.48 +#include <windows.h>
1.49 +#endif
1.50 +
1.51 +#ifdef G_OS_WIN32
1.52 +#define GPID_FORMAT "%p"
1.53 +#else
1.54 +#define GPID_FORMAT "%d"
1.55 +#endif
1.56 +
1.57 +GMainLoop *main_loop;
1.58 +gint alive;
1.59 +
1.60 +#if defined(G_OS_WIN32) || defined(SYMBIAN)
1.61 +char *argv0;
1.62 +#endif
1.63 +
1.64 +GPid
1.65 +get_a_child (gint ttl)
1.66 +{
1.67 + GPid pid;
1.68 +
1.69 +#ifdef G_OS_WIN32
1.70 + STARTUPINFO si;
1.71 + PROCESS_INFORMATION pi;
1.72 + gchar *cmdline;
1.73 +
1.74 + memset (&si, 0, sizeof (si));
1.75 + si.cb = sizeof (&si);
1.76 + memset (&pi, 0, sizeof (pi));
1.77 +
1.78 + cmdline = g_strdup_printf( "child-test -c%d", ttl);
1.79 +
1.80 + if (!CreateProcess (argv0, cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))
1.81 + g_error ("CreateProcess failed: %s\n", g_win32_error_message (GetLastError ()));
1.82 +
1.83 + g_free(cmdline);
1.84 +
1.85 + CloseHandle (pi.hThread);
1.86 + pid = pi.hProcess;
1.87 +
1.88 + return pid;
1.89 +#elif defined(SYMBIAN)
1.90 + gchar *working_directory = NULL;
1.91 + gchar **envp = NULL;
1.92 + gpointer user_data = NULL;
1.93 + GError *error = NULL;
1.94 + GPid child_pid;
1.95 +
1.96 + GSpawnChildSetupFunc child_setup = NULL;
1.97 +
1.98 + int retVal = 0;
1.99 +
1.100 + int flags = G_SPAWN_FILE_AND_ARGV_ZERO|G_SPAWN_DO_NOT_REAP_CHILD;
1.101 +
1.102 + char **argv = NULL;
1.103 + argv = (char **)malloc(3*sizeof(char *));
1.104 + argv[1] = (char *)malloc(10*sizeof(char));
1.105 + argv[0] = argv0;
1.106 + sprintf(argv[1],"-c%d",ttl);
1.107 + argv[2] = NULL;
1.108 + g_assert(g_spawn_async(working_directory,argv,envp,flags,child_setup,user_data,&child_pid,&error));
1.109 + return child_pid;
1.110 +
1.111 +#else
1.112 + pid = fork ();
1.113 + if (pid < 0)
1.114 + exit (1);
1.115 +
1.116 + if (pid > 0)
1.117 + return pid;
1.118 +
1.119 + sleep (ttl);
1.120 + _exit (0);
1.121 +#endif /* G_OS_WIN32 */
1.122 +}
1.123 +
1.124 +gboolean
1.125 +child_watch_callback (GPid pid, gint status, gpointer data)
1.126 +{
1.127 +#ifdef VERBOSE
1.128 + gint ttl = GPOINTER_TO_INT (data);
1.129 +
1.130 + g_print ("child " GPID_FORMAT " (ttl %d) exited, status %d\n", pid, ttl, status);
1.131 +#endif
1.132 +
1.133 + g_spawn_close_pid (pid);
1.134 +
1.135 + if (--alive == 0)
1.136 + g_main_loop_quit (main_loop);
1.137 +
1.138 + return TRUE;
1.139 +}
1.140 +
1.141 +static gboolean
1.142 +quit_loop (gpointer data)
1.143 +{
1.144 + GMainLoop *main_loop = data;
1.145 +
1.146 + g_main_loop_quit (main_loop);
1.147 +
1.148 + return TRUE;
1.149 +}
1.150 +
1.151 +#ifdef TEST_THREAD
1.152 +static gpointer
1.153 +test_thread (gpointer data)
1.154 +{
1.155 + GMainLoop *new_main_loop;
1.156 + GSource *source;
1.157 + GPid pid = 0;
1.158 + gint ttl = GPOINTER_TO_INT (data);
1.159 +
1.160 + new_main_loop = g_main_loop_new (NULL, FALSE);
1.161 +
1.162 + pid = get_a_child (ttl);
1.163 + g_assert(pid != 0);
1.164 + source = g_child_watch_source_new (pid);
1.165 + g_assert(source);
1.166 + g_source_set_callback (source, (GSourceFunc) child_watch_callback, data, NULL);
1.167 + g_source_attach (source, g_main_loop_get_context (new_main_loop));
1.168 + g_source_unref (source);
1.169 +
1.170 +#ifdef VERBOSE
1.171 + g_print ("whee! created pid: " GPID_FORMAT " (ttl %d)\n", pid, ttl);
1.172 +#endif
1.173 +
1.174 + g_main_loop_run (new_main_loop);
1.175 +
1.176 + return NULL;
1.177 +}
1.178 +#endif
1.179 +
1.180 +int
1.181 +main (int argc, char *argv[])
1.182 +{
1.183 +
1.184 + GPid pid = 0;
1.185 + #if defined(SYMBIAN) && (defined(__WINS__) || defined(__WINSCW__))
1.186 +
1.187 + testResultXml("child-test1");
1.188 + return 0;
1.189 +
1.190 + #endif // EMULATOR
1.191 +
1.192 +#if defined(G_OS_WIN32) || defined(SYMBIAN)
1.193 + argv0 = argv[0];
1.194 +
1.195 + if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'c')
1.196 + {
1.197 + int ttl = atoi (argv[1] + 2);
1.198 + usleep (ttl);
1.199 + exit (0);
1.200 + }
1.201 +#endif
1.202 +
1.203 + #ifdef SYMBIAN
1.204 + 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);
1.205 + g_set_print_handler(mrtPrintHandler);
1.206 + #endif /*SYMBIAN*/
1.207 +
1.208 + /* Only run the test, if threads are enabled and a default thread
1.209 + * implementation is available.
1.210 + */
1.211 +#if defined(G_THREADS_ENABLED) && ! defined(G_THREADS_IMPL_NONE)
1.212 + #ifdef TEST_THREAD
1.213 + g_thread_init (NULL);
1.214 + #endif
1.215 +
1.216 + main_loop = g_main_loop_new (NULL, FALSE);
1.217 + g_assert(main_loop != NULL);
1.218 +
1.219 + #ifdef G_OS_WIN32
1.220 + system ("ipconfig /all");
1.221 + #else
1.222 + system ("/bin/true");
1.223 + #endif
1.224 +
1.225 + alive = 2;
1.226 + g_timeout_add (30000, quit_loop, main_loop);
1.227 +
1.228 + #ifdef TEST_THREAD
1.229 + g_thread_create (test_thread, GINT_TO_POINTER (10), FALSE, NULL);
1.230 + g_thread_create (test_thread, GINT_TO_POINTER (20), FALSE, NULL);
1.231 + #else
1.232 + pid = get_a_child (10);
1.233 + g_assert(pid != 0);
1.234 +
1.235 + g_child_watch_add (pid, (GChildWatchFunc) child_watch_callback,
1.236 + GINT_TO_POINTER (10));
1.237 +
1.238 + pid = get_a_child (20);
1.239 + g_assert(pid != 0);
1.240 +
1.241 + g_child_watch_add (pid, (GChildWatchFunc) child_watch_callback,
1.242 + GINT_TO_POINTER (20));
1.243 + #endif
1.244 +
1.245 + g_main_loop_run (main_loop);
1.246 +
1.247 + if (alive > 0)
1.248 + {
1.249 + g_warning ("%d children still alive\n", alive);
1.250 + g_assert(FALSE && "some children still alive");
1.251 + return 1;
1.252 + }
1.253 +
1.254 +#endif
1.255 +
1.256 + #if SYMBIAN
1.257 + testResultXml("child-test1");
1.258 + #endif
1.259 +
1.260 + return 0;
1.261 +}