changeset 370:9a242235a80a NGINX_0_6_29

nginx 0.6.29 *) Feature: the ngx_google_perftools_module. *) Bugfix: the ngx_http_perl_module could be not built on 64-bit platforms; bug appeared in 0.6.27.
author Igor Sysoev <http://sysoev.ru>
date Tue, 18 Mar 2008 00:00:00 +0300
parents 5906b8639a07
children b6a2a305fdad
files CHANGES CHANGES.ru auto/lib/conf auto/lib/google-perftools/conf auto/make auto/modules auto/options auto/sources src/core/nginx.h src/core/ngx_open_file_cache.c src/event/modules/ngx_eventport_module.c src/event/modules/ngx_rtsig_module.c src/event/ngx_event_openssl.c src/http/modules/ngx_http_charset_filter_module.c src/http/modules/ngx_http_fastcgi_module.c src/http/modules/perl/nginx.pm src/http/modules/perl/ngx_http_perl_module.c src/http/ngx_http_parse.c src/http/ngx_http_upstream.c src/misc/ngx_google_perftools_module.c
diffstat 20 files changed, 321 insertions(+), 108 deletions(-) [+]
line wrap: on
line diff
--- a/CHANGES
+++ b/CHANGES
@@ -1,4 +1,12 @@
 
+Changes with nginx 0.6.29                                        18 Mar 2008
+
+    *) Feature: the ngx_google_perftools_module.
+
+    *) Bugfix: the ngx_http_perl_module could be not built on 64-bit 
+       platforms; bug appeared in 0.6.27.
+
+
 Changes with nginx 0.6.28                                        13 Mar 2008
 
     *) Bugfix: the rtsig method could be not built; bug appeared in 0.6.27.
--- a/CHANGES.ru
+++ b/CHANGES.ru
@@ -1,4 +1,12 @@
 
+Изменения в nginx 0.6.29                                          18.03.2008
+
+    *) Добавление: модуль ngx_google_perftools_module.
+
+    *) Исправление: модуль ngx_http_perl_module не собирался на 64-битных 
+       платформах; ошибка появилась в 0.6.27.
+
+
 Изменения в nginx 0.6.28                                          13.03.2008
 
     *) Исправление: метод rtsig не собирался; ошибка появилась в 0.6.27.
--- a/auto/lib/conf
+++ b/auto/lib/conf
@@ -42,3 +42,7 @@ fi
 if [ $USE_PERL = YES ]; then
     . auto/lib/perl/conf
 fi
+
+if [ $NGX_GOOGLE_PERFTOOLS = YES ]; then
+    . auto/lib/google-perftools/conf
+fi
new file mode 100644
--- /dev/null
+++ b/auto/lib/google-perftools/conf
@@ -0,0 +1,33 @@
+
+# Copyright (C) Igor Sysoev
+
+
+    ngx_feature="Google perftools"
+    ngx_feature_name=
+    ngx_feature_run=no
+    ngx_feature_incs=
+    ngx_feature_path=
+    ngx_feature_libs="-lprofiler"
+    ngx_feature_test="ProfilerStop()"
+    . auto/feature
+
+
+if [ $ngx_found = no ]; then
+
+    # FreeBSD port
+
+    ngx_feature="Google perftools in /usr/local/"
+
+    if [ $NGX_RPATH = YES ]; then
+        ngx_feature_libs="-R/usr/local/lib -L/usr/local/lib -lprofiler"
+    else
+        ngx_feature_libs="-L/usr/local/lib -lprofiler"
+    fi
+
+    . auto/feature
+fi
+
+
+if [ $ngx_found = yes ]; then
+    CORE_LIBS="$CORE_LIBS $ngx_feature_libs"
+fi
--- a/auto/make
+++ b/auto/make
@@ -6,7 +6,8 @@ mkdir -p $NGX_OBJS/src/core $NGX_OBJS/sr
          $NGX_OBJS/src/os/unix $NGX_OBJS/src/os/win32 \
          $NGX_OBJS/src/http $NGX_OBJS/src/http/modules \
 	 $NGX_OBJS/src/http/modules/perl \
-         $NGX_OBJS/src/mail
+         $NGX_OBJS/src/mail \
+         $NGX_OBJS/src/misc
 
 
 ngx_objs_dir=$NGX_OBJS$ngx_regex_dirsep
@@ -127,6 +128,9 @@ END
 fi
 
 
+ngx_all_srcs="$ngx_all_srcs $NGX_MISC_SRCS"
+
+
 if test -n "$NGX_ADDON_SRCS"; then
 
 cat << END                                                >> $NGX_MAKEFILE
@@ -309,6 +313,32 @@ END
 fi
 
 
+# the misc sources
+
+if test -n "$NGX_MISC_SRCS"; then
+
+    ngx_cc="\$(CC) $ngx_compile_opt \$(CFLAGS) $ngx_use_pch \$(ALL_INCS)"
+
+    for ngx_src in $NGX_MISC_SRCS
+    do
+        ngx_src=`echo $ngx_src | sed -e "s/\//$ngx_regex_dirsep/g"`
+        ngx_obj=`echo $ngx_src \
+            | sed -e "s#^\(.*\.\)cpp\\$#$ngx_objs_dir\1$ngx_objext#g" \
+                  -e "s#^\(.*\.\)cc\\$#$ngx_objs_dir\1$ngx_objext#g" \
+                  -e "s#^\(.*\.\)c\\$#$ngx_objs_dir\1$ngx_objext#g" \
+                  -e "s#^\(.*\.\)S\\$#$ngx_objs_dir\1$ngx_objext#g"`
+
+        cat << END                                            >> $NGX_MAKEFILE
+
+$ngx_obj:	\$(CORE_DEPS) $ngx_cont$ngx_src
+	$ngx_cc$ngx_tab$ngx_objout$ngx_obj$ngx_tab$ngx_src$NGX_AUX
+
+END
+     done
+
+fi
+
+
 # the addons sources
 
 if test -n "$NGX_ADDON_SRCS"; then
--- a/auto/modules
+++ b/auto/modules
@@ -375,6 +375,12 @@ if [ $MAIL = YES ]; then
 fi
 
 
+if [ $NGX_GOOGLE_PERFTOOLS = YES ]; then
+    modules="$modules $NGX_GOOGLE_PERFTOOLS_MODULE"
+    NGX_MISC_SRCS="$NGX_MISC_SRCS $NGX_GOOGLE_PERFTOOLS_SRCS"
+fi
+
+
 cat << END                                    > $NGX_MODULES_C
 
 #include <ngx_config.h>
--- a/auto/options
+++ b/auto/options
@@ -115,6 +115,8 @@ ZLIB_ASM=NO
 USE_PERL=NO
 NGX_PERL=perl
 
+NGX_GOOGLE_PERFTOOLS=NO
+
 NGX_CPU_CACHE_LINE=
 
 
@@ -201,6 +203,8 @@ do
         --without-mail_imap_module)      MAIL_IMAP=NO               ;;
         --without-mail_smtp_module)      MAIL_SMTP=NO               ;;
 
+        --with-google_perftools_module)  NGX_GOOGLE_PERFTOOLS=YES   ;;
+
         --add-module=*)                  NGX_ADDONS="$NGX_ADDONS $value" ;;
 
         --with-cc=*)                     CC="$value"                ;;
@@ -317,6 +321,8 @@ cat << END
   --without-mail_imap_module         disable ngx_mail_imap_module
   --without-mail_smtp_module         disable ngx_mail_smtp_module
 
+  --with-google_perftools_module     enable ngx_google_perftools_module
+
   --add-module=PATH                  enable an external module
 
   --with-cc=PATH                     set path to C compiler
--- a/auto/sources
+++ b/auto/sources
@@ -460,3 +460,7 @@ MAIL_AUTH_HTTP_SRCS="src/mail/ngx_mail_a
 
 MAIL_PROXY_MODULE="ngx_mail_proxy_module"
 MAIL_PROXY_SRCS="src/mail/ngx_mail_proxy_module.c"
+
+NGX_GOOGLE_PERFTOOLS_MODULE=ngx_google_perftools_module
+NGX_GOOGLE_PERFTOOLS_SRCS=src/misc/ngx_google_perftools_module.c
+
--- a/src/core/nginx.h
+++ b/src/core/nginx.h
@@ -8,7 +8,7 @@
 #define _NGINX_H_INCLUDED_
 
 
-#define NGINX_VERSION      "0.6.28"
+#define NGINX_VERSION      "0.6.29"
 #define NGINX_VER          "nginx/" NGINX_VERSION
 
 #define NGINX_VAR          "NGINX"
--- a/src/core/ngx_open_file_cache.c
+++ b/src/core/ngx_open_file_cache.c
@@ -308,8 +308,6 @@ ngx_open_cached_file(ngx_open_file_cache
 
     /* not found */
 
-    file = NULL;
-
     rc = ngx_open_and_stat_file(name->data, of, pool->log);
 
     if (rc != NGX_OK && (of->err == 0 || !of->errors)) {
@@ -413,9 +411,11 @@ failed:
 
         cache->current--;
 
-        if (ngx_close_file(file->fd) == NGX_FILE_ERROR) {
-            ngx_log_error(NGX_LOG_ALERT, pool->log, ngx_errno,
-                          ngx_close_file_n " \"%s\" failed", file->name);
+        if (file->fd != NGX_INVALID_FILE) {
+            if (ngx_close_file(file->fd) == NGX_FILE_ERROR) {
+                ngx_log_error(NGX_LOG_ALERT, pool->log, ngx_errno,
+                              ngx_close_file_n " \"%s\" failed", file->name);
+            }
         }
 
         ngx_free(file->name);
--- a/src/event/modules/ngx_eventport_module.c
+++ b/src/event/modules/ngx_eventport_module.c
@@ -40,6 +40,15 @@ typedef struct  port_notify {
     void       *portnfy_user;   /* user defined */
 } port_notify_t;
 
+#if (__FreeBSD_version < 700005)
+
+typedef struct itimerspec {     /* definition per POSIX.4 */
+    struct timespec it_interval;/* timer period */
+    struct timespec it_value;   /* timer expiration */
+} itimerspec_t;
+
+#endif
+
 int port_create(void)
 {
     return -1;
--- a/src/event/modules/ngx_rtsig_module.c
+++ b/src/event/modules/ngx_rtsig_module.c
@@ -11,8 +11,14 @@
 
 #if (NGX_TEST_BUILD_RTSIG)
 
+#ifdef  SIGRTMIN
+#define si_fd          _reason.__spare__.__spare2__[0]
+#else
+#define SIGRTMIN       33
+#define si_fd          __spare__[0]
+#endif
+
 #define F_SETSIG       10
-#define si_fd          _reason.__spare__.__spare2__[0]
 #define KERN_RTSIGNR   30
 #define KERN_RTSIGMAX  31
 
--- a/src/event/ngx_event_openssl.c
+++ b/src/event/ngx_event_openssl.c
@@ -1155,11 +1155,21 @@ ngx_ssl_connection_error(ngx_connection_
         n = ERR_GET_REASON(ERR_peek_error());
 
             /* handshake failures */
-        if (n == SSL_R_NO_SHARED_CIPHER
+        if (n == SSL_R_DIGEST_CHECK_FAILED
+            || n == SSL_R_NO_SHARED_CIPHER
             || n == SSL_R_UNEXPECTED_MESSAGE
             || n == SSL_R_WRONG_VERSION_NUMBER
+            || n == SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC
             || n == 1000 /* SSL_R_SSLV3_ALERT_CLOSE_NOTIFY */
+            || n == SSL_R_SSLV3_ALERT_UNEXPECTED_MESSAGE
+            || n == SSL_R_SSLV3_ALERT_BAD_RECORD_MAC
+            || n == SSL_R_SSLV3_ALERT_DECOMPRESSION_FAILURE
+            || n == SSL_R_SSLV3_ALERT_HANDSHAKE_FAILURE
+            || n == SSL_R_SSLV3_ALERT_BAD_CERTIFICATE
+            || n == SSL_R_SSLV3_ALERT_UNSUPPORTED_CERTIFICATE
+            || n == SSL_R_SSLV3_ALERT_CERTIFICATE_REVOKED
             || n == SSL_R_SSLV3_ALERT_CERTIFICATE_EXPIRED
+            || n == SSL_R_SSLV3_ALERT_CERTIFICATE_UNKNOWN
             || n == SSL_R_SSLV3_ALERT_ILLEGAL_PARAMETER
             || n == SSL_R_TLSV1_ALERT_UNKNOWN_CA)
         {
--- a/src/http/modules/ngx_http_charset_filter_module.c
+++ b/src/http/modules/ngx_http_charset_filter_module.c
@@ -561,25 +561,33 @@ ngx_http_charset_body_filter(ngx_http_re
 static ngx_uint_t
 ngx_http_charset_recode(ngx_buf_t *b, u_char *table)
 {
-    u_char  *p;
-
-    for (p = b->pos; p < b->last; p++) {
+    u_char  *p, *last;
 
-        if (*p == table[*p]) {
-            continue;
-        }
+    last = b->last;
 
-        while (p < b->last) {
-            *p = table[*p];
-            p++;
+    for (p = b->pos; p < last; p++) {
+
+        if (*p != table[*p]) {
+            goto recode;
         }
-
-        b->in_file = 0;
-
-        return 1;
     }
 
     return 0;
+
+recode:
+
+    do {
+        if (*p != table[*p]) {
+            *p = table[*p];
+        }
+
+        p++;
+
+    } while (p < last);
+
+    b->in_file = 0;
+
+    return 1;
 }
 
 
--- a/src/http/modules/ngx_http_fastcgi_module.c
+++ b/src/http/modules/ngx_http_fastcgi_module.c
@@ -639,6 +639,11 @@ ngx_http_fastcgi_create_request(ngx_http
                 code((ngx_http_script_engine_t *) &e);
             }
             e.ip += sizeof(uintptr_t);
+
+            ngx_log_debug4(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
+                           "fastcgi param: \"%*s: %*s\"",
+                           key_len, e.pos - (key_len + val_len),
+                           val_len, e.pos - val_len);
         }
 
         b->last = e.pos;
--- a/src/http/modules/perl/nginx.pm
+++ b/src/http/modules/perl/nginx.pm
@@ -47,7 +47,7 @@ our @EXPORT = qw(
     HTTP_INSUFFICIENT_STORAGE
 );
 
-our $VERSION = '0.6.28';
+our $VERSION = '0.6.29';
 
 require XSLoader;
 XSLoader::load('nginx', $VERSION);
--- a/src/http/modules/perl/ngx_http_perl_module.c
+++ b/src/http/modules/perl/ngx_http_perl_module.c
@@ -668,7 +668,7 @@ ngx_http_perl_call_handler(pTHX_ ngx_htt
     XPUSHs(sv);
 
     if (args) {
-        EXTEND(sp, (int) args[0]);
+        EXTEND(sp, (intptr_t) args[0]);
 
         for (i = 1; i <= (ngx_uint_t) args[0]; i++) {
             PUSHs(sv_2mortal(args[i]));
@@ -1055,8 +1055,20 @@ ngx_http_perl_init_worker(ngx_cycle_t *c
     return NGX_OK;
 }
 
+
 static void
 ngx_http_perl_exit(ngx_cycle_t *cycle)
 {
+    ngx_http_perl_main_conf_t  *pmcf;
+
+    pmcf = ngx_http_cycle_get_module_main_conf(cycle, ngx_http_perl_module);
+
+    {
+
+    dTHXa(pmcf->perl);
+    PERL_SET_CONTEXT(pmcf->perl);
+
     PERL_SYS_TERM();
+
+    }
 }
--- a/src/http/ngx_http_parse.c
+++ b/src/http/ngx_http_parse.c
@@ -775,7 +775,7 @@ ngx_http_parse_header_line(ngx_http_requ
             if (c) {
                 hash = ngx_hash(hash, c);
                 r->lowcase_header[i++] = c;
-                i &= ~NGX_HTTP_LC_HEADER_LEN;
+                i &= (NGX_HTTP_LC_HEADER_LEN - 1);
                 break;
             }
 
@@ -882,10 +882,10 @@ ngx_http_parse_header_line(ngx_http_requ
         /* end of header line */
         case sw_almost_done:
             switch (ch) {
+            case LF:
+                goto done;
             case CR:
                 break;
-            case LF:
-                goto done;
             default:
                 return NGX_HTTP_PARSE_INVALID_HEADER;
             }
--- a/src/http/ngx_http_upstream.c
+++ b/src/http/ngx_http_upstream.c
@@ -2125,13 +2125,11 @@ ngx_http_upstream_process_body(ngx_event
 static void
 ngx_http_upstream_store(ngx_http_request_t *r, ngx_http_upstream_t *u)
 {
-    char             *failed;
-    u_char           *name;
-    size_t            root;
-    time_t            lm;
-    ngx_err_t         err;
-    ngx_str_t        *temp, path, *last_modified;
-    ngx_temp_file_t  *tf;
+    size_t                  root;
+    time_t                  lm;
+    ngx_str_t               path;
+    ngx_temp_file_t        *tf;
+    ngx_ext_rename_file_t   ext;
 
     tf = u->pipe->temp_file;
 
@@ -2160,36 +2158,20 @@ ngx_http_upstream_store(ngx_http_request
         u->pipe->temp_file = tf;
     }
 
-    temp = &tf->file.name;
-
-#if !(NGX_WIN32)
-
-    if (ngx_change_file_access(temp->data, u->conf->store_access)
-        == NGX_FILE_ERROR)
-    {
-        err = ngx_errno;
-        failed = ngx_change_file_access_n;
-        name = temp->data;
-
-        goto failed;
-    }
-
-#endif
+    ext.access = u->conf->store_access;
+    ext.time = -1;
+    ext.create_path = 1;
+    ext.delete_file = 1;
+    ext.log = r->connection->log;
 
     if (u->headers_in.last_modified) {
 
-        last_modified = &u->headers_in.last_modified->value;
-
-        lm = ngx_http_parse_time(last_modified->data, last_modified->len);
+        lm = ngx_http_parse_time(u->headers_in.last_modified->value.data,
+                                 u->headers_in.last_modified->value.len);
 
         if (lm != NGX_ERROR) {
-            if (ngx_set_file_time(temp->data, tf->file.fd, lm) != NGX_OK) {
-                err = ngx_errno;
-                failed = ngx_set_file_time_n;
-                name = temp->data;
-
-                goto failed;
-            }
+            ext.time = lm;
+            ext.fd = tf->file.fd;
         }
     }
 
@@ -2207,55 +2189,10 @@ ngx_http_upstream_store(ngx_http_request
     }
 
     ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
-                   "upstream stores \"%s\" to \"%s\"", temp->data, path.data);
-
-    failed = ngx_rename_file_n;
-    name = path.data;
-
-    if (ngx_rename_file(temp->data, path.data) != NGX_FILE_ERROR) {
-        return;
-    }
-
-    err = ngx_errno;
-
-    if (err == NGX_ENOENT) {
-
-        err = ngx_create_full_path(path.data,
-                                   ngx_dir_access(u->conf->store_access));
-        if (err == 0) {
-            if (ngx_rename_file(temp->data, path.data) != NGX_FILE_ERROR) {
-                return;
-            }
-
-            err = ngx_errno;
-        }
-    }
-
-#if (NGX_WIN32)
-
-    if (err == NGX_EEXIST) {
-        if (ngx_win32_rename_file(temp, &path, r->connection->log) == NGX_OK) {
-
-            if (ngx_rename_file(temp->data, path.data) != NGX_FILE_ERROR) {
-                return;
-            }
-        }
-
-        err = ngx_errno;
-    }
-
-#endif
-
-failed:
-
-    if (ngx_delete_file(temp->data) == NGX_FILE_ERROR) {
-        ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno,
-                      ngx_delete_file_n " \"%s\" failed",
-                      temp->data);
-    }
-
-    ngx_log_error(NGX_LOG_CRIT, r->connection->log, err,
-                  "%s \"%s\" failed", failed, name);
+                   "upstream stores \"%s\" to \"%s\"",
+                   tf->file.name.data, path.data);
+
+    (void) ngx_ext_rename_file(&tf->file.name, &path, &ext);
 }
 
 
new file mode 100644
--- /dev/null
+++ b/src/misc/ngx_google_perftools_module.c
@@ -0,0 +1,127 @@
+
+/*
+ * Copyright (C) Igor Sysoev
+ */
+
+
+#include <ngx_config.h>
+#include <ngx_core.h>
+
+/*
+ * declare Profiler here interface because
+ * <google/profiler.h> is C++ header file
+ */
+
+int ProfilerStart(u_char* fname);
+void ProfilerStop(void);
+void ProfilerRegisterThread(void);
+
+
+static void *ngx_google_perftools_create_conf(ngx_cycle_t *cycle);
+static ngx_int_t ngx_google_perftools_worker(ngx_cycle_t *cycle);
+
+
+typedef struct {
+    ngx_str_t  profiles;
+} ngx_google_perftools_conf_t;
+
+
+static ngx_command_t  ngx_google_perftools_commands[] = {
+
+    { ngx_string("google_perftools_profiles"),
+      NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
+      ngx_conf_set_str_slot,
+      0,
+      offsetof(ngx_google_perftools_conf_t, profiles),
+      NULL },
+
+    ngx_null_command
+};
+
+
+static ngx_core_module_t  ngx_google_perftools_module_ctx = {
+    ngx_string("google_perftools"),
+    ngx_google_perftools_create_conf,
+    NULL
+};
+
+
+ngx_module_t  ngx_google_perftools_module = {
+    NGX_MODULE_V1,
+    &ngx_google_perftools_module_ctx,      /* module context */
+    ngx_google_perftools_commands,         /* module directives */
+    NGX_CORE_MODULE,                       /* module type */
+    NULL,                                  /* init master */
+    NULL,                                  /* init module */
+    ngx_google_perftools_worker,           /* init process */
+    NULL,                                  /* init thread */
+    NULL,                                  /* exit thread */
+    NULL,                                  /* exit process */
+    NULL,                                  /* exit master */
+    NGX_MODULE_V1_PADDING
+};
+
+
+static void *
+ngx_google_perftools_create_conf(ngx_cycle_t *cycle)
+{
+    ngx_google_perftools_conf_t  *gptcf;
+
+    gptcf = ngx_pcalloc(cycle->pool, sizeof(ngx_google_perftools_conf_t));
+    if (gptcf == NULL) {
+        return NULL;
+    }
+
+    /*
+     * set by pcalloc()
+     *
+     *     gptcf->profiles = { 0, NULL };
+     */
+
+    return gptcf;
+}
+
+
+static ngx_int_t
+ngx_google_perftools_worker(ngx_cycle_t *cycle)
+{
+    u_char                       *profile;
+    ngx_google_perftools_conf_t  *gptcf;
+
+    gptcf = (ngx_google_perftools_conf_t *)
+                ngx_get_conf(cycle->conf_ctx, ngx_google_perftools_module);
+
+    if (gptcf->profiles.len == 0) {
+        return NGX_OK;
+    }
+
+    profile = ngx_alloc(gptcf->profiles.len + NGX_INT_T_LEN + 2, cycle->log);
+    if (profile == NULL) {
+        return NGX_OK;
+    }
+
+    if (getenv("CPUPROFILE")) {
+
+        /* disable inherited Profiler enabled in master process */
+        ProfilerStop();
+    }
+
+    ngx_sprintf(profile, "%V.%d%Z", &gptcf->profiles, ngx_pid);
+
+    if (ProfilerStart(profile)) {
+
+        /* start ITIMER_PROF timer */
+        ProfilerRegisterThread();
+
+    } else {
+        ngx_log_error(NGX_LOG_CRIT, cycle->log, ngx_errno,
+                      "ProfilerStart(%s) failed", profile);
+    }
+
+    ngx_free(profile);
+
+    return NGX_OK;
+}
+
+
+/* ProfilerStop() is called on Profiler destruction */