sl@0: /* GLIB - Library of useful routines for C programming sl@0: * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald sl@0: * Portion Copyright © 2008-09 Nokia Corporation and/or its subsidiary(-ies). All rights reserved. sl@0: * This library is free software; you can redistribute it and/or sl@0: * modify it under the terms of the GNU Lesser General Public sl@0: * License as published by the Free Software Foundation; either sl@0: * version 2 of the License, or (at your option) any later version. sl@0: * sl@0: * This library is distributed in the hope that it will be useful, sl@0: * but WITHOUT ANY WARRANTY; without even the implied warranty of sl@0: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU sl@0: * Lesser General Public License for more details. sl@0: * sl@0: * You should have received a copy of the GNU Lesser General Public sl@0: * License along with this library; if not, write to the sl@0: * Free Software Foundation, Inc., 59 Temple Place - Suite 330, sl@0: * Boston, MA 02111-1307, USA. sl@0: */ sl@0: sl@0: /* sl@0: * Modified by the GLib Team and others 1997-2000. See the AUTHORS sl@0: * file for a list of people on the GLib Team. See the ChangeLog sl@0: * files for a list of changes. These files are distributed with sl@0: * GLib at ftp://ftp.gtk.org/pub/gtk/. sl@0: */ sl@0: sl@0: // This test case is a threaded test case. sl@0: sl@0: #include "config.h" sl@0: #include sl@0: sl@0: #include sl@0: #ifdef HAVE_UNISTD_H sl@0: #include sl@0: #endif sl@0: #include sl@0: sl@0: #include sl@0: sl@0: #ifdef SYMBIAN sl@0: #include "mrt2_glib2_test.h" sl@0: #endif /*SYMBIAN*/ sl@0: sl@0: #define TEST_THREAD 1 sl@0: sl@0: #ifdef G_OS_WIN32 sl@0: #include sl@0: #endif sl@0: sl@0: #ifdef G_OS_WIN32 sl@0: #define GPID_FORMAT "%p" sl@0: #else sl@0: #define GPID_FORMAT "%d" sl@0: #endif sl@0: sl@0: GMainLoop *main_loop; sl@0: gint alive; sl@0: sl@0: #if defined(G_OS_WIN32) || defined(SYMBIAN) sl@0: char *argv0; sl@0: #endif sl@0: sl@0: GPid sl@0: get_a_child (gint ttl) sl@0: { sl@0: GPid pid; sl@0: sl@0: #ifdef G_OS_WIN32 sl@0: STARTUPINFO si; sl@0: PROCESS_INFORMATION pi; sl@0: gchar *cmdline; sl@0: sl@0: memset (&si, 0, sizeof (si)); sl@0: si.cb = sizeof (&si); sl@0: memset (&pi, 0, sizeof (pi)); sl@0: sl@0: cmdline = g_strdup_printf( "child-test -c%d", ttl); sl@0: sl@0: if (!CreateProcess (argv0, cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) sl@0: g_error ("CreateProcess failed: %s\n", g_win32_error_message (GetLastError ())); sl@0: sl@0: g_free(cmdline); sl@0: sl@0: CloseHandle (pi.hThread); sl@0: pid = pi.hProcess; sl@0: sl@0: return pid; sl@0: #elif defined(SYMBIAN) sl@0: gchar *working_directory = NULL; sl@0: gchar **envp = NULL; sl@0: gpointer user_data = NULL; sl@0: GError *error = NULL; sl@0: GPid child_pid; sl@0: sl@0: GSpawnChildSetupFunc child_setup = NULL; sl@0: sl@0: int retVal = 0; sl@0: sl@0: int flags = G_SPAWN_FILE_AND_ARGV_ZERO|G_SPAWN_DO_NOT_REAP_CHILD; sl@0: sl@0: char **argv = NULL; sl@0: argv = (char **)malloc(3*sizeof(char *)); sl@0: argv[1] = (char *)malloc(10*sizeof(char)); sl@0: argv[0] = argv0; sl@0: sprintf(argv[1],"-c%d",ttl); sl@0: argv[2] = NULL; sl@0: g_assert(g_spawn_async(working_directory,argv,envp,flags,child_setup,user_data,&child_pid,&error)); sl@0: return child_pid; sl@0: sl@0: #else sl@0: pid = fork (); sl@0: if (pid < 0) sl@0: exit (1); sl@0: sl@0: if (pid > 0) sl@0: return pid; sl@0: sl@0: sleep (ttl); sl@0: _exit (0); sl@0: #endif /* G_OS_WIN32 */ sl@0: } sl@0: sl@0: gboolean sl@0: child_watch_callback (GPid pid, gint status, gpointer data) sl@0: { sl@0: #ifdef VERBOSE sl@0: gint ttl = GPOINTER_TO_INT (data); sl@0: sl@0: g_print ("child " GPID_FORMAT " (ttl %d) exited, status %d\n", pid, ttl, status); sl@0: #endif sl@0: sl@0: g_spawn_close_pid (pid); sl@0: sl@0: if (--alive == 0) sl@0: g_main_loop_quit (main_loop); sl@0: sl@0: return TRUE; sl@0: } sl@0: sl@0: static gboolean sl@0: quit_loop (gpointer data) sl@0: { sl@0: GMainLoop *main_loop = data; sl@0: sl@0: g_main_loop_quit (main_loop); sl@0: sl@0: return TRUE; sl@0: } sl@0: sl@0: #ifdef TEST_THREAD sl@0: static gpointer sl@0: test_thread (gpointer data) sl@0: { sl@0: GMainLoop *new_main_loop; sl@0: GSource *source; sl@0: GPid pid = 0; sl@0: gint ttl = GPOINTER_TO_INT (data); sl@0: sl@0: new_main_loop = g_main_loop_new (NULL, FALSE); sl@0: sl@0: pid = get_a_child (ttl); sl@0: g_assert(pid != 0); sl@0: source = g_child_watch_source_new (pid); sl@0: g_source_set_callback (source, (GSourceFunc) child_watch_callback, data, NULL); sl@0: g_source_attach (source, g_main_loop_get_context (new_main_loop)); sl@0: g_source_unref (source); sl@0: sl@0: #ifdef VERBOSE sl@0: g_print ("whee! created pid: " GPID_FORMAT " (ttl %d)\n", pid, ttl); sl@0: #endif sl@0: sl@0: g_main_loop_run (new_main_loop); sl@0: sl@0: return NULL; sl@0: } sl@0: #endif sl@0: sl@0: int sl@0: main (int argc, char *argv[]) sl@0: { sl@0: GPid pid; sl@0: sl@0: #if defined(SYMBIAN) && (defined(__WINS__) || defined(__WINSCW__)) sl@0: sl@0: testResultXml("child-test2"); sl@0: return 0; sl@0: sl@0: #endif // EMULATOR sl@0: #if defined(G_OS_WIN32) || defined(SYMBIAN) sl@0: argv0 = argv[0]; sl@0: sl@0: if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'c') sl@0: { sl@0: int ttl = atoi (argv[1] + 2); sl@0: usleep (ttl); sl@0: exit (0); sl@0: } sl@0: #endif sl@0: sl@0: #ifdef SYMBIAN sl@0: sl@0: 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); sl@0: g_set_print_handler(mrtPrintHandler); sl@0: #endif /*SYMBIAN*/ sl@0: sl@0: /* Only run the test, if threads are enabled and a default thread sl@0: * implementation is available. sl@0: */ sl@0: #if defined(G_THREADS_ENABLED) && ! defined(G_THREADS_IMPL_NONE) sl@0: #ifdef TEST_THREAD sl@0: g_thread_init (NULL); sl@0: #endif sl@0: sl@0: main_loop = g_main_loop_new (NULL, FALSE); sl@0: g_assert(main_loop != NULL); sl@0: sl@0: #ifdef G_OS_WIN32 sl@0: system ("ipconfig /all"); sl@0: #else sl@0: system ("/bin/true"); sl@0: #endif sl@0: sl@0: alive = 2; sl@0: g_timeout_add (30000, quit_loop, main_loop); sl@0: sl@0: #ifdef TEST_THREAD sl@0: g_thread_create (test_thread, GINT_TO_POINTER (10), FALSE, NULL); sl@0: g_thread_create (test_thread, GINT_TO_POINTER (20), FALSE, NULL); sl@0: #else sl@0: pid = get_a_child (10); sl@0: g_assert(pid != 0); sl@0: g_child_watch_add (pid, (GChildWatchFunc) child_watch_callback, sl@0: GINT_TO_POINTER (10)); sl@0: pid = get_a_child (20); sl@0: g_assert(pid != 0); sl@0: g_child_watch_add (pid, (GChildWatchFunc) child_watch_callback, sl@0: GINT_TO_POINTER (20)); sl@0: #endif sl@0: sl@0: g_main_loop_run (main_loop); sl@0: sl@0: if (alive > 0) sl@0: { sl@0: g_warning ("%d children still alive\n", alive); sl@0: g_assert(FALSE && "some children still alive"); sl@0: return 1; sl@0: } sl@0: sl@0: #endif sl@0: sl@0: #if SYMBIAN sl@0: testResultXml("child-test2"); sl@0: #endif sl@0: sl@0: return 0; sl@0: }