diff options
Diffstat (limited to 'erts')
-rw-r--r-- | erts/emulator/nifs/common/socket_dbg.c | 18 | ||||
-rw-r--r-- | erts/emulator/nifs/common/socket_dbg.h | 4 | ||||
-rw-r--r-- | erts/emulator/nifs/common/socket_int.h | 1 | ||||
-rw-r--r-- | erts/emulator/nifs/common/socket_nif.c | 180 | ||||
-rw-r--r-- | erts/emulator/test/socket_SUITE.erl | 76 | ||||
-rw-r--r-- | erts/preloaded/ebin/socket.beam | bin | 75044 -> 75584 bytes | |||
-rw-r--r-- | erts/preloaded/src/socket.erl | 41 |
7 files changed, 302 insertions, 18 deletions
diff --git a/erts/emulator/nifs/common/socket_dbg.c b/erts/emulator/nifs/common/socket_dbg.c index 7dfc4b77bc..0005575017 100644 --- a/erts/emulator/nifs/common/socket_dbg.c +++ b/erts/emulator/nifs/common/socket_dbg.c @@ -37,23 +37,23 @@ #define TNAME(__T__) enif_thread_name( __T__ ) #define TSNAME() TNAME(TSELF()) -static FILE* dbgout = NULL; +FILE* esock_dbgout = NULL; extern void esock_dbg_init(char* filename) { if (filename != NULL) { if (strcmp(filename, ESOCK_DBGOUT_DEFAULT) == 0) { - dbgout = stdout; + esock_dbgout = stdout; } else if (strcmp(filename, ESOCK_DBGOUT_UNIQUE) == 0) { - char template[] = "/tmp/esock-dbg-XXXXXX"; - dbgout = fdopen(mkstemp(template), "w+"); + char template[] = "/tmp/esock-dbg-XXXXXX"; + esock_dbgout = fdopen(mkstemp(template), "w+"); } else { - dbgout = fopen(filename, "w+"); + esock_dbgout = fopen(filename, "w+"); } } else { char template[] = "/tmp/esock-dbg-XXXXXX"; - dbgout = fdopen(mkstemp(template), "w+"); + esock_dbgout = fdopen(mkstemp(template), "w+"); } } @@ -67,7 +67,7 @@ extern void esock_dbg_printf( const char* prefix, const char* format, ... ) { va_list args; - char f[512 + sizeof(format)]; // This has to suffice... + char f[512 + strlen(format)]; // This has to suffice... char stamp[30]; int res; @@ -87,9 +87,9 @@ void esock_dbg_printf( const char* prefix, const char* format, ... ) if (res > 0) { va_start (args, format); - enif_vfprintf (dbgout, f, args); + enif_vfprintf (esock_dbgout, f, args); va_end (args); - fflush(dbgout); + fflush(esock_dbgout); } return; diff --git a/erts/emulator/nifs/common/socket_dbg.h b/erts/emulator/nifs/common/socket_dbg.h index 47739b46da..8fce211a8a 100644 --- a/erts/emulator/nifs/common/socket_dbg.h +++ b/erts/emulator/nifs/common/socket_dbg.h @@ -40,12 +40,12 @@ #endif typedef unsigned long long llu_t; - +extern FILE* esock_dbgout; // Initiated by the 'init' function #define ESOCK_DBG_PRINTF( ___COND___ , proto ) \ if ( ___COND___ ) { \ esock_dbg_printf proto; \ - fflush(stdout); \ + fflush(esock_dbgout); \ } diff --git a/erts/emulator/nifs/common/socket_int.h b/erts/emulator/nifs/common/socket_int.h index d6977be5aa..ac689e82b1 100644 --- a/erts/emulator/nifs/common/socket_int.h +++ b/erts/emulator/nifs/common/socket_int.h @@ -130,6 +130,7 @@ typedef unsigned int BOOLEAN_T; GLOBAL_ATOM_DEF(busy_poll); \ GLOBAL_ATOM_DEF(checksum); \ GLOBAL_ATOM_DEF(close); \ + GLOBAL_ATOM_DEF(command); \ GLOBAL_ATOM_DEF(connect); \ GLOBAL_ATOM_DEF(congestion); \ GLOBAL_ATOM_DEF(context); \ diff --git a/erts/emulator/nifs/common/socket_nif.c b/erts/emulator/nifs/common/socket_nif.c index 56a16a87a1..8b0d7ce4a9 100644 --- a/erts/emulator/nifs/common/socket_nif.c +++ b/erts/emulator/nifs/common/socket_nif.c @@ -659,6 +659,7 @@ typedef union { /* We should *eventually* use this instead of hard-coding the size (to 1) */ #define ESOCK_RECVMSG_IOVEC_SZ 1 +#define SOCKET_CMD_DEBUG 0x0001 #define SOCKET_SUPPORTS_OPTIONS 0x0001 #define SOCKET_SUPPORTS_SCTP 0x0002 @@ -948,6 +949,7 @@ extern char* erl_errno_id(int error); /* THIS IS JUST TEMPORARY??? */ * does the actual work. Except for the info function. * * nif_info + * nif_command * nif_supports * nif_open * nif_bind @@ -973,6 +975,7 @@ extern char* erl_errno_id(int error); /* THIS IS JUST TEMPORARY??? */ #define ESOCK_NIF_FUNCS \ ESOCK_NIF_FUNC_DEF(info); \ + ESOCK_NIF_FUNC_DEF(command); \ ESOCK_NIF_FUNC_DEF(supports); \ ESOCK_NIF_FUNC_DEF(open); \ ESOCK_NIF_FUNC_DEF(bind); \ @@ -1004,7 +1007,17 @@ ESOCK_NIF_FUNCS #if !defined(__WIN32__) + /* And here comes the functions that does the actual work (for the most part) */ +static BOOLEAN_T ecommand2command(ErlNifEnv* env, + ERL_NIF_TERM ecommand, + Uint16* command, + ERL_NIF_TERM* edata); +static ERL_NIF_TERM ncommand(ErlNifEnv* env, + Uint16 cmd, + ERL_NIF_TERM ecdata); +static ERL_NIF_TERM ncommand_debug(ErlNifEnv* env, ERL_NIF_TERM ecdata); + static ERL_NIF_TERM nsupports(ErlNifEnv* env, int key); static ERL_NIF_TERM nsupports_options(ErlNifEnv* env); static ERL_NIF_TERM nsupports_options_socket(ErlNifEnv* env); @@ -2661,6 +2674,7 @@ static char str_exsend[] = "exsend"; // failed send GLOBAL_ATOM_DECL(busy_poll); \ GLOBAL_ATOM_DECL(checksum); \ GLOBAL_ATOM_DECL(close); \ + GLOBAL_ATOM_DECL(command); \ GLOBAL_ATOM_DECL(connect); \ GLOBAL_ATOM_DECL(congestion); \ GLOBAL_ATOM_DECL(context); \ @@ -2967,8 +2981,8 @@ static ESOCK_INLINE ErlNifEnv* esock_alloc_env(const char* slogan) * Utility and admin functions: * ---------------------------- * nif_info/0 + * nif_command/1 * nif_supports/1 - * (nif_debug/1) * * The "proper" socket functions: * ------------------------------ @@ -3055,6 +3069,112 @@ ERL_NIF_TERM nif_info(ErlNifEnv* env, } +/* ---------------------------------------------------------------------- + * nif_command + * + * Description: + * This function is intended to handle "various" commands. That is, + * commands and operations that are not part of the socket API proper. + * Currently it handles setting the global debug. Its a map with two + * attributes command and (command) data: + * #{command :: atom(), data :: term()} + * + * Command Data + * debug boolean() + * + */ + +static +ERL_NIF_TERM nif_command(ErlNifEnv* env, + int argc, + const ERL_NIF_TERM argv[]) +{ +#if defined(__WIN32__) + return enif_raise_exception(env, MKA(env, "notsup")); +#else + ERL_NIF_TERM ecmd, ecdata, result; + Uint16 cmd; + + SGDBG( ("SOCKET", "nif_command -> entry with %d args\r\n", argc) ); + + if ((argc != 1) || + !IS_MAP(env, argv[0])) { + return enif_make_badarg(env); + } + ecmd = argv[0]; + + SGDBG( ("SOCKET", "nif_command -> " + "\r\n (e) command: %T" + "\r\n", ecmd) ); + + if (!ecommand2command(env, ecmd, &cmd, &ecdata)) { + SGDBG( ("SOCKET", "nif_command -> invalid command\r\n") ); + return esock_make_error(env, esock_atom_einval); + } + + SGDBG( ("SOCKET", "nif_command -> " + "\r\n command: %d" + "\r\n (e) command data: %T" + "\r\n", cmd, ecdata) ); + + result = ncommand(env, cmd, ecdata); + + SGDBG( ("SOCKET", "nif_command -> done with result: " + "\r\n %T" + "\r\n", result) ); + + return result; + +#endif +} + + +#if !defined(__WIN32__) +static +ERL_NIF_TERM ncommand(ErlNifEnv* env, Uint16 cmd, ERL_NIF_TERM ecdata) +{ + ERL_NIF_TERM result; + + SGDBG( ("SOCKET", "ncommand -> entry with 0x%lX\r\n", cmd) ); + + switch (cmd) { + case SOCKET_CMD_DEBUG: + result = ncommand_debug(env, ecdata); + break; + + default: + result = esock_make_error(env, esock_atom_einval); + break; + } + + return result; +} + + + +static +ERL_NIF_TERM ncommand_debug(ErlNifEnv* env, ERL_NIF_TERM ecdata) +{ + ERL_NIF_TERM result; + + /* The data *should* be a boolean() */ + + if (COMPARE(ecdata, esock_atom_true) == 0) { + data.dbg = TRUE; + result = esock_atom_ok; + } else if (COMPARE(ecdata, esock_atom_false) == 0) { + data.dbg = FALSE; + result = esock_atom_ok; + } else { + SGDBG( ("SOCKET", "ncommand_debug -> invalid debug value: %T\r\n", + ecdata) ); + result = esock_make_error(env, esock_atom_einval); + } + + return result; +} +#endif + /* ---------------------------------------------------------------------- * nif_supports @@ -17609,6 +17729,59 @@ BOOLEAN_T ehow2how(unsigned int ehow, int* how) +/* ecommand2command - convert erlang command to "native" command (and data) + */ +static +BOOLEAN_T ecommand2command(ErlNifEnv* env, + ERL_NIF_TERM ecommand, + Uint16* command, + ERL_NIF_TERM* edata) +{ + size_t sz; + ERL_NIF_TERM ecmd; + + if (!IS_MAP(env, ecommand)) { + SGDBG( ("SOCKET", "ecommand2command -> (e)command not a map\r\n") ); + return FALSE; + } + + /* The map shall have exactly two attrbutes: + * 'command' and 'data' + */ + if (!enif_get_map_size(env, ecommand, &sz) || (sz != 2)) { + SGDBG( ("SOCKET", "ecommand2command -> comamnd map size invalid\r\n") ); + return FALSE; + } + + /* Get the command value, and transform into integer + * (might as well do that, since theer is no point in + * extracting the data if command is invalid). + */ + if (!GET_MAP_VAL(env, ecommand, esock_atom_command, &ecmd)) { + SGDBG( ("SOCKET", "ecommand2command -> command attribute not found\r\n") ); + return FALSE; + } + if (COMPARE(ecmd, esock_atom_debug) == 0) { + *command = SOCKET_CMD_DEBUG; + } else { + SGDBG( ("SOCKET", "ecommand2command -> unknown command %T\r\n", ecmd) ); + return FALSE; + } + + /* Get the command data value, we do *not* convert it to + * the native form (here) since it may "in theory" be complex. + */ + if (!GET_MAP_VAL(env, ecommand, esock_atom_data, edata)) { + SGDBG( ("SOCKET", "ecommand2command -> (command) data not found\r\n") ); + return FALSE; + } + + return TRUE; +} + + + + #if defined(HAVE_SYS_UN_H) || defined(SO_BINDTODEVICE) /* strnlen doesn't exist everywhere */ /* @@ -19019,12 +19192,9 @@ ErlNifFunc socket_funcs[] = // Some utility and support functions {"nif_info", 0, nif_info, 0}, {"nif_supports", 1, nif_supports, 0}, - // {"nif_debug", 1, nif_debug, 0}, - // {"nif_command", 1, nif_command, 0}, + {"nif_command", 1, nif_command, 0}, // The proper "socket" interface - // nif_open/1 is used when we already have a file descriptor - // {"nif_open", 1, nif_open, 0}, {"nif_open", 4, nif_open, 0}, {"nif_bind", 2, nif_bind, 0}, {"nif_connect", 2, nif_connect, 0}, diff --git a/erts/emulator/test/socket_SUITE.erl b/erts/emulator/test/socket_SUITE.erl index c82e2efad9..410582f4a1 100644 --- a/erts/emulator/test/socket_SUITE.erl +++ b/erts/emulator/test/socket_SUITE.erl @@ -67,6 +67,9 @@ %% Test cases -export([ + %% *** API Misc *** + api_m_debug/1, + %% *** API Basic *** api_b_open_and_close_udp4/1, api_b_open_and_close_tcp4/1, @@ -583,6 +586,7 @@ use_group(Group, Env, Default) -> groups() -> [{api, [], api_cases()}, + {api_misc, [], api_misc_cases()}, {api_basic, [], api_basic_cases()}, {api_async, [], api_async_cases()}, {api_options, [], api_options_cases()}, @@ -660,12 +664,18 @@ groups() -> api_cases() -> [ + {group, api_misc}, {group, api_basic}, {group, api_async}, {group, api_options}, {group, api_op_with_timeout} ]. +api_misc_cases() -> + [ + api_m_debug + ]. + api_basic_cases() -> [ api_b_open_and_close_udp4, @@ -1650,6 +1660,72 @@ quiet_mode(Config) -> %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% %% +%% API MISC %% +%% %% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +%% A simple test case that tests that the global debug can be channged. +%% At the same time, it will test the info function (since it uses it +%% for verification). + +api_m_debug(suite) -> + []; +api_m_debug(doc) -> + []; +api_m_debug(_Config) when is_list(_Config) -> + ?TT(?SECS(5)), + tc_try(api_m_debug, + fun() -> has_bugfree_gcc() end, + fun() -> + ok = api_m_debug() + end). + +%% For some reason this test case triggers a gcc bug, which causes +%% a segfault, on an ancient Fedora 16 VM. So, check the version of gcc... +%% Not pretty, but the simplest way to skip (without actually testing for the host). +has_bugfree_gcc() -> + has_bugfree_gcc(os:type()). + +%% Make sure we are on linux +has_bugfree_gcc({unix, linux}) -> + has_bugfree_gcc2(os:cmd("cat /etc/issue")); +has_bugfree_gcc(_) -> + ok. + +%% Make sure we are on Fedora 16 +has_bugfree_gcc2("Fedora release 16 " ++ _) -> + has_bugfree_gcc3(os:cmd("gcc --version")); +has_bugfree_gcc2(_) -> + ok. + +has_bugfree_gcc3("gcc (GCC) 4.6.3 20120306 (Red Hat 4.6.3-2" ++ _) -> + skip("Buggy GCC"); +has_bugfree_gcc3(_) -> + ok. + +api_m_debug() -> + i("get initial info"), + #{debug := D0} = socket:info(), + D1 = not D0, + i("set new debug (~w => ~w)", [D0, D1]), + ok = socket:debug(D1), + i("get updated info (~w)", [D1]), + #{debug := D1} = socket:info(), + D2 = not D1, + i("set new debug (~w => ~w)", [D1, D2]), + ok = socket:debug(D2), + i("get updated info (~w)", [D2]), + #{debug := D2} = socket:info(), + i("ok"), + ok. + + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% %% %% API BASIC %% %% %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% diff --git a/erts/preloaded/ebin/socket.beam b/erts/preloaded/ebin/socket.beam Binary files differindex 134b4eac13..c4680c2baa 100644 --- a/erts/preloaded/ebin/socket.beam +++ b/erts/preloaded/ebin/socket.beam diff --git a/erts/preloaded/src/socket.erl b/erts/preloaded/src/socket.erl index ae1ffdb4ac..e9a76dc1e9 100644 --- a/erts/preloaded/src/socket.erl +++ b/erts/preloaded/src/socket.erl @@ -26,9 +26,13 @@ %% Administrative and "global" utility functions -export([ on_load/0, on_load/1, + + ensure_sockaddr/1, + + debug/1, + %% command/1, info/0, - supports/0, supports/1, supports/2, supports/3, - ensure_sockaddr/1 + supports/0, supports/1, supports/2, supports/3 ]). -export([ @@ -63,6 +67,8 @@ select_ref/0, select_info/0, + %% command/0, + domain/0, type/0, protocol/0, @@ -135,6 +141,18 @@ ]). +%% The command type has the general form: +%% #{ +%% command := atom(), +%% data := term() +%% } +%% But only certain values are actually valid, so the type gets the form: +-type debug_command() :: #{ + command := debug, + data := boolean() + }. +%% -type command() :: debug_command(). + -type uint8() :: 0..16#FF. -type uint16() :: 0..16#FFFF. -type uint20() :: 0..16#FFFFF. @@ -867,6 +885,22 @@ info() -> nif_info(). +-spec debug(D) -> ok when + D :: boolean(). + +debug(D) when is_boolean(D) -> + command(#{command => debug, + data => D}). + + +-spec command(Command) -> ok when + Command :: debug_command(). + +command(#{command := debug, + data := Dbg} = Command) when is_boolean(Dbg) -> + nif_command(Command). + + %% =========================================================================== %% @@ -3845,6 +3879,9 @@ error(Reason) -> nif_info() -> erlang:nif_error(undef). +nif_command(_Command) -> + erlang:nif_error(undef). + nif_supports(_Key) -> erlang:nif_error(undef). |