* mini-s390.c (add_general): Adjust offset calculation to take into account of roundi...
[mono.git] / mono / io-layer / shared.c
blob7833721551e148239ad05ca438ee6b7b247c8cfa
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 #include <sys/sem.h>
23 #include <sys/utsname.h>
25 #include <mono/io-layer/wapi.h>
26 #include <mono/io-layer/wapi-private.h>
27 #include <mono/io-layer/shared.h>
28 #include <mono/io-layer/handles-private.h>
30 #undef DEBUG
32 #ifdef DISABLE_SHARED_HANDLES
33 gboolean _wapi_shm_disabled = TRUE;
34 #else
35 gboolean _wapi_shm_disabled = FALSE;
36 #endif
38 static gchar *_wapi_shm_file (_wapi_shm_t type)
40 static gchar file[_POSIX_PATH_MAX];
41 gchar *name = NULL, *filename, *dir, *wapi_dir;
42 gchar machine_name[256];
43 const gchar *fake_name;
44 struct utsname ubuf;
45 int ret;
46 int len;
48 ret = uname (&ubuf);
49 if (ret == -1) {
50 ubuf.machine[0] = '\0';
51 ubuf.sysname[0] = '\0';
52 } else {
53 g_strdelimit (ubuf.sysname, "/", '_');
54 g_strdelimit (ubuf.machine, "/", '_');
57 fake_name = g_getenv ("MONO_SHARED_HOSTNAME");
58 if (fake_name == NULL) {
59 if (gethostname(machine_name, sizeof(machine_name)) != 0)
60 machine_name[0] = '\0';
61 } else {
62 len = MIN (strlen (fake_name), sizeof (machine_name) - 1);
63 strncpy (machine_name, fake_name, len);
64 machine_name [len] = '\0';
67 switch (type) {
68 case WAPI_SHM_DATA:
69 name = g_strdup_printf ("shared_data-%s-%s-%s-%d-%d-%d",
70 machine_name, ubuf.sysname,
71 ubuf.machine,
72 (int) sizeof(struct _WapiHandleShared),
73 _WAPI_HANDLE_VERSION, 0);
74 break;
76 case WAPI_SHM_FILESHARE:
77 name = g_strdup_printf ("shared_fileshare-%s-%s-%s-%d-%d-%d",
78 machine_name, ubuf.sysname,
79 ubuf.machine,
80 (int) sizeof(struct _WapiFileShare),
81 _WAPI_HANDLE_VERSION, 0);
82 break;
85 /* I don't know how nfs affects mmap. If mmap() of files on
86 * nfs mounts breaks, then there should be an option to set
87 * the directory.
89 wapi_dir = getenv ("MONO_SHARED_DIR");
90 if (wapi_dir == NULL) {
91 filename = g_build_filename (g_get_home_dir (), ".wapi", name,
92 NULL);
93 } else {
94 filename = g_build_filename (wapi_dir, ".wapi", name, NULL);
96 g_free (name);
98 g_snprintf (file, _POSIX_PATH_MAX, "%s", filename);
99 g_free (filename);
101 /* No need to check if the dir already exists or check
102 * mkdir() errors, because on any error the open() call will
103 * report the problem.
105 dir = g_path_get_dirname (file);
106 mkdir (dir, 0755);
107 g_free (dir);
109 return(file);
112 static int _wapi_shm_file_open (const gchar *filename, guint32 wanted_size)
114 int fd;
115 struct stat statbuf;
116 int ret, tries = 0;
117 gboolean created = FALSE;
119 try_again:
120 if (tries++ > 10) {
121 /* Just give up */
122 return (-1);
123 } else if (tries > 5) {
124 /* Break out of a loop */
125 unlink (filename);
128 /* No O_CREAT yet, because we need to initialise the file if
129 * we have to create it.
131 fd = open (filename, O_RDWR, 0600);
132 if (fd == -1 && errno == ENOENT) {
133 /* OK, its up to us to create it. O_EXCL to avoid a
134 * race condition where two processes can
135 * simultaneously try and create the file
137 fd = open (filename, O_CREAT|O_EXCL|O_RDWR, 0600);
138 if (fd == -1 && errno == EEXIST) {
139 /* It's possible that the file was created in
140 * between finding it didn't exist, and trying
141 * to create it. Just try opening it again
143 goto try_again;
144 } else if (fd == -1) {
145 g_critical ("%s: shared file [%s] open error: %s",
146 __func__, filename, g_strerror (errno));
147 return(-1);
148 } else {
149 /* We created the file, so we need to expand
150 * the file.
152 * (wanted_size-1, because we're about to
153 * write the other byte to actually expand the
154 * file.)
156 if (lseek (fd, wanted_size-1, SEEK_SET) == -1) {
157 g_critical ("%s: shared file [%s] lseek error: %s", __func__, filename, g_strerror (errno));
158 close (fd);
159 unlink (filename);
160 return(-1);
163 do {
164 ret = write (fd, "", 1);
165 } while (ret == -1 && errno == EINTR);
167 if (ret == -1) {
168 g_critical ("%s: shared file [%s] write error: %s", __func__, filename, g_strerror (errno));
169 close (fd);
170 unlink (filename);
171 return(-1);
174 created = TRUE;
176 /* The contents of the file is set to all
177 * zero, because it is opened up with lseek,
178 * so we don't need to do any more
179 * initialisation here
182 } else if (fd == -1) {
183 g_critical ("%s: shared file [%s] open error: %s", __func__,
184 filename, g_strerror (errno));
185 return(-1);
188 /* Use stat to find the file size (instead of hard coding it)
189 * because we can expand the file later if needed (for more
190 * handles or scratch space.)
192 if (fstat (fd, &statbuf) == -1) {
193 g_critical ("%s: fstat error: %s", __func__,
194 g_strerror (errno));
195 if (created == TRUE) {
196 unlink (filename);
198 close (fd);
199 return(-1);
202 if (statbuf.st_size < wanted_size) {
203 close (fd);
204 if (created == TRUE) {
205 #ifdef HAVE_LARGE_FILE_SUPPORT
206 /* Keep gcc quiet... */
207 g_critical ("%s: shared file [%s] is not big enough! (found %lld, need %d bytes)", __func__, filename, statbuf.st_size, wanted_size);
208 #else
209 g_critical ("%s: shared file [%s] is not big enough! (found %ld, need %d bytes)", __func__, filename, statbuf.st_size, wanted_size);
210 #endif
211 unlink (filename);
212 return(-1);
213 } else {
214 /* We didn't create it, so just try opening it again */
215 _wapi_handle_spin (100);
216 goto try_again;
220 return(fd);
223 static gboolean check_disabled (void)
225 if (_wapi_shm_disabled || g_getenv ("MONO_DISABLE_SHM")) {
226 const char* val = g_getenv ("MONO_DISABLE_SHM");
227 if (val == NULL || *val == '1' || *val == 'y' || *val == 'Y') {
228 _wapi_shm_disabled = TRUE;
232 return(_wapi_shm_disabled);
236 * _wapi_shm_attach:
237 * @success: Was it a success
239 * Attach to the shared memory file or create it if it did not exist.
240 * Returns the memory area the file was mmapped to.
242 gpointer _wapi_shm_attach (_wapi_shm_t type)
244 gpointer shm_seg;
245 int fd;
246 struct stat statbuf;
247 gchar *filename=_wapi_shm_file (type);
248 guint32 size;
250 switch(type) {
251 case WAPI_SHM_DATA:
252 size = sizeof(struct _WapiHandleSharedLayout);
253 break;
255 case WAPI_SHM_FILESHARE:
256 size = sizeof(struct _WapiFileShareLayout);
257 break;
258 default:
259 g_error ("Invalid type in _wapi_shm_attach ()");
260 return NULL;
263 if (check_disabled ()) {
264 return g_malloc0 (size);
267 fd = _wapi_shm_file_open (filename, size);
268 if (fd == -1) {
269 g_critical ("%s: shared file [%s] open error", __func__,
270 filename);
271 return(NULL);
274 if (fstat (fd, &statbuf)==-1) {
275 g_critical ("%s: fstat error: %s", __func__,
276 g_strerror (errno));
277 close (fd);
278 return(NULL);
281 shm_seg = mmap (NULL, statbuf.st_size, PROT_READ|PROT_WRITE,
282 MAP_SHARED, fd, 0);
283 if (shm_seg == MAP_FAILED) {
284 shm_seg = mmap (NULL, statbuf.st_size, PROT_READ|PROT_WRITE,
285 MAP_PRIVATE, fd, 0);
286 if (shm_seg == MAP_FAILED) {
287 g_critical ("%s: mmap error: %s", __func__, g_strerror (errno));
288 close (fd);
289 return(NULL);
293 close (fd);
294 return(shm_seg);
297 static void shm_semaphores_init (void)
299 key_t key;
300 key_t oldkey;
301 int thr_ret;
302 struct _WapiHandleSharedLayout *tmp_shared;
304 /* Yet more barmy API - this union is a well-defined parameter
305 * in a syscall, yet I still have to define it here as it
306 * doesn't appear in a header
308 union semun {
309 int val;
310 struct semid_ds *buf;
311 ushort *array;
312 } defs;
313 ushort def_vals[_WAPI_SHARED_SEM_COUNT];
314 int i;
315 int retries = 0;
317 for (i = 0; i < _WAPI_SHARED_SEM_COUNT; i++) {
318 def_vals[i] = 1;
321 /* Process count must start at '0' - the 1 for all the others
322 * sets the semaphore to "unlocked"
324 def_vals[_WAPI_SHARED_SEM_PROCESS_COUNT] = 0;
326 defs.array = def_vals;
328 /* Temporarily attach the shared data so we can read the
329 * semaphore key. We release this mapping and attach again
330 * after getting the semaphores to avoid a race condition
331 * where a terminating process can delete the shared files
332 * between a new process attaching the file and getting access
333 * to the semaphores (which increments the process count,
334 * preventing destruction of the shared data...)
336 tmp_shared = _wapi_shm_attach (WAPI_SHM_DATA);
337 g_assert (tmp_shared != NULL);
339 key = ftok (_wapi_shm_file (WAPI_SHM_DATA), 'M');
341 again:
342 retries++;
343 oldkey = tmp_shared->sem_key;
345 if (oldkey == 0) {
346 #ifdef DEBUG
347 g_message ("%s: Creating with new key (0x%x)", __func__, key);
348 #endif
350 /* The while loop attempts to make some sense of the
351 * bonkers 'think of a random number' method of
352 * picking a key without collision with other
353 * applications
355 while ((_wapi_sem_id = semget (key, _WAPI_SHARED_SEM_COUNT,
356 IPC_CREAT | IPC_EXCL | 0600)) == -1) {
357 if (errno == ENOMEM) {
358 g_error ("%s: semget error: %s", __func__,
359 g_strerror (errno));
360 } else if (errno == ENOSPC) {
361 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));
362 } else if (errno != EEXIST) {
363 if (retries > 3)
364 g_warning ("%s: semget error: %s key 0x%x - trying again", __func__,
365 g_strerror (errno), key);
368 key++;
369 #ifdef DEBUG
370 g_message ("%s: Got (%s), trying with new key (0x%x)",
371 __func__, g_strerror (errno), key);
372 #endif
374 /* Got a semaphore array, so initialise it and install
375 * the key into the shared memory
378 if (semctl (_wapi_sem_id, 0, SETALL, defs) == -1) {
379 if (retries > 3)
380 g_warning ("%s: semctl init error: %s - trying again", __func__, g_strerror (errno));
382 /* Something went horribly wrong, so try
383 * getting a new set from scratch
385 semctl (_wapi_sem_id, 0, IPC_RMID);
386 goto again;
389 if (InterlockedCompareExchange (&tmp_shared->sem_key,
390 key, 0) != 0) {
391 /* Someone else created one and installed the
392 * key while we were working, so delete the
393 * array we created and fall through to the
394 * 'key already known' case.
396 semctl (_wapi_sem_id, 0, IPC_RMID);
397 oldkey = tmp_shared->sem_key;
398 } else {
399 /* We've installed this semaphore set's key into
400 * the shared memory
402 goto done;
406 #ifdef DEBUG
407 g_message ("%s: Trying with old key 0x%x", __func__, oldkey);
408 #endif
410 _wapi_sem_id = semget (oldkey, _WAPI_SHARED_SEM_COUNT, 0600);
411 if (_wapi_sem_id == -1) {
412 if (retries > 3)
413 g_warning ("%s: semget error opening old key 0x%x (%s) - trying again",
414 __func__, oldkey,g_strerror (errno));
416 /* Someone must have deleted the semaphore set, so
417 * blow away the bad key and try again
419 InterlockedCompareExchange (&tmp_shared->sem_key, 0, oldkey);
421 goto again;
424 done:
425 /* Increment the usage count of this semaphore set */
426 thr_ret = _wapi_shm_sem_lock (_WAPI_SHARED_SEM_PROCESS_COUNT_LOCK);
427 g_assert (thr_ret == 0);
429 #ifdef DEBUG
430 g_message ("%s: Incrementing the process count (%d)", __func__, _wapi_getpid ());
431 #endif
433 /* We only ever _unlock_ this semaphore, letting the kernel
434 * restore (ie decrement) this unlock when this process exits.
435 * We lock another semaphore around it so we can serialise
436 * access when we're testing the value of this semaphore when
437 * we exit cleanly, so we can delete the whole semaphore set.
439 _wapi_shm_sem_unlock (_WAPI_SHARED_SEM_PROCESS_COUNT);
441 #ifdef DEBUG
442 g_message ("%s: Process count is now %d (%d)", __func__, semctl (_wapi_sem_id, _WAPI_SHARED_SEM_PROCESS_COUNT, GETVAL), _wapi_getpid ());
443 #endif
445 _wapi_shm_sem_unlock (_WAPI_SHARED_SEM_PROCESS_COUNT_LOCK);
447 if (_wapi_shm_disabled)
448 g_free (tmp_shared);
449 else
450 munmap (tmp_shared, sizeof(struct _WapiHandleSharedLayout));
453 static mono_mutex_t noshm_sems[_WAPI_SHARED_SEM_COUNT];
455 static void noshm_semaphores_init (void)
457 int i;
459 for (i = 0; i < _WAPI_SHARED_SEM_COUNT; i++) {
460 mono_mutex_init (&noshm_sems[i], NULL);
464 static void shm_semaphores_remove (void)
466 int thr_ret;
467 int proc_count;
469 #ifdef DEBUG
470 g_message ("%s: Checking process count (%d)", __func__,
471 _wapi_getpid ());
472 #endif
474 thr_ret = _wapi_shm_sem_lock (_WAPI_SHARED_SEM_PROCESS_COUNT_LOCK);
475 g_assert (thr_ret == 0);
477 proc_count = semctl (_wapi_sem_id, _WAPI_SHARED_SEM_PROCESS_COUNT,
478 GETVAL);
480 g_assert (proc_count > 0);
481 if (proc_count == 1) {
482 /* Just us, so blow away the semaphores and the shared
483 * files
485 #ifdef DEBUG
486 g_message ("%s: Removing semaphores! (%d)", __func__,
487 _wapi_getpid ());
488 #endif
490 semctl (_wapi_sem_id, 0, IPC_RMID);
491 unlink (_wapi_shm_file (WAPI_SHM_DATA));
492 unlink (_wapi_shm_file (WAPI_SHM_FILESHARE));
493 } else {
494 /* "else" clause, because there's no point unlocking
495 * the semaphore if we've just blown it away...
497 _wapi_shm_sem_unlock (_WAPI_SHARED_SEM_PROCESS_COUNT_LOCK);
501 static void noshm_semaphores_remove (void)
503 /* No need to do anything */
506 static int shm_sem_lock (int sem)
508 struct sembuf ops;
509 int ret;
511 #ifdef DEBUG
512 g_message ("%s: locking sem %d", __func__, sem);
513 #endif
515 ops.sem_num = sem;
516 ops.sem_op = -1;
517 ops.sem_flg = SEM_UNDO;
519 retry:
520 do {
521 ret = semop (_wapi_sem_id, &ops, 1);
522 } while (ret == -1 && errno == EINTR);
524 if (ret == -1) {
525 /* EINVAL covers the case when the semaphore was
526 * deleted before we started the semop
528 if (errno == EIDRM || errno == EINVAL) {
529 /* Someone blew away this semaphore set, so
530 * get a new one and try again
532 #ifdef DEBUG
533 g_message ("%s: Reinitialising the semaphores!",
534 __func__);
535 #endif
537 _wapi_shm_semaphores_init ();
538 goto retry;
541 /* Turn this into a pthreads-style return value */
542 ret = errno;
545 #ifdef DEBUG
546 g_message ("%s: returning %d (%s)", __func__, ret, g_strerror (ret));
547 #endif
549 return(ret);
552 static int noshm_sem_lock (int sem)
554 int ret;
556 #ifdef DEBUG
557 g_message ("%s: locking nosem %d", __func__, sem);
558 #endif
560 ret = mono_mutex_lock (&noshm_sems[sem]);
562 return(ret);
565 static int shm_sem_trylock (int sem)
567 struct sembuf ops;
568 int ret;
570 #ifdef DEBUG
571 g_message ("%s: trying to lock sem %d", __func__, sem);
572 #endif
574 ops.sem_num = sem;
575 ops.sem_op = -1;
576 ops.sem_flg = IPC_NOWAIT | SEM_UNDO;
578 retry:
579 do {
580 ret = semop (_wapi_sem_id, &ops, 1);
581 } while (ret == -1 && errno == EINTR);
583 if (ret == -1) {
584 /* EINVAL covers the case when the semaphore was
585 * deleted before we started the semop
587 if (errno == EIDRM || errno == EINVAL) {
588 /* Someone blew away this semaphore set, so
589 * get a new one and try again
591 #ifdef DEBUG
592 g_message ("%s: Reinitialising the semaphores!",
593 __func__);
594 #endif
596 _wapi_shm_semaphores_init ();
597 goto retry;
600 /* Turn this into a pthreads-style return value */
601 ret = errno;
604 if (ret == EAGAIN) {
605 /* But pthreads uses this code instead */
606 ret = EBUSY;
609 #ifdef DEBUG
610 g_message ("%s: returning %d (%s)", __func__, ret, g_strerror (ret));
611 #endif
613 return(ret);
616 static int noshm_sem_trylock (int sem)
618 int ret;
620 #ifdef DEBUG
621 g_message ("%s: trying to lock nosem %d", __func__, sem);
622 #endif
624 ret = mono_mutex_trylock (&noshm_sems[sem]);
626 return(ret);
629 static int shm_sem_unlock (int sem)
631 struct sembuf ops;
632 int ret;
634 #ifdef DEBUG
635 g_message ("%s: unlocking sem %d", __func__, sem);
636 #endif
638 ops.sem_num = sem;
639 ops.sem_op = 1;
640 ops.sem_flg = SEM_UNDO;
642 retry:
643 do {
644 ret = semop (_wapi_sem_id, &ops, 1);
645 } while (ret == -1 && errno == EINTR);
647 if (ret == -1) {
648 /* EINVAL covers the case when the semaphore was
649 * deleted before we started the semop
651 if (errno == EIDRM || errno == EINVAL) {
652 /* Someone blew away this semaphore set, so
653 * get a new one and try again (we can't just
654 * assume that the semaphore is now unlocked)
656 #ifdef DEBUG
657 g_message ("%s: Reinitialising the semaphores!",
658 __func__);
659 #endif
661 _wapi_shm_semaphores_init ();
662 goto retry;
665 /* Turn this into a pthreads-style return value */
666 ret = errno;
669 #ifdef DEBUG
670 g_message ("%s: returning %d (%s)", __func__, ret, g_strerror (ret));
671 #endif
673 return(ret);
676 static int noshm_sem_unlock (int sem)
678 int ret;
680 #ifdef DEBUG
681 g_message ("%s: unlocking nosem %d", __func__, sem);
682 #endif
684 ret = mono_mutex_unlock (&noshm_sems[sem]);
686 return(ret);
689 void _wapi_shm_semaphores_init (void)
691 if (check_disabled ()) {
692 noshm_semaphores_init ();
693 } else {
694 shm_semaphores_init ();
698 void _wapi_shm_semaphores_remove (void)
700 if (_wapi_shm_disabled) {
701 noshm_semaphores_remove ();
702 } else {
703 shm_semaphores_remove ();
707 int _wapi_shm_sem_lock (int sem)
709 if (_wapi_shm_disabled) {
710 return(noshm_sem_lock (sem));
711 } else {
712 return(shm_sem_lock (sem));
716 int _wapi_shm_sem_trylock (int sem)
718 if (_wapi_shm_disabled) {
719 return(noshm_sem_trylock (sem));
720 } else {
721 return(shm_sem_trylock (sem));
725 int _wapi_shm_sem_unlock (int sem)
727 if (_wapi_shm_disabled) {
728 return(noshm_sem_unlock (sem));
729 } else {
730 return(shm_sem_unlock (sem));