lua
local pids = ngx.worker.pids() --return table
ngx_http_lua_worker.c
void
ngx_http_lua_inject_worker_api(lua_State *L)
{
lua_createtable(L, 0 /* narr */, 2 /* nrec */); /* ngx.worker. */
lua_pushcfunction(L, ngx_http_lua_ngx_worker_exiting);
lua_setfield(L, -2, "exiting");
lua_pushcfunction(L, ngx_http_lua_ngx_worker_pids);
lua_setfield(L, -2, "pids");
lua_setfield(L, -2, "worker");
}
static int
ngx_http_lua_ngx_worker_num()
{
int i, num = 0;
for(i = 0; i < NGX_MAX_PROCESSES ; i++){
if(ngx_processes[i].pid != -1 && ngx_processes[i].pid != 0 ){
num++;
}
}
return num;
}
static int
ngx_http_lua_ngx_worker_pids(lua_State *L)
{
lua_createtable(L, 0, ngx_http_lua_ngx_worker_num()+1); /* ngx.worker.pids() */
int i, num = 1;
lua_pushinteger(L, (lua_Integer) num++);
lua_pushinteger(L, (lua_Integer) getppid()); //master pid
lua_settable(L, -3);
for(i = 0; i < NGX_MAX_PROCESSES; ++i){
if(ngx_processes[i].pid != -1 && ngx_processes[i].pid != 0){
lua_pushinteger(L, (lua_Integer)num++);
lua_pushinteger(L, (lua_Integer)ngx_processes[i].pid);
lua_settable(L, -3);
}
}
lua_pushinteger(L, (lua_Integer) num);
lua_pushinteger(L, (lua_Integer) ngx_pid); //current pid
lua_settable(L, -3);
return 1;
}