Update Files
This commit is contained in:
115
Kinc/Sources/kinc/libs/plat/unix/CMakeLists.txt
Normal file
115
Kinc/Sources/kinc/libs/plat/unix/CMakeLists.txt
Normal file
@ -0,0 +1,115 @@
|
||||
#
|
||||
# libwebsockets - small server side websockets and web server implementation
|
||||
#
|
||||
# Copyright (C) 2010 - 2020 Andy Green <andy@warmcat.com>
|
||||
#
|
||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
# of this software and associated documentation files (the "Software"), to
|
||||
# deal in the Software without restriction, including without limitation the
|
||||
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
# sell copies of the Software, and to permit persons to whom the Software is
|
||||
# furnished to do so, subject to the following conditions:
|
||||
#
|
||||
# The above copyright notice and this permission notice shall be included in
|
||||
# all copies or substantial portions of the Software.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
# IN THE SOFTWARE.
|
||||
#
|
||||
# The strategy is to only export to PARENT_SCOPE
|
||||
#
|
||||
# - changes to LIB_LIST
|
||||
# - changes to SOURCES
|
||||
# - includes via include_directories
|
||||
#
|
||||
# and keep everything else private
|
||||
|
||||
include_directories(.)
|
||||
|
||||
execute_process( COMMAND grep -c illumos /lib/ld.so.1
|
||||
OUTPUT_VARIABLE ILLUMOS ERROR_QUIET )
|
||||
# Chomp the \n at end of output.
|
||||
string(REGEX REPLACE "[\n]+" "" ILLUMOS "${ILLUMOS}")
|
||||
|
||||
if (NOT ${ILLUMOS} MATCHES "0")
|
||||
set(ILLUMOS 1)
|
||||
endif()
|
||||
|
||||
set(LWS_PLAT_UNIX 1)
|
||||
list(APPEND SOURCES
|
||||
plat/unix/unix-caps.c
|
||||
plat/unix/unix-misc.c
|
||||
plat/unix/unix-init.c
|
||||
)
|
||||
if (LWS_WITH_FILE_OPS)
|
||||
list(APPEND SOURCES plat/unix/unix-file.c)
|
||||
endif()
|
||||
if (LWS_WITH_NETWORK)
|
||||
list(APPEND SOURCES
|
||||
plat/unix/unix-pipe.c
|
||||
plat/unix/unix-service.c
|
||||
plat/unix/unix-sockets.c
|
||||
plat/unix/unix-fds.c
|
||||
)
|
||||
if (LWS_WITH_SYS_ASYNC_DNS)
|
||||
if (LWS_PLAT_ANDROID)
|
||||
list(APPEND SOURCES plat/unix/android/android-resolv.c)
|
||||
else()
|
||||
list(APPEND SOURCES plat/unix/unix-resolv.c)
|
||||
endif()
|
||||
endif()
|
||||
if (LWS_HAVE_SYSTEMD_H)
|
||||
list(APPEND SOURCES
|
||||
plat/unix/unix-systemd.c
|
||||
)
|
||||
list(APPEND LIB_LIST_AT_END systemd)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
if (LWS_WITH_PLUGINS_API)
|
||||
list(APPEND SOURCES plat/unix/unix-plugins.c)
|
||||
endif()
|
||||
|
||||
if (LWS_WITH_SPAWN)
|
||||
list(APPEND SOURCES plat/unix/unix-spawn.c)
|
||||
endif()
|
||||
|
||||
if (HAIKU)
|
||||
set(CMAKE_REQUIRED_LIBRARIES network)
|
||||
list(APPEND LIB_LIST_AT_END network)
|
||||
endif()
|
||||
|
||||
IF (CMAKE_SYSTEM_NAME STREQUAL Linux AND NOT LWS_WITHOUT_EVENTFD)
|
||||
CHECK_FUNCTION_EXISTS(eventfd_read LWS_HAVE_EVENTFD)
|
||||
endif()
|
||||
|
||||
list(APPEND LIB_LIST_AT_END m)
|
||||
|
||||
if (ILLUMOS)
|
||||
list(APPEND LIB_LIST_AT_END socket)
|
||||
endif()
|
||||
|
||||
if (LWS_HAVE_LIBCAP)
|
||||
list(APPEND LIB_LIST_AT_END cap)
|
||||
endif()
|
||||
|
||||
if (${CMAKE_SYSTEM_NAME} MATCHES "QNX")
|
||||
list(APPEND LIB_LIST_AT_END socket)
|
||||
endif()
|
||||
|
||||
list(APPEND LIB_LIST_AT_END ${CMAKE_DL_LIBS})
|
||||
|
||||
#
|
||||
# Keep explicit parent scope exports at end
|
||||
#
|
||||
|
||||
exports_to_parent_scope()
|
||||
set(LWS_PLAT_UNIX ${LWS_PLAT_UNIX} PARENT_SCOPE)
|
||||
set(ILLUMOS ${ILLUMOS} PARENT_SCOPE)
|
||||
set(LIB_LIST_AT_END ${LIB_LIST_AT_END} PARENT_SCOPE)
|
||||
set(LWS_PLAT_UNIX ${LWS_PLAT_UNIX} PARENT_SCOPE)
|
59
Kinc/Sources/kinc/libs/plat/unix/android/android-resolv.c
Normal file
59
Kinc/Sources/kinc/libs/plat/unix/android/android-resolv.c
Normal file
@ -0,0 +1,59 @@
|
||||
/*
|
||||
* libwebsockets - small server side websockets and web server implementation
|
||||
*
|
||||
* Copyright (C) 2010 - 2021 Andy Green <andy@warmcat.com>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to
|
||||
* deal in the Software without restriction, including without limitation the
|
||||
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
* sell copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
* IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "private-lib-core.h"
|
||||
#include "private-lib-async-dns.h"
|
||||
#include <sys/system_properties.h>
|
||||
|
||||
lws_async_dns_server_check_t
|
||||
lws_plat_asyncdns_init(struct lws_context *context, lws_async_dns_t *dns)
|
||||
{
|
||||
lws_async_dns_server_check_t s = LADNS_CONF_SERVER_SAME;
|
||||
char prop[PROP_VALUE_MAX], netdns[9];
|
||||
lws_async_dns_server_t *dsrv;
|
||||
lws_sockaddr46 sa46t;
|
||||
int n;
|
||||
|
||||
strcpy(netdns, "net.dns1");
|
||||
for (n = 0; n < 4; n++) {
|
||||
|
||||
prop[0] = '\0';
|
||||
if (__system_property_get(netdns, prop) <= 0)
|
||||
continue;
|
||||
|
||||
netdns[7]++; /* net.dns2... etc */
|
||||
|
||||
memset(&sa46t, 0, sizeof(sa46t));
|
||||
if (lws_sa46_parse_numeric_address(prop, &sa46t) < 0)
|
||||
continue;
|
||||
|
||||
dsrv = __lws_async_dns_server_find(dns, &sa46t);
|
||||
if (!dsrv) {
|
||||
__lws_async_dns_server_add(dns, &sa46t);
|
||||
s = LADNS_CONF_SERVER_CHANGED;
|
||||
}
|
||||
}
|
||||
|
||||
return s;
|
||||
}
|
204
Kinc/Sources/kinc/libs/plat/unix/private-lib-plat-unix.h
Normal file
204
Kinc/Sources/kinc/libs/plat/unix/private-lib-plat-unix.h
Normal file
@ -0,0 +1,204 @@
|
||||
/*
|
||||
* libwebsockets - small server side websockets and web server implementation
|
||||
*
|
||||
* Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to
|
||||
* deal in the Software without restriction, including without limitation the
|
||||
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
* sell copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
* IN THE SOFTWARE.
|
||||
*
|
||||
* Included from lib/private-lib-core.h if no explicit platform
|
||||
*/
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <strings.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <netinet/in.h>
|
||||
#include <netinet/tcp.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <poll.h>
|
||||
#include <netdb.h>
|
||||
|
||||
#ifndef __cplusplus
|
||||
#include <errno.h>
|
||||
#endif
|
||||
#include <netdb.h>
|
||||
#include <signal.h>
|
||||
|
||||
#include <sys/socket.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/time.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/un.h>
|
||||
#if defined(LWS_HAVE_EVENTFD)
|
||||
#include <sys/eventfd.h>
|
||||
#endif
|
||||
|
||||
#if defined(__APPLE__)
|
||||
#include <machine/endian.h>
|
||||
#endif
|
||||
#if defined(__FreeBSD__)
|
||||
#include <sys/endian.h>
|
||||
#endif
|
||||
#if defined(__linux__)
|
||||
#include <endian.h>
|
||||
#include <linux/if_packet.h>
|
||||
#include <net/if.h>
|
||||
#endif
|
||||
#if defined(__QNX__)
|
||||
#include <gulliver.h>
|
||||
#if defined(__LITTLEENDIAN__)
|
||||
#define BYTE_ORDER __LITTLEENDIAN__
|
||||
#define LITTLE_ENDIAN __LITTLEENDIAN__
|
||||
#define BIG_ENDIAN 4321 /* to show byte order (taken from gcc); for suppres warning that BIG_ENDIAN is not defined. */
|
||||
#endif
|
||||
#if defined(__BIGENDIAN__)
|
||||
#define BYTE_ORDER __BIGENDIAN__
|
||||
#define LITTLE_ENDIAN 1234 /* to show byte order (taken from gcc); for suppres warning that LITTLE_ENDIAN is not defined. */
|
||||
#define BIG_ENDIAN __BIGENDIAN__
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if defined(LWS_HAVE_PTHREAD_H)
|
||||
#include <pthread.h>
|
||||
typedef pthread_mutex_t lws_mutex_t;
|
||||
#define lws_mutex_init(x) pthread_mutex_init(&(x), NULL)
|
||||
#define lws_mutex_destroy(x) pthread_mutex_destroy(&(x))
|
||||
#define lws_mutex_lock(x) pthread_mutex_lock(&(x))
|
||||
#define lws_mutex_unlock(x) pthread_mutex_unlock(&(x))
|
||||
#endif
|
||||
|
||||
#if defined(__sun) && defined(__GNUC__)
|
||||
|
||||
#include <arpa/nameser_compat.h>
|
||||
|
||||
#if !defined (BYTE_ORDER)
|
||||
#define BYTE_ORDER __BYTE_ORDER__
|
||||
#endif
|
||||
|
||||
#if !defined(LITTLE_ENDIAN)
|
||||
#define LITTLE_ENDIAN __ORDER_LITTLE_ENDIAN__
|
||||
#endif
|
||||
|
||||
#if !defined(BIG_ENDIAN)
|
||||
#define BIG_ENDIAN __ORDER_BIG_ENDIAN__
|
||||
#endif
|
||||
|
||||
#endif /* sun + GNUC */
|
||||
|
||||
#if !defined(BYTE_ORDER)
|
||||
#define BYTE_ORDER __BYTE_ORDER
|
||||
#endif
|
||||
#if !defined(LITTLE_ENDIAN)
|
||||
#define LITTLE_ENDIAN __LITTLE_ENDIAN
|
||||
#endif
|
||||
#if !defined(BIG_ENDIAN)
|
||||
#define BIG_ENDIAN __BIG_ENDIAN
|
||||
#endif
|
||||
|
||||
#if defined(LWS_BUILTIN_GETIFADDRS)
|
||||
#include "./misc/getifaddrs.h"
|
||||
#else
|
||||
|
||||
#if defined(__HAIKU__)
|
||||
#define _BSD_SOURCE
|
||||
#endif
|
||||
#include <ifaddrs.h>
|
||||
|
||||
#endif
|
||||
|
||||
#if defined (__sun) || defined(__HAIKU__) || defined(__QNX__) || defined(__ANDROID__) || defined(__NuttX__)
|
||||
#include <syslog.h>
|
||||
|
||||
#if defined(__ANDROID__)
|
||||
#include <sys/resource.h>
|
||||
#endif
|
||||
|
||||
#else
|
||||
#include <sys/syslog.h>
|
||||
#endif
|
||||
|
||||
#ifdef __QNX__
|
||||
# include "netinet/tcp_var.h"
|
||||
# define TCP_KEEPINTVL TCPCTL_KEEPINTVL
|
||||
# define TCP_KEEPIDLE TCPCTL_KEEPIDLE
|
||||
# define TCP_KEEPCNT TCPCTL_KEEPCNT
|
||||
#endif
|
||||
|
||||
#define LWS_ERRNO errno
|
||||
#define LWS_EAGAIN EAGAIN
|
||||
#define LWS_EALREADY EALREADY
|
||||
#define LWS_EINPROGRESS EINPROGRESS
|
||||
#define LWS_EINTR EINTR
|
||||
#define LWS_EISCONN EISCONN
|
||||
#define LWS_ENOTCONN ENOTCONN
|
||||
#define LWS_EWOULDBLOCK EWOULDBLOCK
|
||||
#define LWS_EADDRINUSE EADDRINUSE
|
||||
#define lws_set_blocking_send(wsi)
|
||||
#define LWS_SOCK_INVALID (-1)
|
||||
|
||||
struct lws_context;
|
||||
|
||||
struct lws *
|
||||
wsi_from_fd(const struct lws_context *context, int fd);
|
||||
|
||||
int
|
||||
insert_wsi(const struct lws_context *context, struct lws *wsi);
|
||||
|
||||
struct lws_dhcpc_ifstate;
|
||||
int
|
||||
lws_plat_ifconfig(int fd, struct lws_dhcpc_ifstate *is);
|
||||
|
||||
void
|
||||
delete_from_fd(const struct lws_context *context, int fd);
|
||||
|
||||
#ifndef LWS_NO_FORK
|
||||
#ifdef LWS_HAVE_SYS_PRCTL_H
|
||||
#include <sys/prctl.h>
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#define compatible_close(x) close(x)
|
||||
#define compatible_file_close(fd) close(fd)
|
||||
#define lws_plat_socket_offset() (0)
|
||||
|
||||
/*
|
||||
* Mac OSX as well as iOS do not define the MSG_NOSIGNAL flag,
|
||||
* but happily have something equivalent in the SO_NOSIGPIPE flag.
|
||||
*/
|
||||
#ifdef __APPLE__
|
||||
/* iOS SDK 12+ seems to define it, undef it for compatibility both ways */
|
||||
#undef MSG_NOSIGNAL
|
||||
#define MSG_NOSIGNAL SO_NOSIGPIPE
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Solaris 11.X only supports POSIX 2001, MSG_NOSIGNAL appears in
|
||||
* POSIX 2008.
|
||||
*/
|
||||
#if defined(__sun) && !defined(MSG_NOSIGNAL)
|
||||
#define MSG_NOSIGNAL 0
|
||||
#endif
|
||||
|
||||
int
|
||||
lws_plat_rawudp_broadcast(uint8_t *p, const uint8_t *canned, size_t canned_len,
|
||||
size_t n, int fd, const char *iface);
|
||||
|
||||
int
|
||||
lws_plat_if_up(const char *ifname, int fd, int up);
|
249
Kinc/Sources/kinc/libs/plat/unix/unix-caps.c
Normal file
249
Kinc/Sources/kinc/libs/plat/unix/unix-caps.c
Normal file
@ -0,0 +1,249 @@
|
||||
/*
|
||||
* libwebsockets - small server side websockets and web server implementation
|
||||
*
|
||||
* Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to
|
||||
* deal in the Software without restriction, including without limitation the
|
||||
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
* sell copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
* IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#if !defined(_GNU_SOURCE)
|
||||
#define _GNU_SOURCE
|
||||
#endif
|
||||
#include "private-lib-core.h"
|
||||
|
||||
#include <pwd.h>
|
||||
#include <grp.h>
|
||||
|
||||
#if defined(LWS_HAVE_SYS_CAPABILITY_H) && defined(LWS_HAVE_LIBCAP)
|
||||
static void
|
||||
_lws_plat_apply_caps(unsigned int mode, const cap_value_t *cv, int count)
|
||||
{
|
||||
cap_t caps;
|
||||
|
||||
if (!count)
|
||||
return;
|
||||
|
||||
caps = cap_get_proc();
|
||||
|
||||
cap_set_flag(caps, (cap_flag_t)mode, count, cv, CAP_SET);
|
||||
cap_set_proc(caps);
|
||||
prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0);
|
||||
cap_free(caps);
|
||||
}
|
||||
#endif
|
||||
|
||||
int
|
||||
lws_plat_user_colon_group_to_ids(const char *u_colon_g, uid_t *puid, gid_t *pgid)
|
||||
{
|
||||
char *colon = strchr(u_colon_g, ':'), u[33];
|
||||
struct group *g;
|
||||
struct passwd *p;
|
||||
size_t ulen;
|
||||
|
||||
if (!colon)
|
||||
return 1;
|
||||
|
||||
ulen = (size_t)(unsigned int)lws_ptr_diff(colon, u_colon_g);
|
||||
if (ulen < 2 || ulen > sizeof(u) - 1)
|
||||
return 1;
|
||||
|
||||
memcpy(u, u_colon_g, ulen);
|
||||
u[ulen] = '\0';
|
||||
|
||||
colon++;
|
||||
|
||||
#if defined(LWS_HAVE_GETGRNAM_R)
|
||||
{
|
||||
struct group gr;
|
||||
char strs[1024];
|
||||
|
||||
if (getgrnam_r(colon, &gr, strs, sizeof(strs), &g) || !g) {
|
||||
#else
|
||||
{
|
||||
g = getgrnam(colon);
|
||||
if (!g) {
|
||||
#endif
|
||||
lwsl_err("%s: unknown group '%s'\n", __func__, colon);
|
||||
|
||||
return 1;
|
||||
}
|
||||
*pgid = g->gr_gid;
|
||||
}
|
||||
|
||||
#if defined(LWS_HAVE_GETPWNAM_R)
|
||||
{
|
||||
struct passwd pr;
|
||||
char strs[1024];
|
||||
|
||||
if (getpwnam_r(u, &pr, strs, sizeof(strs), &p) || !p) {
|
||||
#else
|
||||
{
|
||||
p = getpwnam(u);
|
||||
if (!p) {
|
||||
#endif
|
||||
lwsl_err("%s: unknown user '%s'\n", __func__, u);
|
||||
|
||||
return 1;
|
||||
}
|
||||
*puid = p->pw_uid;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
lws_plat_drop_app_privileges(struct lws_context *context, int actually_drop)
|
||||
{
|
||||
struct passwd *p;
|
||||
struct group *g;
|
||||
|
||||
/* if he gave us the groupname, align gid to match it */
|
||||
|
||||
if (context->groupname) {
|
||||
#if defined(LWS_HAVE_GETGRNAM_R)
|
||||
struct group gr;
|
||||
char strs[1024];
|
||||
|
||||
if (!getgrnam_r(context->groupname, &gr, strs, sizeof(strs), &g) && g) {
|
||||
#else
|
||||
g = getgrnam(context->groupname);
|
||||
if (g) {
|
||||
#endif
|
||||
lwsl_cx_info(context, "group %s -> gid %u",
|
||||
context->groupname, g->gr_gid);
|
||||
context->gid = g->gr_gid;
|
||||
} else {
|
||||
lwsl_cx_err(context, "unknown groupname '%s'",
|
||||
context->groupname);
|
||||
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
/* if he gave us the username, align uid to match it */
|
||||
|
||||
if (context->username) {
|
||||
#if defined(LWS_HAVE_GETPWNAM_R)
|
||||
struct passwd pr;
|
||||
char strs[1024];
|
||||
|
||||
if (!getpwnam_r(context->username, &pr, strs, sizeof(strs), &p) && p) {
|
||||
#else
|
||||
p = getpwnam(context->username);
|
||||
if (p) {
|
||||
#endif
|
||||
context->uid = p->pw_uid;
|
||||
|
||||
lwsl_cx_info(context, "username %s -> uid %u",
|
||||
context->username, (unsigned int)p->pw_uid);
|
||||
} else {
|
||||
lwsl_cx_err(context, "unknown username %s",
|
||||
context->username);
|
||||
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (!actually_drop)
|
||||
return 0;
|
||||
|
||||
/* if he gave us the gid or we have it from the groupname, set it */
|
||||
|
||||
if (context->gid && context->gid != (gid_t)-1l) {
|
||||
#if defined(LWS_HAVE_GETGRGID_R)
|
||||
struct group gr;
|
||||
char strs[1024];
|
||||
|
||||
if (getgrgid_r(context->gid, &gr, strs, sizeof(strs), &g) || !g) {
|
||||
#else
|
||||
g = getgrgid(context->gid);
|
||||
if (!g) {
|
||||
#endif
|
||||
lwsl_cx_err(context, "cannot find name for gid %d",
|
||||
context->gid);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (setgid(context->gid)) {
|
||||
lwsl_cx_err(context, "setgid: %s failed",
|
||||
strerror(LWS_ERRNO));
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
lwsl_cx_notice(context, "effective group '%s'", g->gr_name);
|
||||
} else
|
||||
lwsl_cx_info(context, "not changing group");
|
||||
|
||||
|
||||
/* if he gave us the uid or we have it from the username, set it */
|
||||
|
||||
if (context->uid && context->uid != (uid_t)-1l) {
|
||||
#if defined(LWS_HAVE_GETPWUID_R)
|
||||
struct passwd pr;
|
||||
char strs[1024];
|
||||
|
||||
if (getpwuid_r(context->uid, &pr, strs, sizeof(strs), &p) || !p) {
|
||||
#else
|
||||
p = getpwuid(context->uid);
|
||||
if (!p) {
|
||||
#endif
|
||||
lwsl_cx_err(context, "getpwuid: unable to find uid %d",
|
||||
context->uid);
|
||||
return 1;
|
||||
}
|
||||
|
||||
#if defined(LWS_HAVE_SYS_CAPABILITY_H) && defined(LWS_HAVE_LIBCAP)
|
||||
_lws_plat_apply_caps(CAP_PERMITTED, context->caps,
|
||||
context->count_caps);
|
||||
#endif
|
||||
|
||||
if (initgroups(p->pw_name,
|
||||
#if defined(__APPLE__)
|
||||
(int)
|
||||
#endif
|
||||
context->gid))
|
||||
return 1;
|
||||
|
||||
if (setuid(context->uid)) {
|
||||
lwsl_cx_err(context, "setuid: %s failed",
|
||||
strerror(LWS_ERRNO));
|
||||
|
||||
return 1;
|
||||
} else
|
||||
lwsl_cx_notice(context, "effective user '%s'",
|
||||
p->pw_name);
|
||||
|
||||
#if defined(LWS_HAVE_SYS_CAPABILITY_H) && defined(LWS_HAVE_LIBCAP)
|
||||
_lws_plat_apply_caps(CAP_EFFECTIVE, context->caps,
|
||||
context->count_caps);
|
||||
|
||||
if (context->count_caps) {
|
||||
int n;
|
||||
for (n = 0; n < context->count_caps; n++)
|
||||
lwsl_cx_notice(context, " RETAINING CAP %d",
|
||||
(int)context->caps[n]);
|
||||
}
|
||||
#endif
|
||||
} else
|
||||
lwsl_cx_info(context, "not changing user");
|
||||
|
||||
return 0;
|
||||
}
|
266
Kinc/Sources/kinc/libs/plat/unix/unix-fds.c
Normal file
266
Kinc/Sources/kinc/libs/plat/unix/unix-fds.c
Normal file
@ -0,0 +1,266 @@
|
||||
/*
|
||||
* libwebsockets - small server side websockets and web server implementation
|
||||
*
|
||||
* Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to
|
||||
* deal in the Software without restriction, including without limitation the
|
||||
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
* sell copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
* IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#if !defined(_GNU_SOURCE)
|
||||
#define _GNU_SOURCE
|
||||
#endif
|
||||
#include "private-lib-core.h"
|
||||
|
||||
struct lws *
|
||||
wsi_from_fd(const struct lws_context *context, int fd)
|
||||
{
|
||||
struct lws **p, **done;
|
||||
|
||||
if (!context->max_fds_unrelated_to_ulimit)
|
||||
return context->lws_lookup[fd - lws_plat_socket_offset()];
|
||||
|
||||
/* slow fds handling */
|
||||
|
||||
p = context->lws_lookup;
|
||||
done = &p[context->max_fds];
|
||||
|
||||
while (p != done) {
|
||||
if (*p && (*p)->desc.sockfd == fd)
|
||||
return *p;
|
||||
p++;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
#if defined(_DEBUG)
|
||||
int
|
||||
sanity_assert_no_wsi_traces(const struct lws_context *context, struct lws *wsi)
|
||||
{
|
||||
struct lws **p, **done;
|
||||
|
||||
if (!context->max_fds_unrelated_to_ulimit)
|
||||
/* can't tell */
|
||||
return 0;
|
||||
|
||||
/* slow fds handling */
|
||||
|
||||
p = context->lws_lookup;
|
||||
done = &p[context->max_fds];
|
||||
|
||||
/* confirm the wsi doesn't already exist */
|
||||
|
||||
while (p != done && *p != wsi)
|
||||
p++;
|
||||
|
||||
if (p == done)
|
||||
return 0;
|
||||
|
||||
assert(0); /* this wsi is still mentioned inside lws */
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int
|
||||
sanity_assert_no_sockfd_traces(const struct lws_context *context,
|
||||
lws_sockfd_type sfd)
|
||||
{
|
||||
#if LWS_MAX_SMP > 1
|
||||
/*
|
||||
* We can't really do this test... another thread can accept and
|
||||
* reuse the closed fd
|
||||
*/
|
||||
return 0;
|
||||
#else
|
||||
struct lws **p, **done;
|
||||
|
||||
if (sfd == LWS_SOCK_INVALID || !context->lws_lookup)
|
||||
return 0;
|
||||
|
||||
if (!context->max_fds_unrelated_to_ulimit &&
|
||||
context->lws_lookup[sfd - lws_plat_socket_offset()]) {
|
||||
assert(0); /* the fd is still in use */
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* slow fds handling */
|
||||
|
||||
p = context->lws_lookup;
|
||||
done = &p[context->max_fds];
|
||||
|
||||
/* confirm the sfd not already in use */
|
||||
|
||||
while (p != done && (!*p || (*p)->desc.sockfd != sfd))
|
||||
p++;
|
||||
|
||||
if (p == done)
|
||||
return 0;
|
||||
|
||||
assert(0); /* this fd is still in the tables */
|
||||
|
||||
return 1;
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
int
|
||||
insert_wsi(const struct lws_context *context, struct lws *wsi)
|
||||
{
|
||||
struct lws **p, **done;
|
||||
|
||||
if (sanity_assert_no_wsi_traces(context, wsi))
|
||||
return 0;
|
||||
|
||||
if (!context->max_fds_unrelated_to_ulimit) {
|
||||
assert(context->lws_lookup[wsi->desc.sockfd -
|
||||
lws_plat_socket_offset()] == 0);
|
||||
|
||||
context->lws_lookup[wsi->desc.sockfd - \
|
||||
lws_plat_socket_offset()] = wsi;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* slow fds handling */
|
||||
|
||||
p = context->lws_lookup;
|
||||
done = &p[context->max_fds];
|
||||
|
||||
/* confirm fd isn't already in use by a wsi */
|
||||
|
||||
if (sanity_assert_no_sockfd_traces(context, wsi->desc.sockfd))
|
||||
return 0;
|
||||
|
||||
p = context->lws_lookup;
|
||||
|
||||
/* find an empty slot */
|
||||
|
||||
while (p != done && *p)
|
||||
p++;
|
||||
|
||||
if (p == done) {
|
||||
lwsl_err("%s: reached max fds\n", __func__);
|
||||
return 1;
|
||||
}
|
||||
|
||||
*p = wsi;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void
|
||||
delete_from_fd(const struct lws_context *context, int fd)
|
||||
{
|
||||
|
||||
struct lws **p, **done;
|
||||
|
||||
if (!context->max_fds_unrelated_to_ulimit) {
|
||||
if (context->lws_lookup) {
|
||||
assert((int)context->max_fds > fd - lws_plat_socket_offset());
|
||||
context->lws_lookup[fd - lws_plat_socket_offset()] = NULL;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
/* slow fds handling */
|
||||
|
||||
p = context->lws_lookup;
|
||||
assert(p);
|
||||
|
||||
done = &p[context->max_fds];
|
||||
|
||||
/* find the match */
|
||||
|
||||
while (p != done && (!*p || (*p)->desc.sockfd != fd))
|
||||
p++;
|
||||
|
||||
if (p != done)
|
||||
*p = NULL;
|
||||
|
||||
#if defined(_DEBUG)
|
||||
p = context->lws_lookup;
|
||||
while (p != done && (!*p || (*p)->desc.sockfd != fd))
|
||||
p++;
|
||||
|
||||
if (p != done) {
|
||||
lwsl_err("%s: fd %d in lws_lookup again at %d\n", __func__,
|
||||
fd, (int)(p - context->lws_lookup));
|
||||
assert(0);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void
|
||||
delete_from_fdwsi(const struct lws_context *context, struct lws *wsi)
|
||||
{
|
||||
|
||||
struct lws **p, **done;
|
||||
|
||||
if (!context->max_fds_unrelated_to_ulimit)
|
||||
return;
|
||||
|
||||
|
||||
/* slow fds handling */
|
||||
|
||||
p = context->lws_lookup;
|
||||
done = &p[context->max_fds];
|
||||
|
||||
/* find the match */
|
||||
|
||||
while (p != done && (!*p || (*p) != wsi))
|
||||
p++;
|
||||
|
||||
if (p != done)
|
||||
*p = NULL;
|
||||
}
|
||||
|
||||
void
|
||||
lws_plat_insert_socket_into_fds(struct lws_context *context, struct lws *wsi)
|
||||
{
|
||||
struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
|
||||
|
||||
if (context->event_loop_ops->io)
|
||||
context->event_loop_ops->io(wsi, LWS_EV_START | LWS_EV_READ);
|
||||
|
||||
pt->fds[pt->fds_count++].revents = 0;
|
||||
}
|
||||
|
||||
void
|
||||
lws_plat_delete_socket_from_fds(struct lws_context *context,
|
||||
struct lws *wsi, int m)
|
||||
{
|
||||
struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
|
||||
|
||||
if (context->event_loop_ops->io)
|
||||
context->event_loop_ops->io(wsi,
|
||||
LWS_EV_STOP | LWS_EV_READ | LWS_EV_WRITE);
|
||||
|
||||
pt->fds_count--;
|
||||
}
|
||||
|
||||
int
|
||||
lws_plat_change_pollfd(struct lws_context *context,
|
||||
struct lws *wsi, struct lws_pollfd *pfd)
|
||||
{
|
||||
return 0;
|
||||
}
|
185
Kinc/Sources/kinc/libs/plat/unix/unix-file.c
Normal file
185
Kinc/Sources/kinc/libs/plat/unix/unix-file.c
Normal file
@ -0,0 +1,185 @@
|
||||
/*
|
||||
* libwebsockets - small server side websockets and web server implementation
|
||||
*
|
||||
* Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to
|
||||
* deal in the Software without restriction, including without limitation the
|
||||
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
* sell copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
* IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#if !defined(_GNU_SOURCE)
|
||||
#define _GNU_SOURCE
|
||||
#endif
|
||||
#include "private-lib-core.h"
|
||||
|
||||
#include <pwd.h>
|
||||
#include <grp.h>
|
||||
|
||||
#ifdef LWS_WITH_PLUGINS
|
||||
#include <dlfcn.h>
|
||||
#endif
|
||||
#include <dirent.h>
|
||||
|
||||
int lws_plat_apply_FD_CLOEXEC(int n)
|
||||
{
|
||||
if (n == -1)
|
||||
return 0;
|
||||
|
||||
return fcntl(n, F_SETFD, FD_CLOEXEC);
|
||||
}
|
||||
|
||||
int
|
||||
lws_plat_write_file(const char *filename, void *buf, size_t len)
|
||||
{
|
||||
ssize_t m;
|
||||
int fd;
|
||||
|
||||
fd = lws_open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600);
|
||||
|
||||
if (fd == -1)
|
||||
return 1;
|
||||
|
||||
m = write(fd, buf, len);
|
||||
close(fd);
|
||||
|
||||
if (m < 0)
|
||||
return 1;
|
||||
|
||||
return (size_t)m != len;
|
||||
}
|
||||
|
||||
int
|
||||
lws_plat_read_file(const char *filename, void *buf, size_t len)
|
||||
{
|
||||
int fd = lws_open(filename, O_RDONLY);
|
||||
ssize_t n;
|
||||
|
||||
if (fd == -1)
|
||||
return -1;
|
||||
|
||||
n = read(fd, buf, len);
|
||||
close(fd);
|
||||
|
||||
return (int)n;
|
||||
}
|
||||
|
||||
lws_fop_fd_t
|
||||
_lws_plat_file_open(const struct lws_plat_file_ops *fops_own,
|
||||
const struct lws_plat_file_ops *fops, const char *filename,
|
||||
const char *vpath, lws_fop_flags_t *flags)
|
||||
{
|
||||
struct stat stat_buf;
|
||||
int ret = lws_open(filename, (int)((*flags) & LWS_FOP_FLAGS_MASK), 0664);
|
||||
lws_fop_fd_t fop_fd;
|
||||
|
||||
if (ret < 0)
|
||||
return NULL;
|
||||
|
||||
if (fstat(ret, &stat_buf) < 0)
|
||||
goto bail;
|
||||
|
||||
fop_fd = malloc(sizeof(*fop_fd));
|
||||
if (!fop_fd)
|
||||
goto bail;
|
||||
|
||||
fop_fd->fops = fops;
|
||||
fop_fd->flags = *flags;
|
||||
fop_fd->fd = ret;
|
||||
fop_fd->filesystem_priv = NULL; /* we don't use it */
|
||||
fop_fd->len = (lws_filepos_t)stat_buf.st_size;
|
||||
fop_fd->pos = 0;
|
||||
|
||||
return fop_fd;
|
||||
|
||||
bail:
|
||||
close(ret);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int
|
||||
_lws_plat_file_close(lws_fop_fd_t *fop_fd)
|
||||
{
|
||||
int fd = (*fop_fd)->fd;
|
||||
|
||||
free(*fop_fd);
|
||||
*fop_fd = NULL;
|
||||
|
||||
return close(fd);
|
||||
}
|
||||
|
||||
lws_fileofs_t
|
||||
_lws_plat_file_seek_cur(lws_fop_fd_t fop_fd, lws_fileofs_t offset)
|
||||
{
|
||||
lws_fileofs_t r;
|
||||
|
||||
if (offset > 0 &&
|
||||
offset > (lws_fileofs_t)fop_fd->len - (lws_fileofs_t)fop_fd->pos)
|
||||
offset = (lws_fileofs_t)(fop_fd->len - fop_fd->pos);
|
||||
|
||||
if ((lws_fileofs_t)fop_fd->pos + offset < 0)
|
||||
offset = (lws_fileofs_t)(-fop_fd->pos);
|
||||
|
||||
r = lseek(fop_fd->fd, (off_t)offset, SEEK_CUR);
|
||||
|
||||
if (r >= 0)
|
||||
fop_fd->pos = (lws_filepos_t)r;
|
||||
else
|
||||
lwsl_err("error seeking from cur %ld, offset %ld\n",
|
||||
(long)fop_fd->pos, (long)offset);
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
int
|
||||
_lws_plat_file_read(lws_fop_fd_t fop_fd, lws_filepos_t *amount,
|
||||
uint8_t *buf, lws_filepos_t len)
|
||||
{
|
||||
ssize_t n;
|
||||
|
||||
n = read((int)fop_fd->fd, buf, (size_t)len);
|
||||
if (n == -1l) {
|
||||
*amount = 0;
|
||||
return -1;
|
||||
}
|
||||
fop_fd->pos = (lws_filepos_t)(fop_fd->pos + (lws_filepos_t)n);
|
||||
lwsl_debug("%s: read %ld of req %ld, pos %ld, len %ld\n", __func__,
|
||||
(long)n, (long)len, (long)fop_fd->pos,
|
||||
(long)fop_fd->len);
|
||||
*amount = (lws_filepos_t)n;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
_lws_plat_file_write(lws_fop_fd_t fop_fd, lws_filepos_t *amount,
|
||||
uint8_t *buf, lws_filepos_t len)
|
||||
{
|
||||
ssize_t n;
|
||||
|
||||
n = write((int)fop_fd->fd, buf, (size_t)len);
|
||||
if (n == -1) {
|
||||
*amount = 0;
|
||||
return -1;
|
||||
}
|
||||
|
||||
fop_fd->pos = (lws_filepos_t)(fop_fd->pos + (lws_filepos_t)n);
|
||||
*amount = (lws_filepos_t)n;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
270
Kinc/Sources/kinc/libs/plat/unix/unix-init.c
Normal file
270
Kinc/Sources/kinc/libs/plat/unix/unix-init.c
Normal file
@ -0,0 +1,270 @@
|
||||
/*
|
||||
* libwebsockets - small server side websockets and web server implementation
|
||||
*
|
||||
* Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to
|
||||
* deal in the Software without restriction, including without limitation the
|
||||
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
* sell copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
* IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#if !defined(_GNU_SOURCE)
|
||||
#define _GNU_SOURCE
|
||||
#endif
|
||||
#include "private-lib-core.h"
|
||||
|
||||
#include <pwd.h>
|
||||
#include <grp.h>
|
||||
|
||||
#ifdef LWS_WITH_PLUGINS
|
||||
#include <dlfcn.h>
|
||||
#endif
|
||||
#include <dirent.h>
|
||||
|
||||
#if defined(LWS_WITH_NETWORK)
|
||||
static void
|
||||
lws_sul_plat_unix(lws_sorted_usec_list_t *sul)
|
||||
{
|
||||
struct lws_context_per_thread *pt =
|
||||
lws_container_of(sul, struct lws_context_per_thread, sul_plat);
|
||||
struct lws_context *context = pt->context;
|
||||
int n = 0, m = 0;
|
||||
|
||||
#if !defined(LWS_NO_DAEMONIZE)
|
||||
/* if our parent went down, don't linger around */
|
||||
if (pt->context->started_with_parent &&
|
||||
kill(pt->context->started_with_parent, 0) < 0)
|
||||
kill(getpid(), SIGTERM);
|
||||
#endif
|
||||
|
||||
for (n = 0; n < context->count_threads; n++)
|
||||
m = m | (int)pt->fds_count;
|
||||
|
||||
if (context->deprecated && !m) {
|
||||
lwsl_notice("%s: ending deprecated context\n", __func__);
|
||||
kill(getpid(), SIGINT);
|
||||
return;
|
||||
}
|
||||
|
||||
#if defined(LWS_WITH_SERVER)
|
||||
lws_context_lock(context, "periodic checks");
|
||||
lws_start_foreach_llp(struct lws_vhost **, pv,
|
||||
context->no_listener_vhost_list) {
|
||||
struct lws_vhost *v = *pv;
|
||||
lwsl_debug("deferred iface: checking if on vh %s\n", (*pv)->name);
|
||||
if (_lws_vhost_init_server(NULL, *pv) == 0) {
|
||||
/* became happy */
|
||||
lwsl_notice("vh %s: became connected\n", v->name);
|
||||
*pv = v->no_listener_vhost_list;
|
||||
v->no_listener_vhost_list = NULL;
|
||||
break;
|
||||
}
|
||||
} lws_end_foreach_llp(pv, no_listener_vhost_list);
|
||||
lws_context_unlock(context);
|
||||
#endif
|
||||
|
||||
__lws_sul_insert_us(&pt->pt_sul_owner[LWSSULLI_MISS_IF_SUSPENDED],
|
||||
&pt->sul_plat, 30 * LWS_US_PER_SEC);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(LWS_WITH_PLUGINS)
|
||||
static int
|
||||
protocol_plugin_cb(struct lws_plugin *pin, void *each_user)
|
||||
{
|
||||
struct lws_context *context = (struct lws_context *)each_user;
|
||||
const lws_plugin_protocol_t *plpr =
|
||||
(const lws_plugin_protocol_t *)pin->hdr;
|
||||
|
||||
context->plugin_protocol_count = (short)(context->plugin_protocol_count +
|
||||
plpr->count_protocols);
|
||||
context->plugin_extension_count = (short)(context->plugin_extension_count +
|
||||
plpr->count_extensions);
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
int
|
||||
lws_plat_init(struct lws_context *context,
|
||||
const struct lws_context_creation_info *info)
|
||||
{
|
||||
int fd;
|
||||
#if defined(LWS_WITH_NETWORK)
|
||||
/*
|
||||
* context has the process-global fd lookup array. This can be
|
||||
* done two different ways now; one or the other is done depending on if
|
||||
* info->fd_limit_per_thread was snonzero
|
||||
*
|
||||
* - default: allocate a worst-case lookup array sized for ulimit -n
|
||||
* and use the fd directly as an index into it
|
||||
*
|
||||
* - slow: allocate context->max_fds entries only (which can be
|
||||
* forced at context creation time to be
|
||||
* info->fd_limit_per_thread * the number of threads)
|
||||
* and search the array to lookup fds
|
||||
*
|
||||
* the default way is optimized for server, if you only use one or two
|
||||
* client wsi the slow way may save a lot of memory.
|
||||
*
|
||||
* Both ways allocate an array of struct lws *... one allocates it for
|
||||
* all possible fd indexes the process could produce and uses it as a
|
||||
* map, the other allocates for an amount of wsi the lws context is
|
||||
* expected to use and searches through it to manipulate it.
|
||||
*/
|
||||
|
||||
context->lws_lookup = lws_zalloc(sizeof(struct lws *) *
|
||||
context->max_fds, "lws_lookup");
|
||||
|
||||
if (!context->lws_lookup) {
|
||||
lwsl_cx_err(context, "OOM on alloc lws_lookup array for %d conn",
|
||||
context->max_fds);
|
||||
return 1;
|
||||
}
|
||||
|
||||
#if defined(LWS_WITH_MBEDTLS)
|
||||
{
|
||||
int n;
|
||||
|
||||
/* initialize platform random through mbedtls */
|
||||
mbedtls_entropy_init(&context->mec);
|
||||
mbedtls_ctr_drbg_init(&context->mcdc);
|
||||
|
||||
n = mbedtls_ctr_drbg_seed(&context->mcdc, mbedtls_entropy_func,
|
||||
&context->mec, NULL, 0);
|
||||
if (n)
|
||||
lwsl_err("%s: mbedtls_ctr_drbg_seed() returned 0x%x\n",
|
||||
__func__, n);
|
||||
#if 0
|
||||
else {
|
||||
uint8_t rtest[16];
|
||||
lwsl_notice("%s: started drbg\n", __func__);
|
||||
if (mbedtls_ctr_drbg_random(&context->mcdc, rtest,
|
||||
sizeof(rtest)))
|
||||
lwsl_err("%s: get random failed\n", __func__);
|
||||
else
|
||||
lwsl_hexdump_notice(rtest, sizeof(rtest));
|
||||
}
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
|
||||
lwsl_cx_info(context, " mem: platform fd map: %5lu B",
|
||||
(unsigned long)(sizeof(struct lws *) * context->max_fds));
|
||||
#endif
|
||||
#if defined(LWS_WITH_FILE_OPS)
|
||||
fd = lws_open(SYSTEM_RANDOM_FILEPATH, O_RDONLY);
|
||||
#else
|
||||
fd = open(SYSTEM_RANDOM_FILEPATH, O_RDONLY);
|
||||
#endif
|
||||
context->fd_random = fd;
|
||||
if (context->fd_random < 0) {
|
||||
lwsl_err("Unable to open random device %s %d, errno %d\n",
|
||||
SYSTEM_RANDOM_FILEPATH, context->fd_random, errno);
|
||||
return 1;
|
||||
}
|
||||
|
||||
#if defined(LWS_HAVE_SSL_CTX_set_keylog_callback) && !defined(__COVERITY__) && \
|
||||
defined(LWS_WITH_TLS) && defined(LWS_WITH_CLIENT)
|
||||
{
|
||||
char *klf_env = getenv("SSLKEYLOGFILE");
|
||||
size_t n = 0;
|
||||
|
||||
/* ... coverity taint with lws_strncpy()... */
|
||||
|
||||
while (klf_env && klf_env[n] &&
|
||||
n < sizeof(context->keylog_file) - 1) {
|
||||
context->keylog_file[n] = klf_env[n];
|
||||
n++;
|
||||
}
|
||||
context->keylog_file[n] = '\0';
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(LWS_WITH_PLUGINS) && !defined(LWS_WITH_PLUGINS_BUILTIN)
|
||||
{
|
||||
char *ld_env = getenv("LD_LIBRARY_PATH");
|
||||
|
||||
if (ld_env) {
|
||||
const char *pp[2] = { ld_env, NULL };
|
||||
|
||||
lws_plugins_init(&context->plugin_list, pp,
|
||||
"lws_protocol_plugin", NULL,
|
||||
protocol_plugin_cb, context);
|
||||
}
|
||||
|
||||
if (info->plugin_dirs)
|
||||
lws_plugins_init(&context->plugin_list,
|
||||
info->plugin_dirs,
|
||||
"lws_protocol_plugin", NULL,
|
||||
protocol_plugin_cb, context);
|
||||
}
|
||||
|
||||
#endif
|
||||
#if defined(LWS_BUILTIN_PLUGIN_NAMES) && defined(LWS_WITH_PLUGINS)
|
||||
lws_plugins_handle_builtin(&context->plugin_list,
|
||||
protocol_plugin_cb, context);
|
||||
#endif
|
||||
|
||||
|
||||
#if defined(LWS_WITH_NETWORK)
|
||||
/* we only need to do this on pt[0] */
|
||||
|
||||
context->pt[0].sul_plat.cb = lws_sul_plat_unix;
|
||||
__lws_sul_insert_us(&context->pt[0].pt_sul_owner[LWSSULLI_MISS_IF_SUSPENDED],
|
||||
&context->pt[0].sul_plat, 30 * LWS_US_PER_SEC);
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
lws_plat_context_early_init(void)
|
||||
{
|
||||
#if !defined(LWS_AVOID_SIGPIPE_IGN)
|
||||
signal(SIGPIPE, SIG_IGN);
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void
|
||||
lws_plat_context_early_destroy(struct lws_context *context)
|
||||
{
|
||||
}
|
||||
|
||||
void
|
||||
lws_plat_context_late_destroy(struct lws_context *context)
|
||||
{
|
||||
#if defined(LWS_WITH_PLUGINS)
|
||||
if (context->plugin_list)
|
||||
lws_plugins_destroy(&context->plugin_list, NULL, NULL);
|
||||
#endif
|
||||
#if defined(LWS_WITH_NETWORK)
|
||||
if (context->lws_lookup)
|
||||
lws_free_set_NULL(context->lws_lookup);
|
||||
#endif
|
||||
if (!context->fd_random)
|
||||
lwsl_err("ZERO RANDOM FD\n");
|
||||
if (context->fd_random != LWS_INVALID_FILE)
|
||||
close(context->fd_random);
|
||||
|
||||
#if defined(LWS_WITH_MBEDTLS)
|
||||
mbedtls_entropy_free(&context->mec);
|
||||
mbedtls_ctr_drbg_free(&context->mcdc);
|
||||
#endif
|
||||
}
|
142
Kinc/Sources/kinc/libs/plat/unix/unix-misc.c
Normal file
142
Kinc/Sources/kinc/libs/plat/unix/unix-misc.c
Normal file
@ -0,0 +1,142 @@
|
||||
/*
|
||||
* libwebsockets - small server side websockets and web server implementation
|
||||
*
|
||||
* Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to
|
||||
* deal in the Software without restriction, including without limitation the
|
||||
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
* sell copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
* IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#if !defined(_GNU_SOURCE)
|
||||
#define _GNU_SOURCE
|
||||
#endif
|
||||
#include "private-lib-core.h"
|
||||
|
||||
/*
|
||||
* Normally you don't want this, use lws_sul instead inside the event loop.
|
||||
* But sometimes for drivers it makes sense, so there's an internal-only
|
||||
* crossplatform api for it.
|
||||
*/
|
||||
|
||||
void
|
||||
lws_msleep(unsigned int ms)
|
||||
{
|
||||
usleep((unsigned int)(ms * LWS_US_PER_MS));
|
||||
}
|
||||
|
||||
lws_usec_t
|
||||
lws_now_usecs(void)
|
||||
{
|
||||
#if defined(LWS_HAVE_CLOCK_GETTIME)
|
||||
struct timespec ts;
|
||||
|
||||
if (clock_gettime(CLOCK_MONOTONIC, &ts))
|
||||
return 0;
|
||||
|
||||
return (((lws_usec_t)ts.tv_sec) * LWS_US_PER_SEC) +
|
||||
((lws_usec_t)ts.tv_nsec / LWS_NS_PER_US);
|
||||
#else
|
||||
struct timeval now;
|
||||
|
||||
gettimeofday(&now, NULL);
|
||||
return (((lws_usec_t)now.tv_sec) * LWS_US_PER_SEC) +
|
||||
(lws_usec_t)now.tv_usec;
|
||||
#endif
|
||||
}
|
||||
|
||||
size_t
|
||||
lws_get_random(struct lws_context *context, void *buf, size_t len)
|
||||
{
|
||||
#if defined(__COVERITY__)
|
||||
memset(buf, 0, len);
|
||||
return len;
|
||||
#else
|
||||
/* coverity[tainted_scalar] */
|
||||
return (size_t)read(context->fd_random, (char *)buf, len);
|
||||
#endif
|
||||
}
|
||||
|
||||
void lwsl_emit_syslog(int level, const char *line)
|
||||
{
|
||||
int syslog_level = LOG_DEBUG;
|
||||
|
||||
switch (level) {
|
||||
case LLL_ERR:
|
||||
syslog_level = LOG_ERR;
|
||||
break;
|
||||
case LLL_WARN:
|
||||
syslog_level = LOG_WARNING;
|
||||
break;
|
||||
case LLL_NOTICE:
|
||||
syslog_level = LOG_NOTICE;
|
||||
break;
|
||||
case LLL_INFO:
|
||||
syslog_level = LOG_INFO;
|
||||
break;
|
||||
}
|
||||
syslog(syslog_level, "%s", line);
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
lws_plat_write_cert(struct lws_vhost *vhost, int is_key, int fd, void *buf,
|
||||
size_t len)
|
||||
{
|
||||
ssize_t n;
|
||||
|
||||
n = write(fd, buf, len);
|
||||
|
||||
if (n < 0 || fsync(fd))
|
||||
return 1;
|
||||
if (lseek(fd, 0, SEEK_SET) < 0)
|
||||
return 1;
|
||||
|
||||
return (size_t)n != len;
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
lws_plat_recommended_rsa_bits(void)
|
||||
{
|
||||
return 4096;
|
||||
}
|
||||
|
||||
/*
|
||||
* Platform-specific ntpclient server configuration
|
||||
*/
|
||||
|
||||
int
|
||||
lws_plat_ntpclient_config(struct lws_context *context)
|
||||
{
|
||||
#if defined(LWS_HAVE_GETENV)
|
||||
char *ntpsrv = getenv("LWS_NTP_SERVER");
|
||||
|
||||
if (ntpsrv && strlen(ntpsrv) < 64) {
|
||||
lws_system_blob_t *blob = lws_system_get_blob(context,
|
||||
LWS_SYSBLOB_TYPE_NTP_SERVER, 0);
|
||||
if (!blob)
|
||||
return 0;
|
||||
|
||||
lws_system_blob_direct_set(blob, (const uint8_t *)ntpsrv,
|
||||
strlen(ntpsrv));
|
||||
return 1;
|
||||
}
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
103
Kinc/Sources/kinc/libs/plat/unix/unix-pipe.c
Normal file
103
Kinc/Sources/kinc/libs/plat/unix/unix-pipe.c
Normal file
@ -0,0 +1,103 @@
|
||||
/*
|
||||
* libwebsockets - small server side websockets and web server implementation
|
||||
*
|
||||
* Copyright (C) 2010 - 2020 Andy Green <andy@warmcat.com>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to
|
||||
* deal in the Software without restriction, including without limitation the
|
||||
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
* sell copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
* IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#if !defined(_GNU_SOURCE)
|
||||
#define _GNU_SOURCE
|
||||
#endif
|
||||
#include "private-lib-core.h"
|
||||
|
||||
int
|
||||
lws_plat_pipe_create(struct lws *wsi)
|
||||
{
|
||||
struct lws_context_per_thread *pt = &wsi->a.context->pt[(int)wsi->tsi];
|
||||
int n;
|
||||
|
||||
#if defined(LWS_HAVE_EVENTFD)
|
||||
pt->dummy_pipe_fds[0] = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK);
|
||||
pt->dummy_pipe_fds[1] = -1;
|
||||
|
||||
n = pt->dummy_pipe_fds[0] < 0 ? -1 : 0;
|
||||
goto set;
|
||||
|
||||
#elif defined(LWS_HAVE_PIPE2)
|
||||
n = pipe2(pt->dummy_pipe_fds, O_NONBLOCK);
|
||||
#else
|
||||
n = pipe(pt->dummy_pipe_fds);
|
||||
#endif
|
||||
|
||||
#if defined(LWS_HAVE_EVENTFD)
|
||||
set:
|
||||
#endif
|
||||
if (n >= 0) {
|
||||
if (fcntl(pt->dummy_pipe_fds[0], F_SETFL, O_NONBLOCK) < 0)
|
||||
n = -1;
|
||||
else if (pt->dummy_pipe_fds[1] >= 0) {
|
||||
if (fcntl(pt->dummy_pipe_fds[1], F_SETFL, O_NONBLOCK) < 0)
|
||||
n = -1;
|
||||
}
|
||||
}
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
int
|
||||
lws_plat_pipe_signal(struct lws_context *ctx, int tsi)
|
||||
{
|
||||
struct lws_context_per_thread *pt = &ctx->pt[tsi];
|
||||
#if defined(LWS_HAVE_EVENTFD)
|
||||
eventfd_t value = 1;
|
||||
|
||||
return eventfd_write(pt->dummy_pipe_fds[0], value);
|
||||
#else
|
||||
char buf = 0;
|
||||
int n;
|
||||
|
||||
n = (int)write(pt->dummy_pipe_fds[1], &buf, 1);
|
||||
|
||||
return n != 1;
|
||||
#endif
|
||||
}
|
||||
|
||||
void
|
||||
lws_plat_pipe_close(struct lws *wsi)
|
||||
{
|
||||
struct lws_context_per_thread *pt = &wsi->a.context->pt[(int)wsi->tsi];
|
||||
|
||||
if (pt->dummy_pipe_fds[0] && pt->dummy_pipe_fds[0] != -1) {
|
||||
close(pt->dummy_pipe_fds[0]);
|
||||
pt->dummy_pipe_fds[0] = -1;
|
||||
}
|
||||
if (pt->dummy_pipe_fds[1] && pt->dummy_pipe_fds[1] != -1) {
|
||||
close(pt->dummy_pipe_fds[1]);
|
||||
pt->dummy_pipe_fds[1] = -1;
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
lws_plat_pipe_is_fd_assocated(struct lws_context *cx, int tsi, lws_sockfd_type fd)
|
||||
{
|
||||
struct lws_context_per_thread *pt = &cx->pt[tsi];
|
||||
|
||||
return fd == pt->dummy_pipe_fds[0] || fd == pt->dummy_pipe_fds[1];
|
||||
}
|
124
Kinc/Sources/kinc/libs/plat/unix/unix-plugins.c
Normal file
124
Kinc/Sources/kinc/libs/plat/unix/unix-plugins.c
Normal file
@ -0,0 +1,124 @@
|
||||
/*
|
||||
* libwebsockets - small server side websockets and web server implementation
|
||||
*
|
||||
* Copyright (C) 2010 - 2020 Andy Green <andy@warmcat.com>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to
|
||||
* deal in the Software without restriction, including without limitation the
|
||||
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
* sell copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
* IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#if !defined(_GNU_SOURCE)
|
||||
#define _GNU_SOURCE
|
||||
#endif
|
||||
#include "private-lib-core.h"
|
||||
|
||||
#include <pwd.h>
|
||||
#include <grp.h>
|
||||
#include <dlfcn.h>
|
||||
|
||||
const lws_plugin_header_t *
|
||||
lws_plat_dlopen(struct lws_plugin **pplugin, const char *libpath,
|
||||
const char *sofilename, const char *_class,
|
||||
each_plugin_cb_t each, void *each_user)
|
||||
{
|
||||
const lws_plugin_header_t *hdr;
|
||||
struct lws_plugin *pin;
|
||||
char sym[96];
|
||||
void *l;
|
||||
int m;
|
||||
|
||||
if (strlen(sofilename) < 6)
|
||||
/* [lib]...[.so] */
|
||||
return NULL;
|
||||
|
||||
lwsl_info(" trying %s\n", libpath);
|
||||
|
||||
l = dlopen(libpath, RTLD_NOW);
|
||||
if (!l) {
|
||||
lwsl_info("%s: Error loading DSO: %s\n", __func__, dlerror());
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* we could open it... can we get his export struct? */
|
||||
m = lws_snprintf(sym, sizeof(sym) - 1, "%s", sofilename);
|
||||
if (m < 4)
|
||||
goto bail;
|
||||
if (!strcmp(&sym[m - 3], ".so"))
|
||||
sym[m - 3] = '\0'; /* snip the .so */
|
||||
|
||||
hdr = (const lws_plugin_header_t *)dlsym(l, sym);
|
||||
if (!hdr) {
|
||||
lwsl_info("%s: Failed to get export '%s' from %s: %s\n",
|
||||
__func__, sym, libpath, dlerror());
|
||||
goto bail;
|
||||
}
|
||||
|
||||
if (hdr->api_magic != LWS_PLUGIN_API_MAGIC) {
|
||||
lwsl_info("%s: plugin %s has outdated api %d (vs %d)\n",
|
||||
__func__, libpath, hdr->api_magic,
|
||||
LWS_PLUGIN_API_MAGIC);
|
||||
goto bail;
|
||||
}
|
||||
|
||||
if (strcmp(hdr->lws_build_hash, LWS_BUILD_HASH))
|
||||
goto bail;
|
||||
|
||||
if (strcmp(hdr->_class, _class))
|
||||
goto bail;
|
||||
|
||||
/*
|
||||
* We don't already have one of these, right?
|
||||
*/
|
||||
|
||||
pin = *pplugin;
|
||||
while (pin) {
|
||||
if (!strcmp(pin->hdr->name, hdr->name))
|
||||
goto bail;
|
||||
pin = pin->list;
|
||||
}
|
||||
|
||||
/*
|
||||
* OK let's bring it in
|
||||
*/
|
||||
|
||||
pin = lws_plugin_alloc(pplugin);
|
||||
if (!pin)
|
||||
goto bail;
|
||||
|
||||
pin->u.l = l;
|
||||
pin->hdr = hdr;
|
||||
|
||||
if (each)
|
||||
each(pin, each_user);
|
||||
|
||||
lwsl_notice(" %s\n", libpath);
|
||||
|
||||
return hdr;
|
||||
|
||||
bail:
|
||||
dlclose(l);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int
|
||||
lws_plat_destroy_dl(struct lws_plugin *p)
|
||||
{
|
||||
return dlclose(p->u.l);
|
||||
}
|
92
Kinc/Sources/kinc/libs/plat/unix/unix-resolv.c
Normal file
92
Kinc/Sources/kinc/libs/plat/unix/unix-resolv.c
Normal file
@ -0,0 +1,92 @@
|
||||
/*
|
||||
* libwebsockets - small server side websockets and web server implementation
|
||||
*
|
||||
* Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to
|
||||
* deal in the Software without restriction, including without limitation the
|
||||
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
* sell copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
* IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "private-lib-core.h"
|
||||
#include "private-lib-async-dns.h"
|
||||
|
||||
lws_async_dns_server_check_t
|
||||
lws_plat_asyncdns_init(struct lws_context *context, lws_async_dns_t *dns)
|
||||
{
|
||||
lws_async_dns_server_check_t s = LADNS_CONF_SERVER_SAME;
|
||||
lws_async_dns_server_t *dsrv;
|
||||
lws_sockaddr46 sa46t;
|
||||
lws_tokenize_t ts;
|
||||
char ads[48], *r;
|
||||
int fd, ns = 0;
|
||||
ssize_t n;
|
||||
|
||||
r = (char *)context->pt[0].serv_buf;
|
||||
|
||||
/* grab the first chunk of /etc/resolv.conf */
|
||||
|
||||
fd = open("/etc/resolv.conf", LWS_O_RDONLY);
|
||||
if (fd < 0)
|
||||
return LADNS_CONF_SERVER_UNKNOWN;
|
||||
|
||||
n = read(fd, r, context->pt_serv_buf_size - 1);
|
||||
close(fd);
|
||||
if (n < 0)
|
||||
return LADNS_CONF_SERVER_UNKNOWN;
|
||||
|
||||
r[n] = '\0';
|
||||
lws_tokenize_init(&ts, r, LWS_TOKENIZE_F_DOT_NONTERM |
|
||||
LWS_TOKENIZE_F_NO_FLOATS |
|
||||
LWS_TOKENIZE_F_NO_INTEGERS |
|
||||
LWS_TOKENIZE_F_MINUS_NONTERM |
|
||||
LWS_TOKENIZE_F_HASH_COMMENT);
|
||||
do {
|
||||
ts.e = (int8_t)lws_tokenize(&ts);
|
||||
if (ts.e != LWS_TOKZE_TOKEN) {
|
||||
ns = 0;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!ns && !strncmp("nameserver", ts.token, ts.token_len)) {
|
||||
ns = 1;
|
||||
continue;
|
||||
}
|
||||
if (!ns)
|
||||
continue;
|
||||
|
||||
/* we are a token just after the "nameserver" token */
|
||||
|
||||
ns = 0;
|
||||
if (ts.token_len > (int)sizeof(ads) - 1)
|
||||
continue;
|
||||
|
||||
memcpy(ads, ts.token, ts.token_len);
|
||||
ads[ts.token_len] = '\0';
|
||||
if (lws_sa46_parse_numeric_address(ads, &sa46t) < 0)
|
||||
continue;
|
||||
|
||||
dsrv = __lws_async_dns_server_find(dns, &sa46t);
|
||||
if (!dsrv) {
|
||||
__lws_async_dns_server_add(dns, &sa46t);
|
||||
s = LADNS_CONF_SERVER_CHANGED;
|
||||
}
|
||||
|
||||
} while (ts.e > 0);
|
||||
|
||||
return s;
|
||||
}
|
236
Kinc/Sources/kinc/libs/plat/unix/unix-service.c
Normal file
236
Kinc/Sources/kinc/libs/plat/unix/unix-service.c
Normal file
@ -0,0 +1,236 @@
|
||||
/*
|
||||
* libwebsockets - small server side websockets and web server implementation
|
||||
*
|
||||
* Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to
|
||||
* deal in the Software without restriction, including without limitation the
|
||||
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
* sell copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
* IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#if !defined(_GNU_SOURCE)
|
||||
#define _GNU_SOURCE
|
||||
#endif
|
||||
#include "private-lib-core.h"
|
||||
|
||||
int
|
||||
lws_poll_listen_fd(struct lws_pollfd *fd)
|
||||
{
|
||||
return poll(fd, 1, 0);
|
||||
}
|
||||
|
||||
int
|
||||
_lws_plat_service_forced_tsi(struct lws_context *context, int tsi)
|
||||
{
|
||||
struct lws_context_per_thread *pt = &context->pt[tsi];
|
||||
int m, n, r;
|
||||
|
||||
r = lws_service_flag_pending(context, tsi);
|
||||
|
||||
/* any socket with events to service? */
|
||||
for (n = 0; n < (int)pt->fds_count; n++) {
|
||||
lws_sockfd_type fd = pt->fds[n].fd;
|
||||
|
||||
if (!pt->fds[n].revents)
|
||||
continue;
|
||||
|
||||
m = lws_service_fd_tsi(context, &pt->fds[n], tsi);
|
||||
if (m < 0) {
|
||||
lwsl_err("%s: lws_service_fd_tsi returned %d\n",
|
||||
__func__, m);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* if something closed, retry this slot since may have been
|
||||
* swapped with end fd */
|
||||
if (m && pt->fds[n].fd != fd)
|
||||
n--;
|
||||
}
|
||||
|
||||
lws_service_do_ripe_rxflow(pt);
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
#define LWS_POLL_WAIT_LIMIT 2000000000
|
||||
|
||||
int
|
||||
_lws_plat_service_tsi(struct lws_context *context, int timeout_ms, int tsi)
|
||||
{
|
||||
volatile struct lws_foreign_thread_pollfd *ftp, *next;
|
||||
volatile struct lws_context_per_thread *vpt;
|
||||
struct lws_context_per_thread *pt;
|
||||
lws_usec_t timeout_us, us;
|
||||
#if defined(LWS_WITH_SYS_METRICS)
|
||||
lws_usec_t a, b;
|
||||
#endif
|
||||
int n;
|
||||
#if (defined(LWS_ROLE_WS) && !defined(LWS_WITHOUT_EXTENSIONS)) || defined(LWS_WITH_TLS)
|
||||
int m;
|
||||
#endif
|
||||
|
||||
/* stay dead once we are dead */
|
||||
|
||||
if (!context)
|
||||
return 1;
|
||||
|
||||
#if defined(LWS_WITH_SYS_METRICS)
|
||||
b =
|
||||
#endif
|
||||
us = lws_now_usecs();
|
||||
|
||||
pt = &context->pt[tsi];
|
||||
vpt = (volatile struct lws_context_per_thread *)pt;
|
||||
|
||||
if (timeout_ms < 0)
|
||||
timeout_ms = 0;
|
||||
else
|
||||
/* force a default timeout of 23 days */
|
||||
timeout_ms = LWS_POLL_WAIT_LIMIT;
|
||||
timeout_us = ((lws_usec_t)timeout_ms) * LWS_US_PER_MS;
|
||||
|
||||
if (context->event_loop_ops->run_pt)
|
||||
context->event_loop_ops->run_pt(context, tsi);
|
||||
|
||||
if (!pt->service_tid_detected && context->vhost_list) {
|
||||
lws_fakewsi_def_plwsa(pt);
|
||||
|
||||
lws_fakewsi_prep_plwsa_ctx(context);
|
||||
|
||||
pt->service_tid = context->vhost_list->protocols[0].callback(
|
||||
(struct lws *)plwsa,
|
||||
LWS_CALLBACK_GET_THREAD_ID,
|
||||
NULL, NULL, 0);
|
||||
pt->service_tid_detected = 1;
|
||||
}
|
||||
|
||||
lws_pt_lock(pt, __func__);
|
||||
/*
|
||||
* service ripe scheduled events, and limit wait to next expected one
|
||||
*/
|
||||
us = __lws_sul_service_ripe(pt->pt_sul_owner, LWS_COUNT_PT_SUL_OWNERS, us);
|
||||
if (us && us < timeout_us)
|
||||
/*
|
||||
* If something wants zero wait, that's OK, but if the next sul
|
||||
* coming ripe is an interval less than our wait resolution,
|
||||
* bump it to be the wait resolution.
|
||||
*/
|
||||
timeout_us = us < context->us_wait_resolution ?
|
||||
context->us_wait_resolution : us;
|
||||
|
||||
lws_pt_unlock(pt);
|
||||
|
||||
/*
|
||||
* is there anybody with pending stuff that needs service forcing?
|
||||
*/
|
||||
if (!lws_service_adjust_timeout(context, 1, tsi))
|
||||
timeout_us = 0;
|
||||
|
||||
/* ensure we don't wrap at 2^31 with poll()'s signed int ms */
|
||||
|
||||
timeout_us /= LWS_US_PER_MS; /* ms now */
|
||||
|
||||
#if defined(LWS_WITH_SYS_METRICS)
|
||||
a = lws_now_usecs() - b;
|
||||
#endif
|
||||
vpt->inside_poll = 1;
|
||||
lws_memory_barrier();
|
||||
n = poll(pt->fds, pt->fds_count, (int)timeout_us /* ms now */ );
|
||||
vpt->inside_poll = 0;
|
||||
lws_memory_barrier();
|
||||
|
||||
#if defined(LWS_WITH_SYS_METRICS)
|
||||
b = lws_now_usecs();
|
||||
#endif
|
||||
/* Collision will be rare and brief. Spin until it completes */
|
||||
while (vpt->foreign_spinlock)
|
||||
;
|
||||
|
||||
/*
|
||||
* At this point we are not inside a foreign thread pollfd
|
||||
* change, and we have marked ourselves as outside the poll()
|
||||
* wait. So we are the only guys that can modify the
|
||||
* lws_foreign_thread_pollfd list on the pt. Drain the list
|
||||
* and apply the changes to the affected pollfds in the correct
|
||||
* order.
|
||||
*/
|
||||
|
||||
lws_pt_lock(pt, __func__);
|
||||
|
||||
ftp = vpt->foreign_pfd_list;
|
||||
//lwsl_notice("cleared list %p\n", ftp);
|
||||
while (ftp) {
|
||||
struct lws *wsi;
|
||||
struct lws_pollfd *pfd;
|
||||
|
||||
next = ftp->next;
|
||||
pfd = &vpt->fds[ftp->fd_index];
|
||||
if (lws_socket_is_valid(pfd->fd)) {
|
||||
wsi = wsi_from_fd(context, pfd->fd);
|
||||
if (wsi)
|
||||
__lws_change_pollfd(wsi, ftp->_and,
|
||||
ftp->_or);
|
||||
}
|
||||
lws_free((void *)ftp);
|
||||
ftp = next;
|
||||
}
|
||||
vpt->foreign_pfd_list = NULL;
|
||||
lws_memory_barrier();
|
||||
|
||||
lws_pt_unlock(pt);
|
||||
|
||||
#if (defined(LWS_ROLE_WS) && !defined(LWS_WITHOUT_EXTENSIONS)) || defined(LWS_WITH_TLS)
|
||||
m = 0;
|
||||
#endif
|
||||
#if defined(LWS_ROLE_WS) && !defined(LWS_WITHOUT_EXTENSIONS)
|
||||
m |= !!pt->ws.rx_draining_ext_list;
|
||||
#endif
|
||||
|
||||
#if defined(LWS_WITH_TLS)
|
||||
if (pt->context->tls_ops &&
|
||||
pt->context->tls_ops->fake_POLLIN_for_buffered)
|
||||
m |= pt->context->tls_ops->fake_POLLIN_for_buffered(pt);
|
||||
#endif
|
||||
|
||||
if (
|
||||
#if (defined(LWS_ROLE_WS) && !defined(LWS_WITHOUT_EXTENSIONS)) || defined(LWS_WITH_TLS)
|
||||
!m &&
|
||||
#endif
|
||||
!n) /* nothing to do */
|
||||
lws_service_do_ripe_rxflow(pt);
|
||||
else
|
||||
if (_lws_plat_service_forced_tsi(context, tsi) < 0)
|
||||
return -1;
|
||||
|
||||
#if defined(LWS_WITH_SYS_METRICS)
|
||||
lws_metric_event(context->mt_service, METRES_GO,
|
||||
(u_mt_t) (a + (lws_now_usecs() - b)));
|
||||
#endif
|
||||
|
||||
if (pt->destroy_self) {
|
||||
lws_context_destroy(pt->context);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
lws_plat_service(struct lws_context *context, int timeout_ms)
|
||||
{
|
||||
return _lws_plat_service_tsi(context, timeout_ms, 0);
|
||||
}
|
685
Kinc/Sources/kinc/libs/plat/unix/unix-sockets.c
Normal file
685
Kinc/Sources/kinc/libs/plat/unix/unix-sockets.c
Normal file
@ -0,0 +1,685 @@
|
||||
/*
|
||||
* libwebsockets - small server side websockets and web server implementation
|
||||
*
|
||||
* Copyright (C) 2010 - 2023 Andy Green <andy@warmcat.com>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to
|
||||
* deal in the Software without restriction, including without limitation the
|
||||
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
* sell copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
* IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#if !defined(_GNU_SOURCE)
|
||||
#define _GNU_SOURCE
|
||||
#endif
|
||||
#include "private-lib-core.h"
|
||||
|
||||
#if defined(LWS_HAVE_LINUX_IPV6_H)
|
||||
#include <linux/ipv6.h>
|
||||
#endif
|
||||
|
||||
#include <sys/ioctl.h>
|
||||
|
||||
#if !defined(LWS_DETECTED_PLAT_IOS)
|
||||
#include <net/route.h>
|
||||
#endif
|
||||
|
||||
#include <net/if.h>
|
||||
|
||||
#include <pwd.h>
|
||||
#include <grp.h>
|
||||
|
||||
#if defined(LWS_WITH_MBEDTLS)
|
||||
#if defined(LWS_HAVE_MBEDTLS_NET_SOCKETS)
|
||||
#include "mbedtls/net_sockets.h"
|
||||
#else
|
||||
#include "mbedtls/net.h"
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#include <netinet/ip.h>
|
||||
|
||||
int
|
||||
lws_send_pipe_choked(struct lws *wsi)
|
||||
{
|
||||
struct lws_pollfd fds;
|
||||
struct lws *wsi_eff;
|
||||
|
||||
#if !defined(LWS_WITHOUT_EXTENSIONS)
|
||||
if (wsi->ws && wsi->ws->tx_draining_ext)
|
||||
return 1;
|
||||
#endif
|
||||
|
||||
#if defined(LWS_WITH_HTTP2)
|
||||
wsi_eff = lws_get_network_wsi(wsi);
|
||||
#else
|
||||
wsi_eff = wsi;
|
||||
#endif
|
||||
|
||||
/* the fact we checked implies we avoided back-to-back writes */
|
||||
wsi_eff->could_have_pending = 0;
|
||||
|
||||
/* treat the fact we got a truncated send pending as if we're choked */
|
||||
if (lws_has_buffered_out(wsi_eff)
|
||||
#if defined(LWS_WITH_HTTP_STREAM_COMPRESSION)
|
||||
||wsi->http.comp_ctx.buflist_comp ||
|
||||
wsi->http.comp_ctx.may_have_more
|
||||
#endif
|
||||
)
|
||||
return 1;
|
||||
|
||||
fds.fd = wsi_eff->desc.sockfd;
|
||||
fds.events = POLLOUT;
|
||||
fds.revents = 0;
|
||||
|
||||
if (poll(&fds, 1, 0) != 1)
|
||||
return 1;
|
||||
|
||||
if ((fds.revents & POLLOUT) == 0)
|
||||
return 1;
|
||||
|
||||
/* okay to send another packet without blocking */
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
lws_plat_set_nonblocking(lws_sockfd_type fd)
|
||||
{
|
||||
return fcntl(fd, F_SETFL, O_NONBLOCK) < 0;
|
||||
}
|
||||
|
||||
int
|
||||
lws_plat_set_socket_options(struct lws_vhost *vhost, int fd, int unix_skt)
|
||||
{
|
||||
int optval = 1;
|
||||
socklen_t optlen = sizeof(optval);
|
||||
|
||||
#if defined(__APPLE__) || \
|
||||
defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || \
|
||||
defined(__NetBSD__) || \
|
||||
defined(__OpenBSD__) || \
|
||||
defined(__HAIKU__)
|
||||
struct protoent *tcp_proto;
|
||||
#endif
|
||||
|
||||
(void)fcntl(fd, F_SETFD, FD_CLOEXEC);
|
||||
|
||||
if (!unix_skt && vhost->ka_time) {
|
||||
/* enable keepalive on this socket */
|
||||
optval = 1;
|
||||
if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE,
|
||||
(const void *)&optval, optlen) < 0)
|
||||
return 1;
|
||||
|
||||
#if defined(__APPLE__) || \
|
||||
defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || \
|
||||
defined(__NetBSD__) || \
|
||||
defined(__CYGWIN__) || defined(__OpenBSD__) || defined (__sun) || \
|
||||
defined(__HAIKU__)
|
||||
|
||||
/*
|
||||
* didn't find a way to set these per-socket, need to
|
||||
* tune kernel systemwide values
|
||||
*/
|
||||
#else
|
||||
/* set the keepalive conditions we want on it too */
|
||||
|
||||
#if defined(LWS_HAVE_TCP_USER_TIMEOUT)
|
||||
optval = 1000 * (vhost->ka_time +
|
||||
(vhost->ka_interval * vhost->ka_probes));
|
||||
if (setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT,
|
||||
(const void *)&optval, optlen) < 0)
|
||||
return 1;
|
||||
#endif
|
||||
optval = vhost->ka_time;
|
||||
if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE,
|
||||
(const void *)&optval, optlen) < 0)
|
||||
return 1;
|
||||
|
||||
optval = vhost->ka_interval;
|
||||
if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL,
|
||||
(const void *)&optval, optlen) < 0)
|
||||
return 1;
|
||||
|
||||
optval = vhost->ka_probes;
|
||||
if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT,
|
||||
(const void *)&optval, optlen) < 0)
|
||||
return 1;
|
||||
#endif
|
||||
}
|
||||
|
||||
#if defined(SO_BINDTODEVICE)
|
||||
if (!unix_skt && vhost->bind_iface && vhost->iface) {
|
||||
lwsl_info("binding listen skt to %s using SO_BINDTODEVICE\n", vhost->iface);
|
||||
if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, vhost->iface,
|
||||
(socklen_t)strlen(vhost->iface)) < 0) {
|
||||
lwsl_warn("Failed to bind to device %s\n", vhost->iface);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
/* Disable Nagle */
|
||||
optval = 1;
|
||||
#if defined (__sun) || defined(__QNX__) || defined(__NuttX__)
|
||||
if (!unix_skt && setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (const void *)&optval, optlen) < 0)
|
||||
return 1;
|
||||
#elif !defined(__APPLE__) && \
|
||||
!defined(__FreeBSD__) && !defined(__FreeBSD_kernel__) && \
|
||||
!defined(__NetBSD__) && \
|
||||
!defined(__OpenBSD__) && \
|
||||
!defined(__HAIKU__)
|
||||
if (!unix_skt && setsockopt(fd, SOL_TCP, TCP_NODELAY, (const void *)&optval, optlen) < 0)
|
||||
return 1;
|
||||
#else
|
||||
tcp_proto = getprotobyname("TCP");
|
||||
if (!unix_skt && setsockopt(fd, tcp_proto->p_proto, TCP_NODELAY, &optval, optlen) < 0)
|
||||
return 1;
|
||||
#endif
|
||||
|
||||
return lws_plat_set_nonblocking(fd);
|
||||
}
|
||||
|
||||
#if !defined(__NuttX__)
|
||||
static const int ip_opt_lws_flags[] = {
|
||||
LCCSCF_IP_LOW_LATENCY, LCCSCF_IP_HIGH_THROUGHPUT
|
||||
#if !defined(__OpenBSD__) && !defined(__sun) && !defined(__QNX__)
|
||||
, LCCSCF_IP_HIGH_RELIABILITY
|
||||
, LCCSCF_IP_LOW_COST
|
||||
#endif
|
||||
}, ip_opt_val[] = {
|
||||
IPTOS_LOWDELAY, IPTOS_THROUGHPUT
|
||||
#if !defined(__OpenBSD__) && !defined(__sun) && !defined(__QNX__)
|
||||
, IPTOS_RELIABILITY
|
||||
, IPTOS_MINCOST
|
||||
#endif
|
||||
};
|
||||
#if !defined(LWS_WITH_NO_LOGS)
|
||||
static const char *ip_opt_names[] = {
|
||||
"LOWDELAY", "THROUGHPUT"
|
||||
#if !defined(__OpenBSD__) && !defined(__sun) && !defined(__QNX__)
|
||||
, "RELIABILITY"
|
||||
, "MINCOST"
|
||||
#endif
|
||||
};
|
||||
#endif
|
||||
#endif
|
||||
|
||||
int
|
||||
lws_plat_set_socket_options_ip(lws_sockfd_type fd, uint8_t pri, int lws_flags)
|
||||
{
|
||||
int optval = (int)pri, ret = 0, n;
|
||||
socklen_t optlen = sizeof(optval);
|
||||
#if (_LWS_ENABLED_LOGS & LLL_WARN)
|
||||
int en;
|
||||
#endif
|
||||
|
||||
#if 0
|
||||
#if defined(TCP_FASTOPEN_CONNECT)
|
||||
optval = 1;
|
||||
if (setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN_CONNECT, (void *)&optval,
|
||||
sizeof(optval)))
|
||||
lwsl_warn("%s: FASTOPEN_CONNECT failed\n", __func__);
|
||||
optval = (int)pri;
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if !defined(__APPLE__) && \
|
||||
!defined(__FreeBSD__) && !defined(__FreeBSD_kernel__) && \
|
||||
!defined(__NetBSD__) && \
|
||||
!defined(__OpenBSD__) && \
|
||||
!defined(__sun) && \
|
||||
!defined(__HAIKU__) && \
|
||||
!defined(__CYGWIN__) && \
|
||||
!defined(__QNX__) && \
|
||||
!defined(__NuttX__)
|
||||
|
||||
/* the BSDs don't have SO_PRIORITY */
|
||||
|
||||
if (pri) { /* 0 is the default already */
|
||||
if (setsockopt(fd, SOL_SOCKET, SO_PRIORITY,
|
||||
(const void *)&optval, optlen) < 0) {
|
||||
#if (_LWS_ENABLED_LOGS & LLL_WARN)
|
||||
en = errno;
|
||||
lwsl_warn("%s: unable to set socket pri %d: errno %d\n",
|
||||
__func__, (int)pri, en);
|
||||
#endif
|
||||
ret = 1;
|
||||
} else
|
||||
lwsl_notice("%s: set pri %u\n", __func__, pri);
|
||||
}
|
||||
#endif
|
||||
|
||||
if (lws_flags & LCCSCF_ALLOW_REUSE_ADDR) {
|
||||
optval = 1;
|
||||
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
|
||||
(const void *)&optval, optlen) < 0) {
|
||||
#if (_LWS_ENABLED_LOGS & LLL_WARN)
|
||||
en = errno;
|
||||
lwsl_warn("%s: unable to reuse local addresses: errno %d\n",
|
||||
__func__, en);
|
||||
#endif
|
||||
ret = 1;
|
||||
} else
|
||||
lwsl_notice("%s: set reuse addresses\n", __func__);
|
||||
}
|
||||
|
||||
|
||||
if (lws_flags & LCCSCF_IPV6_PREFER_PUBLIC_ADDR) {
|
||||
#if defined(LWS_WITH_IPV6) && defined(IPV6_PREFER_SRC_PUBLIC)
|
||||
optval = IPV6_PREFER_SRC_PUBLIC;
|
||||
|
||||
if (setsockopt(fd, IPPROTO_IPV6, IPV6_ADDR_PREFERENCES,
|
||||
(const void *)&optval, optlen) < 0) {
|
||||
#if (_LWS_ENABLED_LOGS & LLL_WARN)
|
||||
en = errno;
|
||||
lwsl_warn("%s: unable to set IPV6_PREFER_SRC_PUBLIC: errno %d\n",
|
||||
__func__, en);
|
||||
#endif
|
||||
ret = 1;
|
||||
} else
|
||||
lwsl_notice("%s: set IPV6_PREFER_SRC_PUBLIC\n", __func__);
|
||||
#else
|
||||
lwsl_err("%s: IPV6_PREFER_SRC_PUBLIC UNIMPLEMENTED on this platform\n", __func__);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
#if !defined(__NuttX__)
|
||||
/* array size differs by platform */
|
||||
for (n = 0; n < (int)LWS_ARRAY_SIZE(ip_opt_lws_flags); n++) {
|
||||
if (!(lws_flags & ip_opt_lws_flags[n]))
|
||||
continue;
|
||||
|
||||
optval = (int)ip_opt_val[n];
|
||||
if (setsockopt(fd, IPPROTO_IP, IP_TOS, (const void *)&optval,
|
||||
optlen) < 0) {
|
||||
#if !defined(LWS_WITH_NO_LOGS)
|
||||
en = errno;
|
||||
lwsl_warn("%s: unable to set %s: errno %d\n", __func__,
|
||||
ip_opt_names[n], en);
|
||||
#endif
|
||||
ret = 1;
|
||||
} else
|
||||
lwsl_notice("%s: set ip flag %s\n", __func__,
|
||||
ip_opt_names[n]);
|
||||
}
|
||||
#endif
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* cast a struct sockaddr_in6 * into addr for ipv6 */
|
||||
|
||||
enum {
|
||||
IP_SCORE_NONE,
|
||||
IP_SCORE_NONNATIVE,
|
||||
IP_SCORE_IPV6_SCOPE_BASE,
|
||||
/* ipv6 scopes */
|
||||
IP_SCORE_GLOBAL_NATIVE = 18
|
||||
};
|
||||
|
||||
int
|
||||
lws_interface_to_sa(int ipv6, const char *ifname, struct sockaddr_in *addr,
|
||||
size_t addrlen)
|
||||
{
|
||||
int rc = LWS_ITOSA_NOT_EXIST;
|
||||
|
||||
struct ifaddrs *ifr;
|
||||
struct ifaddrs *ifc;
|
||||
#if defined(LWS_WITH_IPV6)
|
||||
struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)addr;
|
||||
unsigned long sco = IP_SCORE_NONE;
|
||||
unsigned long ts;
|
||||
const uint8_t *p;
|
||||
#endif
|
||||
|
||||
if (getifaddrs(&ifr)) {
|
||||
lwsl_err("%s: unable to getifaddrs: errno %d\n", __func__, errno);
|
||||
|
||||
return LWS_ITOSA_USABLE;
|
||||
}
|
||||
for (ifc = ifr; ifc != NULL; ifc = ifc->ifa_next) {
|
||||
if (!ifc->ifa_addr || !ifc->ifa_name)
|
||||
continue;
|
||||
|
||||
lwsl_debug(" interface %s vs %s (fam %d) ipv6 %d\n",
|
||||
ifc->ifa_name, ifname,
|
||||
ifc->ifa_addr->sa_family, ipv6);
|
||||
|
||||
if (strcmp(ifc->ifa_name, ifname))
|
||||
continue;
|
||||
|
||||
switch (ifc->ifa_addr->sa_family) {
|
||||
#if defined(AF_PACKET)
|
||||
case AF_PACKET:
|
||||
/* interface exists but is not usable */
|
||||
if (rc == LWS_ITOSA_NOT_EXIST)
|
||||
rc = LWS_ITOSA_NOT_USABLE;
|
||||
continue;
|
||||
#endif
|
||||
|
||||
case AF_INET:
|
||||
#if defined(LWS_WITH_IPV6)
|
||||
if (ipv6) {
|
||||
/* any existing solution is better than this */
|
||||
if (sco != IP_SCORE_NONE)
|
||||
break;
|
||||
sco = IP_SCORE_NONNATIVE;
|
||||
rc = LWS_ITOSA_USABLE;
|
||||
/* map IPv4 to IPv6 */
|
||||
memset((char *)&addr6->sin6_addr, 0,
|
||||
sizeof(struct in6_addr));
|
||||
addr6->sin6_addr.s6_addr[10] = 0xff;
|
||||
addr6->sin6_addr.s6_addr[11] = 0xff;
|
||||
memcpy(&addr6->sin6_addr.s6_addr[12],
|
||||
&((struct sockaddr_in *)ifc->ifa_addr)->sin_addr,
|
||||
sizeof(struct in_addr));
|
||||
lwsl_debug("%s: uplevelling ipv4 bind to ipv6\n", __func__);
|
||||
break;
|
||||
}
|
||||
|
||||
sco = IP_SCORE_GLOBAL_NATIVE;
|
||||
#endif
|
||||
rc = LWS_ITOSA_USABLE;
|
||||
memcpy(addr, (struct sockaddr_in *)ifc->ifa_addr,
|
||||
sizeof(struct sockaddr_in));
|
||||
break;
|
||||
#if defined(LWS_WITH_IPV6)
|
||||
case AF_INET6:
|
||||
p = (const uint8_t *)
|
||||
&((struct sockaddr_in6 *)ifc->ifa_addr)->sin6_addr;
|
||||
ts = IP_SCORE_IPV6_SCOPE_BASE;
|
||||
if (p[0] == 0xff)
|
||||
ts = (unsigned long)(IP_SCORE_IPV6_SCOPE_BASE + (p[1] & 0xf));
|
||||
|
||||
if (sco >= ts)
|
||||
break;
|
||||
|
||||
sco = ts;
|
||||
rc = LWS_ITOSA_USABLE;
|
||||
|
||||
memcpy(&addr6->sin6_addr,
|
||||
&((struct sockaddr_in6 *)ifc->ifa_addr)->sin6_addr,
|
||||
sizeof(struct in6_addr));
|
||||
break;
|
||||
#endif
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
freeifaddrs(ifr);
|
||||
|
||||
if (rc &&
|
||||
!lws_sa46_parse_numeric_address(ifname, (lws_sockaddr46 *)addr))
|
||||
rc = LWS_ITOSA_USABLE;
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
||||
const char *
|
||||
lws_plat_inet_ntop(int af, const void *src, char *dst, socklen_t cnt)
|
||||
{
|
||||
return inet_ntop(af, src, dst, cnt);
|
||||
}
|
||||
|
||||
int
|
||||
lws_plat_inet_pton(int af, const char *src, void *dst)
|
||||
{
|
||||
return inet_pton(af, src, dst);
|
||||
}
|
||||
|
||||
int
|
||||
lws_plat_ifname_to_hwaddr(int fd, const char *ifname, uint8_t *hwaddr, int len)
|
||||
{
|
||||
#if defined(__linux__)
|
||||
struct ifreq i;
|
||||
|
||||
memset(&i, 0, sizeof(i));
|
||||
lws_strncpy(i.ifr_name, ifname, sizeof(i.ifr_name));
|
||||
|
||||
if (ioctl(fd, SIOCGIFHWADDR, &i) < 0)
|
||||
return -1;
|
||||
|
||||
memcpy(hwaddr, &i.ifr_hwaddr.sa_data, 6);
|
||||
|
||||
return 6;
|
||||
#else
|
||||
lwsl_err("%s: UNIMPLEMENTED on this platform\n", __func__);
|
||||
|
||||
return -1;
|
||||
#endif
|
||||
}
|
||||
|
||||
int
|
||||
lws_plat_rawudp_broadcast(uint8_t *p, const uint8_t *canned, size_t canned_len,
|
||||
size_t n, int fd, const char *iface)
|
||||
{
|
||||
#if defined(__linux__)
|
||||
struct sockaddr_ll sll;
|
||||
uint16_t *p16 = (uint16_t *)p;
|
||||
uint32_t ucs = 0;
|
||||
|
||||
memcpy(p, canned, canned_len);
|
||||
|
||||
p[2] = (uint8_t)(n >> 8);
|
||||
p[3] = (uint8_t)(n);
|
||||
|
||||
while (p16 < (uint16_t *)(p + 20))
|
||||
ucs = ucs + (uint32_t)(ntohs((uint16_t)(*p16++)));
|
||||
|
||||
ucs += ucs >> 16;
|
||||
ucs ^= 0xffff;
|
||||
|
||||
p[10] = (uint8_t)(ucs >> 8);
|
||||
p[11] = (uint8_t)(ucs);
|
||||
p[24] = (uint8_t)((n - 20) >> 8);
|
||||
p[25] = (uint8_t)((n - 20));
|
||||
|
||||
memset(&sll, 0, sizeof(sll));
|
||||
sll.sll_family = AF_PACKET;
|
||||
sll.sll_protocol = (uint32_t)(htons((uint16_t)0x800));
|
||||
sll.sll_halen = 6;
|
||||
sll.sll_ifindex = (int)if_nametoindex(iface);
|
||||
memset(sll.sll_addr, 0xff, 6);
|
||||
|
||||
return (int)sendto(fd, p, n, 0, (struct sockaddr *)&sll, sizeof(sll));
|
||||
#else
|
||||
lwsl_err("%s: UNIMPLEMENTED on this platform\n", __func__);
|
||||
|
||||
return -1;
|
||||
#endif
|
||||
}
|
||||
|
||||
int
|
||||
lws_plat_if_up(const char *ifname, int fd, int up)
|
||||
{
|
||||
#if defined(__linux__)
|
||||
struct ifreq ifr;
|
||||
|
||||
memset(&ifr, 0, sizeof(ifr));
|
||||
lws_strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
|
||||
|
||||
if (ioctl(fd, SIOCGIFFLAGS, &ifr) < 0) {
|
||||
lwsl_err("%s: SIOCGIFFLAGS fail\n", __func__);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (up)
|
||||
ifr.ifr_flags |= IFF_UP;
|
||||
else
|
||||
ifr.ifr_flags &= ~IFF_UP;
|
||||
|
||||
if (ioctl(fd, SIOCSIFFLAGS, &ifr) < 0) {
|
||||
lwsl_err("%s: SIOCSIFFLAGS fail\n", __func__);
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
#else
|
||||
lwsl_err("%s: UNIMPLEMENTED on this platform\n", __func__);
|
||||
|
||||
return -1;
|
||||
#endif
|
||||
}
|
||||
|
||||
int
|
||||
lws_plat_BINDTODEVICE(lws_sockfd_type fd, const char *ifname)
|
||||
{
|
||||
#if defined(__linux__)
|
||||
struct ifreq i;
|
||||
|
||||
memset(&i, 0, sizeof(i));
|
||||
i.ifr_addr.sa_family = AF_INET;
|
||||
lws_strncpy(i.ifr_ifrn.ifrn_name, ifname,
|
||||
sizeof(i.ifr_ifrn.ifrn_name));
|
||||
if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, &i, sizeof(i)) < 0) {
|
||||
lwsl_notice("%s: failed %d\n", __func__, LWS_ERRNO);
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
#else
|
||||
lwsl_err("%s: UNIMPLEMENTED on this platform\n", __func__);
|
||||
|
||||
return -1;
|
||||
#endif
|
||||
}
|
||||
|
||||
int
|
||||
lws_plat_ifconfig(int fd, lws_dhcpc_ifstate_t *is)
|
||||
{
|
||||
#if defined(__linux__)
|
||||
struct rtentry route;
|
||||
struct ifreq ifr;
|
||||
|
||||
memset(&ifr, 0, sizeof(ifr));
|
||||
memset(&route, 0, sizeof(route));
|
||||
|
||||
lws_strncpy(ifr.ifr_name, is->ifname, IFNAMSIZ);
|
||||
|
||||
lws_plat_if_up(is->ifname, fd, 0);
|
||||
|
||||
memcpy(&ifr.ifr_addr, &is->sa46[LWSDH_SA46_IP], sizeof(struct sockaddr));
|
||||
if (ioctl(fd, SIOCSIFADDR, &ifr) < 0) {
|
||||
lwsl_err("%s: SIOCSIFADDR fail\n", __func__);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (is->sa46[LWSDH_SA46_IP].sa4.sin_family == AF_INET) {
|
||||
struct sockaddr_in sin;
|
||||
|
||||
memset(&sin, 0, sizeof(sin));
|
||||
sin.sin_family = AF_INET;
|
||||
sin.sin_addr.s_addr = *(uint32_t *)&is->nums[LWSDH_IPV4_SUBNET_MASK];
|
||||
memcpy(&ifr.ifr_addr, &sin, sizeof(struct sockaddr));
|
||||
if (ioctl(fd, SIOCSIFNETMASK, &ifr) < 0) {
|
||||
lwsl_err("%s: SIOCSIFNETMASK fail\n", __func__);
|
||||
return 1;
|
||||
}
|
||||
|
||||
lws_plat_if_up(is->ifname, fd, 1);
|
||||
|
||||
memcpy(&route.rt_gateway,
|
||||
&is->sa46[LWSDH_SA46_IPV4_ROUTER].sa4,
|
||||
sizeof(struct sockaddr));
|
||||
|
||||
sin.sin_addr.s_addr = 0;
|
||||
memcpy(&route.rt_dst, &sin, sizeof(struct sockaddr));
|
||||
memcpy(&route.rt_genmask, &sin, sizeof(struct sockaddr));
|
||||
|
||||
route.rt_flags = RTF_UP | RTF_GATEWAY;
|
||||
route.rt_metric = 100;
|
||||
route.rt_dev = (char *)is->ifname;
|
||||
|
||||
if (ioctl(fd, SIOCADDRT, &route) < 0) {
|
||||
lwsl_err("%s: SIOCADDRT 0x%x fail: %d\n", __func__,
|
||||
(unsigned int)htonl(*(uint32_t *)&is->
|
||||
sa46[LWSDH_SA46_IPV4_ROUTER].
|
||||
sa4.sin_addr.s_addr), LWS_ERRNO);
|
||||
return 1;
|
||||
}
|
||||
} else
|
||||
lws_plat_if_up(is->ifname, fd, 1);
|
||||
|
||||
return 0;
|
||||
#else
|
||||
lwsl_err("%s: UNIMPLEMENTED on this platform\n", __func__);
|
||||
|
||||
return -1;
|
||||
#endif
|
||||
}
|
||||
|
||||
int
|
||||
lws_plat_vhost_tls_client_ctx_init(struct lws_vhost *vhost)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
#if defined(LWS_WITH_MBEDTLS)
|
||||
int
|
||||
lws_plat_mbedtls_net_send(void *ctx, const uint8_t *buf, size_t len)
|
||||
{
|
||||
int fd = ((mbedtls_net_context *) ctx)->MBEDTLS_PRIVATE_V30_ONLY(fd);
|
||||
int ret;
|
||||
|
||||
if (fd < 0)
|
||||
return MBEDTLS_ERR_NET_INVALID_CONTEXT;
|
||||
|
||||
ret = (int)write(fd, buf, len);
|
||||
if (ret >= 0)
|
||||
return ret;
|
||||
|
||||
if (errno == EAGAIN || errno == EWOULDBLOCK)
|
||||
return MBEDTLS_ERR_SSL_WANT_WRITE;
|
||||
|
||||
if (errno == EPIPE || errno == ECONNRESET)
|
||||
return MBEDTLS_ERR_NET_CONN_RESET;
|
||||
|
||||
if( errno == EINTR )
|
||||
return MBEDTLS_ERR_SSL_WANT_WRITE;
|
||||
|
||||
return MBEDTLS_ERR_NET_SEND_FAILED;
|
||||
}
|
||||
|
||||
int
|
||||
lws_plat_mbedtls_net_recv(void *ctx, unsigned char *buf, size_t len)
|
||||
{
|
||||
int fd = ((mbedtls_net_context *) ctx)->MBEDTLS_PRIVATE_V30_ONLY(fd);
|
||||
int ret;
|
||||
|
||||
if (fd < 0)
|
||||
return MBEDTLS_ERR_NET_INVALID_CONTEXT;
|
||||
|
||||
ret = (int)read(fd, buf, len);
|
||||
if (ret >= 0)
|
||||
return ret;
|
||||
|
||||
if (errno == EAGAIN || errno == EWOULDBLOCK)
|
||||
return MBEDTLS_ERR_SSL_WANT_READ;
|
||||
|
||||
if (errno == EPIPE || errno == ECONNRESET)
|
||||
return MBEDTLS_ERR_NET_CONN_RESET;
|
||||
|
||||
if (errno == EINTR)
|
||||
return MBEDTLS_ERR_SSL_WANT_READ;
|
||||
|
||||
return MBEDTLS_ERR_NET_RECV_FAILED;
|
||||
}
|
||||
#endif
|
615
Kinc/Sources/kinc/libs/plat/unix/unix-spawn.c
Normal file
615
Kinc/Sources/kinc/libs/plat/unix/unix-spawn.c
Normal file
@ -0,0 +1,615 @@
|
||||
/*
|
||||
* libwebsockets - small server side websockets and web server implementation
|
||||
*
|
||||
* Copyright (C) 2010 - 2020 Andy Green <andy@warmcat.com>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to
|
||||
* deal in the Software without restriction, including without limitation the
|
||||
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
* sell copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
* IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#if !defined(_GNU_SOURCE)
|
||||
#define _GNU_SOURCE
|
||||
#endif
|
||||
|
||||
#include "private-lib-core.h"
|
||||
#include <unistd.h>
|
||||
|
||||
#if defined(__OpenBSD__) || defined(__NetBSD__)
|
||||
#include <sys/resource.h>
|
||||
#include <sys/wait.h>
|
||||
#endif
|
||||
|
||||
void
|
||||
lws_spawn_timeout(struct lws_sorted_usec_list *sul)
|
||||
{
|
||||
struct lws_spawn_piped *lsp = lws_container_of(sul,
|
||||
struct lws_spawn_piped, sul);
|
||||
|
||||
lwsl_warn("%s: spawn exceeded timeout, killing\n", __func__);
|
||||
|
||||
lws_spawn_piped_kill_child_process(lsp);
|
||||
}
|
||||
|
||||
void
|
||||
lws_spawn_sul_reap(struct lws_sorted_usec_list *sul)
|
||||
{
|
||||
struct lws_spawn_piped *lsp = lws_container_of(sul,
|
||||
struct lws_spawn_piped, sul_reap);
|
||||
|
||||
lwsl_notice("%s: reaping spawn after last stdpipe, tries left %d\n",
|
||||
__func__, lsp->reap_retry_budget);
|
||||
if (!lws_spawn_reap(lsp) && !lsp->pipes_alive) {
|
||||
if (--lsp->reap_retry_budget) {
|
||||
lws_sul_schedule(lsp->info.vh->context, lsp->info.tsi,
|
||||
&lsp->sul_reap, lws_spawn_sul_reap,
|
||||
250 * LWS_US_PER_MS);
|
||||
} else {
|
||||
lwsl_err("%s: Unable to reap lsp %p, killing\n",
|
||||
__func__, lsp);
|
||||
lsp->reap_retry_budget = 20;
|
||||
lws_spawn_piped_kill_child_process(lsp);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static struct lws *
|
||||
lws_create_stdwsi(struct lws_context *context, int tsi,
|
||||
const struct lws_role_ops *ops)
|
||||
{
|
||||
struct lws_context_per_thread *pt = &context->pt[tsi];
|
||||
struct lws *new_wsi;
|
||||
|
||||
if (!context->vhost_list)
|
||||
return NULL;
|
||||
|
||||
if ((unsigned int)pt->fds_count == context->fd_limit_per_thread - 1) {
|
||||
lwsl_err("no space for new conn\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
lws_context_lock(context, __func__);
|
||||
new_wsi = __lws_wsi_create_with_role(context, tsi, ops, NULL);
|
||||
lws_context_unlock(context);
|
||||
if (new_wsi == NULL) {
|
||||
lwsl_err("Out of memory for new connection\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
new_wsi->rxflow_change_to = LWS_RXFLOW_ALLOW;
|
||||
|
||||
/* initialize the instance struct */
|
||||
|
||||
lws_role_transition(new_wsi, 0, LRS_ESTABLISHED, ops);
|
||||
|
||||
new_wsi->hdr_parsing_completed = 0;
|
||||
|
||||
/*
|
||||
* these can only be set once the protocol is known
|
||||
* we set an unestablished connection's protocol pointer
|
||||
* to the start of the defauly vhost supported list, so it can look
|
||||
* for matching ones during the handshake
|
||||
*/
|
||||
|
||||
new_wsi->user_space = NULL;
|
||||
|
||||
return new_wsi;
|
||||
}
|
||||
|
||||
void
|
||||
lws_spawn_piped_destroy(struct lws_spawn_piped **_lsp)
|
||||
{
|
||||
struct lws_spawn_piped *lsp = *_lsp;
|
||||
int n;
|
||||
|
||||
if (!lsp)
|
||||
return;
|
||||
|
||||
lws_dll2_remove(&lsp->dll);
|
||||
|
||||
lws_sul_cancel(&lsp->sul);
|
||||
lws_sul_cancel(&lsp->sul_reap);
|
||||
|
||||
for (n = 0; n < 3; n++) {
|
||||
#if 0
|
||||
if (lsp->pipe_fds[n][!!(n == 0)] == 0)
|
||||
lwsl_err("ZERO FD IN CGI CLOSE");
|
||||
|
||||
if (lsp->pipe_fds[n][!!(n == 0)] >= 0) {
|
||||
close(lsp->pipe_fds[n][!!(n == 0)]);
|
||||
lsp->pipe_fds[n][!!(n == 0)] = LWS_SOCK_INVALID;
|
||||
}
|
||||
#endif
|
||||
if (lsp->stdwsi[n]) {
|
||||
lws_set_timeout(lsp->stdwsi[n], 1, LWS_TO_KILL_ASYNC);
|
||||
lsp->stdwsi[n] = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
lws_free_set_NULL((*_lsp));
|
||||
}
|
||||
|
||||
int
|
||||
lws_spawn_reap(struct lws_spawn_piped *lsp)
|
||||
{
|
||||
long hz = sysconf(_SC_CLK_TCK); /* accounting Hz */
|
||||
void *opaque = lsp->info.opaque;
|
||||
lsp_cb_t cb = lsp->info.reap_cb;
|
||||
struct lws_spawn_piped temp;
|
||||
struct tms tms;
|
||||
#if defined(__OpenBSD__) || defined(__NetBSD__)
|
||||
struct rusage rusa;
|
||||
int status;
|
||||
#endif
|
||||
int n;
|
||||
|
||||
if (lsp->child_pid < 1)
|
||||
return 0;
|
||||
|
||||
/* check if exited, do not reap yet */
|
||||
|
||||
memset(&lsp->si, 0, sizeof(lsp->si));
|
||||
#if defined(__OpenBSD__) || defined(__NetBSD__)
|
||||
n = wait4(lsp->child_pid, &status, WNOHANG, &rusa);
|
||||
if (!n)
|
||||
return 0;
|
||||
lsp->si.si_code = WIFEXITED(status);
|
||||
#else
|
||||
n = waitid(P_PID, (id_t)lsp->child_pid, &lsp->si, WEXITED | WNOHANG | WNOWAIT);
|
||||
#endif
|
||||
if (n < 0) {
|
||||
lwsl_info("%s: child %d still running\n", __func__, lsp->child_pid);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!lsp->si.si_code)
|
||||
return 0;
|
||||
|
||||
/* his process has exited... */
|
||||
|
||||
if (!lsp->reaped) {
|
||||
/* mark the earliest time we knew he had gone */
|
||||
lsp->reaped = lws_now_usecs();
|
||||
|
||||
/*
|
||||
* Switch the timeout to restrict the amount of grace time
|
||||
* to drain stdwsi
|
||||
*/
|
||||
|
||||
lws_sul_schedule(lsp->info.vh->context, lsp->info.tsi,
|
||||
&lsp->sul, lws_spawn_timeout,
|
||||
5 * LWS_US_PER_SEC);
|
||||
}
|
||||
|
||||
/*
|
||||
* Stage finalizing our reaction to the process going down until the
|
||||
* stdwsi flushed whatever is in flight and all noticed they were
|
||||
* closed. For that reason, each stdwsi close must call lws_spawn_reap
|
||||
* to check if that was the last one and we can proceed with the reap.
|
||||
*/
|
||||
|
||||
if (!lsp->ungraceful && lsp->pipes_alive) {
|
||||
lwsl_info("%s: %d stdwsi alive, not reaping\n", __func__,
|
||||
lsp->pipes_alive);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* we reached the reap point, no need for timeout wait */
|
||||
|
||||
lws_sul_cancel(&lsp->sul);
|
||||
|
||||
/*
|
||||
* All the stdwsi went down, nothing more is coming... it's over
|
||||
* Collect the final information and then reap the dead process
|
||||
*/
|
||||
|
||||
if (times(&tms) != (clock_t) -1) {
|
||||
/*
|
||||
* Cpu accounting in us
|
||||
*/
|
||||
lsp->accounting[0] = (lws_usec_t)((uint64_t)tms.tms_cstime * 1000000) / hz;
|
||||
lsp->accounting[1] = (lws_usec_t)((uint64_t)tms.tms_cutime * 1000000) / hz;
|
||||
lsp->accounting[2] = (lws_usec_t)((uint64_t)tms.tms_stime * 1000000) / hz;
|
||||
lsp->accounting[3] = (lws_usec_t)((uint64_t)tms.tms_utime * 1000000) / hz;
|
||||
}
|
||||
|
||||
temp = *lsp;
|
||||
#if defined(__OpenBSD__) || defined(__NetBSD__)
|
||||
n = wait4(lsp->child_pid, &status, WNOHANG, &rusa);
|
||||
if (!n)
|
||||
return 0;
|
||||
lsp->si.si_code = WIFEXITED(status);
|
||||
if (lsp->si.si_code == CLD_EXITED)
|
||||
temp.si.si_code = CLD_EXITED;
|
||||
temp.si.si_status = WEXITSTATUS(status);
|
||||
#else
|
||||
n = waitid(P_PID, (id_t)lsp->child_pid, &temp.si, WEXITED | WNOHANG);
|
||||
#endif
|
||||
temp.si.si_status &= 0xff; /* we use b8 + for flags */
|
||||
lwsl_info("%s: waitd says %d, process exit %d\n",
|
||||
__func__, n, temp.si.si_status);
|
||||
|
||||
lsp->child_pid = -1;
|
||||
|
||||
/* destroy the lsp itself first (it's freed and plsp set NULL */
|
||||
|
||||
if (lsp->info.plsp)
|
||||
lws_spawn_piped_destroy(lsp->info.plsp);
|
||||
|
||||
/* then do the parent callback informing it's destroyed */
|
||||
|
||||
if (cb)
|
||||
cb(opaque, temp.accounting, &temp.si,
|
||||
temp.we_killed_him_timeout |
|
||||
(temp.we_killed_him_spew << 1));
|
||||
|
||||
return 1; /* was reaped */
|
||||
}
|
||||
|
||||
int
|
||||
lws_spawn_piped_kill_child_process(struct lws_spawn_piped *lsp)
|
||||
{
|
||||
int status, n;
|
||||
|
||||
if (lsp->child_pid <= 0)
|
||||
return 1;
|
||||
|
||||
lsp->ungraceful = 1; /* don't wait for flushing, just kill it */
|
||||
|
||||
if (lws_spawn_reap(lsp))
|
||||
/* that may have invalidated lsp */
|
||||
return 0;
|
||||
|
||||
/* kill the process group */
|
||||
n = kill(-lsp->child_pid, SIGTERM);
|
||||
lwsl_debug("%s: SIGTERM child PID %d says %d (errno %d)\n", __func__,
|
||||
lsp->child_pid, n, errno);
|
||||
if (n < 0) {
|
||||
/*
|
||||
* hum seen errno=3 when process is listed in ps,
|
||||
* it seems we don't always retain process grouping
|
||||
*
|
||||
* Direct these fallback attempt to the exact child
|
||||
*/
|
||||
n = kill(lsp->child_pid, SIGTERM);
|
||||
if (n < 0) {
|
||||
n = kill(lsp->child_pid, SIGPIPE);
|
||||
if (n < 0) {
|
||||
n = kill(lsp->child_pid, SIGKILL);
|
||||
if (n < 0)
|
||||
lwsl_info("%s: SIGKILL PID %d "
|
||||
"failed errno %d "
|
||||
"(maybe zombie)\n", __func__,
|
||||
lsp->child_pid, errno);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* He could be unkillable because he's a zombie */
|
||||
|
||||
n = 1;
|
||||
while (n > 0) {
|
||||
n = waitpid(-lsp->child_pid, &status, WNOHANG);
|
||||
if (n > 0)
|
||||
lwsl_debug("%s: reaped PID %d\n", __func__, n);
|
||||
if (n <= 0) {
|
||||
n = waitpid(lsp->child_pid, &status, WNOHANG);
|
||||
if (n > 0)
|
||||
lwsl_debug("%s: reaped PID %d\n", __func__, n);
|
||||
}
|
||||
}
|
||||
|
||||
lws_spawn_reap(lsp);
|
||||
/* that may have invalidated lsp */
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Deals with spawning a subprocess and executing it securely with stdin/out/err
|
||||
* diverted into pipes
|
||||
*/
|
||||
|
||||
struct lws_spawn_piped *
|
||||
lws_spawn_piped(const struct lws_spawn_piped_info *i)
|
||||
{
|
||||
const struct lws_protocols *pcol = i->vh->context->vhost_list->protocols;
|
||||
struct lws_context *context = i->vh->context;
|
||||
struct lws_spawn_piped *lsp;
|
||||
const char *wd;
|
||||
int n, m;
|
||||
|
||||
if (i->protocol_name)
|
||||
pcol = lws_vhost_name_to_protocol(i->vh, i->protocol_name);
|
||||
if (!pcol) {
|
||||
lwsl_err("%s: unknown protocol %s\n", __func__,
|
||||
i->protocol_name ? i->protocol_name : "default");
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
lsp = lws_zalloc(sizeof(*lsp), __func__);
|
||||
if (!lsp)
|
||||
return NULL;
|
||||
|
||||
/* wholesale take a copy of info */
|
||||
lsp->info = *i;
|
||||
lsp->reap_retry_budget = 20;
|
||||
|
||||
/*
|
||||
* Prepare the stdin / out / err pipes
|
||||
*/
|
||||
|
||||
for (n = 0; n < 3; n++) {
|
||||
lsp->pipe_fds[n][0] = -1;
|
||||
lsp->pipe_fds[n][1] = -1;
|
||||
}
|
||||
|
||||
/* create pipes for [stdin|stdout] and [stderr] */
|
||||
|
||||
for (n = 0; n < 3; n++) {
|
||||
if (pipe(lsp->pipe_fds[n]) == -1)
|
||||
goto bail1;
|
||||
lws_plat_apply_FD_CLOEXEC(lsp->pipe_fds[n][n == 0]);
|
||||
}
|
||||
|
||||
/*
|
||||
* At this point, we have 6 pipe fds open on lws side and no wsis
|
||||
* bound to them
|
||||
*/
|
||||
|
||||
/* create wsis for each stdin/out/err fd */
|
||||
|
||||
for (n = 0; n < 3; n++) {
|
||||
lsp->stdwsi[n] = lws_create_stdwsi(i->vh->context, i->tsi,
|
||||
i->ops ? i->ops : &role_ops_raw_file);
|
||||
if (!lsp->stdwsi[n]) {
|
||||
lwsl_err("%s: unable to create lsp stdwsi\n", __func__);
|
||||
goto bail2;
|
||||
}
|
||||
|
||||
__lws_lc_tag(i->vh->context, &i->vh->context->lcg[LWSLCG_WSI],
|
||||
&lsp->stdwsi[n]->lc, "nspawn-stdwsi-%d", n);
|
||||
|
||||
lsp->stdwsi[n]->lsp_channel = (uint8_t)n;
|
||||
lws_vhost_bind_wsi(i->vh, lsp->stdwsi[n]);
|
||||
lsp->stdwsi[n]->a.protocol = pcol;
|
||||
lsp->stdwsi[n]->a.opaque_user_data = i->opaque;
|
||||
|
||||
lwsl_debug("%s: lsp stdwsi %p: pipe idx %d -> fd %d / %d\n", __func__,
|
||||
lsp->stdwsi[n], n, lsp->pipe_fds[n][n == 0],
|
||||
lsp->pipe_fds[n][n != 0]);
|
||||
|
||||
/* read side is 0, stdin we want the write side, others read */
|
||||
|
||||
lsp->stdwsi[n]->desc.sockfd = lsp->pipe_fds[n][n == 0];
|
||||
if (fcntl(lsp->pipe_fds[n][n == 0], F_SETFL, O_NONBLOCK) < 0) {
|
||||
lwsl_err("%s: setting NONBLOCK failed\n", __func__);
|
||||
goto bail2;
|
||||
}
|
||||
|
||||
/*
|
||||
* We have bound 3 x pipe fds to wsis, wr side of stdin and rd
|
||||
* side of stdout / stderr... those are marked CLOEXEC so they
|
||||
* won't go through the fork
|
||||
*
|
||||
* rd side of stdin and wr side of stdout / stderr are open but
|
||||
* not bound to anything on lws side.
|
||||
*/
|
||||
}
|
||||
|
||||
/*
|
||||
* Stitch the wsi fd into the poll wait
|
||||
*/
|
||||
|
||||
for (n = 0; n < 3; n++) {
|
||||
if (context->event_loop_ops->sock_accept)
|
||||
if (context->event_loop_ops->sock_accept(lsp->stdwsi[n]))
|
||||
goto bail3;
|
||||
|
||||
if (__insert_wsi_socket_into_fds(context, lsp->stdwsi[n]))
|
||||
goto bail3;
|
||||
if (i->opt_parent) {
|
||||
lsp->stdwsi[n]->parent = i->opt_parent;
|
||||
lsp->stdwsi[n]->sibling_list = i->opt_parent->child_list;
|
||||
i->opt_parent->child_list = lsp->stdwsi[n];
|
||||
}
|
||||
}
|
||||
|
||||
if (lws_change_pollfd(lsp->stdwsi[LWS_STDIN], LWS_POLLIN, LWS_POLLOUT))
|
||||
goto bail3;
|
||||
if (lws_change_pollfd(lsp->stdwsi[LWS_STDOUT], LWS_POLLOUT, LWS_POLLIN))
|
||||
goto bail3;
|
||||
if (lws_change_pollfd(lsp->stdwsi[LWS_STDERR], LWS_POLLOUT, LWS_POLLIN))
|
||||
goto bail3;
|
||||
|
||||
lwsl_info("%s: fds in %d, out %d, err %d\n", __func__,
|
||||
lsp->stdwsi[LWS_STDIN]->desc.sockfd,
|
||||
lsp->stdwsi[LWS_STDOUT]->desc.sockfd,
|
||||
lsp->stdwsi[LWS_STDERR]->desc.sockfd);
|
||||
|
||||
/* we are ready with the redirection pipes... do the (v)fork */
|
||||
#if defined(__sun) || !defined(LWS_HAVE_VFORK) || !defined(LWS_HAVE_EXECVPE)
|
||||
lsp->child_pid = fork();
|
||||
#else
|
||||
lsp->child_pid = vfork();
|
||||
#endif
|
||||
if (lsp->child_pid < 0) {
|
||||
lwsl_err("%s: fork failed, errno %d", __func__, errno);
|
||||
goto bail3;
|
||||
}
|
||||
|
||||
#if defined(__linux__)
|
||||
if (!lsp->child_pid)
|
||||
prctl(PR_SET_PDEATHSIG, SIGTERM);
|
||||
#endif
|
||||
|
||||
if (lsp->info.disable_ctrlc)
|
||||
/* stops non-daemonized main processess getting SIGINT
|
||||
* from TTY */
|
||||
#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
|
||||
setpgid(0, 0);
|
||||
#else
|
||||
setpgrp();
|
||||
#endif
|
||||
|
||||
if (lsp->child_pid) {
|
||||
|
||||
/*
|
||||
* We are the parent process. We can close our copy of the
|
||||
* "other" side of the pipe fds, ie, rd for stdin and wr for
|
||||
* stdout / stderr.
|
||||
*/
|
||||
for (n = 0; n < 3; n++)
|
||||
/* these guys didn't have any wsi footprint */
|
||||
close(lsp->pipe_fds[n][n != 0]);
|
||||
|
||||
lsp->pipes_alive = 3;
|
||||
lsp->created = lws_now_usecs();
|
||||
|
||||
lwsl_info("%s: lsp %p spawned PID %d\n", __func__, lsp,
|
||||
lsp->child_pid);
|
||||
|
||||
lws_sul_schedule(context, i->tsi, &lsp->sul, lws_spawn_timeout,
|
||||
i->timeout_us ? i->timeout_us :
|
||||
300 * LWS_US_PER_SEC);
|
||||
|
||||
if (i->owner)
|
||||
lws_dll2_add_head(&lsp->dll, i->owner);
|
||||
|
||||
if (i->timeout_us)
|
||||
lws_sul_schedule(context, i->tsi, &lsp->sul,
|
||||
lws_spawn_timeout, i->timeout_us);
|
||||
|
||||
return lsp;
|
||||
}
|
||||
|
||||
/*
|
||||
* We are the forked process, redirect and kill inherited things.
|
||||
*
|
||||
* Because of vfork(), we cannot do anything that changes pages in
|
||||
* the parent environment. Stuff that changes kernel state for the
|
||||
* process is OK. Stuff that happens after the execvpe() is OK.
|
||||
*/
|
||||
|
||||
if (i->chroot_path && chroot(i->chroot_path)) {
|
||||
lwsl_err("%s: child chroot %s failed, errno %d\n",
|
||||
__func__, i->chroot_path, errno);
|
||||
|
||||
exit(2);
|
||||
}
|
||||
|
||||
/* cwd: somewhere we can at least read things and enter it */
|
||||
|
||||
wd = i->wd;
|
||||
if (!wd)
|
||||
wd = "/tmp";
|
||||
if (chdir(wd))
|
||||
lwsl_notice("%s: Failed to cd to %s\n", __func__, wd);
|
||||
|
||||
/*
|
||||
* Bind the child's stdin / out / err to its side of our pipes
|
||||
*/
|
||||
|
||||
for (m = 0; m < 3; m++) {
|
||||
if (dup2(lsp->pipe_fds[m][m != 0], m) < 0) {
|
||||
lwsl_err("%s: stdin dup2 failed\n", __func__);
|
||||
goto bail3;
|
||||
}
|
||||
/*
|
||||
* CLOEXEC on the lws-side of the pipe fds should have already
|
||||
* dealt with closing those for the child perspective.
|
||||
*
|
||||
* Now it has done the dup, the child should close its original
|
||||
* copies of its side of the pipes.
|
||||
*/
|
||||
|
||||
close(lsp->pipe_fds[m][m != 0]);
|
||||
}
|
||||
|
||||
#if defined(__sun) || !defined(LWS_HAVE_VFORK) || !defined(LWS_HAVE_EXECVPE)
|
||||
#if defined(__linux__) || defined(__APPLE__) || defined(__sun)
|
||||
m = 0;
|
||||
while (i->env_array[m]){
|
||||
const char *p = strchr(i->env_array[m], '=');
|
||||
int naml = lws_ptr_diff(p, i->env_array[m]);
|
||||
char enam[32];
|
||||
|
||||
if (p) {
|
||||
lws_strnncpy(enam, i->env_array[m], naml, sizeof(enam));
|
||||
setenv(enam, p + 1, 1);
|
||||
}
|
||||
m++;
|
||||
}
|
||||
#endif
|
||||
execvp(i->exec_array[0], (char * const *)&i->exec_array[0]);
|
||||
#else
|
||||
execvpe(i->exec_array[0], (char * const *)&i->exec_array[0],
|
||||
(char **)&i->env_array[0]);
|
||||
#endif
|
||||
|
||||
lwsl_err("%s: child exec of %s failed %d\n", __func__, i->exec_array[0],
|
||||
LWS_ERRNO);
|
||||
|
||||
_exit(1);
|
||||
|
||||
bail3:
|
||||
|
||||
while (--n >= 0)
|
||||
__remove_wsi_socket_from_fds(lsp->stdwsi[n]);
|
||||
bail2:
|
||||
for (n = 0; n < 3; n++)
|
||||
if (lsp->stdwsi[n])
|
||||
__lws_free_wsi(lsp->stdwsi[n]);
|
||||
|
||||
bail1:
|
||||
for (n = 0; n < 3; n++) {
|
||||
if (lsp->pipe_fds[n][0] >= 0)
|
||||
close(lsp->pipe_fds[n][0]);
|
||||
if (lsp->pipe_fds[n][1] >= 0)
|
||||
close(lsp->pipe_fds[n][1]);
|
||||
}
|
||||
|
||||
lws_free(lsp);
|
||||
|
||||
lwsl_err("%s: failed\n", __func__);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void
|
||||
lws_spawn_stdwsi_closed(struct lws_spawn_piped *lsp, struct lws *wsi)
|
||||
{
|
||||
int n;
|
||||
|
||||
assert(lsp);
|
||||
lsp->pipes_alive--;
|
||||
lwsl_debug("%s: pipes alive %d\n", __func__, lsp->pipes_alive);
|
||||
if (!lsp->pipes_alive)
|
||||
lws_sul_schedule(lsp->info.vh->context, lsp->info.tsi,
|
||||
&lsp->sul_reap, lws_spawn_sul_reap, 1);
|
||||
|
||||
for (n = 0; n < 3; n++)
|
||||
if (lsp->stdwsi[n] == wsi)
|
||||
lsp->stdwsi[n] = NULL;
|
||||
}
|
||||
|
||||
int
|
||||
lws_spawn_get_stdfd(struct lws *wsi)
|
||||
{
|
||||
return wsi->lsp_channel;
|
||||
}
|
85
Kinc/Sources/kinc/libs/plat/unix/unix-systemd.c
Normal file
85
Kinc/Sources/kinc/libs/plat/unix/unix-systemd.c
Normal file
@ -0,0 +1,85 @@
|
||||
/*
|
||||
* libwebsockets - small server side websockets and web server implementation
|
||||
*
|
||||
* Copyright (C) 2010 - 2024 Andy Green <andy@warmcat.com>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to
|
||||
* deal in the Software without restriction, including without limitation the
|
||||
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
* sell copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
* IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#if !defined(_GNU_SOURCE)
|
||||
#define _GNU_SOURCE
|
||||
#endif
|
||||
|
||||
#include <sys/socket.h>
|
||||
#include <systemd/sd-daemon.h>
|
||||
|
||||
#include "private-lib-core.h"
|
||||
|
||||
|
||||
int
|
||||
lws_systemd_inherited_fd(unsigned int index,
|
||||
struct lws_context_creation_info *info)
|
||||
{
|
||||
unsigned int inherited = (unsigned int)sd_listen_fds(0);
|
||||
|
||||
if (index >= inherited)
|
||||
return -1;
|
||||
|
||||
info->vh_listen_sockfd = (int)(SD_LISTEN_FDS_START + index);
|
||||
|
||||
if (sd_is_socket_unix(info->vh_listen_sockfd, 0, 0, NULL, 0)) {
|
||||
info->options |= LWS_SERVER_OPTION_UNIX_SOCK;
|
||||
info->port = 0;
|
||||
}
|
||||
|
||||
if (sd_is_socket_inet(info->vh_listen_sockfd, AF_UNSPEC, 0, 1, 0)) {
|
||||
struct sockaddr_storage addr;
|
||||
socklen_t addrlen = sizeof(addr);
|
||||
|
||||
if (getsockname(info->vh_listen_sockfd,
|
||||
(struct sockaddr *)&addr, &addrlen)) {
|
||||
lwsl_err("%s: getsockname failed for fd %d\n",
|
||||
__func__, info->vh_listen_sockfd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
switch (((struct sockaddr *)&addr)->sa_family) {
|
||||
case AF_INET:
|
||||
info->port = ntohs(((struct sockaddr_in *)&addr)->sin_port);
|
||||
lwsl_info("%s: inet socket %d\n", __func__, info->port);
|
||||
break;
|
||||
case AF_INET6:
|
||||
info->port = ntohs(((struct sockaddr_in6 *)&addr)->sin6_port);
|
||||
lwsl_info("%s: inet6 socket %d\n", __func__, info->port);
|
||||
break;
|
||||
}
|
||||
|
||||
if (sd_is_socket_inet(info->vh_listen_sockfd, AF_INET6, 0, 1, 0))
|
||||
info->options |= LWS_SERVER_OPTION_IPV6_V6ONLY_MODIFY |
|
||||
LWS_SERVER_OPTION_IPV6_V6ONLY_VALUE;
|
||||
|
||||
if (sd_is_socket_inet(info->vh_listen_sockfd, AF_INET, 0, 1, 0)) {
|
||||
info->options &= (uint64_t)~(LWS_SERVER_OPTION_IPV6_V6ONLY_MODIFY |
|
||||
LWS_SERVER_OPTION_IPV6_V6ONLY_VALUE);
|
||||
info->options |= LWS_SERVER_OPTION_DISABLE_IPV6;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user