Blame tests/mapping-test.c

Packit ae235b
/* GLIB - Library of useful routines for C programming
Packit ae235b
 * Copyright (C) 2005 Matthias Clasen
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
#include <stdlib.h>
Packit ae235b
#include <string.h>
Packit ae235b
#include <sys/types.h>
Packit ae235b
#include <signal.h>
Packit ae235b
Packit ae235b
#include "glib.h"
Packit ae235b
#include "gstdio.h"
Packit ae235b
Packit ae235b
#ifdef G_OS_UNIX
Packit ae235b
#include <unistd.h>
Packit ae235b
#endif
Packit ae235b
Packit ae235b
static gchar *dir, *filename, *displayname, *childname;
Packit ae235b
Packit ae235b
static gboolean stop = FALSE;
Packit ae235b
Packit ae235b
static gint parent_pid;
Packit ae235b
Packit ae235b
#ifndef G_OS_WIN32
Packit ae235b
Packit ae235b
static void
Packit ae235b
handle_usr1 (int signum)
Packit ae235b
{
Packit ae235b
  stop = TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
#endif
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
check_stop (gpointer data)
Packit ae235b
{
Packit ae235b
  GMainLoop *loop = data;
Packit ae235b
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
  stop = g_file_test ("STOP", G_FILE_TEST_EXISTS);
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  if (stop)
Packit ae235b
    g_main_loop_quit (loop);
Packit ae235b
Packit ae235b
  return TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
write_or_die (const gchar *filename,
Packit ae235b
	      const gchar *contents,
Packit ae235b
	      gssize       length)
Packit ae235b
{
Packit ae235b
  GError *error = NULL;
Packit ae235b
  gchar *displayname;    
Packit ae235b
Packit ae235b
  if (!g_file_set_contents (filename, contents, length, &error)) 
Packit ae235b
    {
Packit ae235b
      displayname = g_filename_display_name (childname);
Packit ae235b
      g_print ("failed to write '%s': %s\n", 
Packit ae235b
	       displayname, error->message);
Packit ae235b
      exit (1);
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
static GMappedFile *
Packit ae235b
map_or_die (const gchar *filename,
Packit ae235b
	    gboolean     writable)
Packit ae235b
{
Packit ae235b
  GError *error = NULL;
Packit ae235b
  GMappedFile *map;
Packit ae235b
  gchar *displayname;
Packit ae235b
Packit ae235b
  map = g_mapped_file_new (filename, writable, &error);
Packit ae235b
  if (!map)
Packit ae235b
    {
Packit ae235b
      displayname = g_filename_display_name (childname);
Packit ae235b
      g_print ("failed to map '%s' non-writable, shared: %s\n", 
Packit ae235b
	       displayname, error->message);
Packit ae235b
      exit (1);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return map;
Packit ae235b
}
Packit ae235b
    
Packit ae235b
static gboolean
Packit ae235b
signal_parent (gpointer data)
Packit ae235b
{
Packit ae235b
#ifndef G_OS_WIN32
Packit ae235b
  kill (parent_pid, SIGUSR1);
Packit ae235b
#endif
Packit ae235b
  return G_SOURCE_REMOVE;
Packit ae235b
}
Packit ae235b
Packit ae235b
static int
Packit ae235b
child_main (int argc, char *argv[])
Packit ae235b
{
Packit ae235b
  GMappedFile *map;
Packit ae235b
  GMainLoop *loop;
Packit ae235b
Packit ae235b
  parent_pid = atoi (argv[2]);
Packit ae235b
  map = map_or_die (filename, FALSE);
Packit ae235b
Packit ae235b
#ifndef G_OS_WIN32
Packit ae235b
  signal (SIGUSR1, handle_usr1);
Packit ae235b
#endif
Packit ae235b
  loop = g_main_loop_new (NULL, FALSE);
Packit ae235b
  g_idle_add (check_stop, loop);
Packit ae235b
  g_idle_add (signal_parent, NULL);
Packit ae235b
  g_main_loop_run (loop);
Packit ae235b
Packit ae235b
 g_message ("test_child_private: received parent signal");
Packit ae235b
Packit ae235b
  write_or_die (childname, 
Packit ae235b
		g_mapped_file_get_contents (map),
Packit ae235b
		g_mapped_file_get_length (map));
Packit ae235b
Packit ae235b
  signal_parent (NULL);
Packit ae235b
Packit ae235b
  return 0;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_mapping (void)
Packit ae235b
{
Packit ae235b
  GMappedFile *map;
Packit ae235b
Packit ae235b
  write_or_die (filename, "ABC", -1);
Packit ae235b
Packit ae235b
  map = map_or_die (filename, FALSE);
Packit ae235b
  g_assert (g_mapped_file_get_length (map) == 3);
Packit ae235b
  g_mapped_file_free (map);
Packit ae235b
Packit ae235b
  map = map_or_die (filename, TRUE);
Packit ae235b
  g_assert (g_mapped_file_get_length (map) == 3);
Packit ae235b
  g_mapped_file_free (map);
Packit ae235b
  g_message ("test_mapping: ok");
Packit ae235b
}
Packit ae235b
Packit ae235b
static void 
Packit ae235b
test_private (void)
Packit ae235b
{
Packit ae235b
  GError *error = NULL;
Packit ae235b
  GMappedFile *map;
Packit ae235b
  gchar *buffer;
Packit ae235b
  gsize len;
Packit ae235b
Packit ae235b
  write_or_die (filename, "ABC", -1);
Packit ae235b
  map = map_or_die (filename, TRUE);
Packit ae235b
Packit ae235b
  buffer = (gchar *)g_mapped_file_get_contents (map);
Packit ae235b
  buffer[0] = '1';
Packit ae235b
  buffer[1] = '2';
Packit ae235b
  buffer[2] = '3';
Packit ae235b
  g_mapped_file_free (map);
Packit ae235b
Packit ae235b
  if (!g_file_get_contents (filename, &buffer, &len, &error))
Packit ae235b
    {
Packit ae235b
      g_print ("failed to read '%s': %s\n", 
Packit ae235b
	       displayname, error->message);
Packit ae235b
      exit (1);
Packit ae235b
      
Packit ae235b
    }
Packit ae235b
  g_assert (len == 3);
Packit ae235b
  g_assert (strcmp (buffer, "ABC") == 0);
Packit ae235b
  g_free (buffer);
Packit ae235b
Packit ae235b
  g_message ("test_private: ok");
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_child_private (gchar *argv0)
Packit ae235b
{
Packit ae235b
  GError *error = NULL;
Packit ae235b
  GMappedFile *map;
Packit ae235b
  gchar *buffer;
Packit ae235b
  gsize len;
Packit ae235b
  gchar *child_argv[4];
Packit ae235b
  GPid  child_pid;
Packit ae235b
  GMainLoop *loop;
Packit ae235b
  gchar pid[100];
Packit ae235b
  
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
  g_remove ("STOP");
Packit ae235b
  g_assert (!g_file_test ("STOP", G_FILE_TEST_EXISTS));
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  write_or_die (filename, "ABC", -1);
Packit ae235b
  map = map_or_die (filename, TRUE);
Packit ae235b
Packit ae235b
#ifndef G_OS_WIN32
Packit ae235b
  signal (SIGUSR1, handle_usr1);
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  g_snprintf (pid, sizeof(pid), "%d", getpid ());
Packit ae235b
  child_argv[0] = argv0;
Packit ae235b
  child_argv[1] = "mapchild";
Packit ae235b
  child_argv[2] = pid;
Packit ae235b
  child_argv[3] = NULL;
Packit ae235b
  if (!g_spawn_async (dir, child_argv, NULL,
Packit ae235b
		      0, NULL, NULL, &child_pid, &error))
Packit ae235b
    {
Packit ae235b
      g_print ("failed to spawn child: %s\n", 
Packit ae235b
	       error->message);
Packit ae235b
      exit (1);            
Packit ae235b
    }
Packit ae235b
 g_message ("test_child_private: child spawned");
Packit ae235b
Packit ae235b
#ifndef G_OS_WIN32
Packit ae235b
  loop = g_main_loop_new (NULL, FALSE);
Packit ae235b
  g_idle_add (check_stop, loop);
Packit ae235b
  g_main_loop_run (loop);
Packit ae235b
  stop = FALSE;
Packit ae235b
#else
Packit ae235b
  g_usleep (2000000);
Packit ae235b
#endif
Packit ae235b
Packit ae235b
 g_message ("test_child_private: received first child signal");
Packit ae235b
Packit ae235b
  buffer = (gchar *)g_mapped_file_get_contents (map);
Packit ae235b
  buffer[0] = '1';
Packit ae235b
  buffer[1] = '2';
Packit ae235b
  buffer[2] = '3';
Packit ae235b
  g_mapped_file_free (map);
Packit ae235b
Packit ae235b
#ifndef G_OS_WIN32
Packit ae235b
  kill (child_pid, SIGUSR1);
Packit ae235b
#else
Packit ae235b
  g_file_set_contents ("STOP", "Hey there\n", -1, NULL);
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifndef G_OS_WIN32
Packit ae235b
  g_idle_add (check_stop, loop);
Packit ae235b
  g_main_loop_run (loop);
Packit ae235b
#else
Packit ae235b
  g_usleep (2000000);
Packit ae235b
#endif
Packit ae235b
Packit ae235b
 g_message ("test_child_private: received second child signal");
Packit ae235b
Packit ae235b
  if (!g_file_get_contents (childname, &buffer, &len, &error))
Packit ae235b
    {
Packit ae235b
      gchar *name;
Packit ae235b
Packit ae235b
      name = g_filename_display_name (childname);
Packit ae235b
      g_print ("failed to read '%s': %s\n", name, error->message);
Packit ae235b
      exit (1);      
Packit ae235b
    }
Packit ae235b
  g_assert (len == 3);
Packit ae235b
  g_assert (strcmp (buffer, "ABC") == 0);
Packit ae235b
  g_free (buffer);
Packit ae235b
Packit ae235b
  g_message ("test_child_private: ok");
Packit ae235b
}
Packit ae235b
Packit ae235b
static int 
Packit ae235b
parent_main (int   argc,
Packit ae235b
	     char *argv[])
Packit ae235b
{
Packit ae235b
  /* test mapping with various flag combinations */
Packit ae235b
  test_mapping ();
Packit ae235b
Packit ae235b
  /* test private modification */
Packit ae235b
  test_private ();
Packit ae235b
Packit ae235b
  /* test multiple clients, non-shared */
Packit ae235b
  test_child_private (argv[0]);
Packit ae235b
Packit ae235b
  return 0;
Packit ae235b
}
Packit ae235b
Packit ae235b
int
Packit ae235b
main (int argc, 
Packit ae235b
      char *argv[])
Packit ae235b
{
Packit ae235b
  int ret;
Packit ae235b
#ifndef G_OS_WIN32
Packit ae235b
  sigset_t sig_mask, old_mask;
Packit ae235b
Packit ae235b
  sigemptyset (&sig_mask);
Packit ae235b
  sigaddset (&sig_mask, SIGUSR1);
Packit ae235b
  if (sigprocmask (SIG_UNBLOCK, &sig_mask, &old_mask) == 0)
Packit ae235b
    {
Packit ae235b
      if (sigismember (&old_mask, SIGUSR1))
Packit ae235b
        g_message ("SIGUSR1 was blocked, unblocking it");
Packit ae235b
    }
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  dir = g_get_current_dir ();
Packit ae235b
  filename = g_build_filename (dir, "maptest", NULL);
Packit ae235b
  displayname = g_filename_display_name (filename);
Packit ae235b
  childname = g_build_filename (dir, "mapchild", NULL);
Packit ae235b
Packit ae235b
  if (argc > 1)
Packit ae235b
    ret = child_main (argc, argv);
Packit ae235b
  else 
Packit ae235b
    ret = parent_main (argc, argv);
Packit ae235b
Packit ae235b
  g_free (childname);
Packit ae235b
  g_free (filename);
Packit ae235b
  g_free (displayname);
Packit ae235b
  g_free (dir);
Packit ae235b
Packit ae235b
  return ret;
Packit ae235b
}