summaryrefslogtreecommitdiff
path: root/nuttx/mm/shm/shmget.c
blob: 35bb7c26ac443cefd2ef8952ffd5c7f7f1324718 (plain) (blame)
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
/****************************************************************************
 * mm/shm/shmget.c
 *
 *   Copyright (C) 2014 Gregory Nutt. All rights reserved.
 *   Author: Gregory Nutt <gnutt@nuttx.org>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 * 3. Neither the name NuttX nor the names of its contributors may be
 *    used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 ****************************************************************************/

/****************************************************************************
 * Included Files
 ****************************************************************************/

#include <nuttx/config.h>

#include <sys/shm.h>
#include <sys/ipc.h>
#include <unistd.h>
#include <semaphore.h>
#include <string.h>
#include <errno.h>

#include <nuttx/pgalloc.h>
#include <nuttx/mm/shm.h>

#include "shm/shm.h"

#ifdef CONFIG_MM_SHM

/****************************************************************************
 * Pre-processor Definitions
 ****************************************************************************/

/****************************************************************************
 * Private Types
 ****************************************************************************/

/****************************************************************************
 * Private Data
 ****************************************************************************/

/****************************************************************************
 * Private Functions
 ****************************************************************************/

/****************************************************************************
 * Name: shm_find
 *
 * Description:
 *   Find the shared memory region with matching key
 *
 * Input parameters:
 *   key - The value that uniquely identifies a shared memory region.
 *
 * Returned value:
 *   On success, an index in the range of 0 to CONFIG_ARCH_SHM_MAXREGIONS-1
 *   is returned to identify the matching region; -ENOENT is returned on
 *   failure.
 *
 ****************************************************************************/

static int shm_find(key_t key)
{
  int i;

  for (i = 0; i < CONFIG_ARCH_SHM_MAXREGIONS; i++)
    {
      if (g_shminfo.si_region[i].sr_key == key)
        {
          return i;
        }
    }

  return -ENOENT;
}

/****************************************************************************
 * Name: shm_reserve
 *
 * Description:
 *   Allocate an unused shared memory region.  That is one with a key of -1
 *
 * Input parameters:
 *   None
 *
 * Returned value:
 *   On success, an index in the range of 0 to CONFIG_ARCH_SHM_MAXREGIONS-1
 *   is returned to identify the matching region; -ENOSPC is returned on
 *   failure.
 *
 ****************************************************************************/

static int shm_reserve(key_t key, int shmflg)
{
  FAR struct shm_region_s *region;
  int i;

  for (i = 0; i < CONFIG_ARCH_SHM_MAXREGIONS; i++)
    {
      /* Is this region in use? */

      region = &g_shminfo.si_region[i];
      if (region->sr_flags == SRFLAG_AVAILABLE)
        {
          /* No... reserve it for the caller now */

          memset(region, 0, sizeof(struct shm_region_s));
          region->sr_key   = key;
          region->sr_flags = SRFLAG_INUSE;

          sem_init(&region->sr_sem, 0, 1);

          /* Set the low-order nine bits of shm_perm.mode to the low-order
           * nine bits of shmflg.
           */

          region->sr_ds.shm_perm.mode = shmflg & IPC_MODE;

          /* The value of shm_segsz is left equal to zero for now because no
           * memory has yet been allocated.
           *
           * The values of shm_lpid, shm_nattch, shm_atime, and shm_dtime are
           * set equal to 0.
           */

          /* The value of shm_ctime is set equal to the current time. */

          region->sr_ds.shm_ctime = time(NULL);
          return i;
        }
    }

  return -ENOSPC;
}

/****************************************************************************
 * Name: shm_extend
 *
 * Description:
 *   Extend the size of a memory regions by allocating physical pages as
 *   necessary
 *
 * Input parameters:
 *   shmid - The index of the region of interest in the shared memory region
 *     table.
 *   size - The new size of the region.
 *
 * Returned value:
 *   Zero is returned on success; -ENOMEM is returned on failure.
 *   (Should a different error be returned if the region is just too big?)
 *
 ****************************************************************************/

static int shm_extend(int shmid, size_t size)
{
  FAR struct shm_region_s *region =  &g_shminfo.si_region[shmid];
  unsigned int pgalloc;
  unsigned int pgneeded;

  /* This is the number of pages that are needed to satisfy the allocation */

  pgneeded = MM_PGALIGNUP(size);

  /* This is the number of pages that have already been allocated */

  pgalloc = MM_PGALIGNUP(region->sr_ds.shm_segsz);

  /* Loop until all pages have been allocated (or something bad happens) */

  while (pgalloc < pgneeded && pgalloc < CONFIG_ARCH_SHM_NPAGES)
    {
      /* Allocate one more physical page */

      region->sr_pages[pgalloc] = mm_pgalloc(1);
      if (region->sr_pages[pgalloc] == 0)
        {
          shmdbg("mm_pgalloc(1) failed\n");
          break;
        }

      /* Increment the number of pages successully allocated */

      pgalloc++;
    }

  /* We get here (1) because all of the pages were successfully, (2) because
   * mm_pgalloc() failed, or (3) because any additional pages allocated
   * would exceed CONFIG_ARCH_SHM_NPAGES.
   */

  if (pgalloc < pgneeded)
    {
      /* Set the amount memory available which will be less than the
       * requested size.
       */

      region->sr_ds.shm_segsz = pgalloc << MM_PGSHIFT;
      return -ENOMEM;
    }

  /* Set the new region size and return success */

  region->sr_ds.shm_segsz = size;
  return OK;
}

/****************************************************************************
 * Name: shm_create
 *
 * Description:
 *   Create the shared memory region.
 *
 * Input parameters:
 *   key     - The key that is used to access the unique shared memory
 *             identifier.
 *   size    - The shared memory region that is created will be at least
 *             this size in bytes.
 *   shmflgs - See IPC_* definitions in sys/ipc.h.  Only the values
 *             IPC_PRIVATE or IPC_CREAT are supported.
 *
 * Returned value:
 *   Zero is returned on success;  A negated errno value is returned on
 *   failure.
 *
 ****************************************************************************/

static int shm_create(key_t key, size_t size, int shmflg)
{
  FAR struct shm_region_s *region;
  int shmid;
  int ret;

  /* Reserve the shared memory region */

  ret = shm_reserve(key, shmflg);
  if (ret < 0)
    {
      shmdbg("shm_reserve failed: %d\n", ret);
      return ret;
    }

  /* Save the shared memory ID */

  shmid = ret;

  /* Then allocate the physical memory (by extending it from the initial
   * size of zero).
   */

  ret = shm_reserve(shmid, size);
  if (ret < 0)
    {
      /* Free any partial allocations and unreserve the region */

      shm_destroy(shmid);
      return ret;
    }

  /* Save the process ID of the creator */

  region = &g_shminfo.si_region[shmid];
  region->sr_ds.shm_cpid = getpid();

  /* Return the shared memory ID */

  return shmid;
}

/****************************************************************************
 * Public Functions
 ****************************************************************************/

/****************************************************************************
 * Name: shmget
 *
 * Description:
 *   The shmget() function will return the shared memory identifier
 *   associated with key.
 *
 *   A shared memory identifier, associated data structure, and shared
 *   memory segment of at least size bytes is created for key if one of the
 *   following is true:
 *
 *     - The argument key is equal to IPC_PRIVATE.
 *     - The argument key does not already have a shared memory identifier
 *       associated with it and (shmflg & IPC_CREAT) is non-zero.
 *
 *   Upon creation, the data structure associated with the new shared memory
 *   identifier will be initialized as follows:
 *
 *     - The low-order nine bits of shm_perm.mode are set equal to the low-
 *       order nine bits of shmflg.
 *     - The value of shm_segsz is set equal to the value of size.
 *     - The values of shm_lpid, shm_nattch, shm_atime, and shm_dtime are
 *       set equal to 0.
 *     - The value of shm_ctime is set equal to the current time.
 *
 *   When the shared memory segment is created, it will be initialized with
 *   all zero values.
 *
 * Input Parameters:
 *   key     - The key that is used to access the unique shared memory
 *             identifier.
 *   size    - The shared memory region that is created will be at least
 *             this size in bytes.
 *   shmflgs - See IPC_* definitions in sys/ipc.h.  Only the values
 *             IPC_PRIVATE or IPC_CREAT are supported.
 *
 * Returned Value:
 *   Upon successful completion, shmget() will return a non-negative
 *   integer, namely a shared memory identifier; otherwise, it will return
 *   -1 and set errno to indicate the error.
 *
 *     - EACCES
 *       A shared memory identifier exists for key but operation permission
 *       as specified by the low-order nine bits of shmflg would not be
 *       granted.
 *     - EEXIST
 *       A shared memory identifier exists for the argument key but
 *      (shmflg & IPC_CREAT) && (shmflg & IPC_EXCL) are non-zero.
 *     - EINVAL
 *       A shared memory segment is to be created and the value of size is
 *       less than the system-imposed minimum or greater than the system-
 *       imposed maximum.
 *     - EINVAL
 *       No shared memory segment is to be created and a shared memory
 *       segment exists for key but the size of the segment associated with
 *       it is less than size and size is not 0.
 *     - ENOENT
 *       A shared memory identifier does not exist for the argument key and
 *       (shmflg & IPC_CREAT) is 0.
 *     - ENOMEM
 *       A shared memory identifier and associated shared memory segment
 *       will be created, but the amount of available physical memory is
 *       not sufficient to fill the request.
 *     - ENOSPC
 *       A shared memory identifier is to be created, but the system-imposed
 *       limit on the maximum number of allowed shared memory identifiers
 *       system-wide would be exceeded.
 *
 * POSIX Deviations:
 *     - The values of shm_perm.cuid, shm_perm.uid, shm_perm.cgid, and
 *       shm_perm.gid should be set equal to the effective user ID and
 *       effective group ID, respectively, of the calling process.
 *       The NuttX ipc_perm structure, however, does not support these
 *       fields because user and group IDs are not yet supported by NuttX.
 *
 ****************************************************************************/

int shmget(key_t key, size_t size, int shmflg)
{
  FAR struct shm_region_s *region;
  int shmid = -1;
  int ret;

  /* Check for the special case where the caller doesn't really want shared
   * memory (they why do they bother to call us?)
   */

  if (key == IPC_PRIVATE)
    {
      /* Not yet implemented */

      ret = -ENOSYS;
      goto errout;
    }

  /* Get exclusive access to the global list of shared memory regions */

  ret = sem_wait(&g_shminfo.si_sem);
  if (ret >= 0)
    {
      /* Find the requested memory region */

      ret = shm_find(key);
      if (ret < 0)
        {
          /* The memory region does not exist.. create it if IPC_CREAT is
           * included in the shmflags.
           */

          if ((shmflg & IPC_CREAT) != 0)
            {
              /* Create the memory region */

              ret = shm_create(key, size, shmflg);
              if (ret < 0)
                {
                  shmdbg("shm_create failed: %d\n", ret);
                  goto errout_with_semaphore;
                }

              /* Return the shared memory ID */

              shmid = ret;
            }
          else
            {
              /* Fail with ENOENT */

              goto errout_with_semaphore;
            }
        }

      /* The region exists */

      else
        {
          /* Remember the shared memory ID */

          shmid = ret;

          /* Is the region big enough for the request? */

          region = &g_shminfo.si_region[shmid];
          if (region->sr_ds.shm_segsz < size)
            {
              /* We we asked to create the region?  If so we can just
               * extend it.
               *
               * REVISIT: We should check the mode bits of the regions
               * first
               */

              if ((shmflg & IPC_CREAT) != 0)
                {
                  /* Extend the region */

                  ret = shm_extend(shmid, size);
                  if (ret < 0)
                    {
                      shmdbg("shm_create failed: %d\n", ret);
                      goto errout_with_semaphore;
                    }
                }
              else
                {
                  /* Fail with EINVAL */

                  ret = -EINVAL;
                  goto errout_with_semaphore;
                }
            }

          /* The region is already big enough or else we successfully
           * extended the size of the region.  If the region was previously
           * deleted, but waiting for processes to detach from the region,
           * then it is no longer deleted.
           */

          region->sr_flags = SRFLAG_INUSE;
        }

      /* Release our lock on the shared memory region list */

      sem_post(&g_shminfo.si_sem);
    }

  return shmid;

errout_with_semaphore:
  sem_post(&g_shminfo.si_sem);
errout:
  set_errno(-ret);
  return ERROR;
}

#endif /* CONFIG_MM_SHM */