Possible bug in SunOS Release 4 Bourne Shell?

Ray Essick essick at prisma.com
Tue Sep 26 00:10:13 AEST 1989


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 at prisma.com



More information about the Comp.sys.sun mailing list