From d6046d5e40f19716572490e67a881da6f45db924 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Wed, 27 Jan 2016 12:42:49 -0800 Subject: [PATCH 1/2] Fix the build when SOCK_CLOEXEC and SOCK_NONBLOCK aren't defined Signed-off-by: Thiago Macieira --- src/lib/comms/sol-socket-impl-linux.c | 21 +++++++++- .../flow/unix-socket/unix-socket-impl.c | 40 ++++++++++++++++++- 2 files changed, 58 insertions(+), 3 deletions(-) diff --git a/src/lib/comms/sol-socket-impl-linux.c b/src/lib/comms/sol-socket-impl-linux.c index 06770b9fb..801fac2bd 100644 --- a/src/lib/comms/sol-socket-impl-linux.c +++ b/src/lib/comms/sol-socket-impl-linux.c @@ -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: @@ -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); diff --git a/src/modules/flow/unix-socket/unix-socket-impl.c b/src/modules/flow/unix-socket/unix-socket-impl.c index 6e182ad47..a92d93ed3 100644 --- a/src/modules/flow/unix-socket/unix-socket-impl.c +++ b/src/modules/flow/unix-socket/unix-socket-impl.c @@ -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)); @@ -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); @@ -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)); From 3e31a504b61a622d21b3faf1cf1cf320d99ed929 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Tue, 26 Jan 2016 17:52:37 -0800 Subject: [PATCH 2/2] Introduce a generic PLATFORM_UNIX A lot of the code that has so far been under PLATFORM_LINUX will actually work on generic Unix. Signed-off-by: Thiago Macieira --- data/jsons/dependencies.json | 7 +++++++ src/lib/common/Kconfig | 15 ++++++++++----- src/lib/common/sol-common-buildopts.h.in | 3 ++- src/lib/crypto/Kconfig | 2 +- src/modules/flow-metatype/js/Kconfig | 2 +- src/modules/flow/gtk/Kconfig | 2 +- src/modules/flow/oic/Kconfig | 2 +- src/modules/flow/platform/Kconfig | 1 + src/modules/flow/process/Kconfig | 2 +- tools/build/Kconfig.linux | 13 ++----------- tools/build/Kconfig.unix | 13 +++++++++++++ 11 files changed, 40 insertions(+), 22 deletions(-) create mode 100644 tools/build/Kconfig.unix diff --git a/data/jsons/dependencies.json b/data/jsons/dependencies.json index 959596351..9c5cfb38c 100644 --- a/data/jsons/dependencies.json +++ b/data/jsons/dependencies.json @@ -190,6 +190,13 @@ "value": "-lpthread" } }, + { + "dependency": "unix", + "type": "ccode", + "headers": [ + "" + ] + }, { "dependency": "linux", "type": "ccode", diff --git a/src/lib/common/Kconfig b/src/lib/common/Kconfig index 65b2a2b10..571662a48 100644 --- a/src/lib/common/Kconfig +++ b/src/lib/common/Kconfig @@ -32,6 +32,11 @@ config DETECT_BOARD_NAME config PLATFORM_LINUX bool +config PLATFORM_UNIX + bool "unix" + depends on HAVE_UNIX + default y + choice prompt "Target platform" default PLATFORM_SYSTEMD if LINUX && HAVE_SYSTEMD @@ -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 @@ -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. @@ -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. @@ -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. diff --git a/src/lib/common/sol-common-buildopts.h.in b/src/lib/common/sol-common-buildopts.h.in index 7c10d926f..836004ca7 100644 --- a/src/lib/common/sol-common-buildopts.h.in +++ b/src/lib/common/sol-common-buildopts.h.in @@ -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", "") @@ -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 diff --git a/src/lib/crypto/Kconfig b/src/lib/crypto/Kconfig index 174e91ef7..ec85eaa6c 100644 --- a/src/lib/crypto/Kconfig +++ b/src/lib/crypto/Kconfig @@ -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 diff --git a/src/modules/flow-metatype/js/Kconfig b/src/modules/flow-metatype/js/Kconfig index 1808a71cc..39e3eab83 100644 --- a/src/modules/flow-metatype/js/Kconfig +++ b/src/modules/flow-metatype/js/Kconfig @@ -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 diff --git a/src/modules/flow/gtk/Kconfig b/src/modules/flow/gtk/Kconfig index 57c5b6847..bb31687e0 100644 --- a/src/modules/flow/gtk/Kconfig +++ b/src/modules/flow/gtk/Kconfig @@ -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 diff --git a/src/modules/flow/oic/Kconfig b/src/modules/flow/oic/Kconfig index 6b3126d00..2d70bb1e9 100644 --- a/src/modules/flow/oic/Kconfig +++ b/src/modules/flow/oic/Kconfig @@ -1,4 +1,4 @@ config FLOW_NODE_TYPE_OIC tristate "Node type: oic" - depends on OIC && PLATFORM_LINUX + depends on OIC && UNIX default m diff --git a/src/modules/flow/platform/Kconfig b/src/modules/flow/platform/Kconfig index 6662201af..a1861e1ff 100644 --- a/src/modules/flow/platform/Kconfig +++ b/src/modules/flow/platform/Kconfig @@ -1,3 +1,4 @@ config FLOW_NODE_TYPE_PLATFORM tristate "Node type: platform" + depends on PLATFORM_RIOTOS || PLATFORM_CONTIKI || PLATFORM_LINUX default y diff --git a/src/modules/flow/process/Kconfig b/src/modules/flow/process/Kconfig index 471ec5325..a8d6f0d7a 100644 --- a/src/modules/flow/process/Kconfig +++ b/src/modules/flow/process/Kconfig @@ -1,4 +1,4 @@ config FLOW_NODE_TYPE_PROCESS tristate "Node type: process" - depends on PLATFORM_LINUX + depends on UNIX default m diff --git a/tools/build/Kconfig.linux b/tools/build/Kconfig.linux index d42d91afa..4b19bfe5b 100644 --- a/tools/build/Kconfig.linux +++ b/tools/build/Kconfig.linux @@ -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 diff --git a/tools/build/Kconfig.unix b/tools/build/Kconfig.unix new file mode 100644 index 000000000..a851c592d --- /dev/null +++ b/tools/build/Kconfig.unix @@ -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