From 38aabeeca86a4a08cdf5def1c47588c02aafcf9c Mon Sep 17 00:00:00 2001 From: "jan.nijtmans" Date: Thu, 5 Mar 2020 11:32:43 +0000 Subject: Move setting of interpreter error-message from tclUnixSock.c to tclIO.c, since the same should be done for all channel types. (Thanks, Don, for noticing this!) Also, ENOTCONN for a half-close should not be fatal, just a EINVAL, when doing it as part of a full close. See: [65c9cd1534] --- generic/tclIO.c | 33 +++++++++++++---------------- unix/tclUnixSock.c | 61 +++++++++++++++++++++++++++--------------------------- 2 files changed, 44 insertions(+), 50 deletions(-) diff --git a/generic/tclIO.c b/generic/tclIO.c index 36f0a8e..67264a0 100644 --- a/generic/tclIO.c +++ b/generic/tclIO.c @@ -365,15 +365,6 @@ ChanClose( } } -static inline int -ChanCloseHalf( - Channel *chanPtr, - Tcl_Interp *interp, - int flags) -{ - return chanPtr->typePtr->close2Proc(chanPtr->instanceData, interp, flags); -} - /* *--------------------------------------------------------------------------- * @@ -3466,7 +3457,7 @@ Tcl_Close( if (chanPtr->typePtr->closeProc == TCL_CLOSE2PROC) { result = chanPtr->typePtr->close2Proc(chanPtr->instanceData, interp, TCL_CLOSE_READ); - if (result == EINVAL) { + if ((result == EINVAL) || result == ENOTCONN) { result = 0; } } @@ -3512,13 +3503,17 @@ Tcl_Close( * message set up to now. */ - if (flushcode != 0 && interp != NULL + if (flushcode != 0) { + /* flushcode has precedence, if available */ + result = flushcode; + } + if ((result != 0) && (result != TCL_ERROR) && (interp != NULL) && 0 == Tcl_GetCharLength(Tcl_GetObjResult(interp))) { - Tcl_SetErrno(flushcode); + Tcl_SetErrno(result); Tcl_SetObjResult(interp, Tcl_NewStringObj(Tcl_PosixError(interp), -1)); } - if ((flushcode != 0) || (result != 0)) { + if (result != 0) { return TCL_ERROR; } return TCL_OK; @@ -3816,7 +3811,7 @@ CloseChannelPart( * message in the interp. */ - result = ChanCloseHalf(chanPtr, interp, flags); + result = chanPtr->typePtr->close2Proc(chanPtr->instanceData, NULL, flags); /* * If we are being called synchronously, report either any latent error on @@ -4220,12 +4215,12 @@ WillRead( * Prevent read attempts on a closed channel. */ - DiscardInputQueued(chanPtr->state, 0); + DiscardInputQueued(chanPtr->state, 0); Tcl_SetErrno(EINVAL); return -1; } if ((Tcl_ChannelSeekProc(chanPtr->typePtr) != NULL) - && (Tcl_OutputBuffered((Tcl_Channel) chanPtr) > 0)) { + && (Tcl_OutputBuffered((Tcl_Channel) chanPtr) > 0)) { /* * CAVEAT - The assumption here is that FlushChannel() will push out @@ -4237,9 +4232,9 @@ WillRead( * blocking mode. */ - if (FlushChannel(NULL, chanPtr, 0) != 0) { - return -1; - } + if (FlushChannel(NULL, chanPtr, 0) != 0) { + return -1; + } } return 0; } diff --git a/unix/tclUnixSock.c b/unix/tclUnixSock.c index 0dd8e86..1901c8b 100644 --- a/unix/tclUnixSock.c +++ b/unix/tclUnixSock.c @@ -228,9 +228,9 @@ InitializeHostName( char *dot = strchr(u.nodename, '.'); if (dot != NULL) { - char *node = ckalloc(dot - u.nodename + 1); + char *node = (char *)ckalloc(dot - u.nodename + 1); - memcpy(node, u.nodename, (size_t) (dot - u.nodename)); + memcpy(node, u.nodename, dot - u.nodename); node[dot - u.nodename] = '\0'; hp = TclpGetHostByName(node); ckfree(node); @@ -320,8 +320,10 @@ Tcl_GetHostName(void) int TclpHasSockets( - Tcl_Interp *interp) /* Not used. */ + Tcl_Interp *dummy) /* Not used. */ { + (void)dummy; + return TCL_OK; } @@ -372,7 +374,7 @@ TcpBlockModeProc( * TCL_MODE_BLOCKING or * TCL_MODE_NONBLOCKING. */ { - TcpState *statePtr = instanceData; + TcpState *statePtr = (TcpState *)instanceData; if (mode == TCL_MODE_BLOCKING) { CLEAR_BITS(statePtr->flags, TCP_NONBLOCKING); @@ -505,7 +507,7 @@ TcpInputProc( * buffer? */ int *errorCodePtr) /* Where to store error code. */ { - TcpState *statePtr = instanceData; + TcpState *statePtr = (TcpState *)instanceData; int bytesRead; *errorCodePtr = 0; @@ -555,7 +557,7 @@ TcpOutputProc( int toWrite, /* How many bytes to write? */ int *errorCodePtr) /* Where to store error code. */ { - TcpState *statePtr = instanceData; + TcpState *statePtr = (TcpState *)instanceData; int written; *errorCodePtr = 0; @@ -593,11 +595,12 @@ TcpOutputProc( static int TcpCloseProc( ClientData instanceData, /* The socket to close. */ - Tcl_Interp *interp) /* For error reporting - unused. */ + Tcl_Interp *dummy) /* For error reporting - unused. */ { - TcpState *statePtr = instanceData; + TcpState *statePtr = (TcpState *)instanceData; int errorCode = 0; TcpFdList *fds; + (void)dummy; /* * Delete a file handler that may be active for this socket if this is a @@ -631,9 +634,6 @@ TcpCloseProc( freeaddrinfo(statePtr->myaddrlist); } ckfree(statePtr); - if (interp && errorCode) { - Tcl_SetResult(interp, (char *)Tcl_PosixError(interp), TCL_STATIC); - } return errorCode; } @@ -657,19 +657,19 @@ TcpCloseProc( static int TcpClose2Proc( ClientData instanceData, /* The socket to close. */ - Tcl_Interp *interp, /* For error reporting. */ + Tcl_Interp *dummy, /* For error reporting. */ int flags) /* Flags that indicate which side to close. */ { - TcpState *statePtr = instanceData; + TcpState *statePtr = (TcpState *)instanceData; int readError = 0; int writeError = 0; - int errorCode = 0; + (void)dummy; /* * Shutdown the OS socket handle. */ if ((flags & (TCL_CLOSE_READ|TCL_CLOSE_WRITE)) == 0) { - return TcpCloseProc(instanceData, interp); + return TcpCloseProc(instanceData, NULL); } if ((flags & TCL_CLOSE_READ) && (shutdown(statePtr->fds.fd, SHUT_RD) < 0)) { readError = errno; @@ -677,12 +677,7 @@ TcpClose2Proc( if ((flags & TCL_CLOSE_WRITE) && (shutdown(statePtr->fds.fd, SHUT_WR) < 0)) { writeError = errno; } - - errorCode = (readError != 0) ? readError : writeError; - if (interp && errorCode) { - Tcl_SetResult(interp, (char *)Tcl_PosixError(interp), TCL_STATIC); - } - return errorCode; + return (readError != 0) ? readError : writeError; } /* @@ -821,7 +816,7 @@ TcpGetOptionProc( Tcl_DString *dsPtr) /* Where to store the computed value; * initialized by caller. */ { - TcpState *statePtr = instanceData; + TcpState *statePtr = (TcpState *)instanceData; size_t len = 0; WaitForConnect(statePtr, NULL); @@ -1013,7 +1008,7 @@ TcpWatchProc( * TCL_READABLE, TCL_WRITABLE and * TCL_EXCEPTION. */ { - TcpState *statePtr = instanceData; + TcpState *statePtr = (TcpState *)instanceData; if (statePtr->acceptProc != NULL) { /* @@ -1086,7 +1081,8 @@ TcpGetHandleProc( int direction, /* Not used. */ ClientData *handlePtr) /* Where to store the handle. */ { - TcpState *statePtr = instanceData; + TcpState *statePtr = (TcpState *)instanceData; + (void)direction; *handlePtr = INT2PTR(statePtr->fds.fd); return TCL_OK; @@ -1111,7 +1107,9 @@ TcpAsyncCallback( * TCL_READABLE, TCL_WRITABLE and * TCL_EXCEPTION. */ { - TcpConnect(NULL, clientData); + (void)mask; + + TcpConnect(NULL, (TcpState *)clientData); } /* @@ -1370,7 +1368,7 @@ Tcl_OpenTcpClient( * Allocate a new TcpState for this socket. */ - statePtr = ckalloc(sizeof(TcpState)); + statePtr = (TcpState *)ckalloc(sizeof(TcpState)); memset(statePtr, 0, sizeof(TcpState)); statePtr->flags = async ? TCP_ASYNC_CONNECT : 0; statePtr->cachedBlocking = TCL_MODE_BLOCKING; @@ -1449,7 +1447,7 @@ TclpMakeTcpClientChannelMode( TcpState *statePtr; char channelName[SOCK_CHAN_LENGTH]; - statePtr = ckalloc(sizeof(TcpState)); + statePtr = (TcpState *)ckalloc(sizeof(TcpState)); memset(statePtr, 0, sizeof(TcpState)); statePtr->fds.fd = PTR2INT(sock); statePtr->flags = 0; @@ -1609,14 +1607,14 @@ Tcl_OpenTcpServer( * Allocate a new TcpState for this socket. */ - statePtr = ckalloc(sizeof(TcpState)); + statePtr = (TcpState *)ckalloc(sizeof(TcpState)); memset(statePtr, 0, sizeof(TcpState)); statePtr->acceptProc = acceptProc; statePtr->acceptProcData = acceptProcData; sprintf(channelName, SOCK_TEMPLATE, (long) statePtr); newfds = &statePtr->fds; } else { - newfds = ckalloc(sizeof(TcpFdList)); + newfds = (TcpFdList *)ckalloc(sizeof(TcpFdList)); memset(newfds, (int) 0, sizeof(TcpFdList)); fds->next = newfds; } @@ -1680,13 +1678,14 @@ TcpAccept( ClientData data, /* Callback token. */ int mask) /* Not used. */ { - TcpFdList *fds = data; /* Client data of server socket. */ + TcpFdList *fds = (TcpFdList *)data; /* Client data of server socket. */ int newsock; /* The new client socket */ TcpState *newSockState; /* State for new socket. */ address addr; /* The remote address */ socklen_t len; /* For accept interface */ char channelName[SOCK_CHAN_LENGTH]; char host[NI_MAXHOST], port[NI_MAXSERV]; + (void)mask; len = sizeof(addr); newsock = accept(fds->fd, &addr.sa, &len); @@ -1701,7 +1700,7 @@ TcpAccept( (void) fcntl(newsock, F_SETFD, FD_CLOEXEC); - newSockState = ckalloc(sizeof(TcpState)); + newSockState = (TcpState *)ckalloc(sizeof(TcpState)); memset(newSockState, 0, sizeof(TcpState)); newSockState->flags = 0; newSockState->fds.fd = newsock; -- cgit v0.12