Do not run the g_child_watch_* test multi-threaded, as that doesn't work
[platform/upstream/glib.git] / tests / child-test.c
1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
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 #include "config.h"
28
29 #include <sys/types.h>
30 #ifdef HAVE_UNISTD_H
31 #include <unistd.h>
32 #endif
33 #include <stdlib.h>
34
35 #include <glib.h>
36
37 #ifdef G_OS_WIN32
38 #include <windows.h>
39 #endif
40
41 #ifdef G_OS_WIN32
42 #define GPID_FORMAT "%p"
43 #else
44 #define GPID_FORMAT "%d"
45 #endif
46
47 GMainLoop *main_loop;
48 gint alive;
49
50 #ifdef G_OS_WIN32
51 char *argv0;
52 #endif
53
54 GPid
55 get_a_child (gint ttl)
56 {
57   GPid pid;
58
59 #ifdef G_OS_WIN32
60   STARTUPINFO si;
61   PROCESS_INFORMATION pi;
62   gchar *cmdline;
63
64   memset (&si, 0, sizeof (si));
65   si.cb = sizeof (&si);
66   memset (&pi, 0, sizeof (pi));
67
68   cmdline = g_strdup_printf( "child-test -c%d", ttl);
69
70   if (!CreateProcess (argv0, cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))
71     g_error ("CreateProcess failed: %s\n", g_win32_error_message (GetLastError ()));
72
73   g_free(cmdline);
74
75   CloseHandle (pi.hThread);
76   pid = pi.hProcess;
77
78   return pid;
79 #else
80   pid = fork ();
81   if (pid < 0)
82     exit (1);
83
84   if (pid > 0)
85     return pid;
86
87   sleep (ttl);
88   _exit (0);
89 #endif /* G_OS_WIN32 */
90 }
91
92 gboolean
93 child_watch_callback (GPid pid, gint status, gpointer data)
94 {
95   gint ttl = GPOINTER_TO_INT (data);
96
97   g_print ("child " GPID_FORMAT " (ttl %d) exited, status %d\n", pid, ttl, status);
98
99   g_spawn_close_pid (pid);
100
101   if (--alive == 0)
102     g_main_loop_quit (main_loop);
103
104   return TRUE;
105 }
106
107 #ifdef TEST_THREAD
108 static gpointer
109 test_thread (gpointer data)
110 {
111   GMainLoop *new_main_loop;
112   GSource *source;
113   GPid pid;
114   gint ttl = GPOINTER_TO_INT (data);
115
116   new_main_loop = g_main_loop_new (NULL, FALSE);
117
118   pid = get_a_child (ttl);
119   source = g_child_watch_source_new (pid);
120   g_source_set_callback (source, (GSourceFunc) child_watch_callback, data, NULL);
121   g_source_attach (source, g_main_loop_get_context (new_main_loop));
122   g_source_unref (source);
123
124   g_print ("whee! created pid: " GPID_FORMAT " (ttl %d)\n", pid, ttl);
125
126   g_main_loop_run (new_main_loop);
127
128   return NULL;
129 }
130 #endif
131
132 int
133 main (int argc, char *argv[])
134 {
135 #ifdef G_OS_WIN32
136   argv0 = argv[0];
137   if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'c')
138     {
139       int ttl = atoi (argv[1] + 2);
140       Sleep (ttl * 1000);
141       /* Exit on purpose with STILL_ACTIVE (which isn't a very common
142        * exit status) to verify that g_child_watch_check() in gmain.c
143        * doesn't believe a child still to be active if it happens to
144        * exit with that status.
145        */
146       exit (STILL_ACTIVE);
147     }
148 #endif
149   /* Only run the test, if threads are enabled and a default thread
150    * implementation is available.
151    */
152 #if defined(G_THREADS_ENABLED) && ! defined(G_THREADS_IMPL_NONE)
153 #ifdef TEST_THREAD
154   g_thread_init (NULL);
155 #else
156   GPid pid;
157 #endif
158   main_loop = g_main_loop_new (NULL, FALSE);
159
160 #ifdef G_OS_WIN32
161   system ("ipconfig /all");
162 #else
163   system ("/bin/true");
164 #endif
165
166   alive = 2;
167 #ifdef TEST_THREAD
168   g_thread_create (test_thread, GINT_TO_POINTER (10), FALSE, NULL);
169   g_thread_create (test_thread, GINT_TO_POINTER (20), FALSE, NULL);
170 #else
171   pid = get_a_child (10);
172   g_child_watch_add (pid, child_watch_callback, GINT_TO_POINTER (10));
173   pid = get_a_child (20);
174   g_child_watch_add (pid, child_watch_callback, GINT_TO_POINTER (20));
175 #endif
176   
177   g_main_loop_run (main_loop);
178
179 #endif
180    return 0;
181 }