From 8a820377bb3e2e506cdff6011057d78507544142 Mon Sep 17 00:00:00 2001 From: Micael Karlberg Date: Mon, 21 Jan 2019 14:46:07 +0100 Subject: [socket-nif] Add support for otp option fd Add a way to *get* the file descriptor (fd) of a socket. Useful mostly for debugging. OTP-15528 --- erts/emulator/nifs/common/socket_nif.c | 35 +++++++++++++++++++------ erts/emulator/test/socket_SUITE.erl | 48 +++++++++------------------------- 2 files changed, 39 insertions(+), 44 deletions(-) (limited to 'erts/emulator') diff --git a/erts/emulator/nifs/common/socket_nif.c b/erts/emulator/nifs/common/socket_nif.c index 6dcd4ae623..44aa44f5fc 100644 --- a/erts/emulator/nifs/common/socket_nif.c +++ b/erts/emulator/nifs/common/socket_nif.c @@ -1,7 +1,7 @@ /* * %CopyrightBegin% * - * Copyright Ericsson AB 2018-2018. All Rights Reserved. + * Copyright Ericsson AB 2018-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. @@ -519,6 +519,7 @@ typedef union { #define SOCKET_OPT_OTP_RCVBUF 4 #define SOCKET_OPT_OTP_RCVCTRLBUF 6 #define SOCKET_OPT_OTP_SNDCTRLBUF 7 +#define SOCKET_OPT_OTP_FD 8 #define SOCKET_OPT_OTP_DOMAIN 0xFF01 // INTERNAL AND ONLY GET #define SOCKET_OPT_OTP_TYPE 0xFF02 // INTERNAL AND ONLY GET #define SOCKET_OPT_OTP_PROTOCOL 0xFF03 // INTERNAL AND ONLY GET @@ -1561,6 +1562,8 @@ static ERL_NIF_TERM ngetopt_otp_rcvctrlbuf(ErlNifEnv* env, SocketDescriptor* descP); static ERL_NIF_TERM ngetopt_otp_sndctrlbuf(ErlNifEnv* env, SocketDescriptor* descP); +static ERL_NIF_TERM ngetopt_otp_fd(ErlNifEnv* env, + SocketDescriptor* descP); static ERL_NIF_TERM ngetopt_otp_domain(ErlNifEnv* env, SocketDescriptor* descP); static ERL_NIF_TERM ngetopt_otp_type(ErlNifEnv* env, @@ -10031,6 +10034,10 @@ ERL_NIF_TERM ngetopt_otp(ErlNifEnv* env, result = ngetopt_otp_sndctrlbuf(env, descP); break; + case SOCKET_OPT_OTP_FD: + result = ngetopt_otp_fd(env, descP); + break; + /* *** INTERNAL *** */ case SOCKET_OPT_OTP_DOMAIN: result = ngetopt_otp_domain(env, descP); @@ -10058,7 +10065,7 @@ ERL_NIF_TERM ngetopt_otp(ErlNifEnv* env, } -/* ngetopt_otp_debug - Handle the OTP (level) debug options +/* ngetopt_otp_debug - Handle the OTP (level) debug option */ static ERL_NIF_TERM ngetopt_otp_debug(ErlNifEnv* env, @@ -10070,7 +10077,7 @@ ERL_NIF_TERM ngetopt_otp_debug(ErlNifEnv* env, } -/* ngetopt_otp_iow - Handle the OTP (level) iow options +/* ngetopt_otp_iow - Handle the OTP (level) iow option */ static ERL_NIF_TERM ngetopt_otp_iow(ErlNifEnv* env, @@ -10082,7 +10089,7 @@ ERL_NIF_TERM ngetopt_otp_iow(ErlNifEnv* env, } -/* ngetopt_otp_ctrl_proc - Handle the OTP (level) controlling_process options +/* ngetopt_otp_ctrl_proc - Handle the OTP (level) controlling_process option */ static ERL_NIF_TERM ngetopt_otp_ctrl_proc(ErlNifEnv* env, @@ -10095,7 +10102,7 @@ ERL_NIF_TERM ngetopt_otp_ctrl_proc(ErlNifEnv* env, -/* ngetopt_otp_rcvbuf - Handle the OTP (level) rcvbuf options +/* ngetopt_otp_rcvbuf - Handle the OTP (level) rcvbuf option */ static ERL_NIF_TERM ngetopt_otp_rcvbuf(ErlNifEnv* env, @@ -10107,7 +10114,7 @@ ERL_NIF_TERM ngetopt_otp_rcvbuf(ErlNifEnv* env, } -/* ngetopt_otp_rcvctrlbuf - Handle the OTP (level) rcvctrlbuf options +/* ngetopt_otp_rcvctrlbuf - Handle the OTP (level) rcvctrlbuf option */ static ERL_NIF_TERM ngetopt_otp_rcvctrlbuf(ErlNifEnv* env, @@ -10119,7 +10126,7 @@ ERL_NIF_TERM ngetopt_otp_rcvctrlbuf(ErlNifEnv* env, } -/* ngetopt_otp_sndctrlbuf - Handle the OTP (level) sndctrlbuf options +/* ngetopt_otp_sndctrlbuf - Handle the OTP (level) sndctrlbuf option */ static ERL_NIF_TERM ngetopt_otp_sndctrlbuf(ErlNifEnv* env, @@ -10131,7 +10138,19 @@ ERL_NIF_TERM ngetopt_otp_sndctrlbuf(ErlNifEnv* env, } -/* ngetopt_otp_domain - Handle the OTP (level) domain options. +/* ngetopt_otp_fd - Handle the OTP (level) fd option + */ +static +ERL_NIF_TERM ngetopt_otp_fd(ErlNifEnv* env, + SocketDescriptor* descP) +{ + ERL_NIF_TERM eVal = MKI(env, descP->sock); + + return esock_make_ok2(env, eVal); +} + + +/* ngetopt_otp_domain - Handle the OTP (level) domain option */ static ERL_NIF_TERM ngetopt_otp_domain(ErlNifEnv* env, diff --git a/erts/emulator/test/socket_SUITE.erl b/erts/emulator/test/socket_SUITE.erl index 6d44a245e4..2fbc0dddad 100644 --- a/erts/emulator/test/socket_SUITE.erl +++ b/erts/emulator/test/socket_SUITE.erl @@ -1,7 +1,7 @@ %% %% %CopyrightBegin% %% -%% Copyright Ericsson AB 2018-2018. All Rights Reserved. +%% Copyright Ericsson AB 2018-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. @@ -3112,37 +3112,6 @@ api_to_connect_tcp_await_timeout3([Sock|Socka]) -> (catch socket:close(Sock)), api_to_connect_tcp_await_timeout3(Socka). -%% api_to_connect_tcp_await_timeout(Socks, To, ServerSA) -> -%% api_to_connect_tcp_await_timeout(Socks, To, ServerSA, 1). - -%% api_to_connect_tcp_await_timeout([], _To, _ServerSA, _ID) -> -%% ?FAIL(unexpected_success); -%% api_to_connect_tcp_await_timeout([Sock|Socks], To, ServerSA, ID) -> -%% ?SEV_IPRINT("~w: try connect", [ID]), -%% Start = t(), -%% case socket:connect(Sock, ServerSA, To) of -%% {error, timeout} -> -%% ?SEV_IPRINT("expected timeout (~w)", [ID]), -%% Stop = t(), -%% TDiff = tdiff(Start, Stop), -%% if -%% (TDiff >= To) -> -%% ok; -%% true -> -%% {error, {unexpected_timeout, TDiff, To}} -%% end; -%% {error, econnreset = Reason} -> -%% ?SEV_IPRINT("failed connecting: ~p - giving up", [Reason]), -%% ok; -%% {error, Reason} -> -%% ?SEV_EPRINT("failed connecting: ~p", [Reason]), -%% ?FAIL({connect, Reason}); -%% ok -> -%% ?SEV_IPRINT("unexpected success (~w) - try next", [ID]), -%% api_to_connect_tcp_await_timeout(Socks, To, ServerSA, ID+1) -%% end. - - %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% @@ -6866,6 +6835,12 @@ sc_rc_tcp_client_create(Domain) -> i("sc_rc_tcp_client_create -> entry"), case socket:open(Domain, stream, tcp) of {ok, Sock} -> + case socket:getopt(Sock, otp, fd) of + {ok, FD} -> + put(sname, f("rclient-~w", [FD])); % Update SName + _ -> + ok + end, Sock; {error, Reason} -> exit({open_failed, Reason}) @@ -6884,10 +6859,11 @@ sc_rc_tcp_client_bind(Sock, Domain) -> end. sc_rc_tcp_client_announce_ready(Parent, Slogan) -> + ?SEV_IPRINT("ready ~w", [Slogan]), ?SEV_ANNOUNCE_READY(Parent, Slogan). sc_rc_tcp_client_await_continue(Parent, Slogan) -> - i("sc_rc_tcp_client_await_continue -> entry"), + ?SEV_IPRINT("await ~w continue", [Slogan]), ?SEV_AWAIT_CONTINUE(Parent, parent, Slogan). sc_rc_tcp_client_connect(Sock, ServerSA) -> @@ -6927,15 +6903,15 @@ sc_rc_tcp_handler_start(ID, Recv, Sock) -> Pid. sc_rc_tcp_handler(ID, Parent, Recv, Sock) -> - sc_rc_tcp_handler_init(ID, Parent), + sc_rc_tcp_handler_init(ID, socket:getopt(Sock, otp, fd), Parent), sc_rc_tcp_handler_await(Parent, recv), RecvRes = sc_rc_tcp_handler_recv(Recv, Sock), sc_rc_tcp_handler_announce_ready(Parent, recv, RecvRes), Reason = sc_rc_tcp_handler_await(Parent, terminate), exit(Reason). -sc_rc_tcp_handler_init(ID, Parent) -> - put(sname, f("handler-~w", [ID])), +sc_rc_tcp_handler_init(ID, {ok, FD}, Parent) -> + put(sname, f("handler-~w:~w", [ID, FD])), _MRef = erlang:monitor(process, Parent), ?SEV_IPRINT("started"), ?SEV_ANNOUNCE_READY(Parent, init), -- cgit v1.2.3