2009-05-18 Miguel de Icaza <miguel@novell.com>
[mono.git] / mono / io-layer / shared.c
1 /*
2  * shared.c:  Shared memory handling, and daemon launching
3  *
4  * Author:
5  *      Dick Porter (dick@ximian.com)
6  *
7  * (C) 2002-2006 Novell, Inc.
8  */
9
10
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
27
28 #ifdef HAVE_SYS_UTSNAME_H
29 #include <sys/utsname.h>
30 #endif
31
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>
36
37 /*
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.
41  */
42 #ifdef HAVE_SHM_OPEN
43 #define USE_SHM 1
44 #endif
45
46 #undef DEBUG
47
48 #ifdef DISABLE_SHARED_HANDLES
49 gboolean _wapi_shm_disabled = TRUE;
50 #else
51 gboolean _wapi_shm_disabled = FALSE;
52 #endif
53
54 static gchar *
55 _wapi_shm_base_name (_wapi_shm_t type)
56 {
57         gchar *name = NULL;
58         gchar machine_name[256];
59         const gchar *fake_name;
60         struct utsname ubuf;
61         int ret;
62         int len;
63         
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, "/", '_');
71         }
72
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';
81         }
82         
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;
91                 
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;
99         }
100
101         return name;
102 }
103
104 #ifdef USE_SHM
105
106 static gchar *_wapi_shm_shm_name (_wapi_shm_t type)
107 {
108         char *base_name = _wapi_shm_base_name (type);
109
110         /* Also add the uid to avoid permission problems */
111         char *res = g_strdup_printf ("/mono-shared-%d-%s", getuid (), base_name);
112
113         g_free (base_name);
114
115         return res;
116 }
117
118 static int
119 _wapi_shm_open (const char *filename, int size)
120 {
121         int fd;
122
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 ();
130         }
131
132         return fd;
133 }
134
135 #endif
136
137 static gchar *_wapi_shm_file (_wapi_shm_t type)
138 {
139         static gchar file[_POSIX_PATH_MAX];
140         gchar *name = NULL, *filename, *dir, *wapi_dir;
141
142         name = _wapi_shm_base_name (type);
143
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.
147          */
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);
154         }
155         g_free (name);
156
157         g_snprintf (file, _POSIX_PATH_MAX, "%s", filename);
158         g_free (filename);
159                 
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.
163          */
164         dir = g_path_get_dirname (file);
165         mkdir (dir, 0755);
166         g_free (dir);
167         
168         return(file);
169 }
170
171 static int _wapi_shm_file_open (const gchar *filename, guint32 wanted_size)
172 {
173         int fd;
174         struct stat statbuf;
175         int ret, tries = 0;
176         gboolean created = FALSE;
177         mode_t oldmask;
178
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);
186         }
187         
188         /* Make sure future processes can open the shared data files */
189         oldmask = umask (066);
190
191         /* No O_CREAT yet, because we need to initialise the file if
192          * we have to create it.
193          */
194         fd = open (filename, O_RDWR, 0600);
195         umask (oldmask);
196         
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
201                  */
202                 oldmask = umask (066);
203                 fd = open (filename, O_CREAT|O_EXCL|O_RDWR, 0600);
204                 umask (oldmask);
205                 
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
210                          */
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.
219                          *
220                          * (wanted_size-1, because we're about to
221                          * write the other byte to actually expand the
222                          * file.)
223                          */
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);
229                         }
230                         
231                         do {
232                                 ret = write (fd, "", 1);
233                         } while (ret == -1 && errno == EINTR);
234                                 
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);
240                         }
241                         
242                         created = TRUE;
243
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
248                          */
249                 }
250         } else if (fd == -1) {
251                 g_critical ("%s: shared file [%s] open error: %s", __func__,
252                             filename, g_strerror (errno));
253                 return(-1);
254         }
255         
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.)
259          */
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);
265                 }
266                 close (fd);
267                 return(-1);
268         }
269
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;
280                 }
281         }
282         
283         return(fd);
284 }
285
286 static gboolean check_disabled (void)
287 {
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;
292                 }
293         }
294
295         return(_wapi_shm_disabled);
296 }
297
298 /*
299  * _wapi_shm_attach:
300  * @success: Was it a success
301  *
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.
304  */
305 gpointer _wapi_shm_attach (_wapi_shm_t type)
306 {
307         gpointer shm_seg;
308         int fd;
309         struct stat statbuf;
310         gchar *filename = _wapi_shm_file (type), *shm_name;
311         guint32 size;
312
313         switch(type) {
314         case WAPI_SHM_DATA:
315                 size = sizeof(struct _WapiHandleSharedLayout);
316                 break;
317                 
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;
324         }
325
326         if (check_disabled ()) {
327                 return g_malloc0 (size);
328         }
329
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
337
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);
345         }
346
347         if (fstat (fd, &statbuf)==-1) {
348                 g_critical ("%s: fstat error: %s", __func__,
349                             g_strerror (errno));
350                 close (fd);
351                 return(NULL);
352         }
353         
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);
363                 }
364         }
365                 
366         close (fd);
367         return(shm_seg);
368 }
369
370 #ifdef HAVE_SYS_SEM
371 static void shm_semaphores_init (void)
372 {
373         key_t key;
374         key_t oldkey;
375         int thr_ret;
376         struct _WapiHandleSharedLayout *tmp_shared;
377         
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
381          */
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;
390         
391         for (i = 0; i < _WAPI_SHARED_SEM_COUNT; i++) {
392                 def_vals[i] = 1;
393         }
394
395         /* Process count must start at '0' - the 1 for all the others
396          * sets the semaphore to "unlocked"
397          */
398         def_vals[_WAPI_SHARED_SEM_PROCESS_COUNT] = 0;
399         
400         defs.array = def_vals;
401         
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...)
409          */
410         tmp_shared = _wapi_shm_attach (WAPI_SHM_DATA);
411         g_assert (tmp_shared != NULL);
412         
413         key = ftok (_wapi_shm_file (WAPI_SHM_DATA), 'M');
414
415 again:
416         retries++;
417         oldkey = tmp_shared->sem_key;
418
419         if (oldkey == 0) {
420 #ifdef DEBUG
421                 g_message ("%s: Creating with new key (0x%x)", __func__, key);
422 #endif
423
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
428                  */
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);
440                         }
441                         
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
447                 }
448                 /* Got a semaphore array, so initialise it and install
449                  * the key into the shared memory
450                  */
451                 
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));
455
456                         /* Something went horribly wrong, so try
457                          * getting a new set from scratch
458                          */
459                         semctl (_wapi_sem_id, 0, IPC_RMID);
460                         goto again;
461                 }
462
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.
469                          */
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
475                          */
476                         goto done;
477                 }
478         }
479         
480 #ifdef DEBUG
481         g_message ("%s: Trying with old key 0x%x", __func__, oldkey);
482 #endif
483
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));
489
490                 /* Someone must have deleted the semaphore set, so
491                  * blow away the bad key and try again
492                  */
493                 InterlockedCompareExchange (&tmp_shared->sem_key, 0, oldkey);
494                 
495                 goto again;
496         }
497
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);
502         
503 #ifdef DEBUG
504         g_message ("%s: Incrementing the process count (%d)", __func__, _wapi_getpid ());
505 #endif
506
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.
512          */
513         _wapi_shm_sem_unlock (_WAPI_SHARED_SEM_PROCESS_COUNT);
514
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
518         
519         _wapi_shm_sem_unlock (_WAPI_SHARED_SEM_PROCESS_COUNT_LOCK);
520
521         if (_wapi_shm_disabled)
522                 g_free (tmp_shared);
523         else
524                 munmap (tmp_shared, sizeof(struct _WapiHandleSharedLayout));
525 }
526 #endif
527
528 static mono_mutex_t noshm_sems[_WAPI_SHARED_SEM_COUNT];
529
530 static void noshm_semaphores_init (void)
531 {
532         int i;
533
534         for (i = 0; i < _WAPI_SHARED_SEM_COUNT; i++) {
535                 mono_mutex_init (&noshm_sems[i], NULL);
536         }
537 }
538
539 #ifdef HAVE_SYS_SEM
540 static void shm_semaphores_remove (void)
541 {
542         int thr_ret;
543         int proc_count;
544         gchar *shm_name;
545         
546 #ifdef DEBUG
547         g_message ("%s: Checking process count (%d)", __func__,
548                    _wapi_getpid ());
549 #endif
550         
551         thr_ret = _wapi_shm_sem_lock (_WAPI_SHARED_SEM_PROCESS_COUNT_LOCK);
552         g_assert (thr_ret == 0);
553         
554         proc_count = semctl (_wapi_sem_id, _WAPI_SHARED_SEM_PROCESS_COUNT,
555                              GETVAL);
556
557         g_assert (proc_count > 0);
558         if (proc_count == 1) {
559                 /* Just us, so blow away the semaphores and the shared
560                  * files
561                  */
562 #ifdef DEBUG
563                 g_message ("%s: Removing semaphores! (%d)", __func__,
564                            _wapi_getpid ());
565 #endif
566
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);
572
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...
582                  */
583                 _wapi_shm_sem_unlock (_WAPI_SHARED_SEM_PROCESS_COUNT_LOCK);
584         }
585 }
586 #endif
587
588 static void noshm_semaphores_remove (void)
589 {
590         /* No need to do anything */
591 }
592
593 #ifdef HAVE_SYS_SEM
594 static int shm_sem_lock (int sem)
595 {
596         struct sembuf ops;
597         int ret;
598         
599 #ifdef DEBUG
600         g_message ("%s: locking sem %d", __func__, sem);
601 #endif
602
603         ops.sem_num = sem;
604         ops.sem_op = -1;
605         ops.sem_flg = SEM_UNDO;
606         
607   retry:
608         do {
609                 ret = semop (_wapi_sem_id, &ops, 1);
610         } while (ret == -1 && errno == EINTR);
611
612         if (ret == -1) {
613                 /* EINVAL covers the case when the semaphore was
614                  * deleted before we started the semop
615                  */
616                 if (errno == EIDRM || errno == EINVAL) {
617                         /* Someone blew away this semaphore set, so
618                          * get a new one and try again
619                          */
620 #ifdef DEBUG
621                         g_message ("%s: Reinitialising the semaphores!",
622                                    __func__);
623 #endif
624
625                         _wapi_shm_semaphores_init ();
626                         goto retry;
627                 }
628                 
629                 /* Turn this into a pthreads-style return value */
630                 ret = errno;
631         }
632         
633 #ifdef DEBUG
634         g_message ("%s: returning %d (%s)", __func__, ret, g_strerror (ret));
635 #endif
636         
637         return(ret);
638 }
639 #endif
640
641 static int noshm_sem_lock (int sem)
642 {
643         int ret;
644         
645 #ifdef DEBUG
646         g_message ("%s: locking nosem %d", __func__, sem);
647 #endif
648         
649         ret = mono_mutex_lock (&noshm_sems[sem]);
650         
651         return(ret);
652 }
653
654 #ifdef HAVE_SYS_SEM
655 static int shm_sem_trylock (int sem)
656 {
657         struct sembuf ops;
658         int ret;
659         
660 #ifdef DEBUG
661         g_message ("%s: trying to lock sem %d", __func__, sem);
662 #endif
663         
664         ops.sem_num = sem;
665         ops.sem_op = -1;
666         ops.sem_flg = IPC_NOWAIT | SEM_UNDO;
667         
668   retry:
669         do {
670                 ret = semop (_wapi_sem_id, &ops, 1);
671         } while (ret == -1 && errno == EINTR);
672
673         if (ret == -1) {
674                 /* EINVAL covers the case when the semaphore was
675                  * deleted before we started the semop
676                  */
677                 if (errno == EIDRM || errno == EINVAL) {
678                         /* Someone blew away this semaphore set, so
679                          * get a new one and try again
680                          */
681 #ifdef DEBUG
682                         g_message ("%s: Reinitialising the semaphores!",
683                                    __func__);
684 #endif
685
686                         _wapi_shm_semaphores_init ();
687                         goto retry;
688                 }
689                 
690                 /* Turn this into a pthreads-style return value */
691                 ret = errno;
692         }
693         
694         if (ret == EAGAIN) {
695                 /* But pthreads uses this code instead */
696                 ret = EBUSY;
697         }
698         
699 #ifdef DEBUG
700         g_message ("%s: returning %d (%s)", __func__, ret, g_strerror (ret));
701 #endif
702         
703         return(ret);
704 }
705 #endif
706
707 static int noshm_sem_trylock (int sem)
708 {
709         int ret;
710         
711 #ifdef DEBUG
712         g_message ("%s: trying to lock nosem %d", __func__, sem);
713 #endif
714         
715         ret = mono_mutex_trylock (&noshm_sems[sem]);
716         
717         return(ret);
718 }
719
720 #ifdef HAVE_SYS_SEM
721 static int shm_sem_unlock (int sem)
722 {
723         struct sembuf ops;
724         int ret;
725         
726 #ifdef DEBUG
727         g_message ("%s: unlocking sem %d", __func__, sem);
728 #endif
729         
730         ops.sem_num = sem;
731         ops.sem_op = 1;
732         ops.sem_flg = SEM_UNDO;
733         
734   retry:
735         do {
736                 ret = semop (_wapi_sem_id, &ops, 1);
737         } while (ret == -1 && errno == EINTR);
738
739         if (ret == -1) {
740                 /* EINVAL covers the case when the semaphore was
741                  * deleted before we started the semop
742                  */
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)
747                          */
748 #ifdef DEBUG
749                         g_message ("%s: Reinitialising the semaphores!",
750                                    __func__);
751 #endif
752
753                         _wapi_shm_semaphores_init ();
754                         goto retry;
755                 }
756                 
757                 /* Turn this into a pthreads-style return value */
758                 ret = errno;
759         }
760         
761 #ifdef DEBUG
762         g_message ("%s: returning %d (%s)", __func__, ret, g_strerror (ret));
763 #endif
764
765         return(ret);
766 }
767 #endif
768
769 static int noshm_sem_unlock (int sem)
770 {
771         int ret;
772         
773 #ifdef DEBUG
774         g_message ("%s: unlocking nosem %d", __func__, sem);
775 #endif
776         
777         ret = mono_mutex_unlock (&noshm_sems[sem]);
778         
779         return(ret);
780 }
781
782 void _wapi_shm_semaphores_init (void)
783 {
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 ();
791         }
792 #endif
793 }
794
795 void _wapi_shm_semaphores_remove (void)
796 {
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 ();
804         }
805 #endif
806 }
807
808 int _wapi_shm_sem_lock (int sem)
809 {
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));
817         }
818 #endif
819 }
820
821 int _wapi_shm_sem_trylock (int sem)
822 {
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));
830         }
831 #endif
832 }
833
834 int _wapi_shm_sem_unlock (int sem)
835 {
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));
843         }
844 #endif
845 }