http listen unix domain sockets
diff --git a/src/core/ngx_connection.c b/src/core/ngx_connection.c
index 01c2058..046d927 100644
--- a/src/core/ngx_connection.c
+++ b/src/core/ngx_connection.c
@@ -15,6 +15,7 @@
ngx_listening_t *
ngx_create_listening(ngx_conf_t *cf, void *sockaddr, socklen_t socklen)
{
+ size_t len;
ngx_listening_t *ls;
struct sockaddr *sa;
u_char text[NGX_SOCKADDR_STRLEN];
@@ -36,17 +37,8 @@
ls->sockaddr = sa;
ls->socklen = socklen;
- ls->addr_text.len = ngx_sock_ntop(sa, text, NGX_SOCKADDR_STRLEN, 1);
-
- ls->addr_text.data = ngx_pnalloc(cf->pool, ls->addr_text.len);
- if (ls->addr_text.data == NULL) {
- return NULL;
- }
-
- ngx_memcpy(ls->addr_text.data, text, ls->addr_text.len);
-
- ls->fd = (ngx_socket_t) -1;
- ls->type = SOCK_STREAM;
+ len = ngx_sock_ntop(sa, text, NGX_SOCKADDR_STRLEN, 1);
+ ls->addr_text.len = len;
switch (ls->sockaddr->sa_family) {
#if (NGX_HAVE_INET6)
@@ -54,6 +46,12 @@
ls->addr_text_max_len = NGX_INET6_ADDRSTRLEN;
break;
#endif
+#if (NGX_HAVE_UNIX_DOMAIN)
+ case AF_UNIX:
+ ls->addr_text_max_len = NGX_UNIX_ADDRSTRLEN;
+ len++;
+ break;
+#endif
case AF_INET:
ls->addr_text_max_len = NGX_INET_ADDRSTRLEN;
break;
@@ -62,6 +60,16 @@
break;
}
+ ls->addr_text.data = ngx_pnalloc(cf->pool, len);
+ if (ls->addr_text.data == NULL) {
+ return NULL;
+ }
+
+ ngx_memcpy(ls->addr_text.data, text, len);
+
+ ls->fd = (ngx_socket_t) -1;
+ ls->type = SOCK_STREAM;
+
ls->backlog = NGX_LISTEN_BACKLOG;
ls->rcvbuf = -1;
ls->sndbuf = -1;
@@ -604,6 +612,21 @@
ngx_close_socket_n " %V failed", &ls[i].addr_text);
}
+#if (NGX_HAVE_UNIX_DOMAIN)
+
+ if (ls[i].sockaddr->sa_family == AF_UNIX
+ && ngx_process != NGX_PROCESS_WORKER)
+ {
+ u_char *name = ls[i].addr_text.data + sizeof("unix:") - 1;
+
+ if (ngx_delete_file(name) == -1) {
+ ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_socket_errno,
+ ngx_delete_file_n " %s failed", name);
+ }
+ }
+
+#endif
+
ls[i].fd = (ngx_socket_t) -1;
}
}
diff --git a/src/core/ngx_inet.c b/src/core/ngx_inet.c
index 4c18036..5702dba 100644
--- a/src/core/ngx_inet.c
+++ b/src/core/ngx_inet.c
@@ -68,6 +68,9 @@
size_t n;
struct sockaddr_in6 *sin6;
#endif
+#if (NGX_HAVE_UNIX_DOMAIN)
+ struct sockaddr_un *saun;
+#endif
switch (sa->sa_family) {
@@ -108,6 +111,17 @@
return n;
#endif
+#if (NGX_HAVE_UNIX_DOMAIN)
+
+ case AF_UNIX:
+ saun = (struct sockaddr_un *) sa;
+
+ /* we do not include trailing zero in address length */
+
+ return ngx_snprintf(text, len, "unix:%s%Z", saun->sun_path) - text - 1;
+
+#endif
+
default:
return 0;
}
diff --git a/src/core/ngx_inet.h b/src/core/ngx_inet.h
index 7562c9e..c1e68c8 100644
--- a/src/core/ngx_inet.h
+++ b/src/core/ngx_inet.h
@@ -12,21 +12,26 @@
#include <ngx_core.h>
-#define NGX_INET_ADDRSTRLEN (sizeof("255.255.255.255") - 1)
-#define NGX_INET6_ADDRSTRLEN \
- (sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255") - 1)
-
-#define NGX_SOCKADDR_STRLEN (NGX_INET6_ADDRSTRLEN + sizeof(":65535") - 1)
-
-
/*
- * TODO: autoconfigure NGX_SOCKADDRLEN as
+ * TODO: autoconfigure NGX_SOCKADDRLEN and NGX_SOCKADDR_STRLEN as
* sizeof(struct sockaddr_storage)
* sizeof(struct sockaddr_un)
* sizeof(struct sockaddr_in6)
* sizeof(struct sockaddr_in)
*/
+#define NGX_INET_ADDRSTRLEN (sizeof("255.255.255.255") - 1)
+#define NGX_INET6_ADDRSTRLEN \
+ (sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255") - 1)
+#define NGX_UNIX_ADDRSTRLEN \
+ (sizeof(struct sockaddr_un) - offsetof(struct sockaddr_un, sun_path))
+
+#if (NGX_HAVE_UNIX_DOMAIN)
+#define NGX_SOCKADDR_STRLEN (sizeof("unix:") - 1 + NGX_UNIX_ADDRSTRLEN)
+#else
+#define NGX_SOCKADDR_STRLEN (NGX_INET6_ADDRSTRLEN + sizeof(":65535") - 1)
+#endif
+
#if (NGX_HAVE_UNIX_DOMAIN)
#define NGX_SOCKADDRLEN sizeof(struct sockaddr_un)
#else