From d443097778e22c51173aaa22b71303b05a89cf84 Mon Sep 17 00:00:00 2001 From: Rickard Green Date: Mon, 28 Jan 2019 14:39:17 +0100 Subject: Fix build of erl_interface on BSD --- lib/erl_interface/src/connect/ei_connect.c | 6 ++---- lib/erl_interface/src/misc/ei_portio.c | 11 +++++++---- 2 files changed, 9 insertions(+), 8 deletions(-) (limited to 'lib/erl_interface/src') diff --git a/lib/erl_interface/src/connect/ei_connect.c b/lib/erl_interface/src/connect/ei_connect.c index 1132c9fc23..206b185e96 100644 --- a/lib/erl_interface/src/connect/ei_connect.c +++ b/lib/erl_interface/src/connect/ei_connect.c @@ -21,6 +21,8 @@ * Purpose: Connect to any node at any host. (EI version) */ +#include "eidef.h" + #include #include #include @@ -40,10 +42,8 @@ #include #include -#include #include #include -#include #include #include #include @@ -53,7 +53,6 @@ #else /* some other unix */ #include -#include #include #if TIME_WITH_SYS_TIME @@ -84,7 +83,6 @@ #include #include -#include "eidef.h" #include "eiext.h" #include "ei_portio.h" #include "ei_internal.h" diff --git a/lib/erl_interface/src/misc/ei_portio.c b/lib/erl_interface/src/misc/ei_portio.c index 726b1af82d..368c56546b 100644 --- a/lib/erl_interface/src/misc/ei_portio.c +++ b/lib/erl_interface/src/misc/ei_portio.c @@ -19,6 +19,9 @@ * */ + +#include "eidef.h" + #ifdef __WIN32__ #include #include @@ -47,10 +50,8 @@ static unsigned long param_one = 1; #include #include #include -#include #include #include -#include #include #include #include @@ -65,7 +66,6 @@ static unsigned long param_one = 1; #else /* other unix */ #include -#include #include #include #include @@ -86,6 +86,7 @@ static unsigned long param_one = 1; /* common includes */ +#include #include #include #include @@ -94,7 +95,9 @@ static unsigned long param_one = 1; #else #include #endif -#include "eidef.h" +#ifdef HAVE_SYS_SELECT_H +#include +#endif #include "ei_portio.h" #include "ei_internal.h" -- cgit v1.2.3 From 50bf9f5d8403b9903ea63867611ac16ffec1c14c Mon Sep 17 00:00:00 2001 From: Rickard Green Date: Tue, 29 Jan 2019 16:58:13 +0100 Subject: Fix bug in ei_accept_tmo --- lib/erl_interface/src/connect/ei_connect.c | 1 - lib/erl_interface/src/misc/ei_portio.c | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) (limited to 'lib/erl_interface/src') diff --git a/lib/erl_interface/src/connect/ei_connect.c b/lib/erl_interface/src/connect/ei_connect.c index 206b185e96..e8ecb7011f 100644 --- a/lib/erl_interface/src/connect/ei_connect.c +++ b/lib/erl_interface/src/connect/ei_connect.c @@ -1277,7 +1277,6 @@ int ei_accept_tmo(ei_cnode* ec, int lfd, ErlConnect *conp, unsigned ms) } if (conp) { memcpy((void *) conp->ipadr, (void *) &addr.sin_addr, sizeof(conp->ipadr)); - strcpy(&conp->nodename[0], her_name); } if (cbs->accept_handshake_complete) { diff --git a/lib/erl_interface/src/misc/ei_portio.c b/lib/erl_interface/src/misc/ei_portio.c index 368c56546b..bccc86c1b1 100644 --- a/lib/erl_interface/src/misc/ei_portio.c +++ b/lib/erl_interface/src/misc/ei_portio.c @@ -249,7 +249,7 @@ static int tcp_accept(void **ctx, void *addr, int *len, unsigned unused) if (res) return res; - res = accept(fd, (struct sockaddr*) &addr, &addr_len); + res = accept(fd, (struct sockaddr*) addr, &addr_len); if (MEANS_SOCKET_ERROR(res)) return get_error(); -- cgit v1.2.3 From dbeaca78bc6c0d4719a522e447fa59d7a1006ae0 Mon Sep 17 00:00:00 2001 From: Rickard Green Date: Tue, 5 Feb 2019 15:55:23 +0100 Subject: Introduce ei_init() You previously had to initialize ei via erl_init() which implied that you were forced to link against the erl_interface even when not using it (besides initializing ei). --- lib/erl_interface/src/Makefile.in | 3 +- lib/erl_interface/src/connect/ei_connect.c | 83 ++++++++++++++++++------------ lib/erl_interface/src/connect/ei_resolve.c | 22 +++++--- lib/erl_interface/src/connect/ei_resolve.h | 2 +- lib/erl_interface/src/legacy/erl_eterm.c | 2 +- lib/erl_interface/src/misc/ei_init.c | 32 ++++++++++++ lib/erl_interface/src/misc/ei_internal.h | 2 + 7 files changed, 104 insertions(+), 42 deletions(-) create mode 100644 lib/erl_interface/src/misc/ei_init.c (limited to 'lib/erl_interface/src') diff --git a/lib/erl_interface/src/Makefile.in b/lib/erl_interface/src/Makefile.in index 614e7325a9..aaf10d4716 100644 --- a/lib/erl_interface/src/Makefile.in +++ b/lib/erl_interface/src/Makefile.in @@ -417,7 +417,8 @@ MISCSRC = \ misc/eimd5.c \ misc/get_type.c \ misc/show_msg.c \ - misc/ei_compat.c + misc/ei_compat.c \ + misc/ei_init.c REGISTRYSRC = \ registry/hash_dohash.c \ diff --git a/lib/erl_interface/src/connect/ei_connect.c b/lib/erl_interface/src/connect/ei_connect.c index e8ecb7011f..7a304e6d4f 100644 --- a/lib/erl_interface/src/connect/ei_connect.c +++ b/lib/erl_interface/src/connect/ei_connect.c @@ -96,6 +96,7 @@ #include "ei_epmd.h" #include "ei_internal.h" +static int ei_connect_initialized = 0; int ei_tracelevel = 0; #define COOKIE_FILE "/.erlang.cookie" @@ -242,7 +243,7 @@ typedef struct { static ei_socket_info_data__ *socket_info_data = NULL; -static int init_socket_info(void) +static int init_socket_info(int late) { int max_fds; int i; @@ -250,7 +251,7 @@ static int init_socket_info(void) ei_socket_info_data__ *info_data, *xchg; if (EI_ATOMIC_LOAD_ACQ(&socket_info_data) != NULL) - return !0; /* Already initialized... */ + return 0; /* Already initialized... */ #if defined(HAVE_SYSCONF) && defined(_SC_OPEN_MAX) max_fds = sysconf(_SC_OPEN_MAX); @@ -259,14 +260,14 @@ static int init_socket_info(void) #endif if (max_fds < 0) - return 0; + return EIO; segments_len = ((max_fds-1)/EI_SOCKET_INFO_SEG_SIZE + 1); info_data = malloc(sizeof(ei_socket_info_data__) + (sizeof(ei_socket_info *)*(segments_len-1))); if (!info_data) - return 0; + return ENOMEM; info_data->max_fds = max_fds; for (i = 0; i < segments_len; i++) @@ -276,7 +277,7 @@ static int init_socket_info(void) if (!EI_ATOMIC_CMPXCHG_ACQ_REL(&socket_info_data, &xchg, info_data)) free(info_data); /* Already initialized... */ - return !0; + return 0; } static int put_ei_socket_info(int fd, int dist_version, char* cookie, ei_cnode *ec, @@ -360,14 +361,16 @@ ei_socket_info *ei_sockets = NULL; ei_mutex_t* ei_sockets_lock = NULL; #endif /* _REENTRANT */ -static int init_socket_info(void) +static int init_socket_info(int late) { #ifdef _REENTRANT - if (ei_sockets_lock == NULL) { - ei_sockets_lock = ei_mutex_create(); - } + if (late) + return ENOTSUP; /* Refuse doing unsafe initialization... */ + ei_sockets_lock = ei_mutex_create(); + if (!ei_sockets_lock) + return ENOMEM; #endif /* _REENTRANT */ - return !0; + return 0; } static int put_ei_socket_info(int fd, int dist_version, char* cookie, ei_cnode *ec, @@ -600,6 +603,38 @@ static int initWinSock(void) } #endif +static int init_connect(int late) +{ + int error; + + /* + * 'late' is non-zero when not called via ei_init(). Such a + * call is not supported, but we for now save the day if + * it easy to do so; otherwise, return ENOTSUP. + */ + +#ifdef __WIN32__ + if (!initWinSock()) { + EI_TRACE_ERR0("ei_init_connect","can't initiate winsock"); + return EIO; + } +#endif /* win32 */ + + error = init_socket_info(late); + if (error) { + EI_TRACE_ERR0("ei_init_connect","can't initiate socket info"); + return error; + } + + ei_connect_initialized = !0; + return 0; +} + +int ei_init_connect(void) +{ + return init_connect(0); +} + /* * Perhaps run this routine instead of ei_connect_init/2 ? * Initailize by setting: @@ -613,24 +648,12 @@ int ei_connect_xinit_ussi(ei_cnode* ec, const char *thishostname, { char *dbglevel; + if (!ei_connect_initialized) + init_connect(!0); + if (cbs != &ei_default_socket_callbacks) EI_SET_HAVE_PLUGIN_SOCKET_IMPL__; -/* FIXME this code was enabled for 'erl'_connect_xinit(), why not here? */ -#if 0 -#ifdef __WIN32__ - if (!initWinSock()) { - EI_TRACE_ERR0("ei_connect_xinit","can't initiate winsock"); - return ERL_ERROR; - } -#endif -#endif - - if (!init_socket_info()) { - EI_TRACE_ERR0("ei_connect_xinit","can't initiate socket info"); - return ERL_ERROR; - } - if (cbs_sz < EI_SOCKET_CALLBACKS_SZ_V1) { EI_TRACE_ERR0("ei_connect_xinit","invalid size of ei_socket_callbacks struct"); return ERL_ERROR; @@ -718,13 +741,9 @@ int ei_connect_init_ussi(ei_cnode* ec, const char* this_node_name, int ei_h_errno; int res; -#ifdef __WIN32__ - if (!initWinSock()) { - EI_TRACE_ERR0("ei_connect_xinit","can't initiate winsock"); - return ERL_ERROR; - } -#endif /* win32 */ - + if (!ei_connect_initialized) + init_connect(!0); + /* gethostname requires len to be max(hostname) + 1 */ if (gethostname(thishostname, EI_MAXHOSTNAMELEN+1) == -1) { #ifdef __WIN32__ diff --git a/lib/erl_interface/src/connect/ei_resolve.c b/lib/erl_interface/src/connect/ei_resolve.c index 022a43d255..225fddc784 100644 --- a/lib/erl_interface/src/connect/ei_resolve.c +++ b/lib/erl_interface/src/connect/ei_resolve.c @@ -57,9 +57,9 @@ #ifdef HAVE_GETHOSTBYNAME_R -void ei_init_resolve(void) +int ei_init_resolve(void) { - return; /* Do nothing */ + return 0; /* Do nothing */ } #else /* !HAVE_GETHOSTBYNAME_R */ @@ -103,7 +103,7 @@ static int verify_dns_configuration(void); * our own, which are just wrappers around hostGetByName() and * hostGetByAddr(). Here we look up the functions. */ -void ei_init_resolve(void) +int ei_init_resolve(void) { #ifdef VXWORKS @@ -134,9 +134,12 @@ void ei_init_resolve(void) #ifdef _REENTRANT ei_gethost_sem = ei_mutex_create(); + if (!ei_gethost_sem) + return ENOMEM; #endif /* _REENTRANT */ ei_resolve_initialized = 1; + return 0; } #ifdef VXWORKS @@ -312,9 +315,11 @@ static struct hostent *my_gethostbyname_r(const char *name, struct hostent *src; struct hostent *rval = NULL; - /* FIXME this should have been done in 'erl'_init()? */ - if (!ei_resolve_initialized) ei_init_resolve(); - + if (!ei_resolve_initialized) { + *h_errnop = NO_RECOVERY; + return NULL; + } + #ifdef _REENTRANT /* === BEGIN critical section === */ if (ei_mutex_lock(ei_gethost_sem,0) != 0) { @@ -377,7 +382,10 @@ static struct hostent *my_gethostbyaddr_r(const char *addr, struct hostent *rval = NULL; /* FIXME this should have been done in 'erl'_init()? */ - if (!ei_resolve_initialized) ei_init_resolve(); + if (!ei_resolve_initialized) { + *h_errnop = NO_RECOVERY; + return NULL; + } #ifdef _REENTRANT /* === BEGIN critical section === */ diff --git a/lib/erl_interface/src/connect/ei_resolve.h b/lib/erl_interface/src/connect/ei_resolve.h index 10a49ffbc6..5711d7da76 100644 --- a/lib/erl_interface/src/connect/ei_resolve.h +++ b/lib/erl_interface/src/connect/ei_resolve.h @@ -20,6 +20,6 @@ #ifndef _EI_RESOLVE_H #define _EI_RESOLVE_H -void ei_init_resolve(void); +int ei_init_resolve(void); #endif /* _EI_RESOLVE_H */ diff --git a/lib/erl_interface/src/legacy/erl_eterm.c b/lib/erl_interface/src/legacy/erl_eterm.c index 9ad92121f4..7ed2bdbc93 100644 --- a/lib/erl_interface/src/legacy/erl_eterm.c +++ b/lib/erl_interface/src/legacy/erl_eterm.c @@ -65,7 +65,7 @@ void erl_init(void *hp,long heap_size) { erl_init_malloc(hp, heap_size); erl_init_marshal(); - ei_init_resolve(); + (void) ei_init(); } void erl_set_compat_rel(unsigned rel) diff --git a/lib/erl_interface/src/misc/ei_init.c b/lib/erl_interface/src/misc/ei_init.c new file mode 100644 index 0000000000..5357968657 --- /dev/null +++ b/lib/erl_interface/src/misc/ei_init.c @@ -0,0 +1,32 @@ +/* + * %CopyrightBegin% + * + * Copyright Ericsson AB 2019. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * %CopyrightEnd% + */ + +#include "ei.h" +#include "ei_resolve.h" +#include "ei_internal.h" + +int +ei_init(void) +{ + int error = ei_init_connect(); + if (error) + return error; + return ei_init_resolve(); +} diff --git a/lib/erl_interface/src/misc/ei_internal.h b/lib/erl_interface/src/misc/ei_internal.h index 0c58245c0a..f28dd6d668 100644 --- a/lib/erl_interface/src/misc/ei_internal.h +++ b/lib/erl_interface/src/misc/ei_internal.h @@ -153,6 +153,8 @@ extern int ei_tracelevel; +int ei_init_connect(void); + void ei_trace_printf(const char *name, int level, const char *format, ...); int ei_internal_use_r9_pids_ports(void); -- cgit v1.2.3