.TH ssh_client_channel 3erl "ssh 4.15.3.1" "Ericsson AB" "Erlang Module Definition" .SH NAME ssh_client_channel \- -behaviour(ssh_client_channel). (Replaces ssh_channel) .SH DESCRIPTION .LP .RS -4 .B Note: .RE This module replaces ssh_channel\&. .LP The old module is still available for compatibility, but should not be used for new programs\&. The old module will not be maintained except for some error corrections .LP SSH services (clients and servers) are implemented as channels that are multiplexed over an SSH connection and communicates over the SSH Connection Protocol\&. This module provides a callback API that takes care of generic channel aspects for clients, such as flow control and close messages\&. It lets the callback functions take care of the service (application) specific parts\&. This behavior also ensures that the channel process honors the principal of an OTP-process so that it can be part of a supervisor tree\&. This is a requirement of channel processes implementing a subsystem that will be added to the \fIssh\fR\& applications supervisor tree\&. .LP .RS -4 .B Note: .RE When implementing a \fIssh\fR\& subsystem for daemons, use -behaviour(ssh_server_channel) (Replaces ssh_daemon_channel) instead\&. .LP .RS -4 .B Dont: .RE Functions in this module are not supposed to be called outside a module implementing this behaviour! .SH EXPORTS .LP .B call(ChannelRef, Msg) -> .br .B call(ChannelRef, Msg, Timeout) -> Reply | {error, Reason} .br .RS .LP Types: .RS 3 ChannelRef = pid() .br .RS 2 As returned by start_link/4 .RE Msg = term() .br Timeout = timeout() .br Reply = term() .br Reason = closed | timeout .br .RE .RE .RS .LP Makes a synchronous call to the channel process by sending a message and waiting until a reply arrives, or a time-out occurs\&. The channel calls Module:handle_call/3 to handle the message\&. If the channel process does not exist, \fI{error, closed}\fR\& is returned\&. .RE .LP .B cast(ChannelRef, Msg) -> ok .br .RS .LP Types: .RS 3 ChannelRef = pid() .br .RS 2 As returned by start_link/4 .RE Msg = term() .br .RE .RE .RS .LP Sends an asynchronous message to the channel process and returns ok immediately, ignoring if the destination node or channel process does not exist\&. The channel calls Module:handle_cast/2 to handle the message\&. .RE .LP .B enter_loop(State) -> _ .br .RS .LP Types: .RS 3 State = term() .br .RS 2 as returned by init/1 .RE .RE .RE .RS .LP Makes an existing process an \fIssh_client_channel\fR\& (replaces ssh_channel) process\&. Does not return, instead the calling process enters the \fIssh_client_channel\fR\& (replaces ssh_channel) process receive loop and become an \fIssh_client_channel\fR\& process\&. The process must have been started using one of the start functions in \fIproc_lib\fR\&, see the proc_lib(3erl) manual page in STDLIB\&. The user is responsible for any initialization of the process and must call init/1\&. .RE .LP .B init(Options) -> {ok, State} | {ok, State, Timeout} | {stop, Reason} .br .RS .LP Types: .RS 3 Options = [{Option, Value}] .br State = term() .br Timeout = timeout() .br Reason = term() .br .RE .RE .RS .LP The following options must be present: .RS 2 .TP 2 .B \fI{channel_cb, atom()}\fR\&: The module that implements the channel behaviour\&. .TP 2 .B \fI{init_args(), list()}\fR\&: The list of arguments to the \fIinit\fR\& function of the callback module\&. .TP 2 .B \fI{cm, \fR\&ssh:connection_ref()\fI}\fR\&: Reference to the \fIssh\fR\& connection as returned by ssh:connect/3\&. .TP 2 .B \fI{channel_id, \fR\&ssh:channel_id()\fI}\fR\&: Id of the \fIssh\fR\& channel as returned by ssh_connection:session_channel/2,4\&. .RE .LP .RS -4 .B Note: .RE This function is normally not called by the user\&. The user only needs to call if the channel process needs to be started with help of \fIproc_lib\fR\& instead of calling \fIstart/4\fR\& or \fIstart_link/4\fR\&\&. .RE .LP .B reply(Client, Reply) -> _ .br .RS .LP Types: .RS 3 Client = opaque() .br Reply = term() .br .RE .RE .RS .LP This function can be used by a channel to send a reply to a client that called \fIcall/[2,3]\fR\& when the reply cannot be defined in the return value of Module:handle_call/3\&. .LP \fIClient\fR\& must be the \fIFrom\fR\& argument provided to the callback function \fIhandle_call/3\fR\&\&. \fIReply\fR\& is an arbitrary term, which is given back to the client as the return value of call/[2,3]\&. .RE .LP .B start(SshConnection, ChannelId, ChannelCb, CbInitArgs) -> .br .B start_link(SshConnection, ChannelId, ChannelCb, CbInitArgs) -> {ok, ChannelRef} | {error, Reason} .br .RS .LP Types: .RS 3 SshConnection = ssh:connection_ref() .br .RS 2 As returned by ssh:connect/3 .RE ChannelId = ssh:channel_id() .br .RS 2 As returned by ssh_connection:session_channel/[2,4]\&. .RE ChannelCb = atom() .br .RS 2 Name of the module implementing the service-specific parts of the channel\&. .RE CbInitArgs = [term()] .br .RS 2 Argument list for the \fIinit\fR\& function in the callback module\&. .RE ChannelRef = pid() .br .RE .RE .RS .LP Starts a process that handles an SSH channel\&. It is called internally, by the \fIssh\fR\& daemon, or explicitly by the \fIssh\fR\& client implementations\&. The behavior sets the \fItrap_exit\fR\& flag to \fItrue\fR\&\&. .RE .SH "CALLBACK FUNCTIONS" .LP The following functions are to be exported from a \fIssh_client_channel\fR\& callback module\&. .LP The timeout values that can be returned by the callback functions have the same semantics as in a gen_server\&. If the time-out occurs, handle_msg/2 is called as \fIhandle_msg(timeout, State)\fR\&\&. .SH EXPORTS .LP .B Module:code_change(OldVsn, State, Extra) -> {ok, NewState} .br .RS .LP Types: .RS 3 OldVsn = term() .br .RS 2 In the case of an upgrade, \fIOldVsn\fR\& is \fIVsn\fR\&, and in the case of a downgrade, \fIOldVsn\fR\& is \fI{down,Vsn}\fR\&\&. \fIVsn\fR\& is defined by the \fIvsn\fR\& attribute(s) of the old version of the callback module \fIModule\fR\&\&. If no such attribute is defined, the version is the checksum of the BEAM file\&. .RE State = term() .br .RS 2 Internal state of the channel\&. .RE Extra = term() .br .RS 2 Passed "as-is" from the \fI{advanced,Extra}\fR\& part of the update instruction\&. .RE .RE .RE .RS .LP Converts process state when code is changed\&. .LP This function is called by a client-side channel when it is to update its internal state during a release upgrade or downgrade, that is, when the instruction \fI{update,Module,Change,\&.\&.\&.}\fR\&, where \fIChange={advanced,Extra}\fR\&, is given in the \fIappup\fR\& file\&. For more information, refer to Section 9\&.11\&.6 Release Handling Instructions in the System Documentation\&. .LP .RS -4 .B Note: .RE Soft upgrade according to the OTP release concept is not straight forward for the server side, as subsystem channel processes are spawned by the \fIssh\fR\& application and hence added to its supervisor tree\&. The subsystem channels can be upgraded when upgrading the user application, if the callback functions can handle two versions of the state, but this function cannot be used in the normal way\&. .RE .LP .B Module:init(Args) -> {ok, State} | {ok, State, timeout()} | {stop, Reason} .br .RS .LP Types: .RS 3 Args = term() .br .RS 2 Last argument to \fIstart_link/4\fR\&\&. .RE State = term() .br Reason = term() .br .RE .RE .RS .LP Makes necessary initializations and returns the initial channel state if the initializations succeed\&. .LP For more detailed information on time-outs, see Section Callback timeouts\&. .RE .LP .B Module:handle_call(Msg, From, State) -> Result .br .RS .LP Types: .RS 3 Msg = term() .br From = opaque() .br .RS 2 Is to be used as argument to reply/2 .RE State = term() .br Result = {reply, Reply, NewState} | {reply, Reply, NewState, timeout()} | {noreply, NewState} | {noreply , NewState, timeout()} | {stop, Reason, Reply, NewState} | {stop, Reason, NewState} .br Reply = term() .br .RS 2 Will be the return value of call/[2,3] .RE NewState = term() .br Reason = term() .br .RE .RE .RS .LP Handles messages sent by calling call/[2,3] .LP For more detailed information on time-outs,, see Section Callback timeouts\&. .RE .LP .B Module:handle_cast(Msg, State) -> Result .br .RS .LP Types: .RS 3 Msg = term() .br State = term() .br Result = {noreply, NewState} | {noreply, NewState, timeout()} | {stop, Reason, NewState} .br NewState = term() .br Reason = term() .br .RE .RE .RS .LP Handles messages sent by calling \fIcast/2\fR\&\&. .LP For more detailed information on time-outs, see Section Callback timeouts\&. .RE .LP .B Module:handle_msg(Msg, State) -> {ok, State} | {stop, ChannelId, State} .br .RS .LP Types: .RS 3 Msg = timeout | term() .br ChannelId = ssh:channel_id() .br State = term() .br .RE .RE .RS .LP Handles other messages than SSH Connection Protocol, call, or cast messages sent to the channel\&. .LP Possible Erlang \&'EXIT\&' messages is to be handled by this function and all channels are to handle the following message\&. .RS 2 .TP 2 .B \fI{ssh_channel_up, \fR\&ssh:channel_id()\fI, \fR\&ssh:connection_ref()\fI}\fR\&: This is the first message that the channel receives\&. It is sent just before the init/1 function returns successfully\&. This is especially useful if the server wants to send a message to the client without first receiving a message from it\&. If the message is not useful for your particular scenario, ignore it by immediately returning \fI{ok, State}\fR\&\&. .RE .RE .LP .B Module:handle_ssh_msg(Msg, State) -> {ok, State} | {stop, ChannelId, State} .br .RS .LP Types: .RS 3 Msg = ssh_connection:event() .br ChannelId = ssh:channel_id() .br State = term() .br .RE .RE .RS .LP Handles SSH Connection Protocol messages that may need service-specific attention\&. For details, see ssh_connection:event()\&. .LP The following message is taken care of by the \fIssh_client_channel\fR\& behavior\&. .RS 2 .TP 2 .B \fI{closed, \fR\&ssh:channel_id()\fI}\fR\&: The channel behavior sends a close message to the other side, if such a message has not already been sent\&. Then it terminates the channel with reason \fInormal\fR\&\&. .RE .RE .LP .B Module:terminate(Reason, State) -> _ .br .RS .LP Types: .RS 3 Reason = term() .br State = term() .br .RE .RE .RS .LP This function is called by a channel process when it is about to terminate\&. Before this function is called, ssh_connection:close/2 is called, if it has not been called earlier\&. This function does any necessary cleaning up\&. When it returns, the channel process terminates with reason \fIReason\fR\&\&. The return value is ignored\&. .RE