httpd-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From humbed...@apache.org
Subject svn commit: r1604339 - in /httpd/httpd/branches/2.4.x: CHANGES modules/lua/lua_request.c modules/lua/mod_lua.c
Date Sat, 21 Jun 2014 11:19:40 GMT
Author: humbedooh
Date: Sat Jun 21 11:19:40 2014
New Revision: 1604339

URL: http://svn.apache.org/r1604339
Log:
mod_lua: Sync 2.4.x with trunk (more or less); Backport a fix and some code harmonization:
- IVM changed to use shm
- More verbose error logging (no functional change)
- Miscellaneous refactoring that was in trunk but not in 2.4.x (no functional change as such)

Modified:
    httpd/httpd/branches/2.4.x/CHANGES
    httpd/httpd/branches/2.4.x/modules/lua/lua_request.c
    httpd/httpd/branches/2.4.x/modules/lua/mod_lua.c

Modified: httpd/httpd/branches/2.4.x/CHANGES
URL: http://svn.apache.org/viewvc/httpd/httpd/branches/2.4.x/CHANGES?rev=1604339&r1=1604338&r2=1604339&view=diff
==============================================================================
--- httpd/httpd/branches/2.4.x/CHANGES [utf-8] (original)
+++ httpd/httpd/branches/2.4.x/CHANGES [utf-8] Sat Jun 21 11:19:40 2014
@@ -136,6 +136,12 @@ Changes with Apache 2.4.10
      to prevent HTTP Response Splitting via tainted headers.
      [Daniel Gruno, Felipe Daragon <filipe syhunt com>]
 
+  *) mod_lua: Change IVM storage to use shm [Daniel Gruno]
+
+  *) mod_lua: More verbose error logging when a handler function cannot be
+     found. [Daniel Gruno]
+
+
 Changes with Apache 2.4.9
 
   *) mod_ssl: Work around a bug in some older versions of OpenSSL that

Modified: httpd/httpd/branches/2.4.x/modules/lua/lua_request.c
URL: http://svn.apache.org/viewvc/httpd/httpd/branches/2.4.x/modules/lua/lua_request.c?rev=1604339&r1=1604338&r2=1604339&view=diff
==============================================================================
--- httpd/httpd/branches/2.4.x/modules/lua/lua_request.c (original)
+++ httpd/httpd/branches/2.4.x/modules/lua/lua_request.c Sat Jun 21 11:19:40 2014
@@ -32,7 +32,8 @@
 #define APR_WANT_BYTEFUNC
 #include "apr_want.h"
 
-extern apr_thread_mutex_t* lua_ivm_mutex;
+extern apr_global_mutex_t* lua_ivm_mutex;
+extern apr_shm_t *lua_ivm_shm;
 
 APLOG_USE_MODULE(lua);
 #define POST_MAX_VARS 500
@@ -298,6 +299,8 @@ static apr_status_t lua_write_body(reque
                                      &written);
             if (written != rsize || rc != OK)
                 return APR_ENOSPC;
+            if (rc != APR_SUCCESS)
+                return rc;
             rpos += rsize;
         }
     }
@@ -659,45 +662,45 @@ static int req_assbackwards_field(reques
     return r->assbackwards;
 }
 
-static req_table_t* req_headers_in(request_rec *r)
+static req_table_t *req_headers_in(request_rec *r)
 {
-  req_table_t* t = apr_palloc(r->pool, sizeof(req_table_t));
+  req_table_t *t = apr_palloc(r->pool, sizeof(req_table_t));
   t->r = r;
   t->t = r->headers_in;
   t->n = "headers_in";
   return t;
 }
 
-static req_table_t* req_headers_out(request_rec *r)
+static req_table_t *req_headers_out(request_rec *r)
 {
-  req_table_t* t = apr_palloc(r->pool, sizeof(req_table_t));
+  req_table_t *t = apr_palloc(r->pool, sizeof(req_table_t));
   t->r = r;
   t->t = r->headers_out;
   t->n = "headers_out";
   return t;
 }
 
-static req_table_t* req_err_headers_out(request_rec *r)
+static req_table_t *req_err_headers_out(request_rec *r)
 {
-  req_table_t* t = apr_palloc(r->pool, sizeof(req_table_t));
+  req_table_t *t = apr_palloc(r->pool, sizeof(req_table_t));
   t->r = r;
   t->t = r->err_headers_out;
   t->n = "err_headers_out";
   return t;
 }
 
-static req_table_t* req_subprocess_env(request_rec *r)
+static req_table_t *req_subprocess_env(request_rec *r)
 {
-  req_table_t* t = apr_palloc(r->pool, sizeof(req_table_t));
+  req_table_t *t = apr_palloc(r->pool, sizeof(req_table_t));
   t->r = r;
   t->t = r->subprocess_env;
   t->n = "subprocess_env";
   return t;
 }
 
-static req_table_t* req_notes(request_rec *r)
+static req_table_t *req_notes(request_rec *r)
 {
-  req_table_t* t = apr_palloc(r->pool, sizeof(req_table_t));
+  req_table_t *t = apr_palloc(r->pool, sizeof(req_table_t));
   t->r = r;
   t->t = r->notes;
   t->n = "notes";
@@ -1928,21 +1931,23 @@ static int req_debug(lua_State *L)
 static int lua_ivm_get(lua_State *L) 
 {
     const char *key, *raw_key;
+    apr_pool_t *pool;
     lua_ivm_object *object = NULL;
     request_rec *r = ap_lua_check_request_rec(L, 1);
     key = luaL_checkstring(L, 2);
     raw_key = apr_pstrcat(r->pool, "lua_ivm_", key, NULL);
-    apr_thread_mutex_lock(lua_ivm_mutex);
-    apr_pool_userdata_get((void **)&object, raw_key, r->server->process->pool);
+    apr_global_mutex_lock(lua_ivm_mutex);
+    pool = *((apr_pool_t**) apr_shm_baseaddr_get(lua_ivm_shm));
+    apr_pool_userdata_get((void **)&object, raw_key, pool);
     if (object) {
         if (object->type == LUA_TBOOLEAN) lua_pushboolean(L, (int) object->number);
         else if (object->type == LUA_TNUMBER) lua_pushnumber(L, object->number);
         else if (object->type == LUA_TSTRING) lua_pushlstring(L, object->vb.buf, object->size);
-        apr_thread_mutex_unlock(lua_ivm_mutex);
+        apr_global_mutex_unlock(lua_ivm_mutex);
         return 1;
     }
     else {
-        apr_thread_mutex_unlock(lua_ivm_mutex);
+        apr_global_mutex_unlock(lua_ivm_mutex);
         return 0;
     }
 }
@@ -1952,6 +1957,7 @@ static int lua_ivm_set(lua_State *L) 
 {
     const char *key, *raw_key;
     const char *value = NULL;
+    apr_pool_t *pool;
     size_t str_len;
     lua_ivm_object *object = NULL;
     request_rec *r = ap_lua_check_request_rec(L, 1);
@@ -1959,11 +1965,12 @@ static int lua_ivm_set(lua_State *L) 
     luaL_checkany(L, 3);
     raw_key = apr_pstrcat(r->pool, "lua_ivm_", key, NULL);
     
-    apr_thread_mutex_lock(lua_ivm_mutex);
-    apr_pool_userdata_get((void **)&object, raw_key, r->server->process->pool);
+    apr_global_mutex_lock(lua_ivm_mutex);
+    pool = *((apr_pool_t**) apr_shm_baseaddr_get(lua_ivm_shm));
+    apr_pool_userdata_get((void **)&object, raw_key, pool);
     if (!object) {
-        object = apr_pcalloc(r->server->process->pool, sizeof(lua_ivm_object));
-        ap_varbuf_init(r->server->process->pool, &object->vb, 2);
+        object = apr_pcalloc(pool, sizeof(lua_ivm_object));
+        ap_varbuf_init(pool, &object->vb, 2);
         object->size = 1;
         object->vb_size = 1;
     }
@@ -1981,8 +1988,8 @@ static int lua_ivm_set(lua_State *L) 
         memset(object->vb.buf, 0, str_len);
         memcpy(object->vb.buf, value, str_len-1);
     }
-    apr_pool_userdata_set(object, raw_key, NULL, r->server->process->pool);
-    apr_thread_mutex_unlock(lua_ivm_mutex);
+    apr_pool_userdata_set(object, raw_key, NULL, pool);
+    apr_global_mutex_unlock(lua_ivm_mutex);
     return 0;
 }
 
@@ -2087,7 +2094,7 @@ static int lua_set_cookie(lua_State *L) 
         /* Domain does NOT like quotes in most browsers, so let's avoid that */
         strdomain = apr_psprintf(r->pool, "Domain=%s;", domain);
     }
-
+    
     /* URL-encode key/value */
     value = ap_escape_urlencoded(r->pool, value);
     key = ap_escape_urlencoded(r->pool, key);
@@ -2111,7 +2118,7 @@ static apr_uint64_t ap_ntoh64(const apr_
     if (APR_IS_BIGENDIAN) {
         return *input;
     }
-
+    
     data[0] = *input >> 56;
     data[1] = *input >> 48;
     data[2] = *input >> 40;
@@ -2226,7 +2233,7 @@ static int lua_websocket_read(lua_State 
     int plaintext;
     
     
-    request_rec *r = (request_rec *) lua_unboxpointer(L, 1);
+    request_rec *r = ap_lua_check_request_rec(L, 1);
     plaintext = ap_lua_ssl_is_https(r->connection) ? 0 : 1;
 
     
@@ -2374,7 +2381,7 @@ static int lua_websocket_write(lua_State
     size_t len;
     int raw = 0;
     char prelude;
-    request_rec *r = (request_rec *) lua_unboxpointer(L, 1);
+    request_rec *r = ap_lua_check_request_rec(L, 1);
     
     if (lua_isboolean(L, 3)) {
         raw = lua_toboolean(L, 3);
@@ -2423,7 +2430,7 @@ static int lua_websocket_close(lua_State
 {
     apr_socket_t *sock;
     char prelude[2];
-    request_rec *r = (request_rec *) lua_unboxpointer(L, 1);
+    request_rec *r = ap_lua_check_request_rec(L, 1);
     
     sock = ap_get_conn_socket(r->connection);
     
@@ -2436,11 +2443,9 @@ static int lua_websocket_close(lua_State
     apr_socket_close(sock);
     r->output_filters = NULL;
     r->connection->keepalive = AP_CONN_CLOSE;
-    ap_destroy_sub_req(r);
-    return DONE;
+    return 0;
 }
 
-
 static int lua_websocket_ping(lua_State *L) 
 {
     apr_socket_t *sock;
@@ -2860,8 +2865,6 @@ void ap_lua_load_request_lmodule(lua_Sta
     apr_hash_set(dispatch, "wsping", APR_HASH_KEY_STRING,
                  makefun(&lua_websocket_ping, APL_REQ_FUNTYPE_LUACFUN, p));
     
-
-
     lua_pushlightuserdata(L, dispatch);
     lua_setfield(L, LUA_REGISTRYINDEX, "Apache2.Request.dispatch");
 

Modified: httpd/httpd/branches/2.4.x/modules/lua/mod_lua.c
URL: http://svn.apache.org/viewvc/httpd/httpd/branches/2.4.x/modules/lua/mod_lua.c?rev=1604339&r1=1604338&r2=1604339&view=diff
==============================================================================
--- httpd/httpd/branches/2.4.x/modules/lua/mod_lua.c (original)
+++ httpd/httpd/branches/2.4.x/modules/lua/mod_lua.c Sat Jun 21 11:19:40 2014
@@ -20,17 +20,32 @@
 #include <stdlib.h>
 #include <ctype.h>
 #include <apr_thread_mutex.h>
-
+#include <apr_pools.h>
 #include "lua_apr.h"
 #include "lua_config.h"
 #include "apr_optional.h"
 #include "mod_ssl.h"
 #include "mod_auth.h"
+#include "util_mutex.h"
+
 
 #ifdef APR_HAS_THREADS
 #include "apr_thread_proc.h"
 #endif
 
+/* getpid for *NIX */
+#if APR_HAVE_SYS_TYPES_H
+#include <sys/types.h>
+#endif
+#if APR_HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+
+/* getpid for Windows */
+#if APR_HAVE_PROCESS_H
+#include <process.h>
+#endif
+
 APR_IMPLEMENT_OPTIONAL_HOOK_RUN_ALL(ap_lua, AP_LUA, int, lua_open,
                                     (lua_State *L, apr_pool_t *p),
                                     (L, p), OK, DECLINED)
@@ -64,7 +79,16 @@ typedef struct
     int broken;
 } lua_filter_ctx;
 
-apr_thread_mutex_t* lua_ivm_mutex = NULL;
+apr_global_mutex_t *lua_ivm_mutex;
+apr_shm_t *lua_ivm_shm;
+char *lua_ivm_shmfile;
+
+static apr_status_t shm_cleanup_wrapper(void *unused) {
+    if (lua_ivm_shm) {
+        return apr_shm_destroy(lua_ivm_shm);
+    }
+    return OK;
+}
 
 /**
  * error reporting if lua has an error.
@@ -282,7 +306,7 @@ static int lua_handler(request_rec *r)
         lua_getglobal(L, "handle");
         if (!lua_isfunction(L, -1)) {
             ap_log_rerror(APLOG_MARK, APLOG_CRIT, 0, r, APLOGNO(01475)
-                          "lua: Unable to find function %s in %s",
+                          "lua: Unable to find entry function '%s' in %s (not a valid function)",
                           "handle",
                           spec->file);
             ap_lua_release_state(L, spec, r);
@@ -352,7 +376,7 @@ static apr_status_t lua_setup_filter_ctx
                 lua_getglobal(L, hook_spec->function_name);
                 if (!lua_isfunction(L, -1)) {
                     ap_log_rerror(APLOG_MARK, APLOG_CRIT, 0, r, APLOGNO(02329)
-                                    "lua: Unable to find function %s in %s",
+                                "lua: Unable to find entry function '%s' in %s (not a valid
function)",
                                     hook_spec->function_name,
                                     hook_spec->file_name);
                     ap_lua_release_state(L, spec, r);
@@ -476,6 +500,9 @@ static apr_status_t lua_output_filter_ha
                 ap_remove_output_filter(f);
                 apr_brigade_cleanup(pbbIn);
                 apr_brigade_cleanup(ctx->tmpBucket);
+                ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
+                                    "lua: Error while executing filter: %s",
+                        lua_tostring(L, -1));
                 return HTTP_INTERNAL_SERVER_ERROR;
             }
         }
@@ -656,7 +683,7 @@ static int lua_request_rec_hook_harness(
 
             if (!L) {
                 ap_log_rerror(APLOG_MARK, APLOG_CRIT, 0, r, APLOGNO(01477)
-                              "lua: Failed to obtain lua interpreter for %s %s",
+                    "lua: Failed to obtain lua interpreter for entry function '%s' in %s",
                               hook_spec->function_name, hook_spec->file_name);
                 return HTTP_INTERNAL_SERVER_ERROR;
             }
@@ -665,7 +692,7 @@ static int lua_request_rec_hook_harness(
                 lua_getglobal(L, hook_spec->function_name);
                 if (!lua_isfunction(L, -1)) {
                     ap_log_rerror(APLOG_MARK, APLOG_CRIT, 0, r, APLOGNO(01478)
-                                  "lua: Unable to find function %s in %s",
+                               "lua: Unable to find entry function '%s' in %s (not a valid
function)",
                                   hook_spec->function_name,
                                   hook_spec->file_name);
                     ap_lua_release_state(L, spec, r);
@@ -709,6 +736,8 @@ static int lua_request_rec_hook_harness(
     return DECLINED;
 }
 
+
+/* Fix for making sure that LuaMapHandler works when FallbackResource is set */
 static int lua_map_handler_fixups(request_rec *r)
 {
     /* If there is no handler set yet, this might be a LuaMapHandler request */
@@ -733,6 +762,7 @@ static int lua_map_handler_fixups(reques
     return DECLINED;
 }
 
+
 static int lua_map_handler(request_rec *r)
 {
     int rc, n = 0;
@@ -773,7 +803,7 @@ static int lua_map_handler(request_rec *
 
             if (!L) {
                 ap_log_rerror(APLOG_MARK, APLOG_CRIT, 0, r, APLOGNO(02330)
-                                "lua: Failed to obtain lua interpreter for %s %s",
+                                "lua: Failed to obtain Lua interpreter for entry function
'%s' in %s",
                                 function_name, filename);
                 ap_lua_release_state(L, spec, r);
                 return HTTP_INTERNAL_SERVER_ERROR;
@@ -783,7 +813,7 @@ static int lua_map_handler(request_rec *
                 lua_getglobal(L, function_name);
                 if (!lua_isfunction(L, -1)) {
                     ap_log_rerror(APLOG_MARK, APLOG_CRIT, 0, r, APLOGNO(02331)
-                                    "lua: Unable to find function %s in %s",
+                                    "lua: Unable to find entry function '%s' in %s (not a
valid function)",
                                     function_name,
                                     filename);
                     ap_lua_release_state(L, spec, r);
@@ -1704,7 +1734,7 @@ static authz_status lua_authz_check(requ
     lua_getglobal(L, prov_spec->function_name);
     if (!lua_isfunction(L, -1)) {
         ap_log_rerror(APLOG_MARK, APLOG_CRIT, 0, r, APLOGNO(02319)
-                      "Unable to find function %s in %s",
+                      "Unable to find entry function '%s' in %s (not a valid function)",
                       prov_spec->function_name, prov_spec->file_name);
         ap_lua_release_state(L, spec, r);
         return AUTHZ_GENERAL_ERROR;
@@ -1939,11 +1969,54 @@ static int lua_request_hook(lua_State *L
     return OK;
 }
 
+static int lua_pre_config(apr_pool_t *pconf, apr_pool_t *plog,
+                            apr_pool_t *ptemp)
+{
+    ap_mutex_register(pconf, "lua-ivm-shm", NULL, APR_LOCK_DEFAULT, 0);
+    return OK;
+}
+
 static int lua_post_config(apr_pool_t *pconf, apr_pool_t *plog,
                              apr_pool_t *ptemp, server_rec *s)
 {
+    apr_pool_t **pool;
+    const char *tempdir;
+    apr_status_t rs;
+
     lua_ssl_val = APR_RETRIEVE_OPTIONAL_FN(ssl_var_lookup);
     lua_ssl_is_https = APR_RETRIEVE_OPTIONAL_FN(ssl_is_https);
+    
+    if (ap_state_query(AP_SQ_MAIN_STATE) == AP_SQ_MS_CREATE_PRE_CONFIG)
+        return OK;
+
+    /* Create ivm mutex */
+    rs = ap_global_mutex_create(&lua_ivm_mutex, NULL, "lua-ivm-shm", NULL,
+                            s, pconf, 0);
+    if (APR_SUCCESS != rs) {
+        return HTTP_INTERNAL_SERVER_ERROR;
+    }
+
+    /* Create shared memory space */
+    rs = apr_temp_dir_get(&tempdir, pconf);
+    if (rs != APR_SUCCESS) {
+        ap_log_error(APLOG_MARK, APLOG_ERR, rs, s,
+                 "mod_lua IVM: Failed to find temporary directory");
+        return HTTP_INTERNAL_SERVER_ERROR;
+    }
+    lua_ivm_shmfile = apr_psprintf(pconf, "%s/httpd_lua_shm.%ld", tempdir,
+                           (long int)getpid());
+    rs = apr_shm_create(&lua_ivm_shm, sizeof(apr_pool_t**),
+                    (const char *) lua_ivm_shmfile, pconf);
+    if (rs != APR_SUCCESS) {
+        ap_log_error(APLOG_MARK, APLOG_ERR, rs, s,
+            "mod_lua: Failed to create shared memory segment on file %s",
+                     lua_ivm_shmfile);
+        return HTTP_INTERNAL_SERVER_ERROR;
+    }
+    pool = (apr_pool_t **)apr_shm_baseaddr_get(lua_ivm_shm);
+    apr_pool_create(pool, pconf);
+    apr_pool_cleanup_register(pconf, NULL, shm_cleanup_wrapper,
+                          apr_pool_cleanup_null);
     return OK;
 }
 static void *overlay_hook_specs(apr_pool_t *p,
@@ -2022,9 +2095,10 @@ static void lua_register_hooks(apr_pool_
 
     ap_hook_check_user_id(lua_check_user_id_harness_first, NULL, NULL,
                           AP_LUA_HOOK_FIRST);
+
     ap_hook_check_user_id(lua_check_user_id_harness, NULL, NULL,
                            APR_HOOK_MIDDLE);
-    ap_hook_check_user_id(lua_check_user_id_harness_last, NULL, NULL,
+   ap_hook_check_user_id(lua_check_user_id_harness_last, NULL, NULL,
                           AP_LUA_HOOK_LAST);
 
     ap_hook_type_checker(lua_type_checker_harness, NULL, NULL,
@@ -2048,6 +2122,7 @@ static void lua_register_hooks(apr_pool_
     ap_hook_quick_handler(lua_quick_harness, NULL, NULL, APR_HOOK_FIRST);
 
     ap_hook_post_config(lua_post_config, NULL, NULL, APR_HOOK_MIDDLE);
+    ap_hook_pre_config(lua_pre_config, NULL, NULL, APR_HOOK_MIDDLE);
 
     APR_OPTIONAL_HOOK(ap_lua, lua_open, lua_open_hook, NULL, NULL,
                       APR_HOOK_REALLY_FIRST);
@@ -2055,6 +2130,7 @@ static void lua_register_hooks(apr_pool_
     APR_OPTIONAL_HOOK(ap_lua, lua_request, lua_request_hook, NULL, NULL,
                       APR_HOOK_REALLY_FIRST);
     ap_hook_handler(lua_map_handler, NULL, NULL, AP_LUA_HOOK_FIRST);
+    
     /* Hook this right before FallbackResource kicks in */
     ap_hook_fixups(lua_map_handler_fixups, NULL, NULL, AP_LUA_HOOK_LAST-2);
 #if APR_HAS_THREADS
@@ -2063,9 +2139,6 @@ static void lua_register_hooks(apr_pool_
     /* providers */
     lua_authz_providers = apr_hash_make(p);
     
-    /* ivm mutex */
-    apr_thread_mutex_create(&lua_ivm_mutex, APR_THREAD_MUTEX_DEFAULT, p);
-    
     /* Logging catcher */
     ap_hook_log_transaction(lua_log_transaction_harness,NULL,NULL,
                             APR_HOOK_FIRST);



Mime
View raw message