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, see <http://www.gnu.org/licenses/>.
19 #include <sys/types.h>
29 static gchar *dir, *filename, *displayname, *childname;
31 static gboolean stop = FALSE;
33 static gint parent_pid;
38 handle_usr1 (int signum)
46 check_stop (gpointer data)
48 GMainLoop *loop = data;
51 stop = g_file_test ("STOP", G_FILE_TEST_EXISTS);
55 g_main_loop_quit (loop);
61 write_or_die (const gchar *filename,
62 const gchar *contents,
68 if (!g_file_set_contents (filename, contents, length, &error))
70 displayname = g_filename_display_name (childname);
71 g_print ("failed to write '%s': %s\n",
72 displayname, error->message);
78 map_or_die (const gchar *filename,
85 map = g_mapped_file_new (filename, writable, &error);
88 displayname = g_filename_display_name (childname);
89 g_print ("failed to map '%s' non-writable, shared: %s\n",
90 displayname, error->message);
98 signal_parent (gpointer data)
101 kill (parent_pid, SIGUSR1);
103 return G_SOURCE_REMOVE;
107 child_main (int argc, char *argv[])
112 parent_pid = atoi (argv[2]);
113 map = map_or_die (filename, FALSE);
116 signal (SIGUSR1, handle_usr1);
118 loop = g_main_loop_new (NULL, FALSE);
119 g_idle_add (check_stop, loop);
120 g_idle_add (signal_parent, NULL);
121 g_main_loop_run (loop);
123 write_or_die (childname,
124 g_mapped_file_get_contents (map),
125 g_mapped_file_get_length (map));
127 signal_parent (NULL);
137 write_or_die (filename, "ABC", -1);
139 map = map_or_die (filename, FALSE);
140 g_assert (g_mapped_file_get_length (map) == 3);
141 g_mapped_file_free (map);
143 map = map_or_die (filename, TRUE);
144 g_assert (g_mapped_file_get_length (map) == 3);
145 g_mapped_file_free (map);
146 g_message ("test_mapping: ok");
152 GError *error = NULL;
157 write_or_die (filename, "ABC", -1);
158 map = map_or_die (filename, TRUE);
160 buffer = (gchar *)g_mapped_file_get_contents (map);
164 g_mapped_file_free (map);
166 if (!g_file_get_contents (filename, &buffer, &len, &error))
168 g_print ("failed to read '%s': %s\n",
169 displayname, error->message);
174 g_assert (strcmp (buffer, "ABC") == 0);
177 g_message ("test_private: ok");
181 test_child_private (gchar *argv0)
183 GError *error = NULL;
187 gchar *child_argv[4];
194 g_assert (!g_file_test ("STOP", G_FILE_TEST_EXISTS));
197 write_or_die (filename, "ABC", -1);
198 map = map_or_die (filename, TRUE);
201 signal (SIGUSR1, handle_usr1);
204 g_snprintf (pid, sizeof(pid), "%d", getpid ());
205 child_argv[0] = argv0;
206 child_argv[1] = "mapchild";
208 child_argv[3] = NULL;
209 if (!g_spawn_async (dir, child_argv, NULL,
210 0, NULL, NULL, &child_pid, &error))
212 g_print ("failed to spawn child: %s\n",
218 loop = g_main_loop_new (NULL, FALSE);
219 g_idle_add (check_stop, loop);
220 g_main_loop_run (loop);
226 buffer = (gchar *)g_mapped_file_get_contents (map);
230 g_mapped_file_free (map);
233 kill (child_pid, SIGUSR1);
235 g_file_set_contents ("STOP", "Hey there\n", -1, NULL);
239 g_idle_add (check_stop, loop);
240 g_main_loop_run (loop);
245 if (!g_file_get_contents (childname, &buffer, &len, &error))
249 name = g_filename_display_name (childname);
250 g_print ("failed to read '%s': %s\n", name, error->message);
254 g_assert (strcmp (buffer, "ABC") == 0);
257 g_message ("test_child_private: ok");
261 parent_main (int argc,
264 /* test mapping with various flag combinations */
267 /* test private modification */
270 /* test multiple clients, non-shared */
271 test_child_private (argv[0]);
282 dir = g_get_current_dir ();
283 filename = g_build_filename (dir, "maptest", NULL);
284 displayname = g_filename_display_name (filename);
285 childname = g_build_filename (dir, "mapchild", NULL);
288 ret = child_main (argc, argv);
290 ret = parent_main (argc, argv);
294 g_free (displayname);