[Gllug] Off Topic: C File Descriptors

Richard Jones rich at annexia.org
Mon Mar 10 15:12:07 UTC 2008


On Sun, Mar 09, 2008 at 11:03:55PM +0000, Adrian McMenamin wrote:
> 
> On Sun, 2008-03-09 at 23:50 +0100, salsaman at xs4all.nl wrote:
> > On Sun, March 9, 2008 23:26, Nix wrote:
> > > On 8 Mar 2008, Richard Jones said:
> > >
> > >> On Fri, Mar 07, 2008 at 02:41:47AM +0100, salsaman at xs4all.nl wrote:
> > >>> something like this (untested, YMMV) should do it:
> > >> [appallingly bad example of C snipped]
> > >
> > > Oh come on, he allocated a whole 512 bytes for the filename and only had
> > > four coredump-inducing bugs, two misunderstandings of the nature of
> > > dynamic memory allocation,, two useless uses of memset() and two blocks
> > > of cut-and-pasted code. I've seen *much* worse.
> > 
> > Say what ? 512 is the maximum path length on Linux, I don't see where the
> > bugs were or how I misunderstood dynamic memory allocation (I presume you
> > mean the sprintf). The memset is needed because readlink() does not append
> > a  null byte - check the manpage.
> 
> /include/linux/limits.h defines the max path length as 4096

And of course don't forget that PATH_MAX doesn't even need to be
defined.  GNU Hurd is an example of an operating system which supports
paths of unlimited length, and consistent with POSIX it doesn't define
PATH_MAX at all.  (http://www.debian.org/ports/hurd/hurd-devel-debian)

Anyway, checking /proc/self/fd/[01] is fundamentally wrong, as this
example shows:

  $ echo $$
  2636
  $ ll /proc/2636/fd
  total 0
  lr-x------ 1 rjones rjones 64 2008-03-10 15:05 0 -> /dev/pts/10
  l-wx------ 1 rjones rjones 64 2008-03-10 15:05 1 -> /dev/pts/10
  l-wx------ 1 rjones rjones 64 2008-03-10 15:05 2 -> /dev/pts/10
  lrwx------ 1 rjones rjones 64 2008-03-10 15:05 255 -> /dev/pts/10
  $ exec 4> /tmp/test
  $ ll /proc/2636/fd
  total 0
  lr-x------ 1 rjones rjones 64 2008-03-10 15:05 0 -> /dev/pts/10
  l-wx------ 1 rjones rjones 64 2008-03-10 15:05 1 -> /dev/pts/10
  l-wx------ 1 rjones rjones 64 2008-03-10 15:05 2 -> /dev/pts/10
  lrwx------ 1 rjones rjones 64 2008-03-10 15:05 255 -> /dev/pts/10
  lr-x------ 1 rjones rjones 64 2008-03-10 15:05 4 -> /tmp/test
  $ rm /tmp/test
  $ ll /proc/2636/fd
  total 0
  lr-x------ 1 rjones rjones 64 2008-03-10 15:05 0 -> /dev/pts/10
  l-wx------ 1 rjones rjones 64 2008-03-10 15:05 1 -> /dev/pts/10
  l-wx------ 1 rjones rjones 64 2008-03-10 15:05 2 -> /dev/pts/10
  lrwx------ 1 rjones rjones 64 2008-03-10 15:05 255 -> /dev/pts/10
  lr-x------ 1 rjones rjones 64 2008-03-10 15:05 4 -> /tmp/test (deleted)
  $ exec 5> /tmp/test
  $ ll /proc/2636/fd
  total 0
  lr-x------ 1 rjones rjones 64 2008-03-10 15:05 0 -> /dev/pts/10
  l-wx------ 1 rjones rjones 64 2008-03-10 15:05 1 -> /dev/pts/10
  l-wx------ 1 rjones rjones 64 2008-03-10 15:05 2 -> /dev/pts/10
  lrwx------ 1 rjones rjones 64 2008-03-10 15:05 255 -> /dev/pts/10
  lr-x------ 1 rjones rjones 64 2008-03-10 15:05 4 -> /tmp/test (deleted)
  l-wx------ 1 rjones rjones 64 2008-03-10 15:05 5 -> /tmp/test
  $ rm /tmp/test
  $ ll /proc/2636/fd
  total 0
  lr-x------ 1 rjones rjones 64 2008-03-10 15:05 0 -> /dev/pts/10
  l-wx------ 1 rjones rjones 64 2008-03-10 15:05 1 -> /dev/pts/10
  l-wx------ 1 rjones rjones 64 2008-03-10 15:05 2 -> /dev/pts/10
  lrwx------ 1 rjones rjones 64 2008-03-10 15:05 255 -> /dev/pts/10
  lr-x------ 1 rjones rjones 64 2008-03-10 15:05 4 -> /tmp/test (deleted)
  l-wx------ 1 rjones rjones 64 2008-03-10 15:05 5 -> /tmp/test (deleted)

Do file descriptors 4 & 5 point to the same file?  Answer: no.

Rich.

-- 
Richard Jones
Red Hat
-- 
Gllug mailing list  -  Gllug at gllug.org.uk
http://lists.gllug.org.uk/mailman/listinfo/gllug




More information about the GLLUG mailing list