src/os/unix/ngx_shmem.c - nginx-1.7.10

Functions defined

Source code


  1. /*
  2. * Copyright (C) Igor Sysoev
  3. * Copyright (C) Nginx, Inc.
  4. */


  5. #include <ngx_config.h>
  6. #include <ngx_core.h>


  7. #if (NGX_HAVE_MAP_ANON)

  8. ngx_int_t
  9. ngx_shm_alloc(ngx_shm_t *shm)
  10. {
  11.     shm->addr = (u_char *) mmap(NULL, shm->size,
  12.                                 PROT_READ|PROT_WRITE,
  13.                                 MAP_ANON|MAP_SHARED, -1, 0);

  14.     if (shm->addr == MAP_FAILED) {
  15.         ngx_log_error(NGX_LOG_ALERT, shm->log, ngx_errno,
  16.                       "mmap(MAP_ANON|MAP_SHARED, %uz) failed", shm->size);
  17.         return NGX_ERROR;
  18.     }

  19.     return NGX_OK;
  20. }


  21. void
  22. ngx_shm_free(ngx_shm_t *shm)
  23. {
  24.     if (munmap((void *) shm->addr, shm->size) == -1) {
  25.         ngx_log_error(NGX_LOG_ALERT, shm->log, ngx_errno,
  26.                       "munmap(%p, %uz) failed", shm->addr, shm->size);
  27.     }
  28. }

  29. #elif (NGX_HAVE_MAP_DEVZERO)

  30. ngx_int_t
  31. ngx_shm_alloc(ngx_shm_t *shm)
  32. {
  33.     ngx_fd_t  fd;

  34.     fd = open("/dev/zero", O_RDWR);

  35.     if (fd == -1) {
  36.         ngx_log_error(NGX_LOG_ALERT, shm->log, ngx_errno,
  37.                       "open(\"/dev/zero\") failed");
  38.         return NGX_ERROR;
  39.     }

  40.     shm->addr = (u_char *) mmap(NULL, shm->size, PROT_READ|PROT_WRITE,
  41.                                 MAP_SHARED, fd, 0);

  42.     if (shm->addr == MAP_FAILED) {
  43.         ngx_log_error(NGX_LOG_ALERT, shm->log, ngx_errno,
  44.                       "mmap(/dev/zero, MAP_SHARED, %uz) failed", shm->size);
  45.     }

  46.     if (close(fd) == -1) {
  47.         ngx_log_error(NGX_LOG_ALERT, shm->log, ngx_errno,
  48.                       "close(\"/dev/zero\") failed");
  49.     }

  50.     return (shm->addr == MAP_FAILED) ? NGX_ERROR : NGX_OK;
  51. }


  52. void
  53. ngx_shm_free(ngx_shm_t *shm)
  54. {
  55.     if (munmap((void *) shm->addr, shm->size) == -1) {
  56.         ngx_log_error(NGX_LOG_ALERT, shm->log, ngx_errno,
  57.                       "munmap(%p, %uz) failed", shm->addr, shm->size);
  58.     }
  59. }

  60. #elif (NGX_HAVE_SYSVSHM)

  61. #include <sys/ipc.h>
  62. #include <sys/shm.h>


  63. ngx_int_t
  64. ngx_shm_alloc(ngx_shm_t *shm)
  65. {
  66.     int  id;

  67.     id = shmget(IPC_PRIVATE, shm->size, (SHM_R|SHM_W|IPC_CREAT));

  68.     if (id == -1) {
  69.         ngx_log_error(NGX_LOG_ALERT, shm->log, ngx_errno,
  70.                       "shmget(%uz) failed", shm->size);
  71.         return NGX_ERROR;
  72.     }

  73.     ngx_log_debug1(NGX_LOG_DEBUG_CORE, shm->log, 0, "shmget id: %d", id);

  74.     shm->addr = shmat(id, NULL, 0);

  75.     if (shm->addr == (void *) -1) {
  76.         ngx_log_error(NGX_LOG_ALERT, shm->log, ngx_errno, "shmat() failed");
  77.     }

  78.     if (shmctl(id, IPC_RMID, NULL) == -1) {
  79.         ngx_log_error(NGX_LOG_ALERT, shm->log, ngx_errno,
  80.                       "shmctl(IPC_RMID) failed");
  81.     }

  82.     return (shm->addr == (void *) -1) ? NGX_ERROR : NGX_OK;
  83. }


  84. void
  85. ngx_shm_free(ngx_shm_t *shm)
  86. {
  87.     if (shmdt(shm->addr) == -1) {
  88.         ngx_log_error(NGX_LOG_ALERT, shm->log, ngx_errno,
  89.                       "shmdt(%p) failed", shm->addr);
  90.     }
  91. }

  92. #endif