/*
*
* Common channel driver for Unix channels based on files, command
* pipes and TCP sockets.
*
* Copyright (c) 1995-1996 Sun Microsystems, Inc.
*
* See the file "license.terms" for information on usage and redistribution
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
*
* SCCS: @(#) tclUnixChan.c 1.185 96/11/12 14:49:17
*/
#include "tclInt.h" /* Internal definitions for Tcl. */
#include "tclPort.h" /* Portability features for Tcl. */
/*
* This structure describes per-instance state of a file based channel.
*/
typedef struct FileState {
} FileState;
/*
* This structure describes per-instance state of a pipe based channel.
*/
typedef struct PipeState {
* the creator of the pipe. */
* Used to decide whether to wait for the children
* at close time. */
} PipeState;
/*
* This structure describes per-instance state of a tcp based channel.
*/
typedef struct TcpState {
* bitfields defined below. */
} TcpState;
/*
* These bits may be ORed together into the "flags" field of a TcpState
* structure.
*/
/*
* The following defines the maximum length of the listen queue. This is
* the number of outstanding yet-to-be-serviced requests for a connection
* on a server socket, more than this number of outstanding requests and
* the connection request will fail.
*/
#ifndef SOMAXCONN
#endif
#if (SOMAXCONN < 100)
#endif
/*
* The following defines how much buffer space the kernel should maintain
* for a socket.
*/
/*
* Static routines for this file:
*/
#if 0
static int CreateSocketAddress _ANSI_ARGS_(
(struct sockaddr_in *sockaddrPtr,
#endif
static int FileBlockModeProc _ANSI_ARGS_((
Tcl_Interp *interp));
int direction));
static int FileOutputProc _ANSI_ARGS_((
int *errorCode));
int mask));
int mask));
#if 1
static int PipeBlockModeProc _ANSI_ARGS_((
Tcl_Interp *interp));
#endif
int direction));
static int PipeOutputProc _ANSI_ARGS_((
int *errorCode));
int mask));
int mask));
#if 0
int mode));
Tcl_Interp *interp));
int direction));
int mask));
int mask));
#else
#if 0
#define PipeBlockModeProc NULL
#define PipeCloseProc NULL
#define PipeGetProc NULL
#define PipeInputProc NULL
#define PipeOutputProc NULL
#define PipeReadyProc NULL
#define PipeWatchProc NULL
#endif
#endif
int *errorCodePtr));
/*
* This structure describes the channel type structure for file based IO:
*/
"file", /* Type name. */
FileBlockModeProc, /* Set blocking/nonblocking mode.*/
FileCloseProc, /* Close proc. */
FileInputProc, /* Input proc. */
FileOutputProc, /* Output proc. */
FileSeekProc, /* Seek proc. */
NULL, /* Set option proc. */
NULL, /* Get option proc. */
FileWatchProc, /* Initialize notifier. */
FileReadyProc, /* Are there events? */
FileGetProc, /* Get Tcl_Files out of channel. */
};
/*
* This structure describes the channel type structure for command pipe
* based IO:
*/
"pipe", /* Type name. */
PipeBlockModeProc, /* Set blocking/nonblocking mode.*/
PipeCloseProc, /* Close proc. */
PipeInputProc, /* Input proc. */
PipeOutputProc, /* Output proc. */
NULL, /* Seek proc. */
NULL, /* Set option proc. */
NULL, /* Get option proc. */
PipeWatchProc, /* Initialize notifier. */
PipeReadyProc, /* Are there events? */
PipeGetProc, /* Get Tcl_Files out of channel. */
};
/*
* This structure describes the channel type structure for TCP socket
* based IO:
*/
"tcp", /* Type name. */
TcpBlockModeProc, /* Set blocking/nonblocking mode.*/
TcpCloseProc, /* Close proc. */
TcpInputProc, /* Input proc. */
TcpOutputProc, /* Output proc. */
NULL, /* Seek proc. */
NULL, /* Set option proc. */
TcpGetOptionProc, /* Get option proc. */
TcpWatchProc, /* Initialize notifier. */
TcpReadyProc, /* Are there events? */
TcpGetProc, /* Get Tcl_Files out of channel. */
};
/*
*----------------------------------------------------------------------
*
* FileBlockModeProc --
*
* Helper procedure to set blocking and nonblocking modes on a
* file based channel. Invoked by generic IO level code.
*
* Results:
* 0 if successful, errno when failed.
*
* Side effects:
* Sets the device into blocking or non-blocking mode.
*
*----------------------------------------------------------------------
*/
/* ARGSUSED */
static int
int mode; /* The mode to set. Can be one of
* TCL_MODE_BLOCKING or
* TCL_MODE_NONBLOCKING. */
{
int curStatus;
int fd;
#ifndef USE_FIONBIO
if (mode == TCL_MODE_BLOCKING) {
curStatus &= (~(O_NONBLOCK));
} else {
curStatus |= O_NONBLOCK;
}
return errno;
}
}
if (mode == TCL_MODE_BLOCKING) {
curStatus &= (~(O_NONBLOCK));
} else {
curStatus |= O_NONBLOCK;
}
return errno;
}
}
#endif
#ifdef USE_FIONBIO
if (mode == TCL_MODE_BLOCKING) {
curStatus = 0;
} else {
curStatus = 1;
}
return errno;
}
}
if (mode == TCL_MODE_BLOCKING) {
curStatus = 0;
} else {
curStatus = 1;
}
return errno;
}
}
#endif
return 0;
}
/*
*----------------------------------------------------------------------
*
* FileInputProc --
*
* This procedure is invoked from the generic IO level to read
* input from a file based channel.
*
* Results:
* The number of bytes read is returned or -1 on error. An output
* argument contains a POSIX error code if an error occurs, or zero.
*
* Side effects:
* Reads input from the input device of the channel.
*
*----------------------------------------------------------------------
*/
static int
char *buf; /* Where to store data read. */
int toRead; /* How much space is available
* in the buffer? */
int *errorCodePtr; /* Where to store error code. */
{
* read from the input device? */
*errorCodePtr = 0;
/*
* Assume there is always enough input available. This will block
* appropriately, and read will unblock as soon as a short read is
* possible, if the channel is in blocking mode. If the channel is
* nonblocking, the read will never block.
*/
if (bytesRead > -1) {
return bytesRead;
}
*errorCodePtr = errno;
return -1;
}
/*
*----------------------------------------------------------------------
*
* FileOutputProc--
*
* This procedure is invoked from the generic IO level to write
* output to a file channel.
*
* Results:
* The number of bytes written is returned or -1 on error. An
* output argument contains a POSIX error code if an error occurred,
* or zero.
*
* Side effects:
* Writes output on the output device of the channel.
*
*----------------------------------------------------------------------
*/
static int
char *buf; /* The data buffer. */
int toWrite; /* How many bytes to write? */
int *errorCodePtr; /* Where to store error code. */
{
int written;
int fd;
*errorCodePtr = 0;
if (written > -1) {
return written;
}
*errorCodePtr = errno;
return -1;
}
/*
*----------------------------------------------------------------------
*
* FileCloseProc --
*
* This procedure is called from the generic IO level to perform
* channel-type-specific cleanup when a file based channel is closed.
*
* Results:
* 0 if successful, errno if failed.
*
* Side effects:
* Closes the device of the channel.
*
*----------------------------------------------------------------------
*/
static int
{
/*
*/
}
}
}
}
}
}
}
return errorCode;
}
/*
*----------------------------------------------------------------------
*
* FileSeekProc --
*
* This procedure is called by the generic IO level to move the
* access point in a file based channel.
*
* Results:
* -1 if failed, the new position if successful. An output
* argument contains the POSIX error code if an error occurred,
* or zero.
*
* Side effects:
* Moves the location at which the channel will be accessed in
* future operations.
*
*----------------------------------------------------------------------
*/
static int
long offset; /* Offset to seek to. */
int mode; /* Relative to where
* should we seek? Can be
* one of SEEK_START,
* SEEK_SET or SEEK_END. */
int *errorCodePtr; /* To store error code. */
{
int newLoc;
int fd;
*errorCodePtr = 0;
} else {
*errorCodePtr = EFAULT;
return -1;
}
if (newLoc != -1) {
return newLoc;
}
*errorCodePtr = errno;
return -1;
}
/*
*----------------------------------------------------------------------
*
* FileWatchProc --
*
* Initialize the notifier to watch Tcl_Files from this channel.
*
* Results:
* None.
*
* Side effects:
* Sets up the notifier so that a future event on the channel will
* be seen by Tcl.
*
*----------------------------------------------------------------------
*/
static void
int mask; /* Events of interest; an OR-ed
* combination of TCL_READABLE,
* TCL_WRITABLE and TCL_EXCEPTION. */
{
}
}
if (mask & TCL_EXCEPTION) {
}
}
}
}
/*
*----------------------------------------------------------------------
*
* FileReadyProc --
*
* Called by the notifier to check whether events of interest are
* present on the channel.
*
* Results:
* Returns OR-ed combination of TCL_READABLE, TCL_WRITABLE and
* TCL_EXCEPTION to indicate which events of interest are present.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static int
int mask; /* Events of interest; an OR-ed
* combination of TCL_READABLE,
* TCL_WRITABLE and TCL_EXCEPTION. */
{
int present = 0;
}
}
if (mask & TCL_EXCEPTION) {
}
}
}
return present;
}
/*
*----------------------------------------------------------------------
*
* FileGetProc --
*
* Called from Tcl_GetChannelFile to retrieve Tcl_Files from inside
* a file based channel.
*
* Results:
* The appropriate Tcl_File or NULL if not present.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static Tcl_File
int direction; /* Which Tcl_File to retrieve? */
{
if (direction == TCL_READABLE) {
}
if (direction == TCL_WRITABLE) {
}
}
#if 0
/*
*----------------------------------------------------------------------
*
* TclGetAndDetachPids --
*
* This procedure is invoked in the generic implementation of a
* background "exec" (An exec when invoked with a terminating "&")
* to store a list of the PIDs for processes in a command pipeline
* in interp->result and to detach the processes.
*
* Results:
* None.
*
* Side effects:
* Modifies interp->result. Detaches processes.
*
*----------------------------------------------------------------------
*/
void
{
int i;
char buf[20];
/*
* Punt if the channel is not a command channel.
*/
if (chanTypePtr != &pipeChannelType) {
return;
}
}
}
}
#endif
/*
*----------------------------------------------------------------------
*
* PipeBlockModeProc --
*
* Helper procedure to set blocking and nonblocking modes on a
* pipe based channel. Invoked by generic IO level code.
*
* Results:
* 0 if successful, errno when failed.
*
* Side effects:
* Sets the device into blocking or non-blocking mode.
*
*----------------------------------------------------------------------
*/
/* ARGSUSED */
static int
int mode; /* The mode to set. Can be one of
* TCL_MODE_BLOCKING or
* TCL_MODE_NONBLOCKING. */
{
int curStatus;
int fd;
#ifndef USE_FIONBIO
if (mode == TCL_MODE_BLOCKING) {
curStatus &= (~(O_NONBLOCK));
} else {
curStatus |= O_NONBLOCK;
}
return errno;
}
}
if (mode == TCL_MODE_BLOCKING) {
curStatus &= (~(O_NONBLOCK));
} else {
curStatus |= O_NONBLOCK;
}
return errno;
}
}
#endif /* !FIONBIO */
#ifdef USE_FIONBIO
if (mode == TCL_MODE_BLOCKING) {
curStatus = 0;
} else {
curStatus = 1;
}
return errno;
}
}
if (mode == TCL_MODE_BLOCKING) {
curStatus = 0;
} else {
curStatus = 1;
}
return errno;
}
}
#endif /* USE_FIONBIO */
return 0;
}
/*
*----------------------------------------------------------------------
*
* PipeCloseProc --
*
* This procedure is invoked by the generic IO level to perform
* channel-type-specific cleanup when a command pipeline channel
* is closed.
*
* Results:
* 0 on success, errno otherwise.
*
* Side effects:
* Closes the command pipeline channel.
*
*----------------------------------------------------------------------
*/
/* ARGSUSED */
static int
{
#if 0
#endif
errorCode = 0;
result = 0;
}
}
}
}
#if 0
/*
* If the channel is non-blocking or Tcl is being cleaned up, just
* detach the children PIDs, reap them (important if we are in a
* dynamic load module), and discard the errorFile.
*/
}
} else {
/*
* Wrap the error file into a channel and give it to the cleanup
* routine.
*/
} else {
}
errChan);
}
#endif
}
if (errorCode == 0) {
return result;
}
return errorCode;
}
/*
*----------------------------------------------------------------------
*
* PipeInputProc --
*
* This procedure is invoked from the generic IO level to read
* input from a command pipeline based channel.
*
* Results:
* The number of bytes read is returned or -1 on error. An output
* argument contains a POSIX error code if an error occurs, or zero.
*
* Side effects:
* Reads input from the input device of the channel.
*
*----------------------------------------------------------------------
*/
static int
char *buf; /* Where to store data read. */
int toRead; /* How much space is available
* in the buffer? */
int *errorCodePtr; /* Where to store error code. */
{
* read from the input device? */
*errorCodePtr = 0;
/*
* Assume there is always enough input available. This will block
* appropriately, and read will unblock as soon as a short read is
* possible, if the channel is in blocking mode. If the channel is
* nonblocking, the read will never block.
*/
if (bytesRead > -1) {
return bytesRead;
}
*errorCodePtr = errno;
return -1;
}
/*
*----------------------------------------------------------------------
*
* PipeOutputProc--
*
* This procedure is invoked from the generic IO level to write
* output to a command pipeline based channel.
*
* Results:
* The number of bytes written is returned or -1 on error. An
* output argument contains a POSIX error code if an error occurred,
* or zero.
*
* Side effects:
* Writes output on the output device of the channel.
*
*----------------------------------------------------------------------
*/
static int
char *buf; /* The data buffer. */
int toWrite; /* How many bytes to write? */
int *errorCodePtr; /* Where to store error code. */
{
int written;
int fd;
*errorCodePtr = 0;
if (written > -1) {
return written;
}
*errorCodePtr = errno;
return -1;
}
/*
*----------------------------------------------------------------------
*
* PipeWatchProc --
*
* Initialize the notifier to watch Tcl_Files from this channel.
*
* Results:
* None.
*
* Side effects:
* Sets up the notifier so that a future event on the channel will
* be seen by Tcl.
*
*----------------------------------------------------------------------
*/
static void
int mask; /* Events of interest; an OR-ed
* combination of TCL_READABLE,
* TCL_WRITABEL and TCL_EXCEPTION. */
{
}
}
if (mask & TCL_EXCEPTION) {
}
}
}
}
/*
*----------------------------------------------------------------------
*
* PipeReadyProc --
*
* Called by the notifier to check whether events of interest are
* present on the channel.
*
* Results:
* Returns OR-ed combination of TCL_READABLE, TCL_WRITABLE and
* TCL_EXCEPTION to indicate which events of interest are present.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static int
int mask; /* Events of interest; an OR-ed
* combination of TCL_READABLE,
* TCL_WRITABLE and TCL_EXCEPTION. */
{
int present = 0;
}
}
if (mask & TCL_EXCEPTION) {
}
}
}
return present;
}
/*
*----------------------------------------------------------------------
*
* PipeGetProc --
*
* Called from Tcl_GetChannelFile to retrieve Tcl_Files from inside
* a command pipeline based channel.
*
* Results:
* The appropriate Tcl_File or NULL if not present.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static Tcl_File
int direction; /* Which Tcl_File to retrieve? */
{
if (direction == TCL_READABLE) {
}
if (direction == TCL_WRITABLE) {
}
}
/*
*----------------------------------------------------------------------
*
* Tcl_OpenFileChannel --
*
* Open an file based channel on Unix systems.
*
* Results:
* The new channel or NULL. If NULL, the output argument
* errorCodePtr is set to a POSIX error and an error message is
* left in interp->result if interp is not NULL.
*
* Side effects:
* May open the channel and may cause creation of a file on the
* file system.
*
*----------------------------------------------------------------------
*/
* can be NULL. */
char *fileName; /* Name of file to open. */
char *modeString; /* A list of POSIX open modes or
* a string such as "rw". */
int permissions; /* If the open involves creating a
* file, with what modes to create
* it? */
{
if (mode == -1) {
return NULL;
}
case O_RDONLY:
break;
case O_WRONLY:
break;
case O_RDWR:
break;
default:
/*
* This may occurr if modeString was "", for example.
*/
panic("Tcl_OpenFileChannel: invalid mode value");
return NULL;
}
if (nativeName == NULL) {
return NULL;
}
/*
* If nativeName is not NULL, the buffer is valid and we must free
* the storage.
*/
if (fd < 0) {
}
return NULL;
}
/*
* Set close-on-exec flag on the fd so that child processes will not
* inherit this fd.
*/
if (channelPermissions & TCL_READABLE) {
} else {
}
if (channelPermissions & TCL_WRITABLE) {
} else {
}
/*
* The channel may not be open now, for example if we tried to
* open a file with permissions that cannot be satisfied.
*/
(char *) NULL);
}
return NULL;
}
if (seekFlag) {
(char *) NULL);
}
return NULL;
}
}
return chan;
}
/*
*----------------------------------------------------------------------
*
* Tcl_MakeFileChannel --
*
* Makes a Tcl_Channel from an existing OS level file handle.
*
* Results:
* The Tcl_Channel created around the preexisting OS level file handle.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
int mode; /* ORed combination of TCL_READABLE and
* TCL_WRITABLE to indicate whether inFile
{
int fileUsed;
if (mode == 0) {
return (Tcl_Channel) NULL;
}
if (mode & TCL_READABLE) {
}
if (mode & TCL_WRITABLE) {
}
/*
* Look to see if a channel with those two Tcl_Files already exists.
* If so, return it.
*/
return chan;
}
/*
* If one of the Tcl_Files is used in another channel, do not
* create a new channel containing it; this avoids core dumps
* later, when the Tcl_File would be freed twice.
*/
if (fileUsed) {
return (Tcl_Channel) NULL;
}
}
/*
*----------------------------------------------------------------------
*
* TclCreateCommandChannel --
*
* This function is called by the generic IO level to perform
* the platform specific channel initialization for a command
* channel.
*
* Results:
* Returns a new channel or NULL on failure.
*
* Side effects:
* Allocates a new channel.
*
*----------------------------------------------------------------------
*/
* can be read. */
int numPids; /* The number of pids in the pid array. */
int *pidPtr; /* An array of process identifiers.
* Allocated by the caller, freed when
* the channel is closed or the processes
* are detached (in a background exec). */
{
int channelId;
int mode;
statePtr->isNonBlocking = 0;
mode = 0;
mode |= TCL_READABLE;
}
mode |= TCL_WRITABLE;
}
/*
* Use one of the fds associated with the channel as the
* channel id.
*/
if (readFile) {
} else if (writeFile) {
} else if (errorFile) {
} else {
channelId = 0;
}
/*
* For backward compatibility with previous versions of Tcl, we
* use "file%d" as the base name for pipes even though it would
* be more natural to use "pipe%d".
*/
/*
* pidPtr will be freed by the caller if the return value is NULL.
*/
}
return channel;
}
/*
*----------------------------------------------------------------------
*
* Tcl_PidCmd --
*
* This procedure is invoked to process the "pid" Tcl command.
* See the user documentation for details on what it does.
*
* Results:
* A standard Tcl result.
*
* Side effects:
* See the user documentation.
*
*----------------------------------------------------------------------
*/
/* ARGSUSED */
int
int argc; /* Number of arguments. */
char **argv; /* Argument strings. */
{
int i; /* Loops over PIDs attached to the
* pipe. */
* PIDs attached to the pipe. */
if (argc > 2) {
return TCL_ERROR;
}
if (argc == 1) {
} else {
return TCL_ERROR;
}
if (chanTypePtr != &pipeChannelType) {
return TCL_OK;
}
}
}
return TCL_OK;
}
#if 0
/*
*----------------------------------------------------------------------
*
* TcpBlockModeProc --
*
* This procedure is invoked by the generic IO level to set blocking
* and nonblocking mode on a TCP socket based channel.
*
* Results:
* 0 if successful, errno when failed.
*
* Side effects:
* Sets the device into blocking or nonblocking mode.
*
*----------------------------------------------------------------------
*/
/* ARGSUSED */
static int
int mode; /* The mode to set. Can be one of
* TCL_MODE_BLOCKING or
* TCL_MODE_NONBLOCKING. */
{
int sock;
int setting;
#ifndef USE_FIONBIO
if (mode == TCL_MODE_BLOCKING) {
setting &= (~(O_NONBLOCK));
} else {
setting |= O_NONBLOCK;
}
return errno;
}
#endif
#ifdef USE_FIONBIO
if (mode == TCL_MODE_BLOCKING) {
setting = 0;
return errno;
}
} else {
setting = 1;
return errno;
}
}
#endif
return 0;
}
/*
*----------------------------------------------------------------------
*
* WaitForConnect --
*
* Waits for a connection on an asynchronously opened socket to
* be completed.
*
* Results:
* None.
*
* Side effects:
* The socket is connected after this function returns.
*
*----------------------------------------------------------------------
*/
static int
int *errorCodePtr; /* Where to store errors? */
{
int sock; /* The socket itself. */
int timeOut; /* How long to wait. */
int state; /* Of calling TclWaitForFile. */
int flags; /* fcntl flags for the socket. */
/*
* If an asynchronous connect is in progress, attempt to wait for it
* to complete before reading.
*/
timeOut = 0;
} else {
timeOut = -1;
}
errno = 0;
timeOut);
#ifndef USE_FIONBIO
flags &= (~(O_NONBLOCK));
#endif
#ifdef USE_FIONBIO
flags = 0;
#endif
}
if (state & TCL_EXCEPTION) {
return -1;
}
if (state & TCL_WRITABLE) {
} else if (timeOut == 0) {
return -1;
}
}
return 0;
}
/*
*----------------------------------------------------------------------
*
* TcpInputProc --
*
* This procedure is invoked by the generic IO level to read input
* from a TCP socket based channel.
*
* NOTE: We cannot share code with FilePipeInputProc because here
* we must use recv to obtain the input from the channel, not read.
*
* Results:
* The number of bytes read is returned or -1 on error. An output
* argument contains the POSIX error code on error, or zero if no
* error occurred.
*
* Side effects:
* Reads input from the input device of the channel.
*
*----------------------------------------------------------------------
*/
/* ARGSUSED */
static int
char *buf; /* Where to store data read. */
int bufSize; /* How much space is available
* in the buffer? */
int *errorCodePtr; /* Where to store error code. */
{
int sock; /* The OS handle. */
int bytesRead; /* How many bytes were read? */
int state; /* Of waiting for connection. */
*errorCodePtr = 0;
if (state != 0) {
return -1;
}
if (bytesRead > -1) {
return bytesRead;
}
if (errno == ECONNRESET) {
/*
* Turn ECONNRESET into a soft EOF condition.
*/
return 0;
}
*errorCodePtr = errno;
return -1;
}
/*
*----------------------------------------------------------------------
*
* TcpOutputProc --
*
* This procedure is invoked by the generic IO level to write output
* to a TCP socket based channel.
*
* NOTE: We cannot share code with FilePipeOutputProc because here
* we must use send, not write, to get reliable error reporting.
*
* Results:
* The number of bytes written is returned. An output argument is
* set to a POSIX error code if an error occurred, or zero.
*
* Side effects:
* Writes output on the output device of the channel.
*
*----------------------------------------------------------------------
*/
static int
char *buf; /* The data buffer. */
int toWrite; /* How many bytes to write? */
int *errorCodePtr; /* Where to store error code. */
{
int written;
int sock; /* OS level socket. */
int state; /* Of waiting for connection. */
*errorCodePtr = 0;
if (state != 0) {
return -1;
}
if (written > -1) {
return written;
}
*errorCodePtr = errno;
return -1;
}
/*
*----------------------------------------------------------------------
*
* TcpCloseProc --
*
* This procedure is invoked by the generic IO level to perform
* channel-type-specific cleanup when a TCP socket based channel
* is closed.
*
* Results:
* 0 if successful, the value of errno if failed.
*
* Side effects:
* Closes the socket of the channel.
*
*----------------------------------------------------------------------
*/
/* ARGSUSED */
static int
{
int sock;
int errorCode = 0;
/*
* Delete a file handler that may be active for this socket if this
* is a server socket - the file handler was created automatically
* by Tcl as part of the mechanism to accept new client connections.
* Channel handlers are already deleted in the generic IO channel
* closing code that called this function, so we do not have to
* delete them here.
*/
/*
* We assume that inFile==outFile==sockFile and so
* we only clean up sockFile.
*/
}
return errorCode;
}
/*
*----------------------------------------------------------------------
*
* TcpGetOptionProc --
*
* Computes an option value for a TCP socket based channel, or a
* list of all options and their values.
*
* Note: This code is based on code contributed by John Haxby.
*
* Results:
* A standard Tcl result. The value of the specified option or a
* list of all options and their values is returned in the
* supplied DString.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static int
char *optionName; /* Name of the option to
* retrieve the value for, or
* NULL to get all options and
* their values. */
* value; initialized by caller. */
{
struct sockaddr_in sockname;
struct sockaddr_in peername;
struct hostent *hostEntPtr;
int sock;
char buf[128];
if (optionName != (char *) NULL) {
}
if ((len == 0) ||
if (len == 0) {
}
} else {
}
if (len == 0) {
} else {
return TCL_OK;
}
}
}
if ((len == 0) ||
if (len == 0) {
}
} else {
}
if (len == 0) {
} else {
return TCL_OK;
}
}
}
if (len > 0) {
return TCL_ERROR;
}
return TCL_OK;
}
/*
*----------------------------------------------------------------------
*
* TcpWatchProc --
*
* Initialize the notifier to watch Tcl_Files from this channel.
*
* Results:
* None.
*
* Side effects:
* Sets up the notifier so that a future event on the channel will
* be seen by Tcl.
*
*----------------------------------------------------------------------
*/
static void
int mask; /* Events of interest; an OR-ed
* combination of TCL_READABLE,
* TCL_WRITABEL and TCL_EXCEPTION. */
{
}
/*
*----------------------------------------------------------------------
*
* TcpReadyProc --
*
* Called by the notifier to check whether events of interest are
* present on the channel.
*
* Results:
* Returns OR-ed combination of TCL_READABLE, TCL_WRITABLE and
* TCL_EXCEPTION to indicate which events of interest are present.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static int
int mask; /* Events of interest; an OR-ed
* combination of TCL_READABLE,
* TCL_WRITABLE and TCL_EXCEPTION. */
{
}
/*
*----------------------------------------------------------------------
*
* TcpGetProc --
*
* Called from Tcl_GetChannelFile to retrieve Tcl_Files from inside
* a TCP socket based channel.
*
* Results:
* The appropriate Tcl_File or NULL if not present.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
/* ARGSUSED */
static Tcl_File
int direction; /* Which Tcl_File to retrieve? */
{
}
/*
*----------------------------------------------------------------------
*
* CreateSocket --
*
* This function opens a new socket in client or server mode
* and initializes the TcpState structure.
*
* Results:
* Returns a new TcpState, or NULL with an error in interp->result,
* if interp is not NULL.
*
* Side effects:
* Opens a socket.
*
*----------------------------------------------------------------------
*/
static TcpState *
int port; /* Port number to open. */
char *host; /* Name of host on which to open port.
* NULL implies INADDR_ANY */
int server; /* 1 if socket should be a server socket,
* else 0 for a client socket. */
char *myaddr; /* Optional client-side address */
int myport; /* Optional client-side port */
int async; /* If nonzero and creating a client socket,
* attempt to do an async connect. Otherwise
* do a synchronous connect or bind. */
{
sock = -1;
origState = 0;
goto addressError;
}
goto addressError;
}
if (sock < 0) {
goto addressError;
}
/*
* Set the close-on-exec flag so that the socket will not get
* inherited by child processes.
*/
/*
* Set kernel space buffering
*/
asyncConnect = 0;
status = 0;
if (server) {
/*
* Set up to reuse server addresses automatically and bind to the
* specified port.
*/
status = 1;
sizeof(status));
sizeof(struct sockaddr));
if (status != -1) {
}
} else {
curState = 1;
sizeof(struct sockaddr));
if (status < 0) {
goto bindError;
}
}
/*
* Attempt to connect. The connect may fail at present with an
* EINPROGRESS but at a later time it will complete. The caller
* will set up a file handler on the socket if she is interested in
* being informed when the connect completes.
*/
if (async) {
#ifndef USE_FIONBIO
#endif
#ifdef USE_FIONBIO
curState = 1;
#endif
} else {
status = 0;
}
if (status > -1) {
sizeof(sockaddr));
if (status < 0) {
if (errno == EINPROGRESS) {
asyncConnect = 1;
status = 0;
}
}
}
}
if (status < 0) {
}
if (sock != -1) {
}
return NULL;
}
/*
* Allocate a new TcpState for this socket.
*/
if (asyncConnect) {
}
return statePtr;
if (sock != -1) {
}
}
return NULL;
}
/*
*----------------------------------------------------------------------
*
* CreateSocketAddress --
*
* This function initializes a sockaddr structure for a host and port.
*
* Results:
* 1 if the host was valid, 0 if the host could not be converted to
* an IP address.
*
* Side effects:
* Fills in the *sockaddrPtr structure.
*
*----------------------------------------------------------------------
*/
static int
char *host; /* Host. NULL implies INADDR_ANY */
int port; /* Port number */
{
} else {
} else {
#ifdef EHOSTUNREACH
#else
#ifdef ENXIO
#endif
#endif
return 0; /* error */
}
}
}
/*
* NOTE: On 64 bit machines the assignment below is rumored to not
* do the right thing. Please report errors related to this if you
* observe incorrect behavior on 64 bit machines such as DEC Alphas.
* Should we modify this code to do an explicit memcpy?
*/
return 1; /* Success. */
}
/*
*----------------------------------------------------------------------
*
* Tcl_OpenTcpClient --
*
* Opens a TCP client socket and creates a channel around it.
*
* Results:
* The channel or NULL if failed. An error message is returned
* in the interpreter on failure.
*
* Side effects:
* Opens a client socket and creates a new channel.
*
*----------------------------------------------------------------------
*/
int port; /* Port number to open. */
char *host; /* Host on which to open port. */
char *myaddr; /* Client-side address */
int myport; /* Client-side port */
int async; /* If nonzero, attempt to do an
* asynchronous connect. Otherwise
* we do a blocking connect. */
{
char channelName[20];
/*
* Create a new client socket and wrap it in a channel.
*/
return NULL;
}
TCL_ERROR) {
return NULL;
}
return chan;
}
/*
*----------------------------------------------------------------------
*
* Tcl_MakeTcpClientChannel --
*
* Creates a Tcl_Channel from an existing client TCP socket.
*
* Results:
* The Tcl_Channel wrapped around the preexisting TCP socket.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
{
char channelName[20];
"auto crlf") == TCL_ERROR) {
return NULL;
}
return chan;
}
/*
*----------------------------------------------------------------------
*
* Tcl_OpenTcpServer --
*
* Opens a TCP server socket and creates a channel around it.
*
* Results:
* The channel or NULL if failed. If an error occurred, an
* error message is left in interp->result if interp is
* not NULL.
*
* Side effects:
* Opens a server socket and creates a new channel.
*
*----------------------------------------------------------------------
*/
* NULL. */
int port; /* Port number to open. */
char *myHost; /* Name of local host. */
* from new clients. */
{
char channelName[20];
/*
* Create a new client socket and wrap it in a channel.
*/
return NULL;
}
/*
* Set up the callback mechanism for accepting connections
* from new clients.
*/
(ClientData) statePtr);
(ClientData) statePtr, 0);
return chan;
}
/*
*----------------------------------------------------------------------
*
* TcpAccept --
* Accept a TCP socket connection. This is called by the event loop.
*
* Results:
* None.
*
* Side effects:
* Creates a new connection socket. Calls the registered callback
* for the connection acceptance mechanism.
*
*----------------------------------------------------------------------
*/
/* ARGSUSED */
static void
int mask; /* Not used. */
{
int newsock; /* The new client socket */
int len; /* For accept interface */
char channelName[20];
len = sizeof(struct sockaddr_in);
if (newsock < 0) {
return;
}
/*
* Set close-on-exec flag to prevent the newly accepted socket from
* being inherited by child processes.
*/
if (newFile) {
newSockState->flags = 0;
ckfree((char *) newSockState);
} else {
"auto crlf") == TCL_ERROR) {
}
}
}
}
}
#endif
/*
*----------------------------------------------------------------------
*
* TclGetDefaultStdChannel --
*
* Creates channels for standard input, standard output or standard
* error output if they do not already exist.
*
* Results:
* Returns the specified default standard channel, or NULL.
*
* Side effects:
* May cause the creation of a standard channel and the underlying
* file.
*
*----------------------------------------------------------------------
*/
int type; /* One of TCL_STDIN, TCL_STDOUT, TCL_STDERR. */
{
switch (type) {
case TCL_STDIN:
return (Tcl_Channel) NULL;
}
fd = 0;
mode = TCL_READABLE;
bufMode = "line";
break;
case TCL_STDOUT:
return (Tcl_Channel) NULL;
}
fd = 1;
mode = TCL_WRITABLE;
bufMode = "line";
break;
case TCL_STDERR:
return (Tcl_Channel) NULL;
}
fd = 2;
mode = TCL_WRITABLE;
bufMode = "none";
break;
default:
panic("TclGetDefaultStdChannel: Unexpected channel type");
break;
}
/*
* Set up the normal channel options for stdio handles.
*/
TCL_ERROR) {
return NULL;
}
TCL_ERROR) {
return NULL;
}
return channel;
}
/*
*----------------------------------------------------------------------
*
* TclClosePipeFile --
*
* This function is a simple wrapper for close on a file or
* pipe handle. Called in the generic command pipeline cleanup
* code to do platform specific closing of the files associated
* with the command channel.
*
* Results:
* None.
*
* Side effects:
* Closes the fd and frees the Tcl_File.
*
*----------------------------------------------------------------------
*/
void
{
}
/*
*----------------------------------------------------------------------
*
* Tcl_GetOpenFile --
*
* Given a name of a channel registered in the given interpreter,
* returns a FILE * for it.
*
* Results:
* A standard Tcl result. If the channel is registered in the given
* interpreter and it is managed by the "file" channel driver, and
* it is open for the requested mode, then the output parameter
* filePtr is set to a FILE * for the underlying file. On error, the
* filePtr is not set, TCL_ERROR is returned and an error message is
* left in interp->result.
*
* Side effects:
* May invoke fdopen to create the FILE * for the requested file.
*
*----------------------------------------------------------------------
*/
int
char *string; /* String that identifies file. */
int forWriting; /* 1 means the file is going to be used
* for writing, 0 means for reading. */
int checkUsage; /* 1 means verify that the file was opened
* in a mode that allows the access specified
* by "forWriting". Ignored, we always
* check that the channel is open for the
* requested mode. */
{
int chanMode;
int fd;
FILE *f;
return TCL_ERROR;
}
return TCL_ERROR;
return TCL_ERROR;
}
/*
* We allow creating a FILE * out of file based, pipe based and socket
* based channels. We currently do not allow any other channel types,
* because it is likely that stdio will not know what to do with them.
*/
|| (chanTypePtr == &tcpChannelType)) {
/*
* The call to fdopen below is probably dangerous, since it will
* truncate an existing file if the file is being opened
* for writing....
*/
if (f == NULL) {
"\"", (char *) NULL);
return TCL_ERROR;
}
*filePtr = (ClientData) f;
return TCL_OK;
}
"\" cannot be used to get a FILE * - unsupported type",
(char *) NULL);
return TCL_ERROR;
}