2 * shared.c: Shared memory handling, and daemon launching
5 * Dick Porter (dick@ximian.com)
7 * (C) 2002 Ximian, Inc.
11 * Code to support inter-process sharing of handles.
13 * I thought of using an mmap()ed file for this. If linuxthreads
14 * supported PTHREAD_PROCESS_SHARED I would have done; however without
15 * that pthread support the only other inter-process IPC
16 * synchronisation option is a sysV semaphore, and if I'm going to use
17 * that I may as well take advantage of sysV shared memory too.
18 * Actually, semaphores seem to be buggy, or I was using them
19 * incorrectly :-). I've replaced the sysV semaphore with a shared
20 * integer controlled with Interlocked functions. And I've since
21 * replaced that with a separate process to serialise access to the
22 * shared memory, to avoid the possibility of DOS by leaving the
23 * shared memory locked, and also to allow the shared memory to be
26 * mmap() files have the advantage of avoiding namespace collisions,
27 * but have the disadvantage of needing cleaning up, and also msync().
28 * sysV shared memory has a really stupid way of getting random key
29 * IDs, which can lead to collisions.
31 * Having tried sysv shm, I tested mmap() and found that MAP_SHARED
32 * makes msync() irrelevent, and both types need cleaning up. Seeing
33 * as mmap() doesn't suffer from the bonkers method of allocating
34 * segments, it seems to be the best method.
36 * This shared memory is needed because w32 processes do not have the
37 * POSIX parent-child relationship, so a process handle is available
38 * to any other process to find out exit status. Handles are
39 * destroyed when the last reference to them is closed. New handles
40 * can be created for long lasting items such as processes or threads,
41 * and also for named synchronisation objects so long as these haven't
42 * been deleted by having the last referencing handle closed.
52 #include <sys/types.h>
57 #include <mono/io-layer/wapi.h>
58 #include <mono/io-layer/wapi-private.h>
59 #include <mono/io-layer/shared.h>
63 static guchar *_wapi_shm_file (void)
65 static guchar file[_POSIX_PATH_MAX];
66 guchar *name = NULL, *filename, *dir, *wapi_dir;
67 gchar machine_name[256];
69 if (gethostname(machine_name, sizeof(machine_name)) != 0)
70 machine_name[0] = '\0';
72 /* Change the filename whenever the format of the contents
75 name = g_strdup_printf ("shared_data-%s-%d-%d",
76 machine_name, _WAPI_HANDLE_VERSION, 0);
78 /* I don't know how nfs affects mmap. If mmap() of files on
79 * nfs mounts breaks, then there should be an option to set
82 wapi_dir = getenv ("MONO_SHARED_DIR");
83 if (wapi_dir == NULL) {
84 filename = g_build_filename (g_get_home_dir (), ".wapi", name,
87 filename = g_build_filename (wapi_dir, ".wapi", name, NULL);
91 g_snprintf (file, _POSIX_PATH_MAX, "%s", filename);
94 /* No need to check if the dir already exists or check
95 * mkdir() errors, because on any error the open() call will
98 dir = g_path_get_dirname (file);
105 static guchar *_wapi_fileshare_shm_file (void)
107 static guchar file[_POSIX_PATH_MAX];
108 guchar *name = NULL, *filename, *dir, *wapi_dir;
109 gchar machine_name[256];
111 if (gethostname(machine_name, sizeof(machine_name)) != 0)
112 machine_name[0] = '\0';
114 /* Change the filename whenever the format of the contents
117 name = g_strdup_printf ("shared_fileshare-%s-%d-%d",
118 machine_name, _WAPI_HANDLE_VERSION, 0);
120 /* I don't know how nfs affects mmap. If mmap() of files on
121 * nfs mounts breaks, then there should be an option to set
124 wapi_dir = getenv ("MONO_SHARED_DIR");
125 if (wapi_dir == NULL) {
126 filename = g_build_filename (g_get_home_dir (), ".wapi", name,
129 filename = g_build_filename (wapi_dir, ".wapi", name, NULL);
133 g_snprintf (file, _POSIX_PATH_MAX, "%s", filename);
136 /* No need to check if the dir already exists or check
137 * mkdir() errors, because on any error the open() call will
138 * report the problem.
140 dir = g_path_get_dirname (file);
147 static int _wapi_shm_file_open (const guchar *filename, guint32 wanted_size)
152 gboolean created = FALSE;
155 /* No O_CREAT yet, because we need to initialise the file if
156 * we have to create it.
158 fd = open (filename, O_RDWR, 0600);
159 if (fd == -1 && errno == ENOENT) {
160 /* OK, its up to us to create it. O_EXCL to avoid a
161 * race condition where two processes can
162 * simultaneously try and create the file
164 fd = open (filename, O_CREAT|O_EXCL|O_RDWR, 0600);
165 if (fd == -1 && errno == EEXIST) {
166 /* It's possible that the file was created in
167 * between finding it didn't exist, and trying
168 * to create it. Just try opening it again
171 } else if (fd == -1) {
172 g_critical ("%s: shared file [%s] open error: %s",
173 __func__, filename, g_strerror (errno));
176 /* We created the file, so we need to expand
179 * (wanted_size-1, because we're about to
180 * write the other byte to actually expand the
183 if (lseek (fd, wanted_size-1, SEEK_SET) == -1) {
184 g_critical ("%s: shared file [%s] lseek error: %s", __func__, filename, g_strerror (errno));
191 ret = write (fd, "", 1);
192 } while (ret == -1 && errno == EINTR);
195 g_critical ("%s: shared file [%s] write error: %s", __func__, filename, g_strerror (errno));
203 /* The contents of the file is set to all
204 * zero, because it is opened up with lseek,
205 * so we don't need to do any more
206 * initialisation here
209 } else if (fd == -1) {
210 g_critical ("%s: shared file [%s] open error: %s", __func__,
211 filename, g_strerror (errno));
215 /* Use stat to find the file size (instead of hard coding it)
216 * because we can expand the file later if needed (for more
217 * handles or scratch space.)
219 if (fstat (fd, &statbuf) == -1) {
220 g_critical ("%s: fstat error: %s", __func__,
222 if (created == TRUE) {
229 if (statbuf.st_size < wanted_size) {
231 if (created == TRUE) {
232 #ifdef HAVE_LARGE_FILE_SUPPORT
233 /* Keep gcc quiet... */
234 g_critical ("%s: shared file [%s] is not big enough! (found %lld, need %d bytes)", __func__, filename, statbuf.st_size, wanted_size);
236 g_critical ("%s: shared file [%s] is not big enough! (found %ld, need %d bytes)", __func__, filename, statbuf.st_size, wanted_size);
241 /* We didn't create it, so just try opening it again */
251 * @success: Was it a success
253 * Attach to the shared memory file or create it if it did not exist.
254 * Returns the memory area the file was mmapped to.
256 gpointer _wapi_shm_attach (void)
261 guchar *filename=_wapi_shm_file ();
263 fd=_wapi_shm_file_open (filename,
264 sizeof(struct _WapiHandleSharedLayout));
266 g_critical ("%s: shared file [%s] open error", __func__,
271 if(fstat (fd, &statbuf)==-1) {
272 g_critical ("%s: fstat error: %s", __func__,
278 shm_seg=mmap (NULL, statbuf.st_size, PROT_READ|PROT_WRITE, MAP_SHARED,
280 if(shm_seg==MAP_FAILED) {
281 g_critical ("%s: mmap error: %s", __func__,
291 gpointer _wapi_fileshare_shm_attach (void)
296 guchar *filename=_wapi_fileshare_shm_file ();
298 fd=_wapi_shm_file_open (filename, sizeof(struct _WapiFileShareLayout));
300 g_critical ("%s: shared file [%s] open error", __func__,
305 if(fstat (fd, &statbuf)==-1) {
306 g_critical ("%s: fstat error: %s", __func__,
312 shm_seg=mmap (NULL, statbuf.st_size, PROT_READ|PROT_WRITE, MAP_SHARED,
314 if(shm_seg==MAP_FAILED) {
315 g_critical ("%s: mmap error: %s", __func__,