Skip to content

Commit b0b9a4f

Browse files
bugfix: windows does not support ngx_*_lua_ffi_worker_pids.
Signed-off-by: lijunlong <lijunlong@openresty.com>
1 parent 8523694 commit b0b9a4f

File tree

2 files changed

+43
-18
lines changed

2 files changed

+43
-18
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
sudo: required
3-
dist: bionic
3+
dist: focal
44

55
branches:
66
only:

lib/resty/core/worker.lua

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,25 @@
22

33

44
local ffi = require "ffi"
5+
local jit = require "jit"
56
local base = require "resty.core.base"
7+
local ffi_cast = ffi.cast
68

79

810
local C = ffi.C
9-
local ffi_new = ffi.new
1011
local new_tab = base.new_tab
1112
local subsystem = ngx.config.subsystem
13+
local get_string_buf = base.get_string_buf
14+
local get_size_ptr = base.get_size_ptr
1215

1316

1417
local ngx_lua_ffi_worker_id
1518
local ngx_lua_ffi_worker_pid
1619
local ngx_lua_ffi_worker_pids
1720
local ngx_lua_ffi_worker_count
1821
local ngx_lua_ffi_worker_exiting
22+
local ffi_intp_type = ffi.typeof("int *")
23+
local ffi_int_size = ffi.sizeof("int")
1924

2025

2126
ngx.worker = new_tab(0, 4)
@@ -25,31 +30,42 @@ if subsystem == "http" then
2530
ffi.cdef[[
2631
int ngx_http_lua_ffi_worker_id(void);
2732
int ngx_http_lua_ffi_worker_pid(void);
28-
int ngx_http_lua_ffi_worker_pids(int *pids, size_t *pids_len);
2933
int ngx_http_lua_ffi_worker_count(void);
3034
int ngx_http_lua_ffi_worker_exiting(void);
3135
]]
3236

3337
ngx_lua_ffi_worker_id = C.ngx_http_lua_ffi_worker_id
3438
ngx_lua_ffi_worker_pid = C.ngx_http_lua_ffi_worker_pid
35-
ngx_lua_ffi_worker_pids = C.ngx_http_lua_ffi_worker_pids
3639
ngx_lua_ffi_worker_count = C.ngx_http_lua_ffi_worker_count
3740
ngx_lua_ffi_worker_exiting = C.ngx_http_lua_ffi_worker_exiting
41+
if jit.os ~= "Windows" then
42+
ffi.cdef[[
43+
int ngx_http_lua_ffi_worker_pids(int *pids, size_t *pids_len);
44+
]]
45+
46+
ngx_lua_ffi_worker_pids = C.ngx_http_lua_ffi_worker_pids
47+
end
3848

3949
elseif subsystem == "stream" then
4050
ffi.cdef[[
4151
int ngx_stream_lua_ffi_worker_id(void);
4252
int ngx_stream_lua_ffi_worker_pid(void);
43-
int ngx_stream_lua_ffi_worker_pids(int *pids, size_t *pids_len);
4453
int ngx_stream_lua_ffi_worker_count(void);
4554
int ngx_stream_lua_ffi_worker_exiting(void);
4655
]]
4756

4857
ngx_lua_ffi_worker_id = C.ngx_stream_lua_ffi_worker_id
4958
ngx_lua_ffi_worker_pid = C.ngx_stream_lua_ffi_worker_pid
50-
ngx_lua_ffi_worker_pids = C.ngx_stream_lua_ffi_worker_pids
5159
ngx_lua_ffi_worker_count = C.ngx_stream_lua_ffi_worker_count
5260
ngx_lua_ffi_worker_exiting = C.ngx_stream_lua_ffi_worker_exiting
61+
62+
if jit.os ~= "Windows" then
63+
ffi.cdef[[
64+
int ngx_stream_lua_ffi_worker_pids(int *pids, size_t *pids_len);
65+
]]
66+
67+
ngx_lua_ffi_worker_pids = C.ngx_stream_lua_ffi_worker_pids
68+
end
5369
end
5470

5571

@@ -62,23 +78,32 @@ function ngx.worker.pid()
6278
return ngx_lua_ffi_worker_pid()
6379
end
6480

65-
local size_ptr = ffi_new("size_t[1]")
66-
local pids_ptr = ffi_new("int[1024]") -- using NGX_MAX_PROCESSES
6781

68-
function ngx.worker.pids()
69-
if ngx.get_phase() == "init" or ngx.get_phase() == "init_worker" then
70-
return nil, "API disabled in the current context"
71-
end
82+
if jit.os ~= "Windows" then
83+
function ngx.worker.pids()
84+
if ngx.get_phase() == "init" or ngx.get_phase() == "init_worker" then
85+
return nil, "API disabled in the current context"
86+
end
87+
88+
local pids = {}
89+
local size_ptr = get_size_ptr()
90+
local worker_cnt = ngx.worker.count()
91+
if worker_cnt == 0 then
92+
return pids
93+
end
7294

73-
local res = ngx_lua_ffi_worker_pids(pids_ptr, size_ptr)
95+
size_ptr[0] = worker_cnt
96+
local pids_ptr = get_string_buf(worker_cnt * ffi_int_size)
97+
local intp_buf = ffi_cast(ffi_intp_type, pids_ptr)
98+
local res = ngx_lua_ffi_worker_pids(intp_buf, size_ptr)
7499

75-
local pids = {}
76-
if res == 0 then
77-
for i = 1, tonumber(size_ptr[0]) do
78-
pids[i] = pids_ptr[i-1]
100+
if res == 0 then
101+
for i = 1, tonumber(size_ptr[0]) do
102+
pids[i] = intp_buf[i-1]
103+
end
79104
end
105+
return pids
80106
end
81-
return pids
82107
end
83108

84109
function ngx.worker.id()

0 commit comments

Comments
 (0)