/*1* tclUnixChan.c2*3* Common channel driver for Unix channels based on files, command4* pipes and TCP sockets.5*6* Copyright (c) 1995-1996 Sun Microsystems, Inc.7*8* See the file "license.terms" for information on usage and redistribution9* of this file, and for a DISCLAIMER OF ALL WARRANTIES.10*11* SCCS: @(#) tclUnixChan.c 1.185 96/11/12 14:49:1712*/1314#include "tclInt.h" /* Internal definitions for Tcl. */15#include "tclPort.h" /* Portability features for Tcl. */1617#undef lseek1819/*20* This structure describes per-instance state of a file based channel.21*/2223typedef struct FileState {24Tcl_File inFile; /* Input from file. */25Tcl_File outFile; /* Output to file. */26} FileState;2728/*29* This structure describes per-instance state of a pipe based channel.30*/3132typedef struct PipeState {33Tcl_File inFile; /* Output from pipe. */34Tcl_File outFile; /* Input to pipe. */35Tcl_File errorFile; /* Error output from pipe. */36int numPids; /* How many processes are attached to this pipe? */37int *pidPtr; /* The process IDs themselves. Allocated by38* the creator of the pipe. */39int isNonBlocking; /* Nonzero when the pipe is in nonblocking mode.40* Used to decide whether to wait for the children41* at close time. */42} PipeState;4344/*45* This structure describes per-instance state of a tcp based channel.46*/4748typedef struct TcpState {49int flags; /* ORed combination of the50* bitfields defined below. */51Tcl_File sock; /* The socket itself. */52Tcl_TcpAcceptProc *acceptProc; /* Proc to call on accept. */53ClientData acceptProcData; /* The data for the accept proc. */54} TcpState;5556/*57* These bits may be ORed together into the "flags" field of a TcpState58* structure.59*/6061#define TCP_ASYNC_SOCKET (1<<0) /* Asynchronous socket. */62#define TCP_ASYNC_CONNECT (1<<1) /* Async connect in progress. */6364/*65* The following defines the maximum length of the listen queue. This is66* the number of outstanding yet-to-be-serviced requests for a connection67* on a server socket, more than this number of outstanding requests and68* the connection request will fail.69*/7071#ifndef SOMAXCONN72#define SOMAXCONN 10073#endif7475#if (SOMAXCONN < 100)76#undef SOMAXCONN77#define SOMAXCONN 10078#endif7980/*81* The following defines how much buffer space the kernel should maintain82* for a socket.83*/8485#define SOCKET_BUFSIZE 40968687/*88* Static routines for this file:89*/9091#if 092static TcpState * CreateSocket _ANSI_ARGS_((Tcl_Interp *interp,93int port, char *host, int server,94char *myaddr, int myport, int async));95static int CreateSocketAddress _ANSI_ARGS_(96(struct sockaddr_in *sockaddrPtr,97char *host, int port));98#endif99static int FileBlockModeProc _ANSI_ARGS_((100ClientData instanceData, int mode));101static int FileCloseProc _ANSI_ARGS_((ClientData instanceData,102Tcl_Interp *interp));103static Tcl_File FileGetProc _ANSI_ARGS_((ClientData instanceData,104int direction));105static int FileInputProc _ANSI_ARGS_((ClientData instanceData,106char *buf, int toRead, int *errorCode));107static int FileOutputProc _ANSI_ARGS_((108ClientData instanceData, char *buf, int toWrite,109int *errorCode));110static int FileReadyProc _ANSI_ARGS_((ClientData instanceData,111int mask));112static int FileSeekProc _ANSI_ARGS_((ClientData instanceData,113long offset, int mode, int *errorCode));114static void FileWatchProc _ANSI_ARGS_((ClientData instanceData,115int mask));116#if 1117static int PipeBlockModeProc _ANSI_ARGS_((118ClientData instanceData, int mode));119static int PipeCloseProc _ANSI_ARGS_((ClientData instanceData,120Tcl_Interp *interp));121#endif122static Tcl_File PipeGetProc _ANSI_ARGS_((ClientData instanceData,123int direction));124static int PipeInputProc _ANSI_ARGS_((ClientData instanceData,125char *buf, int toRead, int *errorCode));126static int PipeOutputProc _ANSI_ARGS_((127ClientData instanceData, char *buf, int toWrite,128int *errorCode));129static int PipeReadyProc _ANSI_ARGS_((ClientData instanceData,130int mask));131static void PipeWatchProc _ANSI_ARGS_((ClientData instanceData,132int mask));133#if 0134static void TcpAccept _ANSI_ARGS_((ClientData data, int mask));135static int TcpBlockModeProc _ANSI_ARGS_((ClientData data,136int mode));137static int TcpCloseProc _ANSI_ARGS_((ClientData instanceData,138Tcl_Interp *interp));139static Tcl_File TcpGetProc _ANSI_ARGS_((ClientData instanceData,140int direction));141static int TcpGetOptionProc _ANSI_ARGS_((ClientData instanceData,142char *optionName, Tcl_DString *dsPtr));143static int TcpInputProc _ANSI_ARGS_((ClientData instanceData,144char *buf, int toRead, int *errorCode));145static int TcpOutputProc _ANSI_ARGS_((ClientData instanceData,146char *buf, int toWrite, int *errorCode));147static int TcpReadyProc _ANSI_ARGS_((ClientData instanceData,148int mask));149static void TcpWatchProc _ANSI_ARGS_((ClientData instanceData,150int mask));151#else152#if 0153#define PipeBlockModeProc NULL154#define PipeCloseProc NULL155#define PipeGetProc NULL156#define PipeInputProc NULL157#define PipeOutputProc NULL158#define PipeReadyProc NULL159#define PipeWatchProc NULL160#endif161#define TcpGetOptionProc NULL162#define TcpCloseProc NULL163#define TcpInputProc NULL164#define TcpOutputProc NULL165#define TcpBlockModeProc NULL166#define TcpReadyProc NULL167#define TcpWatchProc NULL168#define TcpGetProc NULL169#endif170static int WaitForConnect _ANSI_ARGS_((TcpState *statePtr,171int *errorCodePtr));172173/*174* This structure describes the channel type structure for file based IO:175*/176177static Tcl_ChannelType fileChannelType = {178"file", /* Type name. */179FileBlockModeProc, /* Set blocking/nonblocking mode.*/180FileCloseProc, /* Close proc. */181FileInputProc, /* Input proc. */182FileOutputProc, /* Output proc. */183FileSeekProc, /* Seek proc. */184NULL, /* Set option proc. */185NULL, /* Get option proc. */186FileWatchProc, /* Initialize notifier. */187FileReadyProc, /* Are there events? */188FileGetProc, /* Get Tcl_Files out of channel. */189};190191/*192* This structure describes the channel type structure for command pipe193* based IO:194*/195196static Tcl_ChannelType pipeChannelType = {197"pipe", /* Type name. */198PipeBlockModeProc, /* Set blocking/nonblocking mode.*/199PipeCloseProc, /* Close proc. */200PipeInputProc, /* Input proc. */201PipeOutputProc, /* Output proc. */202NULL, /* Seek proc. */203NULL, /* Set option proc. */204NULL, /* Get option proc. */205PipeWatchProc, /* Initialize notifier. */206PipeReadyProc, /* Are there events? */207PipeGetProc, /* Get Tcl_Files out of channel. */208};209210/*211* This structure describes the channel type structure for TCP socket212* based IO:213*/214215static Tcl_ChannelType tcpChannelType = {216"tcp", /* Type name. */217TcpBlockModeProc, /* Set blocking/nonblocking mode.*/218TcpCloseProc, /* Close proc. */219TcpInputProc, /* Input proc. */220TcpOutputProc, /* Output proc. */221NULL, /* Seek proc. */222NULL, /* Set option proc. */223TcpGetOptionProc, /* Get option proc. */224TcpWatchProc, /* Initialize notifier. */225TcpReadyProc, /* Are there events? */226TcpGetProc, /* Get Tcl_Files out of channel. */227};228229/*230*----------------------------------------------------------------------231*232* FileBlockModeProc --233*234* Helper procedure to set blocking and nonblocking modes on a235* file based channel. Invoked by generic IO level code.236*237* Results:238* 0 if successful, errno when failed.239*240* Side effects:241* Sets the device into blocking or non-blocking mode.242*243*----------------------------------------------------------------------244*/245246/* ARGSUSED */247static int248FileBlockModeProc(instanceData, mode)249ClientData instanceData; /* File state. */250int mode; /* The mode to set. Can be one of251* TCL_MODE_BLOCKING or252* TCL_MODE_NONBLOCKING. */253{254FileState *fsPtr = (FileState *) instanceData;255int curStatus;256int fd;257258#ifndef USE_FIONBIO259if (fsPtr->inFile != NULL) {260fd = (int) Tcl_GetFileInfo(fsPtr->inFile, NULL);261curStatus = fcntl(fd, F_GETFL, 0);262if (mode == TCL_MODE_BLOCKING) {263curStatus &= (~(O_NONBLOCK));264} else {265curStatus |= O_NONBLOCK;266}267if (fcntl(fd, F_SETFL, curStatus) < 0) {268return errno;269}270curStatus = fcntl(fd, F_GETFL, 0);271}272if (fsPtr->outFile != NULL) {273fd = (int) Tcl_GetFileInfo(fsPtr->outFile, NULL);274curStatus = fcntl(fd, F_GETFL, 0);275if (mode == TCL_MODE_BLOCKING) {276curStatus &= (~(O_NONBLOCK));277} else {278curStatus |= O_NONBLOCK;279}280if (fcntl(fd, F_SETFL, curStatus) < 0) {281return errno;282}283}284#endif285286#ifdef USE_FIONBIO287if (fsPtr->inFile != NULL) {288fd = (int) Tcl_GetFileInfo(fsPtr->inFile, NULL);289if (mode == TCL_MODE_BLOCKING) {290curStatus = 0;291} else {292curStatus = 1;293}294if (ioctl(fd, (int) FIONBIO, &curStatus) < 0) {295return errno;296}297}298if (fsPtr->outFile != NULL) {299fd = (int) Tcl_GetFileInfo(fsPtr->outFile, NULL);300if (mode == TCL_MODE_BLOCKING) {301curStatus = 0;302} else {303curStatus = 1;304}305if (ioctl(fd, (int) FIONBIO, &curStatus) < 0) {306return errno;307}308}309#endif310311return 0;312}313314/*315*----------------------------------------------------------------------316*317* FileInputProc --318*319* This procedure is invoked from the generic IO level to read320* input from a file based channel.321*322* Results:323* The number of bytes read is returned or -1 on error. An output324* argument contains a POSIX error code if an error occurs, or zero.325*326* Side effects:327* Reads input from the input device of the channel.328*329*----------------------------------------------------------------------330*/331332static int333FileInputProc(instanceData, buf, toRead, errorCodePtr)334ClientData instanceData; /* File state. */335char *buf; /* Where to store data read. */336int toRead; /* How much space is available337* in the buffer? */338int *errorCodePtr; /* Where to store error code. */339{340FileState *fsPtr = (FileState *) instanceData;341int fd; /* The OS handle for reading. */342int bytesRead; /* How many bytes were actually343* read from the input device? */344345*errorCodePtr = 0;346fd = (int) Tcl_GetFileInfo(fsPtr->inFile, NULL);347348/*349* Assume there is always enough input available. This will block350* appropriately, and read will unblock as soon as a short read is351* possible, if the channel is in blocking mode. If the channel is352* nonblocking, the read will never block.353*/354355bytesRead = read(fd, buf, (size_t) toRead);356if (bytesRead > -1) {357return bytesRead;358}359*errorCodePtr = errno;360return -1;361}362363/*364*----------------------------------------------------------------------365*366* FileOutputProc--367*368* This procedure is invoked from the generic IO level to write369* output to a file channel.370*371* Results:372* The number of bytes written is returned or -1 on error. An373* output argument contains a POSIX error code if an error occurred,374* or zero.375*376* Side effects:377* Writes output on the output device of the channel.378*379*----------------------------------------------------------------------380*/381382static int383FileOutputProc(instanceData, buf, toWrite, errorCodePtr)384ClientData instanceData; /* File state. */385char *buf; /* The data buffer. */386int toWrite; /* How many bytes to write? */387int *errorCodePtr; /* Where to store error code. */388{389FileState *fsPtr = (FileState *) instanceData;390int written;391int fd;392393*errorCodePtr = 0;394fd = (int) Tcl_GetFileInfo(fsPtr->outFile, NULL);395written = write(fd, buf, (size_t) toWrite);396if (written > -1) {397return written;398}399*errorCodePtr = errno;400return -1;401}402403/*404*----------------------------------------------------------------------405*406* FileCloseProc --407*408* This procedure is called from the generic IO level to perform409* channel-type-specific cleanup when a file based channel is closed.410*411* Results:412* 0 if successful, errno if failed.413*414* Side effects:415* Closes the device of the channel.416*417*----------------------------------------------------------------------418*/419420static int421FileCloseProc(instanceData, interp)422ClientData instanceData; /* File state. */423Tcl_Interp *interp; /* For error reporting - unused. */424{425FileState *fsPtr = (FileState *) instanceData;426int fd, errorCode = 0;427428if (fsPtr->inFile != NULL) {429430/*431* Check for read/write file so we only close it once.432*/433434if (fsPtr->inFile == fsPtr->outFile) {435fsPtr->outFile = NULL;436}437fd = (int) Tcl_GetFileInfo(fsPtr->inFile, NULL);438Tcl_FreeFile(fsPtr->inFile);439if (!TclInExit() || ((fd != 0) && (fd != 1) && (fd != 2))) {440if (close(fd) < 0) {441errorCode = errno;442}443}444}445446if (fsPtr->outFile != NULL) {447fd = (int) Tcl_GetFileInfo(fsPtr->outFile, NULL);448Tcl_FreeFile(fsPtr->outFile);449if (!TclInExit() || ((fd != 0) && (fd != 1) && (fd != 2))) {450if ((close(fd) < 0) && (errorCode == 0)) {451errorCode = errno;452}453}454}455456ckfree((char *) fsPtr);457458return errorCode;459}460461/*462*----------------------------------------------------------------------463*464* FileSeekProc --465*466* This procedure is called by the generic IO level to move the467* access point in a file based channel.468*469* Results:470* -1 if failed, the new position if successful. An output471* argument contains the POSIX error code if an error occurred,472* or zero.473*474* Side effects:475* Moves the location at which the channel will be accessed in476* future operations.477*478*----------------------------------------------------------------------479*/480481static int482FileSeekProc(instanceData, offset, mode, errorCodePtr)483ClientData instanceData; /* File state. */484long offset; /* Offset to seek to. */485int mode; /* Relative to where486* should we seek? Can be487* one of SEEK_START,488* SEEK_SET or SEEK_END. */489int *errorCodePtr; /* To store error code. */490{491FileState *fsPtr = (FileState *) instanceData;492int newLoc;493int fd;494495*errorCodePtr = 0;496if (fsPtr->inFile != (Tcl_File) NULL) {497fd = (int) Tcl_GetFileInfo(fsPtr->inFile, NULL);498} else if (fsPtr->outFile != (Tcl_File) NULL) {499fd = (int) Tcl_GetFileInfo(fsPtr->outFile, NULL);500} else {501*errorCodePtr = EFAULT;502return -1;503}504newLoc = lseek(fd, offset, mode);505if (newLoc != -1) {506return newLoc;507}508*errorCodePtr = errno;509return -1;510}511512/*513*----------------------------------------------------------------------514*515* FileWatchProc --516*517* Initialize the notifier to watch Tcl_Files from this channel.518*519* Results:520* None.521*522* Side effects:523* Sets up the notifier so that a future event on the channel will524* be seen by Tcl.525*526*----------------------------------------------------------------------527*/528529static void530FileWatchProc(instanceData, mask)531ClientData instanceData; /* The file state. */532int mask; /* Events of interest; an OR-ed533* combination of TCL_READABLE,534* TCL_WRITABLE and TCL_EXCEPTION. */535{536FileState *fsPtr = (FileState *) instanceData;537538if ((mask & TCL_READABLE) && (fsPtr->inFile != (Tcl_File) NULL)) {539Tcl_WatchFile(fsPtr->inFile, TCL_READABLE);540}541if ((mask & TCL_WRITABLE) && (fsPtr->outFile != (Tcl_File) NULL)) {542Tcl_WatchFile(fsPtr->outFile, TCL_WRITABLE);543}544545if (mask & TCL_EXCEPTION) {546if (fsPtr->inFile != (Tcl_File) NULL) {547Tcl_WatchFile(fsPtr->inFile, TCL_EXCEPTION);548}549if (fsPtr->outFile != (Tcl_File) NULL) {550Tcl_WatchFile(fsPtr->outFile, TCL_EXCEPTION);551}552}553}554555/*556*----------------------------------------------------------------------557*558* FileReadyProc --559*560* Called by the notifier to check whether events of interest are561* present on the channel.562*563* Results:564* Returns OR-ed combination of TCL_READABLE, TCL_WRITABLE and565* TCL_EXCEPTION to indicate which events of interest are present.566*567* Side effects:568* None.569*570*----------------------------------------------------------------------571*/572573static int574FileReadyProc(instanceData, mask)575ClientData instanceData; /* The file state. */576int mask; /* Events of interest; an OR-ed577* combination of TCL_READABLE,578* TCL_WRITABLE and TCL_EXCEPTION. */579{580FileState *fsPtr = (FileState *) instanceData;581int present = 0;582583if ((mask & TCL_READABLE) && (fsPtr->inFile != (Tcl_File) NULL)) {584present |= Tcl_FileReady(fsPtr->inFile, TCL_READABLE);585}586if ((mask & TCL_WRITABLE) && (fsPtr->outFile != (Tcl_File) NULL)) {587present |= Tcl_FileReady(fsPtr->outFile, TCL_WRITABLE);588}589if (mask & TCL_EXCEPTION) {590if (fsPtr->inFile != (Tcl_File) NULL) {591present |= Tcl_FileReady(fsPtr->inFile, TCL_EXCEPTION);592}593if (fsPtr->outFile != (Tcl_File) NULL) {594present |= Tcl_FileReady(fsPtr->outFile, TCL_EXCEPTION);595}596}597return present;598}599600/*601*----------------------------------------------------------------------602*603* FileGetProc --604*605* Called from Tcl_GetChannelFile to retrieve Tcl_Files from inside606* a file based channel.607*608* Results:609* The appropriate Tcl_File or NULL if not present.610*611* Side effects:612* None.613*614*----------------------------------------------------------------------615*/616617static Tcl_File618FileGetProc(instanceData, direction)619ClientData instanceData; /* The file state. */620int direction; /* Which Tcl_File to retrieve? */621{622FileState *fsPtr = (FileState *) instanceData;623624if (direction == TCL_READABLE) {625return fsPtr->inFile;626}627if (direction == TCL_WRITABLE) {628return fsPtr->outFile;629}630return (Tcl_File) NULL;631}632633#if 0634/*635*----------------------------------------------------------------------636*637* TclGetAndDetachPids --638*639* This procedure is invoked in the generic implementation of a640* background "exec" (An exec when invoked with a terminating "&")641* to store a list of the PIDs for processes in a command pipeline642* in interp->result and to detach the processes.643*644* Results:645* None.646*647* Side effects:648* Modifies interp->result. Detaches processes.649*650*----------------------------------------------------------------------651*/652653void654TclGetAndDetachPids(interp, chan)655Tcl_Interp *interp;656Tcl_Channel chan;657{658PipeState *pipePtr;659Tcl_ChannelType *chanTypePtr;660int i;661char buf[20];662663/*664* Punt if the channel is not a command channel.665*/666667chanTypePtr = Tcl_GetChannelType(chan);668if (chanTypePtr != &pipeChannelType) {669return;670}671672pipePtr = (PipeState *) Tcl_GetChannelInstanceData(chan);673for (i = 0; i < pipePtr->numPids; i++) {674sprintf(buf, "%d", pipePtr->pidPtr[i]);675Tcl_AppendElement(interp, buf);676Tcl_DetachPids(1, &(pipePtr->pidPtr[i]));677}678if (pipePtr->numPids > 0) {679ckfree((char *) pipePtr->pidPtr);680pipePtr->numPids = 0;681}682}683#endif684685/*686*----------------------------------------------------------------------687*688* PipeBlockModeProc --689*690* Helper procedure to set blocking and nonblocking modes on a691* pipe based channel. Invoked by generic IO level code.692*693* Results:694* 0 if successful, errno when failed.695*696* Side effects:697* Sets the device into blocking or non-blocking mode.698*699*----------------------------------------------------------------------700*/701702/* ARGSUSED */703static int704PipeBlockModeProc(instanceData, mode)705ClientData instanceData; /* Pipe state. */706int mode; /* The mode to set. Can be one of707* TCL_MODE_BLOCKING or708* TCL_MODE_NONBLOCKING. */709{710PipeState *psPtr = (PipeState *) instanceData;711int curStatus;712int fd;713714#ifndef USE_FIONBIO715if (psPtr->inFile != NULL) {716fd = (int) Tcl_GetFileInfo(psPtr->inFile, NULL);717curStatus = fcntl(fd, F_GETFL, 0);718if (mode == TCL_MODE_BLOCKING) {719curStatus &= (~(O_NONBLOCK));720} else {721curStatus |= O_NONBLOCK;722}723if (fcntl(fd, F_SETFL, curStatus) < 0) {724return errno;725}726curStatus = fcntl(fd, F_GETFL, 0);727}728if (psPtr->outFile != NULL) {729fd = (int) Tcl_GetFileInfo(psPtr->outFile, NULL);730curStatus = fcntl(fd, F_GETFL, 0);731if (mode == TCL_MODE_BLOCKING) {732curStatus &= (~(O_NONBLOCK));733} else {734curStatus |= O_NONBLOCK;735}736if (fcntl(fd, F_SETFL, curStatus) < 0) {737return errno;738}739}740#endif /* !FIONBIO */741742#ifdef USE_FIONBIO743if (psPtr->inFile != NULL) {744fd = (int) Tcl_GetFileInfo(psPtr->inFile, NULL);745if (mode == TCL_MODE_BLOCKING) {746curStatus = 0;747} else {748curStatus = 1;749}750if (ioctl(fd, (int) FIONBIO, &curStatus) < 0) {751return errno;752}753}754if (psPtr->outFile != NULL) {755fd = (int) Tcl_GetFileInfo(psPtr->outFile, NULL);756if (mode == TCL_MODE_BLOCKING) {757curStatus = 0;758} else {759curStatus = 1;760}761if (ioctl(fd, (int) FIONBIO, &curStatus) < 0) {762return errno;763}764}765#endif /* USE_FIONBIO */766767return 0;768}769770/*771*----------------------------------------------------------------------772*773* PipeCloseProc --774*775* This procedure is invoked by the generic IO level to perform776* channel-type-specific cleanup when a command pipeline channel777* is closed.778*779* Results:780* 0 on success, errno otherwise.781*782* Side effects:783* Closes the command pipeline channel.784*785*----------------------------------------------------------------------786*/787788/* ARGSUSED */789static int790PipeCloseProc(instanceData, interp)791ClientData instanceData; /* The pipe to close. */792Tcl_Interp *interp; /* For error reporting. */793{794PipeState *pipePtr;795#if 0796FileState *fsPtr;797Tcl_Channel errChan;798#endif799int fd, errorCode, result;800801errorCode = 0;802result = 0;803pipePtr = (PipeState *) instanceData;804if (pipePtr->inFile != NULL) {805fd = (int) Tcl_GetFileInfo(pipePtr->inFile, NULL);806Tcl_FreeFile(pipePtr->inFile);807if (close(fd) < 0) {808errorCode = errno;809}810}811if (pipePtr->outFile != NULL) {812fd = (int) Tcl_GetFileInfo(pipePtr->outFile, NULL);813Tcl_FreeFile(pipePtr->outFile);814if ((close(fd) < 0) && (errorCode == 0)) {815errorCode = errno;816}817}818819#if 0820if (pipePtr->isNonBlocking || TclInExit()) {821822/*823* If the channel is non-blocking or Tcl is being cleaned up, just824* detach the children PIDs, reap them (important if we are in a825* dynamic load module), and discard the errorFile.826*/827828Tcl_DetachPids(pipePtr->numPids, pipePtr->pidPtr);829Tcl_ReapDetachedProcs();830831if (pipePtr->errorFile != NULL) {832Tcl_FreeFile(pipePtr->errorFile);833}834} else {835836/*837* Wrap the error file into a channel and give it to the cleanup838* routine.839*/840841if (pipePtr->errorFile != NULL) {842fsPtr = (FileState *) ckalloc((unsigned) sizeof(FileState));843fsPtr->inFile = pipePtr->errorFile;844fsPtr->outFile = (Tcl_File) NULL;845errChan = Tcl_CreateChannel(&fileChannelType, "pipeError",846(ClientData) fsPtr, TCL_READABLE);847} else {848errChan = NULL;849}850result = TclCleanupChildren(interp, pipePtr->numPids, pipePtr->pidPtr,851errChan);852}853#endif854855if (pipePtr->numPids != 0) {856ckfree((char *) pipePtr->pidPtr);857}858ckfree((char *) pipePtr);859if (errorCode == 0) {860return result;861}862return errorCode;863}864865/*866*----------------------------------------------------------------------867*868* PipeInputProc --869*870* This procedure is invoked from the generic IO level to read871* input from a command pipeline based channel.872*873* Results:874* The number of bytes read is returned or -1 on error. An output875* argument contains a POSIX error code if an error occurs, or zero.876*877* Side effects:878* Reads input from the input device of the channel.879*880*----------------------------------------------------------------------881*/882883static int884PipeInputProc(instanceData, buf, toRead, errorCodePtr)885ClientData instanceData; /* Pipe state. */886char *buf; /* Where to store data read. */887int toRead; /* How much space is available888* in the buffer? */889int *errorCodePtr; /* Where to store error code. */890{891PipeState *psPtr = (PipeState *) instanceData;892int fd; /* The OS handle for reading. */893int bytesRead; /* How many bytes were actually894* read from the input device? */895896*errorCodePtr = 0;897fd = (int) Tcl_GetFileInfo(psPtr->inFile, NULL);898899/*900* Assume there is always enough input available. This will block901* appropriately, and read will unblock as soon as a short read is902* possible, if the channel is in blocking mode. If the channel is903* nonblocking, the read will never block.904*/905906bytesRead = read(fd, buf, (size_t) toRead);907if (bytesRead > -1) {908return bytesRead;909}910*errorCodePtr = errno;911return -1;912}913914/*915*----------------------------------------------------------------------916*917* PipeOutputProc--918*919* This procedure is invoked from the generic IO level to write920* output to a command pipeline based channel.921*922* Results:923* The number of bytes written is returned or -1 on error. An924* output argument contains a POSIX error code if an error occurred,925* or zero.926*927* Side effects:928* Writes output on the output device of the channel.929*930*----------------------------------------------------------------------931*/932933static int934PipeOutputProc(instanceData, buf, toWrite, errorCodePtr)935ClientData instanceData; /* Pipe state. */936char *buf; /* The data buffer. */937int toWrite; /* How many bytes to write? */938int *errorCodePtr; /* Where to store error code. */939{940PipeState *psPtr = (PipeState *) instanceData;941int written;942int fd;943944*errorCodePtr = 0;945fd = (int) Tcl_GetFileInfo(psPtr->outFile, NULL);946written = write(fd, buf, (size_t) toWrite);947if (written > -1) {948return written;949}950*errorCodePtr = errno;951return -1;952}953954/*955*----------------------------------------------------------------------956*957* PipeWatchProc --958*959* Initialize the notifier to watch Tcl_Files from this channel.960*961* Results:962* None.963*964* Side effects:965* Sets up the notifier so that a future event on the channel will966* be seen by Tcl.967*968*----------------------------------------------------------------------969*/970971static void972PipeWatchProc(instanceData, mask)973ClientData instanceData; /* The pipe state. */974int mask; /* Events of interest; an OR-ed975* combination of TCL_READABLE,976* TCL_WRITABEL and TCL_EXCEPTION. */977{978PipeState *psPtr = (PipeState *) instanceData;979980if ((mask & TCL_READABLE) && (psPtr->inFile != (Tcl_File) NULL)) {981Tcl_WatchFile(psPtr->inFile, TCL_READABLE);982}983if ((mask & TCL_WRITABLE) && (psPtr->outFile != (Tcl_File) NULL)) {984Tcl_WatchFile(psPtr->outFile, TCL_WRITABLE);985}986987if (mask & TCL_EXCEPTION) {988if (psPtr->inFile != (Tcl_File) NULL) {989Tcl_WatchFile(psPtr->inFile, TCL_EXCEPTION);990}991if (psPtr->outFile != (Tcl_File) NULL) {992Tcl_WatchFile(psPtr->outFile, TCL_EXCEPTION);993}994}995}996997/*998*----------------------------------------------------------------------999*1000* PipeReadyProc --1001*1002* Called by the notifier to check whether events of interest are1003* present on the channel.1004*1005* Results:1006* Returns OR-ed combination of TCL_READABLE, TCL_WRITABLE and1007* TCL_EXCEPTION to indicate which events of interest are present.1008*1009* Side effects:1010* None.1011*1012*----------------------------------------------------------------------1013*/10141015static int1016PipeReadyProc(instanceData, mask)1017ClientData instanceData; /* The pipe state. */1018int mask; /* Events of interest; an OR-ed1019* combination of TCL_READABLE,1020* TCL_WRITABLE and TCL_EXCEPTION. */1021{1022PipeState *psPtr = (PipeState *) instanceData;1023int present = 0;10241025if ((mask & TCL_READABLE) && (psPtr->inFile != (Tcl_File) NULL)) {1026present |= Tcl_FileReady(psPtr->inFile, TCL_READABLE);1027}1028if ((mask & TCL_WRITABLE) && (psPtr->outFile != (Tcl_File) NULL)) {1029present |= Tcl_FileReady(psPtr->outFile, TCL_WRITABLE);1030}1031if (mask & TCL_EXCEPTION) {1032if (psPtr->inFile != (Tcl_File) NULL) {1033present |= Tcl_FileReady(psPtr->inFile, TCL_EXCEPTION);1034}1035if (psPtr->outFile != (Tcl_File) NULL) {1036present |= Tcl_FileReady(psPtr->outFile, TCL_EXCEPTION);1037}1038}1039return present;1040}10411042/*1043*----------------------------------------------------------------------1044*1045* PipeGetProc --1046*1047* Called from Tcl_GetChannelFile to retrieve Tcl_Files from inside1048* a command pipeline based channel.1049*1050* Results:1051* The appropriate Tcl_File or NULL if not present.1052*1053* Side effects:1054* None.1055*1056*----------------------------------------------------------------------1057*/10581059static Tcl_File1060PipeGetProc(instanceData, direction)1061ClientData instanceData; /* The pipe state. */1062int direction; /* Which Tcl_File to retrieve? */1063{1064PipeState *psPtr = (PipeState *) instanceData;10651066if (direction == TCL_READABLE) {1067return psPtr->inFile;1068}1069if (direction == TCL_WRITABLE) {1070return psPtr->outFile;1071}1072return (Tcl_File) NULL;1073}10741075/*1076*----------------------------------------------------------------------1077*1078* Tcl_OpenFileChannel --1079*1080* Open an file based channel on Unix systems.1081*1082* Results:1083* The new channel or NULL. If NULL, the output argument1084* errorCodePtr is set to a POSIX error and an error message is1085* left in interp->result if interp is not NULL.1086*1087* Side effects:1088* May open the channel and may cause creation of a file on the1089* file system.1090*1091*----------------------------------------------------------------------1092*/10931094Tcl_Channel1095Tcl_OpenFileChannel(interp, fileName, modeString, permissions)1096Tcl_Interp *interp; /* Interpreter for error reporting;1097* can be NULL. */1098char *fileName; /* Name of file to open. */1099char *modeString; /* A list of POSIX open modes or1100* a string such as "rw". */1101int permissions; /* If the open involves creating a1102* file, with what modes to create1103* it? */1104{1105int fd, seekFlag, mode, channelPermissions;1106Tcl_File file;1107FileState *fsPtr;1108Tcl_Channel chan;1109char *nativeName, channelName[20];1110Tcl_DString buffer;11111112mode = TclGetOpenMode(interp, modeString, &seekFlag);1113if (mode == -1) {1114return NULL;1115}1116switch (mode & (O_RDONLY | O_WRONLY | O_RDWR)) {1117case O_RDONLY:1118channelPermissions = TCL_READABLE;1119break;1120case O_WRONLY:1121channelPermissions = TCL_WRITABLE;1122break;1123case O_RDWR:1124channelPermissions = (TCL_READABLE | TCL_WRITABLE);1125break;1126default:1127/*1128* This may occurr if modeString was "", for example.1129*/1130panic("Tcl_OpenFileChannel: invalid mode value");1131return NULL;1132}11331134nativeName = Tcl_TranslateFileName(interp, fileName, &buffer);1135if (nativeName == NULL) {1136return NULL;1137}1138fd = open(nativeName, mode, permissions);11391140/*1141* If nativeName is not NULL, the buffer is valid and we must free1142* the storage.1143*/11441145Tcl_DStringFree(&buffer);11461147if (fd < 0) {1148if (interp != (Tcl_Interp *) NULL) {1149Tcl_AppendResult(interp, "couldn't open \"", fileName, "\": ",1150Tcl_PosixError(interp), (char *) NULL);1151}1152return NULL;1153}11541155/*1156* Set close-on-exec flag on the fd so that child processes will not1157* inherit this fd.1158*/11591160fcntl(fd, F_SETFD, FD_CLOEXEC);11611162sprintf(channelName, "file%d", fd);1163file = Tcl_GetFile((ClientData) fd, TCL_UNIX_FD);11641165fsPtr = (FileState *) ckalloc((unsigned) sizeof(FileState));1166if (channelPermissions & TCL_READABLE) {1167fsPtr->inFile = file;1168} else {1169fsPtr->inFile = (Tcl_File) NULL;1170}1171if (channelPermissions & TCL_WRITABLE) {1172fsPtr->outFile = file;1173} else {1174fsPtr->outFile = (Tcl_File) NULL;1175}1176chan = Tcl_CreateChannel(&fileChannelType, channelName,1177(ClientData) fsPtr, channelPermissions);11781179/*1180* The channel may not be open now, for example if we tried to1181* open a file with permissions that cannot be satisfied.1182*/11831184if (chan == (Tcl_Channel) NULL) {1185if (interp != (Tcl_Interp *) NULL) {1186Tcl_AppendResult(interp, "couldn't create channel \"",1187channelName, "\": ", Tcl_PosixError(interp),1188(char *) NULL);1189}1190Tcl_FreeFile(file);1191close(fd);1192return NULL;1193}11941195if (seekFlag) {1196if (Tcl_Seek(chan, 0, SEEK_END) < 0) {1197if (interp != (Tcl_Interp *) NULL) {1198Tcl_AppendResult(interp, "couldn't seek to end of file on \"",1199channelName, "\": ", Tcl_PosixError(interp),1200(char *) NULL);1201}1202Tcl_Close(NULL, chan);1203return NULL;1204}1205}1206return chan;1207}12081209/*1210*----------------------------------------------------------------------1211*1212* Tcl_MakeFileChannel --1213*1214* Makes a Tcl_Channel from an existing OS level file handle.1215*1216* Results:1217* The Tcl_Channel created around the preexisting OS level file handle.1218*1219* Side effects:1220* None.1221*1222*----------------------------------------------------------------------1223*/12241225Tcl_Channel1226Tcl_MakeFileChannel(inFd, outFd, mode)1227ClientData inFd; /* OS level handle used for input. */1228ClientData outFd; /* OS level handle used for output. */1229int mode; /* ORed combination of TCL_READABLE and1230* TCL_WRITABLE to indicate whether inFile1231* and/or outFile are valid. */1232{1233Tcl_Channel chan;1234int fileUsed;1235Tcl_File inFile, outFile;1236FileState *fsPtr;1237char channelName[20];12381239if (mode == 0) {1240return (Tcl_Channel) NULL;1241}12421243inFile = (Tcl_File) NULL;1244outFile = (Tcl_File) NULL;12451246if (mode & TCL_READABLE) {1247sprintf(channelName, "file%d", (int) inFd);1248inFile = Tcl_GetFile(inFd, TCL_UNIX_FD);1249}12501251if (mode & TCL_WRITABLE) {1252sprintf(channelName, "file%d", (int) outFd);1253outFile = Tcl_GetFile(outFd, TCL_UNIX_FD);1254}12551256/*1257* Look to see if a channel with those two Tcl_Files already exists.1258* If so, return it.1259*/12601261chan = TclFindFileChannel(inFile, outFile, &fileUsed);1262if (chan != (Tcl_Channel) NULL) {1263return chan;1264}12651266/*1267* If one of the Tcl_Files is used in another channel, do not1268* create a new channel containing it; this avoids core dumps1269* later, when the Tcl_File would be freed twice.1270*/12711272if (fileUsed) {1273return (Tcl_Channel) NULL;1274}1275fsPtr = (FileState *) ckalloc((unsigned) sizeof(FileState));1276fsPtr->inFile = inFile;1277fsPtr->outFile = outFile;12781279return Tcl_CreateChannel(&fileChannelType, channelName,1280(ClientData) fsPtr, mode);1281}12821283/*1284*----------------------------------------------------------------------1285*1286* TclCreateCommandChannel --1287*1288* This function is called by the generic IO level to perform1289* the platform specific channel initialization for a command1290* channel.1291*1292* Results:1293* Returns a new channel or NULL on failure.1294*1295* Side effects:1296* Allocates a new channel.1297*1298*----------------------------------------------------------------------1299*/13001301Tcl_Channel1302TclCreateCommandChannel(readFile, writeFile, errorFile, numPids, pidPtr)1303Tcl_File readFile; /* If non-null, gives the file for reading. */1304Tcl_File writeFile; /* If non-null, gives the file for writing. */1305Tcl_File errorFile; /* If non-null, gives the file where errors1306* can be read. */1307int numPids; /* The number of pids in the pid array. */1308int *pidPtr; /* An array of process identifiers.1309* Allocated by the caller, freed when1310* the channel is closed or the processes1311* are detached (in a background exec). */1312{1313Tcl_Channel channel;1314char channelName[20];1315int channelId;1316PipeState *statePtr = (PipeState *) ckalloc((unsigned) sizeof(PipeState));1317int mode;13181319statePtr->inFile = readFile;1320statePtr->outFile = writeFile;1321statePtr->errorFile = errorFile;1322statePtr->numPids = numPids;1323statePtr->pidPtr = pidPtr;1324statePtr->isNonBlocking = 0;13251326mode = 0;1327if (readFile != (Tcl_File) NULL) {1328mode |= TCL_READABLE;1329}1330if (writeFile != (Tcl_File) NULL) {1331mode |= TCL_WRITABLE;1332}13331334/*1335* Use one of the fds associated with the channel as the1336* channel id.1337*/13381339if (readFile) {1340channelId = (int) Tcl_GetFileInfo(readFile, NULL);1341} else if (writeFile) {1342channelId = (int) Tcl_GetFileInfo(writeFile, NULL);1343} else if (errorFile) {1344channelId = (int) Tcl_GetFileInfo(errorFile, NULL);1345} else {1346channelId = 0;1347}13481349/*1350* For backward compatibility with previous versions of Tcl, we1351* use "file%d" as the base name for pipes even though it would1352* be more natural to use "pipe%d".1353*/13541355sprintf(channelName, "file%d", channelId);1356channel = Tcl_CreateChannel(&pipeChannelType, channelName,1357(ClientData) statePtr, mode);13581359if (channel == NULL) {13601361/*1362* pidPtr will be freed by the caller if the return value is NULL.1363*/13641365ckfree((char *)statePtr);1366}1367return channel;1368}13691370/*1371*----------------------------------------------------------------------1372*1373* Tcl_PidCmd --1374*1375* This procedure is invoked to process the "pid" Tcl command.1376* See the user documentation for details on what it does.1377*1378* Results:1379* A standard Tcl result.1380*1381* Side effects:1382* See the user documentation.1383*1384*----------------------------------------------------------------------1385*/13861387/* ARGSUSED */1388int1389Tcl_PidCmd(dummy, interp, argc, argv)1390ClientData dummy; /* Not used. */1391Tcl_Interp *interp; /* Current interpreter. */1392int argc; /* Number of arguments. */1393char **argv; /* Argument strings. */1394{1395Tcl_Channel chan; /* The channel to get pids for. */1396Tcl_ChannelType *chanTypePtr; /* The type of that channel. */1397PipeState *pipePtr; /* The pipe state. */1398int i; /* Loops over PIDs attached to the1399* pipe. */1400char string[50]; /* Temp buffer for string rep. of1401* PIDs attached to the pipe. */14021403if (argc > 2) {1404Tcl_AppendResult(interp, "wrong # args: should be \"",1405argv[0], " ?channelId?\"", (char *) NULL);1406return TCL_ERROR;1407}1408if (argc == 1) {1409sprintf(interp->result, "%ld", (long) getpid());1410} else {1411chan = Tcl_GetChannel(interp, argv[1], NULL);1412if (chan == (Tcl_Channel) NULL) {1413return TCL_ERROR;1414}1415chanTypePtr = Tcl_GetChannelType(chan);1416if (chanTypePtr != &pipeChannelType) {1417return TCL_OK;1418}1419pipePtr = (PipeState *) Tcl_GetChannelInstanceData(chan);1420for (i = 0; i < pipePtr->numPids; i++) {1421sprintf(string, "%d", pipePtr->pidPtr[i]);1422Tcl_AppendElement(interp, string);1423}1424}1425return TCL_OK;1426}14271428#if 01429/*1430*----------------------------------------------------------------------1431*1432* TcpBlockModeProc --1433*1434* This procedure is invoked by the generic IO level to set blocking1435* and nonblocking mode on a TCP socket based channel.1436*1437* Results:1438* 0 if successful, errno when failed.1439*1440* Side effects:1441* Sets the device into blocking or nonblocking mode.1442*1443*----------------------------------------------------------------------1444*/14451446/* ARGSUSED */1447static int1448TcpBlockModeProc(instanceData, mode)1449ClientData instanceData; /* Socket state. */1450int mode; /* The mode to set. Can be one of1451* TCL_MODE_BLOCKING or1452* TCL_MODE_NONBLOCKING. */1453{1454TcpState *statePtr;1455int sock;1456int setting;14571458statePtr = (TcpState *) instanceData;1459sock = (int) Tcl_GetFileInfo(statePtr->sock, NULL);1460#ifndef USE_FIONBIO1461setting = fcntl(sock, F_GETFL, 0);1462if (mode == TCL_MODE_BLOCKING) {1463statePtr->flags &= (~(TCP_ASYNC_SOCKET));1464setting &= (~(O_NONBLOCK));1465} else {1466statePtr->flags |= TCP_ASYNC_SOCKET;1467setting |= O_NONBLOCK;1468}1469if (fcntl(sock, F_SETFL, setting) < 0) {1470return errno;1471}1472#endif14731474#ifdef USE_FIONBIO1475if (mode == TCL_MODE_BLOCKING) {1476statePtr->flags &= (~(TCP_ASYNC_SOCKET));1477setting = 0;1478if (ioctl(sock, (int) FIONBIO, &setting) == -1) {1479return errno;1480}1481} else {1482statePtr->flags |= TCP_ASYNC_SOCKET;1483setting = 1;1484if (ioctl(sock, (int) FIONBIO, &setting) == -1) {1485return errno;1486}1487}1488#endif14891490return 0;1491}14921493/*1494*----------------------------------------------------------------------1495*1496* WaitForConnect --1497*1498* Waits for a connection on an asynchronously opened socket to1499* be completed.1500*1501* Results:1502* None.1503*1504* Side effects:1505* The socket is connected after this function returns.1506*1507*----------------------------------------------------------------------1508*/15091510static int1511WaitForConnect(statePtr, errorCodePtr)1512TcpState *statePtr; /* State of the socket. */1513int *errorCodePtr; /* Where to store errors? */1514{1515int sock; /* The socket itself. */1516int timeOut; /* How long to wait. */1517int state; /* Of calling TclWaitForFile. */1518int flags; /* fcntl flags for the socket. */15191520/*1521* If an asynchronous connect is in progress, attempt to wait for it1522* to complete before reading.1523*/15241525if (statePtr->flags & TCP_ASYNC_CONNECT) {1526if (statePtr->flags & TCP_ASYNC_SOCKET) {1527timeOut = 0;1528} else {1529timeOut = -1;1530}1531errno = 0;1532state = TclWaitForFile(statePtr->sock, TCL_WRITABLE | TCL_EXCEPTION,1533timeOut);1534if (!(statePtr->flags & TCP_ASYNC_SOCKET)) {1535sock = (int) Tcl_GetFileInfo(statePtr->sock, NULL);1536#ifndef USE_FIONBIO1537flags = fcntl(sock, F_GETFL, 0);1538flags &= (~(O_NONBLOCK));1539(void) fcntl(sock, F_SETFL, flags);1540#endif15411542#ifdef USE_FIONBIO1543flags = 0;1544(void) ioctl(sock, FIONBIO, &flags);1545#endif1546}1547if (state & TCL_EXCEPTION) {1548return -1;1549}1550if (state & TCL_WRITABLE) {1551statePtr->flags &= (~(TCP_ASYNC_CONNECT));1552} else if (timeOut == 0) {1553*errorCodePtr = errno = EWOULDBLOCK;1554return -1;1555}1556}1557return 0;1558}15591560/*1561*----------------------------------------------------------------------1562*1563* TcpInputProc --1564*1565* This procedure is invoked by the generic IO level to read input1566* from a TCP socket based channel.1567*1568* NOTE: We cannot share code with FilePipeInputProc because here1569* we must use recv to obtain the input from the channel, not read.1570*1571* Results:1572* The number of bytes read is returned or -1 on error. An output1573* argument contains the POSIX error code on error, or zero if no1574* error occurred.1575*1576* Side effects:1577* Reads input from the input device of the channel.1578*1579*----------------------------------------------------------------------1580*/15811582/* ARGSUSED */1583static int1584TcpInputProc(instanceData, buf, bufSize, errorCodePtr)1585ClientData instanceData; /* Socket state. */1586char *buf; /* Where to store data read. */1587int bufSize; /* How much space is available1588* in the buffer? */1589int *errorCodePtr; /* Where to store error code. */1590{1591TcpState *statePtr; /* The state of the socket. */1592int sock; /* The OS handle. */1593int bytesRead; /* How many bytes were read? */1594int state; /* Of waiting for connection. */15951596*errorCodePtr = 0;1597statePtr = (TcpState *) instanceData;1598sock = (int) Tcl_GetFileInfo(statePtr->sock, NULL);15991600state = WaitForConnect(statePtr, errorCodePtr);1601if (state != 0) {1602return -1;1603}1604bytesRead = recv(sock, buf, bufSize, 0);1605if (bytesRead > -1) {1606return bytesRead;1607}1608if (errno == ECONNRESET) {16091610/*1611* Turn ECONNRESET into a soft EOF condition.1612*/16131614return 0;1615}1616*errorCodePtr = errno;1617return -1;1618}16191620/*1621*----------------------------------------------------------------------1622*1623* TcpOutputProc --1624*1625* This procedure is invoked by the generic IO level to write output1626* to a TCP socket based channel.1627*1628* NOTE: We cannot share code with FilePipeOutputProc because here1629* we must use send, not write, to get reliable error reporting.1630*1631* Results:1632* The number of bytes written is returned. An output argument is1633* set to a POSIX error code if an error occurred, or zero.1634*1635* Side effects:1636* Writes output on the output device of the channel.1637*1638*----------------------------------------------------------------------1639*/16401641static int1642TcpOutputProc(instanceData, buf, toWrite, errorCodePtr)1643ClientData instanceData; /* Socket state. */1644char *buf; /* The data buffer. */1645int toWrite; /* How many bytes to write? */1646int *errorCodePtr; /* Where to store error code. */1647{1648TcpState *statePtr;1649int written;1650int sock; /* OS level socket. */1651int state; /* Of waiting for connection. */16521653*errorCodePtr = 0;1654statePtr = (TcpState *) instanceData;1655sock = (int) Tcl_GetFileInfo(statePtr->sock, NULL);1656state = WaitForConnect(statePtr, errorCodePtr);1657if (state != 0) {1658return -1;1659}1660written = send(sock, buf, toWrite, 0);1661if (written > -1) {1662return written;1663}1664*errorCodePtr = errno;1665return -1;1666}16671668/*1669*----------------------------------------------------------------------1670*1671* TcpCloseProc --1672*1673* This procedure is invoked by the generic IO level to perform1674* channel-type-specific cleanup when a TCP socket based channel1675* is closed.1676*1677* Results:1678* 0 if successful, the value of errno if failed.1679*1680* Side effects:1681* Closes the socket of the channel.1682*1683*----------------------------------------------------------------------1684*/16851686/* ARGSUSED */1687static int1688TcpCloseProc(instanceData, interp)1689ClientData instanceData; /* The socket to close. */1690Tcl_Interp *interp; /* For error reporting - unused. */1691{1692TcpState *statePtr;1693Tcl_File sockFile;1694int sock;1695int errorCode = 0;16961697statePtr = (TcpState *) instanceData;1698sockFile = statePtr->sock;1699sock = (int) Tcl_GetFileInfo(sockFile, NULL);17001701/*1702* Delete a file handler that may be active for this socket if this1703* is a server socket - the file handler was created automatically1704* by Tcl as part of the mechanism to accept new client connections.1705* Channel handlers are already deleted in the generic IO channel1706* closing code that called this function, so we do not have to1707* delete them here.1708*/17091710Tcl_DeleteFileHandler(sockFile);17111712ckfree((char *) statePtr);17131714/*1715* We assume that inFile==outFile==sockFile and so1716* we only clean up sockFile.1717*/17181719Tcl_FreeFile(sockFile);17201721if (close(sock) < 0) {1722errorCode = errno;1723}17241725return errorCode;1726}17271728/*1729*----------------------------------------------------------------------1730*1731* TcpGetOptionProc --1732*1733* Computes an option value for a TCP socket based channel, or a1734* list of all options and their values.1735*1736* Note: This code is based on code contributed by John Haxby.1737*1738* Results:1739* A standard Tcl result. The value of the specified option or a1740* list of all options and their values is returned in the1741* supplied DString.1742*1743* Side effects:1744* None.1745*1746*----------------------------------------------------------------------1747*/17481749static int1750TcpGetOptionProc(instanceData, optionName, dsPtr)1751ClientData instanceData; /* Socket state. */1752char *optionName; /* Name of the option to1753* retrieve the value for, or1754* NULL to get all options and1755* their values. */1756Tcl_DString *dsPtr; /* Where to store the computed1757* value; initialized by caller. */1758{1759TcpState *statePtr;1760struct sockaddr_in sockname;1761struct sockaddr_in peername;1762struct hostent *hostEntPtr;1763int sock;1764size_t size = sizeof(struct sockaddr_in);1765size_t len = 0;1766char buf[128];17671768statePtr = (TcpState *) instanceData;1769sock = (int) Tcl_GetFileInfo(statePtr->sock, NULL);1770if (optionName != (char *) NULL) {1771len = strlen(optionName);1772}17731774if ((len == 0) ||1775((len > 1) && (optionName[1] == 'p') &&1776(strncmp(optionName, "-peername", len) == 0))) {1777if (getpeername(sock, (struct sockaddr *) &peername, &size) >= 0) {1778if (len == 0) {1779Tcl_DStringAppendElement(dsPtr, "-peername");1780Tcl_DStringStartSublist(dsPtr);1781}1782Tcl_DStringAppendElement(dsPtr, inet_ntoa(peername.sin_addr));1783hostEntPtr = gethostbyaddr((char *) &(peername.sin_addr),1784sizeof(peername.sin_addr), AF_INET);1785if (hostEntPtr != (struct hostent *) NULL) {1786Tcl_DStringAppendElement(dsPtr, hostEntPtr->h_name);1787} else {1788Tcl_DStringAppendElement(dsPtr, inet_ntoa(peername.sin_addr));1789}1790sprintf(buf, "%d", ntohs(peername.sin_port));1791Tcl_DStringAppendElement(dsPtr, buf);1792if (len == 0) {1793Tcl_DStringEndSublist(dsPtr);1794} else {1795return TCL_OK;1796}1797}1798}17991800if ((len == 0) ||1801((len > 1) && (optionName[1] == 's') &&1802(strncmp(optionName, "-sockname", len) == 0))) {1803if (getsockname(sock, (struct sockaddr *) &sockname, &size) >= 0) {1804if (len == 0) {1805Tcl_DStringAppendElement(dsPtr, "-sockname");1806Tcl_DStringStartSublist(dsPtr);1807}1808Tcl_DStringAppendElement(dsPtr, inet_ntoa(sockname.sin_addr));1809hostEntPtr = gethostbyaddr((char *) &(sockname.sin_addr),1810sizeof(peername.sin_addr), AF_INET);1811if (hostEntPtr != (struct hostent *) NULL) {1812Tcl_DStringAppendElement(dsPtr, hostEntPtr->h_name);1813} else {1814Tcl_DStringAppendElement(dsPtr, inet_ntoa(sockname.sin_addr));1815}1816sprintf(buf, "%d", ntohs(sockname.sin_port));1817Tcl_DStringAppendElement(dsPtr, buf);1818if (len == 0) {1819Tcl_DStringEndSublist(dsPtr);1820} else {1821return TCL_OK;1822}1823}1824}18251826if (len > 0) {1827Tcl_SetErrno(EINVAL);1828return TCL_ERROR;1829}18301831return TCL_OK;1832}18331834/*1835*----------------------------------------------------------------------1836*1837* TcpWatchProc --1838*1839* Initialize the notifier to watch Tcl_Files from this channel.1840*1841* Results:1842* None.1843*1844* Side effects:1845* Sets up the notifier so that a future event on the channel will1846* be seen by Tcl.1847*1848*----------------------------------------------------------------------1849*/18501851static void1852TcpWatchProc(instanceData, mask)1853ClientData instanceData; /* The socket state. */1854int mask; /* Events of interest; an OR-ed1855* combination of TCL_READABLE,1856* TCL_WRITABEL and TCL_EXCEPTION. */1857{1858TcpState *statePtr = (TcpState *) instanceData;18591860Tcl_WatchFile(statePtr->sock, mask);1861}18621863/*1864*----------------------------------------------------------------------1865*1866* TcpReadyProc --1867*1868* Called by the notifier to check whether events of interest are1869* present on the channel.1870*1871* Results:1872* Returns OR-ed combination of TCL_READABLE, TCL_WRITABLE and1873* TCL_EXCEPTION to indicate which events of interest are present.1874*1875* Side effects:1876* None.1877*1878*----------------------------------------------------------------------1879*/18801881static int1882TcpReadyProc(instanceData, mask)1883ClientData instanceData; /* The socket state. */1884int mask; /* Events of interest; an OR-ed1885* combination of TCL_READABLE,1886* TCL_WRITABLE and TCL_EXCEPTION. */1887{1888TcpState *statePtr = (TcpState *) instanceData;18891890return Tcl_FileReady(statePtr->sock, mask);1891}18921893/*1894*----------------------------------------------------------------------1895*1896* TcpGetProc --1897*1898* Called from Tcl_GetChannelFile to retrieve Tcl_Files from inside1899* a TCP socket based channel.1900*1901* Results:1902* The appropriate Tcl_File or NULL if not present.1903*1904* Side effects:1905* None.1906*1907*----------------------------------------------------------------------1908*/19091910/* ARGSUSED */1911static Tcl_File1912TcpGetProc(instanceData, direction)1913ClientData instanceData; /* The socket state. */1914int direction; /* Which Tcl_File to retrieve? */1915{1916TcpState *statePtr = (TcpState *) instanceData;19171918return statePtr->sock;1919}19201921/*1922*----------------------------------------------------------------------1923*1924* CreateSocket --1925*1926* This function opens a new socket in client or server mode1927* and initializes the TcpState structure.1928*1929* Results:1930* Returns a new TcpState, or NULL with an error in interp->result,1931* if interp is not NULL.1932*1933* Side effects:1934* Opens a socket.1935*1936*----------------------------------------------------------------------1937*/19381939static TcpState *1940CreateSocket(interp, port, host, server, myaddr, myport, async)1941Tcl_Interp *interp; /* For error reporting; can be NULL. */1942int port; /* Port number to open. */1943char *host; /* Name of host on which to open port.1944* NULL implies INADDR_ANY */1945int server; /* 1 if socket should be a server socket,1946* else 0 for a client socket. */1947char *myaddr; /* Optional client-side address */1948int myport; /* Optional client-side port */1949int async; /* If nonzero and creating a client socket,1950* attempt to do an async connect. Otherwise1951* do a synchronous connect or bind. */1952{1953int status, sock, asyncConnect, curState, origState;1954struct sockaddr_in sockaddr; /* socket address */1955struct sockaddr_in mysockaddr; /* Socket address for client */1956TcpState *statePtr;19571958sock = -1;1959origState = 0;1960if (! CreateSocketAddress(&sockaddr, host, port)) {1961goto addressError;1962}1963if ((myaddr != NULL || myport != 0) &&1964! CreateSocketAddress(&mysockaddr, myaddr, myport)) {1965goto addressError;1966}19671968sock = socket(AF_INET, SOCK_STREAM, 0);1969if (sock < 0) {1970goto addressError;1971}19721973/*1974* Set the close-on-exec flag so that the socket will not get1975* inherited by child processes.1976*/19771978fcntl(sock, F_SETFD, FD_CLOEXEC);19791980/*1981* Set kernel space buffering1982*/19831984TclSockMinimumBuffers(sock, SOCKET_BUFSIZE);19851986asyncConnect = 0;1987status = 0;1988if (server) {19891990/*1991* Set up to reuse server addresses automatically and bind to the1992* specified port.1993*/19941995status = 1;1996(void) setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *) &status,1997sizeof(status));1998status = bind(sock, (struct sockaddr *) &sockaddr,1999sizeof(struct sockaddr));2000if (status != -1) {2001status = listen(sock, SOMAXCONN);2002}2003} else {2004if (myaddr != NULL || myport != 0) {2005curState = 1;2006(void) setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,2007(char *) &curState, sizeof(curState));2008status = bind(sock, (struct sockaddr *) &mysockaddr,2009sizeof(struct sockaddr));2010if (status < 0) {2011goto bindError;2012}2013}20142015/*2016* Attempt to connect. The connect may fail at present with an2017* EINPROGRESS but at a later time it will complete. The caller2018* will set up a file handler on the socket if she is interested in2019* being informed when the connect completes.2020*/20212022if (async) {2023#ifndef USE_FIONBIO2024origState = fcntl(sock, F_GETFL, 0);2025curState = origState | O_NONBLOCK;2026status = fcntl(sock, F_SETFL, curState);2027#endif20282029#ifdef USE_FIONBIO2030curState = 1;2031status = ioctl(sock, FIONBIO, &curState);2032#endif2033} else {2034status = 0;2035}2036if (status > -1) {2037status = connect(sock, (struct sockaddr *) &sockaddr,2038sizeof(sockaddr));2039if (status < 0) {2040if (errno == EINPROGRESS) {2041asyncConnect = 1;2042status = 0;2043}2044}2045}2046}20472048bindError:2049if (status < 0) {2050if (interp != NULL) {2051Tcl_AppendResult(interp, "couldn't open socket: ",2052Tcl_PosixError(interp), (char *) NULL);2053}2054if (sock != -1) {2055close(sock);2056}2057return NULL;2058}20592060/*2061* Allocate a new TcpState for this socket.2062*/20632064statePtr = (TcpState *) ckalloc((unsigned) sizeof(TcpState));2065statePtr->flags = 0;2066if (asyncConnect) {2067statePtr->flags = TCP_ASYNC_CONNECT;2068}2069statePtr->sock = Tcl_GetFile((ClientData) sock, TCL_UNIX_FD);20702071return statePtr;20722073addressError:2074if (sock != -1) {2075close(sock);2076}2077if (interp != NULL) {2078Tcl_AppendResult(interp, "couldn't open socket: ",2079Tcl_PosixError(interp), (char *) NULL);2080}2081return NULL;2082}20832084/*2085*----------------------------------------------------------------------2086*2087* CreateSocketAddress --2088*2089* This function initializes a sockaddr structure for a host and port.2090*2091* Results:2092* 1 if the host was valid, 0 if the host could not be converted to2093* an IP address.2094*2095* Side effects:2096* Fills in the *sockaddrPtr structure.2097*2098*----------------------------------------------------------------------2099*/21002101static int2102CreateSocketAddress(sockaddrPtr, host, port)2103struct sockaddr_in *sockaddrPtr; /* Socket address */2104char *host; /* Host. NULL implies INADDR_ANY */2105int port; /* Port number */2106{2107struct hostent *hostent; /* Host database entry */2108struct in_addr addr; /* For 64/32 bit madness */21092110(void) memset((VOID *) sockaddrPtr, '\0', sizeof(struct sockaddr_in));2111sockaddrPtr->sin_family = AF_INET;2112sockaddrPtr->sin_port = htons((unsigned short) (port & 0xFFFF));2113if (host == NULL) {2114addr.s_addr = INADDR_ANY;2115} else {2116addr.s_addr = inet_addr(host);2117if (addr.s_addr == -1) {2118hostent = gethostbyname(host);2119if (hostent != NULL) {2120memcpy((VOID *) &addr,2121(VOID *) hostent->h_addr_list[0],2122(size_t) hostent->h_length);2123} else {2124#ifdef EHOSTUNREACH2125errno = EHOSTUNREACH;2126#else2127#ifdef ENXIO2128errno = ENXIO;2129#endif2130#endif2131return 0; /* error */2132}2133}2134}21352136/*2137* NOTE: On 64 bit machines the assignment below is rumored to not2138* do the right thing. Please report errors related to this if you2139* observe incorrect behavior on 64 bit machines such as DEC Alphas.2140* Should we modify this code to do an explicit memcpy?2141*/21422143sockaddrPtr->sin_addr.s_addr = addr.s_addr;2144return 1; /* Success. */2145}21462147/*2148*----------------------------------------------------------------------2149*2150* Tcl_OpenTcpClient --2151*2152* Opens a TCP client socket and creates a channel around it.2153*2154* Results:2155* The channel or NULL if failed. An error message is returned2156* in the interpreter on failure.2157*2158* Side effects:2159* Opens a client socket and creates a new channel.2160*2161*----------------------------------------------------------------------2162*/21632164Tcl_Channel2165Tcl_OpenTcpClient(interp, port, host, myaddr, myport, async)2166Tcl_Interp *interp; /* For error reporting; can be NULL. */2167int port; /* Port number to open. */2168char *host; /* Host on which to open port. */2169char *myaddr; /* Client-side address */2170int myport; /* Client-side port */2171int async; /* If nonzero, attempt to do an2172* asynchronous connect. Otherwise2173* we do a blocking connect. */2174{2175Tcl_Channel chan;2176TcpState *statePtr;2177char channelName[20];21782179/*2180* Create a new client socket and wrap it in a channel.2181*/21822183statePtr = CreateSocket(interp, port, host, 0, myaddr, myport, async);2184if (statePtr == NULL) {2185return NULL;2186}21872188statePtr->acceptProc = NULL;2189statePtr->acceptProcData = (ClientData) NULL;21902191sprintf(channelName, "sock%d",2192(int) Tcl_GetFileInfo(statePtr->sock, NULL));21932194chan = Tcl_CreateChannel(&tcpChannelType, channelName,2195(ClientData) statePtr, (TCL_READABLE | TCL_WRITABLE));2196if (Tcl_SetChannelOption(interp, chan, "-translation", "auto crlf") ==2197TCL_ERROR) {2198Tcl_Close((Tcl_Interp *) NULL, chan);2199return NULL;2200}2201return chan;2202}22032204/*2205*----------------------------------------------------------------------2206*2207* Tcl_MakeTcpClientChannel --2208*2209* Creates a Tcl_Channel from an existing client TCP socket.2210*2211* Results:2212* The Tcl_Channel wrapped around the preexisting TCP socket.2213*2214* Side effects:2215* None.2216*2217*----------------------------------------------------------------------2218*/22192220Tcl_Channel2221Tcl_MakeTcpClientChannel(sock)2222ClientData sock; /* The socket to wrap up into a channel. */2223{2224TcpState *statePtr;2225Tcl_File sockFile;2226char channelName[20];2227Tcl_Channel chan;22282229sockFile = Tcl_GetFile(sock, TCL_UNIX_FD);2230statePtr = (TcpState *) ckalloc((unsigned) sizeof(TcpState));2231statePtr->sock = sockFile;2232statePtr->acceptProc = NULL;2233statePtr->acceptProcData = (ClientData) NULL;22342235sprintf(channelName, "sock%d", (int) sock);22362237chan = Tcl_CreateChannel(&tcpChannelType, channelName,2238(ClientData) statePtr, (TCL_READABLE | TCL_WRITABLE));2239if (Tcl_SetChannelOption((Tcl_Interp *) NULL, chan, "-translation",2240"auto crlf") == TCL_ERROR) {2241Tcl_Close((Tcl_Interp *) NULL, chan);2242return NULL;2243}2244return chan;2245}22462247/*2248*----------------------------------------------------------------------2249*2250* Tcl_OpenTcpServer --2251*2252* Opens a TCP server socket and creates a channel around it.2253*2254* Results:2255* The channel or NULL if failed. If an error occurred, an2256* error message is left in interp->result if interp is2257* not NULL.2258*2259* Side effects:2260* Opens a server socket and creates a new channel.2261*2262*----------------------------------------------------------------------2263*/22642265Tcl_Channel2266Tcl_OpenTcpServer(interp, port, myHost, acceptProc, acceptProcData)2267Tcl_Interp *interp; /* For error reporting - may be2268* NULL. */2269int port; /* Port number to open. */2270char *myHost; /* Name of local host. */2271Tcl_TcpAcceptProc *acceptProc; /* Callback for accepting connections2272* from new clients. */2273ClientData acceptProcData; /* Data for the callback. */2274{2275Tcl_Channel chan;2276TcpState *statePtr;2277char channelName[20];22782279/*2280* Create a new client socket and wrap it in a channel.2281*/22822283statePtr = CreateSocket(interp, port, myHost, 1, NULL, 0, 0);2284if (statePtr == NULL) {2285return NULL;2286}22872288statePtr->acceptProc = acceptProc;2289statePtr->acceptProcData = acceptProcData;22902291/*2292* Set up the callback mechanism for accepting connections2293* from new clients.2294*/22952296Tcl_CreateFileHandler(statePtr->sock, TCL_READABLE, TcpAccept,2297(ClientData) statePtr);2298sprintf(channelName, "sock%d",2299(int) Tcl_GetFileInfo(statePtr->sock, NULL));2300chan = Tcl_CreateChannel(&tcpChannelType, channelName,2301(ClientData) statePtr, 0);2302return chan;2303}23042305/*2306*----------------------------------------------------------------------2307*2308* TcpAccept --2309* Accept a TCP socket connection. This is called by the event loop.2310*2311* Results:2312* None.2313*2314* Side effects:2315* Creates a new connection socket. Calls the registered callback2316* for the connection acceptance mechanism.2317*2318*----------------------------------------------------------------------2319*/23202321/* ARGSUSED */2322static void2323TcpAccept(data, mask)2324ClientData data; /* Callback token. */2325int mask; /* Not used. */2326{2327TcpState *sockState; /* Client data of server socket. */2328int newsock; /* The new client socket */2329Tcl_File newFile; /* Its file. */2330TcpState *newSockState; /* State for new socket. */2331struct sockaddr_in addr; /* The remote address */2332int len; /* For accept interface */2333Tcl_Channel chan; /* Channel instance created. */2334char channelName[20];23352336sockState = (TcpState *) data;23372338len = sizeof(struct sockaddr_in);2339newsock = accept((int) Tcl_GetFileInfo(sockState->sock, NULL),2340(struct sockaddr *)&addr, &len);2341if (newsock < 0) {2342return;2343}23442345/*2346* Set close-on-exec flag to prevent the newly accepted socket from2347* being inherited by child processes.2348*/23492350(void) fcntl(newsock, F_SETFD, FD_CLOEXEC);23512352newFile = Tcl_GetFile((ClientData) newsock, TCL_UNIX_FD);2353if (newFile) {2354newSockState = (TcpState *) ckalloc((unsigned) sizeof(TcpState));23552356newSockState->flags = 0;2357newSockState->sock = newFile;2358newSockState->acceptProc = (Tcl_TcpAcceptProc *) NULL;2359newSockState->acceptProcData = (ClientData) NULL;23602361sprintf(channelName, "sock%d", (int) newsock);2362chan = Tcl_CreateChannel(&tcpChannelType, channelName,2363(ClientData) newSockState, (TCL_READABLE | TCL_WRITABLE));2364if (chan == (Tcl_Channel) NULL) {2365ckfree((char *) newSockState);2366close(newsock);2367Tcl_FreeFile(newFile);2368} else {2369if (Tcl_SetChannelOption((Tcl_Interp *) NULL, chan, "-translation",2370"auto crlf") == TCL_ERROR) {2371Tcl_Close((Tcl_Interp *) NULL, chan);2372}2373if (sockState->acceptProc != (Tcl_TcpAcceptProc *) NULL) {2374(sockState->acceptProc) (sockState->acceptProcData, chan,2375inet_ntoa(addr.sin_addr), ntohs(addr.sin_port));2376}2377}2378}2379}2380#endif23812382/*2383*----------------------------------------------------------------------2384*2385* TclGetDefaultStdChannel --2386*2387* Creates channels for standard input, standard output or standard2388* error output if they do not already exist.2389*2390* Results:2391* Returns the specified default standard channel, or NULL.2392*2393* Side effects:2394* May cause the creation of a standard channel and the underlying2395* file.2396*2397*----------------------------------------------------------------------2398*/23992400Tcl_Channel2401TclGetDefaultStdChannel(type)2402int type; /* One of TCL_STDIN, TCL_STDOUT, TCL_STDERR. */2403{2404Tcl_Channel channel = NULL;2405int fd = 0; /* Initializations needed to prevent */2406int mode = 0; /* compiler warning (used before set). */2407char *bufMode = NULL;24082409switch (type) {2410case TCL_STDIN:2411if ((lseek(0, (off_t) 0, SEEK_CUR) == -1) &&2412(errno == EBADF)) {2413return (Tcl_Channel) NULL;2414}2415fd = 0;2416mode = TCL_READABLE;2417bufMode = "line";2418break;2419case TCL_STDOUT:2420if ((lseek(1, (off_t) 0, SEEK_CUR) == -1) &&2421(errno == EBADF)) {2422return (Tcl_Channel) NULL;2423}2424fd = 1;2425mode = TCL_WRITABLE;2426bufMode = "line";2427break;2428case TCL_STDERR:2429if ((lseek(2, (off_t) 0, SEEK_CUR) == -1) &&2430(errno == EBADF)) {2431return (Tcl_Channel) NULL;2432}2433fd = 2;2434mode = TCL_WRITABLE;2435bufMode = "none";2436break;2437default:2438panic("TclGetDefaultStdChannel: Unexpected channel type");2439break;2440}24412442channel = Tcl_MakeFileChannel((ClientData) fd, (ClientData) fd, mode);24432444/*2445* Set up the normal channel options for stdio handles.2446*/24472448if (Tcl_SetChannelOption(NULL, channel, "-translation", "auto") ==2449TCL_ERROR) {2450Tcl_Close((Tcl_Interp *) NULL, channel);2451return NULL;2452}2453if (Tcl_SetChannelOption(NULL, channel, "-buffering", bufMode) ==2454TCL_ERROR) {2455Tcl_Close((Tcl_Interp *) NULL, channel);2456return NULL;2457}2458return channel;2459}24602461/*2462*----------------------------------------------------------------------2463*2464* TclClosePipeFile --2465*2466* This function is a simple wrapper for close on a file or2467* pipe handle. Called in the generic command pipeline cleanup2468* code to do platform specific closing of the files associated2469* with the command channel.2470*2471* Results:2472* None.2473*2474* Side effects:2475* Closes the fd and frees the Tcl_File.2476*2477*----------------------------------------------------------------------2478*/24792480void2481TclClosePipeFile(file)2482Tcl_File file;2483{2484int fd = (int) Tcl_GetFileInfo(file, NULL);2485close(fd);2486Tcl_FreeFile(file);2487}24882489/*2490*----------------------------------------------------------------------2491*2492* Tcl_GetOpenFile --2493*2494* Given a name of a channel registered in the given interpreter,2495* returns a FILE * for it.2496*2497* Results:2498* A standard Tcl result. If the channel is registered in the given2499* interpreter and it is managed by the "file" channel driver, and2500* it is open for the requested mode, then the output parameter2501* filePtr is set to a FILE * for the underlying file. On error, the2502* filePtr is not set, TCL_ERROR is returned and an error message is2503* left in interp->result.2504*2505* Side effects:2506* May invoke fdopen to create the FILE * for the requested file.2507*2508*----------------------------------------------------------------------2509*/25102511int2512Tcl_GetOpenFile(interp, string, forWriting, checkUsage, filePtr)2513Tcl_Interp *interp; /* Interpreter in which to find file. */2514char *string; /* String that identifies file. */2515int forWriting; /* 1 means the file is going to be used2516* for writing, 0 means for reading. */2517int checkUsage; /* 1 means verify that the file was opened2518* in a mode that allows the access specified2519* by "forWriting". Ignored, we always2520* check that the channel is open for the2521* requested mode. */2522ClientData *filePtr; /* Store pointer to FILE structure here. */2523{2524Tcl_Channel chan;2525int chanMode;2526Tcl_ChannelType *chanTypePtr;2527Tcl_File tf;2528int fd;2529FILE *f;25302531chan = Tcl_GetChannel(interp, string, &chanMode);2532if (chan == (Tcl_Channel) NULL) {2533return TCL_ERROR;2534}2535if ((forWriting) && ((chanMode & TCL_WRITABLE) == 0)) {2536Tcl_AppendResult(interp,2537"\"", string, "\" wasn't opened for writing", (char *) NULL);2538return TCL_ERROR;2539} else if ((!(forWriting)) && ((chanMode & TCL_READABLE) == 0)) {2540Tcl_AppendResult(interp,2541"\"", string, "\" wasn't opened for reading", (char *) NULL);2542return TCL_ERROR;2543}25442545/*2546* We allow creating a FILE * out of file based, pipe based and socket2547* based channels. We currently do not allow any other channel types,2548* because it is likely that stdio will not know what to do with them.2549*/25502551chanTypePtr = Tcl_GetChannelType(chan);2552if ((chanTypePtr == &fileChannelType) || (chanTypePtr == &pipeChannelType)2553|| (chanTypePtr == &tcpChannelType)) {2554tf = Tcl_GetChannelFile(chan,2555(forWriting ? TCL_WRITABLE : TCL_READABLE));2556fd = (int) Tcl_GetFileInfo(tf, NULL);25572558/*2559* The call to fdopen below is probably dangerous, since it will2560* truncate an existing file if the file is being opened2561* for writing....2562*/25632564f = fdopen(fd, (forWriting ? "w" : "r"));2565if (f == NULL) {2566Tcl_AppendResult(interp, "cannot get a FILE * for \"", string,2567"\"", (char *) NULL);2568return TCL_ERROR;2569}2570*filePtr = (ClientData) f;2571return TCL_OK;2572}25732574Tcl_AppendResult(interp, "\"", string,2575"\" cannot be used to get a FILE * - unsupported type",2576(char *) NULL);2577return TCL_ERROR;2578}257925802581