Back to home page

Nginx displayed by LXR

Source navigation ]
Diff markup ]
Identifier search ]
general search ]
 
 
Version: nginx-1.15.11 ]​[ nginx-1.14.2 ]​

0001 
0002 /*
0003  * Copyright (C) Igor Sysoev
0004  * Copyright (C) Nginx, Inc.
0005  */
0006 
0007 
0008 #include <ngx_config.h>
0009 #include <ngx_core.h>
0010 
0011 
0012 #if (NGX_HAVE_MAP_ANON)
0013 
0014 ngx_int_t
0015 ngx_shm_alloc(ngx_shm_t *shm)
0016 {
0017     shm->addr = (u_char *) mmap(NULL, shm->size,
0018                                 PROT_READ|PROT_WRITE,
0019                                 MAP_ANON|MAP_SHARED, -1, 0);
0020 
0021     if (shm->addr == MAP_FAILED) {
0022         ngx_log_error(NGX_LOG_ALERT, shm->log, ngx_errno,
0023                       "mmap(MAP_ANON|MAP_SHARED, %uz) failed", shm->size);
0024         return NGX_ERROR;
0025     }
0026 
0027     return NGX_OK;
0028 }
0029 
0030 
0031 void
0032 ngx_shm_free(ngx_shm_t *shm)
0033 {
0034     if (munmap((void *) shm->addr, shm->size) == -1) {
0035         ngx_log_error(NGX_LOG_ALERT, shm->log, ngx_errno,
0036                       "munmap(%p, %uz) failed", shm->addr, shm->size);
0037     }
0038 }
0039 
0040 #elif (NGX_HAVE_MAP_DEVZERO)
0041 
0042 ngx_int_t
0043 ngx_shm_alloc(ngx_shm_t *shm)
0044 {
0045     ngx_fd_t  fd;
0046 
0047     fd = open("/dev/zero", O_RDWR);
0048 
0049     if (fd == -1) {
0050         ngx_log_error(NGX_LOG_ALERT, shm->log, ngx_errno,
0051                       "open(\"/dev/zero\") failed");
0052         return NGX_ERROR;
0053     }
0054 
0055     shm->addr = (u_char *) mmap(NULL, shm->size, PROT_READ|PROT_WRITE,
0056                                 MAP_SHARED, fd, 0);
0057 
0058     if (shm->addr == MAP_FAILED) {
0059         ngx_log_error(NGX_LOG_ALERT, shm->log, ngx_errno,
0060                       "mmap(/dev/zero, MAP_SHARED, %uz) failed", shm->size);
0061     }
0062 
0063     if (close(fd) == -1) {
0064         ngx_log_error(NGX_LOG_ALERT, shm->log, ngx_errno,
0065                       "close(\"/dev/zero\") failed");
0066     }
0067 
0068     return (shm->addr == MAP_FAILED) ? NGX_ERROR : NGX_OK;
0069 }
0070 
0071 
0072 void
0073 ngx_shm_free(ngx_shm_t *shm)
0074 {
0075     if (munmap((void *) shm->addr, shm->size) == -1) {
0076         ngx_log_error(NGX_LOG_ALERT, shm->log, ngx_errno,
0077                       "munmap(%p, %uz) failed", shm->addr, shm->size);
0078     }
0079 }
0080 
0081 #elif (NGX_HAVE_SYSVSHM)
0082 
0083 #include <sys/ipc.h>
0084 #include <sys/shm.h>
0085 
0086 
0087 ngx_int_t
0088 ngx_shm_alloc(ngx_shm_t *shm)
0089 {
0090     int  id;
0091 
0092     id = shmget(IPC_PRIVATE, shm->size, (SHM_R|SHM_W|IPC_CREAT));
0093 
0094     if (id == -1) {
0095         ngx_log_error(NGX_LOG_ALERT, shm->log, ngx_errno,
0096                       "shmget(%uz) failed", shm->size);
0097         return NGX_ERROR;
0098     }
0099 
0100     ngx_log_debug1(NGX_LOG_DEBUG_CORE, shm->log, 0, "shmget id: %d", id);
0101 
0102     shm->addr = shmat(id, NULL, 0);
0103 
0104     if (shm->addr == (void *) -1) {
0105         ngx_log_error(NGX_LOG_ALERT, shm->log, ngx_errno, "shmat() failed");
0106     }
0107 
0108     if (shmctl(id, IPC_RMID, NULL) == -1) {
0109         ngx_log_error(NGX_LOG_ALERT, shm->log, ngx_errno,
0110                       "shmctl(IPC_RMID) failed");
0111     }
0112 
0113     return (shm->addr == (void *) -1) ? NGX_ERROR : NGX_OK;
0114 }
0115 
0116 
0117 void
0118 ngx_shm_free(ngx_shm_t *shm)
0119 {
0120     if (shmdt(shm->addr) == -1) {
0121         ngx_log_error(NGX_LOG_ALERT, shm->log, ngx_errno,
0122                       "shmdt(%p) failed", shm->addr);
0123     }
0124 }
0125 
0126 #endif