2009-05-15 Geoff Norton <gnorton@novell.com>
[mono-project.git] / mono / io-layer / shared.c
blob7260ca7df9959c5a831968f727ba7e64572ab6ce
1 /*
2 * shared.c: Shared memory handling, and daemon launching
4 * Author:
5 * Dick Porter (dick@ximian.com)
7 * (C) 2002-2006 Novell, Inc.
8 */
11 #include <config.h>
12 #include <glib.h>
13 #include <stdio.h>
14 #include <fcntl.h>
15 #include <unistd.h>
16 #include <sys/mman.h>
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <errno.h>
20 #include <string.h>
21 #include <sys/ipc.h>
22 #ifdef HAVE_SYS_SEM
23 #include <sys/sem.h>
24 #else
25 #define DISABLE_SHARED_HANDLES
26 #endif
28 #ifdef HAVE_SYS_UTSNAME_H
29 #include <sys/utsname.h>
30 #endif
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.
42 #ifdef HAVE_SHM_OPEN
43 #define USE_SHM 1
44 #endif
46 #undef DEBUG
48 #ifdef DISABLE_SHARED_HANDLES
49 gboolean _wapi_shm_disabled = TRUE;
50 #else
51 gboolean _wapi_shm_disabled = FALSE;
52 #endif
54 static gchar *
55 _wapi_shm_base_name (_wapi_shm_t type)
57 gchar *name = NULL;
58 gchar machine_name[256];
59 const gchar *fake_name;
60 struct utsname ubuf;
61 int ret;
62 int len;
64 ret = uname (&ubuf);
65 if (ret == -1) {
66 ubuf.machine[0] = '\0';
67 ubuf.sysname[0] = '\0';
68 } else {
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';
77 } else {
78 len = MIN (strlen (fake_name), sizeof (machine_name) - 1);
79 strncpy (machine_name, fake_name, len);
80 machine_name [len] = '\0';
83 switch (type) {
84 case WAPI_SHM_DATA:
85 name = g_strdup_printf ("shared_data-%s-%s-%s-%d-%d-%d",
86 machine_name, ubuf.sysname,
87 ubuf.machine,
88 (int) sizeof(struct _WapiHandleShared),
89 _WAPI_HANDLE_VERSION, 0);
90 break;
92 case WAPI_SHM_FILESHARE:
93 name = g_strdup_printf ("shared_fileshare-%s-%s-%s-%d-%d-%d",
94 machine_name, ubuf.sysname,
95 ubuf.machine,
96 (int) sizeof(struct _WapiFileShare),
97 _WAPI_HANDLE_VERSION, 0);
98 break;
101 return name;
104 #ifdef USE_SHM
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);
113 g_free (base_name);
115 return res;
118 static int
119 _wapi_shm_open (const char *filename, int size)
121 int fd;
123 fd = shm_open (filename, O_CREAT|O_RDWR, S_IRUSR|S_IWUSR|S_IRGRP);
124 if (fd == -1)
125 /* Maybe /dev/shm is not mounted */
126 return -1;
127 if (ftruncate (fd, size) != 0) {
128 perror ("_wapi_shm_open (): ftruncate ()");
129 g_assert_not_reached ();
132 return fd;
135 #endif
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
146 * the directory.
148 wapi_dir = getenv ("MONO_SHARED_DIR");
149 if (wapi_dir == NULL) {
150 filename = g_build_filename (g_get_home_dir (), ".wapi", name,
151 NULL);
152 } else {
153 filename = g_build_filename (wapi_dir, ".wapi", name, NULL);
155 g_free (name);
157 g_snprintf (file, _POSIX_PATH_MAX, "%s", filename);
158 g_free (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);
165 mkdir (dir, 0755);
166 g_free (dir);
168 return(file);
171 static int _wapi_shm_file_open (const gchar *filename, guint32 wanted_size)
173 int fd;
174 struct stat statbuf;
175 int ret, tries = 0;
176 gboolean created = FALSE;
177 mode_t oldmask;
179 try_again:
180 if (tries++ > 10) {
181 /* Just give up */
182 return (-1);
183 } else if (tries > 5) {
184 /* Break out of a loop */
185 unlink (filename);
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);
195 umask (oldmask);
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);
204 umask (oldmask);
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
211 goto try_again;
212 } else if (fd == -1) {
213 g_critical ("%s: shared file [%s] open error: %s",
214 __func__, filename, g_strerror (errno));
215 return(-1);
216 } else {
217 /* We created the file, so we need to expand
218 * the file.
220 * (wanted_size-1, because we're about to
221 * write the other byte to actually expand the
222 * file.)
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));
226 close (fd);
227 unlink (filename);
228 return(-1);
231 do {
232 ret = write (fd, "", 1);
233 } while (ret == -1 && errno == EINTR);
235 if (ret == -1) {
236 g_critical ("%s: shared file [%s] write error: %s", __func__, filename, g_strerror (errno));
237 close (fd);
238 unlink (filename);
239 return(-1);
242 created = TRUE;
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));
253 return(-1);
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__,
262 g_strerror (errno));
263 if (created == TRUE) {
264 unlink (filename);
266 close (fd);
267 return(-1);
270 if (statbuf.st_size < wanted_size) {
271 close (fd);
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);
274 unlink (filename);
275 return(-1);
276 } else {
277 /* We didn't create it, so just try opening it again */
278 _wapi_handle_spin (100);
279 goto try_again;
283 return(fd);
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);
299 * _wapi_shm_attach:
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)
307 gpointer shm_seg;
308 int fd;
309 struct stat statbuf;
310 gchar *filename = _wapi_shm_file (type), *shm_name;
311 guint32 size;
313 switch(type) {
314 case WAPI_SHM_DATA:
315 size = sizeof(struct _WapiHandleSharedLayout);
316 break;
318 case WAPI_SHM_FILESHARE:
319 size = sizeof(struct _WapiFileShareLayout);
320 break;
321 default:
322 g_error ("Invalid type in _wapi_shm_attach ()");
323 return NULL;
326 if (check_disabled ()) {
327 return g_malloc0 (size);
330 #ifdef USE_SHM
331 shm_name = _wapi_shm_shm_name (type);
332 fd = _wapi_shm_open (shm_name, size);
333 g_free (shm_name);
334 #else
335 fd = -1;
336 #endif
338 /* Fall back to files if POSIX shm fails (for example, because /dev/shm is not mounted */
339 if (fd == -1)
340 fd = _wapi_shm_file_open (filename, size);
341 if (fd == -1) {
342 g_critical ("%s: shared file [%s] open error", __func__,
343 filename);
344 return(NULL);
347 if (fstat (fd, &statbuf)==-1) {
348 g_critical ("%s: fstat error: %s", __func__,
349 g_strerror (errno));
350 close (fd);
351 return(NULL);
354 shm_seg = mmap (NULL, statbuf.st_size, PROT_READ|PROT_WRITE,
355 MAP_SHARED, fd, 0);
356 if (shm_seg == MAP_FAILED) {
357 shm_seg = mmap (NULL, statbuf.st_size, PROT_READ|PROT_WRITE,
358 MAP_PRIVATE, fd, 0);
359 if (shm_seg == MAP_FAILED) {
360 g_critical ("%s: mmap error: %s", __func__, g_strerror (errno));
361 close (fd);
362 return(NULL);
366 close (fd);
367 return(shm_seg);
370 #ifdef HAVE_SYS_SEM
371 static void shm_semaphores_init (void)
373 key_t key;
374 key_t oldkey;
375 int thr_ret;
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
382 union semun {
383 int val;
384 struct semid_ds *buf;
385 ushort *array;
386 } defs;
387 ushort def_vals[_WAPI_SHARED_SEM_COUNT];
388 int i;
389 int retries = 0;
391 for (i = 0; i < _WAPI_SHARED_SEM_COUNT; i++) {
392 def_vals[i] = 1;
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');
415 again:
416 retries++;
417 oldkey = tmp_shared->sem_key;
419 if (oldkey == 0) {
420 #ifdef DEBUG
421 g_message ("%s: Creating with new key (0x%x)", __func__, key);
422 #endif
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
427 * applications
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__,
433 g_strerror (errno));
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) {
437 if (retries > 3)
438 g_warning ("%s: semget error: %s key 0x%x - trying again", __func__,
439 g_strerror (errno), key);
442 key++;
443 #ifdef DEBUG
444 g_message ("%s: Got (%s), trying with new key (0x%x)",
445 __func__, g_strerror (errno), key);
446 #endif
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) {
453 if (retries > 3)
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);
460 goto again;
463 if (InterlockedCompareExchange (&tmp_shared->sem_key,
464 key, 0) != 0) {
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;
472 } else {
473 /* We've installed this semaphore set's key into
474 * the shared memory
476 goto done;
480 #ifdef DEBUG
481 g_message ("%s: Trying with old key 0x%x", __func__, oldkey);
482 #endif
484 _wapi_sem_id = semget (oldkey, _WAPI_SHARED_SEM_COUNT, 0600);
485 if (_wapi_sem_id == -1) {
486 if (retries > 3)
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);
495 goto again;
498 done:
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);
503 #ifdef DEBUG
504 g_message ("%s: Incrementing the process count (%d)", __func__, _wapi_getpid ());
505 #endif
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);
515 #ifdef DEBUG
516 g_message ("%s: Process count is now %d (%d)", __func__, semctl (_wapi_sem_id, _WAPI_SHARED_SEM_PROCESS_COUNT, GETVAL), _wapi_getpid ());
517 #endif
519 _wapi_shm_sem_unlock (_WAPI_SHARED_SEM_PROCESS_COUNT_LOCK);
521 if (_wapi_shm_disabled)
522 g_free (tmp_shared);
523 else
524 munmap (tmp_shared, sizeof(struct _WapiHandleSharedLayout));
526 #endif
528 static mono_mutex_t noshm_sems[_WAPI_SHARED_SEM_COUNT];
530 static void noshm_semaphores_init (void)
532 int i;
534 for (i = 0; i < _WAPI_SHARED_SEM_COUNT; i++) {
535 mono_mutex_init (&noshm_sems[i], NULL);
539 #ifdef HAVE_SYS_SEM
540 static void shm_semaphores_remove (void)
542 int thr_ret;
543 int proc_count;
544 gchar *shm_name;
546 #ifdef DEBUG
547 g_message ("%s: Checking process count (%d)", __func__,
548 _wapi_getpid ());
549 #endif
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,
555 GETVAL);
557 g_assert (proc_count > 0);
558 if (proc_count == 1) {
559 /* Just us, so blow away the semaphores and the shared
560 * files
562 #ifdef DEBUG
563 g_message ("%s: Removing semaphores! (%d)", __func__,
564 _wapi_getpid ());
565 #endif
567 semctl (_wapi_sem_id, 0, IPC_RMID);
568 #ifdef USE_SHM
569 shm_name = _wapi_shm_shm_name (WAPI_SHM_DATA);
570 shm_unlink (shm_name);
571 g_free (shm_name);
573 shm_name = _wapi_shm_shm_name (WAPI_SHM_FILESHARE);
574 shm_unlink (shm_name);
575 g_free (shm_name);
576 #endif
577 unlink (_wapi_shm_file (WAPI_SHM_DATA));
578 unlink (_wapi_shm_file (WAPI_SHM_FILESHARE));
579 } else {
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);
586 #endif
588 static void noshm_semaphores_remove (void)
590 /* No need to do anything */
593 #ifdef HAVE_SYS_SEM
594 static int shm_sem_lock (int sem)
596 struct sembuf ops;
597 int ret;
599 #ifdef DEBUG
600 g_message ("%s: locking sem %d", __func__, sem);
601 #endif
603 ops.sem_num = sem;
604 ops.sem_op = -1;
605 ops.sem_flg = SEM_UNDO;
607 retry:
608 do {
609 ret = semop (_wapi_sem_id, &ops, 1);
610 } while (ret == -1 && errno == EINTR);
612 if (ret == -1) {
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
620 #ifdef DEBUG
621 g_message ("%s: Reinitialising the semaphores!",
622 __func__);
623 #endif
625 _wapi_shm_semaphores_init ();
626 goto retry;
629 /* Turn this into a pthreads-style return value */
630 ret = errno;
633 #ifdef DEBUG
634 g_message ("%s: returning %d (%s)", __func__, ret, g_strerror (ret));
635 #endif
637 return(ret);
639 #endif
641 static int noshm_sem_lock (int sem)
643 int ret;
645 #ifdef DEBUG
646 g_message ("%s: locking nosem %d", __func__, sem);
647 #endif
649 ret = mono_mutex_lock (&noshm_sems[sem]);
651 return(ret);
654 #ifdef HAVE_SYS_SEM
655 static int shm_sem_trylock (int sem)
657 struct sembuf ops;
658 int ret;
660 #ifdef DEBUG
661 g_message ("%s: trying to lock sem %d", __func__, sem);
662 #endif
664 ops.sem_num = sem;
665 ops.sem_op = -1;
666 ops.sem_flg = IPC_NOWAIT | SEM_UNDO;
668 retry:
669 do {
670 ret = semop (_wapi_sem_id, &ops, 1);
671 } while (ret == -1 && errno == EINTR);
673 if (ret == -1) {
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
681 #ifdef DEBUG
682 g_message ("%s: Reinitialising the semaphores!",
683 __func__);
684 #endif
686 _wapi_shm_semaphores_init ();
687 goto retry;
690 /* Turn this into a pthreads-style return value */
691 ret = errno;
694 if (ret == EAGAIN) {
695 /* But pthreads uses this code instead */
696 ret = EBUSY;
699 #ifdef DEBUG
700 g_message ("%s: returning %d (%s)", __func__, ret, g_strerror (ret));
701 #endif
703 return(ret);
705 #endif
707 static int noshm_sem_trylock (int sem)
709 int ret;
711 #ifdef DEBUG
712 g_message ("%s: trying to lock nosem %d", __func__, sem);
713 #endif
715 ret = mono_mutex_trylock (&noshm_sems[sem]);
717 return(ret);
720 #ifdef HAVE_SYS_SEM
721 static int shm_sem_unlock (int sem)
723 struct sembuf ops;
724 int ret;
726 #ifdef DEBUG
727 g_message ("%s: unlocking sem %d", __func__, sem);
728 #endif
730 ops.sem_num = sem;
731 ops.sem_op = 1;
732 ops.sem_flg = SEM_UNDO;
734 retry:
735 do {
736 ret = semop (_wapi_sem_id, &ops, 1);
737 } while (ret == -1 && errno == EINTR);
739 if (ret == -1) {
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)
748 #ifdef DEBUG
749 g_message ("%s: Reinitialising the semaphores!",
750 __func__);
751 #endif
753 _wapi_shm_semaphores_init ();
754 goto retry;
757 /* Turn this into a pthreads-style return value */
758 ret = errno;
761 #ifdef DEBUG
762 g_message ("%s: returning %d (%s)", __func__, ret, g_strerror (ret));
763 #endif
765 return(ret);
767 #endif
769 static int noshm_sem_unlock (int sem)
771 int ret;
773 #ifdef DEBUG
774 g_message ("%s: unlocking nosem %d", __func__, sem);
775 #endif
777 ret = mono_mutex_unlock (&noshm_sems[sem]);
779 return(ret);
782 void _wapi_shm_semaphores_init (void)
784 #ifndef HAVE_SYS_SEM
785 noshm_semaphores_init ();
786 #else
787 if (check_disabled ()) {
788 noshm_semaphores_init ();
789 } else {
790 shm_semaphores_init ();
792 #endif
795 void _wapi_shm_semaphores_remove (void)
797 #ifndef HAVE_SYS_SEM
798 noshm_semaphores_remove ();
799 #else
800 if (_wapi_shm_disabled) {
801 noshm_semaphores_remove ();
802 } else {
803 shm_semaphores_remove ();
805 #endif
808 int _wapi_shm_sem_lock (int sem)
810 #ifndef HAVE_SYS_SEM
811 return(noshm_sem_lock (sem));
812 #else
813 if (_wapi_shm_disabled) {
814 return(noshm_sem_lock (sem));
815 } else {
816 return(shm_sem_lock (sem));
818 #endif
821 int _wapi_shm_sem_trylock (int sem)
823 #ifndef HAVE_SYS_SEM
824 return(noshm_sem_trylock (sem));
825 #else
826 if (_wapi_shm_disabled) {
827 return(noshm_sem_trylock (sem));
828 } else {
829 return(shm_sem_trylock (sem));
831 #endif
834 int _wapi_shm_sem_unlock (int sem)
836 #ifndef HAVE_SYS_SEM
837 return(noshm_sem_unlock (sem));
838 #else
839 if (_wapi_shm_disabled) {
840 return(noshm_sem_unlock (sem));
841 } else {
842 return(shm_sem_unlock (sem));
844 #endif