Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

deamonizing a program in Unix
Hi all
I have query regarding deamonizing programs in unix.

Usually we do a close of all file descriptors in a loop for
deamonizing. Is this correct?

What if some backend(such as NIS) has cached an file descriptor for
performance issue because of function call before we do deamonizing and
after that during deamonizing since we close all the file descriptors
we are closing the file descriptor opened by the backend also. Is this
correct behaviour of a deamoonizing programs?

Also where can i get the source of daemon() function call?
Any pointers would be helpful

Thanks
Pappan


Report this thread to moderator Post Follow-up to this message
Old Post
Pappan
01-04-05 08:59 PM


Re: deamonizing a program in Unix
On Tue, 4 Jan 2005, Pappan wrote:

> Usually we do a close of all file descriptors in a loop for
> deamonizing. Is this correct?

Yep.  Well, you close all the file descriptors you're not using,
which isn't necessarily ALL of them...

> What if some backend(such as NIS) has cached an file descriptor for
> performance issue because of function call before we do deamonizing and
> after that during deamonizing since we close all the file descriptors
> we are closing the file descriptor opened by the backend also. Is this

???  How does one program using a file descriptor affect how you use
it in another?

> Also where can i get the source of daemon() function call?
> Any pointers would be helpful

The example source code for my book, Solaris Systems Programming,
contains a suitable function.  You can find the source code (and
much more) from www.rite-group.com/rich/ssp.

HTH,

--
Rich Teer, SCNA, SCSA, author of "Solaris Systems Programming"

.  *   * . * .* .
.   *   .   .*
President,                                          * .  . /\ ( .  . *
Rite Online Inc.                                     . .  / .\   . * .
.*.  / *  \  . .
. /*   o \     .
Voice: +1 (250) 979-1638                            *   '''||'''   .
URL: http://www.rite-online.net                     ******************

Report this thread to moderator Post Follow-up to this message
Old Post
Rich Teer
01-04-05 08:59 PM


Re: deamonizing a program in Unix
"Pappan" <padmanabhanp@fastmail.fm> writes:

> I have query regarding deamonizing programs in unix.
>
> Usually we do a close of all file descriptors in a loop for
> deamonizing. Is this correct?

It might be. Depends on your requirements. For example, a daemon that
spawns other programs would probably want to leave descriptors 0, 1
and 2 open, but pointing to /dev/null.

> What if some backend(such as NIS) has cached an file descriptor for
> performance issue because of function call before we do deamonizing
> and after that during deamonizing since we close all the file
> descriptors we are closing the file descriptor opened by the backend
> also. Is this correct behaviour of a deamoonizing programs?

I didn't understand this. Can you rephrase?

> Also where can i get the source of daemon() function call?
> Any pointers would be helpful

You can get the source code in Stevens' "Advanced Programming the UNIX
Environment" at http://www.kohala.com/start/apue.html. It has a daemon
function in it. Also see

http://www.faqs.org/faqs/unix-faq/programmer/faq/

Look for question 1.7 and the example section at the end.

Joe

Report this thread to moderator Post Follow-up to this message
Old Post
joe@invalid.address
01-04-05 08:59 PM


Re: deamonizing a program in Unix
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Pappan wrote:
> Hi all
> I have query regarding deamonizing programs in unix.
>
> Usually we do a close of all file descriptors in a loop for
> deamonizing. Is this correct?

Usually, but not always. Certainly, you normally want to close and
discard the controlling terminal, and you usually want to close any
'unused' file descriptors (i.e. descriptors that might have been left
open by the process that exec'ed your daemon), but closing any other
descriptors is something that you do if you want or need to, not because
you have to.

> What if some backend(such as NIS) has cached an file descriptor for
> performance issue because of function call before we do deamonizing and
> after that during deamonizing since we close all the file descriptors
> we are closing the file descriptor opened by the backend also. Is this
> correct behaviour of a deamoonizing programs?

I'm not sure I understand your scenario here. Could you explain it in
simpler terms? FWIW, if you suspect that a descriptor has been cached,
and that closing the descriptor will cause problems, you don't /have/ to
close the descriptor.

> Also where can i get the source of daemon() function call?
> Any pointers would be helpful

You can take a look at this helpfull page...
http://www.enderunix.org/docs/eng/daemon.php

- --

Lew Pitcher, IT Consultant, Enterprise Data Systems
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)

 iD8DBQFB2uPKagVFX4UWr64RApDJAJ9HWamZrSCt
uNznrK9ErE1ukbb34gCg8XBj
ljlZFAylWWidFUwSvFGN+O4=
=xnZs
-----END PGP SIGNATURE-----

Report this thread to moderator Post Follow-up to this message
Old Post
Lew Pitcher
01-04-05 08:59 PM


Re: deamonizing a program in Unix
<joe@invalid.address> wrote in message
news:m3sm5hvxco.fsf@invalid.address...
> "Pappan" <padmanabhanp@fastmail.fm> writes: 
>
> It might be. Depends on your requirements. For example, a daemon that
> spawns other programs would probably want to leave descriptors 0, 1
> and 2 open, but pointing to /dev/null.

I'd say it's not a bad idea to do this even in a daemon the doesn't spawn
other programs: at least any spurious output goes to the bit bucket instead
of possibly ending up somewhere you didn't want it to.
 
>
> I didn't understand this. Can you rephrase?

I think Pappan was thinking this: suppose you link your application with a
library, and that library obtains a descriptor (perhaps for a socket) when
you first call into it, but - intentionally - doesn't close it. This may be
done so that subsequent calls are faster.

Clearly, closing that (unknown) descriptor could have "interesting" effects,
particularly if the descriptor is reused.

The way I see it, you can't usually be sure whether or not some library has
deliberately left a descriptor open as part of its internal operation, but
if it has, bad things may happen, so it's usually unwise to close "random"
descriptors.

Alex



Report this thread to moderator Post Follow-up to this message
Old Post
Alex Fraser
01-04-05 08:59 PM


Re: deamonizing a program in Unix
"Alex Fraser" <me@privacy.net> wrote in message
news:340999F40h55nU1@individual.net...
[snip]
> The way I see it, you can't usually be sure whether or not some library
> has deliberately left a descriptor open as part of its internal
> operation, but if it has,

In case it isn't obvious, I meant "but if it has, and you close it"

> bad things may happen, so it's usually unwise to close "random"
> descriptors.

Alex



Report this thread to moderator Post Follow-up to this message
Old Post
Alex Fraser
01-05-05 02:07 AM


Re: deamonizing a program in Unix
On Tue, 4 Jan 2005, Alex Fraser wrote:

> I think Pappan was thinking this: suppose you link your application with a
> library, and that library obtains a descriptor (perhaps for a socket) when
> you first call into it, but - intentionally - doesn't close it. This may b
e
> done so that subsequent calls are faster.
>
> Clearly, closing that (unknown) descriptor could have "interesting" effect
s,
> particularly if the descriptor is reused.
>
> The way I see it, you can't usually be sure whether or not some library ha
s
> deliberately left a descriptor open as part of its internal operation, but
> if it has, bad things may happen, so it's usually unwise to close "random"
> descriptors.

A fair point, but given that daemonisation (usually) happens very early in
the lifetime of a (long-lived) process, a moot one.  The process should beco
me
a daemon first, before calling other stuff that might have the side effects
you mention.

--
Rich Teer, SCNA, SCSA, author of "Solaris Systems Programming"

.  *   * . * .* .
.   *   .   .*
President,                                          * .  . /\ ( .  . *
Rite Online Inc.                                     . .  / .\   . * .
.*.  / *  \  . .
. /*   o \     .
Voice: +1 (250) 979-1638                            *   '''||'''   .
URL: http://www.rite-online.net                     ******************

Report this thread to moderator Post Follow-up to this message
Old Post
Rich Teer
01-05-05 02:07 AM


Re: deamonizing a program in Unix
Rich Teer wrote:
> On Tue, 4 Jan 2005, Alex Fraser wrote:
>
> 
>
>
> A fair point, but given that daemonisation (usually) happens very early in
> the lifetime of a (long-lived) process, a moot one.  The process should be
come
> a daemon first, before calling other stuff that might have the side effect
s
> you mention.
>

I am currently working on patches for Solaris 8 and 9 to fix a bug
due to the (undocumented) use of a file descriptor by the Netscape
security code (NSS) which is used by LDAP. Daemons sometimes try to
initialize things before daemonizing so that errors can be reported
when they start up. In the case of the LDAP backend to the name
service switch, any name service call at all before daemonizing
resulted in this file descriptor being opened, and then being closed
when the process daemonized, resulting in subsequent errors in the
LDAP calls. There are two ways to deal with this kind of thing in
a library. One is to document the usage of the the file descriptor,
and the other is to test if the descriptor is still used and pointing
where you expect it, and to reinit if it is not. The NSS code did
neither.

--
blu

I voted electronically...I think.
----------------------------------------------------------------------
Brian Utterback - OP/N1 RPE, Sun Microsystems, Inc.
Ph:877-259-7345, Em:brian.utterback-at-ess-you-enn-dot-kom

Report this thread to moderator Post Follow-up to this message
Old Post
Brian Utterback
01-05-05 02:07 AM


Re: deamonizing a program in Unix
Rich Teer <rich.teer@rite-group.com> writes:

> On Tue, 4 Jan 2005, Alex Fraser wrote:
> 
>
> A fair point, but given that daemonisation (usually) happens very
> early in the lifetime of a (long-lived) process, a moot one.  The
> process should become a daemon first, before calling other stuff
> that might have the side effects you mention.

It might be desirable to do some initial setup before daemonizing, so
if something goes wrong, it can be easily reported.  Many servers
attempt to bind() a socket before becoming a deamon, and exit with an
error code on failure.

--
Måns Rullgård
mru@inprovide.com

Report this thread to moderator Post Follow-up to this message
Old Post
Måns Rullgård
01-05-05 02:07 AM


Re: deamonizing a program in Unix
On Tue, 4 Jan 2005, Richard Kettlewell wrote:

> Why?  It's not that they can do any harm, and it's sometimes useful to

File descriptors are a finite resource, so they should be manage
accordingly.

> be able to pass file descriptors down to programs executed by the

Yes, but how often would one want a daemon to pass on a file descriptor
IT gets from its parent?  Not also the word "usually", which implies
there might be circumstances under which closing the descriptors is
not a good idea.  But generally, it is the correct thing to do.

> Some bit of the C library called before you daemonize might have
> opened a file descriptor for its own purposes.  If you close it,
> library functions might mysteriously fail later.

Agreed; this is one of those circumstances...  :-)

--
Rich Teer, SCNA, SCSA, author of "Solaris Systems Programming"

.  *   * . * .* .
.   *   .   .*
President,                                          * .  . /\ ( .  . *
Rite Online Inc.                                     . .  / .\   . * .
.*.  / *  \  . .
. /*   o \     .
Voice: +1 (250) 979-1638                            *   '''||'''   .
URL: http://www.rite-online.net                     ******************

Report this thread to moderator Post Follow-up to this message
Old Post
Rich Teer
01-05-05 02:07 AM


Sponsored Links




Last Thread Next Thread Next
Pages (2): [1] 2 »
Search this forum -> 
Post New Thread

Unix Programming archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 07:40 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.