nginx-0.0.1-2003-05-29-17:02:09 import
diff --git a/src/os/unix/ngx_errno.h b/src/os/unix/ngx_errno.h
index b980281..6006471 100644
--- a/src/os/unix/ngx_errno.h
+++ b/src/os/unix/ngx_errno.h
@@ -24,6 +24,8 @@
 #define ngx_socket_errno           errno
 #define ngx_set_socket_errno(err)  errno = err
 
+#define ngx_strerror(err)          strerror(err)
+
 #define ngx_strerror_r(err, errstr, size)  \
              ngx_cpystrn(errstr, strerror(err), size) - (errstr)
 
diff --git a/src/os/unix/ngx_freebsd_init.c b/src/os/unix/ngx_freebsd_init.c
index 100dfc4..cacc457 100644
--- a/src/os/unix/ngx_freebsd_init.c
+++ b/src/os/unix/ngx_freebsd_init.c
@@ -2,6 +2,7 @@
 #include <ngx_freebsd_init.h>
 
 
+/* FreeBSD 3.4 at least */
 char ngx_freebsd_kern_ostype[20];
 char ngx_freebsd_kern_osrelease[20];
 int ngx_freebsd_kern_osreldate;
@@ -9,6 +10,9 @@
 int ngx_freebsd_net_inet_tcp_sendspace;
 int ngx_freebsd_sendfile_nbytes_bug;
 
+/* FreeBSD 5.0 */
+int ngx_freebsd_kern_ipc_zero_copy_send;
+
 
 ngx_os_io_t ngx_os_io = {
     ngx_unix_recv,
@@ -19,9 +23,35 @@
 };
 
 
+typedef struct {
+    char    *name;
+    int     *value;
+    size_t   size;
+} sysctl_t;
+
+
+sysctl_t sysctls[] = {
+    {"hw.ncpu",
+     &ngx_freebsd_hw_ncpu,
+     sizeof(int)},
+
+    {"net.inet.tcp.sendspace",
+     &ngx_freebsd_net_inet_tcp_sendspace,
+     sizeof(int)},
+
+    {"kern.ipc.zero_copy.send",
+     &ngx_freebsd_kern_ipc_zero_copy_send,
+     sizeof(int)},
+
+    {NULL, NULL, 0}
+};
+
+
 int ngx_os_init(ngx_log_t *log)
 {
-    size_t  size;
+    int        i;
+    size_t     size;
+    ngx_err_t  err;
 
     size = 20;
     if (sysctlbyname("kern.ostype",
@@ -85,28 +115,23 @@
 #endif /* HAVE_FREEBSD_SENDFILE */
 
 
-    size = 4;
-    if (sysctlbyname("hw.ncpu", &ngx_freebsd_hw_ncpu, &size, NULL, 0) == -1) {
-        ngx_log_error(NGX_LOG_ALERT, log, errno,
-                      "sysctlbyname(hw.ncpu) failed");
-        return NGX_ERROR;
+    for (i = 0; sysctls[i].name; i++) {
+        *sysctls[i].value = 0;
+        size = sysctls[i].size;
+        if (sysctlbyname(sysctls[i].name, sysctls[i].value, &size, NULL, 0)
+                                                                       == -1) {
+            err = errno;
+            if (err != NGX_ENOENT) {
+                ngx_log_error(NGX_LOG_ALERT, log, err,
+                              "sysctlbyname(%s) failed", sysctls[i].name);
+                return NGX_ERROR;
+            }
+
+        } else {
+            ngx_log_error(NGX_LOG_INFO, log, 0, "%s: %d",
+                          sysctls[i].name, *sysctls[i].value);
+        }
     }
 
-    ngx_log_error(NGX_LOG_INFO, log, 0, "hw.ncpu: %d", ngx_freebsd_hw_ncpu);
-
-
-    size = 4;
-    if (sysctlbyname("net.inet.tcp.sendspace",
-                     &ngx_freebsd_net_inet_tcp_sendspace,
-                     &size, NULL, 0) == -1)
-    {
-        ngx_log_error(NGX_LOG_ALERT, log, errno,
-                      "sysctlbyname(net.inet.tcp.sendspace) failed");
-        return NGX_ERROR;
-    }
-
-    ngx_log_error(NGX_LOG_INFO, log, 0, "net.inet.tcp.sendspace: %d",
-                  ngx_freebsd_net_inet_tcp_sendspace);
-
     return ngx_posix_init(log);
 }
diff --git a/src/os/unix/ngx_linux_config.h b/src/os/unix/ngx_linux_config.h
index e4c5dd0..cfd9413 100644
--- a/src/os/unix/ngx_linux_config.h
+++ b/src/os/unix/ngx_linux_config.h
@@ -26,6 +26,7 @@
 #include <sys/socket.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>
+#include <netdb.h>
 
 
 typedef unsigned int   u_int;
diff --git a/src/os/unix/ngx_linux_sendfile_chain.c b/src/os/unix/ngx_linux_sendfile_chain.c
new file mode 100644
index 0000000..6ea67f4
--- /dev/null
+++ b/src/os/unix/ngx_linux_sendfile_chain.c
@@ -0,0 +1,183 @@
+
+#include <ngx_config.h>
+#include <ngx_core.h>
+#include <ngx_linux_init.h>
+
+
+ngx_chain_t *ngx_linux_sendfile_chain(ngx_connection_t *c, ngx_chain_t *in)
+{
+    int              rc, on, off;
+    char            *prev;
+    size_t           hsize, size;
+    ssize_t          sent;
+    struct iovec    *iov;
+    struct sf_hdtr   hdtr;
+    ngx_err_t        err;
+    ngx_array_t      header, trailer;
+    ngx_hunk_t      *file;
+    ngx_chain_t     *ce;
+
+    ce = in;
+    file = NULL;
+    hsize = 0;
+
+    on = 1;
+    off = 0;
+
+    ngx_init_array(header, c->pool, 10, sizeof(struct iovec), NGX_CHAIN_ERROR);
+    ngx_init_array(trailer, c->pool, 10, sizeof(struct iovec), NGX_CHAIN_ERROR);
+
+    /* create the header iovec */
+    if (ngx_hunk_in_memory_only(ce->hunk)) {
+        prev = NULL;
+        iov = NULL;
+
+        /* create the iovec and coalesce the neighbouring chain entries */
+        while (ce && ngx_hunk_in_memory_only(ce->hunk)) {
+
+            if (prev == ce->hunk->pos) {
+                iov->iov_len += ce->hunk->last - ce->hunk->pos;
+                prev = ce->hunk->last;
+
+            } else {
+                ngx_test_null(iov, ngx_push_array(&header), NGX_CHAIN_ERROR);
+                iov->iov_base = ce->hunk->pos;
+                iov->iov_len = ce->hunk->last - ce->hunk->pos;
+                prev = ce->hunk->last;
+            }
+
+            if (ngx_freebsd_sendfile_nbytes_bug) {
+                hsize += ce->hunk->last - ce->hunk->pos;
+            }
+
+            ce = ce->next;
+        }
+    }
+
+    /* TODO: coalesce the neighbouring file hunks */
+    if (ce && (ce->hunk->type & NGX_HUNK_FILE)) {
+        file = ce->hunk;
+        ce = ce->next;
+    }
+
+    /* create the trailer iovec */
+    if (ce && ngx_hunk_in_memory_only(ce->hunk)) {
+        prev = NULL;
+        iov = NULL;
+
+        /* create the iovec and coalesce the neighbouring chain entries */
+        while (ce && ngx_hunk_in_memory_only(ce->hunk)) {
+
+            if (prev == ce->hunk->pos) {
+                iov->iov_len += ce->hunk->last - ce->hunk->pos;
+                prev = ce->hunk->last;
+
+            } else {
+                ngx_test_null(iov, ngx_push_array(&trailer), NGX_CHAIN_ERROR);
+                iov->iov_base = ce->hunk->pos;
+                iov->iov_len = ce->hunk->last - ce->hunk->pos;
+                prev = ce->hunk->last;
+            }
+
+            ce = ce->next;
+        }
+    }
+
+    if (file) {
+        if (setsockopt(c->fd, IPPROTO_TCP, TCP_CORK,
+                       (const void *) &on, sizeof(int)) == -1) {
+            ngx_log_error(NGX_LOG_CRIT, c->log, err,
+                          "setsockopt(TCP_CORK, 1) failed");
+            return NGX_CHAIN_ERROR;
+        }
+
+
+        rc = sendfile(c->fd, file->file->fd, file->file_pos,
+                        (size_t) (file->file_last - file->file_pos));
+
+        if (rc == -1) {
+            err = ngx_errno;
+            if (err == NGX_EAGAIN) {
+                ngx_log_error(NGX_LOG_INFO, c->log, err, "senfile() EAGAIN");
+
+            } else if (err == NGX_EINTR) {
+                ngx_log_error(NGX_LOG_INFO, c->log, err, "senfile() EINTR");
+
+            } else {
+                ngx_log_error(NGX_LOG_CRIT, c->log, err, "sendfile() failed");
+                return NGX_CHAIN_ERROR;
+            }
+        }
+
+        sent = rc > 0 ? rc : 0;
+
+#if (NGX_DEBUG_WRITE_CHAIN)
+        ngx_log_debug(c->log, "sendfile: %d, @%qd %d:%d" _
+                      rc _ file->file_pos _ sent _
+                      (size_t) (file->file_last - file->file_pos));
+#endif
+
+    } else {
+        rc = writev(c->fd, (struct iovec *) header.elts, header.nelts);
+
+        if (rc == -1) {
+            err = ngx_errno;
+            if (err == NGX_EAGAIN) {
+                ngx_log_error(NGX_LOG_INFO, c->log, err, "writev() EAGAIN");
+
+            } else if (err == NGX_EINTR) {
+                ngx_log_error(NGX_LOG_INFO, c->log, err, "writev() EINTR");
+
+            } else {
+                ngx_log_error(NGX_LOG_CRIT, c->log, err, "writev() failed");
+                return NGX_CHAIN_ERROR;
+            }
+        }
+
+        sent = rc > 0 ? rc : 0;
+
+#if (NGX_DEBUG_WRITE_CHAIN)
+        ngx_log_debug(c->log, "writev: %d" _ sent);
+#endif
+    }
+
+    c->sent += sent;
+
+    for (ce = in; ce && sent > 0; ce = ce->next) {
+
+        if (ce->hunk->type & NGX_HUNK_IN_MEMORY) {
+            size = ce->hunk->last - ce->hunk->pos;
+        } else {
+            size = ce->hunk->file_last - ce->hunk->file_pos;
+        }
+
+        if (sent >= size) {
+            sent -= size;
+
+            if (ce->hunk->type & NGX_HUNK_IN_MEMORY) {
+                ce->hunk->pos = ce->hunk->last;
+            }
+
+            if (ce->hunk->type & NGX_HUNK_FILE) {
+                ce->hunk->file_pos = ce->hunk->file_last;
+            }
+
+            continue;
+        }
+
+        if (ce->hunk->type & NGX_HUNK_IN_MEMORY) {
+            ce->hunk->pos += sent;
+        }
+
+        if (ce->hunk->type & NGX_HUNK_FILE) {
+            ce->hunk->file_pos += sent;
+        }
+
+        break;
+    }
+
+    ngx_destroy_array(&trailer);
+    ngx_destroy_array(&header);
+
+    return ce;
+}
diff --git a/src/os/unix/ngx_solaris_config.h b/src/os/unix/ngx_solaris_config.h
index 786cf0b..1977501 100644
--- a/src/os/unix/ngx_solaris_config.h
+++ b/src/os/unix/ngx_solaris_config.h
@@ -22,6 +22,7 @@
 #include <sys/socket.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>
+#include <netdb.h>
 
 
 typedef uint32_t  u_int32_t;