lua_vmprep.c revision ec7851f543720a66e94295707116b4bc91961030
1b05bdb88b90d3c947351f262d7ae7d68f0a4a6fTill Mossakowski * Licensed to the Apache Software Foundation (ASF) under one or more
c06dd8856a03b72f6b3f69e874f8700f10cb8522Christian Maeder * contributor license agreements. See the NOTICE file distributed with
75a6279dbae159d018ef812185416cf6df386c10Till Mossakowski * this work for additional information regarding copyright ownership.
75a6279dbae159d018ef812185416cf6df386c10Till Mossakowski * The ASF licenses this file to You under the Apache License, Version 2.0
c06dd8856a03b72f6b3f69e874f8700f10cb8522Christian Maeder * (the "License"); you may not use this file except in compliance with
ae17d457c2d00d47d65e8cd510c3fd21b9516ccbTill Mossakowski * the License. You may obtain a copy of the License at
e379124f467e5d0ef7d3c0ca238bff0521f70831Till Mossakowski * http://www.apache.org/licenses/LICENSE-2.0
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder * Unless required by applicable law or agreed to in writing, software
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder * distributed under the License is distributed on an "AS IS" BASIS,
679d3f541f7a9ede4079e045f7758873bb901872Till Mossakowski * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
679d3f541f7a9ede4079e045f7758873bb901872Till Mossakowski * See the License for the specific language governing permissions and
1bb1684c83317dfd1692ab53415027b67d8f2faeTill Mossakowski * limitations under the License.
2b4130336e941b7d01c78a6da55449a4c6eca609Till Mossakowski/* forward dec'l from this file */
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maederstatic void pstack_dump(lua_State *L, apr_pool_t *r, int level,
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder ap_log_perror(APLOG_MARK, level, 0, r, "Lua Stack Dump: [%s]", msg);
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder int t = lua_type(L, i);
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder ap_log_perror(APLOG_MARK, level, 0, r, "%d: userdata", i);
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder ap_log_perror(APLOG_MARK, level, 0, r, "%d: lightuserdata",
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder ap_log_perror(APLOG_MARK, level, 0, r, "%d: NIL", i);
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder ap_log_perror(APLOG_MARK, level, 0, r, "%d: None", i);
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder i) ? "true" :
c0380b947eef252db81ee562246bb732555427f4Till Mossakowski ap_log_perror(APLOG_MARK, level, 0, r, "%d: <table>", i);
1bb1684c83317dfd1692ab53415027b67d8f2faeTill Mossakowski ap_log_perror(APLOG_MARK, level, 0, r, "%d: <thread>", i);
1bb1684c83317dfd1692ab53415027b67d8f2faeTill Mossakowski ap_log_perror(APLOG_MARK, level, 0, r, "%d: <function>", i);
1bb1684c83317dfd1692ab53415027b67d8f2faeTill Mossakowski "%d: unknown: [%s]", i, lua_typename(L, i));
1bb1684c83317dfd1692ab53415027b67d8f2faeTill Mossakowski/* BEGIN modules*/
1bb1684c83317dfd1692ab53415027b67d8f2faeTill Mossakowski/* BEGIN apache lmodule */
1bb1684c83317dfd1692ab53415027b67d8f2faeTill MossakowskiAP_LUA_DECLARE(void) ap_lua_load_apache2_lmodule(lua_State *L)
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder lua_pushinteger(L, HTTP_MOVED_TEMPORARILY);
ba904a15082557e939db689fcfba0c68c9a4f740Christian Maeder lua_setfield(L, -2, "HTTP_MOVED_TEMPORARILY");
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder lua_pushinteger(L, HTTP_CONTINUE);
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder lua_setfield(L, -2, "HTTP_CONTINUE");
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder lua_pushinteger(L, HTTP_SWITCHING_PROTOCOLS);
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder lua_setfield(L, -2, "HTTP_SWITCHING_PROTOCOLS");
c0380b947eef252db81ee562246bb732555427f4Till Mossakowski lua_pushinteger(L, HTTP_PROCESSING);
3476beb5baf84bef7cc7d627b130de9d48700399Christian Maeder lua_setfield(L, -2, "HTTP_PROCESSING");
6a57a555c8ef0a79aa5d20e1d721400dbffa564aMaciek Makowski lua_pushinteger(L, HTTP_OK);
e379124f467e5d0ef7d3c0ca238bff0521f70831Till Mossakowski lua_setfield(L, -2, "HTTP_OK");
31c49f2fa23d4ac089f35145d80a224deb6ea7e4Till Mossakowski lua_pushinteger(L, HTTP_CREATED);
1b05bdb88b90d3c947351f262d7ae7d68f0a4a6fTill Mossakowski lua_setfield(L, -2, "HTTP_CREATED");
1b05bdb88b90d3c947351f262d7ae7d68f0a4a6fTill Mossakowski lua_pushinteger(L, HTTP_ACCEPTED);
1b05bdb88b90d3c947351f262d7ae7d68f0a4a6fTill Mossakowski lua_setfield(L, -2, "HTTP_ACCEPTED");
1b05bdb88b90d3c947351f262d7ae7d68f0a4a6fTill Mossakowski lua_pushinteger(L, HTTP_NON_AUTHORITATIVE);
3476beb5baf84bef7cc7d627b130de9d48700399Christian Maeder lua_setfield(L, -2, "HTTP_NON_AUTHORITATIVE");
b10d6cef708b7a659f2d3b367e8e0db0d03ae3f5Till Mossakowski lua_pushinteger(L, HTTP_NO_CONTENT);
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder lua_setfield(L, -2, "HTTP_NO_CONTENT");
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder lua_pushinteger(L, HTTP_RESET_CONTENT);
58b671de3fe578346fef9642ffa3c5a0a0edb3cbTill Mossakowski lua_setfield(L, -2, "HTTP_RESET_CONTENT");
242691238a8d1a89581751d782af87ec5d7470c0Till Mossakowski lua_pushinteger(L, HTTP_PARTIAL_CONTENT);
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder lua_setfield(L, -2, "HTTP_PARTIAL_CONTENT");
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder lua_pushinteger(L, HTTP_MULTI_STATUS);
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder lua_setfield(L, -2, "HTTP_MULTI_STATUS");
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder lua_pushinteger(L, HTTP_MULTIPLE_CHOICES);
b10d6cef708b7a659f2d3b367e8e0db0d03ae3f5Till Mossakowski lua_setfield(L, -2, "HTTP_MULTIPLE_CHOICES");
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder lua_pushinteger(L, HTTP_MOVED_PERMANENTLY);
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder lua_setfield(L, -2, "HTTP_MOVED_PERMANENTLY");
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder lua_pushinteger(L, HTTP_SEE_OTHER);
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder lua_setfield(L, -2, "HTTP_SEE_OTHER");
f534c0116096e25659ceaa57de030c497ce9345aTill Mossakowski lua_pushinteger(L, HTTP_NOT_MODIFIED);
e379124f467e5d0ef7d3c0ca238bff0521f70831Till Mossakowski lua_setfield(L, -2, "HTTP_NOT_MODIFIED");
e379124f467e5d0ef7d3c0ca238bff0521f70831Till Mossakowski lua_pushinteger(L, HTTP_USE_PROXY);
e379124f467e5d0ef7d3c0ca238bff0521f70831Till Mossakowski lua_setfield(L, -2, "HTTP_USE_PROXY");
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder lua_pushinteger(L, HTTP_TEMPORARY_REDIRECT);
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder lua_setfield(L, -2, "HTTP_TEMPORARY_REDIRECT");
c0380b947eef252db81ee562246bb732555427f4Till Mossakowski lua_pushinteger(L, HTTP_BAD_REQUEST);
6a57a555c8ef0a79aa5d20e1d721400dbffa564aMaciek Makowski lua_setfield(L, -2, "HTTP_BAD_REQUEST");
c0380b947eef252db81ee562246bb732555427f4Till Mossakowski lua_pushinteger(L, HTTP_UNAUTHORIZED);
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder lua_setfield(L, -2, "HTTP_UNAUTHORIZED");
bfa9e03532243ceb487f0384d0f6a447f1ce7670Till Mossakowski lua_pushinteger(L, HTTP_PAYMENT_REQUIRED);
6a57a555c8ef0a79aa5d20e1d721400dbffa564aMaciek Makowski lua_setfield(L, -2, "HTTP_PAYMENT_REQUIRED");
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder lua_pushinteger(L, HTTP_FORBIDDEN);
788dd403da4203e895e15892ef7fa48129617d30Till Mossakowski lua_setfield(L, -2, "HTTP_FORBIDDEN");
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder lua_pushinteger(L, HTTP_NOT_FOUND);
c0380b947eef252db81ee562246bb732555427f4Till Mossakowski lua_setfield(L, -2, "HTTP_NOT_FOUND");
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder lua_pushinteger(L, HTTP_METHOD_NOT_ALLOWED);
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder lua_setfield(L, -2, "HTTP_METHOD_NOT_ALLOWED");
a938729e277da5c7742bb88946ab2c150416fd5dTill Mossakowski lua_pushinteger(L, HTTP_NOT_ACCEPTABLE);
242691238a8d1a89581751d782af87ec5d7470c0Till Mossakowski lua_setfield(L, -2, "HTTP_NOT_ACCEPTABLE");
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder lua_pushinteger(L, HTTP_PROXY_AUTHENTICATION_REQUIRED);
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder lua_setfield(L, -2, "HTTP_PROXY_AUTHENTICATION_REQUIRED");
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder lua_pushinteger(L, HTTP_REQUEST_TIME_OUT);
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder lua_setfield(L, -2, "HTTP_REQUEST_TIME_OUT");
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder lua_pushinteger(L, HTTP_CONFLICT);
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder lua_setfield(L, -2, "HTTP_CONFLICT");
c0380b947eef252db81ee562246bb732555427f4Till Mossakowski lua_pushinteger(L, HTTP_GONE);
e379124f467e5d0ef7d3c0ca238bff0521f70831Till Mossakowski lua_setfield(L, -2, "HTTP_GONE");
f534c0116096e25659ceaa57de030c497ce9345aTill Mossakowski lua_pushinteger(L, HTTP_LENGTH_REQUIRED);
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder lua_setfield(L, -2, "HTTP_LENGTH_REQUIRED");
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder lua_pushinteger(L, HTTP_PRECONDITION_FAILED);
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder lua_setfield(L, -2, "HTTP_PRECONDITION_FAILED");
c0380b947eef252db81ee562246bb732555427f4Till Mossakowski lua_pushinteger(L, HTTP_REQUEST_ENTITY_TOO_LARGE);
6a57a555c8ef0a79aa5d20e1d721400dbffa564aMaciek Makowski lua_setfield(L, -2, "HTTP_REQUEST_ENTITY_TOO_LARGE");
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder lua_pushinteger(L, HTTP_REQUEST_URI_TOO_LARGE);
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder lua_setfield(L, -2, "HTTP_REQUEST_URI_TOO_LARGE");
c0380b947eef252db81ee562246bb732555427f4Till Mossakowski lua_pushinteger(L, HTTP_UNSUPPORTED_MEDIA_TYPE);
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder lua_setfield(L, -2, "HTTP_UNSUPPORTED_MEDIA_TYPE");
6a57a555c8ef0a79aa5d20e1d721400dbffa564aMaciek Makowski lua_pushinteger(L, HTTP_RANGE_NOT_SATISFIABLE);
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder lua_setfield(L, -2, "HTTP_RANGE_NOT_SATISFIABLE");
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder lua_pushinteger(L, HTTP_EXPECTATION_FAILED);
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder lua_setfield(L, -2, "HTTP_EXPECTATION_FAILED");
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder lua_pushinteger(L, HTTP_UNPROCESSABLE_ENTITY);
c0380b947eef252db81ee562246bb732555427f4Till Mossakowski lua_setfield(L, -2, "HTTP_UNPROCESSABLE_ENTITY");
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder lua_pushinteger(L, HTTP_LOCKED);
c0380b947eef252db81ee562246bb732555427f4Till Mossakowski lua_setfield(L, -2, "HTTP_LOCKED");
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder lua_pushinteger(L, HTTP_FAILED_DEPENDENCY);
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder lua_setfield(L, -2, "HTTP_FAILED_DEPENDENCY");
58b671de3fe578346fef9642ffa3c5a0a0edb3cbTill Mossakowski lua_pushinteger(L, HTTP_UPGRADE_REQUIRED);
242691238a8d1a89581751d782af87ec5d7470c0Till Mossakowski lua_setfield(L, -2, "HTTP_UPGRADE_REQUIRED");
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder lua_pushinteger(L, HTTP_INTERNAL_SERVER_ERROR);
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder lua_setfield(L, -2, "HTTP_INTERNAL_SERVER_ERROR");
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder lua_pushinteger(L, HTTP_NOT_IMPLEMENTED);
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder lua_setfield(L, -2, "HTTP_NOT_IMPLEMENTED");
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder lua_pushinteger(L, HTTP_BAD_GATEWAY);
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder lua_setfield(L, -2, "HTTP_BAD_GATEWAY");
c0380b947eef252db81ee562246bb732555427f4Till Mossakowski lua_pushinteger(L, HTTP_SERVICE_UNAVAILABLE);
c0380b947eef252db81ee562246bb732555427f4Till Mossakowski lua_setfield(L, -2, "HTTP_SERVICE_UNAVAILABLE");
f534c0116096e25659ceaa57de030c497ce9345aTill Mossakowski lua_pushinteger(L, HTTP_GATEWAY_TIME_OUT);
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder lua_setfield(L, -2, "HTTP_GATEWAY_TIME_OUT");
d08907a7832988612fbc0682b216e150d1e738d2Christian Maeder lua_pushinteger(L, HTTP_VERSION_NOT_SUPPORTED);
d08907a7832988612fbc0682b216e150d1e738d2Christian Maeder lua_setfield(L, -2, "HTTP_VERSION_NOT_SUPPORTED");
d08907a7832988612fbc0682b216e150d1e738d2Christian Maeder lua_pushinteger(L, HTTP_VARIANT_ALSO_VARIES);
d08907a7832988612fbc0682b216e150d1e738d2Christian Maeder lua_setfield(L, -2, "HTTP_VARIANT_ALSO_VARIES");
d08907a7832988612fbc0682b216e150d1e738d2Christian Maeder lua_pushinteger(L, HTTP_INSUFFICIENT_STORAGE);
d08907a7832988612fbc0682b216e150d1e738d2Christian Maeder lua_setfield(L, -2, "HTTP_INSUFFICIENT_STORAGE");
58b671de3fe578346fef9642ffa3c5a0a0edb3cbTill Mossakowski lua_pushinteger(L, HTTP_NOT_EXTENDED);
58b671de3fe578346fef9642ffa3c5a0a0edb3cbTill Mossakowski lua_setfield(L, -2, "HTTP_NOT_EXTENDED");
58b671de3fe578346fef9642ffa3c5a0a0edb3cbTill Mossakowski/* END apache2 lmodule */
242691238a8d1a89581751d782af87ec5d7470c0Till Mossakowski/* END library functions */
58b671de3fe578346fef9642ffa3c5a0a0edb3cbTill Mossakowski/* callback for cleaning up a lua vm when pool is closed */
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder munge_path(L,
242691238a8d1a89581751d782af87ec5d7470c0Till Mossakowski lifecycle_pool,
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder spec->package_paths,
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder * field -> "path" or "cpath"
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder * sub_pat -> "?.lua"
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder * rep_pat -> "./?.lua"
58b671de3fe578346fef9642ffa3c5a0a0edb3cbTill Mossakowski * pool -> lifecycle pool for allocations
242691238a8d1a89581751d782af87ec5d7470c0Till Mossakowski * paths -> things to add
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder * file -> ???
a205324331077b7d5a2c08fb3f0f57e5c029f9aaTill Mossakowski parent_dir = ap_make_dirstr_parent(pool, file);
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder pattern = apr_pstrcat(pool, parent_dir, sub_pat, NULL);
b10d6cef708b7a659f2d3b367e8e0db0d03ae3f5Till Mossakowski part = apr_pstrcat(pool, modified, ";", apr_array_pstrcat(pool, paths, ';'),
b10d6cef708b7a659f2d3b367e8e0db0d03ae3f5Till Mossakowski lua_pop(L, 1); /* pop "package" off the stack */
b10d6cef708b7a659f2d3b367e8e0db0d03ae3f5Till Mossakowskistatic int loadjitmodule(lua_State *L, apr_pool_t *lifecycle_pool) {
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder ap_log_perror(APLOG_MARK, APLOG_DEBUG, 0, lifecycle_pool, APLOGNO(01480)
1a7b7802544aa94828d7f4e7be5788501c572934Till Mossakowskistatic apr_status_t vm_construct(lua_State **vm, void *params, apr_pool_t *lifecycle_pool)
1a7b7802544aa94828d7f4e7be5788501c572934Till Mossakowski munge_path(L, "cpath", "?.so", "./?.so", lifecycle_pool,
c0380b947eef252db81ee562246bb732555427f4Till Mossakowski if (spec->bytecode && spec->bytecode_len > 0) {
3476beb5baf84bef7cc7d627b130de9d48700399Christian Maeder luaL_loadbuffer(L, spec->bytecode, spec->bytecode_len, spec->file);
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder ap_log_perror(APLOG_MARK, APLOG_DEBUG, 0, lifecycle_pool, APLOGNO(01481)
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder ap_log_perror(APLOG_MARK, APLOG_ERR, 0, lifecycle_pool, APLOGNO(01482)
3476beb5baf84bef7cc7d627b130de9d48700399Christian Maeder rc == LUA_ERRMEM ? "memory allocation error"
3476beb5baf84bef7cc7d627b130de9d48700399Christian Maeder lua_setfield(L, LUA_REGISTRYINDEX, "Apache2.Wombat.pool");
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder * Function used to create a lua_State instance bound into the web
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder * server in the appropriate scope.
b10d6cef708b7a659f2d3b367e8e0db0d03ae3f5Till MossakowskiAP_LUA_DECLARE(lua_State*)ap_lua_get_lua_state(apr_pool_t *lifecycle_pool,
f534c0116096e25659ceaa57de030c497ce9345aTill Mossakowski if (apr_pool_userdata_get((void **)&L, spec->file,
c0380b947eef252db81ee562246bb732555427f4Till Mossakowski ap_log_perror(APLOG_MARK, APLOG_DEBUG, 0, lifecycle_pool, APLOGNO(01483)
c0380b947eef252db81ee562246bb732555427f4Till Mossakowski "creating lua_State with file %s", spec->file);
6a57a555c8ef0a79aa5d20e1d721400dbffa564aMaciek Makowski /* not available, so create */