FAQ
Hi All,

This is really not a beginner question but I'm not sure what list to post it on.

I've created a daemon library for easily building daemons and at some
point code was added to handle closing all fd's so the daemonized
process would not have fd's hanging around it shouldn't have or didn't
expect to have. The code to close the fd was using POSIX::close(),
and this was a bad idea because, though it closes the fd, it leaves
any perl filehandles associated with the fd hanging around. This
makes for really twisty and perplexing bugs.

So I don't want to do that, but I can't figure out for the life of me
how to get a hold of all filehandles associated with an fd, or to
close all filehandles associated with an fd. Is there a way to do
this? I tried walking through the symbol tables, but that won't get
lexically scoped and localized filehandles. How does one do this?

BTW, at this moment I'm looking through perlio.c to try to see how the
filehandles and fd's are managed internally. If I had to I could
write a c routine to do this and expose it through XS. Any clues in
this area though, would be nice (like is there something already in
perlio.c to do this (-:).

And of course pointing me to a more appropriate list is fine too.

Thanks...james

Search Discussions

  • Shawn H Corey at Dec 11, 2009 at 9:35 pm

    James Olin Oden wrote:
    Hi All,

    This is really not a beginner question but I'm not sure what list to post it on.

    I've created a daemon library for easily building daemons and at some
    point code was added to handle closing all fd's so the daemonized
    process would not have fd's hanging around it shouldn't have or didn't
    expect to have. The code to close the fd was using POSIX::close(),
    and this was a bad idea because, though it closes the fd, it leaves
    any perl filehandles associated with the fd hanging around. This
    makes for really twisty and perplexing bugs.

    So I don't want to do that, but I can't figure out for the life of me
    how to get a hold of all filehandles associated with an fd, or to
    close all filehandles associated with an fd. Is there a way to do
    this? I tried walking through the symbol tables, but that won't get
    lexically scoped and localized filehandles. How does one do this?

    BTW, at this moment I'm looking through perlio.c to try to see how the
    filehandles and fd's are managed internally. If I had to I could
    write a c routine to do this and expose it through XS. Any clues in
    this area though, would be nice (like is there something already in
    perlio.c to do this (-:).

    And of course pointing me to a more appropriate list is fine too.

    Thanks...james
    All daemons should periodically kill themselves and restart. This will:

    * Close forgotten file handles.
    * Free any memory leaks.
    * Remove zombie children.
    * Do other cleanup.

    This is best done by done by setting the daemon in the appropriate
    init(8) runlevel.


    --
    Just my 0.00000002 million dollars worth,
    Shawn

    Programming is as much about organization and communication
    as it is about coding.

    I like Perl; it's the only language where you can bless your
    thingy.
  • James Olin Oden at Dec 11, 2009 at 11:10 pm

    On Fri, Dec 11, 2009 at 4:36 PM, Shawn H Corey wrote:
    James Olin Oden wrote:
    Hi All,

    This is really not a beginner question but I'm not sure what list to post it on.

    I've created a daemon library for easily building daemons and at some
    point code was added to handle closing all fd's so the daemonized
    process would not have fd's hanging around it shouldn't have or didn't
    expect to have.   The code to close the fd was using POSIX::close(),
    and this was a bad idea because, though it closes the fd, it leaves
    any perl filehandles associated with the fd hanging around.  This
    makes for really twisty and perplexing bugs.

    So I don't want to do that, but I can't figure out for the life of me
    how to get a hold of all filehandles associated with an fd, or to
    close all filehandles associated with an fd.  Is there a way to do
    this?  I tried walking through the symbol tables, but that won't get
    lexically scoped and localized filehandles.  How does one do this?

    BTW, at this moment I'm looking through perlio.c to try to see how the
    filehandles and fd's are managed internally.  If I had to I could
    write a c routine to do this and expose it through XS.  Any clues in
    this area though, would be nice (like is there something already in
    perlio.c to do this (-:).

    And of course pointing me to a more appropriate list is fine too.

    Thanks...james
    All daemons should periodically kill themselves and restart.  This will:
    Shawn good point. Do you know a way to handle the initial fd leak
    upon first fork? Remember this is a library that handles
    daemonization for some unknown program. The goal is to close all
    filed descriptors except the ones they tell me not close (or to not
    close them at all if they specify this). This is actually as much a
    security feature as anything. Right now though, being in this library
    I don't know of any means to figure out without them telling what
    FileHandles have been opened before calling my daemonize function. Is
    there a way to do this? or a way to get the FileHandles from the fd?

    Thanks...james
  • C.DeRykus at Dec 12, 2009 at 9:28 am

    On Dec 11, 3:10 pm, james.o...@gmail.com (James Olin Oden) wrote:
    On Fri, Dec 11, 2009 at 4:36 PM, Shawn H Corey wrote:

    James Olin Oden wrote:
    Hi All,
    This is really not a beginner question but I'm not sure what list to post it on.
    I've created a daemon library for easily building daemons and at some
    point code was added to handle closing all fd's so the daemonized
    process would not have fd's hanging around it shouldn't have or didn't
    expect to have.   The code to close the fd was using POSIX::close(),
    and this was a bad idea because, though it closes the fd, it leaves
    any perl filehandles associated with the fd hanging around.  This
    makes for really twisty and perplexing bugs.
    So I don't want to do that, but I can't figure out for the life of me
    how to get a hold of all filehandles associated with an fd, or to
    close all filehandles associated with an fd.  Is there a way to do
    this?  I tried walking through the symbol tables, but that won't get
    lexically scoped and localized filehandles.  How does one do this?
    BTW, at this moment I'm looking through perlio.c to try to see how the
    filehandles and fd's are managed internally.  If I had to I could
    write a c routine to do this and expose it through XS.  Any clues in
    this area though, would be nice (like is there something already in
    perlio.c to do this (-:).
    And of course pointing me to a more appropriate list is fine too.
    Thanks...james
    All daemons should periodically kill themselves and restart.  This will:
    Shawn good point.  Do you know a way to handle the initial fd leak
    upon first fork?  Remember this is a library that handles
    daemonization for some unknown program.  The goal is to close all
    filed descriptors except the ones they tell me not close (or to not
    close them at all if they specify this).  This is actually as much a
    security feature as anything.  Right now though, being in this library
    I don't know of any means to figure out without them telling what
    FileHandles have been opened before calling my daemonize function.  Is
    there a way to do this?  or a way to get the FileHandles from the fd?
    The forked process could exec itself to close fd's higher
    than $^F (ordinarily 2) for the initial leak. But I suspect
    there's no easy way to close fd's more selectively.

    --
    Charles DeRykus
  • James Olin Oden at Dec 12, 2009 at 2:31 pm
    <snip>
    The forked process could exec itself to close fd's higher
    than $^F (ordinarily 2) for the initial leak. But I suspect
    there's no easy way to close fd's more selectively.
    Yeah I had thought about the exec(), but being its a library I would
    have no idea what to exec...they could tell me but that would feel
    like a really clumsy daemon library.

    I did find this out though. There is a "table" of filehandles (or
    really PerlIO structures) in perlio.c. This is what is iterated
    across when you exit perl. What I need to do is write some C code
    that will provide a function that will return this table as a list of
    filehandles. It would be cool though if this already existed, but I'm
    not seeing it.

    Thanks...james

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedDec 11, '09 at 9:16p
activeDec 12, '09 at 2:31p
posts5
users3
websiteperl.org

People

Translate

site design / logo © 2021 Grokbase