Welcome to mirror list, hosted at ThFree Co, Russian Federation.

shm.cc « cygwin « winsup - cygwin.com/git/newlib-cygwin.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 92ea20f8697ed65c05b2d3ea090b6a02619cffbb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/* shm.cc: XSI IPC interface for Cygwin.

   Copyright 2003 Red Hat, Inc.

This file is part of Cygwin.

This software is a copyrighted work licensed under the terms of the
Cygwin license.  Please consult the file "CYGWIN_LICENSE" for
details. */

#include "winsup.h"
#include "cygerrno.h"
#include <signal.h>
#ifdef USE_SERVER
#include <sys/types.h>
#include <sys/queue.h>
#include <stdio.h>
#include <unistd.h>

#include "pinfo.h"
#include "sigproc.h"

#include "cygserver_ipc.h"
#include "cygserver_shm.h"

/*
 * client_request_shm Constructors
 */

client_request_shm::client_request_shm (int shmid,
					const void *shmaddr,
					int shmflg)
  : client_request (CYGSERVER_REQUEST_SHM, &_parameters, sizeof (_parameters))
{
  _parameters.in.shmop = SHMOP_shmat;
  ipc_set_proc_info (_parameters.in.ipcblk);

  _parameters.in.atargs.shmid = shmid;
  _parameters.in.atargs.shmaddr = shmaddr;
  _parameters.in.atargs.shmflg = shmflg;

  msglen (sizeof (_parameters.in));
}

client_request_shm::client_request_shm (int shmid,
					int cmd,
					struct shmid_ds *buf)
  : client_request (CYGSERVER_REQUEST_SHM, &_parameters, sizeof (_parameters))
{
  _parameters.in.shmop = SHMOP_shmctl;
  ipc_set_proc_info (_parameters.in.ipcblk);

   _parameters.in.ctlargs.shmid = shmid;
   _parameters.in.ctlargs.cmd = cmd;
   _parameters.in.ctlargs.buf = buf;

  msglen (sizeof (_parameters.in));
}

client_request_shm::client_request_shm (const void *shmaddr)
  : client_request (CYGSERVER_REQUEST_SHM, &_parameters, sizeof (_parameters))
{
  _parameters.in.shmop = SHMOP_shmdt;
  ipc_set_proc_info (_parameters.in.ipcblk);

  _parameters.in.dtargs.shmaddr = shmaddr;

  msglen (sizeof (_parameters.in));
}

client_request_shm::client_request_shm (key_t key,
					size_t size,
					int shmflg)
  : client_request (CYGSERVER_REQUEST_SHM, &_parameters, sizeof (_parameters))
{
  _parameters.in.shmop = SHMOP_shmget;
  ipc_set_proc_info (_parameters.in.ipcblk);

  _parameters.in.getargs.key = key;
  _parameters.in.getargs.size = size;
  _parameters.in.getargs.shmflg = shmflg;

  msglen (sizeof (_parameters.in));
}

client_request_shm::client_request_shm (proc *p1)
  : client_request (CYGSERVER_REQUEST_SHM, &_parameters, sizeof (_parameters))
{
  _parameters.in.shmop = SHMOP_shmfork;
  ipc_set_proc_info (_parameters.in.ipcblk);

  _parameters.in.forkargs = *p1;
}

/* List of shmid's with file mapping HANDLE and size, returned by shmget. */
struct shm_shmid_list {
  SLIST_ENTRY(shm_shmid_list) ssh_next;
  int shmid;
  vm_object_t hdl;
  size_t size;
};

static SLIST_HEAD(, shm_shmid_list) ssh_list;

/* List of attached mappings, as returned by shmat. */
struct shm_attached_list {
  SLIST_ENTRY(shm_attached_list) sph_next;
  vm_object_t ptr;
  vm_object_t hdl;
  size_t size;
  int access;
};

static SLIST_HEAD(, shm_attached_list) sph_list;

int __stdcall
fixup_shms_after_fork ()
{
  if (!SLIST_FIRST (&sph_list))
    return 0;
  pinfo p (myself->ppid);
  proc parent = { myself->ppid, p->dwProcessId, p->uid, p->gid };

  client_request_shm request (&parent);
  if (request.make_request () == -1 || request.retval () == -1)
    {
      syscall_printf ("-1 [%d] = shmctl ()", request.error_code ());
      set_errno (request.error_code ());
      return 0;
    }
  shm_attached_list *sph_entry;
  /* Remove map from list... */
  SLIST_FOREACH (sph_entry, &sph_list, sph_next)
    {
      vm_object_t ptr = MapViewOfFileEx(sph_entry->hdl, sph_entry->access,
      					0, 0, sph_entry->size, sph_entry->ptr);
      if (ptr != sph_entry->ptr)
        api_fatal ("MapViewOfFileEx (%p), %E.  Terminating.", sph_entry->ptr);
    }
  return 0;
}
#endif /* USE_SERVER */

/*
 * XSI shmaphore API.  These are exported by the DLL.
 */

extern "C" void *
shmat (int shmid, const void *shmaddr, int shmflg)
{
#ifdef USE_SERVER
  syscall_printf ("shmat (shmid = %d, shmaddr = %p, shmflg = 0x%x)",
		  shmid, shmaddr, shmflg);

  shm_shmid_list *ssh_entry;
  SLIST_FOREACH (ssh_entry, &ssh_list, ssh_next)
    {
      if (ssh_entry->shmid == shmid)
	break;
    }
  if (!ssh_entry)
    {
      /* Invalid shmid */
      set_errno (EINVAL);
      return NULL;
    }
  vm_object_t attach_va = NULL;
  if (shmaddr)
    {
      if (shmflg & SHM_RND)
        attach_va = (vm_object_t)((vm_offset_t)shmaddr & ~(SHMLBA-1));
      else
	attach_va = (vm_object_t)shmaddr;
      /* Don't even bother to call anything if shmaddr is NULL or
         not aligned. */
      if (!attach_va || (vm_offset_t)attach_va % SHMLBA)
	{
	  set_errno (EINVAL);
	  return NULL;
	}
    }
  /* Try allocating memory before calling cygserver. */
  shm_attached_list *sph_entry = new (shm_attached_list);
  if (!sph_entry)
    {
      set_errno (ENOMEM);
      return NULL;
    }
  DWORD access = (shmflg & SHM_RDONLY) ? FILE_MAP_READ : FILE_MAP_WRITE;
  vm_object_t ptr = MapViewOfFileEx(ssh_entry->hdl, access, 0, 0,
				    ssh_entry->size, attach_va);
  if (!ptr)
    {
      __seterrno ();
      delete sph_entry;
      return NULL;
    }
  /* Use returned ptr address as is, so it's stored using the exact value
     in cygserver. */
  client_request_shm request (shmid, ptr, shmflg & ~SHM_RND);
  if (request.make_request () == -1 || request.ptrval () == NULL)
    {
      syscall_printf ("-1 [%d] = shmctl ()", request.error_code ());
      UnmapViewOfFile (ptr);
      delete sph_entry;
      set_errno (request.error_code ());
      if (request.error_code () == ENOSYS)
        raise (SIGSYS);
      return NULL;
    }
  sph_entry->ptr = ptr;
  sph_entry->hdl = ssh_entry->hdl;
  sph_entry->size = ssh_entry->size;
  sph_entry->access = access;
  SLIST_INSERT_HEAD (&sph_list, sph_entry, sph_next);
  return ptr;
#else
  set_errno (ENOSYS);
  raise (SIGSYS);
  return NULL;
#endif
}

extern "C" int
shmctl (int shmid, int cmd, struct shmid_ds *buf)
{
#ifdef USE_SERVER
  syscall_printf ("shmctl (shmid = %d, cmd = %d, buf = 0x%x)",
		  shmid, cmd, buf);
  switch (cmd)
    {
      case IPC_STAT:
      case IPC_SET:
	if (__check_null_invalid_struct_errno (buf, sizeof (struct shmid_ds)))
	  return -1;
        break;
      case IPC_INFO:
	/* shmid == 0: Request for shminfo struct. */
	if (!shmid
	    && __check_null_invalid_struct_errno (buf, sizeof (struct shminfo)))
	    return -1;
	/* Otherwise, request shmid entries from internal shmid_ds array. */
	if (shmid)
	  if (__check_null_invalid_struct_errno (buf, shmid * sizeof (struct shmid_ds)))
	    return -1;
        break;
      case SHM_INFO:
        if (__check_null_invalid_struct_errno (buf, sizeof (struct shm_info)))
	  return -1;
        break;
    }
  client_request_shm request (shmid, cmd, buf);
  if (request.make_request () == -1 || request.retval () == -1)
    {
      syscall_printf ("-1 [%d] = shmctl ()", request.error_code ());
      set_errno (request.error_code ());
      if (request.error_code () == ENOSYS)
        raise (SIGSYS);
      return -1;
    }
  if (cmd == IPC_RMID)
    {
      /* The process must cleanup its own storage... */
      shm_shmid_list *ssh_entry, *ssh_next_entry;
      SLIST_FOREACH_SAFE (ssh_entry, &ssh_list, ssh_next, ssh_next_entry)
        {
	  if (ssh_entry->shmid == shmid)
	    {
	      SLIST_REMOVE (&ssh_list, ssh_entry, shm_shmid_list, ssh_next);
	      /* ...and close the handle. */
	      CloseHandle (ssh_entry->hdl);
	      delete ssh_entry;
	      break;
	    }
	}
    }
  return request.retval ();
#else
  set_errno (ENOSYS);
  raise (SIGSYS);
  return -1;
#endif
}

extern "C" int
shmdt (const void *shmaddr)
{
#ifdef USE_SERVER
  syscall_printf ("shmget (shmaddr = %p)", shmaddr);
  client_request_shm request (shmaddr);
  if (request.make_request () == -1 || request.retval () == -1)
    {
      syscall_printf ("-1 [%d] = shmctl ()", request.error_code ());
      set_errno (request.error_code ());
      if (request.error_code () == ENOSYS)
        raise (SIGSYS);
      return -1;
    }
  shm_attached_list *sph_entry, *sph_next_entry;
  /* Remove map from list... */
  SLIST_FOREACH_SAFE (sph_entry, &sph_list, sph_next, sph_next_entry)
    {
      if (sph_entry->ptr == shmaddr)
        {
	  SLIST_REMOVE (&sph_list, sph_entry, shm_attached_list, sph_next);
	  /* ...and unmap view. */
	  UnmapViewOfFile (sph_entry->ptr);
	  delete sph_entry;
	  break;
	}
    }
  return request.retval ();
#else
  set_errno (ENOSYS);
  raise (SIGSYS);
  return -1;
#endif
}

extern "C" int
shmget (key_t key, size_t size, int shmflg)
{
#ifdef USE_SERVER
  syscall_printf ("shmget (key = %U, size = %d, shmflg = 0x%x)",
		  key, size, shmflg);
  /* Try allocating memory before calling cygserver. */
  shm_shmid_list *ssh_new_entry = new (shm_shmid_list);
  if (!ssh_new_entry)
    {
      set_errno (ENOMEM);
      return -1;
    }
  client_request_shm request (key, size, shmflg);
  if (request.make_request () == -1 || request.retval () == -1)
    {
      syscall_printf ("-1 [%d] = shmctl ()", request.error_code ());
      delete ssh_new_entry;
      set_errno (request.error_code ());
      if (request.error_code () == ENOSYS)
        raise (SIGSYS);
      return -1;
    }
  int shmid = request.retval ();	/* Shared mem ID */
  vm_object_t hdl = request.objval ();	/* HANDLE associated with it. */
  shm_shmid_list *ssh_entry;
  SLIST_FOREACH (ssh_entry, &ssh_list, ssh_next)
    {
      if (ssh_entry->shmid == shmid)
        {
	  /* We already maintain an entry for this shmid.  That means,
	     the hdl returned by cygserver is a superfluous duplicate
	     of the original hdl maintained by cygserver.  We can safely
	     delete it. */
	  CloseHandle (hdl);
	  delete ssh_new_entry;
	  return shmid;
	}
    }
  /* We arrive here only if shmid is a new one for this process.  Add the
     shmid and hdl value to the list. */
  ssh_new_entry->shmid = shmid;
  ssh_new_entry->hdl = hdl;
  ssh_new_entry->size = size;
  SLIST_INSERT_HEAD (&ssh_list, ssh_new_entry, ssh_next);
  return shmid;
#else
  set_errno (ENOSYS);
  raise (SIGSYS);
  return -1;
#endif
}