1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 2005 Matthias Clasen
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.
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.
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.
26 #include <sys/types.h>
32 static gchar *dir, *filename, *displayname, *childname;
34 static gboolean stop = FALSE;
39 handle_usr1 (int signum)
47 check_stop (gpointer data)
49 GMainLoop *loop = data;
52 stop = g_file_test ("STOP", G_FILE_TEST_EXISTS);
56 g_main_loop_quit (loop);
62 write_or_die (const gchar *filename,
63 const gchar *contents,
69 if (!g_file_set_contents (filename, contents, length, &error))
71 displayname = g_filename_display_name (childname);
72 g_print ("failed to write '%s': %s\n",
73 displayname, error->message);
79 map_or_die (const gchar *filename,
86 map = g_mapped_file_new (filename, writable, &error);
89 displayname = g_filename_display_name (childname);
90 g_print ("failed to map '%s' non-writable, shared: %s\n",
91 displayname, error->message);
99 child_main (int argc, char *argv[])
104 map = map_or_die (filename, FALSE);
106 loop = g_main_loop_new (NULL, FALSE);
109 signal (SIGUSR1, handle_usr1);
111 g_idle_add (check_stop, loop);
112 g_main_loop_run (loop);
114 write_or_die (childname,
115 g_mapped_file_get_contents (map),
116 g_mapped_file_get_length (map));
126 write_or_die (filename, "ABC", -1);
128 map = map_or_die (filename, FALSE);
129 g_assert (g_mapped_file_get_length (map) == 3);
130 g_mapped_file_free (map);
132 map = map_or_die (filename, TRUE);
133 g_assert (g_mapped_file_get_length (map) == 3);
134 g_mapped_file_free (map);
140 GError *error = NULL;
145 write_or_die (filename, "ABC", -1);
146 map = map_or_die (filename, TRUE);
148 buffer = (gchar *)g_mapped_file_get_contents (map);
152 g_mapped_file_free (map);
154 if (!g_file_get_contents (filename, &buffer, &len, &error))
156 g_print ("failed to read '%s': %s\n",
157 displayname, error->message);
162 g_assert (strcmp (buffer, "ABC") == 0);
168 test_child_private (gchar *argv0)
170 GError *error = NULL;
174 gchar *child_argv[3];
179 g_assert (!g_file_test ("STOP", G_FILE_TEST_EXISTS));
182 write_or_die (filename, "ABC", -1);
183 map = map_or_die (filename, TRUE);
185 child_argv[0] = argv0;
186 child_argv[1] = "mapchild";
187 child_argv[2] = NULL;
188 if (!g_spawn_async (dir, child_argv, NULL,
189 0, NULL, NULL, &child_pid, &error))
191 g_print ("failed to spawn child: %s\n",
196 /* give the child some time to set up its mapping */
199 buffer = (gchar *)g_mapped_file_get_contents (map);
203 g_mapped_file_free (map);
206 kill (child_pid, SIGUSR1);
208 g_file_set_contents ("STOP", "Hey there\n", -1, NULL);
211 /* give the child some time to write the file */
214 if (!g_file_get_contents (childname, &buffer, &len, &error))
218 name = g_filename_display_name (childname);
219 g_print ("failed to read '%s': %s\n", name, error->message);
223 g_assert (strcmp (buffer, "ABC") == 0);
228 parent_main (int argc,
231 /* test mapping with various flag combinations */
234 /* test private modification */
237 /* test multiple clients, non-shared */
238 test_child_private (argv[0]);
247 dir = g_get_current_dir ();
248 filename = g_build_filename (dir, "maptest", NULL);
249 displayname = g_filename_display_name (filename);
250 childname = g_build_filename (dir, "mapchild", NULL);
253 return child_main (argc, argv);
255 return parent_main (argc, argv);