Re: password (& host?) file lookups, daemons and stray descriptors

From: Barry Margolin (barmar_at_alum.mit.edu)
Date: 06/25/05

  • Next message: MacKay: "network KVM"
    Date: Fri, 24 Jun 2005 23:52:28 -0400
    
    

    In article <42bc1ccc$0$5791$9a6e19ea@news.newshosting.com>,
     "Mr. Uh Clem" <uhclem@DutchElmSt.invalid> wrote:

    > Yeah, I guess so, but this is contrary to all I've ever
    > seen on how to do a daemon. Consider these early hits
    > from Googling for [unix daemon process] :
    >
    > http://www.enderunix.org/docs/eng/daemon.php
    > http://www.erlenstar.demon.co.uk/unix/faq_2.html#SEC16
    > http://www.unix.com/archive/index.php/t-3736.html
    >
    > I've never seen any cautionary text regarding fds from
    > called routines which must be handled with care:

    I think everyone may assume that you do this so early in your program
    that nothing would have already opened fd's. Notice that the first page
    you referenced above refers to these as "inherited" fd's -- the
    expectation is that any fd's that are open were inherited from the
    calling process, not opened by this process. But when you call library
    routines, and they may have opened descriptors and stored them in static
    variables, this assumption is no longer valid.

    > I'm also rather surprised that the man pages for
    > routines such as getpwuid() are silent on their
    > creation of fds and the handling required. (And

    These fd's are internal implementation details, and the caller is not
    expected to "handle" them. They might change from release to release,
    and programs should not depend on them.

    > I still don't see anything in the truss which
    > explains the lost lock.) I'm also surprised
    > it is not possible to properly close such open
    > fds, with routines such as endpwent().

    Since it's a network socket, I think it's probably part of the NIS
    library, not anything specific to password lookup. Endpwent() doesn't
    have any way to make NIS discard the socket it uses to talk to the
    server.

    >
    > > What you may want to do is set the close-on-exec flag on all the
    > > descriptors, if your daemon execs other programs.
    >
    > So generic daemonizing might be something like:
    >
    > - close all fds above 2 at the start of main (?)

    Right. Then you can call getpwuid() to perform your validity check.
    Since stderr is still pointing to its original location, you can report
    an error.

    >
    > - fork; setsid; fork; chdir("/" or other safe place);
    > close 0-2 and redirect (typically to /dev/null);
    > set close-on-exec for fds above 2

    Seems right to me.

    -- 
    Barry Margolin, barmar@alum.mit.edu
    Arlington, MA
    *** PLEASE post questions in newsgroups, not directly to me ***
    

  • Next message: MacKay: "network KVM"

    Relevant Pages

    • Re: password (& host?) file lookups, daemons and stray descriptors
      ... routines, and they may have opened descriptors and stored them in static ... > creation of fds and the handling required. ... have any way to make NIS discard the socket it uses to talk to the ... if your daemon execs other programs. ...
      (comp.unix.programmer)
    • Re: password (& host?) file lookups, daemons and stray descriptors
      ... > I think the solution is obvious: don't close all file descriptors, ... seen on how to do a daemon. ... I've never seen any cautionary text regarding fds from ... called routines which must be handled with care: ...
      (comp.sys.hp.hpux)
    • Re: password (& host?) file lookups, daemons and stray descriptors
      ... > I think the solution is obvious: don't close all file descriptors, ... seen on how to do a daemon. ... I've never seen any cautionary text regarding fds from ... called routines which must be handled with care: ...
      (comp.unix.programmer)
    • Dealing With Zombies in a Daemon
      ... I have a Perl program that runs as a daemon, with a specified loop, doing ... the start of the minute it runs one set of routines, ... reaperuntil the launched program finishes. ...
      (comp.lang.perl.misc)
    • Re: deamonizing a program in Unix
      ... > that calls the daemon. ... > of pointless FDs" is to close the ones that aren't necessary. ... > the deny by default school, and you seem to be from the allow by ...
      (comp.unix.programmer)