2 * shared.c: Shared memory handling, and daemon launching
5 * Dick Porter (dick@ximian.com)
7 * (C) 2002 Ximian, Inc.
17 #include <sys/types.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>
32 static guchar *_wapi_shm_file (_wapi_shm_t type)
34 static guchar file[_POSIX_PATH_MAX];
35 guchar *name = NULL, *filename, *dir, *wapi_dir;
36 gchar machine_name[256];
44 ubuf.machine[0] = '\0';
45 ubuf.sysname[0] = '\0';
48 fake_name = g_getenv ("MONO_SHARED_HOSTNAME");
49 if (fake_name == NULL) {
50 if (gethostname(machine_name, sizeof(machine_name)) != 0)
51 machine_name[0] = '\0';
53 len = MIN (strlen (fake_name), sizeof (machine_name) - 1);
54 strncpy (machine_name, fake_name, len);
55 machine_name [len] = '\0';
60 name = g_strdup_printf ("shared_data-%s-%s-%s-%d-%d-%d",
61 machine_name, ubuf.sysname,
63 (int) sizeof(struct _WapiHandleShared),
64 _WAPI_HANDLE_VERSION, 0);
67 case WAPI_SHM_FILESHARE:
68 name = g_strdup_printf ("shared_fileshare-%s-%s-%s-%d-%d-%d",
69 machine_name, ubuf.sysname,
71 (int) sizeof(struct _WapiFileShare),
72 _WAPI_HANDLE_VERSION, 0);
76 /* I don't know how nfs affects mmap. If mmap() of files on
77 * nfs mounts breaks, then there should be an option to set
80 wapi_dir = getenv ("MONO_SHARED_DIR");
81 if (wapi_dir == NULL) {
82 filename = g_build_filename (g_get_home_dir (), ".wapi", name,
85 filename = g_build_filename (wapi_dir, ".wapi", name, NULL);
89 g_snprintf (file, _POSIX_PATH_MAX, "%s", filename);
92 /* No need to check if the dir already exists or check
93 * mkdir() errors, because on any error the open() call will
96 dir = g_path_get_dirname (file);
103 static int _wapi_shm_file_open (const guchar *filename, guint32 wanted_size)
108 gboolean created = FALSE;
111 /* No O_CREAT yet, because we need to initialise the file if
112 * we have to create it.
114 fd = open (filename, O_RDWR, 0600);
115 if (fd == -1 && errno == ENOENT) {
116 /* OK, its up to us to create it. O_EXCL to avoid a
117 * race condition where two processes can
118 * simultaneously try and create the file
120 fd = open (filename, O_CREAT|O_EXCL|O_RDWR, 0600);
121 if (fd == -1 && errno == EEXIST) {
122 /* It's possible that the file was created in
123 * between finding it didn't exist, and trying
124 * to create it. Just try opening it again
127 } else if (fd == -1) {
128 g_critical ("%s: shared file [%s] open error: %s",
129 __func__, filename, g_strerror (errno));
132 /* We created the file, so we need to expand
135 * (wanted_size-1, because we're about to
136 * write the other byte to actually expand the
139 if (lseek (fd, wanted_size-1, SEEK_SET) == -1) {
140 g_critical ("%s: shared file [%s] lseek error: %s", __func__, filename, g_strerror (errno));
147 ret = write (fd, "", 1);
148 } while (ret == -1 && errno == EINTR);
151 g_critical ("%s: shared file [%s] write error: %s", __func__, filename, g_strerror (errno));
159 /* The contents of the file is set to all
160 * zero, because it is opened up with lseek,
161 * so we don't need to do any more
162 * initialisation here
165 } else if (fd == -1) {
166 g_critical ("%s: shared file [%s] open error: %s", __func__,
167 filename, g_strerror (errno));
171 /* Use stat to find the file size (instead of hard coding it)
172 * because we can expand the file later if needed (for more
173 * handles or scratch space.)
175 if (fstat (fd, &statbuf) == -1) {
176 g_critical ("%s: fstat error: %s", __func__,
178 if (created == TRUE) {
185 if (statbuf.st_size < wanted_size) {
187 if (created == TRUE) {
188 #ifdef HAVE_LARGE_FILE_SUPPORT
189 /* Keep gcc quiet... */
190 g_critical ("%s: shared file [%s] is not big enough! (found %lld, need %d bytes)", __func__, filename, statbuf.st_size, wanted_size);
192 g_critical ("%s: shared file [%s] is not big enough! (found %ld, need %d bytes)", __func__, filename, statbuf.st_size, wanted_size);
197 /* We didn't create it, so just try opening it again */
207 * @success: Was it a success
209 * Attach to the shared memory file or create it if it did not exist.
210 * Returns the memory area the file was mmapped to.
212 gpointer _wapi_shm_attach (_wapi_shm_t type)
217 guchar *filename=_wapi_shm_file (type);
222 size = sizeof(struct _WapiHandleSharedLayout);
225 case WAPI_SHM_FILESHARE:
226 size = sizeof(struct _WapiFileShareLayout);
230 fd = _wapi_shm_file_open (filename, size);
232 g_critical ("%s: shared file [%s] open error", __func__,
237 if (fstat (fd, &statbuf)==-1) {
238 g_critical ("%s: fstat error: %s", __func__,
244 shm_seg = mmap (NULL, statbuf.st_size, PROT_READ|PROT_WRITE,
246 if (shm_seg == MAP_FAILED) {
247 shm_seg = mmap (NULL, statbuf.st_size, PROT_READ|PROT_WRITE,
249 if (shm_seg == MAP_FAILED) {
250 g_critical ("%s: mmap error: %s", __func__, g_strerror (errno));
260 void _wapi_shm_semaphores_init ()
262 key_t key = ftok (_wapi_shm_file (WAPI_SHM_DATA), 'M');
265 /* Yet more barmy API - this union is a well-defined parameter
266 * in a syscall, yet I still have to define it here as it
267 * doesn't appear in a header
271 struct semid_ds *buf;
274 ushort def_vals[_WAPI_SHARED_SEM_COUNT];
278 for (i = 0; i < _WAPI_SHARED_SEM_COUNT; i++) {
281 defs.array = def_vals;
285 oldkey = _wapi_shared_layout->sem_key;
289 g_message ("%s: Creating with new key (0x%x)", __func__, key);
292 /* The while loop attempts to make some sense of the
293 * bonkers 'think of a random number' method of
294 * picking a key without collision with other
297 while ((_wapi_sem_id = semget (key, _WAPI_SHARED_SEM_COUNT,
298 IPC_CREAT | IPC_EXCL | 0600)) == -1) {
299 if (errno == ENOMEM) {
300 g_critical ("%s: semget error: %s", __func__,
302 } else if (errno == ENOSPC) {
303 g_critical ("%s: semget error: %s. Try deleting some semaphores with ipcs and ipcrm", __func__, g_strerror (errno));
304 } else if (errno != EEXIST) {
306 g_warning ("%s: semget error: %s key 0x%x - trying again", __func__,
307 g_strerror (errno), key);
312 g_message ("%s: Got (%s), trying with new key (0x%x)",
313 __func__, g_strerror (errno), key);
316 /* Got a semaphore array, so initialise it and install
317 * the key into the shared memory
320 if (semctl (_wapi_sem_id, 0, SETALL, defs) == -1) {
322 g_warning ("%s: semctl init error: %s - trying again", __func__, g_strerror (errno));
324 /* Something went horribly wrong, so try
325 * getting a new set from scratch
327 semctl (_wapi_sem_id, 0, IPC_RMID);
331 if (InterlockedCompareExchange (&_wapi_shared_layout->sem_key,
333 /* Someone else created one and installed the
334 * key while we were working, so delete the
335 * array we created and fall through to the
336 * 'key already known' case.
338 semctl (_wapi_sem_id, 0, IPC_RMID);
339 oldkey = _wapi_shared_layout->sem_key;
341 /* We've installed this semaphore set's key into
349 g_message ("%s: Trying with old key 0x%x", __func__, oldkey);
352 _wapi_sem_id = semget (oldkey, _WAPI_SHARED_SEM_COUNT, 0600);
353 if (_wapi_sem_id == -1) {
355 g_warning ("%s: semget error opening old key 0x%x (%s) - trying again",
356 __func__, oldkey,g_strerror (errno));
358 /* Someone must have deleted the semaphore set, so
359 * blow away the bad key and try again
361 InterlockedCompareExchange (&_wapi_shared_layout->sem_key, 0,
368 int _wapi_shm_sem_lock (int sem)
374 g_message ("%s: locking sem %d", __func__, sem);
379 ops.sem_flg = SEM_UNDO;
382 ret = semop (_wapi_sem_id, &ops, 1);
383 } while (ret == -1 && errno == EINTR);
386 /* Turn this into a pthreads-style return value */
391 g_message ("%s: returning %d (%s)", __func__, ret, g_strerror (ret));
397 int _wapi_shm_sem_trylock (int sem)
403 g_message ("%s: trying to lock sem %d", __func__, sem);
408 ops.sem_flg = IPC_NOWAIT | SEM_UNDO;
411 ret = semop (_wapi_sem_id, &ops, 1);
412 } while (ret == -1 && errno == EINTR);
415 /* Turn this into a pthreads-style return value */
420 /* But pthreads uses this code instead */
425 g_message ("%s: returning %d (%s)", __func__, ret, g_strerror (ret));
431 int _wapi_shm_sem_unlock (int sem)
437 g_message ("%s: unlocking sem %d", __func__, sem);
442 ops.sem_flg = SEM_UNDO;
445 ret = semop (_wapi_sem_id, &ops, 1);
446 } while (ret == -1 && errno == EINTR);
449 /* Turn this into a pthreads-style return value */
454 g_message ("%s: returning %d (%s)", __func__, ret, g_strerror (ret));