LCOV - code coverage report
Current view: top level - home/build/openresty-1.13.6.1/build/ngx_lua-0.10.11/src - ngx_http_lua_api.c (source / functions) Hit Total Coverage
Test: coverage ngix Lines: 13 82 15.9 %
Date: 2020-03-03 04:25:50 Functions: 1 5 20.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : 
       2             : /*
       3             :  * Copyright (C) Yichun Zhang (agentzh)
       4             :  */
       5             : 
       6             : 
       7             : #ifndef DDEBUG
       8             : #define DDEBUG 0
       9             : #endif
      10             : #include "ddebug.h"
      11             : 
      12             : 
      13             : #include "ngx_http_lua_common.h"
      14             : #include "api/ngx_http_lua_api.h"
      15             : #include "ngx_http_lua_shdict.h"
      16             : #include "ngx_http_lua_util.h"
      17             : 
      18             : 
      19             : lua_State *
      20           0 : ngx_http_lua_get_global_state(ngx_conf_t *cf)
      21             : {
      22             :     ngx_http_lua_main_conf_t *lmcf;
      23             : 
      24           0 :     lmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_lua_module);
      25             : 
      26           0 :     return lmcf->lua;
      27             : }
      28             : 
      29             : 
      30             : ngx_http_request_t *
      31           0 : ngx_http_lua_get_request(lua_State *L)
      32             : {
      33           0 :     return ngx_http_lua_get_req(L);
      34             : }
      35             : 
      36             : 
      37             : static ngx_int_t ngx_http_lua_shared_memory_init(ngx_shm_zone_t *shm_zone,
      38             :     void *data);
      39             : 
      40             : 
      41             : ngx_int_t
      42          18 : ngx_http_lua_add_package_preload(ngx_conf_t *cf, const char *package,
      43             :     lua_CFunction func)
      44             : {
      45             :     lua_State                     *L;
      46             :     ngx_http_lua_main_conf_t      *lmcf;
      47             :     ngx_http_lua_preload_hook_t   *hook;
      48             : 
      49          18 :     lmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_lua_module);
      50             : 
      51          18 :     L = lmcf->lua;
      52             : 
      53          18 :     if (L) {
      54           0 :         lua_getglobal(L, "package");
      55           0 :         lua_getfield(L, -1, "preload");
      56           0 :         lua_pushcfunction(L, func);
      57           0 :         lua_setfield(L, -2, package);
      58           0 :         lua_pop(L, 2);
      59             :     }
      60             : 
      61             :     /* we always register preload_hooks since we always create new Lua VMs
      62             :      * when lua code cache is off. */
      63             : 
      64          18 :     if (lmcf->preload_hooks == NULL) {
      65          18 :         lmcf->preload_hooks =
      66          18 :             ngx_array_create(cf->pool, 4,
      67             :                              sizeof(ngx_http_lua_preload_hook_t));
      68             : 
      69          18 :         if (lmcf->preload_hooks == NULL) {
      70           0 :             return NGX_ERROR;
      71             :         }
      72             :     }
      73             : 
      74          18 :     hook = ngx_array_push(lmcf->preload_hooks);
      75          18 :     if (hook == NULL) {
      76           0 :         return NGX_ERROR;
      77             :     }
      78             : 
      79          18 :     hook->package = (u_char *) package;
      80          18 :     hook->loader = func;
      81             : 
      82          18 :     return NGX_OK;
      83             : }
      84             : 
      85             : 
      86             : ngx_shm_zone_t *
      87           0 : ngx_http_lua_shared_memory_add(ngx_conf_t *cf, ngx_str_t *name, size_t size,
      88             :     void *tag)
      89             : {
      90             :     ngx_http_lua_main_conf_t     *lmcf;
      91             :     ngx_shm_zone_t              **zp;
      92             :     ngx_shm_zone_t               *zone;
      93             :     ngx_http_lua_shm_zone_ctx_t  *ctx;
      94             :     ngx_int_t                     n;
      95             : 
      96           0 :     lmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_lua_module);
      97           0 :     if (lmcf == NULL) {
      98           0 :         return NULL;
      99             :     }
     100             : 
     101           0 :     if (lmcf->shm_zones == NULL) {
     102           0 :         lmcf->shm_zones = ngx_palloc(cf->pool, sizeof(ngx_array_t));
     103           0 :         if (lmcf->shm_zones == NULL) {
     104           0 :             return NULL;
     105             :         }
     106             : 
     107           0 :         if (ngx_array_init(lmcf->shm_zones, cf->pool, 2,
     108             :                            sizeof(ngx_shm_zone_t *))
     109             :             != NGX_OK)
     110             :         {
     111           0 :             return NULL;
     112             :         }
     113             :     }
     114             : 
     115           0 :     zone = ngx_shared_memory_add(cf, name, (size_t) size, tag);
     116           0 :     if (zone == NULL) {
     117           0 :         return NULL;
     118             :     }
     119             : 
     120           0 :     if (zone->data) {
     121           0 :         ctx = (ngx_http_lua_shm_zone_ctx_t *) zone->data;
     122           0 :         return &ctx->zone;
     123             :     }
     124             : 
     125           0 :     n = sizeof(ngx_http_lua_shm_zone_ctx_t);
     126             : 
     127           0 :     ctx = ngx_pcalloc(cf->pool, n);
     128           0 :     if (ctx == NULL) {
     129           0 :         return NULL;
     130             :     }
     131             : 
     132           0 :     ctx->lmcf = lmcf;
     133           0 :     ctx->log = &cf->cycle->new_log;
     134           0 :     ctx->cycle = cf->cycle;
     135             : 
     136           0 :     ngx_memcpy(&ctx->zone, zone, sizeof(ngx_shm_zone_t));
     137             : 
     138           0 :     zp = ngx_array_push(lmcf->shm_zones);
     139           0 :     if (zp == NULL) {
     140           0 :         return NULL;
     141             :     }
     142             : 
     143           0 :     *zp = zone;
     144             : 
     145             :     /* set zone init */
     146           0 :     zone->init = ngx_http_lua_shared_memory_init;
     147           0 :     zone->data = ctx;
     148             : 
     149           0 :     lmcf->requires_shm = 1;
     150             : 
     151           0 :     return &ctx->zone;
     152             : }
     153             : 
     154             : 
     155             : static ngx_int_t
     156           0 : ngx_http_lua_shared_memory_init(ngx_shm_zone_t *shm_zone, void *data)
     157             : {
     158           0 :     ngx_http_lua_shm_zone_ctx_t *octx = data;
     159             :     ngx_shm_zone_t              *ozone;
     160             :     void                        *odata;
     161             : 
     162             :     ngx_int_t                    rc;
     163             :     volatile ngx_cycle_t        *saved_cycle;
     164             :     ngx_http_lua_main_conf_t    *lmcf;
     165             :     ngx_http_lua_shm_zone_ctx_t *ctx;
     166             :     ngx_shm_zone_t              *zone;
     167             : 
     168           0 :     ctx = (ngx_http_lua_shm_zone_ctx_t *) shm_zone->data;
     169           0 :     zone = &ctx->zone;
     170             : 
     171           0 :     odata = NULL;
     172           0 :     if (octx) {
     173           0 :         ozone = &octx->zone;
     174           0 :         odata = ozone->data;
     175             :     }
     176             : 
     177           0 :     zone->shm = shm_zone->shm;
     178             : #if defined(nginx_version) && nginx_version >= 1009000
     179           0 :     zone->noreuse = shm_zone->noreuse;
     180             : #endif
     181             : 
     182           0 :     if (zone->init(zone, odata) != NGX_OK) {
     183           0 :         return NGX_ERROR;
     184             :     }
     185             : 
     186             :     dd("get lmcf");
     187             : 
     188           0 :     lmcf = ctx->lmcf;
     189           0 :     if (lmcf == NULL) {
     190           0 :         return NGX_ERROR;
     191             :     }
     192             : 
     193             :     dd("lmcf->lua: %p", lmcf->lua);
     194             : 
     195           0 :     lmcf->shm_zones_inited++;
     196             : 
     197           0 :     if (lmcf->shm_zones_inited == lmcf->shm_zones->nelts
     198           0 :         && lmcf->init_handler)
     199             :     {
     200           0 :         saved_cycle = ngx_cycle;
     201           0 :         ngx_cycle = ctx->cycle;
     202             : 
     203           0 :         rc = lmcf->init_handler(ctx->log, lmcf, lmcf->lua);
     204             : 
     205           0 :         ngx_cycle = saved_cycle;
     206             : 
     207           0 :         if (rc != NGX_OK) {
     208             :             /* an error happened */
     209           0 :             return NGX_ERROR;
     210             :         }
     211             :     }
     212             : 
     213           0 :     return NGX_OK;
     214             : }
     215             : 
     216             : /* vi:set ft=c ts=4 sw=4 et fdm=marker: */

Generated by: LCOV version 1.13