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>
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 #ifdef DISABLE_SHARED_HANDLES
48 gboolean _wapi_shm_disabled = TRUE;
50 gboolean _wapi_shm_disabled = FALSE;
54 noshm_semaphores_init (void)
58 for (i = 0; i < _WAPI_SHARED_SEM_COUNT; i++)
59 mono_mutex_init (&noshm_sems [i], NULL);
63 noshm_sem_lock (int sem)
67 DEBUGLOG ("%s: locking nosem %d", __func__, sem);
69 ret = mono_mutex_lock (&noshm_sems[sem]);
75 noshm_sem_trylock (int sem)
79 DEBUGLOG ("%s: trying to lock nosem %d", __func__, sem);
81 ret = mono_mutex_trylock (&noshm_sems[sem]);
87 noshm_sem_unlock (int sem)
91 DEBUGLOG ("%s: unlocking nosem %d", __func__, sem);
93 ret = mono_mutex_unlock (&noshm_sems[sem]);
98 #ifdef DISABLE_SHARED_HANDLES
100 _wapi_shm_semaphores_init (void)
102 noshm_semaphores_init ();
106 _wapi_shm_semaphores_remove (void)
112 _wapi_shm_sem_lock (int sem)
114 return noshm_sem_lock (sem);
118 _wapi_shm_sem_trylock (int sem)
120 return noshm_sem_trylock (sem);
124 _wapi_shm_sem_unlock (int sem)
126 return noshm_sem_unlock (sem);
130 _wapi_shm_attach (_wapi_shm_t type)
136 return g_malloc0 (sizeof(struct _WapiHandleSharedLayout));
138 case WAPI_SHM_FILESHARE:
139 return g_malloc0 (sizeof(struct _WapiFileShareLayout));
142 g_error ("Invalid type in _wapi_shm_attach ()");
148 * Use POSIX shared memory if possible, it is simpler, and it has the advantage that
149 * writes to the shared area does not need to be written to disk, avoiding spinning up
150 * the disk every x secs on laptops.
157 _wapi_shm_base_name (_wapi_shm_t type)
160 gchar machine_name[256];
161 const gchar *fake_name;
168 ubuf.machine[0] = '\0';
169 ubuf.sysname[0] = '\0';
171 g_strdelimit (ubuf.sysname, "/", '_');
172 g_strdelimit (ubuf.machine, "/", '_');
175 fake_name = g_getenv ("MONO_SHARED_HOSTNAME");
176 if (fake_name == NULL) {
177 if (gethostname(machine_name, sizeof(machine_name)) != 0)
178 machine_name[0] = '\0';
180 len = MIN (strlen (fake_name), sizeof (machine_name) - 1);
181 strncpy (machine_name, fake_name, len);
182 machine_name [len] = '\0';
187 name = g_strdup_printf ("shared_data-%s-%s-%s-%d-%d-%d",
188 machine_name, ubuf.sysname,
190 (int) sizeof(struct _WapiHandleShared),
191 _WAPI_HANDLE_VERSION, 0);
194 case WAPI_SHM_FILESHARE:
195 name = g_strdup_printf ("shared_fileshare-%s-%s-%s-%d-%d-%d",
196 machine_name, ubuf.sysname,
198 (int) sizeof(struct _WapiFileShare),
199 _WAPI_HANDLE_VERSION, 0);
208 static gchar *_wapi_shm_shm_name (_wapi_shm_t type)
210 char *base_name = _wapi_shm_base_name (type);
212 /* Also add the uid to avoid permission problems */
213 char *res = g_strdup_printf ("/mono-shared-%d-%s", getuid (), base_name);
221 _wapi_shm_open (const char *filename, int size)
225 fd = shm_open (filename, O_CREAT|O_RDWR, S_IRUSR|S_IWUSR|S_IRGRP);
227 /* Maybe /dev/shm is not mounted */
229 if (ftruncate (fd, size) != 0) {
230 perror ("_wapi_shm_open (): ftruncate ()");
231 g_assert_not_reached ();
240 _wapi_shm_file (_wapi_shm_t type)
242 static gchar file[_POSIX_PATH_MAX];
243 gchar *name = NULL, *filename, *dir, *wapi_dir;
245 name = _wapi_shm_base_name (type);
247 /* I don't know how nfs affects mmap. If mmap() of files on
248 * nfs mounts breaks, then there should be an option to set
251 wapi_dir = getenv ("MONO_SHARED_DIR");
252 if (wapi_dir == NULL) {
253 filename = g_build_filename (g_get_home_dir (), ".wapi", name,
256 filename = g_build_filename (wapi_dir, ".wapi", name, NULL);
260 g_snprintf (file, _POSIX_PATH_MAX, "%s", filename);
263 /* No need to check if the dir already exists or check
264 * mkdir() errors, because on any error the open() call will
265 * report the problem.
267 dir = g_path_get_dirname (file);
275 _wapi_shm_file_open (const gchar *filename, guint32 wanted_size)
280 gboolean created = FALSE;
287 } else if (tries > 5) {
288 /* Break out of a loop */
292 /* Make sure future processes can open the shared data files */
293 oldmask = umask (066);
295 /* No O_CREAT yet, because we need to initialise the file if
296 * we have to create it.
298 fd = open (filename, O_RDWR, 0600);
301 if (fd == -1 && errno == ENOENT) {
302 /* OK, its up to us to create it. O_EXCL to avoid a
303 * race condition where two processes can
304 * simultaneously try and create the file
306 oldmask = umask (066);
307 fd = open (filename, O_CREAT|O_EXCL|O_RDWR, 0600);
310 if (fd == -1 && errno == EEXIST) {
311 /* It's possible that the file was created in
312 * between finding it didn't exist, and trying
313 * to create it. Just try opening it again
316 } else if (fd == -1) {
317 g_critical ("%s: shared file [%s] open error: %s",
318 __func__, filename, g_strerror (errno));
321 /* We created the file, so we need to expand
324 * (wanted_size-1, because we're about to
325 * write the other byte to actually expand the
328 if (lseek (fd, wanted_size-1, SEEK_SET) == -1) {
329 g_critical ("%s: shared file [%s] lseek error: %s", __func__, filename, g_strerror (errno));
336 ret = write (fd, "", 1);
337 } while (ret == -1 && errno == EINTR);
340 g_critical ("%s: shared file [%s] write error: %s", __func__, filename, g_strerror (errno));
348 /* The contents of the file is set to all
349 * zero, because it is opened up with lseek,
350 * so we don't need to do any more
351 * initialisation here
354 } else if (fd == -1) {
355 g_critical ("%s: shared file [%s] open error: %s", __func__,
356 filename, g_strerror (errno));
360 /* Use stat to find the file size (instead of hard coding it)
361 * because we can expand the file later if needed (for more
362 * handles or scratch space.)
364 if (fstat (fd, &statbuf) == -1) {
365 g_critical ("%s: fstat error: %s", __func__,
367 if (created == TRUE) {
374 if (statbuf.st_size < wanted_size) {
376 if (created == TRUE) {
377 g_critical ("%s: shared file [%s] is not big enough! (found %ld, need %d bytes)", __func__, filename, (long)statbuf.st_size, wanted_size);
381 /* We didn't create it, so just try opening it again */
382 _wapi_handle_spin (100);
391 check_disabled (void)
393 if (_wapi_shm_disabled || g_getenv ("MONO_DISABLE_SHM")) {
394 const char* val = g_getenv ("MONO_DISABLE_SHM");
395 if (val == NULL || *val == '1' || *val == 'y' || *val == 'Y') {
396 _wapi_shm_disabled = TRUE;
400 return _wapi_shm_disabled;
405 * @success: Was it a success
407 * Attach to the shared memory file or create it if it did not exist.
408 * Returns the memory area the file was mmapped to.
411 _wapi_shm_attach (_wapi_shm_t type)
416 gchar *filename = _wapi_shm_file (type), *shm_name;
421 size = sizeof(struct _WapiHandleSharedLayout);
424 case WAPI_SHM_FILESHARE:
425 size = sizeof(struct _WapiFileShareLayout);
428 g_error ("Invalid type in _wapi_shm_attach ()");
432 if (check_disabled ()) {
433 return g_malloc0 (size);
437 shm_name = _wapi_shm_shm_name (type);
438 fd = _wapi_shm_open (shm_name, size);
444 /* Fall back to files if POSIX shm fails (for example, because /dev/shm is not mounted */
446 fd = _wapi_shm_file_open (filename, size);
448 g_critical ("%s: shared file [%s] open error", __func__,
453 if (fstat (fd, &statbuf)==-1) {
454 g_critical ("%s: fstat error: %s", __func__,
460 shm_seg = mmap (NULL, statbuf.st_size, PROT_READ|PROT_WRITE,
462 if (shm_seg == MAP_FAILED) {
463 shm_seg = mmap (NULL, statbuf.st_size, PROT_READ|PROT_WRITE,
465 if (shm_seg == MAP_FAILED) {
466 g_critical ("%s: mmap error: %s", __func__, g_strerror (errno));
477 shm_semaphores_init (void)
482 struct _WapiHandleSharedLayout *tmp_shared;
485 * Yet more barmy API - this union is a well-defined parameter
486 * in a syscall, yet I still have to define it here as it
487 * doesn't appear in a header
491 struct semid_ds *buf;
494 ushort def_vals[_WAPI_SHARED_SEM_COUNT];
498 for (i = 0; i < _WAPI_SHARED_SEM_COUNT; i++) {
503 * Process count must start at '0' - the 1 for all the others
504 * sets the semaphore to "unlocked"
506 def_vals[_WAPI_SHARED_SEM_PROCESS_COUNT] = 0;
508 defs.array = def_vals;
511 *Temporarily attach the shared data so we can read the
512 * semaphore key. We release this mapping and attach again
513 * after getting the semaphores to avoid a race condition
514 * where a terminating process can delete the shared files
515 * between a new process attaching the file and getting access
516 * to the semaphores (which increments the process count,
517 * preventing destruction of the shared data...)
519 tmp_shared = _wapi_shm_attach (WAPI_SHM_DATA);
520 g_assert (tmp_shared != NULL);
522 key = ftok (_wapi_shm_file (WAPI_SHM_DATA), 'M');
526 oldkey = tmp_shared->sem_key;
529 DEBUGLOG ("%s: Creating with new key (0x%x)", __func__, key);
532 * The while loop attempts to make some sense of the
533 * bonkers 'think of a random number' method of
534 * picking a key without collision with other
537 while ((_wapi_sem_id = semget (key, _WAPI_SHARED_SEM_COUNT,
538 IPC_CREAT | IPC_EXCL | 0600)) == -1) {
539 if (errno == ENOMEM) {
540 g_error ("%s: semget error: %s", __func__,
542 } else if (errno == ENOSPC) {
543 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));
544 } else if (errno != EEXIST) {
546 g_warning ("%s: semget error: %s key 0x%x - trying again", __func__,
547 g_strerror (errno), key);
551 DEBUGLOG ("%s: Got (%s), trying with new key (0x%x)", __func__, g_strerror (errno), key);
554 * Got a semaphore array, so initialise it and install
555 * the key into the shared memory
558 if (semctl (_wapi_sem_id, 0, SETALL, defs) == -1) {
560 g_warning ("%s: semctl init error: %s - trying again", __func__, g_strerror (errno));
563 * Something went horribly wrong, so try
564 * getting a new set from scratch
566 semctl (_wapi_sem_id, 0, IPC_RMID);
570 if (InterlockedCompareExchange (&tmp_shared->sem_key,
573 * Someone else created one and installed the
574 * key while we were working, so delete the
575 * array we created and fall through to the
576 * 'key already known' case.
578 semctl (_wapi_sem_id, 0, IPC_RMID);
579 oldkey = tmp_shared->sem_key;
582 * We've installed this semaphore set's key into
589 DEBUGLOG ("%s: Trying with old key 0x%x", __func__, oldkey);
591 _wapi_sem_id = semget (oldkey, _WAPI_SHARED_SEM_COUNT, 0600);
592 if (_wapi_sem_id == -1) {
594 g_warning ("%s: semget error opening old key 0x%x (%s) - trying again",
595 __func__, oldkey,g_strerror (errno));
598 * Someone must have deleted the semaphore set, so
599 * blow away the bad key and try again
601 InterlockedCompareExchange (&tmp_shared->sem_key, 0, oldkey);
607 /* Increment the usage count of this semaphore set */
608 thr_ret = _wapi_shm_sem_lock (_WAPI_SHARED_SEM_PROCESS_COUNT_LOCK);
609 g_assert (thr_ret == 0);
611 DEBUGLOG ("%s: Incrementing the process count (%d)", __func__, _wapi_getpid ());
614 * We only ever _unlock_ this semaphore, letting the kernel
615 * restore (ie decrement) this unlock when this process exits.
616 * We lock another semaphore around it so we can serialise
617 * access when we're testing the value of this semaphore when
618 * we exit cleanly, so we can delete the whole semaphore set.
620 _wapi_shm_sem_unlock (_WAPI_SHARED_SEM_PROCESS_COUNT);
622 DEBUGLOG ("%s: Process count is now %d (%d)", __func__, semctl (_wapi_sem_id, _WAPI_SHARED_SEM_PROCESS_COUNT, GETVAL), _wapi_getpid ());
624 _wapi_shm_sem_unlock (_WAPI_SHARED_SEM_PROCESS_COUNT_LOCK);
626 if (_wapi_shm_disabled)
629 munmap (tmp_shared, sizeof(struct _WapiHandleSharedLayout));
633 shm_semaphores_remove (void)
639 DEBUGLOG ("%s: Checking process count (%d)", __func__, _wapi_getpid ());
641 thr_ret = _wapi_shm_sem_lock (_WAPI_SHARED_SEM_PROCESS_COUNT_LOCK);
642 g_assert (thr_ret == 0);
644 proc_count = semctl (_wapi_sem_id, _WAPI_SHARED_SEM_PROCESS_COUNT,
647 g_assert (proc_count > 0);
648 if (proc_count == 1) {
650 * Just us, so blow away the semaphores and the shared
653 DEBUGLOG ("%s: Removing semaphores! (%d)", __func__, _wapi_getpid ());
655 semctl (_wapi_sem_id, 0, IPC_RMID);
657 shm_name = _wapi_shm_shm_name (WAPI_SHM_DATA);
658 shm_unlink (shm_name);
661 shm_name = _wapi_shm_shm_name (WAPI_SHM_FILESHARE);
662 shm_unlink (shm_name);
665 unlink (_wapi_shm_file (WAPI_SHM_DATA));
666 unlink (_wapi_shm_file (WAPI_SHM_FILESHARE));
669 * "else" clause, because there's no point unlocking
670 * the semaphore if we've just blown it away...
672 _wapi_shm_sem_unlock (_WAPI_SHARED_SEM_PROCESS_COUNT_LOCK);
677 shm_sem_lock (int sem)
682 DEBUGLOG ("%s: locking sem %d", __func__, sem);
686 ops.sem_flg = SEM_UNDO;
690 ret = semop (_wapi_sem_id, &ops, 1);
691 } while (ret == -1 && errno == EINTR);
695 * EINVAL covers the case when the semaphore was
696 * deleted before we started the semop
698 if (errno == EIDRM || errno == EINVAL) {
700 * Someone blew away this semaphore set, so
701 * get a new one and try again
703 DEBUGLOG ("%s: Reinitialising the semaphores!", __func__);
705 _wapi_shm_semaphores_init ();
709 /* Turn this into a pthreads-style return value */
713 DEBUGLOG ("%s: returning %d (%s)", __func__, ret, g_strerror (ret));
719 shm_sem_trylock (int sem)
724 DEBUGLOG ("%s: trying to lock sem %d", __func__, sem);
728 ops.sem_flg = IPC_NOWAIT | SEM_UNDO;
732 ret = semop (_wapi_sem_id, &ops, 1);
733 } while (ret == -1 && errno == EINTR);
737 * EINVAL covers the case when the semaphore was
738 * deleted before we started the semop
740 if (errno == EIDRM || errno == EINVAL) {
742 * Someone blew away this semaphore set, so
743 * get a new one and try again
745 DEBUGLOG ("%s: Reinitialising the semaphores!", __func__);
747 _wapi_shm_semaphores_init ();
751 /* Turn this into a pthreads-style return value */
756 /* But pthreads uses this code instead */
760 DEBUGLOG ("%s: returning %d (%s)", __func__, ret, g_strerror (ret));
766 shm_sem_unlock (int sem)
771 DEBUGLOG ("%s: unlocking sem %d", __func__, sem);
775 ops.sem_flg = SEM_UNDO;
779 ret = semop (_wapi_sem_id, &ops, 1);
780 } while (ret == -1 && errno == EINTR);
783 /* EINVAL covers the case when the semaphore was
784 * deleted before we started the semop
786 if (errno == EIDRM || errno == EINVAL) {
787 /* Someone blew away this semaphore set, so
788 * get a new one and try again (we can't just
789 * assume that the semaphore is now unlocked)
791 DEBUGLOG ("%s: Reinitialising the semaphores!", __func__);
793 _wapi_shm_semaphores_init ();
797 /* Turn this into a pthreads-style return value */
801 DEBUGLOG ("%s: returning %d (%s)", __func__, ret, g_strerror (ret));
807 _wapi_shm_semaphores_init (void)
809 if (check_disabled ())
810 noshm_semaphores_init ();
812 shm_semaphores_init ();
816 _wapi_shm_semaphores_remove (void)
818 if (!_wapi_shm_disabled)
819 shm_semaphores_remove ();
823 _wapi_shm_sem_lock (int sem)
825 if (_wapi_shm_disabled)
826 return noshm_sem_lock (sem);
828 return shm_sem_lock (sem);
832 _wapi_shm_sem_trylock (int sem)
834 if (_wapi_shm_disabled)
835 return noshm_sem_trylock (sem);
837 return shm_sem_trylock (sem);
841 _wapi_shm_sem_unlock (int sem)
843 if (_wapi_shm_disabled)
844 return noshm_sem_unlock (sem);
846 return shm_sem_unlock (sem);
848 #endif /* !DISABLE_SHARED_HANDLES */