Skip to content
This repository has been archived by the owner on Jun 27, 2019. It is now read-only.

Make sockets work on non-Linux Unix systems #1395

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions data/jsons/dependencies.json
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,13 @@
"value": "-lpthread"
}
},
{
"dependency": "unix",
"type": "ccode",
"headers": [
"<unistd.h>"
]
},
{
"dependency": "linux",
"type": "ccode",
Expand Down
15 changes: 10 additions & 5 deletions src/lib/common/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ config DETECT_BOARD_NAME
config PLATFORM_LINUX
bool

config PLATFORM_UNIX
bool "unix"
depends on HAVE_UNIX
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

different indentation with line above

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Damn. It didn't show because of 8-space tabs. My OS X's Emacs isn't properly configured for space mode.

default y

choice
prompt "Target platform"
default PLATFORM_SYSTEMD if LINUX && HAVE_SYSTEMD
Expand Down Expand Up @@ -162,7 +167,7 @@ source "src/modules/linux-micro/Kconfig"

choice
prompt "Mainloop"
default MAINLOOP_POSIX if LINUX
default MAINLOOP_POSIX if UNIX
default MAINLOOP_RIOTOS if RIOT
default MAINLOOP_CONTIKI if CONTIKI
default MAINLOOP_ZEPHYR if ZEPHYR
Expand Down Expand Up @@ -200,7 +205,7 @@ choice

config MAINLOOP_GLIB
bool "glib"
depends on LINUX && HAVE_GLIB
depends on UNIX && HAVE_GLIB
help
The mainloop is implementated on top of Glib.

Expand All @@ -215,7 +220,7 @@ config MAINLOOP_GLIB

config MAINLOOP_POSIX
bool "posix"
depends on LINUX
depends on UNIX
help
The mainloop is implementated on top of poll(2) syscall.

Expand Down Expand Up @@ -253,8 +258,8 @@ config PTHREAD

config THREADS
bool "Thread safety"
depends on (HAVE_PTHREAD_H && LINUX) || RIOT
select PTHREAD if LINUX
depends on (HAVE_PTHREAD_H && UNIX) || RIOT
select PTHREAD if UNIX
default y
help
Allow usage of threads in Soletta applications.
Expand Down
3 changes: 2 additions & 1 deletion src/lib/common/sol-common-buildopts.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#pragma once

{{
st.on_value("PLATFORM_UNIX", "y", "#define SOL_PLATFORM_UNIX 1", "")
st.on_value("PLATFORM_LINUX", "y", "#define SOL_PLATFORM_LINUX 1", "")
st.on_value("PLATFORM_RIOTOS", "y", "#define SOL_PLATFORM_RIOT 1", "")
st.on_value("PLATFORM_CONTIKI", "y", "#define SOL_PLATFORM_CONTIKI 1", "")
Expand All @@ -48,7 +49,7 @@ st.on_value("NO_API_VERSION_NEEDED", "y", "#define SOL_NO_API_VERSION 1", "")
st.on_value("MODULES", "y", "#define SOL_DYNAMIC_MODULES 1", "")
}}

#ifdef SOL_PLATFORM_LINUX
#ifdef SOL_PLATFORM_UNIX
#define SOL_MAINLOOP_FD_ENABLED 1
#define SOL_MAINLOOP_FORK_WATCH_ENABLED 1
#endif
Expand Down
21 changes: 20 additions & 1 deletion src/lib/comms/sol-socket-impl-linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,11 @@ static struct sol_socket *
sol_socket_linux_new(int domain, enum sol_socket_type type, int protocol)
{
struct sol_socket_linux *s;
int fd, socktype = SOCK_CLOEXEC | SOCK_NONBLOCK;
int fd, socktype = 0;

#ifdef SOCK_CLOEXEC
socktype |= SOCK_CLOEXEC | SOCK_NONBLOCK;
#endif

switch (type) {
case SOL_SOCKET_UDP:
Expand All @@ -167,6 +171,21 @@ sol_socket_linux_new(int domain, enum sol_socket_type type, int protocol)
fd = socket(sol_network_sol_to_af(domain), socktype, protocol);
SOL_INT_CHECK(fd, < 0, NULL);

#ifndef SOCK_CLOEXEC
{
/* We need to set the socket to FD_CLOEXEC and non-blocking mode */
int n = fcntl(fd, F_GETFD);
if (n >= 0)
n = fcntl(fd, F_SETFD, n | FD_CLOEXEC);
if (n >= 0)
n = sol_util_fd_set_flag(fd, O_NONBLOCK);
if (n < 0) {
SOL_WRN("Failed to set the socket to FD_CLOEXEC or O_NONBLOCK, %s", sol_util_strerrora(errno));
goto calloc_error;
}
}
#endif

s = calloc(1, sizeof(*s));
SOL_NULL_CHECK_GOTO(s, calloc_error);

Expand Down
2 changes: 1 addition & 1 deletion src/lib/crypto/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ config CRYPTO_MESSAGE_DIGEST_COMMON
choice
prompt "Message Digest (Hash)"
depends on FEATURE_CRYPTO_MESSAGE_DIGEST
default CRYPTO_MESSAGE_DIGEST_OPENSSL if LINUX && HAVE_OPENSSL
default CRYPTO_MESSAGE_DIGEST_OPENSSL if UNIX && HAVE_OPENSSL
default CRYPTO_MESSAGE_DIGEST_LINUX_KCAPI if LINUX
default CRYPTO_MESSAGE_DIGEST_RIOT if RIOT
help
Expand Down
2 changes: 1 addition & 1 deletion src/modules/flow-metatype/js/Kconfig
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
config FLOW_METATYPE_JAVASCRIPT
tristate "JavaScript flow metatype"
depends on FLOW_SUPPORT && PLATFORM_LINUX && HAVE_DUKTAPE_SRC
depends on FLOW_SUPPORT && UNIX && HAVE_DUKTAPE_SRC
default m if MODULES=y
help
Allow creating node types using JavaScript
2 changes: 1 addition & 1 deletion src/modules/flow/gtk/Kconfig
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
config FLOW_NODE_TYPE_GTK
tristate "Node type: gtk"
depends on LINUX && HAVE_GTK && HAVE_GLIB
depends on UNIX && HAVE_GTK && HAVE_GLIB
default m
2 changes: 1 addition & 1 deletion src/modules/flow/oic/Kconfig
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
config FLOW_NODE_TYPE_OIC
tristate "Node type: oic"
depends on OIC && PLATFORM_LINUX
depends on OIC && UNIX
default m
1 change: 1 addition & 0 deletions src/modules/flow/platform/Kconfig
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
config FLOW_NODE_TYPE_PLATFORM
tristate "Node type: platform"
depends on PLATFORM_RIOTOS || PLATFORM_CONTIKI || PLATFORM_LINUX
default y
2 changes: 1 addition & 1 deletion src/modules/flow/process/Kconfig
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
config FLOW_NODE_TYPE_PROCESS
tristate "Node type: process"
depends on PLATFORM_LINUX
depends on UNIX
default m
40 changes: 38 additions & 2 deletions src/modules/flow/unix-socket/unix-socket-impl.c
Original file line number Diff line number Diff line change
Expand Up @@ -215,18 +215,36 @@ unix_socket_client_new(const void *data, const char *socket_path, void (*data_re
{
struct sockaddr_un local;
struct unix_socket *client = calloc(1, sizeof(*client));
int n = SOCK_STREAM;

#ifdef SOCK_CLOEXEC
n |= SOCK_CLOEXEC | SOCK_NONBLOCK;
#endif

SOL_NULL_CHECK(client, NULL);

client->data = data;
client->data_read_cb = data_read_cb;

client->sock = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0);
client->sock = socket(AF_UNIX, n, 0);
if (client->sock < 0) {
SOL_WRN("Failed to create the socket %s", sol_util_strerrora(errno));
goto err;
}

#ifndef SOCK_CLOEXEC
/* We need to set the socket to FD_CLOEXEC and non-blocking mode */
n = fcntl(client->sock, F_GETFD);
if (n >= 0)
n = fcntl(client->sock, F_SETFD, n | FD_CLOEXEC);
if (n >= 0)
n = sol_util_fd_set_flag(client->sock, O_NONBLOCK);
if (n < 0) {
SOL_WRN("Failed to set the socket to FD_CLOEXEC or O_NONBLOCK, %s", sol_util_strerrora(errno));
goto sock_err;
}
#endif

local.sun_family = AF_UNIX;
if (strncpy(local.sun_path, socket_path, sizeof(local.sun_path) - 1) == NULL) {
SOL_WRN("Falied to copy the string, %s", sol_util_strerrora(errno));
Expand Down Expand Up @@ -294,6 +312,11 @@ struct unix_socket *
unix_socket_server_new(const void *data, const char *socket_path, void (*data_read_cb)(void *data, int fd))
{
struct unix_socket_server *server;
int n = SOCK_STREAM;

#ifdef SOCK_CLOEXEC
n |= SOCK_CLOEXEC | SOCK_NONBLOCK;
#endif

SOL_NULL_CHECK(socket_path, NULL);

Expand All @@ -304,12 +327,25 @@ unix_socket_server_new(const void *data, const char *socket_path, void (*data_re
server->base.data_read_cb = data_read_cb;
sol_vector_init(&server->clients, sizeof(struct client_data));

server->base.sock = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0);
server->base.sock = socket(AF_UNIX, n, 0);
if (server->base.sock < 0) {
SOL_WRN("Failed to create the socket %s", sol_util_strerrora(errno));
goto err;
}

#ifndef SOCK_CLOEXEC
/* We need to set the socket to FD_CLOEXEC and non-blocking mode */
n = fcntl(server->base.sock, F_GETFD);
if (n >= 0)
n = fcntl(server->base.sock, F_SETFD, n | FD_CLOEXEC);
if (n >= 0)
n = sol_util_fd_set_flag(server->base.sock, O_NONBLOCK);
if (n < 0) {
SOL_WRN("Failed to set the socket to FD_CLOEXEC or O_NONBLOCK, %s", sol_util_strerrora(errno));
goto sock_err;
}
#endif

server->local.sun_family = AF_UNIX;
if (strncpy(server->local.sun_path, socket_path, sizeof(server->local.sun_path) - 1) == NULL) {
SOL_WRN("Falied to copy the string, %s", sol_util_strerrora(errno));
Expand Down
13 changes: 2 additions & 11 deletions tools/build/Kconfig.linux
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
source "tools/build/Kconfig.unix"

config LINUX
def_bool y
select FEATURE_DYNAMIC_LINKER
select FEATURE_HW_AIO
select FEATURE_HW_PWM
select FEATURE_HW_SPI
select FEATURE_HW_GPIO
select FEATURE_HW_UART
select FEATURE_HW_I2C
select FEATURE_RUNNABLE_PROGRAMS
select FEATURE_WORKER_THREADS
select FEATURE_UNIX_SOCKETS
select FEATURE_CRYPTO_MESSAGE_DIGEST
select FEATURE_NETWORK
select FEATURE_COAP
select FEATURE_HTTP_CLIENT if HAVE_LIBCURL
select FEATURE_HTTP_SERVER if HAVE_LIBMICROHTTPD
select FEATURE_FLOW
select FEATURE_FILESYSTEM
select FEATURE_CC_SANITIZE
13 changes: 13 additions & 0 deletions tools/build/Kconfig.unix
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
config UNIX
def_bool y
select FEATURE_DYNAMIC_LINKER
select FEATURE_RUNNABLE_PROGRAMS
select FEATURE_WORKER_THREADS
select FEATURE_UNIX_SOCKETS
select FEATURE_CRYPTO_MESSAGE_DIGEST
select FEATURE_NETWORK
select FEATURE_COAP
select FEATURE_HTTP_CLIENT if HAVE_LIBCURL
select FEATURE_HTTP_SERVER if HAVE_LIBMICROHTTPD
select FEATURE_FLOW
select FEATURE_FILESYSTEM