os/ossrv/glib/tsrc/BC/tests/child-test2.c
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/glib/tsrc/BC/tests/child-test2.c	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,256 @@
     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 case is a 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 +#define TEST_THREAD 1
    1.48 +
    1.49 +#ifdef G_OS_WIN32
    1.50 +#include <windows.h>
    1.51 +#endif
    1.52 +
    1.53 +#ifdef G_OS_WIN32
    1.54 +#define GPID_FORMAT "%p"
    1.55 +#else
    1.56 +#define GPID_FORMAT "%d"
    1.57 +#endif
    1.58 +
    1.59 +GMainLoop *main_loop;
    1.60 +gint alive;
    1.61 +
    1.62 +#if defined(G_OS_WIN32) || defined(SYMBIAN)
    1.63 +char *argv0;
    1.64 +#endif
    1.65 +
    1.66 +GPid
    1.67 +get_a_child (gint ttl)
    1.68 +{
    1.69 +  GPid pid;
    1.70 +
    1.71 +#ifdef G_OS_WIN32
    1.72 +  STARTUPINFO si;
    1.73 +  PROCESS_INFORMATION pi;
    1.74 +  gchar *cmdline;
    1.75 +
    1.76 +  memset (&si, 0, sizeof (si));
    1.77 +  si.cb = sizeof (&si);
    1.78 +  memset (&pi, 0, sizeof (pi));
    1.79 +
    1.80 +  cmdline = g_strdup_printf( "child-test -c%d", ttl);
    1.81 +
    1.82 +  if (!CreateProcess (argv0, cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))
    1.83 +    g_error ("CreateProcess failed: %s\n", g_win32_error_message (GetLastError ()));
    1.84 +
    1.85 +  g_free(cmdline);
    1.86 +
    1.87 +  CloseHandle (pi.hThread);
    1.88 +  pid = pi.hProcess;
    1.89 +
    1.90 +  return pid;
    1.91 +#elif defined(SYMBIAN)
    1.92 +  	gchar *working_directory = NULL;
    1.93 +    gchar **envp = NULL;
    1.94 +    gpointer user_data = NULL;
    1.95 +	GError *error = NULL;
    1.96 +	GPid child_pid;
    1.97 +	   
    1.98 +    GSpawnChildSetupFunc child_setup = NULL;
    1.99 +    
   1.100 +    int retVal = 0;
   1.101 +    
   1.102 +    int flags = G_SPAWN_FILE_AND_ARGV_ZERO|G_SPAWN_DO_NOT_REAP_CHILD;
   1.103 +    
   1.104 +    char **argv = NULL;      
   1.105 +	argv = (char **)malloc(3*sizeof(char *));
   1.106 +	argv[1] = (char *)malloc(10*sizeof(char));
   1.107 +	argv[0] = argv0;
   1.108 +    sprintf(argv[1],"-c%d",ttl);
   1.109 +    argv[2] = NULL;
   1.110 +    g_assert(g_spawn_async(working_directory,argv,envp,flags,child_setup,user_data,&child_pid,&error));
   1.111 +    return child_pid;
   1.112 +    
   1.113 +#else
   1.114 +  pid = fork ();
   1.115 +  if (pid < 0)
   1.116 +    exit (1);
   1.117 +
   1.118 +  if (pid > 0)
   1.119 +    return pid;
   1.120 +
   1.121 +  sleep (ttl);
   1.122 +  _exit (0);
   1.123 +#endif /* G_OS_WIN32 */
   1.124 +}
   1.125 +
   1.126 +gboolean
   1.127 +child_watch_callback (GPid pid, gint status, gpointer data)
   1.128 +{
   1.129 +#ifdef VERBOSE
   1.130 +  gint ttl = GPOINTER_TO_INT (data);
   1.131 +
   1.132 +  g_print ("child " GPID_FORMAT " (ttl %d) exited, status %d\n", pid, ttl, status);
   1.133 +#endif
   1.134 +
   1.135 +  g_spawn_close_pid (pid);
   1.136 +
   1.137 +  if (--alive == 0)
   1.138 +    g_main_loop_quit (main_loop);
   1.139 +
   1.140 +  return TRUE;
   1.141 +}
   1.142 +
   1.143 +static gboolean
   1.144 +quit_loop (gpointer data)
   1.145 +{
   1.146 +  GMainLoop *main_loop = data;
   1.147 +
   1.148 +  g_main_loop_quit (main_loop);
   1.149 +
   1.150 +  return TRUE;
   1.151 +}
   1.152 +
   1.153 +#ifdef TEST_THREAD
   1.154 +static gpointer
   1.155 +test_thread (gpointer data)
   1.156 +{
   1.157 +  GMainLoop *new_main_loop;
   1.158 +  GSource *source;
   1.159 +  GPid pid = 0;
   1.160 +  gint ttl = GPOINTER_TO_INT (data);
   1.161 +
   1.162 +  new_main_loop = g_main_loop_new (NULL, FALSE);
   1.163 +
   1.164 +  pid = get_a_child (ttl);
   1.165 +  g_assert(pid != 0);
   1.166 +  source = g_child_watch_source_new (pid);
   1.167 +  g_source_set_callback (source, (GSourceFunc) child_watch_callback, data, NULL);
   1.168 +  g_source_attach (source, g_main_loop_get_context (new_main_loop));
   1.169 +  g_source_unref (source);
   1.170 +
   1.171 +#ifdef VERBOSE
   1.172 +  g_print ("whee! created pid: " GPID_FORMAT " (ttl %d)\n", pid, ttl);
   1.173 +#endif
   1.174 +
   1.175 +  g_main_loop_run (new_main_loop);
   1.176 +
   1.177 +  return NULL;
   1.178 +}
   1.179 +#endif
   1.180 +
   1.181 +int
   1.182 +main (int argc, char *argv[])
   1.183 +{
   1.184 +  GPid pid;
   1.185 +  
   1.186 +  #if defined(SYMBIAN) && (defined(__WINS__) || defined(__WINSCW__))
   1.187 +     
   1.188 +   testResultXml("child-test2");
   1.189 +   return 0;
   1.190 +  
   1.191 +  #endif // EMULATOR
   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 +
   1.205 +	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.206 +	g_set_print_handler(mrtPrintHandler);
   1.207 +	#endif /*SYMBIAN*/
   1.208 +
   1.209 +  /* Only run the test, if threads are enabled and a default thread
   1.210 +   * implementation is available.
   1.211 +   */
   1.212 +#if defined(G_THREADS_ENABLED) && ! defined(G_THREADS_IMPL_NONE)
   1.213 +	#ifdef TEST_THREAD
   1.214 +		g_thread_init (NULL);
   1.215 +	#endif
   1.216 +	
   1.217 +		main_loop = g_main_loop_new (NULL, FALSE);
   1.218 +		g_assert(main_loop != NULL);
   1.219 +
   1.220 +	#ifdef G_OS_WIN32
   1.221 +		system ("ipconfig /all");
   1.222 +	#else
   1.223 +		system ("/bin/true");
   1.224 +	#endif
   1.225 +
   1.226 +	alive = 2;
   1.227 +	g_timeout_add (30000, quit_loop, main_loop);
   1.228 +
   1.229 +	#ifdef TEST_THREAD
   1.230 +		g_thread_create (test_thread, GINT_TO_POINTER (10), FALSE, NULL);
   1.231 +		g_thread_create (test_thread, GINT_TO_POINTER (20), FALSE, NULL);
   1.232 +	#else
   1.233 +		pid = get_a_child (10);
   1.234 +		g_assert(pid != 0);
   1.235 +		g_child_watch_add (pid, (GChildWatchFunc) child_watch_callback,
   1.236 +		     GINT_TO_POINTER (10));
   1.237 +		pid = get_a_child (20);
   1.238 +		g_assert(pid != 0);
   1.239 +		g_child_watch_add (pid, (GChildWatchFunc) child_watch_callback,
   1.240 +		     GINT_TO_POINTER (20));
   1.241 +	#endif
   1.242 +	
   1.243 +	g_main_loop_run (main_loop);
   1.244 +	
   1.245 +  	if (alive > 0)
   1.246 +    {
   1.247 +      g_warning ("%d children still alive\n", alive);
   1.248 +      g_assert(FALSE && "some children still alive");
   1.249 +      return 1;
   1.250 +    }
   1.251 +    
   1.252 +#endif
   1.253 +
   1.254 +   #if SYMBIAN
   1.255 +   testResultXml("child-test2");
   1.256 +   #endif
   1.257 +   
   1.258 +   return 0;
   1.259 +}