From f44e38f148e7a1fde7b17ef6f0e22c092dee89e6 Mon Sep 17 00:00:00 2001
From: Andy Green <andy@warmcat.com>
Date: Thu, 2 Aug 2018 19:13:53 +0800
Subject: [PATCH] unix socket: fixes and improvements
Auto-remove any unix socket file already there.
Correctly identify if it's in use per-vhost.
Make the peer-limits stuff ignore it.
---
lib/core/context.c | 7 ++++---
lib/core/libwebsockets.c | 12 ++++++++++--
lib/misc/peer-limits.c | 3 +++
lib/plat/unix/unix-init.c | 2 +-
lib/roles/http/server/lejp-conf.c | 7 ++++---
5 files changed, 22 insertions(+), 9 deletions(-)
diff --git a/lib/core/context.c b/lib/core/context.c
index c7df2628..34d5346c 100644
--- a/lib/core/context.c
+++ b/lib/core/context.c
@@ -877,7 +877,7 @@ lws_create_vhost(struct lws_context *context,
#endif
#ifdef LWS_WITH_UNIX_SOCK
- if (LWS_UNIX_SOCK_ENABLED(context)) {
+ if (LWS_UNIX_SOCK_ENABLED(vh)) {
lwsl_notice("Creating Vhost '%s' path \"%s\", %d protocols\n",
vh->name, vh->iface, vh->count_protocols);
} else
@@ -1424,16 +1424,17 @@ lws_create_context(const struct lws_context_creation_info *info)
return NULL;
}
-
#if defined(LWS_WITH_PEER_LIMITS)
/* scale the peer hash table according to the max fds for the process,
* so that the max list depth averages 16. Eg, 1024 fd -> 64,
* 102400 fd -> 6400
*/
+
context->pl_hash_elements =
(context->count_threads * context->fd_limit_per_thread) / 16;
context->pl_hash_table = lws_zalloc(sizeof(struct lws_peer *) *
context->pl_hash_elements, "peer limits hash table");
+
context->ip_limit_ah = info->ip_limit_ah;
context->ip_limit_wsi = info->ip_limit_wsi;
#endif
@@ -1812,7 +1813,7 @@ __lws_vhost_destroy2(struct lws_vhost *vh)
#endif
#if defined(LWS_WITH_UNIX_SOCK)
- if (LWS_UNIX_SOCK_ENABLED(context)) {
+ if (LWS_UNIX_SOCK_ENABLED(vh)) {
n = unlink(vh->iface);
if (n)
lwsl_info("Closing unix socket %s: errno %d\n",
diff --git a/lib/core/libwebsockets.c b/lib/core/libwebsockets.c
index 413bddff..de932517 100644
--- a/lib/core/libwebsockets.c
+++ b/lib/core/libwebsockets.c
@@ -2488,7 +2488,7 @@ lws_socket_bind(struct lws_vhost *vhost, lws_sockfd_type sockfd, int port,
struct sockaddr_storage sin;
struct sockaddr *v;
-#ifdef LWS_WITH_UNIX_SOCK
+#if defined(LWS_WITH_UNIX_SOCK)
if (LWS_UNIX_SOCK_ENABLED(vhost)) {
v = (struct sockaddr *)&serv_unix;
n = sizeof(struct sockaddr_un);
@@ -2504,6 +2504,8 @@ lws_socket_bind(struct lws_vhost *vhost, lws_sockfd_type sockfd, int port,
strcpy(serv_unix.sun_path, iface);
if (serv_unix.sun_path[0] == '@')
serv_unix.sun_path[0] = '\0';
+ else
+ unlink(serv_unix.sun_path);
} else
#endif
@@ -2566,7 +2568,7 @@ lws_socket_bind(struct lws_vhost *vhost, lws_sockfd_type sockfd, int port,
#ifdef LWS_WITH_UNIX_SOCK
if (n < 0 && LWS_UNIX_SOCK_ENABLED(vhost)) {
lwsl_err("ERROR on binding fd %d to \"%s\" (%d %d)\n",
- sockfd, iface, n, LWS_ERRNO);
+ sockfd, iface, n, LWS_ERRNO);
return -1;
} else
#endif
@@ -2576,6 +2578,12 @@ lws_socket_bind(struct lws_vhost *vhost, lws_sockfd_type sockfd, int port,
return -1;
}
+#if defined(LWS_WITH_UNIX_SOCK)
+ if (LWS_UNIX_SOCK_ENABLED(vhost) && vhost->context->uid) {
+ chown(serv_unix.sun_path, vhost->context->uid, vhost->context->gid);
+ }
+#endif
+
#ifndef LWS_PLAT_OPTEE
if (getsockname(sockfd, (struct sockaddr *)&sin, &len) == -1)
lwsl_warn("getsockname: %s\n", strerror(LWS_ERRNO));
diff --git a/lib/misc/peer-limits.c b/lib/misc/peer-limits.c
index 1f1ef4d3..8ccf3fdb 100644
--- a/lib/misc/peer-limits.c
+++ b/lib/misc/peer-limits.c
@@ -64,6 +64,9 @@ lws_get_or_create_peer(struct lws_vhost *vhost, lws_sockfd_type sockfd)
int n, af = AF_INET;
struct sockaddr_storage addr;
+ if (vhost->options & LWS_SERVER_OPTION_UNIX_SOCK)
+ return NULL;
+
#ifdef LWS_WITH_IPV6
if (LWS_IPV6_ENABLED(vhost)) {
af = AF_INET6;
diff --git a/lib/plat/unix/unix-init.c b/lib/plat/unix/unix-init.c
index a71718e7..fa9a30e8 100644
--- a/lib/plat/unix/unix-init.c
+++ b/lib/plat/unix/unix-init.c
@@ -57,7 +57,7 @@ lws_plat_init(struct lws_context *context,
}
#ifdef LWS_WITH_PLUGINS
- if (info->plugin_dirs)
+ if (info->plugin_dirs && (context->options & LWS_SERVER_OPTION_LIBUV))
lws_plat_plugins_init(context, info->plugin_dirs);
#endif
diff --git a/lib/roles/http/server/lejp-conf.c b/lib/roles/http/server/lejp-conf.c
index f9ac68a5..dc49bf61 100644
--- a/lib/roles/http/server/lejp-conf.c
+++ b/lib/roles/http/server/lejp-conf.c
@@ -1,7 +1,7 @@
/*
* libwebsockets web server application
*
- * Copyright (C) 2010-2017 Andy Green <andy@warmcat.com>
+ * Copyright (C) 2010-2018 Andy Green <andy@warmcat.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
@@ -442,8 +442,9 @@ lejp_vhosts_cb(struct lejp_ctx *ctx, char reason)
struct lws_vhost *vhost;
//lwsl_notice("%s\n", ctx->path);
- if (!a->info->port) {
- lwsl_err("Port required (eg, 443)");
+ if (!a->info->port &&
+ !(a->info->options & LWS_SERVER_OPTION_UNIX_SOCK)) {
+ lwsl_err("Port required (eg, 443)\n");
return 1;
}
a->valid = 0;
--
GitLab