lua_vmprep.c revision 19a96e59a4250d39c542c58782b5aa06d10d03f1
33cb45dc8c5106018b7c2f6ae42478b109423e0eniq/**
78cd48acd325773619d78ac0d7263a99a8922faend * Licensed to the Apache Software Foundation (ASF) under one or more
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fielding * contributor license agreements. See the NOTICE file distributed with
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * this work for additional information regarding copyright ownership.
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * The ASF licenses this file to You under the Apache License, Version 2.0
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * (the "License"); you may not use this file except in compliance with
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * the License. You may obtain a copy of the License at
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd *
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * http://www.apache.org/licenses/LICENSE-2.0
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd *
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * Unless required by applicable law or agreed to in writing, software
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * distributed under the License is distributed on an "AS IS" BASIS,
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * See the License for the specific language governing permissions and
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * limitations under the License.
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding */
7708bd70088b64148d7d78fd84ede43ced63c713minfrin#include "mod_lua.h"
7708bd70088b64148d7d78fd84ede43ced63c713minfrin#include "http_log.h"
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding#include "apr_uuid.h"
36a72c96fc2dda27eadbae8a108fa428cc1419c1wrowe#include "lua_config.h"
1723d9ccdd3b647f5b7bae44cab9ab3eca7a4874dougm#include "apr_file_info.h"
59b96ad34c087942eea06884c97d12c2796a977amturk#include "mod_auth.h"
c0a549c3f6e8edc87e921cf76fac95d04feba72bwrowe
2555a6b5da21d61804f47084d8fcc98eb4acbc42wroweAPLOG_USE_MODULE(lua);
2555a6b5da21d61804f47084d8fcc98eb4acbc42wrowe
70535d6421eb979ac79d8f49d31cd94d75dd8b2fjorton#if APR_HAS_THREADS
2555a6b5da21d61804f47084d8fcc98eb4acbc42wrowe apr_thread_mutex_t *ap_lua_mutex;
2555a6b5da21d61804f47084d8fcc98eb4acbc42wrowe
2555a6b5da21d61804f47084d8fcc98eb4acbc42wrowevoid ap_lua_init_mutex(apr_pool_t *pool, server_rec *s)
2555a6b5da21d61804f47084d8fcc98eb4acbc42wrowe{
1723d9ccdd3b647f5b7bae44cab9ab3eca7a4874dougm apr_thread_mutex_create(&ap_lua_mutex, APR_THREAD_MUTEX_DEFAULT, pool);
e9501b71b8a1e76384cb010b1e41e76a1e47aacctrawick}
e9501b71b8a1e76384cb010b1e41e76a1e47aacctrawick#endif
e9501b71b8a1e76384cb010b1e41e76a1e47aacctrawick
6335eb31f0f0ed54628a04ed32946360b8b77684minfrin/* forward dec'l from this file */
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding#if 0
0f081398cf0eef8cc7c66a535d450110a92dc8aefieldingstatic void pstack_dump(lua_State *L, apr_pool_t *r, int level,
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding const char *msg)
2ceedfca3a2fdfdb5ff60ca17f030ce91f6331cbwrowe{
2ceedfca3a2fdfdb5ff60ca17f030ce91f6331cbwrowe int i;
2ceedfca3a2fdfdb5ff60ca17f030ce91f6331cbwrowe int top = lua_gettop(L);
2ceedfca3a2fdfdb5ff60ca17f030ce91f6331cbwrowe
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding ap_log_perror(APLOG_MARK, level, 0, r, "Lua Stack Dump: [%s]", msg);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding for (i = 1; i <= top; i++) {
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding int t = lua_type(L, i);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding switch (t) {
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding case LUA_TSTRING:{
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding ap_log_perror(APLOG_MARK, level, 0, r,
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding "%d: '%s'", i, lua_tostring(L, i));
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding break;
33cb45dc8c5106018b7c2f6ae42478b109423e0eniq }
a812b025d139f465a31c76fc02ed162ed5271b03nd case LUA_TUSERDATA:{
a812b025d139f465a31c76fc02ed162ed5271b03nd ap_log_perror(APLOG_MARK, level, 0, r, "%d: userdata", i);
a812b025d139f465a31c76fc02ed162ed5271b03nd break;
a812b025d139f465a31c76fc02ed162ed5271b03nd }
a812b025d139f465a31c76fc02ed162ed5271b03nd case LUA_TLIGHTUSERDATA:{
a812b025d139f465a31c76fc02ed162ed5271b03nd ap_log_perror(APLOG_MARK, level, 0, r, "%d: lightuserdata",
a812b025d139f465a31c76fc02ed162ed5271b03nd i);
a812b025d139f465a31c76fc02ed162ed5271b03nd break;
a812b025d139f465a31c76fc02ed162ed5271b03nd }
a812b025d139f465a31c76fc02ed162ed5271b03nd case LUA_TNIL:{
a812b025d139f465a31c76fc02ed162ed5271b03nd ap_log_perror(APLOG_MARK, level, 0, r, "%d: NIL", i);
33cb45dc8c5106018b7c2f6ae42478b109423e0eniq break;
a812b025d139f465a31c76fc02ed162ed5271b03nd }
00211b036b78699ace57a6d800a52e6c2d57652fnd case LUA_TNONE:{
00211b036b78699ace57a6d800a52e6c2d57652fnd ap_log_perror(APLOG_MARK, level, 0, r, "%d: None", i);
00211b036b78699ace57a6d800a52e6c2d57652fnd break;
a812b025d139f465a31c76fc02ed162ed5271b03nd }
a812b025d139f465a31c76fc02ed162ed5271b03nd case LUA_TBOOLEAN:{
a812b025d139f465a31c76fc02ed162ed5271b03nd ap_log_perror(APLOG_MARK, level, 0, r,
33cb45dc8c5106018b7c2f6ae42478b109423e0eniq "%d: %s", i, lua_toboolean(L,
33cb45dc8c5106018b7c2f6ae42478b109423e0eniq i) ? "true" :
a812b025d139f465a31c76fc02ed162ed5271b03nd "false");
1dac466bcc84f8ebf410016dcf2a4cd4312e8611wrowe break;
1dac466bcc84f8ebf410016dcf2a4cd4312e8611wrowe }
1dac466bcc84f8ebf410016dcf2a4cd4312e8611wrowe case LUA_TNUMBER:{
1dac466bcc84f8ebf410016dcf2a4cd4312e8611wrowe ap_log_perror(APLOG_MARK, level, 0, r,
1dac466bcc84f8ebf410016dcf2a4cd4312e8611wrowe "%d: %g", i, lua_tonumber(L, i));
1dac466bcc84f8ebf410016dcf2a4cd4312e8611wrowe break;
1dac466bcc84f8ebf410016dcf2a4cd4312e8611wrowe }
1dac466bcc84f8ebf410016dcf2a4cd4312e8611wrowe case LUA_TTABLE:{
1dac466bcc84f8ebf410016dcf2a4cd4312e8611wrowe ap_log_perror(APLOG_MARK, level, 0, r, "%d: <table>", i);
1dac466bcc84f8ebf410016dcf2a4cd4312e8611wrowe break;
eee895b02dd7867622afd0a8a94f2efc7de9c618wrowe }
eee895b02dd7867622afd0a8a94f2efc7de9c618wrowe case LUA_TTHREAD:{
38fd849bd99e2765ee633b6dc576b5f17acdc455wrowe ap_log_perror(APLOG_MARK, level, 0, r, "%d: <thread>", i);
38fd849bd99e2765ee633b6dc576b5f17acdc455wrowe break;
38fd849bd99e2765ee633b6dc576b5f17acdc455wrowe }
38fd849bd99e2765ee633b6dc576b5f17acdc455wrowe case LUA_TFUNCTION:{
38fd849bd99e2765ee633b6dc576b5f17acdc455wrowe ap_log_perror(APLOG_MARK, level, 0, r, "%d: <function>", i);
38fd849bd99e2765ee633b6dc576b5f17acdc455wrowe break;
01261c7d9578aadd1891f94c8ee03f32ba51db3dmturk }
01261c7d9578aadd1891f94c8ee03f32ba51db3dmturk default:{
01261c7d9578aadd1891f94c8ee03f32ba51db3dmturk ap_log_perror(APLOG_MARK, level, 0, r,
38fd849bd99e2765ee633b6dc576b5f17acdc455wrowe "%d: unknown: [%s]", i, lua_typename(L, i));
38fd849bd99e2765ee633b6dc576b5f17acdc455wrowe break;
1dac466bcc84f8ebf410016dcf2a4cd4312e8611wrowe }
38fd849bd99e2765ee633b6dc576b5f17acdc455wrowe }
38fd849bd99e2765ee633b6dc576b5f17acdc455wrowe }
01261c7d9578aadd1891f94c8ee03f32ba51db3dmturk}
01261c7d9578aadd1891f94c8ee03f32ba51db3dmturk#endif
01261c7d9578aadd1891f94c8ee03f32ba51db3dmturk
01261c7d9578aadd1891f94c8ee03f32ba51db3dmturk/* BEGIN modules*/
38fd849bd99e2765ee633b6dc576b5f17acdc455wrowe
38fd849bd99e2765ee633b6dc576b5f17acdc455wrowe/* BEGIN apache lmodule */
c1c0628ca9788908a5fc7502d04a89c348b75ee6wrowe
38fd849bd99e2765ee633b6dc576b5f17acdc455wrowe#define makeintegerfield(L, n) lua_pushinteger(L, n); lua_setfield(L, -2, #n)
38fd849bd99e2765ee633b6dc576b5f17acdc455wrowe
38fd849bd99e2765ee633b6dc576b5f17acdc455wroweAP_LUA_DECLARE(void) ap_lua_load_apache2_lmodule(lua_State *L)
01261c7d9578aadd1891f94c8ee03f32ba51db3dmturk{
01261c7d9578aadd1891f94c8ee03f32ba51db3dmturk lua_getglobal(L, "package");
01261c7d9578aadd1891f94c8ee03f32ba51db3dmturk lua_getfield(L, -1, "loaded");
38fd849bd99e2765ee633b6dc576b5f17acdc455wrowe lua_newtable(L);
38fd849bd99e2765ee633b6dc576b5f17acdc455wrowe lua_setfield(L, -2, "apache2");
1dac466bcc84f8ebf410016dcf2a4cd4312e8611wrowe lua_setglobal(L, "apache2");
38fd849bd99e2765ee633b6dc576b5f17acdc455wrowe lua_pop(L, 1); /* empty stack */
38fd849bd99e2765ee633b6dc576b5f17acdc455wrowe
38fd849bd99e2765ee633b6dc576b5f17acdc455wrowe lua_getglobal(L, "apache2");
01261c7d9578aadd1891f94c8ee03f32ba51db3dmturk
01261c7d9578aadd1891f94c8ee03f32ba51db3dmturk lua_pushstring(L, ap_get_server_banner());
38fd849bd99e2765ee633b6dc576b5f17acdc455wrowe lua_setfield(L, -2, "version");
38fd849bd99e2765ee633b6dc576b5f17acdc455wrowe
1dac466bcc84f8ebf410016dcf2a4cd4312e8611wrowe makeintegerfield(L, OK);
38fd849bd99e2765ee633b6dc576b5f17acdc455wrowe makeintegerfield(L, DECLINED);
38fd849bd99e2765ee633b6dc576b5f17acdc455wrowe makeintegerfield(L, DONE);
38fd849bd99e2765ee633b6dc576b5f17acdc455wrowe makeintegerfield(L, HTTP_MOVED_TEMPORARILY);
01261c7d9578aadd1891f94c8ee03f32ba51db3dmturk makeintegerfield(L, PROXYREQ_NONE);
01261c7d9578aadd1891f94c8ee03f32ba51db3dmturk makeintegerfield(L, PROXYREQ_PROXY);
38fd849bd99e2765ee633b6dc576b5f17acdc455wrowe makeintegerfield(L, PROXYREQ_REVERSE);
38fd849bd99e2765ee633b6dc576b5f17acdc455wrowe makeintegerfield(L, PROXYREQ_RESPONSE);
1dac466bcc84f8ebf410016dcf2a4cd4312e8611wrowe makeintegerfield(L, PROXYREQ_RESPONSE);
38fd849bd99e2765ee633b6dc576b5f17acdc455wrowe makeintegerfield(L, AUTHZ_DENIED);
38fd849bd99e2765ee633b6dc576b5f17acdc455wrowe makeintegerfield(L, AUTHZ_GRANTED);
38fd849bd99e2765ee633b6dc576b5f17acdc455wrowe makeintegerfield(L, AUTHZ_NEUTRAL);
38fd849bd99e2765ee633b6dc576b5f17acdc455wrowe makeintegerfield(L, AUTHZ_GENERAL_ERROR);
01261c7d9578aadd1891f94c8ee03f32ba51db3dmturk makeintegerfield(L, AUTHZ_DENIED_NO_USER);
01261c7d9578aadd1891f94c8ee03f32ba51db3dmturk
01261c7d9578aadd1891f94c8ee03f32ba51db3dmturk /*
38fd849bd99e2765ee633b6dc576b5f17acdc455wrowe makeintegerfield(L, HTTP_CONTINUE);
38fd849bd99e2765ee633b6dc576b5f17acdc455wrowe makeintegerfield(L, HTTP_SWITCHING_PROTOCOLS);
1dac466bcc84f8ebf410016dcf2a4cd4312e8611wrowe makeintegerfield(L, HTTP_PROCESSING);
38fd849bd99e2765ee633b6dc576b5f17acdc455wrowe makeintegerfield(L, HTTP_OK);
38fd849bd99e2765ee633b6dc576b5f17acdc455wrowe makeintegerfield(L, HTTP_CREATED);
9621e4c4056383e4a2b844b14687bae500b33a82wrowe makeintegerfield(L, HTTP_ACCEPTED);
01261c7d9578aadd1891f94c8ee03f32ba51db3dmturk makeintegerfield(L, HTTP_NON_AUTHORITATIVE);
01261c7d9578aadd1891f94c8ee03f32ba51db3dmturk makeintegerfield(L, HTTP_NO_CONTENT);
01261c7d9578aadd1891f94c8ee03f32ba51db3dmturk makeintegerfield(L, HTTP_RESET_CONTENT);
01261c7d9578aadd1891f94c8ee03f32ba51db3dmturk makeintegerfield(L, HTTP_PARTIAL_CONTENT);
9621e4c4056383e4a2b844b14687bae500b33a82wrowe makeintegerfield(L, HTTP_MULTI_STATUS);
9621e4c4056383e4a2b844b14687bae500b33a82wrowe makeintegerfield(L, HTTP_ALREADY_REPORTED);
1dac466bcc84f8ebf410016dcf2a4cd4312e8611wrowe makeintegerfield(L, HTTP_IM_USED);
9621e4c4056383e4a2b844b14687bae500b33a82wrowe makeintegerfield(L, HTTP_MULTIPLE_CHOICES);
9621e4c4056383e4a2b844b14687bae500b33a82wrowe makeintegerfield(L, HTTP_MOVED_PERMANENTLY);
1dac466bcc84f8ebf410016dcf2a4cd4312e8611wrowe makeintegerfield(L, HTTP_MOVED_TEMPORARILY);
4c67ef499845a08771e81254ce6eb2324a160bc7wrowe makeintegerfield(L, HTTP_SEE_OTHER);
01261c7d9578aadd1891f94c8ee03f32ba51db3dmturk makeintegerfield(L, HTTP_NOT_MODIFIED);
01261c7d9578aadd1891f94c8ee03f32ba51db3dmturk makeintegerfield(L, HTTP_USE_PROXY);
01261c7d9578aadd1891f94c8ee03f32ba51db3dmturk makeintegerfield(L, HTTP_TEMPORARY_REDIRECT);
4c67ef499845a08771e81254ce6eb2324a160bc7wrowe makeintegerfield(L, HTTP_PERMANENT_REDIRECT);
4c67ef499845a08771e81254ce6eb2324a160bc7wrowe makeintegerfield(L, HTTP_BAD_REQUEST);
1dac466bcc84f8ebf410016dcf2a4cd4312e8611wrowe makeintegerfield(L, HTTP_UNAUTHORIZED);
4c67ef499845a08771e81254ce6eb2324a160bc7wrowe makeintegerfield(L, HTTP_PAYMENT_REQUIRED);
4c67ef499845a08771e81254ce6eb2324a160bc7wrowe makeintegerfield(L, HTTP_FORBIDDEN);
1dac466bcc84f8ebf410016dcf2a4cd4312e8611wrowe makeintegerfield(L, HTTP_NOT_FOUND);
1dac466bcc84f8ebf410016dcf2a4cd4312e8611wrowe makeintegerfield(L, HTTP_METHOD_NOT_ALLOWED);
1dac466bcc84f8ebf410016dcf2a4cd4312e8611wrowe makeintegerfield(L, HTTP_NOT_ACCEPTABLE);
1dac466bcc84f8ebf410016dcf2a4cd4312e8611wrowe makeintegerfield(L, HTTP_PROXY_AUTHENTICATION_REQUIRED);
1dac466bcc84f8ebf410016dcf2a4cd4312e8611wrowe makeintegerfield(L, HTTP_REQUEST_TIME_OUT);
1dac466bcc84f8ebf410016dcf2a4cd4312e8611wrowe makeintegerfield(L, HTTP_CONFLICT);
1dac466bcc84f8ebf410016dcf2a4cd4312e8611wrowe makeintegerfield(L, HTTP_GONE);
1dac466bcc84f8ebf410016dcf2a4cd4312e8611wrowe makeintegerfield(L, HTTP_LENGTH_REQUIRED);
1dac466bcc84f8ebf410016dcf2a4cd4312e8611wrowe makeintegerfield(L, HTTP_PRECONDITION_FAILED);
1dac466bcc84f8ebf410016dcf2a4cd4312e8611wrowe makeintegerfield(L, HTTP_REQUEST_ENTITY_TOO_LARGE);
1dac466bcc84f8ebf410016dcf2a4cd4312e8611wrowe makeintegerfield(L, HTTP_REQUEST_URI_TOO_LARGE);
1dac466bcc84f8ebf410016dcf2a4cd4312e8611wrowe makeintegerfield(L, HTTP_UNSUPPORTED_MEDIA_TYPE);
1dac466bcc84f8ebf410016dcf2a4cd4312e8611wrowe makeintegerfield(L, HTTP_RANGE_NOT_SATISFIABLE);
1dac466bcc84f8ebf410016dcf2a4cd4312e8611wrowe makeintegerfield(L, HTTP_EXPECTATION_FAILED);
41c38e78e8e5dc73544571cc2b749d40869e84fawrowe makeintegerfield(L, HTTP_UNPROCESSABLE_ENTITY);
41c38e78e8e5dc73544571cc2b749d40869e84fawrowe makeintegerfield(L, HTTP_LOCKED);
41c38e78e8e5dc73544571cc2b749d40869e84fawrowe makeintegerfield(L, HTTP_FAILED_DEPENDENCY);
41c38e78e8e5dc73544571cc2b749d40869e84fawrowe makeintegerfield(L, HTTP_UPGRADE_REQUIRED);
41c38e78e8e5dc73544571cc2b749d40869e84fawrowe makeintegerfield(L, HTTP_PRECONDITION_REQUIRED);
41c38e78e8e5dc73544571cc2b749d40869e84fawrowe makeintegerfield(L, HTTP_TOO_MANY_REQUESTS);
41c38e78e8e5dc73544571cc2b749d40869e84fawrowe makeintegerfield(L, HTTP_REQUEST_HEADER_FIELDS_TOO_LARGE);
41c38e78e8e5dc73544571cc2b749d40869e84fawrowe makeintegerfield(L, HTTP_INTERNAL_SERVER_ERROR);
41c38e78e8e5dc73544571cc2b749d40869e84fawrowe makeintegerfield(L, HTTP_NOT_IMPLEMENTED);
eee895b02dd7867622afd0a8a94f2efc7de9c618wrowe makeintegerfield(L, HTTP_BAD_GATEWAY);
01261c7d9578aadd1891f94c8ee03f32ba51db3dmturk makeintegerfield(L, HTTP_SERVICE_UNAVAILABLE);
01261c7d9578aadd1891f94c8ee03f32ba51db3dmturk makeintegerfield(L, HTTP_GATEWAY_TIME_OUT);
1d2ff7570139286b0f0d16f92187a16ed5932291mturk makeintegerfield(L, HTTP_VERSION_NOT_SUPPORTED);
1d2ff7570139286b0f0d16f92187a16ed5932291mturk makeintegerfield(L, HTTP_VARIANT_ALSO_VARIES);
eee895b02dd7867622afd0a8a94f2efc7de9c618wrowe makeintegerfield(L, HTTP_INSUFFICIENT_STORAGE);
eee895b02dd7867622afd0a8a94f2efc7de9c618wrowe makeintegerfield(L, HTTP_LOOP_DETECTED);
eee895b02dd7867622afd0a8a94f2efc7de9c618wrowe makeintegerfield(L, HTTP_NOT_EXTENDED);
01261c7d9578aadd1891f94c8ee03f32ba51db3dmturk makeintegerfield(L, HTTP_NETWORK_AUTHENTICATION_REQUIRED);
01261c7d9578aadd1891f94c8ee03f32ba51db3dmturk */
1d2ff7570139286b0f0d16f92187a16ed5932291mturk}
1d2ff7570139286b0f0d16f92187a16ed5932291mturk
eee895b02dd7867622afd0a8a94f2efc7de9c618wrowe/* END apache2 lmodule */
eee895b02dd7867622afd0a8a94f2efc7de9c618wrowe
4415d997ac73262e513c0a571bd5be4f609040bawrowe/* END library functions */
65efbf0826de766a90d745cc44427bfa4e2447b6mturk
4415d997ac73262e513c0a571bd5be4f609040bawrowe/* callback for cleaning up a lua vm when pool is closed */
4415d997ac73262e513c0a571bd5be4f609040bawrowestatic apr_status_t cleanup_lua(void *l)
4415d997ac73262e513c0a571bd5be4f609040bawrowe{
38fd849bd99e2765ee633b6dc576b5f17acdc455wrowe AP_DEBUG_ASSERT(l != NULL);
eee895b02dd7867622afd0a8a94f2efc7de9c618wrowe lua_close((lua_State *) l);
eee895b02dd7867622afd0a8a94f2efc7de9c618wrowe return APR_SUCCESS;
4415d997ac73262e513c0a571bd5be4f609040bawrowe}
4415d997ac73262e513c0a571bd5be4f609040bawrowe
4415d997ac73262e513c0a571bd5be4f609040bawrowestatic apr_status_t server_cleanup_lua(void *resource)
4415d997ac73262e513c0a571bd5be4f609040bawrowe{
4415d997ac73262e513c0a571bd5be4f609040bawrowe ap_lua_server_spec* spec = (ap_lua_server_spec*) resource;
4415d997ac73262e513c0a571bd5be4f609040bawrowe AP_DEBUG_ASSERT(spec != NULL);
01261c7d9578aadd1891f94c8ee03f32ba51db3dmturk if (spec->L != NULL) {
01261c7d9578aadd1891f94c8ee03f32ba51db3dmturk lua_close((lua_State *) spec->L);
01261c7d9578aadd1891f94c8ee03f32ba51db3dmturk }
01261c7d9578aadd1891f94c8ee03f32ba51db3dmturk return APR_SUCCESS;
eee895b02dd7867622afd0a8a94f2efc7de9c618wrowe}
4415d997ac73262e513c0a571bd5be4f609040bawrowe
4415d997ac73262e513c0a571bd5be4f609040bawrowe/*
01261c7d9578aadd1891f94c8ee03f32ba51db3dmturk munge_path(L,
01261c7d9578aadd1891f94c8ee03f32ba51db3dmturk "path",
01261c7d9578aadd1891f94c8ee03f32ba51db3dmturk "?.lua",
01261c7d9578aadd1891f94c8ee03f32ba51db3dmturk "./?.lua",
4415d997ac73262e513c0a571bd5be4f609040bawrowe lifecycle_pool,
4415d997ac73262e513c0a571bd5be4f609040bawrowe spec->package_paths,
4415d997ac73262e513c0a571bd5be4f609040bawrowe spec->file);
4415d997ac73262e513c0a571bd5be4f609040bawrowe*/
4415d997ac73262e513c0a571bd5be4f609040bawrowe/**
4415d997ac73262e513c0a571bd5be4f609040bawrowe * field -> "path" or "cpath"
4415d997ac73262e513c0a571bd5be4f609040bawrowe * sub_pat -> "?.lua"
4415d997ac73262e513c0a571bd5be4f609040bawrowe * rep_pat -> "./?.lua"
01261c7d9578aadd1891f94c8ee03f32ba51db3dmturk * pool -> lifecycle pool for allocations
01261c7d9578aadd1891f94c8ee03f32ba51db3dmturk * paths -> things to add
01261c7d9578aadd1891f94c8ee03f32ba51db3dmturk * file -> ???
01261c7d9578aadd1891f94c8ee03f32ba51db3dmturk */
01261c7d9578aadd1891f94c8ee03f32ba51db3dmturkstatic void munge_path(lua_State *L,
4415d997ac73262e513c0a571bd5be4f609040bawrowe const char *field,
4415d997ac73262e513c0a571bd5be4f609040bawrowe const char *sub_pat,
eee895b02dd7867622afd0a8a94f2efc7de9c618wrowe const char *rep_pat,
4415d997ac73262e513c0a571bd5be4f609040bawrowe apr_pool_t *pool,
4415d997ac73262e513c0a571bd5be4f609040bawrowe apr_array_header_t *paths,
00b70ae978854b5eb51722cbeda99c9067b5faf2mturk const char *file)
00b70ae978854b5eb51722cbeda99c9067b5faf2mturk{
00b70ae978854b5eb51722cbeda99c9067b5faf2mturk const char *current;
00b70ae978854b5eb51722cbeda99c9067b5faf2mturk const char *parent_dir;
00b70ae978854b5eb51722cbeda99c9067b5faf2mturk const char *pattern;
00b70ae978854b5eb51722cbeda99c9067b5faf2mturk const char *modified;
00b70ae978854b5eb51722cbeda99c9067b5faf2mturk char *part;
00b70ae978854b5eb51722cbeda99c9067b5faf2mturk
00b70ae978854b5eb51722cbeda99c9067b5faf2mturk lua_getglobal(L, "package");
00b70ae978854b5eb51722cbeda99c9067b5faf2mturk lua_getfield(L, -1, field);
4415d997ac73262e513c0a571bd5be4f609040bawrowe
65efbf0826de766a90d745cc44427bfa4e2447b6mturk current = lua_tostring(L, -1);
4415d997ac73262e513c0a571bd5be4f609040bawrowe
38fd849bd99e2765ee633b6dc576b5f17acdc455wrowe parent_dir = ap_make_dirstr_parent(pool, file);
38fd849bd99e2765ee633b6dc576b5f17acdc455wrowe
38fd849bd99e2765ee633b6dc576b5f17acdc455wrowe pattern = apr_pstrcat(pool, parent_dir, sub_pat, NULL);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding luaL_gsub(L, current, rep_pat, pattern);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding lua_setfield(L, -3, field);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding lua_getfield(L, -2, field);
00211b036b78699ace57a6d800a52e6c2d57652fnd modified = lua_tostring(L, -1);
a812b025d139f465a31c76fc02ed162ed5271b03nd
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
00211b036b78699ace57a6d800a52e6c2d57652fnd lua_pop(L, 2);
e6366481b8fe06a24337f0b30b7da66cf64d6062stoddard
e6366481b8fe06a24337f0b30b7da66cf64d6062stoddard part = apr_pstrcat(pool, modified, ";", apr_array_pstrcat(pool, paths, ';'),
e6366481b8fe06a24337f0b30b7da66cf64d6062stoddard NULL);
e6366481b8fe06a24337f0b30b7da66cf64d6062stoddard
e6366481b8fe06a24337f0b30b7da66cf64d6062stoddard lua_pushstring(L, part);
e6366481b8fe06a24337f0b30b7da66cf64d6062stoddard lua_setfield(L, -2, field);
e6366481b8fe06a24337f0b30b7da66cf64d6062stoddard lua_pop(L, 1); /* pop "package" off the stack */
e6366481b8fe06a24337f0b30b7da66cf64d6062stoddard}
e6366481b8fe06a24337f0b30b7da66cf64d6062stoddard
e6366481b8fe06a24337f0b30b7da66cf64d6062stoddard#ifdef AP_ENABLE_LUAJIT
e6366481b8fe06a24337f0b30b7da66cf64d6062stoddardstatic int loadjitmodule(lua_State *L, apr_pool_t *lifecycle_pool) {
e6366481b8fe06a24337f0b30b7da66cf64d6062stoddard lua_getglobal(L, "require");
e6366481b8fe06a24337f0b30b7da66cf64d6062stoddard lua_pushliteral(L, "jit.");
33cb45dc8c5106018b7c2f6ae42478b109423e0eniq lua_pushvalue(L, -3);
e6366481b8fe06a24337f0b30b7da66cf64d6062stoddard lua_concat(L, 2);
e6366481b8fe06a24337f0b30b7da66cf64d6062stoddard if (lua_pcall(L, 1, 1, 0)) {
e6366481b8fe06a24337f0b30b7da66cf64d6062stoddard const char *msg = lua_tostring(L, -1);
33cb45dc8c5106018b7c2f6ae42478b109423e0eniq ap_log_perror(APLOG_MARK, APLOG_DEBUG, 0, lifecycle_pool, APLOGNO(01480)
33cb45dc8c5106018b7c2f6ae42478b109423e0eniq "Failed to init LuaJIT: %s", msg);
a812b025d139f465a31c76fc02ed162ed5271b03nd return 1;
a812b025d139f465a31c76fc02ed162ed5271b03nd }
a812b025d139f465a31c76fc02ed162ed5271b03nd lua_getfield(L, -1, "start");
33cb45dc8c5106018b7c2f6ae42478b109423e0eniq lua_remove(L, -2); /* drop module table */
a812b025d139f465a31c76fc02ed162ed5271b03nd return 0;
33cb45dc8c5106018b7c2f6ae42478b109423e0eniq}
a812b025d139f465a31c76fc02ed162ed5271b03nd
a812b025d139f465a31c76fc02ed162ed5271b03nd#endif
a812b025d139f465a31c76fc02ed162ed5271b03nd
33cb45dc8c5106018b7c2f6ae42478b109423e0eniqstatic apr_status_t vm_construct(lua_State **vm, void *params, apr_pool_t *lifecycle_pool)
a812b025d139f465a31c76fc02ed162ed5271b03nd{
33cb45dc8c5106018b7c2f6ae42478b109423e0eniq lua_State* L;
a812b025d139f465a31c76fc02ed162ed5271b03nd
33cb45dc8c5106018b7c2f6ae42478b109423e0eniq ap_lua_vm_spec *spec = params;
33cb45dc8c5106018b7c2f6ae42478b109423e0eniq
33cb45dc8c5106018b7c2f6ae42478b109423e0eniq L = luaL_newstate();
e6366481b8fe06a24337f0b30b7da66cf64d6062stoddard#ifdef AP_ENABLE_LUAJIT
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding luaopen_jit(L);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding#endif
00211b036b78699ace57a6d800a52e6c2d57652fnd luaL_openlibs(L);
00211b036b78699ace57a6d800a52e6c2d57652fnd if (spec->package_paths) {
00211b036b78699ace57a6d800a52e6c2d57652fnd munge_path(L,
00211b036b78699ace57a6d800a52e6c2d57652fnd "path", "?.lua", "./?.lua",
00211b036b78699ace57a6d800a52e6c2d57652fnd lifecycle_pool,
00211b036b78699ace57a6d800a52e6c2d57652fnd spec->package_paths,
00211b036b78699ace57a6d800a52e6c2d57652fnd spec->file);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding }
50b2d068ddf98cf75622a0020cd143d379d1b235jfclere if (spec->package_cpaths) {
50b2d068ddf98cf75622a0020cd143d379d1b235jfclere munge_path(L, "cpath", "?.so", "./?.so", lifecycle_pool,
50b2d068ddf98cf75622a0020cd143d379d1b235jfclere spec->package_cpaths, spec->file);
50b2d068ddf98cf75622a0020cd143d379d1b235jfclere }
50b2d068ddf98cf75622a0020cd143d379d1b235jfclere
50b2d068ddf98cf75622a0020cd143d379d1b235jfclere if (spec->cb) {
50b2d068ddf98cf75622a0020cd143d379d1b235jfclere spec->cb(L, lifecycle_pool, spec->cb_arg);
00211b036b78699ace57a6d800a52e6c2d57652fnd }
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
e6366481b8fe06a24337f0b30b7da66cf64d6062stoddard
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding if (spec->bytecode && spec->bytecode_len > 0) {
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding luaL_loadbuffer(L, spec->bytecode, spec->bytecode_len, spec->file);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding lua_pcall(L, 0, LUA_MULTRET, 0);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding }
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding else {
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding int rc;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding ap_log_perror(APLOG_MARK, APLOG_DEBUG, 0, lifecycle_pool, APLOGNO(01481)
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding "loading lua file %s", spec->file);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding rc = luaL_loadfile(L, spec->file);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding if (rc != 0) {
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding ap_log_perror(APLOG_MARK, APLOG_ERR, 0, lifecycle_pool, APLOGNO(01482)
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding "Error loading %s: %s", spec->file,
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding rc == LUA_ERRMEM ? "memory allocation error"
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding : lua_tostring(L, 0));
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding return APR_EBADF;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding }
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding lua_pcall(L, 0, LUA_MULTRET, 0);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding }
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding#ifdef AP_ENABLE_LUAJIT
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding loadjitmodule(L, lifecycle_pool);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding#endif
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding lua_pushlightuserdata(L, lifecycle_pool);
33cb45dc8c5106018b7c2f6ae42478b109423e0eniq lua_setfield(L, LUA_REGISTRYINDEX, "Apache2.Wombat.pool");
a812b025d139f465a31c76fc02ed162ed5271b03nd *vm = L;
33cb45dc8c5106018b7c2f6ae42478b109423e0eniq
33cb45dc8c5106018b7c2f6ae42478b109423e0eniq return APR_SUCCESS;
33cb45dc8c5106018b7c2f6ae42478b109423e0eniq}
33cb45dc8c5106018b7c2f6ae42478b109423e0eniq
0f081398cf0eef8cc7c66a535d450110a92dc8aefieldingstatic ap_lua_vm_spec* copy_vm_spec(apr_pool_t* pool, ap_lua_vm_spec* spec)
16d38ac65d7e54cd44eeda7b23f84ee68b35094ewrowe{
16d38ac65d7e54cd44eeda7b23f84ee68b35094ewrowe ap_lua_vm_spec* copied_spec = apr_pcalloc(pool, sizeof(ap_lua_vm_spec));
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding copied_spec->bytecode_len = spec->bytecode_len;
e6366481b8fe06a24337f0b30b7da66cf64d6062stoddard copied_spec->bytecode = spec->bytecode ? apr_pstrdup(pool, spec->bytecode) : 0;
e6366481b8fe06a24337f0b30b7da66cf64d6062stoddard copied_spec->cb = spec->cb;
e6366481b8fe06a24337f0b30b7da66cf64d6062stoddard copied_spec->cb_arg = NULL;
e6366481b8fe06a24337f0b30b7da66cf64d6062stoddard copied_spec->file = spec->file ? apr_pstrdup(pool, spec->file) : 0;
e6366481b8fe06a24337f0b30b7da66cf64d6062stoddard copied_spec->package_cpaths = apr_array_copy(pool, spec->package_cpaths);
e6366481b8fe06a24337f0b30b7da66cf64d6062stoddard copied_spec->package_paths = apr_array_copy(pool, spec->package_paths);
e6366481b8fe06a24337f0b30b7da66cf64d6062stoddard copied_spec->pool = pool;
e6366481b8fe06a24337f0b30b7da66cf64d6062stoddard copied_spec->scope = AP_LUA_SCOPE_SERVER;
e6366481b8fe06a24337f0b30b7da66cf64d6062stoddard copied_spec->codecache = spec->codecache;
e6366481b8fe06a24337f0b30b7da66cf64d6062stoddard return copied_spec;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding}
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
0f081398cf0eef8cc7c66a535d450110a92dc8aefieldingstatic apr_status_t server_vm_construct(lua_State **resource, void *params, apr_pool_t *pool)
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding{
e6366481b8fe06a24337f0b30b7da66cf64d6062stoddard lua_State* L;
e6366481b8fe06a24337f0b30b7da66cf64d6062stoddard ap_lua_server_spec* spec = apr_pcalloc(pool, sizeof(ap_lua_server_spec));
e6366481b8fe06a24337f0b30b7da66cf64d6062stoddard if (vm_construct(&L, params, pool) == APR_SUCCESS) {
e6366481b8fe06a24337f0b30b7da66cf64d6062stoddard spec->finfo = apr_pcalloc(pool, sizeof(ap_lua_finfo));
e6366481b8fe06a24337f0b30b7da66cf64d6062stoddard if (L != NULL) {
e6366481b8fe06a24337f0b30b7da66cf64d6062stoddard spec->L = L;
33cb45dc8c5106018b7c2f6ae42478b109423e0eniq *resource = (void*) spec;
33cb45dc8c5106018b7c2f6ae42478b109423e0eniq lua_pushlightuserdata(L, spec);
33cb45dc8c5106018b7c2f6ae42478b109423e0eniq lua_setfield(L, LUA_REGISTRYINDEX, "Apache2.Lua.server_spec");
33cb45dc8c5106018b7c2f6ae42478b109423e0eniq return APR_SUCCESS;
33cb45dc8c5106018b7c2f6ae42478b109423e0eniq }
33cb45dc8c5106018b7c2f6ae42478b109423e0eniq }
33cb45dc8c5106018b7c2f6ae42478b109423e0eniq return APR_EGENERAL;
a812b025d139f465a31c76fc02ed162ed5271b03nd}
33cb45dc8c5106018b7c2f6ae42478b109423e0eniq
33cb45dc8c5106018b7c2f6ae42478b109423e0eniq/**
33cb45dc8c5106018b7c2f6ae42478b109423e0eniq * Function used to create a lua_State instance bound into the web
33cb45dc8c5106018b7c2f6ae42478b109423e0eniq * server in the appropriate scope.
33cb45dc8c5106018b7c2f6ae42478b109423e0eniq */
33cb45dc8c5106018b7c2f6ae42478b109423e0eniqAP_LUA_DECLARE(lua_State*)ap_lua_get_lua_state(apr_pool_t *lifecycle_pool,
33cb45dc8c5106018b7c2f6ae42478b109423e0eniq ap_lua_vm_spec *spec, request_rec* r)
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding{
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding lua_State *L = NULL;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding ap_lua_finfo *cache_info;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding int tryCache = 0;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding if (spec->scope == AP_LUA_SCOPE_SERVER) {
134330b92fbc39045b7e56654f4c252fb7c53803nd char *hash;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding apr_reslist_t* reslist = NULL;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding ap_lua_server_spec* sspec = NULL;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding hash = apr_psprintf(r->pool, "reslist:%s", spec->file);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding#if APR_HAS_THREADS
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding apr_thread_mutex_lock(ap_lua_mutex);
134330b92fbc39045b7e56654f4c252fb7c53803nd#endif
a812b025d139f465a31c76fc02ed162ed5271b03nd if (apr_pool_userdata_get((void **)&reslist, hash,
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding r->server->process->pool) == APR_SUCCESS) {
e6366481b8fe06a24337f0b30b7da66cf64d6062stoddard if (reslist != NULL) {
e6366481b8fe06a24337f0b30b7da66cf64d6062stoddard if (apr_reslist_acquire(reslist, (void**) &sspec) == APR_SUCCESS) {
e6366481b8fe06a24337f0b30b7da66cf64d6062stoddard L = sspec->L;
e6366481b8fe06a24337f0b30b7da66cf64d6062stoddard cache_info = sspec->finfo;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding }
a812b025d139f465a31c76fc02ed162ed5271b03nd }
33cb45dc8c5106018b7c2f6ae42478b109423e0eniq }
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding if (L == NULL) {
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding ap_lua_vm_spec* server_spec = copy_vm_spec(r->server->process->pool, spec);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding apr_reslist_create(&reslist, spec->vm_min, spec->vm_max, spec->vm_max, 0,
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding (apr_reslist_constructor) server_vm_construct,
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding (apr_reslist_destructor) server_cleanup_lua,
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding server_spec, r->server->process->pool);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding apr_pool_userdata_set(reslist, hash, NULL,
e6366481b8fe06a24337f0b30b7da66cf64d6062stoddard r->server->process->pool);
11c3b5180e1de6776035320b012a28bb146e7b46chuck if (apr_reslist_acquire(reslist, (void**) &sspec) == APR_SUCCESS) {
a812b025d139f465a31c76fc02ed162ed5271b03nd L = sspec->L;
22d348febc3c258df246ac93e37945398dbf0348ianh cache_info = sspec->finfo;
22d348febc3c258df246ac93e37945398dbf0348ianh }
22d348febc3c258df246ac93e37945398dbf0348ianh }
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm#if APR_HAS_THREADS
a812b025d139f465a31c76fc02ed162ed5271b03nd apr_thread_mutex_unlock(ap_lua_mutex);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding#endif
1d7f1b96b49dafbd6cb414fb709cb85de2686a72chuck }
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding else {
e6366481b8fe06a24337f0b30b7da66cf64d6062stoddard if (apr_pool_userdata_get((void **)&L, spec->file,
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding lifecycle_pool) != APR_SUCCESS) {
33cb45dc8c5106018b7c2f6ae42478b109423e0eniq L = NULL;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding }
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding }
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding if (L == NULL) {
9b3001f2097437c3c605d29e353fda5131b9952bminfrin ap_log_perror(APLOG_MARK, APLOG_DEBUG, 0, lifecycle_pool, APLOGNO(01483)
2ceedfca3a2fdfdb5ff60ca17f030ce91f6331cbwrowe "creating lua_State with file %s", spec->file);
2ceedfca3a2fdfdb5ff60ca17f030ce91f6331cbwrowe /* not available, so create */
2ceedfca3a2fdfdb5ff60ca17f030ce91f6331cbwrowe
2ceedfca3a2fdfdb5ff60ca17f030ce91f6331cbwrowe if (!vm_construct(&L, spec, lifecycle_pool)) {
2ceedfca3a2fdfdb5ff60ca17f030ce91f6331cbwrowe AP_DEBUG_ASSERT(L != NULL);
2ceedfca3a2fdfdb5ff60ca17f030ce91f6331cbwrowe apr_pool_userdata_set(L, spec->file, cleanup_lua, lifecycle_pool);
2ceedfca3a2fdfdb5ff60ca17f030ce91f6331cbwrowe }
2ceedfca3a2fdfdb5ff60ca17f030ce91f6331cbwrowe }
e6366481b8fe06a24337f0b30b7da66cf64d6062stoddard
e6366481b8fe06a24337f0b30b7da66cf64d6062stoddard if (spec->codecache == AP_LUA_CACHE_FOREVER || (spec->bytecode && spec->bytecode_len > 0)) {
e6366481b8fe06a24337f0b30b7da66cf64d6062stoddard tryCache = 1;
2ceedfca3a2fdfdb5ff60ca17f030ce91f6331cbwrowe }
2ceedfca3a2fdfdb5ff60ca17f030ce91f6331cbwrowe else {
2ceedfca3a2fdfdb5ff60ca17f030ce91f6331cbwrowe char* mkey;
2ceedfca3a2fdfdb5ff60ca17f030ce91f6331cbwrowe if (spec->scope != AP_LUA_SCOPE_SERVER) {
2ceedfca3a2fdfdb5ff60ca17f030ce91f6331cbwrowe mkey = apr_psprintf(r->pool, "ap_lua_modified:%s", spec->file);
2ceedfca3a2fdfdb5ff60ca17f030ce91f6331cbwrowe apr_pool_userdata_get((void **)&cache_info, mkey, lifecycle_pool);
2ceedfca3a2fdfdb5ff60ca17f030ce91f6331cbwrowe if (cache_info == NULL) {
2ceedfca3a2fdfdb5ff60ca17f030ce91f6331cbwrowe cache_info = apr_pcalloc(lifecycle_pool, sizeof(ap_lua_finfo));
2ceedfca3a2fdfdb5ff60ca17f030ce91f6331cbwrowe apr_pool_userdata_set((void*) cache_info, mkey, NULL, lifecycle_pool);
2ceedfca3a2fdfdb5ff60ca17f030ce91f6331cbwrowe }
2ceedfca3a2fdfdb5ff60ca17f030ce91f6331cbwrowe }
2ceedfca3a2fdfdb5ff60ca17f030ce91f6331cbwrowe if (spec->codecache == AP_LUA_CACHE_STAT) {
2ceedfca3a2fdfdb5ff60ca17f030ce91f6331cbwrowe apr_finfo_t lua_finfo;
2ceedfca3a2fdfdb5ff60ca17f030ce91f6331cbwrowe apr_stat(&lua_finfo, spec->file, APR_FINFO_MTIME|APR_FINFO_SIZE, lifecycle_pool);
2ceedfca3a2fdfdb5ff60ca17f030ce91f6331cbwrowe
2ceedfca3a2fdfdb5ff60ca17f030ce91f6331cbwrowe /* On first visit, modified will be zero, but that's fine - The file is
2ceedfca3a2fdfdb5ff60ca17f030ce91f6331cbwrowe loaded in the vm_construct function.
2ceedfca3a2fdfdb5ff60ca17f030ce91f6331cbwrowe */
2ceedfca3a2fdfdb5ff60ca17f030ce91f6331cbwrowe if ((cache_info->modified == lua_finfo.mtime && cache_info->size == lua_finfo.size) \
2ceedfca3a2fdfdb5ff60ca17f030ce91f6331cbwrowe || cache_info->modified == 0) {
2ceedfca3a2fdfdb5ff60ca17f030ce91f6331cbwrowe tryCache = 1;
2ceedfca3a2fdfdb5ff60ca17f030ce91f6331cbwrowe }
2ceedfca3a2fdfdb5ff60ca17f030ce91f6331cbwrowe cache_info->modified = lua_finfo.mtime;
2ceedfca3a2fdfdb5ff60ca17f030ce91f6331cbwrowe cache_info->size = lua_finfo.size;
2ceedfca3a2fdfdb5ff60ca17f030ce91f6331cbwrowe }
2ceedfca3a2fdfdb5ff60ca17f030ce91f6331cbwrowe else if (spec->codecache == AP_LUA_CACHE_NEVER) {
2ceedfca3a2fdfdb5ff60ca17f030ce91f6331cbwrowe if (cache_info->runs == 0)
2ceedfca3a2fdfdb5ff60ca17f030ce91f6331cbwrowe tryCache = 1;
2ceedfca3a2fdfdb5ff60ca17f030ce91f6331cbwrowe }
2ceedfca3a2fdfdb5ff60ca17f030ce91f6331cbwrowe cache_info->runs++;
2ceedfca3a2fdfdb5ff60ca17f030ce91f6331cbwrowe }
2ceedfca3a2fdfdb5ff60ca17f030ce91f6331cbwrowe if (tryCache == 0 && spec->scope != AP_LUA_SCOPE_ONCE) {
2ceedfca3a2fdfdb5ff60ca17f030ce91f6331cbwrowe int rc;
8632261c895a84c88ae6ade6ea4c62b27bd22b3ebrianp ap_log_perror(APLOG_MARK, APLOG_DEBUG, 0, lifecycle_pool, APLOGNO(02332)
2ceedfca3a2fdfdb5ff60ca17f030ce91f6331cbwrowe "(re)loading lua file %s", spec->file);
2ceedfca3a2fdfdb5ff60ca17f030ce91f6331cbwrowe rc = luaL_loadfile(L, spec->file);
2ceedfca3a2fdfdb5ff60ca17f030ce91f6331cbwrowe if (rc != 0) {
2ceedfca3a2fdfdb5ff60ca17f030ce91f6331cbwrowe ap_log_perror(APLOG_MARK, APLOG_ERR, 0, lifecycle_pool, APLOGNO(02333)
2ceedfca3a2fdfdb5ff60ca17f030ce91f6331cbwrowe "Error loading %s: %s", spec->file,
2ceedfca3a2fdfdb5ff60ca17f030ce91f6331cbwrowe rc == LUA_ERRMEM ? "memory allocation error"
2ceedfca3a2fdfdb5ff60ca17f030ce91f6331cbwrowe : lua_tostring(L, 0));
2ceedfca3a2fdfdb5ff60ca17f030ce91f6331cbwrowe return 0;
2ceedfca3a2fdfdb5ff60ca17f030ce91f6331cbwrowe }
2ceedfca3a2fdfdb5ff60ca17f030ce91f6331cbwrowe lua_pcall(L, 0, LUA_MULTRET, 0);
2ceedfca3a2fdfdb5ff60ca17f030ce91f6331cbwrowe }
2ceedfca3a2fdfdb5ff60ca17f030ce91f6331cbwrowe
33cb45dc8c5106018b7c2f6ae42478b109423e0eniq return L;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding}
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding