diff options
Diffstat (limited to 'src/os/unix')
-rw-r--r-- | src/os/unix/ngx_errno.h | 21 | ||||
-rw-r--r-- | src/os/unix/ngx_sendfile.c | 65 | ||||
-rw-r--r-- | src/os/unix/ngx_sendfile.h | 17 | ||||
-rw-r--r-- | src/os/unix/ngx_sendv.c | 14 | ||||
-rw-r--r-- | src/os/unix/ngx_sendv.h | 14 | ||||
-rw-r--r-- | src/os/unix/ngx_stat.h | 19 | ||||
-rw-r--r-- | src/os/unix/ngx_time.c | 19 | ||||
-rw-r--r-- | src/os/unix/ngx_time.h | 22 | ||||
-rw-r--r-- | src/os/unix/ngx_types.h | 12 |
9 files changed, 203 insertions, 0 deletions
diff --git a/src/os/unix/ngx_errno.h b/src/os/unix/ngx_errno.h new file mode 100644 index 000000000..c14a977fa --- /dev/null +++ b/src/os/unix/ngx_errno.h @@ -0,0 +1,21 @@ +#ifndef _NGX_ERRNO_H_INCLUDED_ +#define _NGX_ERRNO_H_INCLUDED_ + + +#include <errno.h> +#include <string.h> + +typedef int ngx_err_t; + +#define NGX_ENOENT ENOENT +#define NGX_EINTR EINTR +#define NGX_EAGAIN EWOULDBLOCK + +#define ngx_errno errno +#define ngx_socket_errno errno + +#define ngx_strerror_r(err, errstr, size) \ + ngx_cpystrn(errstr, strerror(err), size) - (errstr) + + +#endif /* _NGX_ERRNO_H_INCLUDED_ */ diff --git a/src/os/unix/ngx_sendfile.c b/src/os/unix/ngx_sendfile.c new file mode 100644 index 000000000..2bc157361 --- /dev/null +++ b/src/os/unix/ngx_sendfile.c @@ -0,0 +1,65 @@ + +#include <ngx_config.h> +#include <ngx_types.h> +#include <ngx_errno.h> +#include <ngx_log.h> +#include <ngx_sendv.h> +#include <ngx_sendfile.h> + +/* + TODO: + FreeBSD: + check sent if errno == EINTR then should return right sent. +*/ + +/* + returns + 0 done + -1 error +*/ + +#if (HAVE_FREEBSD_SENDFILE) + +int ngx_sendfile(ngx_socket_t s, + ngx_iovec_t *headers, int hdr_cnt, + ngx_file_t fd, off_t offset, size_t nbytes, + ngx_iovec_t *trailers, int trl_cnt, + off_t *sent, + ngx_log_t *log) +{ + int rc, i; + ngx_err_t err; + struct sf_hdtr hdtr; + + hdtr.headers = headers; + hdtr.hdr_cnt = hdr_cnt; + hdtr.trailers = trailers; + hdtr.trl_cnt = trl_cnt; + +#if (HAVE_FREEBSD_SENDFILE_NBYTES_BUG) + for (i = 0; i < hdr_cnt; i++) + nbytes += headers[i].iov_len; +#endif + + rc = sendfile(fd, s, offset, nbytes, &hdtr, sent, 0); + + if (rc == -1) { + err = ngx_socket_errno; + if (err != NGX_EAGAIN && err != NGX_EINTR) { + ngx_log_error(NGX_LOG_ERR, log, err, + "ngx_sendfile: sendfile failed"); + return -1; + + } else { + ngx_log_error(NGX_LOG_INFO, log, err, + "ngx_sendfile: sendfile sent only %qd bytes", *sent); + } + } + + ngx_log_debug(log, "ngx_sendfile: %d, @%qd %d:%qd" _ + rc _ offset _ nbytes _ *sent); + + return 0; +} + +#endif diff --git a/src/os/unix/ngx_sendfile.h b/src/os/unix/ngx_sendfile.h new file mode 100644 index 000000000..a347c6c50 --- /dev/null +++ b/src/os/unix/ngx_sendfile.h @@ -0,0 +1,17 @@ +#ifndef _NGX_SENDFILE_H_INCLUDED_ +#define _NGX_SENDFILE_H_INCLUDED_ + + +#include <ngx_types.h> +#include <ngx_log.h> +#include <ngx_sendv.h> + +int ngx_sendfile(ngx_socket_t s, + ngx_iovec_t *headers, int hdr_cnt, + ngx_file_t fd, off_t offset, size_t nbytes, + ngx_iovec_t *trailers, int trl_cnt, + off_t *sent, + ngx_log_t *log); + + +#endif /* _NGX_SENDFILE_H_INCLUDED_ */ diff --git a/src/os/unix/ngx_sendv.c b/src/os/unix/ngx_sendv.c new file mode 100644 index 000000000..bd95d7a7d --- /dev/null +++ b/src/os/unix/ngx_sendv.c @@ -0,0 +1,14 @@ + +#include <ngx_types.h> +#include <ngx_sendv.h> + +ssize_t ngx_sendv(ngx_socket_t s, ngx_iovec_t *iovec, int n, size_t *sent) +{ + ssize_t rc = writev(s, iovec, n); + + if (rc == -1) + return -1; + + *sent = rc; + return 0; +} diff --git a/src/os/unix/ngx_sendv.h b/src/os/unix/ngx_sendv.h new file mode 100644 index 000000000..16c24039b --- /dev/null +++ b/src/os/unix/ngx_sendv.h @@ -0,0 +1,14 @@ +#ifndef _NGX_SENDV_H_INCLUDED_ +#define _NGX_SENDV_H_INCLUDED_ + + +#include <ngx_types.h> + +typedef struct iovec ngx_iovec_t; +#define ngx_iov_base iov_base +#define ngx_iov_len iov_len + +ssize_t ngx_sendv(ngx_socket_t s, ngx_iovec_t *iovec, int n, size_t *sent); + + +#endif /* _NGX_SENDV_H_INCLUDED_ */ diff --git a/src/os/unix/ngx_stat.h b/src/os/unix/ngx_stat.h new file mode 100644 index 000000000..f42edff13 --- /dev/null +++ b/src/os/unix/ngx_stat.h @@ -0,0 +1,19 @@ +#ifndef _NGX_STAT_H_INCLUDED_ +#define _NGX_STAT_H_INCLUDED_ + + +#include <sys/types.h> +#include <sys/stat.h> + +typedef struct stat ngx_stat_t; + +#define ngx_is_dir(sb) (S_ISDIR(sb.st_mode)) + +#define ngx_stat(file, sb) stat(file, sb) +#define ngx_stat_n "stat" + +#define ngx_fstat(file, fd, sb) fstat(fd, sb) +#define ngx_fstat_n "stat" + + +#endif /* _NGX_STAT_H_INCLUDED_ */ diff --git a/src/os/unix/ngx_time.c b/src/os/unix/ngx_time.c new file mode 100644 index 000000000..d4be966dd --- /dev/null +++ b/src/os/unix/ngx_time.c @@ -0,0 +1,19 @@ + +#include <ngx_config.h> +#include <ngx_time.h> + +void ngx_localtime(ngx_tm_t *tm) +{ + time_t clock = time(NULL); + localtime_r(&clock, tm); +} + +u_int ngx_msec(void) +{ + struct timeval tv; + + gettimeofday(&tv, NULL); + + return tv.tv_sec * 1000 + tv.tv_usec / 1000; +} + diff --git a/src/os/unix/ngx_time.h b/src/os/unix/ngx_time.h new file mode 100644 index 000000000..b262da7cf --- /dev/null +++ b/src/os/unix/ngx_time.h @@ -0,0 +1,22 @@ +#ifndef _NGX_TIME_H_INCLUDED_ +#define _NGX_TIME_H_INCLUDED_ + + +#include <ngx_config.h> + +typedef struct tm ngx_tm_t; + +#define ngx_tm_sec tm_sec +#define ngx_tm_min tm_min +#define ngx_tm_hour tm_hour +#define ngx_tm_mday tm_mday +#define ngx_tm_mon tm_mon +#define ngx_tm_year tm_year +#define ngx_tm_wday tm_wday + +void ngx_localtime(ngx_tm_t *tm); + +u_int ngx_msec(void); + + +#endif /* _NGX_TIME_H_INCLUDED_ */ diff --git a/src/os/unix/ngx_types.h b/src/os/unix/ngx_types.h new file mode 100644 index 000000000..ebcbb1fac --- /dev/null +++ b/src/os/unix/ngx_types.h @@ -0,0 +1,12 @@ +#ifndef _NGX_TYPES_H_INCLUDED_ +#define _NGX_TYPES_H_INCLUDED_ + + +#include <ngx_config.h> + + +typedef int ngx_file_t; +typedef int ngx_socket_t; + + +#endif /* _NGX_TYPES_H_INCLUDED_ */ |