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_worker.h"
14 :
15 :
16 : #define NGX_PROCESS_PRIVILEGED_AGENT 99
17 :
18 :
19 : static int ngx_http_lua_ngx_worker_exiting(lua_State *L);
20 : static int ngx_http_lua_ngx_worker_pid(lua_State *L);
21 : static int ngx_http_lua_ngx_worker_id(lua_State *L);
22 : static int ngx_http_lua_ngx_worker_count(lua_State *L);
23 :
24 :
25 : void
26 18 : ngx_http_lua_inject_worker_api(lua_State *L)
27 : {
28 18 : lua_createtable(L, 0 /* narr */, 4 /* nrec */); /* ngx.worker. */
29 :
30 18 : lua_pushcfunction(L, ngx_http_lua_ngx_worker_exiting);
31 18 : lua_setfield(L, -2, "exiting");
32 :
33 18 : lua_pushcfunction(L, ngx_http_lua_ngx_worker_pid);
34 18 : lua_setfield(L, -2, "pid");
35 :
36 18 : lua_pushcfunction(L, ngx_http_lua_ngx_worker_id);
37 18 : lua_setfield(L, -2, "id");
38 :
39 18 : lua_pushcfunction(L, ngx_http_lua_ngx_worker_count);
40 18 : lua_setfield(L, -2, "count");
41 :
42 18 : lua_setfield(L, -2, "worker");
43 18 : }
44 :
45 :
46 : static int
47 0 : ngx_http_lua_ngx_worker_exiting(lua_State *L)
48 : {
49 0 : lua_pushboolean(L, ngx_exiting);
50 0 : return 1;
51 : }
52 :
53 :
54 : static int
55 0 : ngx_http_lua_ngx_worker_pid(lua_State *L)
56 : {
57 0 : lua_pushinteger(L, (lua_Integer) ngx_pid);
58 0 : return 1;
59 : }
60 :
61 :
62 : static int
63 0 : ngx_http_lua_ngx_worker_id(lua_State *L)
64 : {
65 : #if (nginx_version >= 1009001)
66 0 : if (ngx_process != NGX_PROCESS_WORKER
67 0 : && ngx_process != NGX_PROCESS_SINGLE)
68 : {
69 0 : lua_pushnil(L);
70 0 : return 1;
71 : }
72 :
73 0 : lua_pushinteger(L, (lua_Integer) ngx_worker);
74 : #else
75 : lua_pushnil(L);
76 : #endif
77 0 : return 1;
78 : }
79 :
80 :
81 : static int
82 0 : ngx_http_lua_ngx_worker_count(lua_State *L)
83 : {
84 : ngx_core_conf_t *ccf;
85 :
86 0 : ccf = (ngx_core_conf_t *) ngx_get_conf(ngx_cycle->conf_ctx,
87 : ngx_core_module);
88 :
89 0 : lua_pushinteger(L, (lua_Integer) ccf->worker_processes);
90 0 : return 1;
91 : }
92 :
93 :
94 : #ifndef NGX_LUA_NO_FFI_API
95 : int
96 0 : ngx_http_lua_ffi_worker_pid(void)
97 : {
98 0 : return (int) ngx_pid;
99 : }
100 :
101 :
102 : int
103 0 : ngx_http_lua_ffi_worker_id(void)
104 : {
105 : #if (nginx_version >= 1009001)
106 0 : if (ngx_process != NGX_PROCESS_WORKER
107 0 : && ngx_process != NGX_PROCESS_SINGLE)
108 : {
109 0 : return -1;
110 : }
111 :
112 0 : return (int) ngx_worker;
113 : #else
114 : return -1;
115 : #endif
116 : }
117 :
118 :
119 : int
120 0 : ngx_http_lua_ffi_worker_exiting(void)
121 : {
122 0 : return (int) ngx_exiting;
123 : }
124 :
125 :
126 : int
127 0 : ngx_http_lua_ffi_worker_count(void)
128 : {
129 : ngx_core_conf_t *ccf;
130 :
131 0 : ccf = (ngx_core_conf_t *) ngx_get_conf(ngx_cycle->conf_ctx,
132 : ngx_core_module);
133 :
134 0 : return (int) ccf->worker_processes;
135 : }
136 :
137 :
138 : int
139 0 : ngx_http_lua_ffi_get_process_type(void)
140 : {
141 : #if defined(HAVE_PRIVILEGED_PROCESS_PATCH) && !NGX_WIN32
142 0 : if (ngx_process == NGX_PROCESS_HELPER) {
143 0 : if (ngx_is_privileged_agent) {
144 0 : return NGX_PROCESS_PRIVILEGED_AGENT;
145 : }
146 : }
147 : #endif
148 :
149 0 : return ngx_process;
150 : }
151 :
152 :
153 : int
154 0 : ngx_http_lua_ffi_enable_privileged_agent(char **err)
155 : {
156 : #ifdef HAVE_PRIVILEGED_PROCESS_PATCH
157 : ngx_core_conf_t *ccf;
158 :
159 0 : ccf = (ngx_core_conf_t *) ngx_get_conf(ngx_cycle->conf_ctx,
160 : ngx_core_module);
161 :
162 0 : ccf->privileged_agent = 1;
163 :
164 0 : return NGX_OK;
165 :
166 : #else
167 : *err = "missing privileged agent process patch in the nginx core";
168 : return NGX_ERROR;
169 : #endif
170 : }
171 :
172 :
173 : void
174 0 : ngx_http_lua_ffi_process_signal_graceful_exit(void)
175 : {
176 0 : ngx_quit = 1;
177 0 : }
178 : #endif
|