[erlang-questions] Who called my gen_server?

Mikael Pettersson mikpelinux@REDACTED
Wed Apr 26 22:00:24 CEST 2017

Frank Muller writes:
 > Hi guys,
 > I've two connected nodes: N1, N2.
 > N1 is mine and I've a gen_server running on it.
 > N2 is external and doesn't belong to me.
 > A process on N2 is calling my gen_server (on N1) via an rpc:call.
 > Is there a way to know the Pid of that process?

Based on a half hour study of lib/kernel/src/rpc.erl in OTP-19.3 I didn't
think so, but now I think I may have found a way.

On the callee node, rpc has a gen_server (rex) which handles incoming calls.
When it calls the {Mod,Fun,Args} from the rpc:call, it does so asynchronously
via a temporary process (see handle_call_call/6).  There is a mapping from
that process' Pid to the From passed to rex' handle_call (called 'To' in the code)
in rex' state, which you could dig out using sys:get_state or :get_status.

But on the caller node, rpc:call also uses a temporary process when doing the
gen_server:call to the callee node (see do_call/3), and the only association
between that process and the application process doing the rpc:call is buried
in the local variables of do_call/3 and the temporary process.  (So if you do
the sys:get_state on the callee node, you'd end up with the Pid of the temporary
process on the caller node.)

... but now it struck me that the temporary process is monitored by the process
doing the rpc:call, which erlang:process_info(Pid, monitored_by) should be able
to retrieve.  So a reverse rpc:call to the caller node to do that could work.

Not tested, will break if the rpc implementation changes, etc.

Why do you need this anyway?

 > Or, can I log all rpc_call(s) calling my gen_server?

rex doesn't have that built-in, but you could modify it to do so (it's on the
node you control)


More information about the erlang-questions mailing list