Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2003-10-24
01:03 • Closed ticket [813087ffff]: tclsh treats Unix pipelines as sockets plus 7 ot... artifact: d58b6361af user: andreas_kupries
2003-10-03
20:37 • Ticket [813087ffff]: 4 changes artifact: 7a41b5cb8d user: cc_benny
07:35 • Ticket [813087ffff]: 4 changes artifact: 251d442528 user: jenglish
2003-09-27
23:31 • Ticket [813087ffff]: 4 changes artifact: a6f3573900 user: cc_benny
22:50 • Ticket [813087ffff]: 4 changes artifact: d38dc3a95a user: cc_benny
2003-09-26
21:58 • Ticket [813087ffff]: 4 changes artifact: 356f679d16 user: cc_benny
14:12 • New ticket [813087ffff]. artifact: d6638170b7 user: dmacks

tclsh treats Unix pipelines as sockets

Closed / Fixed813087ffffffffffffffffffffffffffffffffffCreated 23 years agoUpdated 23 years ago
Descriptionplain2003-09-26 14:12:13
tcltk-8.4.1 compiled from the unix/ directory on OS X
10.2.6.
tcltk-8.3.4 that comes precompiled with my OS X is not
affected.

When tclsh is used in a pipeline, stdout is treated
like a socket. This is bad because it defaults to CR/LF
line endings, not the LF that is expected and
appropriate for a Unix platform when parsing piped
commands. Simple Unix file descriptor redirection does
not have this problem.

Here are three examples that look correct:

% tclsh
% fconfigure stdout
-blocking 1 -buffering line -buffersize 4096 -encoding
utf-8 -eofchar {} -translation crlf -mode 9600,n,8,1
-xchar {p r}
[that {p r} is actually untypable characters)

% echo "puts [fconfigure stdout]" | tclsh
-blocking 1 -buffering line -buffersize 4096 -encoding
utf-8 -eofchar {} -translation crlf -mode 9600,n,8,1
-xchar {p r}

% echo "puts [fconfigure stdout]" | tclsh > foo ; cat
-vte foo
-blocking 1 -buffering line -buffersize 4096 -encoding
utf-8 -eofchar {} -translation lf$

So simple output and redirected output and piped input
are okay, but if output is piped:

% echo "puts [fconfigure stdout]" | tclsh | cat -vte 
-blocking 1 -buffering line -buffersize 4096 -encoding
utf-8 -eofchar {{} {}} -translation {auto crlf}
-peername {0.0.0.0 0.0.0.0 0} -sockname {0.0.0.0
0.0.0.0 0}^M$
cc_bennyplain2003-09-26 21:58:47
Logged In: YES 
user_id=143885

Preliminary points of analysis:

- getsockopt() is called in tclUnixChan.c:Tcl_MakeFileChannel()
to determine if a file handle is a socket.  For anonymous pipes
this returns -1/ENOTSOCK on Linux as expected, but on Mac OS
X (10.3.6) it returns 0, so the pipe is handled as a socket.

- This happens in 8.4.4. In 8.3.4 getsockopt() is not called
at all according to ktrace/strace. I haven't investigated the
8.3 source yet, where the actual difference are here.
cc_bennyplain2003-09-27 22:50:17
Logged In: YES 
user_id=143885

More points:

- Darwin/Mac OS X implements pipes as sockets internally. This
is also on NetBSD.

- The call to getsockopt() was added to
tclUnixChan.c:Tcl_MakeFileChannel() in version 1.20 of the
file by
dkf (Donal K. Fellows) in response to bug #219137.

See posting on a NetBSD list
<http://groups.google.com/groups?selm=20010327154747.A13991%40vaasje.org>
for alternative code to detect socket vs. other file types,
based on getsockname().  That code works here on Mac OS
X and could be used in Tcl_MakeFileChannel() instead of (or in
addition to) getsockopt(SO_TYPE).
cc_bennyplain2003-09-27 23:31:47
Logged In: YES 
user_id=143885

See patch #813606.
jenglishplain2003-10-03 07:35:02
Logged In: YES 
user_id=68433

From the patch:
!     if (getsockname(fd, (struct sockaddr *)&sockaddr,
&sockaddrLen) == 0
!             && sockaddrLen > 0
!             && sockaddr.ss_family != AF_UNSPEC
!             && sockaddr.ss_family != AF_LOCAL) {
!         return MakeTcpClientChannelMode((ClientData) fd,
mode);

A better test would be:

if (sockaddrLen > 0 && sockaddr.ss_family == AF_INET) { ...
}
cc_bennyplain2003-10-03 20:37:28
Logged In: YES 
user_id=143885

>if (sockaddrLen > 0 && sockaddr.ss_family == AF_INET)

Don't forgot the call to getsockname().

AF_INET limits this function to IP.  But the rest of the code
is limited to IP anyway, so you are right.
andreas_kupriesplain2003-10-24 01:03:19
Logged In: YES 
user_id=75003

The referenced patch was accepted with small changes (see 
patch page) and committed to both head and core-8-4-
branch. This fixes the bug.
Log in (anonymous is fine) to add a comment.