os/ossrv/glib/tsrc/BC/tests/child-test1.c
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     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 is a non-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 #ifdef G_OS_WIN32
    45 #include <windows.h>
    46 #endif
    47 
    48 #ifdef G_OS_WIN32
    49 #define GPID_FORMAT "%p"
    50 #else
    51 #define GPID_FORMAT "%d"
    52 #endif
    53 
    54 GMainLoop *main_loop;
    55 gint alive;
    56 
    57 #if defined(G_OS_WIN32) || defined(SYMBIAN)
    58 char *argv0;
    59 #endif
    60 
    61 GPid
    62 get_a_child (gint ttl)
    63 {
    64   GPid pid;
    65 
    66 #ifdef G_OS_WIN32
    67   STARTUPINFO si;
    68   PROCESS_INFORMATION pi;
    69   gchar *cmdline;
    70 
    71   memset (&si, 0, sizeof (si));
    72   si.cb = sizeof (&si);
    73   memset (&pi, 0, sizeof (pi));
    74 
    75   cmdline = g_strdup_printf( "child-test -c%d", ttl);
    76 
    77   if (!CreateProcess (argv0, cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))
    78     g_error ("CreateProcess failed: %s\n", g_win32_error_message (GetLastError ()));
    79 
    80   g_free(cmdline);
    81 
    82   CloseHandle (pi.hThread);
    83   pid = pi.hProcess;
    84 
    85   return pid;
    86 #elif defined(SYMBIAN)
    87   	gchar *working_directory = NULL;
    88     gchar **envp = NULL;
    89     gpointer user_data = NULL;
    90 	GError *error = NULL;
    91 	GPid child_pid;
    92 	   
    93     GSpawnChildSetupFunc child_setup = NULL;
    94     
    95     int retVal = 0;
    96     
    97     int flags = G_SPAWN_FILE_AND_ARGV_ZERO|G_SPAWN_DO_NOT_REAP_CHILD;
    98     
    99     char **argv = NULL;      
   100 	argv = (char **)malloc(3*sizeof(char *));
   101 	argv[1] = (char *)malloc(10*sizeof(char));
   102 	argv[0] = argv0;
   103     sprintf(argv[1],"-c%d",ttl);
   104     argv[2] = NULL;
   105     g_assert(g_spawn_async(working_directory,argv,envp,flags,child_setup,user_data,&child_pid,&error));
   106     return child_pid;
   107     
   108 #else
   109   pid = fork ();
   110   if (pid < 0)
   111     exit (1);
   112 
   113   if (pid > 0)
   114     return pid;
   115 
   116   sleep (ttl);
   117   _exit (0);
   118 #endif /* G_OS_WIN32 */
   119 }
   120 
   121 gboolean
   122 child_watch_callback (GPid pid, gint status, gpointer data)
   123 {
   124 #ifdef VERBOSE
   125   gint ttl = GPOINTER_TO_INT (data);
   126 
   127   g_print ("child " GPID_FORMAT " (ttl %d) exited, status %d\n", pid, ttl, status);
   128 #endif
   129 
   130   g_spawn_close_pid (pid);
   131 
   132   if (--alive == 0)
   133     g_main_loop_quit (main_loop);
   134 
   135   return TRUE;
   136 }
   137 
   138 static gboolean
   139 quit_loop (gpointer data)
   140 {
   141   GMainLoop *main_loop = data;
   142 
   143   g_main_loop_quit (main_loop);
   144 
   145   return TRUE;
   146 }
   147 
   148 #ifdef TEST_THREAD
   149 static gpointer
   150 test_thread (gpointer data)
   151 {
   152   GMainLoop *new_main_loop;
   153   GSource *source;
   154   GPid pid = 0;
   155   gint ttl = GPOINTER_TO_INT (data);
   156 
   157   new_main_loop = g_main_loop_new (NULL, FALSE);
   158 
   159   pid = get_a_child (ttl);
   160   g_assert(pid != 0);
   161   source = g_child_watch_source_new (pid);
   162   g_assert(source);
   163   g_source_set_callback (source, (GSourceFunc) child_watch_callback, data, NULL);
   164   g_source_attach (source, g_main_loop_get_context (new_main_loop));
   165   g_source_unref (source);
   166 
   167 #ifdef VERBOSE
   168   g_print ("whee! created pid: " GPID_FORMAT " (ttl %d)\n", pid, ttl);
   169 #endif
   170 
   171   g_main_loop_run (new_main_loop);
   172 
   173   return NULL;
   174 }
   175 #endif
   176 
   177 int
   178 main (int argc, char *argv[])
   179 {
   180   
   181   GPid pid = 0;
   182   #if defined(SYMBIAN) && (defined(__WINS__) || defined(__WINSCW__))
   183      
   184    testResultXml("child-test1");
   185    return 0;
   186   
   187   #endif // EMULATOR
   188 
   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 	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);
   202 	g_set_print_handler(mrtPrintHandler);
   203 	#endif /*SYMBIAN*/
   204 
   205   /* Only run the test, if threads are enabled and a default thread
   206    * implementation is available.
   207    */
   208 #if defined(G_THREADS_ENABLED) && ! defined(G_THREADS_IMPL_NONE)
   209 	#ifdef TEST_THREAD
   210 		g_thread_init (NULL);
   211 	#endif
   212 	
   213 		main_loop = g_main_loop_new (NULL, FALSE);
   214 		g_assert(main_loop != NULL);
   215 
   216 	#ifdef G_OS_WIN32
   217 		system ("ipconfig /all");
   218 	#else
   219 		system ("/bin/true");
   220 	#endif
   221 
   222 	alive = 2;
   223 	g_timeout_add (30000, quit_loop, main_loop);
   224 
   225 	#ifdef TEST_THREAD
   226 		g_thread_create (test_thread, GINT_TO_POINTER (10), FALSE, NULL);
   227 		g_thread_create (test_thread, GINT_TO_POINTER (20), FALSE, NULL);
   228 	#else
   229 		pid = get_a_child (10);
   230 		g_assert(pid != 0);
   231 		
   232 		g_child_watch_add (pid, (GChildWatchFunc) child_watch_callback,
   233 		     GINT_TO_POINTER (10));
   234 		     
   235 		pid = get_a_child (20);
   236 		g_assert(pid != 0);
   237 		
   238 		g_child_watch_add (pid, (GChildWatchFunc) child_watch_callback,
   239 		     GINT_TO_POINTER (20));
   240 	#endif
   241 	
   242 	g_main_loop_run (main_loop);
   243 	
   244   	if (alive > 0)
   245     {
   246       g_warning ("%d children still alive\n", alive);
   247       g_assert(FALSE && "some children still alive");
   248       return 1;
   249     }
   250     
   251 #endif
   252 
   253    #if SYMBIAN
   254    testResultXml("child-test1");
   255    #endif
   256    
   257    return 0;
   258 }