[comp.sys.sun] Possible bug in SunOS Release 4 Bourne Shell?

keith%tira@gargoyle.uchicago.edu (Keith Waclena) (08/05/89)

Hardware: Sun 4/110, Sun 3/50
OS:	  SunOS 4.0, SunOS 4.0.1

The following shell script gives different results when invoked with
standard input redirected from a file, compared to when the same file is
piped into it via cat.

    $ cat testread.sh
    #!/bin/sh

    read FIRSTLINE
    read SECONDLINE
    (echo "$FIRSTLINE"; echo "$SECONDLINE"; cat)
    $ cat FOO
    AAA
    BBB
    CCC
    $ cat FOO | /bin/sh testread.sh
    AAA
    BBB
    CCC
    $ /bin/sh testread.sh <FOO
    AAA
    BBB
    AAA
    BBB
    CCC
    $

(This problem showed up in a realistic context; testread.sh is the
smallest script I can write that illustrates the problem.)

The script works (i.e., produces the same output regardless of whether the
input comes from redirection or from a pipe) under the following standard
Bourne shells: on a Sun 3/50 running SunOS 3.4, on an Elxsi running their
version of Unix, and on an AT&T 3B15 running System V r2.

Is this a known bug?  Does it exist in the latest version of SunOS?
(We're upgrading soon.) 

Thanks for the info,

						Keith

--
Keith WACLENA                             keith@curry.uchicago.edu
GLS / TIRA / U of Chicago                 keith%curry@uchimvs1.bitnet
1100 E.57th.St Chi IL 60637 USA           ...!uunet!curry.uchicago.edu!keith

gorpong@uunet.uu.net (Gordon C. Galligher) (08/29/89)

In article <763@brazos.Rice.edu> keith%tira@gargoyle.uchicago.edu (Keith Waclena) writes:
>X-Sun-Spots-Digest: Volume 8, Issue 93, message 13 of 16
>
>Hardware: Sun 4/110, Sun 3/50
>OS:	  SunOS 4.0, SunOS 4.0.1
>
>The following shell script gives different results when invoked with
>standard input redirected from a file, compared to when the same file is
>piped into it via cat.
[text of script deleted]
>
>Is this a known bug?  Does it exist in the latest version of SunOS?
>(We're upgrading soon.) 

We currently have SunOS 4.0.3 on a Sun 4/280 and it still exists in that
version.  Are you listening SUN?

		-- Gordon.

Gordon C. Galligher  <|> ...!uunet!telxon!gorpong <|> gorpong@telxon.uucp.uu.net
Telxon Corporation   <|> "What are ya standin' around for?  Don't ya know
Akron, Ohio, 44313   <|>  a Jail Break when ya see one?" - Scotty
(216) 867-3700 (3512)<|>         Star Trek V:  The Final Frontier

essick@prisma.com (Ray Essick) (09/25/89)

Keith Waclena shows a simple shell script that surprisingly changes
behavior depending on whether its input is a pipe or a file.  The alleged
culprit is the Bourne shell under SunOS 4.x

We recreated the same situation here at Prisma and found that the problem
is NOT in the Bourne shell. Instead, the bad program is /bin/cat.

SunOS 4 /bin/cat knows about mmap() and tries to use a mmap/write cycle
instead of a read/write cycle. This saves a buffer copy from the kernel to
user space; so it does make sense.

The bad news is that the code that says "can we use mmap()" within
/bin/cat assumes that the entire file is to be copied.  It starts the
mmap() at offset 0.

In Keith's script, the shell hands cat a perfectly positioned file
descriptor and, in essence, cat blindly does an lseek(,0,0) when it uses
mmap().

Our fix was to modify /bin/cat's test so that it checked the file position
and only uses mmap() if the file was positioned on a page boundary. A more
complete solution could have done the appropriate mmap() and offset into
the page, but we felt that it wasn't worth the effort. Our fix reverts to
the read/write loop when things aren't perfectly aligned.

-- Ray Essick, essick@prisma.com