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 gboolean _wapi_shm_disabled
= TRUE
;
49 static gpointer wapi_storage
[16];
52 noshm_semaphores_init (void)
56 for (i
= 0; i
< _WAPI_SHARED_SEM_COUNT
; i
++)
57 mono_mutex_init (&noshm_sems
[i
], NULL
);
61 noshm_sem_lock (int sem
)
65 DEBUGLOG ("%s: locking nosem %d", __func__
, sem
);
67 ret
= mono_mutex_lock (&noshm_sems
[sem
]);
73 noshm_sem_trylock (int sem
)
77 DEBUGLOG ("%s: trying to lock nosem %d", __func__
, sem
);
79 ret
= mono_mutex_trylock (&noshm_sems
[sem
]);
85 noshm_sem_unlock (int sem
)
89 DEBUGLOG ("%s: unlocking nosem %d", __func__
, sem
);
91 ret
= mono_mutex_unlock (&noshm_sems
[sem
]);
96 #ifdef DISABLE_SHARED_HANDLES
98 _wapi_shm_semaphores_init (void)
100 noshm_semaphores_init ();
104 _wapi_shm_semaphores_remove (void)
110 _wapi_shm_sem_lock (int sem
)
112 return noshm_sem_lock (sem
);
116 _wapi_shm_sem_trylock (int sem
)
118 return noshm_sem_trylock (sem
);
122 _wapi_shm_sem_unlock (int sem
)
124 return noshm_sem_unlock (sem
);
128 _wapi_shm_attach (_wapi_shm_t type
)
134 res
= g_malloc0 (sizeof(struct _WapiHandleSharedLayout
));
136 case WAPI_SHM_FILESHARE
:
137 res
= g_malloc0 (sizeof(struct _WapiFileShareLayout
));
140 g_error ("Invalid type in _wapi_shm_attach ()");
144 wapi_storage
[type
] = res
;
149 _wapi_shm_detach (_wapi_shm_t type
)
151 g_free (wapi_storage
[type
]);
155 _wapi_shm_enabled (void)
162 * Use POSIX shared memory if possible, it is simpler, and it has the advantage that
163 * writes to the shared area does not need to be written to disk, avoiding spinning up
164 * the disk every x secs on laptops.
171 _wapi_shm_base_name (_wapi_shm_t type
)
174 gchar machine_name
[256];
175 const gchar
*fake_name
;
182 ubuf
.machine
[0] = '\0';
183 ubuf
.sysname
[0] = '\0';
185 g_strdelimit (ubuf
.sysname
, "/", '_');
186 g_strdelimit (ubuf
.machine
, "/", '_');
189 fake_name
= g_getenv ("MONO_SHARED_HOSTNAME");
190 if (fake_name
== NULL
) {
191 if (gethostname(machine_name
, sizeof(machine_name
)) != 0)
192 machine_name
[0] = '\0';
194 len
= MIN (strlen (fake_name
), sizeof (machine_name
) - 1);
195 strncpy (machine_name
, fake_name
, len
);
196 machine_name
[len
] = '\0';
201 name
= g_strdup_printf ("shared_data-%s-%s-%s-%d-%d-%d",
202 machine_name
, ubuf
.sysname
,
204 (int) sizeof(struct _WapiHandleShared
),
205 _WAPI_HANDLE_VERSION
, 0);
208 case WAPI_SHM_FILESHARE
:
209 name
= g_strdup_printf ("shared_fileshare-%s-%s-%s-%d-%d-%d",
210 machine_name
, ubuf
.sysname
,
212 (int) sizeof(struct _WapiFileShare
),
213 _WAPI_HANDLE_VERSION
, 0);
222 static gchar
*_wapi_shm_shm_name (_wapi_shm_t type
)
224 char *base_name
= _wapi_shm_base_name (type
);
226 /* Also add the uid to avoid permission problems */
227 char *res
= g_strdup_printf ("/mono-shared-%d-%s", getuid (), base_name
);
235 _wapi_shm_open (const char *filename
, int size
)
239 fd
= shm_open (filename
, O_CREAT
|O_RDWR
, S_IRUSR
|S_IWUSR
|S_IRGRP
);
241 /* Maybe /dev/shm is not mounted */
243 if (ftruncate (fd
, size
) != 0) {
244 perror ("_wapi_shm_open (): ftruncate ()");
245 g_assert_not_reached ();
254 _wapi_shm_file (_wapi_shm_t type
)
256 static gchar file
[_POSIX_PATH_MAX
];
257 gchar
*name
= NULL
, *filename
, *wapi_dir
;
259 name
= _wapi_shm_base_name (type
);
261 /* I don't know how nfs affects mmap. If mmap() of files on
262 * nfs mounts breaks, then there should be an option to set
265 wapi_dir
= getenv ("MONO_SHARED_DIR");
266 if (wapi_dir
== NULL
) {
267 filename
= g_build_filename (g_get_home_dir (), ".wapi", name
,
270 filename
= g_build_filename (wapi_dir
, ".wapi", name
, NULL
);
274 g_snprintf (file
, _POSIX_PATH_MAX
, "%s", filename
);
281 _wapi_shm_file_open (const gchar
*filename
, guint32 wanted_size
)
286 gboolean created
= FALSE
;
290 /* No need to check if the dir already exists or check
291 * mkdir() errors, because on any error the open() call will
292 * report the problem.
294 dir
= g_path_get_dirname (filename
);
302 } else if (tries
> 5) {
303 /* Break out of a loop */
307 /* Make sure future processes can open the shared data files */
308 oldmask
= umask (066);
310 /* No O_CREAT yet, because we need to initialise the file if
311 * we have to create it.
313 fd
= open (filename
, O_RDWR
, 0600);
316 if (fd
== -1 && errno
== ENOENT
) {
317 /* OK, its up to us to create it. O_EXCL to avoid a
318 * race condition where two processes can
319 * simultaneously try and create the file
321 oldmask
= umask (066);
322 fd
= open (filename
, O_CREAT
|O_EXCL
|O_RDWR
, 0600);
325 if (fd
== -1 && errno
== EEXIST
) {
326 /* It's possible that the file was created in
327 * between finding it didn't exist, and trying
328 * to create it. Just try opening it again
331 } else if (fd
== -1) {
332 g_critical ("%s: shared file [%s] open error: %s",
333 __func__
, filename
, g_strerror (errno
));
336 /* We created the file, so we need to expand
339 * (wanted_size-1, because we're about to
340 * write the other byte to actually expand the
343 if (lseek (fd
, wanted_size
-1, SEEK_SET
) == -1) {
344 g_critical ("%s: shared file [%s] lseek error: %s", __func__
, filename
, g_strerror (errno
));
351 ret
= write (fd
, "", 1);
352 } while (ret
== -1 && errno
== EINTR
);
355 g_critical ("%s: shared file [%s] write error: %s", __func__
, filename
, g_strerror (errno
));
363 /* The contents of the file is set to all
364 * zero, because it is opened up with lseek,
365 * so we don't need to do any more
366 * initialisation here
369 } else if (fd
== -1) {
370 g_critical ("%s: shared file [%s] open error: %s", __func__
,
371 filename
, g_strerror (errno
));
375 /* Use stat to find the file size (instead of hard coding it)
376 * because we can expand the file later if needed (for more
377 * handles or scratch space.)
379 if (fstat (fd
, &statbuf
) == -1) {
380 g_critical ("%s: fstat error: %s", __func__
,
382 if (created
== TRUE
) {
389 if (statbuf
.st_size
< wanted_size
) {
391 if (created
== TRUE
) {
392 g_critical ("%s: shared file [%s] is not big enough! (found %ld, need %d bytes)", __func__
, filename
, (long)statbuf
.st_size
, wanted_size
);
396 /* We didn't create it, so just try opening it again */
397 _wapi_handle_spin (100);
406 _wapi_shm_enabled (void)
408 static gboolean env_checked
;
411 if (g_getenv ("MONO_ENABLE_SHM"))
412 _wapi_shm_disabled
= FALSE
;
416 return !_wapi_shm_disabled
;
421 * @success: Was it a success
423 * Attach to the shared memory file or create it if it did not exist.
424 * Returns the memory area the file was mmapped to.
427 _wapi_shm_attach (_wapi_shm_t type
)
432 gchar
*filename
= _wapi_shm_file (type
), *shm_name
;
437 size
= sizeof(struct _WapiHandleSharedLayout
);
440 case WAPI_SHM_FILESHARE
:
441 size
= sizeof(struct _WapiFileShareLayout
);
444 g_error ("Invalid type in _wapi_shm_attach ()");
448 if (!_wapi_shm_enabled ()) {
449 wapi_storage
[type
] = g_malloc0 (size
);
450 return wapi_storage
[type
];
454 shm_name
= _wapi_shm_shm_name (type
);
455 fd
= _wapi_shm_open (shm_name
, size
);
461 /* Fall back to files if POSIX shm fails (for example, because /dev/shm is not mounted */
463 fd
= _wapi_shm_file_open (filename
, size
);
465 g_critical ("%s: shared file [%s] open error", __func__
,
470 if (fstat (fd
, &statbuf
)==-1) {
471 g_critical ("%s: fstat error: %s", __func__
,
477 shm_seg
= mmap (NULL
, statbuf
.st_size
, PROT_READ
|PROT_WRITE
,
479 if (shm_seg
== MAP_FAILED
) {
480 shm_seg
= mmap (NULL
, statbuf
.st_size
, PROT_READ
|PROT_WRITE
,
482 if (shm_seg
== MAP_FAILED
) {
483 g_critical ("%s: mmap error: %s", __func__
, g_strerror (errno
));
494 _wapi_shm_detach (_wapi_shm_t type
)
496 if (!_wapi_shm_enabled ())
497 g_free (wapi_storage
[type
]);
501 shm_semaphores_init (void)
506 struct _WapiHandleSharedLayout
*tmp_shared
;
511 * Yet more barmy API - this union is a well-defined parameter
512 * in a syscall, yet I still have to define it here as it
513 * doesn't appear in a header
517 struct semid_ds
*buf
;
520 ushort def_vals
[_WAPI_SHARED_SEM_COUNT
];
524 for (i
= 0; i
< _WAPI_SHARED_SEM_COUNT
; i
++) {
529 * Process count must start at '0' - the 1 for all the others
530 * sets the semaphore to "unlocked"
532 def_vals
[_WAPI_SHARED_SEM_PROCESS_COUNT
] = 0;
534 defs
.array
= def_vals
;
537 *Temporarily attach the shared data so we can read the
538 * semaphore key. We release this mapping and attach again
539 * after getting the semaphores to avoid a race condition
540 * where a terminating process can delete the shared files
541 * between a new process attaching the file and getting access
542 * to the semaphores (which increments the process count,
543 * preventing destruction of the shared data...)
545 tmp_shared
= _wapi_shm_attach (WAPI_SHM_DATA
);
546 g_assert (tmp_shared
!= NULL
);
549 ftmp
=_wapi_shm_shm_name (WAPI_SHM_DATA
);
550 filename
= g_build_filename ("/dev/shm", ftmp
, NULL
);
551 g_assert (filename
!=NULL
);
552 key
= ftok (filename
, 'M');
556 key
= ftok ( _wapi_shm_file (WAPI_SHM_DATA
), 'M');
561 oldkey
= tmp_shared
->sem_key
;
564 DEBUGLOG ("%s: Creating with new key (0x%x)", __func__
, key
);
567 * The while loop attempts to make some sense of the
568 * bonkers 'think of a random number' method of
569 * picking a key without collision with other
572 while ((_wapi_sem_id
= semget (key
, _WAPI_SHARED_SEM_COUNT
,
573 IPC_CREAT
| IPC_EXCL
| 0600)) == -1) {
574 if (errno
== ENOMEM
) {
575 g_error ("%s: semget error: %s", __func__
,
577 } else if (errno
== ENOSPC
) {
578 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
));
579 } else if (errno
!= EEXIST
) {
581 g_warning ("%s: semget error: %s key 0x%x - trying again", __func__
,
582 g_strerror (errno
), key
);
586 DEBUGLOG ("%s: Got (%s), trying with new key (0x%x)", __func__
, g_strerror (errno
), key
);
589 * Got a semaphore array, so initialise it and install
590 * the key into the shared memory
593 if (semctl (_wapi_sem_id
, 0, SETALL
, defs
) == -1) {
595 g_warning ("%s: semctl init error: %s - trying again", __func__
, g_strerror (errno
));
598 * Something went horribly wrong, so try
599 * getting a new set from scratch
601 semctl (_wapi_sem_id
, 0, IPC_RMID
);
605 if (InterlockedCompareExchange (&tmp_shared
->sem_key
,
608 * Someone else created one and installed the
609 * key while we were working, so delete the
610 * array we created and fall through to the
611 * 'key already known' case.
613 semctl (_wapi_sem_id
, 0, IPC_RMID
);
614 oldkey
= tmp_shared
->sem_key
;
617 * We've installed this semaphore set's key into
624 DEBUGLOG ("%s: Trying with old key 0x%x", __func__
, oldkey
);
626 _wapi_sem_id
= semget (oldkey
, _WAPI_SHARED_SEM_COUNT
, 0600);
627 if (_wapi_sem_id
== -1) {
629 g_warning ("%s: semget error opening old key 0x%x (%s) - trying again",
630 __func__
, oldkey
,g_strerror (errno
));
633 * Someone must have deleted the semaphore set, so
634 * blow away the bad key and try again
636 InterlockedCompareExchange (&tmp_shared
->sem_key
, 0, oldkey
);
642 /* Increment the usage count of this semaphore set */
643 thr_ret
= _wapi_shm_sem_lock (_WAPI_SHARED_SEM_PROCESS_COUNT_LOCK
);
644 g_assert (thr_ret
== 0);
646 DEBUGLOG ("%s: Incrementing the process count (%d)", __func__
, _wapi_getpid ());
649 * We only ever _unlock_ this semaphore, letting the kernel
650 * restore (ie decrement) this unlock when this process exits.
651 * We lock another semaphore around it so we can serialise
652 * access when we're testing the value of this semaphore when
653 * we exit cleanly, so we can delete the whole semaphore set.
655 _wapi_shm_sem_unlock (_WAPI_SHARED_SEM_PROCESS_COUNT
);
657 DEBUGLOG ("%s: Process count is now %d (%d)", __func__
, semctl (_wapi_sem_id
, _WAPI_SHARED_SEM_PROCESS_COUNT
, GETVAL
), _wapi_getpid ());
659 _wapi_shm_sem_unlock (_WAPI_SHARED_SEM_PROCESS_COUNT_LOCK
);
661 if (_wapi_shm_disabled
)
664 munmap (tmp_shared
, sizeof(struct _WapiHandleSharedLayout
));
668 shm_semaphores_remove (void)
674 DEBUGLOG ("%s: Checking process count (%d)", __func__
, _wapi_getpid ());
676 thr_ret
= _wapi_shm_sem_lock (_WAPI_SHARED_SEM_PROCESS_COUNT_LOCK
);
677 g_assert (thr_ret
== 0);
679 proc_count
= semctl (_wapi_sem_id
, _WAPI_SHARED_SEM_PROCESS_COUNT
,
682 g_assert (proc_count
> 0);
683 if (proc_count
== 1) {
685 * Just us, so blow away the semaphores and the shared
688 DEBUGLOG ("%s: Removing semaphores! (%d)", __func__
, _wapi_getpid ());
690 semctl (_wapi_sem_id
, 0, IPC_RMID
);
692 shm_name
= _wapi_shm_shm_name (WAPI_SHM_DATA
);
693 shm_unlink (shm_name
);
696 shm_name
= _wapi_shm_shm_name (WAPI_SHM_FILESHARE
);
697 shm_unlink (shm_name
);
700 unlink (_wapi_shm_file (WAPI_SHM_DATA
));
701 unlink (_wapi_shm_file (WAPI_SHM_FILESHARE
));
704 * "else" clause, because there's no point unlocking
705 * the semaphore if we've just blown it away...
707 _wapi_shm_sem_unlock (_WAPI_SHARED_SEM_PROCESS_COUNT_LOCK
);
712 shm_sem_lock (int sem
)
717 DEBUGLOG ("%s: locking sem %d", __func__
, sem
);
721 ops
.sem_flg
= SEM_UNDO
;
725 ret
= semop (_wapi_sem_id
, &ops
, 1);
726 } while (ret
== -1 && errno
== EINTR
);
730 * EINVAL covers the case when the semaphore was
731 * deleted before we started the semop
733 if (errno
== EIDRM
|| errno
== EINVAL
) {
735 * Someone blew away this semaphore set, so
736 * get a new one and try again
738 DEBUGLOG ("%s: Reinitialising the semaphores!", __func__
);
740 _wapi_shm_semaphores_init ();
744 /* Turn this into a pthreads-style return value */
748 DEBUGLOG ("%s: returning %d (%s)", __func__
, ret
, g_strerror (ret
));
754 shm_sem_trylock (int sem
)
759 DEBUGLOG ("%s: trying to lock sem %d", __func__
, sem
);
763 ops
.sem_flg
= IPC_NOWAIT
| SEM_UNDO
;
767 ret
= semop (_wapi_sem_id
, &ops
, 1);
768 } while (ret
== -1 && errno
== EINTR
);
772 * EINVAL covers the case when the semaphore was
773 * deleted before we started the semop
775 if (errno
== EIDRM
|| errno
== EINVAL
) {
777 * Someone blew away this semaphore set, so
778 * get a new one and try again
780 DEBUGLOG ("%s: Reinitialising the semaphores!", __func__
);
782 _wapi_shm_semaphores_init ();
786 /* Turn this into a pthreads-style return value */
791 /* But pthreads uses this code instead */
795 DEBUGLOG ("%s: returning %d (%s)", __func__
, ret
, g_strerror (ret
));
801 shm_sem_unlock (int sem
)
806 DEBUGLOG ("%s: unlocking sem %d", __func__
, sem
);
810 ops
.sem_flg
= SEM_UNDO
;
814 ret
= semop (_wapi_sem_id
, &ops
, 1);
815 } while (ret
== -1 && errno
== EINTR
);
818 /* EINVAL covers the case when the semaphore was
819 * deleted before we started the semop
821 if (errno
== EIDRM
|| errno
== EINVAL
) {
822 /* Someone blew away this semaphore set, so
823 * get a new one and try again (we can't just
824 * assume that the semaphore is now unlocked)
826 DEBUGLOG ("%s: Reinitialising the semaphores!", __func__
);
828 _wapi_shm_semaphores_init ();
832 /* Turn this into a pthreads-style return value */
836 DEBUGLOG ("%s: returning %d (%s)", __func__
, ret
, g_strerror (ret
));
842 _wapi_shm_semaphores_init (void)
844 if (!_wapi_shm_enabled ())
845 noshm_semaphores_init ();
847 shm_semaphores_init ();
851 _wapi_shm_semaphores_remove (void)
853 if (!_wapi_shm_disabled
)
854 shm_semaphores_remove ();
858 _wapi_shm_sem_lock (int sem
)
860 if (_wapi_shm_disabled
)
861 return noshm_sem_lock (sem
);
863 return shm_sem_lock (sem
);
867 _wapi_shm_sem_trylock (int sem
)
869 if (_wapi_shm_disabled
)
870 return noshm_sem_trylock (sem
);
872 return shm_sem_trylock (sem
);
876 _wapi_shm_sem_unlock (int sem
)
878 if (_wapi_shm_disabled
)
879 return noshm_sem_unlock (sem
);
881 return shm_sem_unlock (sem
);
883 #endif /* !DISABLE_SHARED_HANDLES */