All of lore.kernel.org
 help / color / mirror / Atom feed
* Re: Bug in open() function (?)
@ 2003-07-17  7:54 Michael Kerrisk
  2003-07-17 10:54 ` Andries Brouwer
  0 siblings, 1 reply; 10+ messages in thread
From: Michael Kerrisk @ 2003-07-17  7:54 UTC (permalink / raw)
  To: aebr; +Cc: linux-kernel

> > On Fri, 11 Jul 2003 22:23:00 PDT, Andrew Morton said:

> >

> > > We've lived with it for this long.

> >

> > Well... you have a point there..

> >

> > > Given that the behaviour is undefined, the behaviour which we should

> > > implement is clearly "whatever 2.4 is doing".  So let's leave it
alone.

> >

> > I suppose I could live with that *IF* somebody fixes 'man 2 open' to

> > reflect reality.

>

> Corrections and additions to manpages are always welcome.

> Mail to aeb@cwi.nl .

>

>

> (Concerning the topic under discussion, the man page says

>

>        O_TRUNC

>               If  the  file  already exists and is a regular file

>               and the open mode allows writing (i.e.,  is  O_RDWR

>               or  O_WRONLY) it will be truncated to length 0.  If

>               the file is a FIFO or  terminal  device  file,  the

>               O_TRUNC  flag  is  ignored. Otherwise the effect of

>               O_TRUNC is unspecified.

>

> which is precisely right. It continues

>

>                                        (On many Linux versions it

>               will  be  ignored; on other versions it will return

>               an error.)

>

> where someone may read this as if this is an exhaustive list of

> possibilities. So adding ", or actually do the truncate" will

> clarify.)

>

>

> Concerning the desired behaviour: if I recall things correctly

> doing the truncate was old SunOS behaviour, not doing it,

> that is, honouring the O_RDONLY, is new Solaris behaviour.

> Maybe someone with access to such machines can check.

>

> Software exists that does O_RDONLY | O_TRUNC.



A late addition to this thread, but all of these systems DO truncate with

O_RDONLY | O_TRUNC:



Solaris 8

Tru64 5.1B

HP-UX 11.22

FreeBSD 4.7



Although this flag combination is left unspecified by SUSv3, I don't

know of an implementation that DOESN'T truncate in these circumstances.



Cheers



Michael


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Bug in open() function (?)
  2003-07-17  7:54 Bug in open() function (?) Michael Kerrisk
@ 2003-07-17 10:54 ` Andries Brouwer
  0 siblings, 0 replies; 10+ messages in thread
From: Andries Brouwer @ 2003-07-17 10:54 UTC (permalink / raw)
  To: Michael Kerrisk; +Cc: aebr, linux-kernel

> >        O_TRUNC
> >               If  the  file  already exists and is a regular file
> >               and the open mode allows writing (i.e.,  is  O_RDWR
> >               or  O_WRONLY) it will be truncated to length 0.  If
> >               the file is a FIFO or  terminal  device  file,  the
> >               O_TRUNC  flag  is  ignored. Otherwise the effect of
> >               O_TRUNC is unspecified.
> 
> A late addition to this thread, but all of these systems DO truncate with
> 
> O_RDONLY | O_TRUNC:
> 
> Solaris 8
> Tru64 5.1B
> HP-UX 11.22
> FreeBSD 4.7
> 
> Although this flag combination is left unspecified by SUSv3, I don't
> know of an implementation that DOESN'T truncate in these circumstances.

Yes, when we talked about it I checked a few Linux versions, Solaris 5.7, 5.8,
and Irix 6.5 and they all truncate.

So, the standard says "unspecified" but the industry consensus is clearly
"truncate", never mind the O_RDONLY.

Probably the "unspecified" is there for a good reason, so people should be
able to find systems that do not truncate, but there is no reason at all
for Linux to change behaviour.


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Bug in open() function (?)
  2003-07-12  9:37               ` Andries Brouwer
@ 2003-07-12 12:04                 ` jw schultz
  0 siblings, 0 replies; 10+ messages in thread
From: jw schultz @ 2003-07-12 12:04 UTC (permalink / raw)
  To: linux-kernel

On Sat, Jul 12, 2003 at 11:37:08AM +0200, Andries Brouwer wrote:
> On Sat, Jul 12, 2003 at 02:14:43AM -0400, Valdis.Kletnieks@vt.edu wrote:
> 
> > On Fri, 11 Jul 2003 22:23:00 PDT, Andrew Morton said:
> > 
> > > We've lived with it for this long.
> > 
> > Well... you have a point there..
> > 
> > > Given that the behaviour is undefined, the behaviour which we should
> > > implement is clearly "whatever 2.4 is doing".  So let's leave it alone.
> > 
> > I suppose I could live with that *IF* somebody fixes 'man 2 open' to
> > reflect reality.
> 
> Corrections and additions to manpages are always welcome.
> Mail to aeb@cwi.nl .
> 
> 
> (Concerning the topic under discussion, the man page says
> 
>        O_TRUNC
>               If  the  file  already exists and is a regular file
>               and the open mode allows writing (i.e.,  is  O_RDWR
>               or  O_WRONLY) it will be truncated to length 0.  If
>               the file is a FIFO or  terminal  device  file,  the
>               O_TRUNC  flag  is  ignored. Otherwise the effect of
>               O_TRUNC is unspecified.
> 
> which is precisely right. It continues
> 
>                                        (On many Linux versions it
>               will  be  ignored; on other versions it will return
>               an error.)
> 
> where someone may read this as if this is an exhaustive list of
> possibilities. So adding ", or actually do the truncate" will
> clarify.)

I'd be inclined to at least drop the parenthetic, it only
confuses things.  The alternative would be to tie the
parenthetic to the FIFO and device files.

I'll grant that O_RDONLY would cause one to expect that the
file would not be modified in any way so truncating it on a
read-only seems wrong but that does fall under the
definition of undefined so is not contrary to the
documentation.

Anyone depending on undefined behaviour is asking for
trouble.  Given that there is code floating around expecting
O_TRUNC|O_RDONLY to truncate, caution should be applied in
changing this.

I'd suggest replacing this text to match that of SUSv3 which
is much clearer.  Perhaps with the addition of a clause
stating "The use of O_TRUNC in combination with O_RDONLY to
truncate files is deprecated" or something to that effect.

SUSv3:
|	O_TRUNC
|		If the file exists and is a regular file,
|		and the file is successfully opened O_RDWR
|		or O_WRONLY, its length shall be truncated
|		to 0, and the mode and owner shall be
|		unchanged. It shall have no effect on FIFO
|		special files or terminal device files. Its
|		effect on other file types is
|		implementation-defined. The result of using
|		O_TRUNC with O_RDONLY is undefined.

-- 
________________________________________________________________
	J.W. Schultz            Pegasystems Technologies
	email address:		jw@pegasys.ws

		Remember Cernan and Schmitt

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Bug in open() function (?)
  2003-07-12  6:14             ` Valdis.Kletnieks
@ 2003-07-12  9:37               ` Andries Brouwer
  2003-07-12 12:04                 ` jw schultz
  0 siblings, 1 reply; 10+ messages in thread
From: Andries Brouwer @ 2003-07-12  9:37 UTC (permalink / raw)
  To: Valdis.Kletnieks; +Cc: Andrew Morton, jcwren, linux-kernel

On Sat, Jul 12, 2003 at 02:14:43AM -0400, Valdis.Kletnieks@vt.edu wrote:

> On Fri, 11 Jul 2003 22:23:00 PDT, Andrew Morton said:
> 
> > We've lived with it for this long.
> 
> Well... you have a point there..
> 
> > Given that the behaviour is undefined, the behaviour which we should
> > implement is clearly "whatever 2.4 is doing".  So let's leave it alone.
> 
> I suppose I could live with that *IF* somebody fixes 'man 2 open' to
> reflect reality.

Corrections and additions to manpages are always welcome.
Mail to aeb@cwi.nl .


(Concerning the topic under discussion, the man page says

       O_TRUNC
              If  the  file  already exists and is a regular file
              and the open mode allows writing (i.e.,  is  O_RDWR
              or  O_WRONLY) it will be truncated to length 0.  If
              the file is a FIFO or  terminal  device  file,  the
              O_TRUNC  flag  is  ignored. Otherwise the effect of
              O_TRUNC is unspecified.

which is precisely right. It continues

                                       (On many Linux versions it
              will  be  ignored; on other versions it will return
              an error.)

where someone may read this as if this is an exhaustive list of
possibilities. So adding ", or actually do the truncate" will
clarify.)


Concerning the desired behaviour: if I recall things correctly
doing the truncate was old SunOS behaviour, not doing it,
that is, honouring the O_RDONLY, is new Solaris behaviour.
Maybe someone with access to such machines can check.

Software exists that does O_RDONLY | O_TRUNC.

Andries


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Bug in open() function (?)
  2003-07-12  5:23           ` Andrew Morton
@ 2003-07-12  6:14             ` Valdis.Kletnieks
  2003-07-12  9:37               ` Andries Brouwer
  0 siblings, 1 reply; 10+ messages in thread
From: Valdis.Kletnieks @ 2003-07-12  6:14 UTC (permalink / raw)
  To: Andrew Morton; +Cc: jcwren, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 362 bytes --]

On Fri, 11 Jul 2003 22:23:00 PDT, Andrew Morton said:

> We've lived with it for this long.

Well... you have a point there..

> Given that the behaviour is undefined, the behaviour which we should
> implement is clearly "whatever 2.4 is doing".  So let's leave it alone.

I suppose I could live with that *IF* somebody fixes 'man 2 open' to
reflect reality.




[-- Attachment #2: Type: application/pgp-signature, Size: 226 bytes --]

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Bug in open() function (?)
  2003-07-12  5:11         ` Valdis.Kletnieks
  2003-07-12  5:23           ` Andrew Morton
@ 2003-07-12  5:39           ` J.C. Wren
  1 sibling, 0 replies; 10+ messages in thread
From: J.C. Wren @ 2003-07-12  5:39 UTC (permalink / raw)
  To: Valdis.Kletnieks, Andrew Morton; +Cc: linux-kernel

	While I'm sure I don't have a grasp of "the big picture", I would have 
imagines these would have acted like a mask against the file system 
attributes.  In effect setting O_RDONLY would clear the write permission bits 
read by stat(), and O_WRONLY would clear the read permission bits.  O_RDONLY 
+ O_WRONLY (O_RDWR) would leave the permissions alone.  These would be 
applied somewhere around the may_open() call in fs/open_namei.c

	A poor mans umaks(), I guess.

	--John

On Saturday 12 July 2003 01:11 am, Valdis.Kletnieks@vt.edu wrote:
> On Fri, 11 Jul 2003 20:38:09 PDT, Andrew Morton said:
> > "J.C. Wren" <jcwren@jcwren.com> wrote:
> > > I was playing around today and found that if an existing file is opened
> > > wit
>
> h
>
> > >  O_TRUNC | O_RDONLY, the existing file is truncated.
> >
> > Well that's fairly idiotic, isn't it?
>
> Not idiotic at all, and even if it was, it's still contrary to specific
> language in the manpage.
>
> I could *easily* see some program having a line of code:
>
> 	if (do_ro_testing) openflags |= O_RDONLY;
>
> I'd not be surprised if J.C. was playing around because a file unexpectedly
> shrank to zero size because of code like this.  There's a LOT of programs
> that implement some sort of "don't really do it" option, from "/bin/bash
> -n" to "cdrecord -dummy".  So you do something like the above to make your
> file R/O - and O_TRUNC *STILL* zaps the file, in *direct violation* of the
> language in the manpage.
>
> Whoops.  Ouch. Where's the backup tapes?
>
> > The Open Group go on to say "The result of using O_TRUNC with O_RDONLY is
> > undefined" which is also rather silly.
> >
> > I'd be inclined to leave it as-is, really.
>
> I hate to think how many programmers are relying on the *documented*
> behavior to prevent data loss during debugging/test runs....
>
> /Valdis


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Bug in open() function (?)
  2003-07-12  5:11         ` Valdis.Kletnieks
@ 2003-07-12  5:23           ` Andrew Morton
  2003-07-12  6:14             ` Valdis.Kletnieks
  2003-07-12  5:39           ` J.C. Wren
  1 sibling, 1 reply; 10+ messages in thread
From: Andrew Morton @ 2003-07-12  5:23 UTC (permalink / raw)
  To: Valdis.Kletnieks; +Cc: jcwren, linux-kernel

Valdis.Kletnieks@vt.edu wrote:
>
> On Fri, 11 Jul 2003 20:38:09 PDT, Andrew Morton said:
> > "J.C. Wren" <jcwren@jcwren.com> wrote:
> > >
> > > I was playing around today and found that if an existing file is opened wit
> h 
> > >  O_TRUNC | O_RDONLY, the existing file is truncated.
> > 
> > Well that's fairly idiotic, isn't it?
> 
> Not idiotic at all

Sigh.  I meant the kernel behaviour is idiotic.  Returning -EINVAL would
have been much better behaviour.

> 
> > The Open Group go on to say "The result of using O_TRUNC with O_RDONLY is
> > undefined" which is also rather silly.
> > 
> > I'd be inclined to leave it as-is, really.
> 
> I hate to think how many programmers are relying on the *documented* behavior to
> prevent data loss during debugging/test runs....

We've lived with it for this long.

The behaviour is "undefined".  Any application which uses O_RDONLY|O_TRUNC
is buggy.

If we were to alter the behaviour now, any buggy-but-happens-to-work app
which is accidentally using O_RDONLY|O_TRUNC may break.  And now is not the
time to break things.

Given that the behaviour is undefined, the behaviour which we should
implement is clearly "whatever 2.4 is doing".  So let's leave it alone.

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Bug in open() function (?)
  2003-07-12  3:38       ` Andrew Morton
@ 2003-07-12  5:11         ` Valdis.Kletnieks
  2003-07-12  5:23           ` Andrew Morton
  2003-07-12  5:39           ` J.C. Wren
  0 siblings, 2 replies; 10+ messages in thread
From: Valdis.Kletnieks @ 2003-07-12  5:11 UTC (permalink / raw)
  To: Andrew Morton; +Cc: jcwren, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 1229 bytes --]

On Fri, 11 Jul 2003 20:38:09 PDT, Andrew Morton said:
> "J.C. Wren" <jcwren@jcwren.com> wrote:
> >
> > I was playing around today and found that if an existing file is opened wit
h 
> >  O_TRUNC | O_RDONLY, the existing file is truncated.
> 
> Well that's fairly idiotic, isn't it?

Not idiotic at all, and even if it was, it's still contrary to specific
language in the manpage.

I could *easily* see some program having a line of code:

	if (do_ro_testing) openflags |= O_RDONLY;

I'd not be surprised if J.C. was playing around because a file unexpectedly
shrank to zero size because of code like this.  There's a LOT of programs that
implement some sort of "don't really do it" option, from "/bin/bash -n" to
"cdrecord -dummy".  So you do something like the above to make your
file R/O - and O_TRUNC *STILL* zaps the file, in *direct violation* of
the language in the manpage.

Whoops.  Ouch. Where's the backup tapes?

> The Open Group go on to say "The result of using O_TRUNC with O_RDONLY is
> undefined" which is also rather silly.
> 
> I'd be inclined to leave it as-is, really.

I hate to think how many programmers are relying on the *documented* behavior to
prevent data loss during debugging/test runs....

/Valdis

[-- Attachment #2: Type: application/pgp-signature, Size: 226 bytes --]

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Bug in open() function (?)
  2003-07-12  3:09     ` Bug in open() function (?) J.C. Wren
@ 2003-07-12  3:38       ` Andrew Morton
  2003-07-12  5:11         ` Valdis.Kletnieks
  0 siblings, 1 reply; 10+ messages in thread
From: Andrew Morton @ 2003-07-12  3:38 UTC (permalink / raw)
  To: jcwren; +Cc: linux-kernel

"J.C. Wren" <jcwren@jcwren.com> wrote:
>
> I was playing around today and found that if an existing file is opened with 
>  O_TRUNC | O_RDONLY, the existing file is truncated.

Well that's fairly idiotic, isn't it?

The Open Group go on to say "The result of using O_TRUNC with O_RDONLY is
undefined" which is also rather silly.

I'd be inclined to leave it as-is, really.

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Bug in open() function (?)
  2003-07-12  2:42   ` Samuel Thibault
@ 2003-07-12  3:09     ` J.C. Wren
  2003-07-12  3:38       ` Andrew Morton
  0 siblings, 1 reply; 10+ messages in thread
From: J.C. Wren @ 2003-07-12  3:09 UTC (permalink / raw)
  To: linux-kernel

	I was playing around today and found that if an existing file is opened with 
O_TRUNC | O_RDONLY, the existing file is truncated.  This is contrary to the 
documentation for "man 2 open".  Which behavior is correct, the man page, or 
what actually happens?  Or wold this be considered a glibc/libc problem?  
This is on a stock 2.5.74 kernel.

	'man 2 open', on O_TRUNC: If  the  file already exists and is a regular file 
and the open mode allows writing (i.e., is O_RDWR or O_WRONLY) it will be 
truncated to length 0.

	--John

#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>

int main (int argc, char **argv)
{
   int fd;

   if ((fd = open ("test", O_TRUNC | O_RDONLY)) == -1)
   {
      printf ("%d:%s\n", errno, strerror (errno));
      exit (1);
   }

   close (fd);

   exit (0);
}

[bash] cc test.c
[bash] ls -l >test
[bash] ls -l test
-rw-r--r--    1 jcw      users         195 Jul 11 23:06 test
[bash] ./a.out
[bash] ls -l test
-rw-r--r--    1 jcw      users           0 Jul 11 23:06 test


^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2003-07-17 10:41 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-07-17  7:54 Bug in open() function (?) Michael Kerrisk
2003-07-17 10:54 ` Andries Brouwer
  -- strict thread matches above, loose matches on Subject: below --
2003-07-12  1:17 [PATCH] [2.5] adding ppp xon/xoff support Samuel Thibault
2003-07-12  1:30 ` Paul Mackerras
2003-07-12  2:42   ` Samuel Thibault
2003-07-12  3:09     ` Bug in open() function (?) J.C. Wren
2003-07-12  3:38       ` Andrew Morton
2003-07-12  5:11         ` Valdis.Kletnieks
2003-07-12  5:23           ` Andrew Morton
2003-07-12  6:14             ` Valdis.Kletnieks
2003-07-12  9:37               ` Andries Brouwer
2003-07-12 12:04                 ` jw schultz
2003-07-12  5:39           ` J.C. Wren

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.