summaryrefslogtreecommitdiff
path: root/ngx_lua-0.10.28/src/ngx_http_lua_exitworkerby.c
blob: e8f65110aed11f8d5a4476bd3139585af63b468d (plain)
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
/*
 * Copyright (C) Yichun Zhang (agentzh)
 */


#ifndef DDEBUG
#define DDEBUG 0
#endif
#include "ddebug.h"


#include "ngx_http_lua_exitworkerby.h"
#include "ngx_http_lua_util.h"

#if (NGX_THREADS)
#include "ngx_http_lua_worker_thread.h"
#endif


void
ngx_http_lua_exit_worker(ngx_cycle_t *cycle)
{
    ngx_http_lua_main_conf_t    *lmcf;
    ngx_connection_t            *c = NULL;
    ngx_http_request_t          *r = NULL;
    ngx_http_lua_ctx_t          *ctx;
    ngx_http_conf_ctx_t         *conf_ctx;

#if (NGX_THREADS)
    ngx_http_lua_thread_exit_process();
#endif

    lmcf = ngx_http_cycle_get_module_main_conf(cycle, ngx_http_lua_module);
    if (lmcf == NULL
        || lmcf->exit_worker_handler == NULL
        || lmcf->lua == NULL
#if !(NGX_WIN32)
        || (ngx_process == NGX_PROCESS_HELPER
#   ifdef HAVE_PRIVILEGED_PROCESS_PATCH
            && !ngx_is_privileged_agent
#   endif
           )
#endif  /* NGX_WIN32 */
       )
    {
        return;
    }

    conf_ctx = ((ngx_http_conf_ctx_t *) cycle->conf_ctx[ngx_http_module.index]);

    c = ngx_http_lua_create_fake_connection(NULL);
    if (c == NULL) {
        goto failed;
    }

    c->log = ngx_cycle->log;

    r = ngx_http_lua_create_fake_request(c);
    if (r == NULL) {
        goto failed;
    }

    r->main_conf = conf_ctx->main_conf;
    r->srv_conf = conf_ctx->srv_conf;
    r->loc_conf = conf_ctx->loc_conf;

    ctx = ngx_http_lua_create_ctx(r);
    if (ctx == NULL) {
        goto failed;
    }

    ctx->context = NGX_HTTP_LUA_CONTEXT_EXIT_WORKER;
    ctx->cur_co_ctx = NULL;

    ngx_http_lua_set_req(lmcf->lua, r);

    (void) lmcf->exit_worker_handler(cycle->log, lmcf, lmcf->lua);

    ngx_destroy_pool(c->pool);
    return;

failed:

    if (c) {
        ngx_http_lua_close_fake_connection(c);
    }

    return;
}


ngx_int_t
ngx_http_lua_exit_worker_by_inline(ngx_log_t *log,
    ngx_http_lua_main_conf_t *lmcf, lua_State *L)
{
    int         status;
    const char *chunkname;

    if (lmcf->exit_worker_chunkname == NULL) {
        chunkname = "=exit_worker_by_lua";

    } else {
        chunkname = (const char *) lmcf->exit_worker_chunkname;
    }

    status = luaL_loadbuffer(L, (char *) lmcf->exit_worker_src.data,
                             lmcf->exit_worker_src.len, chunkname)
             || ngx_http_lua_do_call(log, L);

    return ngx_http_lua_report(log, L, status, "exit_worker_by_lua");
}


ngx_int_t
ngx_http_lua_exit_worker_by_file(ngx_log_t *log, ngx_http_lua_main_conf_t *lmcf,
    lua_State *L)
{
    int         status;

    status = luaL_loadfile(L, (char *) lmcf->exit_worker_src.data)
             || ngx_http_lua_do_call(log, L);

    return ngx_http_lua_report(log, L, status, "exit_worker_by_lua_file");
}

/* vi:set ft=c ts=4 sw=4 et fdm=marker: */