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