nginx-0.1.5-RELEASE import

    *) Bugfix: on Solaris and Linux there may be too many "recvmsg()
       returned not enough data" alerts.

    *) Bugfix: there were the "writev() failed (22: Invalid argument)"
       errors on Solaris in proxy mode without sendfile. On other platforms
       that do not support sendfile at all the process got caught in an
       endless loop.

    *) Bugfix: segmentation fault on Solaris in proxy mode and using
       sendfile.

    *) Bugfix: segmentation fault on Solaris.

    *) Bugfix: on-line upgrade did not work on Linux.

    *) Bugfix: the ngx_http_autoindex_module module did not escape the
       spaces, the quotes, and the percent signs in the directory listing.

    *) Change: the decrease of the copy operations.

    *) Feature: the userid_p3p directive.
diff --git a/src/os/unix/ngx_aio_write_chain.c b/src/os/unix/ngx_aio_write_chain.c
index b8c1335..4d45f07 100644
--- a/src/os/unix/ngx_aio_write_chain.c
+++ b/src/os/unix/ngx_aio_write_chain.c
@@ -13,11 +13,10 @@
 ngx_chain_t *ngx_aio_write_chain(ngx_connection_t *c, ngx_chain_t *in,
                                  off_t limit)
 {
-    int           n;
     u_char       *buf, *prev;
     off_t         send, sent;
     size_t        len;
-    ssize_t       size;
+    ssize_t       n, size;
     ngx_err_t     err;
     ngx_chain_t  *cl;
 
@@ -63,7 +62,7 @@
 
         n = ngx_aio_write(c, buf, len);
 
-        ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, "aio_write: %d", n);
+        ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, "aio_write: %z", n);
 
         if (n == NGX_ERROR) {
             return NGX_CHAIN_ERROR;
@@ -75,7 +74,7 @@
         }
 
         ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0,
-                       "aio_write sent: " OFF_T_FMT, c->sent);
+                       "aio_write sent: %O", c->sent);
 
         for (cl = in; cl; cl = cl->next) {
 
diff --git a/src/os/unix/ngx_alloc.c b/src/os/unix/ngx_alloc.c
index dc36092..7ec4f87 100644
--- a/src/os/unix/ngx_alloc.c
+++ b/src/os/unix/ngx_alloc.c
@@ -17,11 +17,10 @@
 
     if (!(p = malloc(size))) {
         ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
-                      "malloc() " SIZE_T_FMT " bytes failed", size);
+                      "malloc() %uz bytes failed", size);
     }
 
-    ngx_log_debug2(NGX_LOG_DEBUG_ALLOC, log, 0,
-                   "malloc: " PTR_FMT ":" SIZE_T_FMT, p, size);
+    ngx_log_debug2(NGX_LOG_DEBUG_ALLOC, log, 0, "malloc: %p:%uz", p, size);
 
     return p;
 }
@@ -49,12 +48,12 @@
 
     if (posix_memalign(&p, alignment, size) == -1) {
         ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
-                      "posix_memalign() " SIZE_T_FMT " bytes aligned to "
-                      SIZE_T_FMT " failed", size, alignment);
+                      "posix_memalign() %uz bytes aligned to %uz failed",
+                      size, alignment);
     }
 
     ngx_log_debug2(NGX_LOG_DEBUG_ALLOC, log, 0,
-                   "posix_memalign: " PTR_FMT ":" SIZE_T_FMT, p, size);
+                   "posix_memalign: %p:%uz", p, size);
 
     return p;
 }
@@ -67,12 +66,12 @@
 
     if (!(p = memalign(alignment, size))) {
         ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
-                      "memalign() " SIZE_T_FMT " bytes aligned to "
-                      SIZE_T_FMT " failed", size, alignment);
+                      "memalign() %uz bytes aligned to %uz failed",
+                      size, alignment);
     }
 
     ngx_log_debug2(NGX_LOG_DEBUG_ALLOC, log, 0,
-                   "memalign: " PTR_FMT ":" SIZE_T_FMT, p, size);
+                   "memalign: %p:%uz", p, size);
 
     return p;
 }
diff --git a/src/os/unix/ngx_channel.c b/src/os/unix/ngx_channel.c
index 3d09d68..5c5a5e1 100644
--- a/src/os/unix/ngx_channel.c
+++ b/src/os/unix/ngx_channel.c
@@ -120,9 +120,14 @@
         return NGX_ERROR;
     }
 
+    if (n == 0) {
+        ngx_log_debug0(NGX_LOG_DEBUG_CORE, log, 0, "recvmsg() returned zero");
+        return NGX_ERROR;
+    }
+
     if ((size_t) n < sizeof(ngx_channel_t)) {
         ngx_log_error(NGX_LOG_ALERT, log, 0,
-                      "recvmsg() returned not enough data");
+                      "recvmsg() returned not enough data: %uz", n);
         return NGX_ERROR;
     }
 
diff --git a/src/os/unix/ngx_errno.c b/src/os/unix/ngx_errno.c
index 0c93d23..ced0eaf 100644
--- a/src/os/unix/ngx_errno.c
+++ b/src/os/unix/ngx_errno.c
@@ -10,32 +10,29 @@
 
 #if (NGX_STRERROR_R)
 
-ngx_int_t ngx_strerror_r(int err, char *errstr, size_t size)
+u_char *ngx_strerror_r(int err, u_char *errstr, size_t size)
 {
-    size_t  len;
-
     if (size == 0) {
         return 0;
     }
 
     errstr[0] = '\0';
 
-    strerror_r(err, errstr, size);
+    strerror_r(err, (char *) errstr, size);
 
-    for (len = 0; len < size; len++) {
-        if (errstr[len] == '\0') {
-            break;
-        }
+    while (*errstr && size) {
+        errstr++;
+        size--;
     }
 
-    return len;
+    return errstr;
 }
 
 #elif (NGX_GNU_STRERROR_R)
 
 /* Linux strerror_r() */
 
-ngx_int_t ngx_strerror_r(int err, char *errstr, size_t size)
+u_char *ngx_strerror_r(int err, u_char *errstr, size_t size)
 {
     char    *str;
     size_t   len;
@@ -46,20 +43,18 @@
 
     errstr[0] = '\0';
 
-    str = strerror_r(err, errstr, size);
+    str = strerror_r(err, (char *) errstr, size);
 
-    if (str != errstr) {
-        return ngx_cpystrn((u_char *) errstr, (u_char *) str, size)
-                                                           - (u_char *) errstr;
+    if (str != (char *) errstr) {
+        return ngx_cpystrn(errstr, (u_char *) str, size);
     }
 
-    for (len = 0; len < size; len++) {
-        if (errstr[len] == '\0') {
-            break;
-        }
+    while (*errstr && size) {
+        errstr++;
+        size--;
     }
 
-    return len;
+    return errstr;
 }
 
 #endif
diff --git a/src/os/unix/ngx_errno.h b/src/os/unix/ngx_errno.h
index cc9b1d1..9a392f8 100644
--- a/src/os/unix/ngx_errno.h
+++ b/src/os/unix/ngx_errno.h
@@ -47,14 +47,14 @@
 
 #if (HAVE_STRERROR_R || HAVE_GNU_STRERROR_R)
 
-ngx_int_t ngx_strerror_r(int err, char *errstr, size_t size);
+u_char *ngx_strerror_r(int err, u_char *errstr, size_t size);
 
 #else
 
 /* Solaris has threads-safe strerror() */
 
 #define ngx_strerror_r(err, errstr, size)  \
-             (char *) ngx_cpystrn(errstr, strerror(err), size) - (errstr)
+             ngx_cpystrn(errstr, (u_char *) strerror(err), size)
 
 #endif
 
diff --git a/src/os/unix/ngx_files.c b/src/os/unix/ngx_files.c
index da75ba0..7bf5ad1 100644
--- a/src/os/unix/ngx_files.c
+++ b/src/os/unix/ngx_files.c
@@ -13,7 +13,7 @@
     ssize_t n;
 
     ngx_log_debug4(NGX_LOG_DEBUG_CORE, file->log, 0,
-                   "read: %d, %X, %d, " OFF_T_FMT, file->fd, buf, size, offset);
+                   "read: %d, %p, %uz, %O", file->fd, buf, size, offset);
 
 #if (NGX_PREAD)
 
@@ -57,6 +57,9 @@
 {
     ssize_t n;
 
+    ngx_log_debug4(NGX_LOG_DEBUG_CORE, file->log, 0,
+                   "write: %d, %p, %uz, %O", file->fd, buf, size, offset);
+
 #if (NGX_PWRITE)
 
     n = pwrite(file->fd, buf, size, offset);
@@ -68,7 +71,7 @@
 
     if ((size_t) n != size) {
         ngx_log_error(NGX_LOG_CRIT, file->log, 0,
-                      "pwrite() has written only %d of %d", n, size);
+                      "pwrite() has written only %z of %uz", n, size);
         return NGX_ERROR;
     }
 
@@ -92,7 +95,7 @@
 
     if ((size_t) n != size) {
         ngx_log_error(NGX_LOG_CRIT, file->log, 0,
-                      "write() has written only %d of %d", n, size);
+                      "write() has written only %z of %uz", n, size);
         return NGX_ERROR;
     }
 
@@ -120,17 +123,19 @@
 }
 
 
+#define NGX_IOVS  8
+
 ssize_t ngx_write_chain_to_file(ngx_file_t *file, ngx_chain_t *cl,
                                 off_t offset, ngx_pool_t *pool)
 {
     u_char        *prev;
     size_t         size;
     ssize_t        n;
-    struct iovec  *iov;
     ngx_err_t      err;
-    ngx_array_t    io;
+    ngx_array_t    vec;
+    struct iovec  *iov, iovs[NGX_IOVS];
 
-    /* use pwrite() if there's the only buf in a chain */
+    /* use pwrite() if there is the only buf in a chain */
 
     if (cl->next == NULL) {
         return ngx_write_file(file, cl->buf->pos,
@@ -138,61 +143,74 @@
                               offset);
     }
 
-    prev = NULL;
-    iov = NULL;
-    size = 0;
+    vec.elts = iovs;
+    vec.size = sizeof(struct iovec);
+    vec.nalloc = NGX_IOVS; 
+    vec.pool = pool;
 
-    ngx_init_array(io, pool, 10, sizeof(struct iovec), NGX_ERROR);
+    do {
+        prev = NULL;
+        iov = NULL;
+        size = 0;
 
-    /* create the iovec and coalesce the neighbouring bufs */
+        vec.nelts = 0;
 
-    while (cl) {
-        if (prev == cl->buf->pos) {
-            iov->iov_len += cl->buf->last - cl->buf->pos;
+        /* create the iovec and coalesce the neighbouring bufs */
 
-        } else {
-            ngx_test_null(iov, ngx_push_array(&io), NGX_ERROR);
-            iov->iov_base = (void *) cl->buf->pos;
-            iov->iov_len = cl->buf->last - cl->buf->pos;
+        while (cl && vec.nelts < IOV_MAX) {
+            if (prev == cl->buf->pos) {
+                iov->iov_len += cl->buf->last - cl->buf->pos;
+
+            } else {
+                if (!(iov = ngx_array_push(&vec))) {
+                    return NGX_ERROR;
+                }
+
+                iov->iov_base = (void *) cl->buf->pos;
+                iov->iov_len = cl->buf->last - cl->buf->pos;
+            }
+
+            size += cl->buf->last - cl->buf->pos;
+            prev = cl->buf->last;
+            cl = cl->next;
         }
 
-        size += cl->buf->last - cl->buf->pos;
-        prev = cl->buf->last;
-        cl = cl->next;
-    }
+        /* use pwrite() if there is the only iovec buffer */
 
-    /* use pwrite() if there's the only iovec buffer */
+        if (vec.nelts == 1) {
+            iov = vec.elts;
+            return ngx_write_file(file, (u_char *) iov[0].iov_base,
+                                  iov[0].iov_len, offset);
+        }
 
-    if (io.nelts == 1) {
-        iov = io.elts;
-        return ngx_write_file(file, (u_char *) iov[0].iov_base, iov[0].iov_len,
-                              offset);
-    }
+        if (file->sys_offset != offset) {
+            if (lseek(file->fd, offset, SEEK_SET) == -1) {
+                ngx_log_error(NGX_LOG_CRIT, file->log, ngx_errno,
+                              "lseek() failed");
+                return NGX_ERROR;
+            }
 
-    if (file->sys_offset != offset) {
-        if (lseek(file->fd, offset, SEEK_SET) == -1) {
-            ngx_log_error(NGX_LOG_CRIT, file->log, ngx_errno, "lseek() failed");
+            file->sys_offset = offset;
+        }
+
+        n = writev(file->fd, vec.elts, vec.nelts);
+
+        if (n == -1) {
+            ngx_log_error(NGX_LOG_CRIT, file->log, ngx_errno,
+                          "writev() failed");
             return NGX_ERROR;
         }
 
-        file->sys_offset = offset;
-    }
+        if ((size_t) n != size) {
+            ngx_log_error(NGX_LOG_CRIT, file->log, 0,
+                          "writev() has written only %z of %uz", n, size);
+            return NGX_ERROR;
+        }
 
-    n = writev(file->fd, io.elts, io.nelts);
+        file->sys_offset += n;
+        file->offset += n;
 
-    if (n == -1) {
-        ngx_log_error(NGX_LOG_CRIT, file->log, ngx_errno, "writev() failed");
-        return NGX_ERROR;
-    }
-
-    if ((size_t) n != size) {
-        ngx_log_error(NGX_LOG_CRIT, file->log, 0,
-                      "writev() has written only %d of %d", n, size);
-        return NGX_ERROR;
-    }
-
-    file->sys_offset += n;
-    file->offset += n;
+    } while (cl);
 
     return n;
 }
diff --git a/src/os/unix/ngx_freebsd_init.c b/src/os/unix/ngx_freebsd_init.c
index 39a057b..faf8e9f 100644
--- a/src/os/unix/ngx_freebsd_init.c
+++ b/src/os/unix/ngx_freebsd_init.c
@@ -9,8 +9,8 @@
 
 
 /* FreeBSD 3.0 at least */
-char ngx_freebsd_kern_ostype[20];
-char ngx_freebsd_kern_osrelease[20];
+char ngx_freebsd_kern_ostype[16];
+char ngx_freebsd_kern_osrelease[128];
 int ngx_freebsd_kern_osreldate;
 int ngx_freebsd_hw_ncpu;
 int ngx_freebsd_net_inet_tcp_sendspace;
@@ -95,7 +95,12 @@
                      ngx_freebsd_kern_ostype, &size, NULL, 0) == -1) {
         ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
                       "sysctlbyname(kern.ostype) failed");
-        return NGX_ERROR;
+
+        if (ngx_errno != NGX_ENOMEM) {
+            return NGX_ERROR;
+        }
+
+        ngx_freebsd_kern_ostype[size - 1] = '\0';
     }
 
     size = sizeof(ngx_freebsd_kern_osrelease);
@@ -103,7 +108,12 @@
                      ngx_freebsd_kern_osrelease, &size, NULL, 0) == -1) {
         ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
                       "sysctlbyname(kern.osrelease) failed");
-        return NGX_ERROR;
+
+        if (ngx_errno != NGX_ENOMEM) {
+            return NGX_ERROR;
+        }
+
+        ngx_freebsd_kern_osrelease[size - 1] = '\0';
     }
 
 
diff --git a/src/os/unix/ngx_freebsd_rfork_thread.c b/src/os/unix/ngx_freebsd_rfork_thread.c
index 774adb9..b64325b 100644
--- a/src/os/unix/ngx_freebsd_rfork_thread.c
+++ b/src/os/unix/ngx_freebsd_rfork_thread.c
@@ -21,9 +21,10 @@
  * The SysV semop() is a cheap syscall, particularly if it has little sembuf's
  * and does not use SEM_UNDO.
  *
- * The condition variable implementation uses signal #64.  The signal handler
- * is SIG_IGN so the kill() is a cheap syscall.  The thread waits a signal
- * in kevent().  The use of the EVFILT_SIGNAL is safe since FreeBSD 4.7.
+ * The condition variable implementation uses the signal #64.
+ * The signal handler is SIG_IGN so the kill() is a cheap syscall.
+ * The thread waits a signal in kevent().  The use of the EVFILT_SIGNAL
+ * is safe since FreeBSD 4.10-STABLE.
  *
  * This threads implementation currently works on i386 (486+) and amd64
  * platforms only.
@@ -114,15 +115,16 @@
 #endif
 
 
-int ngx_create_thread(ngx_tid_t *tid, void* (*func)(void *arg), void *arg,
-                      ngx_log_t *log)
+ngx_err_t ngx_create_thread(ngx_tid_t *tid, void* (*func)(void *arg), void *arg,
+                            ngx_log_t *log)
 {
-    int    id, err;
-    char  *stack, *stack_top;
+    ngx_pid_t   id;
+    ngx_err_t   err;
+    char       *stack, *stack_top;
 
     if (nthreads >= max_threads) {
         ngx_log_error(NGX_LOG_CRIT, log, 0,
-                      "no more than %d threads can be created", max_threads);
+                      "no more than %ui threads can be created", max_threads);
         return NGX_ERROR;
     }
 
@@ -133,20 +135,21 @@
 
     if (stack == MAP_FAILED) {
         ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
-                      "mmap(" PTR_FMT ":" SIZE_T_FMT
-                      ", MAP_STACK) thread stack failed",
+                      "mmap(%p:%uz, MAP_STACK) thread stack failed",
                       last_stack, usable_stack_size);
         return NGX_ERROR;
     }
 
     if (stack != last_stack) {
-        ngx_log_error(NGX_LOG_ALERT, log, 0, "stack address was changed");
+        ngx_log_error(NGX_LOG_ALERT, log, 0,
+                      "stack %p address was changed to %p", last_stack, stack);
+        return NGX_ERROR;
     }
 
     stack_top = stack + usable_stack_size;
 
     ngx_log_debug2(NGX_LOG_DEBUG_CORE, log, 0,
-                   "thread stack: " PTR_FMT "-" PTR_FMT, stack, stack_top);
+                   "thread stack: %p-%p", stack, stack_top);
 
     ngx_set_errno(0);
 
@@ -164,7 +167,7 @@
                                                        / ngx_thread_stack_size;
         tids[nthreads] = id;
 
-        ngx_log_debug1(NGX_LOG_DEBUG_CORE, log, 0, "rfork()ed thread: %d", id);
+        ngx_log_debug1(NGX_LOG_DEBUG_CORE, log, 0, "rfork()ed thread: %P", id);
     }
 
     return err;
@@ -205,30 +208,30 @@
     red_zone = ngx_freebsd_kern_usrstack - (size + rz_size);
 
     ngx_log_debug2(NGX_LOG_DEBUG_CORE, cycle->log, 0,
-                   "usrstack: " PTR_FMT " red zone: " PTR_FMT,
+                   "usrstack: %p red zone: %p",
                    ngx_freebsd_kern_usrstack, red_zone);
 
     zone = mmap(red_zone, rz_size, PROT_NONE, MAP_ANON, -1, 0);
     if (zone == MAP_FAILED) {
         ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
-                      "mmap(" PTR_FMT ":" SIZE_T_FMT
-                      ", PROT_NONE, MAP_ANON) red zone failed",
+                      "mmap(%p:%uz, PROT_NONE, MAP_ANON) red zone failed",
                       red_zone, rz_size);
         return NGX_ERROR;
     }
 
     if (zone != red_zone) {
         ngx_log_error(NGX_LOG_ALERT, cycle->log, 0,
-                      "red zone address was changed");
+                      "red zone %p address was changed to %p", red_zone, zone);
+        return NGX_ERROR;
     }
 
-    /* create the threads errno's array */
+    /* create the thread errno' array */
 
     if (!(errnos = ngx_calloc(n * sizeof(int), cycle->log))) {
         return NGX_ERROR;
     }
 
-    /* create the threads tids array */
+    /* create the thread tids array */
 
     if (!(tids = ngx_calloc((n + 1) * sizeof(ngx_tid_t), cycle->log))) {
         return NGX_ERROR;
@@ -236,7 +239,7 @@
 
     tids[0] = ngx_pid;
 
-    /* create the threads tls's array */
+    /* create the thread tls' array */
 
     ngx_tls = ngx_calloc(NGX_THREAD_KEYS_MAX * (n + 1) * sizeof(void *),
                          cycle->log);
@@ -274,7 +277,7 @@
 }
 
 
-ngx_int_t ngx_thread_key_create(ngx_tls_key_t *key)
+ngx_err_t ngx_thread_key_create(ngx_tls_key_t *key)
 {
     if (nkeys >= NGX_THREAD_KEYS_MAX) {
         return NGX_ENOMEM;
@@ -286,7 +289,7 @@
 }
 
 
-ngx_int_t ngx_thread_set_tls(ngx_tls_key_t key, void *value)
+ngx_err_t ngx_thread_set_tls(ngx_tls_key_t key, void *value)
 {
     if (key >= NGX_THREAD_KEYS_MAX) {
         return NGX_EINVAL;
@@ -297,7 +300,7 @@
 }
 
 
-ngx_mutex_t *ngx_mutex_init(ngx_log_t *log, uint flags)
+ngx_mutex_t *ngx_mutex_init(ngx_log_t *log, ngx_uint_t flags)
 {
     ngx_mutex_t  *m;
     union semun   op;
@@ -361,10 +364,10 @@
 #if (NGX_DEBUG)
     if (try) {
         ngx_log_debug2(NGX_LOG_DEBUG_MUTEX, m->log, 0,
-                       "try lock mutex " PTR_FMT " lock:%X", m, m->lock);
+                       "try lock mutex %p lock:%XD", m, m->lock);
     } else {
         ngx_log_debug2(NGX_LOG_DEBUG_MUTEX, m->log, 0,
-                       "lock mutex " PTR_FMT " lock:%X", m, m->lock);
+                       "lock mutex %p lock:%XD", m, m->lock);
     }
 #endif
 
@@ -395,7 +398,7 @@
             }
 
             ngx_log_debug2(NGX_LOG_DEBUG_MUTEX, m->log, 0,
-                           "mutex " PTR_FMT " lock:%X", m, m->lock);
+                           "mutex %p lock:%XD", m, m->lock);
 
             /*
              * The mutex is locked so we increase a number
@@ -406,8 +409,8 @@
 
             if ((lock & ~NGX_MUTEX_LOCK_BUSY) > nthreads) {
                 ngx_log_error(NGX_LOG_ALERT, m->log, ngx_errno,
-                              "%d threads wait for mutex " PTR_FMT
-                              ", while only %d threads are available",
+                              "%D threads wait for mutex %p, "
+                              "while only %ui threads are available",
                               lock & ~NGX_MUTEX_LOCK_BUSY, m, nthreads);
                 return NGX_ERROR;
             }
@@ -415,7 +418,7 @@
             if (ngx_atomic_cmp_set(&m->lock, old, lock)) {
 
                 ngx_log_debug2(NGX_LOG_DEBUG_MUTEX, m->log, 0,
-                               "wait mutex " PTR_FMT " lock:%X", m, m->lock);
+                               "wait mutex %p lock:%XD", m, m->lock);
 
                 /*
                  * The number of the waiting threads has been increased
@@ -430,14 +433,12 @@
 
                 if (semop(m->semid, &op, 1) == -1) {
                     ngx_log_error(NGX_LOG_ALERT, m->log, ngx_errno,
-                                  "semop() failed while waiting "
-                                  "on mutex " PTR_FMT, m);
+                                 "semop() failed while waiting on mutex %p", m);
                     return NGX_ERROR;
                 }
 
                 ngx_log_debug2(NGX_LOG_DEBUG_MUTEX, m->log, 0,
-                               "mutex waked up " PTR_FMT " lock:%X",
-                               m, m->lock);
+                               "mutex waked up %p lock:%XD", m, m->lock);
 
                 tries = 0;
                 old = m->lock;
@@ -462,7 +463,7 @@
         if (tries++ > 1000) {
 
             ngx_log_debug1(NGX_LOG_DEBUG_MUTEX, m->log, 0,
-                           "mutex " PTR_FMT " is contested", m);
+                           "mutex %p is contested", m);
 
             /* the mutex is probably contested so we are giving up now */
 
@@ -474,7 +475,7 @@
     }
 
     ngx_log_debug2(NGX_LOG_DEBUG_MUTEX, m->log, 0,
-                   "mutex " PTR_FMT " is locked, lock:%X", m, m->lock);
+                   "mutex %p is locked, lock:%XD", m, m->lock);
 
     return NGX_OK;
 }
@@ -493,7 +494,7 @@
 
     if (!(old & NGX_MUTEX_LOCK_BUSY)) {
         ngx_log_error(NGX_LOG_ALERT, m->log, 0,
-                      "trying to unlock the free mutex " PTR_FMT, m);
+                      "trying to unlock the free mutex %p", m);
         return NGX_ERROR;
     }
 
@@ -501,7 +502,7 @@
 
 #if 0
     ngx_log_debug2(NGX_LOG_DEBUG_MUTEX, m->log, 0,
-                   "unlock mutex " PTR_FMT " lock:%X", m, old);
+                   "unlock mutex %p lock:%XD", m, old);
 #endif
 
     for ( ;; ) {
@@ -516,7 +517,7 @@
 
     if (m->semid == -1) {
         ngx_log_debug1(NGX_LOG_DEBUG_MUTEX, m->log, 0,
-                       "mutex " PTR_FMT " is unlocked", m);
+                       "mutex %p is unlocked", m);
 
         return NGX_OK;
     }
@@ -546,7 +547,7 @@
             /* wake up the thread that waits on semaphore */
 
             ngx_log_debug1(NGX_LOG_DEBUG_MUTEX, m->log, 0,
-                           "wake up mutex " PTR_FMT "", m);
+                           "wake up mutex %p", m);
 
             op.sem_num = 0;
             op.sem_op = 1;
@@ -554,8 +555,7 @@
 
             if (semop(m->semid, &op, 1) == -1) {
                 ngx_log_error(NGX_LOG_ALERT, m->log, ngx_errno,
-                              "semop() failed while waking up on mutex "
-                              PTR_FMT, m);
+                              "semop() failed while waking up on mutex %p", m);
                 return NGX_ERROR;
             }
 
@@ -566,7 +566,7 @@
     }
 
     ngx_log_debug1(NGX_LOG_DEBUG_MUTEX, m->log, 0,
-                   "mutex " PTR_FMT " is unlocked", m);
+                   "mutex %p is unlocked", m);
 
     return NGX_OK;
 }
@@ -647,21 +647,20 @@
     }
 
     ngx_log_debug3(NGX_LOG_DEBUG_CORE, cv->log, 0,
-                   "cv " PTR_FMT " wait, kq:%d, signo:%d",
-                   cv, cv->kq, cv->signo);
+                   "cv %p wait, kq:%d, signo:%d", cv, cv->kq, cv->signo);
 
     for ( ;; ) {
         n = kevent(cv->kq, NULL, 0, &kev, 1, NULL);
 
         ngx_log_debug2(NGX_LOG_DEBUG_CORE, cv->log, 0,
-                       "cv " PTR_FMT " kevent: %d", cv, n);
+                       "cv %p kevent: %d", cv, n);
 
         if (n == -1) {
             err = ngx_errno;
             ngx_log_error((err == NGX_EINTR) ? NGX_LOG_INFO : NGX_LOG_ALERT,
                           cv->log, ngx_errno,
-                          "kevent() failed while waiting condition variable "
-                          PTR_FMT, cv);
+                          "kevent() failed while waiting condition variable %p",
+                          cv);
 
             if (err == NGX_EINTR) {
                 break;
@@ -673,7 +672,7 @@
         if (n == 0) {
             ngx_log_error(NGX_LOG_ALERT, cv->log, 0,
                           "kevent() returned no events "
-                          "while waiting condition variable " PTR_FMT,
+                          "while waiting condition variable %p",
                           cv);
             continue;
         }
@@ -681,7 +680,7 @@
         if (kev.filter != EVFILT_SIGNAL) {
             ngx_log_error(NGX_LOG_ALERT, cv->log, 0,
                           "kevent() returned unexpected events: %d "
-                          "while waiting condition variable " PTR_FMT,
+                          "while waiting condition variable %p",
                           kev.filter, cv);
             continue;
         }
@@ -689,7 +688,7 @@
         if (kev.ident != (uintptr_t) cv->signo) {
             ngx_log_error(NGX_LOG_ALERT, cv->log, 0,
                           "kevent() returned unexpected signal: %d ",
-                          "while waiting condition variable " PTR_FMT,
+                          "while waiting condition variable %p",
                           kev.ident, cv);
             continue;
         }
@@ -697,8 +696,7 @@
         break;
     }
 
-    ngx_log_debug1(NGX_LOG_DEBUG_CORE, cv->log, 0,
-                   "cv " PTR_FMT " is waked up", cv);
+    ngx_log_debug1(NGX_LOG_DEBUG_CORE, cv->log, 0, "cv %p is waked up", cv);
 
     if (ngx_mutex_lock(m) == NGX_ERROR) {
         return NGX_ERROR;
@@ -713,7 +711,7 @@
     ngx_err_t  err;
 
     ngx_log_debug3(NGX_LOG_DEBUG_CORE, cv->log, 0,
-                   "cv " PTR_FMT " to signal " PID_T_FMT " %d",
+                   "cv %p to signal %P %d",
                    cv, cv->tid, cv->signo);
 
     if (kill(cv->tid, cv->signo) == -1) {
@@ -721,8 +719,7 @@
         err = ngx_errno;
 
         ngx_log_error(NGX_LOG_ALERT, cv->log, err,
-                      "kill() failed while signaling condition variable "
-                      PTR_FMT, cv);
+                     "kill() failed while signaling condition variable %p", cv);
 
         if (err == NGX_ESRCH) {
             cv->tid = -1;
@@ -731,8 +728,7 @@
         return NGX_ERROR;
     }
 
-    ngx_log_debug1(NGX_LOG_DEBUG_CORE, cv->log, 0,
-                   "cv " PTR_FMT " is signaled", cv);
+    ngx_log_debug1(NGX_LOG_DEBUG_CORE, cv->log, 0, "cv %p is signaled", cv);
 
     return NGX_OK;
 }
diff --git a/src/os/unix/ngx_freebsd_rfork_thread.h b/src/os/unix/ngx_freebsd_rfork_thread.h
index bef230a..2af0adb 100644
--- a/src/os/unix/ngx_freebsd_rfork_thread.h
+++ b/src/os/unix/ngx_freebsd_rfork_thread.h
@@ -18,7 +18,7 @@
 #define ngx_log_pid    ngx_thread_self()
 #define ngx_log_tid    0
 
-#define TID_T_FMT      PID_T_FMT
+#define NGX_TID_T_FMT  "%P"
 
 
 #define NGX_MUTEX_LIGHT      1
@@ -91,10 +91,10 @@
 
 extern void    **ngx_tls;
 
-ngx_int_t ngx_thread_key_create(ngx_tls_key_t *key);
+ngx_err_t ngx_thread_key_create(ngx_tls_key_t *key);
 #define ngx_thread_key_create_n  "the tls key creation"
 
-ngx_int_t ngx_thread_set_tls(ngx_tls_key_t key, void *value);
+ngx_err_t ngx_thread_set_tls(ngx_tls_key_t key, void *value);
 #define ngx_thread_set_tls_n     "the tls key setting"
 
 
diff --git a/src/os/unix/ngx_freebsd_sendfile_chain.c b/src/os/unix/ngx_freebsd_sendfile_chain.c
index 8303c06..192a1a1 100644
--- a/src/os/unix/ngx_freebsd_sendfile_chain.c
+++ b/src/os/unix/ngx_freebsd_sendfile_chain.c
@@ -265,7 +265,7 @@
                     }
 
                     ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, err,
-                                   "sendfile() sent only " OFF_T_FMT " bytes",
+                                   "sendfile() sent only %O bytes",
                                    sent);
 
                 } else {
@@ -290,14 +290,14 @@
             }
 
             ngx_log_debug4(NGX_LOG_DEBUG_EVENT, c->log, 0,
-                           "sendfile: %d, @" OFF_T_FMT " " OFF_T_FMT ":%d",
+                           "sendfile: %d, @%O %O:%uz",
                            rc, file->file_pos, sent, fsize + hsize);
 
         } else {
             rc = writev(c->fd, header.elts, header.nelts);
 
             ngx_log_debug2(NGX_LOG_DEBUG_EVENT, c->log, 0,
-                           "writev: %d of " SIZE_T_FMT, rc, hsize);
+                           "writev: %d of %uz", rc, hsize);
 
             if (rc == -1) {
                 err = ngx_errno;
diff --git a/src/os/unix/ngx_linux_config.h b/src/os/unix/ngx_linux_config.h
index 9a0fee9..fb2cbc8 100644
--- a/src/os/unix/ngx_linux_config.h
+++ b/src/os/unix/ngx_linux_config.h
@@ -44,6 +44,7 @@
 #include <netdb.h>
 
 #include <time.h>               /* tzset() */
+#include <malloc.h>             /* memalign() */
 #include <sys/ioctl.h>
 #include <sys/sysctl.h>
 
diff --git a/src/os/unix/ngx_linux_sendfile_chain.c b/src/os/unix/ngx_linux_sendfile_chain.c
index 4de7ac2..ad40f55 100644
--- a/src/os/unix/ngx_linux_sendfile_chain.c
+++ b/src/os/unix/ngx_linux_sendfile_chain.c
@@ -195,7 +195,7 @@
             sent = rc > 0 ? rc : 0;
 
             ngx_log_debug4(NGX_LOG_DEBUG_EVENT, c->log, 0,
-                           "sendfile: %d, @" OFF_T_FMT " %d:%d",
+                           "sendfile: %d, @%O %z:%uz",
                            rc, file->file_pos, sent, fsize);
 
         } else {
@@ -221,7 +221,7 @@
 
             sent = rc > 0 ? rc : 0;
 
-            ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, "writev: %d", sent);
+            ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, "writev: %z", sent);
         }
 
         if (send - sprev == sent) {
diff --git a/src/os/unix/ngx_posix_init.c b/src/os/unix/ngx_posix_init.c
index 1c7b1b6..19d65a4 100644
--- a/src/os/unix/ngx_posix_init.c
+++ b/src/os/unix/ngx_posix_init.c
@@ -134,7 +134,7 @@
 void ngx_posix_status(ngx_log_t *log)
 {
     ngx_log_error(NGX_LOG_INFO, log, 0,
-                  "getrlimit(RLIMIT_NOFILE): " RLIM_T_FMT ":" RLIM_T_FMT,
+                  "getrlimit(RLIMIT_NOFILE): %r:%r",
                   rlmt.rlim_cur, rlmt.rlim_max);
 }
 
diff --git a/src/os/unix/ngx_process.c b/src/os/unix/ngx_process.c
index 4f7ed64..3141e14 100644
--- a/src/os/unix/ngx_process.c
+++ b/src/os/unix/ngx_process.c
@@ -143,7 +143,7 @@
     }
 
     ngx_log_debug2(NGX_LOG_DEBUG_CORE, cycle->log, 0,
-                   "spawn %s: " PID_T_FMT, name, pid);
+                   "spawn %s: %P", name, pid);
 
     ngx_processes[s].pid = pid;
     ngx_processes[s].exited = 0;
@@ -282,22 +282,40 @@
 
         if (WTERMSIG(status)) {
             ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, 0,
-                          "%s " PID_T_FMT " exited on signal %d%s",
+                          "%s %P exited on signal %d%s",
                           process, pid, WTERMSIG(status),
                           WCOREDUMP(status) ? " (core dumped)" : "");
 
         } else {
             ngx_log_error(NGX_LOG_INFO, ngx_cycle->log, 0,
-                          "%s " PID_T_FMT " exited with code %d",
+                          "%s %P exited with code %d",
                           process, pid, WEXITSTATUS(status));
         }
 
         if (WEXITSTATUS(status) == 2 && ngx_processes[i].respawn) {
             ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, 0,
-                          "%s " PID_T_FMT
-                          " exited with fatal code %d and could not respawn",
-                          process, pid, WEXITSTATUS(status));
+                        "%s %P exited with fatal code %d and could not respawn",
+                        process, pid, WEXITSTATUS(status));
             ngx_processes[i].respawn = 0;
         }
     }
 }
+
+
+void ngx_debug_point()
+{
+    ngx_core_conf_t  *ccf;
+
+    ccf = (ngx_core_conf_t *) ngx_get_conf(ngx_cycle->conf_ctx,
+                                           ngx_core_module);
+
+    switch (ccf->debug_points) {
+
+    case NGX_DEBUG_POINTS_STOP:
+        raise(SIGSTOP);
+        break;
+
+    case NGX_DEBUG_POINTS_ABORT:
+        abort();
+    }
+}
diff --git a/src/os/unix/ngx_process.h b/src/os/unix/ngx_process.h
index 9cb0700..12703b3 100644
--- a/src/os/unix/ngx_process.h
+++ b/src/os/unix/ngx_process.h
@@ -53,6 +53,7 @@
                             char *name, ngx_int_t respawn);
 ngx_pid_t ngx_execute(ngx_cycle_t *cycle, ngx_exec_ctx_t *ctx);
 void ngx_process_get_status(void);
+void ngx_debug_point(void);
 
 
 #if (NGX_HAVE_SCHED_YIELD)
diff --git a/src/os/unix/ngx_process_cycle.c b/src/os/unix/ngx_process_cycle.c
index 1da4915..0447bc4 100644
--- a/src/os/unix/ngx_process_cycle.c
+++ b/src/os/unix/ngx_process_cycle.c
@@ -322,11 +322,10 @@
             }
 
             ngx_log_debug6(NGX_LOG_DEBUG_CORE, cycle->log, 0,
-                           "pass channel s:%d pid:" PID_T_FMT
-                           " fd:%d to s:%d pid:" PID_T_FMT " fd:%d",
-                           ch.slot, ch.pid, ch.fd,
-                           i, ngx_processes[i].pid,
-                           ngx_processes[i].channel[0]);
+                          "pass channel s:%d pid:%P fd:%d to s:%i pid:%P fd:%d",
+                          ch.slot, ch.pid, ch.fd,
+                          i, ngx_processes[i].pid,
+                          ngx_processes[i].channel[0]);
 
             /* TODO: NGX_AGAIN */
 
@@ -384,7 +383,7 @@
     for (i = 0; i < ngx_last_process; i++) {
 
         ngx_log_debug7(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
-                       "child: %d " PID_T_FMT " e:%d t:%d d:%d r:%d j:%d",
+                       "child: %d %P e:%d t:%d d:%d r:%d j:%d",
                        i,
                        ngx_processes[i].pid,
                        ngx_processes[i].exiting,
@@ -421,13 +420,12 @@
         }
 
         ngx_log_debug2(NGX_LOG_DEBUG_CORE, cycle->log, 0,
-                       "kill (" PID_T_FMT ", %d)" ,
-                       ngx_processes[i].pid, signo);
+                       "kill (%P, %d)" , ngx_processes[i].pid, signo);
 
         if (kill(ngx_processes[i].pid, signo) == -1) {
             err = ngx_errno;
             ngx_log_error(NGX_LOG_ALERT, cycle->log, err,
-                          "kill(%d, %d) failed",
+                          "kill(%P, %d) failed",
                           ngx_processes[i].pid, signo);
 
             if (err == NGX_ESRCH) {
@@ -459,7 +457,7 @@
     for (i = 0; i < ngx_last_process; i++) {
 
         ngx_log_debug7(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
-                       "child: %d " PID_T_FMT " e:%d t:%d d:%d r:%d j:%d",
+                       "child: %d %P e:%d t:%d d:%d r:%d j:%d",
                        i,
                        ngx_processes[i].pid,
                        ngx_processes[i].exiting,
@@ -492,8 +490,8 @@
                     }
 
                     ngx_log_debug3(NGX_LOG_DEBUG_CORE, cycle->log, 0,
-                       "pass close channel s:%d pid:" PID_T_FMT
-                       " to:" PID_T_FMT, ch.slot, ch.pid, ngx_processes[n].pid);
+                                   "pass close channel s:%i pid:%P to:%P",
+                                   ch.slot, ch.pid, ngx_processes[n].pid);
 
                     /* TODO: NGX_AGAIN */
 
@@ -786,9 +784,19 @@
 
     n = ngx_read_channel(c->fd, &ch, sizeof(ngx_channel_t), ev->log);
 
-    ngx_log_debug1(NGX_LOG_DEBUG_CORE, ev->log, 0, "channel: %d", n);
+    ngx_log_debug1(NGX_LOG_DEBUG_CORE, ev->log, 0, "channel: %i", n);
 
-    if (n <= 0) {
+    if (n == NGX_ERROR) {
+        if (close(c->fd) == -1) {
+            ngx_log_error(NGX_LOG_ALERT, ev->log, ngx_errno,
+                          "close() channel failed");
+        }
+
+        c->fd = -1;
+        return;
+    }
+
+    if (n == NGX_AGAIN) {
         return;
     }
 
@@ -812,8 +820,7 @@
     case NGX_CMD_OPEN_CHANNEL:
 
         ngx_log_debug3(NGX_LOG_DEBUG_CORE, ev->log, 0,
-                       "get channel s:%d pid:" PID_T_FMT " fd:%d",
-                       ch.slot, ch.pid, ch.fd);
+                       "get channel s:%i pid:%P fd:%d", ch.slot, ch.pid, ch.fd);
 
         ngx_processes[ch.slot].pid = ch.pid;
         ngx_processes[ch.slot].channel[0] = ch.fd;
@@ -822,8 +829,7 @@
     case NGX_CMD_CLOSE_CHANNEL:
 
         ngx_log_debug4(NGX_LOG_DEBUG_CORE, ev->log, 0,
-                       "close channel s:%d pid:" PID_T_FMT " our:" PID_T_FMT
-                       " fd:%d",
+                       "close channel s:%i pid:%P our:%P fd:%d",
                        ch.slot, ch.pid, ngx_processes[ch.slot].pid,
                        ngx_processes[ch.slot].channel[0]);
 
@@ -882,7 +888,7 @@
 }
 
 
-static void* ngx_worker_thread_cycle(void *data)
+static void *ngx_worker_thread_cycle(void *data)
 {
     ngx_thread_t  *thr = data;
 
@@ -909,7 +915,7 @@
     }
 
     ngx_log_debug1(NGX_LOG_DEBUG_CORE, cycle->log, 0,
-                   "thread " TID_T_FMT " started", ngx_thread_self());
+                   "thread " NGX_TID_T_FMT " started", ngx_thread_self());
 
     ngx_setthrtitle("worker thread");
 
@@ -941,7 +947,8 @@
             ngx_mutex_unlock(ngx_posted_events_mutex);
 
             ngx_log_debug1(NGX_LOG_DEBUG_CORE, cycle->log, 0,
-                           "thread %d is done", ngx_thread_self());
+                           "thread " NGX_TID_T_FMT " is done",
+                           ngx_thread_self());
 
             return (void *) 0;
         }
diff --git a/src/os/unix/ngx_pthread_thread.c b/src/os/unix/ngx_pthread_thread.c
index ad7987b..4903d5e 100644
--- a/src/os/unix/ngx_pthread_thread.c
+++ b/src/os/unix/ngx_pthread_thread.c
@@ -15,14 +15,14 @@
 static pthread_attr_t  thr_attr;
 
 
-int ngx_create_thread(ngx_tid_t *tid, void* (*func)(void *arg), void *arg,
-                      ngx_log_t *log)
+ngx_err_t ngx_create_thread(ngx_tid_t *tid, void* (*func)(void *arg), void *arg,
+                            ngx_log_t *log)
 {
     int  err;
 
     if (nthreads >= max_threads) {
         ngx_log_error(NGX_LOG_CRIT, log, 0,
-                      "no more than %d threads can be created", max_threads);
+                      "no more than %ui threads can be created", max_threads);
         return NGX_ERROR;
     }
 
@@ -34,7 +34,7 @@
     }
 
     ngx_log_debug1(NGX_LOG_DEBUG_CORE, log, 0,
-                   "thread is created: " TID_T_FMT, *tid);
+                   "thread is created: " NGX_TID_T_FMT, *tid);
 
     nthreads++;
 
@@ -70,7 +70,7 @@
 }
 
 
-ngx_mutex_t *ngx_mutex_init(ngx_log_t *log, uint flags)
+ngx_mutex_t *ngx_mutex_init(ngx_log_t *log, ngx_uint_t flags)
 {
     int           err;
     ngx_mutex_t  *m;
@@ -101,7 +101,7 @@
 
     if (err != 0) {
         ngx_log_error(NGX_LOG_ALERT, m->log, err,
-                      "pthread_mutex_destroy(" PTR_FMT ") failed", m);
+                      "pthread_mutex_destroy(%p) failed", m);
     }
 
     ngx_free(m);
@@ -116,18 +116,17 @@
         return NGX_OK;
     }
 
-    ngx_log_debug1(NGX_LOG_DEBUG_MUTEX, m->log, 0, "lock mutex " PTR_FMT, m);
+    ngx_log_debug1(NGX_LOG_DEBUG_MUTEX, m->log, 0, "lock mutex %p", m);
 
     err = pthread_mutex_lock(&m->mutex);
 
     if (err != 0) {
         ngx_log_error(NGX_LOG_ALERT, m->log, err,
-                      "pthread_mutex_lock(" PTR_FMT ") failed", m);
+                      "pthread_mutex_lock(%p) failed", m);
         return NGX_ERROR;
     }
 
-    ngx_log_debug1(NGX_LOG_DEBUG_MUTEX, m->log, 0,
-                   "mutex " PTR_FMT " is locked", m);
+    ngx_log_debug1(NGX_LOG_DEBUG_MUTEX, m->log, 0, "mutex %p is locked", m);
 
     return NGX_OK;
 }
@@ -141,8 +140,7 @@
         return NGX_OK;
     }
 
-    ngx_log_debug1(NGX_LOG_DEBUG_MUTEX, m->log, 0,
-                   "try lock mutex " PTR_FMT, m);
+    ngx_log_debug1(NGX_LOG_DEBUG_MUTEX, m->log, 0, "try lock mutex %p", m);
 
     err = pthread_mutex_trylock(&m->mutex);
 
@@ -152,12 +150,11 @@
 
     if (err != 0) {
         ngx_log_error(NGX_LOG_ALERT, m->log, err,
-                      "pthread_mutex_trylock(" PTR_FMT ") failed", m);
+                      "pthread_mutex_trylock(%p) failed", m);
         return NGX_ERROR;
     }
 
-    ngx_log_debug1(NGX_LOG_DEBUG_MUTEX, m->log, 0,
-                   "mutex " PTR_FMT " is locked", m);
+    ngx_log_debug1(NGX_LOG_DEBUG_MUTEX, m->log, 0, "mutex %p is locked", m);
 
     return NGX_OK;
 }
@@ -171,18 +168,17 @@
         return NGX_OK;
     }
 
-    ngx_log_debug1(NGX_LOG_DEBUG_MUTEX, m->log, 0, "unlock mutex " PTR_FMT, m);
+    ngx_log_debug1(NGX_LOG_DEBUG_MUTEX, m->log, 0, "unlock mutex %p", m);
 
     err = pthread_mutex_unlock(&m->mutex);
 
     if (err != 0) {
         ngx_log_error(NGX_LOG_ALERT, m->log, err,
-                      "pthread_mutex_unlock(" PTR_FMT ") failed", m);
+                      "pthread_mutex_unlock(%p) failed", m);
         return NGX_ERROR;
     }
 
-    ngx_log_debug1(NGX_LOG_DEBUG_MUTEX, m->log, 0,
-                   "mutex " PTR_FMT " is unlocked", m);
+    ngx_log_debug1(NGX_LOG_DEBUG_MUTEX, m->log, 0, "mutex %p is unlocked", m);
 
     return NGX_OK;
 }
@@ -219,7 +215,7 @@
 
     if (err != 0) {
         ngx_log_error(NGX_LOG_ALERT, cv->log, err,
-                      "pthread_cond_destroy(" PTR_FMT ") failed", cv);
+                      "pthread_cond_destroy(%p) failed", cv);
     }
 
     ngx_free(cv);
@@ -230,22 +226,19 @@
 {
     int  err;
 
-    ngx_log_debug1(NGX_LOG_DEBUG_CORE, cv->log, 0,
-                   "cv " PTR_FMT " wait", cv);
+    ngx_log_debug1(NGX_LOG_DEBUG_CORE, cv->log, 0, "cv %p wait", cv);
 
     err = pthread_cond_wait(&cv->cond, &m->mutex);
 
     if (err != 0) {
         ngx_log_error(NGX_LOG_ALERT, cv->log, err,
-                      "pthread_cond_wait(" PTR_FMT ") failed", cv);
+                      "pthread_cond_wait(%p) failed", cv);
         return NGX_ERROR;
     }
 
-    ngx_log_debug1(NGX_LOG_DEBUG_CORE, cv->log, 0,
-                   "cv " PTR_FMT " is waked up", cv);
+    ngx_log_debug1(NGX_LOG_DEBUG_CORE, cv->log, 0, "cv %p is waked up", cv);
 
-    ngx_log_debug1(NGX_LOG_DEBUG_MUTEX, m->log, 0,
-                   "mutex " PTR_FMT " is locked", m);
+    ngx_log_debug1(NGX_LOG_DEBUG_MUTEX, m->log, 0, "mutex %p is locked", m);
 
     return NGX_OK;
 }
@@ -255,19 +248,17 @@
 {
     int  err;
 
-    ngx_log_debug1(NGX_LOG_DEBUG_CORE, cv->log, 0,
-                   "cv " PTR_FMT " to signal", cv);
+    ngx_log_debug1(NGX_LOG_DEBUG_CORE, cv->log, 0, "cv %p to signal", cv);
 
     err = pthread_cond_signal(&cv->cond);
 
     if (err != 0) {
         ngx_log_error(NGX_LOG_ALERT, cv->log, err,
-                      "pthread_cond_signal(" PTR_FMT ") failed", cv);
+                      "pthread_cond_signal(%p) failed", cv);
         return NGX_ERROR;
     }
 
-    ngx_log_debug1(NGX_LOG_DEBUG_CORE, cv->log, 0,
-                   "cv " PTR_FMT " is signaled", cv);
+    ngx_log_debug1(NGX_LOG_DEBUG_CORE, cv->log, 0, "cv %p is signaled", cv);
 
     return NGX_OK;
 }
diff --git a/src/os/unix/ngx_shared.c b/src/os/unix/ngx_shared.c
index 60f2de0..7f3c5e9 100644
--- a/src/os/unix/ngx_shared.c
+++ b/src/os/unix/ngx_shared.c
@@ -18,8 +18,7 @@
 
     if (p == MAP_FAILED) {
         ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
-                      "mmap(MAP_ANON|MAP_SHARED, " SIZE_T_FMT ") failed",
-                      size);
+                      "mmap(MAP_ANON|MAP_SHARED, %uz) failed", size);
         return NULL;
     }
 
@@ -45,8 +44,7 @@
 
     if (p == MAP_FAILED) {
         ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
-                      "mmap(/dev/zero, MAP_SHARED, " SIZE_T_FMT ") failed",
-                      size);
+                      "mmap(/dev/zero, MAP_SHARED, %uz) failed", size);
         p = NULL;
     }
 
@@ -72,7 +70,7 @@
 
     if (id == -1) {
         ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
-                      "shmget(" SIZE_T_FMT ") failed", size);
+                      "shmget(%uz) failed", size);
         return NULL;
     }
 
diff --git a/src/os/unix/ngx_socket.h b/src/os/unix/ngx_socket.h
index 34449d0..c6eb941 100644
--- a/src/os/unix/ngx_socket.h
+++ b/src/os/unix/ngx_socket.h
@@ -15,7 +15,7 @@
 
 typedef int  ngx_socket_t;
 
-#define ngx_socket(af, type, proto, flags)   socket(af, type, proto)
+#define ngx_socket          socket
 #define ngx_socket_n        "socket()"
 
 
diff --git a/src/os/unix/ngx_solaris_sendfilev_chain.c b/src/os/unix/ngx_solaris_sendfilev_chain.c
index 9430dc0..21f8b8f 100644
--- a/src/os/unix/ngx_solaris_sendfilev_chain.c
+++ b/src/os/unix/ngx_solaris_sendfilev_chain.c
@@ -136,8 +136,7 @@
                 }
 
                 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, err,
-                              "sendfilev() sent only " SIZE_T_FMT " bytes",
-                              sent);
+                              "sendfilev() sent only %z bytes", sent);
 
             } else {
                 wev->error = 1;
@@ -147,7 +146,7 @@
         }
 
         ngx_log_debug2(NGX_LOG_DEBUG_EVENT, c->log, 0,
-                       "sendfilev: %d " SIZE_T_FMT, n, sent);
+                       "sendfilev: %z %z", n, sent);
 
         if (send - sprev == sent) {
             complete = 1;
diff --git a/src/os/unix/ngx_thread.h b/src/os/unix/ngx_thread.h
index 16e216e..ac7cfad 100644
--- a/src/os/unix/ngx_thread.h
+++ b/src/os/unix/ngx_thread.h
@@ -29,9 +29,9 @@
 #define ngx_log_tid                  (int) ngx_thread_self()
 
 #if (NGX_FREEBSD) && !(NGX_LINUXTHREADS)
-#define TID_T_FMT                    PTR_FMT
+#define NGX_TID_T_FMT                "%p"
 #else
-#define TID_T_FMT                    "%d"
+#define NGX_TID_T_FMT                "%d"
 #endif
 
 
@@ -92,11 +92,11 @@
 
 
 ngx_int_t ngx_init_threads(int n, size_t size, ngx_cycle_t *cycle);
-int ngx_create_thread(ngx_tid_t *tid, void* (*func)(void *arg), void *arg,
-                      ngx_log_t *log);
+ngx_err_t ngx_create_thread(ngx_tid_t *tid, void* (*func)(void *arg), void *arg,
+                            ngx_log_t *log);
 
 
-ngx_mutex_t *ngx_mutex_init(ngx_log_t *log, uint flags);
+ngx_mutex_t *ngx_mutex_init(ngx_log_t *log, ngx_uint_t flags);
 void ngx_mutex_destroy(ngx_mutex_t *m);
 
 
@@ -110,8 +110,8 @@
 
 #define ngx_thread_volatile
 
-#define ngx_log_tid  0
-#define TID_T_FMT    "%d"
+#define ngx_log_tid           0
+#define NGX_TID_T_FMT         "%d"
 
 #define ngx_mutex_trylock(m)  NGX_OK
 #define ngx_mutex_lock(m)     NGX_OK
diff --git a/src/os/unix/ngx_writev_chain.c b/src/os/unix/ngx_writev_chain.c
index e57b593..e389322 100644
--- a/src/os/unix/ngx_writev_chain.c
+++ b/src/os/unix/ngx_writev_chain.c
@@ -9,14 +9,14 @@
 #include <ngx_event.h>
 
 
-#define NGX_IOVS  8
+#define NGX_IOVS  16
 
 
 ngx_chain_t *ngx_writev_chain(ngx_connection_t *c, ngx_chain_t *in, off_t limit)
 {
     u_char        *prev;
-    ssize_t        n, size;
-    off_t          send, sprev, sent;
+    ssize_t        n, size, sent;
+    off_t          send, sprev;
     ngx_uint_t     eintr, complete;
     ngx_err_t      err;
     ngx_array_t    vec;
@@ -66,6 +66,12 @@
                 continue;
             }
 
+#if 1
+            if (!ngx_buf_in_memory(cl->buf)) {
+                ngx_debug_point();
+            }
+#endif
+
             size = cl->buf->last - cl->buf->pos;
 
             if (send + size > limit) {
@@ -110,8 +116,7 @@
 
         sent = n > 0 ? n : 0;
 
-        ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0,
-                       "writev: " OFF_T_FMT, sent);
+        ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, "writev: %z", sent);
 
         if (send - sprev == sent) {
             complete = 1;
@@ -119,7 +124,8 @@
 
         c->sent += sent;
 
-        for (cl = in; cl && sent > 0; cl = cl->next) {
+        for (cl = in; cl; cl = cl->next) {
+
             if (ngx_buf_special(cl->buf)) {
                 continue;
             }