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.
21 #include <sys/types.h>
31 static gchar
*dir
, *filename
, *displayname
, *childname
;
33 static gboolean stop
= FALSE
;
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 child_main (int argc
, char *argv
[])
103 map
= map_or_die (filename
, FALSE
);
105 loop
= g_main_loop_new (NULL
, FALSE
);
108 signal (SIGUSR1
, handle_usr1
);
110 g_idle_add (check_stop
, loop
);
111 g_main_loop_run (loop
);
113 write_or_die (childname
,
114 g_mapped_file_get_contents (map
),
115 g_mapped_file_get_length (map
));
125 write_or_die (filename
, "ABC", -1);
127 map
= map_or_die (filename
, FALSE
);
128 g_assert (g_mapped_file_get_length (map
) == 3);
129 g_mapped_file_free (map
);
131 map
= map_or_die (filename
, TRUE
);
132 g_assert (g_mapped_file_get_length (map
) == 3);
133 g_mapped_file_free (map
);
139 GError
*error
= NULL
;
144 write_or_die (filename
, "ABC", -1);
145 map
= map_or_die (filename
, TRUE
);
147 buffer
= (gchar
*)g_mapped_file_get_contents (map
);
151 g_mapped_file_free (map
);
153 if (!g_file_get_contents (filename
, &buffer
, &len
, &error
))
155 g_print ("failed to read '%s': %s\n",
156 displayname
, error
->message
);
161 g_assert (strcmp (buffer
, "ABC") == 0);
167 test_child_private (gchar
*argv0
)
169 GError
*error
= NULL
;
173 gchar
*child_argv
[3];
178 g_assert (!g_file_test ("STOP", G_FILE_TEST_EXISTS
));
181 write_or_die (filename
, "ABC", -1);
182 map
= map_or_die (filename
, TRUE
);
184 child_argv
[0] = argv0
;
185 child_argv
[1] = "mapchild";
186 child_argv
[2] = NULL
;
187 if (!g_spawn_async (dir
, child_argv
, NULL
,
188 0, NULL
, NULL
, &child_pid
, &error
))
190 g_print ("failed to spawn child: %s\n",
195 /* give the child some time to set up its mapping */
198 buffer
= (gchar
*)g_mapped_file_get_contents (map
);
202 g_mapped_file_free (map
);
205 kill (child_pid
, SIGUSR1
);
207 g_file_set_contents ("STOP", "Hey there\n", -1, NULL
);
210 /* give the child some time to write the file */
213 if (!g_file_get_contents (childname
, &buffer
, &len
, &error
))
217 name
= g_filename_display_name (childname
);
218 g_print ("failed to read '%s': %s\n", name
, error
->message
);
222 g_assert (strcmp (buffer
, "ABC") == 0);
227 parent_main (int argc
,
230 /* test mapping with various flag combinations */
233 /* test private modification */
236 /* test multiple clients, non-shared */
237 test_child_private (argv
[0]);
248 dir
= g_get_current_dir ();
249 filename
= g_build_filename (dir
, "maptest", NULL
);
250 displayname
= g_filename_display_name (filename
);
251 childname
= g_build_filename (dir
, "mapchild", NULL
);
254 ret
= child_main (argc
, argv
);
256 ret
= parent_main (argc
, argv
);
260 g_free (displayname
);