2 * shared.c: Shared memory handling, and daemon launching
5 * Dick Porter (dick@ximian.com)
7 * (C) 2002-2006 Novell, Inc.
15 #include <sys/types.h>
21 #if defined(HAVE_SYS_SEM_H) && !(defined(__native_client__) && defined(__GLIBC__))
24 # define DISABLE_SHARED_HANDLES
27 #ifndef DISABLE_SHARED_HANDLES
28 # include <sys/mman.h>
30 # ifdef HAVE_SYS_UTSNAME_H
31 # include <sys/utsname.h>
35 #include <mono/io-layer/wapi.h>
36 #include <mono/io-layer/wapi-private.h>
37 #include <mono/io-layer/shared.h>
38 #include <mono/io-layer/handles-private.h>
41 //#define DEBUGLOG(...) g_message(__VA_ARGS__);
43 // Semaphores used when no-shared-memory use is in use
45 static mono_mutex_t noshm_sems[_WAPI_SHARED_SEM_COUNT];
47 static gpointer wapi_storage [16];
50 noshm_semaphores_init (void)
54 for (i = 0; i < _WAPI_SHARED_SEM_COUNT; i++)
55 mono_os_mutex_init (&noshm_sems [i]);
59 noshm_sem_lock (int sem)
63 DEBUGLOG ("%s: locking nosem %d", __func__, sem);
65 ret = mono_os_mutex_lock (&noshm_sems[sem]);
71 noshm_sem_trylock (int sem)
75 DEBUGLOG ("%s: trying to lock nosem %d", __func__, sem);
77 ret = mono_os_mutex_trylock (&noshm_sems[sem]);
83 noshm_sem_unlock (int sem)
87 DEBUGLOG ("%s: unlocking nosem %d", __func__, sem);
89 ret = mono_os_mutex_unlock (&noshm_sems[sem]);
94 #ifdef DISABLE_SHARED_HANDLES
96 _wapi_shm_semaphores_init (void)
98 noshm_semaphores_init ();
102 _wapi_shm_semaphores_remove (void)
108 _wapi_shm_sem_lock (int sem)
110 return noshm_sem_lock (sem);
114 _wapi_shm_sem_trylock (int sem)
116 return noshm_sem_trylock (sem);
120 _wapi_shm_sem_unlock (int sem)
122 return noshm_sem_unlock (sem);
126 _wapi_shm_attach (_wapi_shm_t type)
132 res = g_malloc0 (sizeof(struct _WapiHandleSharedLayout));
134 case WAPI_SHM_FILESHARE:
135 res = g_malloc0 (sizeof(struct _WapiFileShareLayout));
138 g_error ("Invalid type in _wapi_shm_attach ()");
142 wapi_storage [type] = res;
147 _wapi_shm_detach (_wapi_shm_t type)
149 g_free (wapi_storage [type]);
153 _wapi_shm_enabled_internal (void)
158 #else /* DISABLE_SHARED_HANDLES */
161 * Use POSIX shared memory if possible, it is simpler, and it has the advantage that
162 * writes to the shared area does not need to be written to disk, avoiding spinning up
163 * the disk every x secs on laptops.
169 static gboolean _wapi_shm_disabled = TRUE;
172 _wapi_shm_base_name (_wapi_shm_t type)
175 gchar machine_name[256];
176 const gchar *fake_name;
183 ubuf.machine[0] = '\0';
184 ubuf.sysname[0] = '\0';
186 g_strdelimit (ubuf.sysname, "/", '_');
187 g_strdelimit (ubuf.machine, "/", '_');
190 fake_name = g_getenv ("MONO_SHARED_HOSTNAME");
191 if (fake_name == NULL) {
192 if (gethostname(machine_name, sizeof(machine_name)) != 0)
193 machine_name[0] = '\0';
195 len = MIN (strlen (fake_name), sizeof (machine_name) - 1);
196 strncpy (machine_name, fake_name, len);
197 machine_name [len] = '\0';
202 name = g_strdup_printf ("shared_data-%s-%s-%s-%d-%d-%d",
203 machine_name, ubuf.sysname,
205 (int) sizeof(struct _WapiHandleShared),
206 _WAPI_HANDLE_VERSION, 0);
209 case WAPI_SHM_FILESHARE:
210 name = g_strdup_printf ("shared_fileshare-%s-%s-%s-%d-%d-%d",
211 machine_name, ubuf.sysname,
213 (int) sizeof(struct _WapiFileShare),
214 _WAPI_HANDLE_VERSION, 0);
223 static gchar *_wapi_shm_shm_name (_wapi_shm_t type)
225 char *base_name = _wapi_shm_base_name (type);
227 /* Also add the uid to avoid permission problems */
228 char *res = g_strdup_printf ("/mono-shared-%d-%s", getuid (), base_name);
236 _wapi_shm_open (const char *filename, int size)
240 fd = shm_open (filename, O_CREAT|O_RDWR, S_IRUSR|S_IWUSR|S_IRGRP);
242 /* Maybe /dev/shm is not mounted */
244 if (ftruncate (fd, size) != 0) {
245 perror ("_wapi_shm_open (): ftruncate ()");
246 g_assert_not_reached ();
255 _wapi_shm_file (_wapi_shm_t type)
257 static gchar file[_POSIX_PATH_MAX];
258 gchar *name = NULL, *filename;
259 const gchar *wapi_dir;
261 name = _wapi_shm_base_name (type);
263 /* I don't know how nfs affects mmap. If mmap() of files on
264 * nfs mounts breaks, then there should be an option to set
267 wapi_dir = g_getenv ("MONO_SHARED_DIR");
268 if (wapi_dir == NULL) {
269 filename = g_build_filename (g_get_home_dir (), ".wapi", name,
272 filename = g_build_filename (wapi_dir, ".wapi", name, NULL);
276 g_snprintf (file, _POSIX_PATH_MAX, "%s", filename);
283 _wapi_shm_file_open (const gchar *filename, guint32 wanted_size)
288 gboolean created = FALSE;
292 /* No need to check if the dir already exists or check
293 * mkdir() errors, because on any error the open() call will
294 * report the problem.
296 dir = g_path_get_dirname (filename);
304 } else if (tries > 5) {
305 /* Break out of a loop */
309 /* Make sure future processes can open the shared data files */
310 oldmask = umask (066);
312 /* No O_CREAT yet, because we need to initialise the file if
313 * we have to create it.
315 fd = open (filename, O_RDWR, 0600);
318 if (fd == -1 && errno == ENOENT) {
319 /* OK, its up to us to create it. O_EXCL to avoid a
320 * race condition where two processes can
321 * simultaneously try and create the file
323 oldmask = umask (066);
324 fd = open (filename, O_CREAT|O_EXCL|O_RDWR, 0600);
327 if (fd == -1 && errno == EEXIST) {
328 /* It's possible that the file was created in
329 * between finding it didn't exist, and trying
330 * to create it. Just try opening it again
333 } else if (fd == -1) {
334 g_critical ("%s: shared file [%s] open error: %s",
335 __func__, filename, g_strerror (errno));
338 /* We created the file, so we need to expand
341 * (wanted_size-1, because we're about to
342 * write the other byte to actually expand the
345 if (lseek (fd, wanted_size-1, SEEK_SET) == -1) {
346 g_critical ("%s: shared file [%s] lseek error: %s", __func__, filename, g_strerror (errno));
353 ret = write (fd, "", 1);
354 } while (ret == -1 && errno == EINTR);
357 g_critical ("%s: shared file [%s] write error: %s", __func__, filename, g_strerror (errno));
365 /* The contents of the file is set to all
366 * zero, because it is opened up with lseek,
367 * so we don't need to do any more
368 * initialisation here
371 } else if (fd == -1) {
372 g_critical ("%s: shared file [%s] open error: %s", __func__,
373 filename, g_strerror (errno));
377 /* Use stat to find the file size (instead of hard coding it)
378 * because we can expand the file later if needed (for more
379 * handles or scratch space.)
381 if (fstat (fd, &statbuf) == -1) {
382 g_critical ("%s: fstat error: %s", __func__,
384 if (created == TRUE) {
391 if (statbuf.st_size < wanted_size) {
393 if (created == TRUE) {
394 g_critical ("%s: shared file [%s] is not big enough! (found %ld, need %d bytes)", __func__, filename, (long)statbuf.st_size, wanted_size);
398 /* We didn't create it, so just try opening it again */
399 _wapi_handle_spin (100);
408 _wapi_shm_enabled_internal (void)
410 static gboolean env_checked;
413 if (g_getenv ("MONO_ENABLE_SHM"))
414 _wapi_shm_disabled = FALSE;
418 return !_wapi_shm_disabled;
423 * @success: Was it a success
425 * Attach to the shared memory file or create it if it did not exist.
426 * Returns the memory area the file was mmapped to.
429 _wapi_shm_attach (_wapi_shm_t type)
434 gchar *filename = _wapi_shm_file (type), *shm_name;
439 size = sizeof(struct _WapiHandleSharedLayout);
442 case WAPI_SHM_FILESHARE:
443 size = sizeof(struct _WapiFileShareLayout);
446 g_error ("Invalid type in _wapi_shm_attach ()");
450 if (!_wapi_shm_enabled ()) {
451 wapi_storage [type] = g_malloc0 (size);
452 return wapi_storage [type];
456 shm_name = _wapi_shm_shm_name (type);
457 fd = _wapi_shm_open (shm_name, size);
463 /* Fall back to files if POSIX shm fails (for example, because /dev/shm is not mounted */
465 fd = _wapi_shm_file_open (filename, size);
467 g_critical ("%s: shared file [%s] open error", __func__,
472 if (fstat (fd, &statbuf)==-1) {
473 g_critical ("%s: fstat error: %s", __func__,
479 shm_seg = mmap (NULL, statbuf.st_size, PROT_READ|PROT_WRITE,
481 if (shm_seg == MAP_FAILED) {
482 shm_seg = mmap (NULL, statbuf.st_size, PROT_READ|PROT_WRITE,
484 if (shm_seg == MAP_FAILED) {
485 g_critical ("%s: mmap error: %s", __func__, g_strerror (errno));
496 _wapi_shm_detach (_wapi_shm_t type)
498 if (!_wapi_shm_enabled ())
499 g_free (wapi_storage [type]);
503 shm_semaphores_init (void)
508 struct _WapiHandleSharedLayout *tmp_shared;
513 * Yet more barmy API - this union is a well-defined parameter
514 * in a syscall, yet I still have to define it here as it
515 * doesn't appear in a header
519 struct semid_ds *buf;
522 ushort def_vals[_WAPI_SHARED_SEM_COUNT];
526 for (i = 0; i < _WAPI_SHARED_SEM_COUNT; i++) {
531 * Process count must start at '0' - the 1 for all the others
532 * sets the semaphore to "unlocked"
534 def_vals[_WAPI_SHARED_SEM_PROCESS_COUNT] = 0;
536 defs.array = def_vals;
539 *Temporarily attach the shared data so we can read the
540 * semaphore key. We release this mapping and attach again
541 * after getting the semaphores to avoid a race condition
542 * where a terminating process can delete the shared files
543 * between a new process attaching the file and getting access
544 * to the semaphores (which increments the process count,
545 * preventing destruction of the shared data...)
547 tmp_shared = _wapi_shm_attach (WAPI_SHM_DATA);
548 g_assert (tmp_shared != NULL);
551 ftmp=_wapi_shm_shm_name (WAPI_SHM_DATA);
552 filename = g_build_filename ("/dev/shm", ftmp, NULL);
553 g_assert (filename!=NULL);
554 key = ftok (filename, 'M');
558 key = ftok ( _wapi_shm_file (WAPI_SHM_DATA), 'M');
563 oldkey = tmp_shared->sem_key;
566 DEBUGLOG ("%s: Creating with new key (0x%x)", __func__, key);
569 * The while loop attempts to make some sense of the
570 * bonkers 'think of a random number' method of
571 * picking a key without collision with other
574 while ((_wapi_sem_id = semget (key, _WAPI_SHARED_SEM_COUNT,
575 IPC_CREAT | IPC_EXCL | 0600)) == -1) {
576 if (errno == ENOMEM) {
577 g_error ("%s: semget error: %s", __func__,
579 } else if (errno == ENOSPC) {
580 g_error ("%s: semget error: %s. Try deleting some semaphores with ipcs and ipcrm\nor increase the maximum number of semaphore in the system.", __func__, g_strerror (errno));
581 } else if (errno != EEXIST) {
583 g_warning ("%s: semget error: %s key 0x%x - trying again", __func__,
584 g_strerror (errno), key);
588 DEBUGLOG ("%s: Got (%s), trying with new key (0x%x)", __func__, g_strerror (errno), key);
591 * Got a semaphore array, so initialise it and install
592 * the key into the shared memory
595 if (semctl (_wapi_sem_id, 0, SETALL, defs) == -1) {
597 g_warning ("%s: semctl init error: %s - trying again", __func__, g_strerror (errno));
600 * Something went horribly wrong, so try
601 * getting a new set from scratch
603 semctl (_wapi_sem_id, 0, IPC_RMID);
607 if (InterlockedCompareExchange (&tmp_shared->sem_key,
610 * Someone else created one and installed the
611 * key while we were working, so delete the
612 * array we created and fall through to the
613 * 'key already known' case.
615 semctl (_wapi_sem_id, 0, IPC_RMID);
616 oldkey = tmp_shared->sem_key;
619 * We've installed this semaphore set's key into
626 DEBUGLOG ("%s: Trying with old key 0x%x", __func__, oldkey);
628 _wapi_sem_id = semget (oldkey, _WAPI_SHARED_SEM_COUNT, 0600);
629 if (_wapi_sem_id == -1) {
631 g_warning ("%s: semget error opening old key 0x%x (%s) - trying again",
632 __func__, oldkey,g_strerror (errno));
635 * Someone must have deleted the semaphore set, so
636 * blow away the bad key and try again
638 InterlockedCompareExchange (&tmp_shared->sem_key, 0, oldkey);
644 /* Increment the usage count of this semaphore set */
645 thr_ret = _wapi_shm_sem_lock (_WAPI_SHARED_SEM_PROCESS_COUNT_LOCK);
646 g_assert (thr_ret == 0);
648 DEBUGLOG ("%s: Incrementing the process count (%d)", __func__, _wapi_getpid ());
651 * We only ever _unlock_ this semaphore, letting the kernel
652 * restore (ie decrement) this unlock when this process exits.
653 * We lock another semaphore around it so we can serialise
654 * access when we're testing the value of this semaphore when
655 * we exit cleanly, so we can delete the whole semaphore set.
657 _wapi_shm_sem_unlock (_WAPI_SHARED_SEM_PROCESS_COUNT);
659 DEBUGLOG ("%s: Process count is now %d (%d)", __func__, semctl (_wapi_sem_id, _WAPI_SHARED_SEM_PROCESS_COUNT, GETVAL), _wapi_getpid ());
661 _wapi_shm_sem_unlock (_WAPI_SHARED_SEM_PROCESS_COUNT_LOCK);
663 if (_wapi_shm_disabled)
666 munmap (tmp_shared, sizeof(struct _WapiHandleSharedLayout));
670 shm_semaphores_remove (void)
676 DEBUGLOG ("%s: Checking process count (%d)", __func__, _wapi_getpid ());
678 thr_ret = _wapi_shm_sem_lock (_WAPI_SHARED_SEM_PROCESS_COUNT_LOCK);
679 g_assert (thr_ret == 0);
681 proc_count = semctl (_wapi_sem_id, _WAPI_SHARED_SEM_PROCESS_COUNT,
684 g_assert (proc_count > 0);
685 if (proc_count == 1) {
687 * Just us, so blow away the semaphores and the shared
690 DEBUGLOG ("%s: Removing semaphores! (%d)", __func__, _wapi_getpid ());
692 semctl (_wapi_sem_id, 0, IPC_RMID);
694 shm_name = _wapi_shm_shm_name (WAPI_SHM_DATA);
695 shm_unlink (shm_name);
698 shm_name = _wapi_shm_shm_name (WAPI_SHM_FILESHARE);
699 shm_unlink (shm_name);
702 unlink (_wapi_shm_file (WAPI_SHM_DATA));
703 unlink (_wapi_shm_file (WAPI_SHM_FILESHARE));
706 * "else" clause, because there's no point unlocking
707 * the semaphore if we've just blown it away...
709 _wapi_shm_sem_unlock (_WAPI_SHARED_SEM_PROCESS_COUNT_LOCK);
714 shm_sem_lock (int sem)
719 DEBUGLOG ("%s: locking sem %d", __func__, sem);
723 ops.sem_flg = SEM_UNDO;
727 ret = semop (_wapi_sem_id, &ops, 1);
728 } while (ret == -1 && errno == EINTR);
732 * EINVAL covers the case when the semaphore was
733 * deleted before we started the semop
735 if (errno == EIDRM || errno == EINVAL) {
737 * Someone blew away this semaphore set, so
738 * get a new one and try again
740 DEBUGLOG ("%s: Reinitialising the semaphores!", __func__);
742 _wapi_shm_semaphores_init ();
746 /* Turn this into a pthreads-style return value */
750 DEBUGLOG ("%s: returning %d (%s)", __func__, ret, g_strerror (ret));
756 shm_sem_trylock (int sem)
761 DEBUGLOG ("%s: trying to lock sem %d", __func__, sem);
765 ops.sem_flg = IPC_NOWAIT | SEM_UNDO;
769 ret = semop (_wapi_sem_id, &ops, 1);
770 } while (ret == -1 && errno == EINTR);
774 * EINVAL covers the case when the semaphore was
775 * deleted before we started the semop
777 if (errno == EIDRM || errno == EINVAL) {
779 * Someone blew away this semaphore set, so
780 * get a new one and try again
782 DEBUGLOG ("%s: Reinitialising the semaphores!", __func__);
784 _wapi_shm_semaphores_init ();
788 /* Turn this into a pthreads-style return value */
793 /* But pthreads uses this code instead */
797 DEBUGLOG ("%s: returning %d (%s)", __func__, ret, g_strerror (ret));
803 shm_sem_unlock (int sem)
808 DEBUGLOG ("%s: unlocking sem %d", __func__, sem);
812 ops.sem_flg = SEM_UNDO;
816 ret = semop (_wapi_sem_id, &ops, 1);
817 } while (ret == -1 && errno == EINTR);
820 /* EINVAL covers the case when the semaphore was
821 * deleted before we started the semop
823 if (errno == EIDRM || errno == EINVAL) {
824 /* Someone blew away this semaphore set, so
825 * get a new one and try again (we can't just
826 * assume that the semaphore is now unlocked)
828 DEBUGLOG ("%s: Reinitialising the semaphores!", __func__);
830 _wapi_shm_semaphores_init ();
834 /* Turn this into a pthreads-style return value */
838 DEBUGLOG ("%s: returning %d (%s)", __func__, ret, g_strerror (ret));
844 _wapi_shm_semaphores_init (void)
846 if (!_wapi_shm_enabled ())
847 noshm_semaphores_init ();
849 shm_semaphores_init ();
853 _wapi_shm_semaphores_remove (void)
855 if (!_wapi_shm_disabled)
856 shm_semaphores_remove ();
860 _wapi_shm_sem_lock (int sem)
862 if (_wapi_shm_disabled)
863 return noshm_sem_lock (sem);
865 return shm_sem_lock (sem);
869 _wapi_shm_sem_trylock (int sem)
871 if (_wapi_shm_disabled)
872 return noshm_sem_trylock (sem);
874 return shm_sem_trylock (sem);
878 _wapi_shm_sem_unlock (int sem)
880 if (_wapi_shm_disabled)
881 return noshm_sem_unlock (sem);
883 return shm_sem_unlock (sem);
885 #endif /* !DISABLE_SHARED_HANDLES */