2 * shared.c: Shared memory handling, and daemon launching
5 * Dick Porter (dick@ximian.com)
7 * (C) 2002-2006 Novell, Inc.
17 #include <sys/types.h>
25 #define DISABLE_SHARED_HANDLES
28 #ifdef HAVE_SYS_UTSNAME_H
29 #include <sys/utsname.h>
32 #include <mono/io-layer/wapi.h>
33 #include <mono/io-layer/wapi-private.h>
34 #include <mono/io-layer/shared.h>
35 #include <mono/io-layer/handles-private.h>
38 * Use POSIX shared memory if possible, it is simpler, and it has the advantage that
39 * writes to the shared area does not need to be written to disk, avoiding spinning up
40 * the disk every x secs on laptops.
48 #ifdef DISABLE_SHARED_HANDLES
49 gboolean _wapi_shm_disabled = TRUE;
51 gboolean _wapi_shm_disabled = FALSE;
55 _wapi_shm_base_name (_wapi_shm_t type)
58 gchar machine_name[256];
59 const gchar *fake_name;
66 ubuf.machine[0] = '\0';
67 ubuf.sysname[0] = '\0';
69 g_strdelimit (ubuf.sysname, "/", '_');
70 g_strdelimit (ubuf.machine, "/", '_');
73 fake_name = g_getenv ("MONO_SHARED_HOSTNAME");
74 if (fake_name == NULL) {
75 if (gethostname(machine_name, sizeof(machine_name)) != 0)
76 machine_name[0] = '\0';
78 len = MIN (strlen (fake_name), sizeof (machine_name) - 1);
79 strncpy (machine_name, fake_name, len);
80 machine_name [len] = '\0';
85 name = g_strdup_printf ("shared_data-%s-%s-%s-%d-%d-%d",
86 machine_name, ubuf.sysname,
88 (int) sizeof(struct _WapiHandleShared),
89 _WAPI_HANDLE_VERSION, 0);
92 case WAPI_SHM_FILESHARE:
93 name = g_strdup_printf ("shared_fileshare-%s-%s-%s-%d-%d-%d",
94 machine_name, ubuf.sysname,
96 (int) sizeof(struct _WapiFileShare),
97 _WAPI_HANDLE_VERSION, 0);
106 static gchar *_wapi_shm_shm_name (_wapi_shm_t type)
108 char *base_name = _wapi_shm_base_name (type);
110 /* Also add the uid to avoid permission problems */
111 char *res = g_strdup_printf ("/mono-shared-%d-%s", getuid (), base_name);
119 _wapi_shm_open (const char *filename, int size)
123 fd = shm_open (filename, O_CREAT|O_RDWR, S_IRUSR|S_IWUSR|S_IRGRP);
125 /* Maybe /dev/shm is not mounted */
127 if (ftruncate (fd, size) != 0) {
128 perror ("_wapi_shm_open (): ftruncate ()");
129 g_assert_not_reached ();
137 static gchar *_wapi_shm_file (_wapi_shm_t type)
139 static gchar file[_POSIX_PATH_MAX];
140 gchar *name = NULL, *filename, *dir, *wapi_dir;
142 name = _wapi_shm_base_name (type);
144 /* I don't know how nfs affects mmap. If mmap() of files on
145 * nfs mounts breaks, then there should be an option to set
148 wapi_dir = getenv ("MONO_SHARED_DIR");
149 if (wapi_dir == NULL) {
150 filename = g_build_filename (g_get_home_dir (), ".wapi", name,
153 filename = g_build_filename (wapi_dir, ".wapi", name, NULL);
157 g_snprintf (file, _POSIX_PATH_MAX, "%s", filename);
160 /* No need to check if the dir already exists or check
161 * mkdir() errors, because on any error the open() call will
162 * report the problem.
164 dir = g_path_get_dirname (file);
171 static int _wapi_shm_file_open (const gchar *filename, guint32 wanted_size)
176 gboolean created = FALSE;
183 } else if (tries > 5) {
184 /* Break out of a loop */
188 /* Make sure future processes can open the shared data files */
189 oldmask = umask (066);
191 /* No O_CREAT yet, because we need to initialise the file if
192 * we have to create it.
194 fd = open (filename, O_RDWR, 0600);
197 if (fd == -1 && errno == ENOENT) {
198 /* OK, its up to us to create it. O_EXCL to avoid a
199 * race condition where two processes can
200 * simultaneously try and create the file
202 oldmask = umask (066);
203 fd = open (filename, O_CREAT|O_EXCL|O_RDWR, 0600);
206 if (fd == -1 && errno == EEXIST) {
207 /* It's possible that the file was created in
208 * between finding it didn't exist, and trying
209 * to create it. Just try opening it again
212 } else if (fd == -1) {
213 g_critical ("%s: shared file [%s] open error: %s",
214 __func__, filename, g_strerror (errno));
217 /* We created the file, so we need to expand
220 * (wanted_size-1, because we're about to
221 * write the other byte to actually expand the
224 if (lseek (fd, wanted_size-1, SEEK_SET) == -1) {
225 g_critical ("%s: shared file [%s] lseek error: %s", __func__, filename, g_strerror (errno));
232 ret = write (fd, "", 1);
233 } while (ret == -1 && errno == EINTR);
236 g_critical ("%s: shared file [%s] write error: %s", __func__, filename, g_strerror (errno));
244 /* The contents of the file is set to all
245 * zero, because it is opened up with lseek,
246 * so we don't need to do any more
247 * initialisation here
250 } else if (fd == -1) {
251 g_critical ("%s: shared file [%s] open error: %s", __func__,
252 filename, g_strerror (errno));
256 /* Use stat to find the file size (instead of hard coding it)
257 * because we can expand the file later if needed (for more
258 * handles or scratch space.)
260 if (fstat (fd, &statbuf) == -1) {
261 g_critical ("%s: fstat error: %s", __func__,
263 if (created == TRUE) {
270 if (statbuf.st_size < wanted_size) {
272 if (created == TRUE) {
273 g_critical ("%s: shared file [%s] is not big enough! (found %ld, need %d bytes)", __func__, filename, (long)statbuf.st_size, wanted_size);
277 /* We didn't create it, so just try opening it again */
278 _wapi_handle_spin (100);
286 static gboolean check_disabled (void)
288 if (_wapi_shm_disabled || g_getenv ("MONO_DISABLE_SHM")) {
289 const char* val = g_getenv ("MONO_DISABLE_SHM");
290 if (val == NULL || *val == '1' || *val == 'y' || *val == 'Y') {
291 _wapi_shm_disabled = TRUE;
295 return(_wapi_shm_disabled);
300 * @success: Was it a success
302 * Attach to the shared memory file or create it if it did not exist.
303 * Returns the memory area the file was mmapped to.
305 gpointer _wapi_shm_attach (_wapi_shm_t type)
310 gchar *filename = _wapi_shm_file (type), *shm_name;
315 size = sizeof(struct _WapiHandleSharedLayout);
318 case WAPI_SHM_FILESHARE:
319 size = sizeof(struct _WapiFileShareLayout);
322 g_error ("Invalid type in _wapi_shm_attach ()");
326 if (check_disabled ()) {
327 return g_malloc0 (size);
331 shm_name = _wapi_shm_shm_name (type);
332 fd = _wapi_shm_open (shm_name, size);
338 /* Fall back to files if POSIX shm fails (for example, because /dev/shm is not mounted */
340 fd = _wapi_shm_file_open (filename, size);
342 g_critical ("%s: shared file [%s] open error", __func__,
347 if (fstat (fd, &statbuf)==-1) {
348 g_critical ("%s: fstat error: %s", __func__,
354 shm_seg = mmap (NULL, statbuf.st_size, PROT_READ|PROT_WRITE,
356 if (shm_seg == MAP_FAILED) {
357 shm_seg = mmap (NULL, statbuf.st_size, PROT_READ|PROT_WRITE,
359 if (shm_seg == MAP_FAILED) {
360 g_critical ("%s: mmap error: %s", __func__, g_strerror (errno));
371 static void shm_semaphores_init (void)
376 struct _WapiHandleSharedLayout *tmp_shared;
378 /* Yet more barmy API - this union is a well-defined parameter
379 * in a syscall, yet I still have to define it here as it
380 * doesn't appear in a header
384 struct semid_ds *buf;
387 ushort def_vals[_WAPI_SHARED_SEM_COUNT];
391 for (i = 0; i < _WAPI_SHARED_SEM_COUNT; i++) {
395 /* Process count must start at '0' - the 1 for all the others
396 * sets the semaphore to "unlocked"
398 def_vals[_WAPI_SHARED_SEM_PROCESS_COUNT] = 0;
400 defs.array = def_vals;
402 /* Temporarily attach the shared data so we can read the
403 * semaphore key. We release this mapping and attach again
404 * after getting the semaphores to avoid a race condition
405 * where a terminating process can delete the shared files
406 * between a new process attaching the file and getting access
407 * to the semaphores (which increments the process count,
408 * preventing destruction of the shared data...)
410 tmp_shared = _wapi_shm_attach (WAPI_SHM_DATA);
411 g_assert (tmp_shared != NULL);
413 key = ftok (_wapi_shm_file (WAPI_SHM_DATA), 'M');
417 oldkey = tmp_shared->sem_key;
421 g_message ("%s: Creating with new key (0x%x)", __func__, key);
424 /* The while loop attempts to make some sense of the
425 * bonkers 'think of a random number' method of
426 * picking a key without collision with other
429 while ((_wapi_sem_id = semget (key, _WAPI_SHARED_SEM_COUNT,
430 IPC_CREAT | IPC_EXCL | 0600)) == -1) {
431 if (errno == ENOMEM) {
432 g_error ("%s: semget error: %s", __func__,
434 } else if (errno == ENOSPC) {
435 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));
436 } else if (errno != EEXIST) {
438 g_warning ("%s: semget error: %s key 0x%x - trying again", __func__,
439 g_strerror (errno), key);
444 g_message ("%s: Got (%s), trying with new key (0x%x)",
445 __func__, g_strerror (errno), key);
448 /* Got a semaphore array, so initialise it and install
449 * the key into the shared memory
452 if (semctl (_wapi_sem_id, 0, SETALL, defs) == -1) {
454 g_warning ("%s: semctl init error: %s - trying again", __func__, g_strerror (errno));
456 /* Something went horribly wrong, so try
457 * getting a new set from scratch
459 semctl (_wapi_sem_id, 0, IPC_RMID);
463 if (InterlockedCompareExchange (&tmp_shared->sem_key,
465 /* Someone else created one and installed the
466 * key while we were working, so delete the
467 * array we created and fall through to the
468 * 'key already known' case.
470 semctl (_wapi_sem_id, 0, IPC_RMID);
471 oldkey = tmp_shared->sem_key;
473 /* We've installed this semaphore set's key into
481 g_message ("%s: Trying with old key 0x%x", __func__, oldkey);
484 _wapi_sem_id = semget (oldkey, _WAPI_SHARED_SEM_COUNT, 0600);
485 if (_wapi_sem_id == -1) {
487 g_warning ("%s: semget error opening old key 0x%x (%s) - trying again",
488 __func__, oldkey,g_strerror (errno));
490 /* Someone must have deleted the semaphore set, so
491 * blow away the bad key and try again
493 InterlockedCompareExchange (&tmp_shared->sem_key, 0, oldkey);
499 /* Increment the usage count of this semaphore set */
500 thr_ret = _wapi_shm_sem_lock (_WAPI_SHARED_SEM_PROCESS_COUNT_LOCK);
501 g_assert (thr_ret == 0);
504 g_message ("%s: Incrementing the process count (%d)", __func__, _wapi_getpid ());
507 /* We only ever _unlock_ this semaphore, letting the kernel
508 * restore (ie decrement) this unlock when this process exits.
509 * We lock another semaphore around it so we can serialise
510 * access when we're testing the value of this semaphore when
511 * we exit cleanly, so we can delete the whole semaphore set.
513 _wapi_shm_sem_unlock (_WAPI_SHARED_SEM_PROCESS_COUNT);
516 g_message ("%s: Process count is now %d (%d)", __func__, semctl (_wapi_sem_id, _WAPI_SHARED_SEM_PROCESS_COUNT, GETVAL), _wapi_getpid ());
519 _wapi_shm_sem_unlock (_WAPI_SHARED_SEM_PROCESS_COUNT_LOCK);
521 if (_wapi_shm_disabled)
524 munmap (tmp_shared, sizeof(struct _WapiHandleSharedLayout));
528 static mono_mutex_t noshm_sems[_WAPI_SHARED_SEM_COUNT];
530 static void noshm_semaphores_init (void)
534 for (i = 0; i < _WAPI_SHARED_SEM_COUNT; i++) {
535 mono_mutex_init (&noshm_sems[i], NULL);
540 static void shm_semaphores_remove (void)
547 g_message ("%s: Checking process count (%d)", __func__,
551 thr_ret = _wapi_shm_sem_lock (_WAPI_SHARED_SEM_PROCESS_COUNT_LOCK);
552 g_assert (thr_ret == 0);
554 proc_count = semctl (_wapi_sem_id, _WAPI_SHARED_SEM_PROCESS_COUNT,
557 g_assert (proc_count > 0);
558 if (proc_count == 1) {
559 /* Just us, so blow away the semaphores and the shared
563 g_message ("%s: Removing semaphores! (%d)", __func__,
567 semctl (_wapi_sem_id, 0, IPC_RMID);
569 shm_name = _wapi_shm_shm_name (WAPI_SHM_DATA);
570 shm_unlink (shm_name);
573 shm_name = _wapi_shm_shm_name (WAPI_SHM_FILESHARE);
574 shm_unlink (shm_name);
577 unlink (_wapi_shm_file (WAPI_SHM_DATA));
578 unlink (_wapi_shm_file (WAPI_SHM_FILESHARE));
580 /* "else" clause, because there's no point unlocking
581 * the semaphore if we've just blown it away...
583 _wapi_shm_sem_unlock (_WAPI_SHARED_SEM_PROCESS_COUNT_LOCK);
588 static void noshm_semaphores_remove (void)
590 /* No need to do anything */
594 static int shm_sem_lock (int sem)
600 g_message ("%s: locking sem %d", __func__, sem);
605 ops.sem_flg = SEM_UNDO;
609 ret = semop (_wapi_sem_id, &ops, 1);
610 } while (ret == -1 && errno == EINTR);
613 /* EINVAL covers the case when the semaphore was
614 * deleted before we started the semop
616 if (errno == EIDRM || errno == EINVAL) {
617 /* Someone blew away this semaphore set, so
618 * get a new one and try again
621 g_message ("%s: Reinitialising the semaphores!",
625 _wapi_shm_semaphores_init ();
629 /* Turn this into a pthreads-style return value */
634 g_message ("%s: returning %d (%s)", __func__, ret, g_strerror (ret));
641 static int noshm_sem_lock (int sem)
646 g_message ("%s: locking nosem %d", __func__, sem);
649 ret = mono_mutex_lock (&noshm_sems[sem]);
655 static int shm_sem_trylock (int sem)
661 g_message ("%s: trying to lock sem %d", __func__, sem);
666 ops.sem_flg = IPC_NOWAIT | SEM_UNDO;
670 ret = semop (_wapi_sem_id, &ops, 1);
671 } while (ret == -1 && errno == EINTR);
674 /* EINVAL covers the case when the semaphore was
675 * deleted before we started the semop
677 if (errno == EIDRM || errno == EINVAL) {
678 /* Someone blew away this semaphore set, so
679 * get a new one and try again
682 g_message ("%s: Reinitialising the semaphores!",
686 _wapi_shm_semaphores_init ();
690 /* Turn this into a pthreads-style return value */
695 /* But pthreads uses this code instead */
700 g_message ("%s: returning %d (%s)", __func__, ret, g_strerror (ret));
707 static int noshm_sem_trylock (int sem)
712 g_message ("%s: trying to lock nosem %d", __func__, sem);
715 ret = mono_mutex_trylock (&noshm_sems[sem]);
721 static int shm_sem_unlock (int sem)
727 g_message ("%s: unlocking sem %d", __func__, sem);
732 ops.sem_flg = SEM_UNDO;
736 ret = semop (_wapi_sem_id, &ops, 1);
737 } while (ret == -1 && errno == EINTR);
740 /* EINVAL covers the case when the semaphore was
741 * deleted before we started the semop
743 if (errno == EIDRM || errno == EINVAL) {
744 /* Someone blew away this semaphore set, so
745 * get a new one and try again (we can't just
746 * assume that the semaphore is now unlocked)
749 g_message ("%s: Reinitialising the semaphores!",
753 _wapi_shm_semaphores_init ();
757 /* Turn this into a pthreads-style return value */
762 g_message ("%s: returning %d (%s)", __func__, ret, g_strerror (ret));
769 static int noshm_sem_unlock (int sem)
774 g_message ("%s: unlocking nosem %d", __func__, sem);
777 ret = mono_mutex_unlock (&noshm_sems[sem]);
782 void _wapi_shm_semaphores_init (void)
785 noshm_semaphores_init ();
787 if (check_disabled ()) {
788 noshm_semaphores_init ();
790 shm_semaphores_init ();
795 void _wapi_shm_semaphores_remove (void)
798 noshm_semaphores_remove ();
800 if (_wapi_shm_disabled) {
801 noshm_semaphores_remove ();
803 shm_semaphores_remove ();
808 int _wapi_shm_sem_lock (int sem)
811 return(noshm_sem_lock (sem));
813 if (_wapi_shm_disabled) {
814 return(noshm_sem_lock (sem));
816 return(shm_sem_lock (sem));
821 int _wapi_shm_sem_trylock (int sem)
824 return(noshm_sem_trylock (sem));
826 if (_wapi_shm_disabled) {
827 return(noshm_sem_trylock (sem));
829 return(shm_sem_trylock (sem));
834 int _wapi_shm_sem_unlock (int sem)
837 return(noshm_sem_unlock (sem));
839 if (_wapi_shm_disabled) {
840 return(noshm_sem_unlock (sem));
842 return(shm_sem_unlock (sem));