Blame nginx/modsecurity/ngx_pool_context.c

Packit 284210
Packit 284210
Packit 284210
#include <ngx_core.h>
Packit 284210
Packit 284210
#define NGX_POOL_CTX_SIZE  1024
Packit 284210
Packit 284210
typedef struct ngx_pool_context_node_s ngx_pool_context_node_t;
Packit 284210
struct ngx_pool_context_node_s
Packit 284210
{
Packit 284210
    ngx_pool_context_node_t   *next;
Packit 284210
    ngx_pool_context_node_t  **prev;
Packit 284210
    ngx_pool_t                *pool;
Packit 284210
    ngx_uint_t                 index;
Packit 284210
    void                      *data;
Packit 284210
};
Packit 284210
Packit 284210
static void
Packit 284210
ngx_pool_context_cleanup(void *data);
Packit 284210
Packit 284210
typedef struct {
Packit 284210
    ngx_uint_t                 size;
Packit 284210
} ngx_pool_context_conf_t;
Packit 284210
Packit 284210
static void * ngx_pool_context_create_conf(ngx_cycle_t *cycle);
Packit 284210
static char * ngx_pool_context_init_conf(ngx_cycle_t *cycle, void *conf);
Packit 284210
Packit 284210
static ngx_core_module_t  ngx_pool_context_module_ctx = {
Packit 284210
    ngx_string("pool_context"),
Packit 284210
    ngx_pool_context_create_conf,
Packit 284210
    ngx_pool_context_init_conf,
Packit 284210
};
Packit 284210
Packit 284210
static ngx_command_t  ngx_pool_context_commands[] = {
Packit 284210
Packit 284210
    {   ngx_string("pool_context_hash_size"),
Packit 284210
        NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
Packit 284210
        ngx_conf_set_num_slot,
Packit 284210
        0,
Packit 284210
        offsetof(ngx_pool_context_conf_t, size),
Packit 284210
        NULL
Packit 284210
    },
Packit 284210
    ngx_null_command
Packit 284210
};
Packit 284210
Packit 284210
Packit 284210
ngx_module_t  ngx_pool_context_module = {
Packit 284210
    NGX_MODULE_V1,
Packit 284210
    &ngx_pool_context_module_ctx,          /* module context */
Packit 284210
    ngx_pool_context_commands,             /* module directives */
Packit 284210
    NGX_CORE_MODULE,                       /* module type */
Packit 284210
    NULL,                                  /* init master */
Packit 284210
    NULL,                                  /* init module */
Packit 284210
    NULL,                                  /* init process */
Packit 284210
    NULL,                                  /* init thread */
Packit 284210
    NULL,                                  /* exit thread */
Packit 284210
    NULL,                                  /* exit process */
Packit 284210
    NULL,                                  /* exit master */
Packit 284210
    NGX_MODULE_V1_PADDING
Packit 284210
};
Packit 284210
Packit 284210
Packit 284210
#define ngx_pool_context_hash_key(r, ctx_index) ((ngx_uint_t) r + ctx_index)
Packit 284210
Packit 284210
#define ngx_pool_context_unlink(node) \
Packit 284210
                                                                              \
Packit 284210
    *(node->prev) = node->next;                                               \
Packit 284210
                                                                              \
Packit 284210
    if (node->next) {                                                         \
Packit 284210
        node->next->prev = node->prev;                                        \
Packit 284210
    }                                                                         \
Packit 284210
                                                                              \
Packit 284210
    node->prev = NULL;                                                        \
Packit 284210
 
Packit 284210
Packit 284210
#define ngx_pool_context_link(queue, node)                                \
Packit 284210
                                                                          \
Packit 284210
    if (node->prev != NULL) {                                             \
Packit 284210
        ngx_pool_context_unlink(node);                                    \
Packit 284210
    }                                                                     \
Packit 284210
    node->next = (ngx_pool_context_node_t *) *queue;                      \
Packit 284210
    node->prev = (ngx_pool_context_node_t **) queue;                      \
Packit 284210
    *queue = node;                                                        \
Packit 284210
                                                                          \
Packit 284210
    if (node->next) {                                                     \
Packit 284210
        node->next->prev = &node->next;                                   \
Packit 284210
    }
Packit 284210
Packit 284210
Packit 284210
static ngx_pool_context_node_t **ngx_pool_context_hash;
Packit 284210
static ngx_uint_t            ngx_pool_context_hash_size;
Packit 284210
Packit 284210
/* Nginx has removed multi-thread support, so we do not need mutex */
Packit 284210
Packit 284210
void *
Packit 284210
ngx_pool_get_ctx(ngx_pool_t *pool, ngx_uint_t index)
Packit 284210
{
Packit 284210
    ngx_uint_t               hash;
Packit 284210
    uint32_t                 key;
Packit 284210
    ngx_pool_context_node_t *node;
Packit 284210
Packit 284210
    hash = (ngx_uint_t) pool + index;
Packit 284210
    key = ngx_murmur_hash2((u_char *)&hash, sizeof(hash)) % ngx_pool_context_hash_size;
Packit 284210
Packit 284210
    node = ngx_pool_context_hash[key];
Packit 284210
Packit 284210
    while (node) {
Packit 284210
Packit 284210
        if (node->pool == pool && node->index == index) {
Packit 284210
Packit 284210
            return node->data;
Packit 284210
        }
Packit 284210
        node = node->next;
Packit 284210
    }
Packit 284210
Packit 284210
    return NULL;
Packit 284210
Packit 284210
}
Packit 284210
Packit 284210
Packit 284210
ngx_int_t
Packit 284210
ngx_pool_set_ctx(ngx_pool_t *pool, ngx_uint_t index, void *data)
Packit 284210
{
Packit 284210
    ngx_uint_t              hash;
Packit 284210
    uint32_t                key;
Packit 284210
    ngx_pool_context_node_t *node;
Packit 284210
    ngx_pool_cleanup_t     *cln;
Packit 284210
Packit 284210
    hash = (ngx_uint_t) pool + index;
Packit 284210
    key = ngx_murmur_hash2((u_char *)&hash, sizeof(hash)) % ngx_pool_context_hash_size;
Packit 284210
Packit 284210
    node = ngx_pool_context_hash[key];
Packit 284210
Packit 284210
    while (node) {
Packit 284210
Packit 284210
        if (node->pool == pool
Packit 284210
                && node->index == index) {
Packit 284210
Packit 284210
Packit 284210
            node->data = data;
Packit 284210
            return NGX_OK;
Packit 284210
        }
Packit 284210
        node = node->next;
Packit 284210
    }
Packit 284210
Packit 284210
    cln = ngx_pool_cleanup_add(pool, sizeof(ngx_pool_context_node_t));
Packit 284210
Packit 284210
    if (cln == NULL) {
Packit 284210
Packit 284210
        return NGX_ERROR;
Packit 284210
    }
Packit 284210
Packit 284210
    cln->handler = ngx_pool_context_cleanup;
Packit 284210
    node = cln->data;
Packit 284210
Packit 284210
    node->prev = NULL;
Packit 284210
    node->next = NULL;
Packit 284210
    node->pool = pool;
Packit 284210
    node->index = index;
Packit 284210
    node->data = data;
Packit 284210
Packit 284210
    ngx_pool_context_link(&ngx_pool_context_hash[key], node);
Packit 284210
Packit 284210
    return NGX_OK;
Packit 284210
}
Packit 284210
Packit 284210
Packit 284210
static void
Packit 284210
ngx_pool_context_cleanup(void *data)
Packit 284210
{
Packit 284210
    ngx_pool_context_node_t *node = data;
Packit 284210
Packit 284210
    ngx_pool_context_unlink(node);
Packit 284210
Packit 284210
}
Packit 284210
Packit 284210
Packit 284210
static void *
Packit 284210
ngx_pool_context_create_conf(ngx_cycle_t *cycle)
Packit 284210
{
Packit 284210
    ngx_pool_context_conf_t     *pcf;
Packit 284210
Packit 284210
    /* create config  */
Packit 284210
    pcf = ngx_pcalloc(cycle->pool, sizeof(ngx_pool_context_conf_t));
Packit 284210
    if (pcf == NULL) {
Packit 284210
        return NULL;
Packit 284210
    }
Packit 284210
Packit 284210
    pcf->size = NGX_CONF_UNSET_UINT;
Packit 284210
Packit 284210
    return pcf;
Packit 284210
}
Packit 284210
Packit 284210
Packit 284210
static char *
Packit 284210
ngx_pool_context_init_conf(ngx_cycle_t *cycle, void *conf)
Packit 284210
{
Packit 284210
    ngx_pool_context_conf_t      *pcf = conf;
Packit 284210
Packit 284210
    ngx_conf_init_uint_value(pcf->size, cycle->connection_n);
Packit 284210
Packit 284210
    ngx_pool_context_hash_size = pcf->size;
Packit 284210
Packit 284210
    ngx_pool_context_hash = ngx_pcalloc(cycle->pool, sizeof(ngx_pool_context_node_t *) * ngx_pool_context_hash_size);
Packit 284210
Packit 284210
    if (ngx_pool_context_hash == NULL) {
Packit 284210
        return NGX_CONF_ERROR;
Packit 284210
    }
Packit 284210
Packit 284210
    return NGX_CONF_OK;
Packit 284210
}
Packit 284210
Packit 284210