2 * shared.c: Shared memory handling, and daemon launching
5 * Dick Porter (dick@ximian.com)
7 * (C) 2002 Ximian, Inc.
11 * Code to support inter-process sharing of handles.
13 * I thought of using an mmap()ed file for this. If linuxthreads
14 * supported PTHREAD_PROCESS_SHARED I would have done; however without
15 * that pthread support the only other inter-process IPC
16 * synchronisation option is a sysV semaphore, and if I'm going to use
17 * that I may as well take advantage of sysV shared memory too.
18 * Actually, semaphores seem to be buggy, or I was using them
19 * incorrectly :-). I've replaced the sysV semaphore with a shared
20 * integer controlled with Interlocked functions. And I've since
21 * replaced that with a separate process to serialise access to the
22 * shared memory, to avoid the possibility of DOS by leaving the
23 * shared memory locked, and also to allow the shared memory to be
26 * mmap() files have the advantage of avoiding namespace collisions,
27 * but have the disadvantage of needing cleaning up, and also msync().
28 * sysV shared memory has a really stupid way of getting random key
29 * IDs, which can lead to collisions.
31 * Having tried sysv shm, I tested mmap() and found that MAP_SHARED
32 * makes msync() irrelevent, and both types need cleaning up. Seeing
33 * as mmap() doesn't suffer from the bonkers method of allocating
34 * segments, it seems to be the best method.
36 * This shared memory is needed because w32 processes do not have the
37 * POSIX parent-child relationship, so a process handle is available
38 * to any other process to find out exit status. Handles are
39 * destroyed when the last reference to them is closed. New handles
40 * can be created for long lasting items such as processes or threads,
41 * and also for named synchronisation objects so long as these haven't
42 * been deleted by having the last referencing handle closed.
52 #include <sys/types.h>
57 #include <mono/io-layer/wapi.h>
58 #include <mono/io-layer/wapi-private.h>
59 #include <mono/io-layer/shared.h>
60 #include <mono/io-layer/daemon-private.h>
64 /* Define this to make it easier to run valgrind on the daemon. Then
65 * the first process to start will turn into a daemon without forking
66 * (the debug utility mono/handles/hps is ideal for this.)
70 guchar *_wapi_shm_file (_wapi_shm_t type, guint32 segment)
72 static guchar file[_POSIX_PATH_MAX];
73 guchar *name, *filename, *dir, *wapi_dir;
75 /* Change the filename whenever the format of the contents
78 if(type==WAPI_SHM_DATA) {
79 name=g_strdup_printf ("shared_data-%d-%d",
80 _WAPI_HANDLE_VERSION, segment);
81 } else if (type==WAPI_SHM_SCRATCH) {
82 name=g_strdup_printf ("shared_scratch-%d-%d",
83 _WAPI_HANDLE_VERSION, segment);
85 g_assert_not_reached ();
88 /* I don't know how nfs affects mmap. If mmap() of files on
89 * nfs mounts breaks, then there should be an option to set
92 wapi_dir=getenv ("MONO_SHARED_DIR");
94 filename=g_build_filename (g_get_home_dir (), ".wapi", name,
97 filename=g_build_filename (wapi_dir, ".wapi", name, NULL);
101 g_snprintf (file, _POSIX_PATH_MAX, "%s", filename);
103 /* No need to check if the dir already exists or check
104 * mkdir() errors, because on any error the open() call will
105 * report the problem.
107 dir=g_path_get_dirname (file);
114 gpointer _wapi_shm_file_expand (gpointer mem, _wapi_shm_t type,
115 guint32 segment, guint32 old_len,
120 guchar *filename=_wapi_shm_file (type, segment);
122 if(old_len>=new_len) {
126 munmap (mem, old_len);
128 fd=open (filename, O_RDWR, 0600);
130 g_critical (G_GNUC_PRETTY_FUNCTION
131 ": shared file [%s] open error: %s", filename,
136 if(lseek (fd, new_len-1, SEEK_SET)==-1) {
137 g_critical (G_GNUC_PRETTY_FUNCTION
138 ": shared file [%s] lseek error: %s", filename,
143 if(write (fd, "", 1)==-1) {
144 g_critical (G_GNUC_PRETTY_FUNCTION
145 ": shared file [%s] write error: %s", filename,
151 new_mem=_wapi_shm_file_map (type, segment, NULL);
156 static int _wapi_shm_file_open (const guchar *filename, _wapi_shm_t type,
167 if(type==WAPI_SHM_DATA) {
168 wanted_size=sizeof(struct _WapiHandleShared_list);
169 } else if (type==WAPI_SHM_SCRATCH) {
170 wanted_size=sizeof(struct _WapiHandleScratch);
172 g_assert_not_reached ();
176 /* No O_CREAT yet, because we need to initialise the file if
177 * we have to create it.
179 fd=open (filename, O_RDWR, 0600);
180 if(fd==-1 && errno==ENOENT) {
181 /* OK, its up to us to create it. O_EXCL to avoid a
182 * race condition where two processes can
183 * simultaneously try and create the file
185 fd=open (filename, O_CREAT|O_EXCL|O_RDWR, 0600);
186 if(fd==-1 && errno==EEXIST) {
187 /* It's possible that the file was created in
188 * between finding it didn't exist, and trying
189 * to create it. Just try opening it again
193 g_critical (G_GNUC_PRETTY_FUNCTION
194 ": shared file [%s] open error: %s",
195 filename, g_strerror (errno));
198 /* We created the file, so we need to expand
199 * the file and inform the caller so it can
200 * fork the handle daemon too.
202 * (wanted_size-1, because we're about to
203 * write the other byte to actually expand the
206 if(lseek (fd, wanted_size-1, SEEK_SET)==-1) {
207 g_critical (G_GNUC_PRETTY_FUNCTION ": shared file [%s] lseek error: %s", filename, g_strerror (errno));
213 if(write (fd, "", 1)==-1) {
214 g_critical (G_GNUC_PRETTY_FUNCTION ": shared file [%s] write error: %s", filename, g_strerror (errno));
224 /* The contents of the file is set to all
225 * zero, because it is opened up with lseek,
226 * so we don't need to do any more
227 * initialisation here
231 g_critical (G_GNUC_PRETTY_FUNCTION
232 ": shared file [%s] open error: %s", filename,
237 /* From now on, we need to delete the file before exiting on
238 * error if we created it (ie, if *created==TRUE)
241 /* Use stat to find the file size (instead of hard coding it)
242 * because we can expand the file later if needed (for more
243 * handles or scratch space.)
245 if(fstat (fd, &statbuf)==-1) {
246 g_critical (G_GNUC_PRETTY_FUNCTION ": fstat error: %s",
255 if(statbuf.st_size < wanted_size) {
256 #ifdef HAVE_LARGE_FILE_SUPPORT
257 /* Keep gcc quiet... */
258 g_critical (G_GNUC_PRETTY_FUNCTION ": shared file [%s] is not big enough! (found %lld, need %d bytes)", filename, statbuf.st_size, wanted_size);
260 g_critical (G_GNUC_PRETTY_FUNCTION ": shared file [%s] is not big enough! (found %ld, need %d bytes)", filename, statbuf.st_size, wanted_size);
272 gpointer _wapi_shm_file_map (_wapi_shm_t type, guint32 segment,
278 guchar *filename=_wapi_shm_file (type, segment);
280 fd=_wapi_shm_file_open (filename, type, created);
282 g_critical (G_GNUC_PRETTY_FUNCTION
283 ": shared file [%s] open error", filename);
287 if(fstat (fd, &statbuf)==-1) {
288 g_critical (G_GNUC_PRETTY_FUNCTION ": fstat error: %s",
294 shm_seg=mmap (NULL, statbuf.st_size, PROT_READ|PROT_WRITE, MAP_SHARED,
296 if(shm_seg==MAP_FAILED) {
297 g_critical (G_GNUC_PRETTY_FUNCTION ": mmap error: %s",
309 * @success: Was it a success
311 * Attach to the shared memory file or create it if it did not
312 * exist. If it was created and daemon was FALSE a new daemon is
313 * forked into existence. Returns the memory area the file was mmapped
316 gboolean _wapi_shm_attach (struct _WapiHandleShared_list **data,
317 struct _WapiHandleScratch **scratch)
319 gboolean data_created=FALSE, scratch_created=FALSE;
322 *data=_wapi_shm_file_map (WAPI_SHM_DATA, 0, &data_created);
327 *scratch=_wapi_shm_file_map (WAPI_SHM_SCRATCH, 0, &scratch_created);
330 _wapi_shm_destroy ();
335 if(data_created==TRUE) {
337 /* _wapi_daemon_main() does not return */
338 _wapi_daemon_main (*data, *scratch);
340 /* But just in case... */
341 (*data)->daemon_running=DAEMON_DIED_AT_STARTUP;
348 g_critical (G_GNUC_PRETTY_FUNCTION ": fork error: %s",
350 _wapi_shm_destroy ();
358 /* FIXME: Set process title to something
362 /* Start the daemon with a clean sheet of file
365 for(i=3; i<getdtablesize (); i++) {
369 /* _wapi_daemon_main() does not return */
370 _wapi_daemon_main (*data, *scratch);
372 /* But just in case... */
373 (*data)->daemon_running=DAEMON_DIED_AT_STARTUP;
376 /* parent carries on */
378 g_message (G_GNUC_PRETTY_FUNCTION ": Daemon pid %d", pid);
380 #endif /* !VALGRINDING */
382 /* Do some sanity checking on the shared memory we
385 if(!((*data)->daemon_running==DAEMON_STARTING ||
386 (*data)->daemon_running==DAEMON_RUNNING ||
387 (*data)->daemon_running==DAEMON_DIED_AT_STARTUP) ||
388 #ifdef NEED_LINK_UNLINK
389 (strncmp ((*data)->daemon, "/tmp/mono-handle-daemon-",
392 (strncmp ((*data)->daemon+1, "mono-handle-daemon-", 19)!=0)) {
394 g_warning ("Shared memory sanity check failed.");
399 for(tries=0; (*data)->daemon_running==DAEMON_STARTING && tries < 100;
401 /* wait for the daemon to sort itself out. To be
402 * completely safe, we should have a timeout before
405 struct timespec sleepytime;
408 sleepytime.tv_nsec=10000000; /* 10ms */
410 nanosleep (&sleepytime, NULL);
412 if(tries==100 && (*data)->daemon_running==DAEMON_STARTING) {
413 /* Daemon didnt get going */
414 if(data_created==TRUE) {
415 _wapi_shm_destroy ();
417 g_warning ("The handle daemon didnt start up properly");
421 if((*data)->daemon_running==DAEMON_DIED_AT_STARTUP) {
422 /* Oh dear, the daemon had an error starting up */
423 if(data_created==TRUE) {
424 _wapi_shm_destroy ();
426 g_warning ("Handle daemon failed to start");
430 /* From now on, it's up to the daemon to delete the shared
437 void _wapi_shm_destroy (void)
439 #ifndef DISABLE_SHARED_HANDLES
441 g_message (G_GNUC_PRETTY_FUNCTION ": unlinking shared data");
443 /* Only delete the first segments. The daemon will destroy
444 * any others when it exits
446 unlink (_wapi_shm_file (WAPI_SHM_DATA, 0));
447 unlink (_wapi_shm_file (WAPI_SHM_SCRATCH, 0));
448 #endif /* DISABLE_SHARED_HANDLES */