Blame tests/child-test.c

Packit ae235b
/* GLIB - Library of useful routines for C programming
Packit ae235b
 * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General Public
Packit ae235b
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/*
Packit ae235b
 * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
Packit ae235b
 * file for a list of people on the GLib Team.  See the ChangeLog
Packit ae235b
 * files for a list of changes.  These files are distributed with
Packit ae235b
 * GLib at ftp://ftp.gtk.org/pub/gtk/. 
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include <sys/types.h>
Packit ae235b
#include <stdlib.h>
Packit ae235b
Packit ae235b
#include <glib.h>
Packit ae235b
Packit ae235b
#ifdef G_OS_UNIX
Packit ae235b
#include <unistd.h>
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
#include <windows.h>
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
#define GPID_FORMAT "%p"
Packit ae235b
#else
Packit ae235b
#define GPID_FORMAT "%d"
Packit ae235b
#endif
Packit ae235b
Packit ae235b
GMainLoop *main_loop;
Packit ae235b
gint alive;
Packit ae235b
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
char *argv0;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
static GPid
Packit ae235b
get_a_child (gint ttl)
Packit ae235b
{
Packit ae235b
  GPid pid;
Packit ae235b
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
  STARTUPINFO si;
Packit ae235b
  PROCESS_INFORMATION pi;
Packit ae235b
  gchar *cmdline;
Packit ae235b
Packit ae235b
  memset (&si, 0, sizeof (si));
Packit ae235b
  si.cb = sizeof (&si);
Packit ae235b
  memset (&pi, 0, sizeof (pi));
Packit ae235b
Packit ae235b
  cmdline = g_strdup_printf( "child-test -c%d", ttl);
Packit ae235b
Packit ae235b
  if (!CreateProcess (argv0, cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))
Packit ae235b
    g_error ("CreateProcess failed: %s\n", g_win32_error_message (GetLastError ()));
Packit ae235b
Packit ae235b
  g_free(cmdline);
Packit ae235b
Packit ae235b
  CloseHandle (pi.hThread);
Packit ae235b
  pid = pi.hProcess;
Packit ae235b
Packit ae235b
  return pid;
Packit ae235b
#else
Packit ae235b
  pid = fork ();
Packit ae235b
  if (pid < 0)
Packit ae235b
    exit (1);
Packit ae235b
Packit ae235b
  if (pid > 0)
Packit ae235b
    return pid;
Packit ae235b
Packit ae235b
  sleep (ttl);
Packit ae235b
  _exit (0);
Packit ae235b
#endif /* G_OS_WIN32 */
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
child_watch_callback (GPid pid, gint status, gpointer data)
Packit ae235b
{
Packit ae235b
#ifdef VERBOSE
Packit ae235b
  gint ttl = GPOINTER_TO_INT (data);
Packit ae235b
Packit ae235b
  g_print ("child " GPID_FORMAT " (ttl %d) exited, status %d\n", pid, ttl, status);
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  g_spawn_close_pid (pid);
Packit ae235b
Packit ae235b
  if (--alive == 0)
Packit ae235b
    g_main_loop_quit (main_loop);
Packit ae235b
Packit ae235b
  return TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
quit_loop (gpointer data)
Packit ae235b
{
Packit ae235b
  GMainLoop *main_loop = data;
Packit ae235b
Packit ae235b
  g_main_loop_quit (main_loop);
Packit ae235b
Packit ae235b
  return TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
#ifdef TEST_THREAD
Packit ae235b
static gpointer
Packit ae235b
test_thread (gpointer data)
Packit ae235b
{
Packit ae235b
  GMainLoop *new_main_loop;
Packit ae235b
  GSource *source;
Packit ae235b
  GPid pid;
Packit ae235b
  gint ttl = GPOINTER_TO_INT (data);
Packit ae235b
Packit ae235b
  new_main_loop = g_main_loop_new (NULL, FALSE);
Packit ae235b
Packit ae235b
  pid = get_a_child (ttl);
Packit ae235b
  source = g_child_watch_source_new (pid);
Packit ae235b
  g_source_set_callback (source, (GSourceFunc) child_watch_callback, data, NULL);
Packit ae235b
  g_source_attach (source, g_main_loop_get_context (new_main_loop));
Packit ae235b
  g_source_unref (source);
Packit ae235b
Packit ae235b
#ifdef VERBOSE
Packit ae235b
  g_print ("whee! created pid: " GPID_FORMAT " (ttl %d)\n", pid, ttl);
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  g_main_loop_run (new_main_loop);
Packit ae235b
Packit ae235b
  return NULL;
Packit ae235b
}
Packit ae235b
#endif
Packit ae235b
Packit ae235b
int
Packit ae235b
main (int argc, char *argv[])
Packit ae235b
{
Packit ae235b
#ifndef TEST_THREAD
Packit ae235b
  GPid pid;
Packit ae235b
#endif
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
  argv0 = argv[0];
Packit ae235b
  if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'c')
Packit ae235b
    {
Packit ae235b
      int ttl = atoi (argv[1] + 2);
Packit ae235b
      Sleep (ttl * 1000);
Packit ae235b
      /* Exit on purpose with STILL_ACTIVE (which isn't a very common
Packit ae235b
       * exit status) to verify that g_child_watch_check() in gmain.c
Packit ae235b
       * doesn't believe a child still to be active if it happens to
Packit ae235b
       * exit with that status.
Packit ae235b
       */
Packit ae235b
      exit (STILL_ACTIVE);
Packit ae235b
    }
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  main_loop = g_main_loop_new (NULL, FALSE);
Packit ae235b
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
  system ("ipconfig /all");
Packit ae235b
#else
Packit ae235b
  system ("true");
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  alive = 2;
Packit ae235b
  g_timeout_add_seconds (30, quit_loop, main_loop);
Packit ae235b
Packit ae235b
#ifdef TEST_THREAD
Packit ae235b
  g_thread_create (test_thread, GINT_TO_POINTER (10), FALSE, NULL);
Packit ae235b
  g_thread_create (test_thread, GINT_TO_POINTER (20), FALSE, NULL);
Packit ae235b
#else
Packit ae235b
  pid = get_a_child (10);
Packit ae235b
  g_child_watch_add (pid, (GChildWatchFunc) child_watch_callback,
Packit ae235b
		     GINT_TO_POINTER (10));
Packit ae235b
  pid = get_a_child (20);
Packit ae235b
  g_child_watch_add (pid, (GChildWatchFunc) child_watch_callback,
Packit ae235b
		     GINT_TO_POINTER (20));
Packit ae235b
#endif
Packit ae235b
  
Packit ae235b
  g_main_loop_run (main_loop);
Packit ae235b
Packit ae235b
  g_main_loop_unref (main_loop);
Packit ae235b
Packit ae235b
  if (alive > 0)
Packit ae235b
    {
Packit ae235b
      g_warning ("%d children still alive\n", alive);
Packit ae235b
      return 1;
Packit ae235b
    }
Packit ae235b
    
Packit ae235b
   return 0;
Packit ae235b
}