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;
36 handle_usr1 (int signum)
44 check_stop (gpointer data)
46 GMainLoop *loop = data;
49 stop = g_file_test ("STOP", G_FILE_TEST_EXISTS);
53 g_main_loop_quit (loop);
59 write_or_die (const gchar *filename,
60 const gchar *contents,
66 if (!g_file_set_contents (filename, contents, length, &error))
68 displayname = g_filename_display_name (childname);
69 g_print ("failed to write '%s': %s\n",
70 displayname, error->message);
76 map_or_die (const gchar *filename,
83 map = g_mapped_file_new (filename, writable, &error);
86 displayname = g_filename_display_name (childname);
87 g_print ("failed to map '%s' non-writable, shared: %s\n",
88 displayname, error->message);
96 child_main (int argc, char *argv[])
101 map = map_or_die (filename, FALSE);
103 loop = g_main_loop_new (NULL, FALSE);
106 signal (SIGUSR1, handle_usr1);
108 g_idle_add (check_stop, loop);
109 g_main_loop_run (loop);
111 write_or_die (childname,
112 g_mapped_file_get_contents (map),
113 g_mapped_file_get_length (map));
123 write_or_die (filename, "ABC", -1);
125 map = map_or_die (filename, FALSE);
126 g_assert (g_mapped_file_get_length (map) == 3);
127 g_mapped_file_free (map);
129 map = map_or_die (filename, TRUE);
130 g_assert (g_mapped_file_get_length (map) == 3);
131 g_mapped_file_free (map);
137 GError *error = NULL;
142 write_or_die (filename, "ABC", -1);
143 map = map_or_die (filename, TRUE);
145 buffer = (gchar *)g_mapped_file_get_contents (map);
149 g_mapped_file_free (map);
151 if (!g_file_get_contents (filename, &buffer, &len, &error))
153 g_print ("failed to read '%s': %s\n",
154 displayname, error->message);
159 g_assert (strcmp (buffer, "ABC") == 0);
165 test_child_private (gchar *argv0)
167 GError *error = NULL;
171 gchar *child_argv[3];
176 g_assert (!g_file_test ("STOP", G_FILE_TEST_EXISTS));
179 write_or_die (filename, "ABC", -1);
180 map = map_or_die (filename, TRUE);
182 child_argv[0] = argv0;
183 child_argv[1] = "mapchild";
184 child_argv[2] = NULL;
185 if (!g_spawn_async (dir, child_argv, NULL,
186 0, NULL, NULL, &child_pid, &error))
188 g_print ("failed to spawn child: %s\n",
193 /* give the child some time to set up its mapping */
196 buffer = (gchar *)g_mapped_file_get_contents (map);
200 g_mapped_file_free (map);
203 kill (child_pid, SIGUSR1);
205 g_file_set_contents ("STOP", "Hey there\n", -1, NULL);
208 /* give the child some time to write the file */
211 if (!g_file_get_contents (childname, &buffer, &len, &error))
215 name = g_filename_display_name (childname);
216 g_print ("failed to read '%s': %s\n", name, error->message);
220 g_assert (strcmp (buffer, "ABC") == 0);
225 parent_main (int argc,
228 /* test mapping with various flag combinations */
231 /* test private modification */
234 /* test multiple clients, non-shared */
235 test_child_private (argv[0]);
246 dir = g_get_current_dir ();
247 filename = g_build_filename (dir, "maptest", NULL);
248 displayname = g_filename_display_name (filename);
249 childname = g_build_filename (dir, "mapchild", NULL);
252 ret = child_main (argc, argv);
254 ret = parent_main (argc, argv);
258 g_free (displayname);