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 = NULL, *filename, *dir, *wapi_dir;
74 gchar machine_name[256];
76 if (gethostname(machine_name, sizeof(machine_name)) != 0)
77 machine_name[0] = '\0';
79 /* Change the filename whenever the format of the contents
82 if(type==WAPI_SHM_DATA) {
83 name=g_strdup_printf ("shared_data-%s-%d-%d",
84 machine_name, _WAPI_HANDLE_VERSION, segment);
85 } else if (type==WAPI_SHM_SCRATCH) {
86 name=g_strdup_printf ("shared_scratch-%s-%d-%d",
87 machine_name, _WAPI_HANDLE_VERSION, segment);
89 g_assert_not_reached ();
92 /* I don't know how nfs affects mmap. If mmap() of files on
93 * nfs mounts breaks, then there should be an option to set
96 wapi_dir=getenv ("MONO_SHARED_DIR");
98 filename=g_build_filename (g_get_home_dir (), ".wapi", name,
101 filename=g_build_filename (wapi_dir, ".wapi", name, NULL);
105 g_snprintf (file, _POSIX_PATH_MAX, "%s", filename);
107 /* No need to check if the dir already exists or check
108 * mkdir() errors, because on any error the open() call will
109 * report the problem.
111 dir=g_path_get_dirname (file);
118 gpointer _wapi_shm_file_expand (gpointer mem, _wapi_shm_t type,
119 guint32 segment, guint32 old_len,
124 guchar *filename=_wapi_shm_file (type, segment);
127 if(old_len>=new_len) {
131 munmap (mem, old_len);
133 fd=open (filename, O_RDWR, 0600);
135 g_critical (G_GNUC_PRETTY_FUNCTION
136 ": shared file [%s] open error: %s", filename,
141 if(lseek (fd, new_len-1, SEEK_SET)==-1) {
142 g_critical (G_GNUC_PRETTY_FUNCTION
143 ": shared file [%s] lseek error: %s", filename,
149 ret=write (fd, "", 1);
151 while (ret==-1 && errno==EINTR);
154 g_critical (G_GNUC_PRETTY_FUNCTION
155 ": shared file [%s] write error: %s", filename,
162 new_mem=_wapi_shm_file_map (type, segment, NULL, NULL);
167 static int _wapi_shm_file_open (const guchar *filename, _wapi_shm_t type,
172 guint32 wanted_size = 0;
179 if(type==WAPI_SHM_DATA) {
180 wanted_size=sizeof(struct _WapiHandleShared_list);
181 } else if (type==WAPI_SHM_SCRATCH) {
182 wanted_size=sizeof(struct _WapiHandleScratch) +
183 (_WAPI_SHM_SCRATCH_SIZE - MONO_ZERO_ARRAY_LENGTH);
185 g_assert_not_reached ();
189 /* No O_CREAT yet, because we need to initialise the file if
190 * we have to create it.
192 fd=open (filename, O_RDWR, 0600);
193 if(fd==-1 && errno==ENOENT) {
194 /* OK, its up to us to create it. O_EXCL to avoid a
195 * race condition where two processes can
196 * simultaneously try and create the file
198 fd=open (filename, O_CREAT|O_EXCL|O_RDWR, 0600);
199 if(fd==-1 && errno==EEXIST) {
200 /* It's possible that the file was created in
201 * between finding it didn't exist, and trying
202 * to create it. Just try opening it again
206 g_critical (G_GNUC_PRETTY_FUNCTION
207 ": shared file [%s] open error: %s",
208 filename, g_strerror (errno));
211 /* We created the file, so we need to expand
212 * the file and inform the caller so it can
213 * fork the handle daemon too.
215 * (wanted_size-1, because we're about to
216 * write the other byte to actually expand the
219 if(lseek (fd, wanted_size-1, SEEK_SET)==-1) {
220 g_critical (G_GNUC_PRETTY_FUNCTION ": shared file [%s] lseek error: %s", filename, g_strerror (errno));
227 ret=write (fd, "", 1);
229 while (ret==-1 && errno==EINTR);
232 g_critical (G_GNUC_PRETTY_FUNCTION ": shared file [%s] write error: %s", filename, g_strerror (errno));
242 /* The contents of the file is set to all
243 * zero, because it is opened up with lseek,
244 * so we don't need to do any more
245 * initialisation here
249 g_critical (G_GNUC_PRETTY_FUNCTION
250 ": shared file [%s] open error: %s", filename,
255 /* From now on, we need to delete the file before exiting on
256 * error if we created it (ie, if *created==TRUE)
259 /* Use stat to find the file size (instead of hard coding it)
260 * because we can expand the file later if needed (for more
261 * handles or scratch space.)
263 if(fstat (fd, &statbuf)==-1) {
264 g_critical (G_GNUC_PRETTY_FUNCTION ": fstat error: %s",
266 if(created && *created==TRUE) {
273 if(statbuf.st_size < wanted_size) {
275 if(created && *created==TRUE) {
276 #ifdef HAVE_LARGE_FILE_SUPPORT
277 /* Keep gcc quiet... */
278 g_critical (G_GNUC_PRETTY_FUNCTION ": shared file [%s] is not big enough! (found %lld, need %d bytes)", filename, statbuf.st_size, wanted_size);
280 g_critical (G_GNUC_PRETTY_FUNCTION ": shared file [%s] is not big enough! (found %ld, need %d bytes)", filename, statbuf.st_size, wanted_size);
285 /* We didn't create it, so just try opening it again */
293 gpointer _wapi_shm_file_map (_wapi_shm_t type, guint32 segment,
294 gboolean *created, off_t *size)
299 guchar *filename=_wapi_shm_file (type, segment);
301 fd=_wapi_shm_file_open (filename, type, created);
303 g_critical (G_GNUC_PRETTY_FUNCTION
304 ": shared file [%s] open error", filename);
308 if(fstat (fd, &statbuf)==-1) {
309 g_critical (G_GNUC_PRETTY_FUNCTION ": fstat error: %s",
315 *size=statbuf.st_size;
318 shm_seg=mmap (NULL, statbuf.st_size, PROT_READ|PROT_WRITE, MAP_SHARED,
320 if(shm_seg==MAP_FAILED) {
321 g_critical (G_GNUC_PRETTY_FUNCTION ": mmap error: %s",
333 * @success: Was it a success
335 * Attach to the shared memory file or create it if it did not
336 * exist. If it was created and daemon was FALSE a new daemon is
337 * forked into existence. Returns the memory area the file was mmapped
340 gboolean _wapi_shm_attach (struct _WapiHandleShared_list **data,
341 struct _WapiHandleScratch **scratch)
343 gboolean data_created=FALSE, scratch_created=FALSE;
344 off_t data_size, scratch_size;
345 int tries, closing_tries=0;
348 *data=_wapi_shm_file_map (WAPI_SHM_DATA, 0, &data_created, &data_size);
353 *scratch=_wapi_shm_file_map (WAPI_SHM_SCRATCH, 0, &scratch_created,
357 _wapi_shm_destroy ();
363 (*scratch)->data_len = scratch_size -
364 (sizeof(struct _WapiHandleScratch) - MONO_ZERO_ARRAY_LENGTH);
366 if(data_created==FALSE && (*data)->daemon_running==DAEMON_CLOSING) {
367 /* Daemon is closing down, give it a few ms and try
371 struct timespec sleepytime;
373 /* Something must have gone wrong, so delete the
374 * shared segments and try again.
376 _wapi_shm_destroy ();
378 munmap (*data, data_size);
379 munmap (*scratch, scratch_size);
381 if(closing_tries++ == 5) {
382 /* Still can't get going, so bail out */
383 g_warning ("The handle daemon is stuck closing");
388 sleepytime.tv_nsec=10000000; /* 10ms */
390 nanosleep (&sleepytime, NULL);
394 if(data_created==TRUE) {
396 /* _wapi_daemon_main() does not return */
397 _wapi_daemon_main (*data, *scratch);
399 /* But just in case... */
400 (*data)->daemon_running=DAEMON_DIED_AT_STARTUP;
407 g_critical (G_GNUC_PRETTY_FUNCTION ": fork error: %s",
409 _wapi_shm_destroy ();
417 /* FIXME: Set process title to something
421 /* Start the daemon with a clean sheet of file
424 for(i=3; i<getdtablesize (); i++) {
428 /* _wapi_daemon_main() does not return */
429 _wapi_daemon_main (*data, *scratch);
431 /* But just in case... */
432 (*data)->daemon_running=DAEMON_DIED_AT_STARTUP;
435 /* parent carries on */
437 g_message (G_GNUC_PRETTY_FUNCTION ": Daemon pid %d", pid);
439 #endif /* !VALGRINDING */
442 for(tries=0; (*data)->daemon_running==DAEMON_STARTING && tries < 100;
444 /* wait for the daemon to sort itself out. To be
445 * completely safe, we should have a timeout before
448 struct timespec sleepytime;
451 sleepytime.tv_nsec=10000000; /* 10ms */
453 nanosleep (&sleepytime, NULL);
455 if(tries==100 && (*data)->daemon_running==DAEMON_STARTING) {
456 /* Daemon didnt get going */
457 struct timespec sleepytime;
459 /* Something must have gone wrong, so delete the
460 * shared segments and try again.
462 _wapi_shm_destroy ();
464 /* Daemon didn't get going, give it a few ms and try
468 munmap (*data, data_size);
469 munmap (*scratch, scratch_size);
471 if(closing_tries++ == 5) {
472 /* Still can't get going, so bail out */
473 g_warning ("The handle daemon didnt start up properly");
478 sleepytime.tv_nsec=10000000; /* 10ms */
480 nanosleep (&sleepytime, NULL);
484 if((*data)->daemon_running==DAEMON_DIED_AT_STARTUP) {
485 /* Oh dear, the daemon had an error starting up */
486 if(data_created==TRUE) {
487 _wapi_shm_destroy ();
489 g_warning ("Handle daemon failed to start");
493 /* Do some sanity checking on the shared memory we
496 if(((*data)->daemon_running!=DAEMON_RUNNING) ||
497 #ifdef NEED_LINK_UNLINK
498 (strncmp ((*data)->daemon, "/tmp/mono-handle-daemon-",
501 (strncmp ((*data)->daemon+1, "mono-handle-daemon-", 19)!=0)) {
503 g_warning ("Shared memory sanity check failed.");
504 g_warning("status: %d", (*data)->daemon_running);
505 #ifdef NEED_LINK_UNLINK
506 g_warning("daemon: [%s]", (*data)->daemon);
508 g_warning("daemon: [%s]", (*data)->daemon+1);
513 /* From now on, it's up to the daemon to delete the shared
520 void _wapi_shm_destroy (void)
522 #ifndef DISABLE_SHARED_HANDLES
524 g_message (G_GNUC_PRETTY_FUNCTION ": unlinking shared data");
526 /* Only delete the first segments. The daemon will destroy
527 * any others when it exits
529 unlink (_wapi_shm_file (WAPI_SHM_DATA, 0));
530 unlink (_wapi_shm_file (WAPI_SHM_SCRATCH, 0));
531 #endif /* DISABLE_SHARED_HANDLES */