- noshm_semaphores_init ();
-}
-
-void
-_wapi_shm_semaphores_remove (void)
-{
- /* Nothing */
-}
-
-int
-_wapi_shm_sem_lock (int sem)
-{
- return noshm_sem_lock (sem);
-}
-
-int
-_wapi_shm_sem_trylock (int sem)
-{
- return noshm_sem_trylock (sem);
-}
-
-int
-_wapi_shm_sem_unlock (int sem)
-{
- return noshm_sem_unlock (sem);
-}
-
-gpointer
-_wapi_shm_attach (_wapi_shm_t type)
-{
- gpointer res;
-
- switch(type) {
- case WAPI_SHM_DATA:
- res = g_malloc0 (sizeof(struct _WapiHandleSharedLayout));
- break;
- case WAPI_SHM_FILESHARE:
- res = g_malloc0 (sizeof(struct _WapiFileShareLayout));
- break;
- default:
- g_error ("Invalid type in _wapi_shm_attach ()");
- return NULL;
- }
-
- wapi_storage [type] = res;
- return res;
-}
-
-void
-_wapi_shm_detach (_wapi_shm_t type)
-{
- g_free (wapi_storage [type]);
-}
-
-gboolean
-_wapi_shm_enabled (void)
-{
- return FALSE;
-}
-
-#else
-/*
- * Use POSIX shared memory if possible, it is simpler, and it has the advantage that
- * writes to the shared area does not need to be written to disk, avoiding spinning up
- * the disk every x secs on laptops.
- */
-#ifdef HAVE_SHM_OPEN
-#define USE_SHM 1
-#endif
-
-static gchar *
-_wapi_shm_base_name (_wapi_shm_t type)
-{
- gchar *name = NULL;
- gchar machine_name[256];
- const gchar *fake_name;
- struct utsname ubuf;
- int ret;
- int len;
-
- ret = uname (&ubuf);
- if (ret == -1) {
- ubuf.machine[0] = '\0';
- ubuf.sysname[0] = '\0';
- } else {
- g_strdelimit (ubuf.sysname, "/", '_');
- g_strdelimit (ubuf.machine, "/", '_');
- }
-
- fake_name = g_getenv ("MONO_SHARED_HOSTNAME");
- if (fake_name == NULL) {
- if (gethostname(machine_name, sizeof(machine_name)) != 0)
- machine_name[0] = '\0';
- } else {
- len = MIN (strlen (fake_name), sizeof (machine_name) - 1);
- strncpy (machine_name, fake_name, len);
- machine_name [len] = '\0';
- }
-
- switch (type) {
- case WAPI_SHM_DATA:
- name = g_strdup_printf ("shared_data-%s-%s-%s-%d-%d-%d",
- machine_name, ubuf.sysname,
- ubuf.machine,
- (int) sizeof(struct _WapiHandleShared),
- _WAPI_HANDLE_VERSION, 0);
- break;
-
- case WAPI_SHM_FILESHARE:
- name = g_strdup_printf ("shared_fileshare-%s-%s-%s-%d-%d-%d",
- machine_name, ubuf.sysname,
- ubuf.machine,
- (int) sizeof(struct _WapiFileShare),
- _WAPI_HANDLE_VERSION, 0);
- break;
- }
-
- return name;
-}
-
-#ifdef USE_SHM
-
-static gchar *_wapi_shm_shm_name (_wapi_shm_t type)
-{
- char *base_name = _wapi_shm_base_name (type);
-
- /* Also add the uid to avoid permission problems */
- char *res = g_strdup_printf ("/mono-shared-%d-%s", getuid (), base_name);
-
- g_free (base_name);
-
- return res;
-}
-
-static int
-_wapi_shm_open (const char *filename, int size)
-{
- int fd;
-
- fd = shm_open (filename, O_CREAT|O_RDWR, S_IRUSR|S_IWUSR|S_IRGRP);
- if (fd == -1)
- /* Maybe /dev/shm is not mounted */
- return -1;
- if (ftruncate (fd, size) != 0) {
- perror ("_wapi_shm_open (): ftruncate ()");
- g_assert_not_reached ();
- }
-
- return fd;
-}
-
-#endif
-
-static gchar *
-_wapi_shm_file (_wapi_shm_t type)
-{
- static gchar file[_POSIX_PATH_MAX];
- gchar *name = NULL, *filename, *wapi_dir;
-
- name = _wapi_shm_base_name (type);
-
- /* I don't know how nfs affects mmap. If mmap() of files on
- * nfs mounts breaks, then there should be an option to set
- * the directory.
- */
- wapi_dir = g_getenv ("MONO_SHARED_DIR");
- if (wapi_dir == NULL) {
- filename = g_build_filename (g_get_home_dir (), ".wapi", name,
- NULL);
- } else {
- filename = g_build_filename (wapi_dir, ".wapi", name, NULL);
- }
- g_free (name);
-
- g_snprintf (file, _POSIX_PATH_MAX, "%s", filename);
- g_free (filename);
-
- return file;
-}
-
-static int
-_wapi_shm_file_open (const gchar *filename, guint32 wanted_size)
-{
- int fd;
- struct stat statbuf;
- int ret, tries = 0;
- gboolean created = FALSE;
- mode_t oldmask;
- gchar *dir;
-
- /* No need to check if the dir already exists or check
- * mkdir() errors, because on any error the open() call will
- * report the problem.
- */
- dir = g_path_get_dirname (filename);
- mkdir (dir, 0755);
- g_free (dir);
-
-try_again:
- if (tries++ > 10) {
- /* Just give up */
- return (-1);
- } else if (tries > 5) {
- /* Break out of a loop */
- unlink (filename);
- }
-
- /* Make sure future processes can open the shared data files */
- oldmask = umask (066);
-
- /* No O_CREAT yet, because we need to initialise the file if
- * we have to create it.
- */
- fd = open (filename, O_RDWR, 0600);
- umask (oldmask);
-
- if (fd == -1 && errno == ENOENT) {
- /* OK, its up to us to create it. O_EXCL to avoid a
- * race condition where two processes can
- * simultaneously try and create the file
- */
- oldmask = umask (066);
- fd = open (filename, O_CREAT|O_EXCL|O_RDWR, 0600);
- umask (oldmask);
-
- if (fd == -1 && errno == EEXIST) {
- /* It's possible that the file was created in
- * between finding it didn't exist, and trying
- * to create it. Just try opening it again
- */
- goto try_again;
- } else if (fd == -1) {
- g_critical ("%s: shared file [%s] open error: %s",
- __func__, filename, g_strerror (errno));
- return -1;
- } else {
- /* We created the file, so we need to expand
- * the file.
- *
- * (wanted_size-1, because we're about to
- * write the other byte to actually expand the
- * file.)
- */
- if (lseek (fd, wanted_size-1, SEEK_SET) == -1) {
- g_critical ("%s: shared file [%s] lseek error: %s", __func__, filename, g_strerror (errno));
- close (fd);
- unlink (filename);
- return -1;
- }
-
- do {
- ret = write (fd, "", 1);
- } while (ret == -1 && errno == EINTR);
-
- if (ret == -1) {
- g_critical ("%s: shared file [%s] write error: %s", __func__, filename, g_strerror (errno));
- close (fd);
- unlink (filename);
- return -1;
- }
-
- created = TRUE;
-
- /* The contents of the file is set to all
- * zero, because it is opened up with lseek,
- * so we don't need to do any more
- * initialisation here
- */
- }
- } else if (fd == -1) {
- g_critical ("%s: shared file [%s] open error: %s", __func__,
- filename, g_strerror (errno));
- return -1;
- }
-
- /* Use stat to find the file size (instead of hard coding it)
- * because we can expand the file later if needed (for more
- * handles or scratch space.)
- */
- if (fstat (fd, &statbuf) == -1) {
- g_critical ("%s: fstat error: %s", __func__,
- g_strerror (errno));
- if (created == TRUE) {
- unlink (filename);
- }
- close (fd);
- return -1;
- }
-
- if (statbuf.st_size < wanted_size) {
- close (fd);
- if (created == TRUE) {
- g_critical ("%s: shared file [%s] is not big enough! (found %ld, need %d bytes)", __func__, filename, (long)statbuf.st_size, wanted_size);
- unlink (filename);
- return -1;
- } else {
- /* We didn't create it, so just try opening it again */
- _wapi_handle_spin (100);
- goto try_again;
- }
- }
-
- return fd;
-}
-
-gboolean
-_wapi_shm_enabled (void)
-{
- static gboolean env_checked;
-
- if (!env_checked) {
- if (g_getenv ("MONO_ENABLE_SHM"))
- _wapi_shm_disabled = FALSE;
- env_checked = TRUE;
- }
-
- return !_wapi_shm_disabled;
-}
-
-/*
- * _wapi_shm_attach:
- * @success: Was it a success
- *
- * Attach to the shared memory file or create it if it did not exist.
- * Returns the memory area the file was mmapped to.
- */
-gpointer
-_wapi_shm_attach (_wapi_shm_t type)
-{
- gpointer shm_seg;
- int fd;
- struct stat statbuf;
- gchar *filename = _wapi_shm_file (type), *shm_name;
- guint32 size;
-
- switch(type) {
- case WAPI_SHM_DATA:
- size = sizeof(struct _WapiHandleSharedLayout);
- break;
-
- case WAPI_SHM_FILESHARE:
- size = sizeof(struct _WapiFileShareLayout);
- break;
- default:
- g_error ("Invalid type in _wapi_shm_attach ()");
- return NULL;
- }
-
- if (!_wapi_shm_enabled ()) {
- wapi_storage [type] = g_malloc0 (size);
- return wapi_storage [type];
- }
-
-#ifdef USE_SHM
- shm_name = _wapi_shm_shm_name (type);
- fd = _wapi_shm_open (shm_name, size);
- g_free (shm_name);
-#else
- fd = -1;
-#endif
-
- /* Fall back to files if POSIX shm fails (for example, because /dev/shm is not mounted */
- if (fd == -1)
- fd = _wapi_shm_file_open (filename, size);
- if (fd == -1) {
- g_critical ("%s: shared file [%s] open error", __func__,
- filename);
- return NULL;
- }
-
- if (fstat (fd, &statbuf)==-1) {
- g_critical ("%s: fstat error: %s", __func__,
- g_strerror (errno));
- close (fd);
- return NULL;
- }
-
- shm_seg = mmap (NULL, statbuf.st_size, PROT_READ|PROT_WRITE,
- MAP_SHARED, fd, 0);
- if (shm_seg == MAP_FAILED) {
- shm_seg = mmap (NULL, statbuf.st_size, PROT_READ|PROT_WRITE,
- MAP_PRIVATE, fd, 0);
- if (shm_seg == MAP_FAILED) {
- g_critical ("%s: mmap error: %s", __func__, g_strerror (errno));
- close (fd);
- return NULL;
- }
- }
-
- close (fd);
- return shm_seg;
-}
-
-void
-_wapi_shm_detach (_wapi_shm_t type)
-{
- if (!_wapi_shm_enabled ())
- g_free (wapi_storage [type]);
-}
-
-static void
-shm_semaphores_init (void)
-{
- key_t key;
- key_t oldkey;
- int thr_ret;
- struct _WapiHandleSharedLayout *tmp_shared;
- gchar *ftmp;
- gchar *filename;
-
- /*
- * Yet more barmy API - this union is a well-defined parameter
- * in a syscall, yet I still have to define it here as it
- * doesn't appear in a header
- */
- union semun {
- int val;
- struct semid_ds *buf;
- ushort *array;
- } defs;
- ushort def_vals[_WAPI_SHARED_SEM_COUNT];