Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: open fails in daemon



On Tuesday, July 22, 2003, at 10:58, Steve Cox wrote:
email@hidden writes:
Well, daemon() does a fork() for you. So, it still could be a
fork()-related issue. Does your code not call daemon() when launched
as a startupitem?
Yes, it does. But from what you've said, at that point, it's called
within the startup context.

On the issue of fork() and daemon() and using Apple libraries (above the BSD layer) afterwards, I wrote this not long ago in response to the same question on another list:


Why shouldn't a process call daemon() if it's using the higher-level frameworks? It seems like an odd restriction. Does this also apply to processes that do similar things to the daemon() function (e.g. detach themselves from the terminal and fork() into the background)?

The restriction applies to any use of fork() which is not soon followed by an exec*(), not just daemon()'s use of fork(). You cannot reliably use higher-level API after a fork(), before the exec*() either.

The issue stems from the fact that fork() does not replicate all a parent's state (such as some kernel resources) into the child, but it does replicate the memory and some resources. Thus (1) libraries in the new process have not gone through their ordinary initialization steps in the new process, and (2) libraries' cached state or opened resources have now gone silently invalid underneath them, due to the fork() (for those things not replicated by fork()). For example, calls to library functions may produce errors now where they weren't before the fork(), or crash, or silently misbehave, or whatever.

I believe there is a Tech Note on this. You need to exec*() after a fork() if you want to use APIs above the BSD/POSIX layer. And POSIX itself, I believe I've been told, specifies only a small list of things that are guaranteed safe after fork(). [But in practice...]


Chris Kane
CoreFoundation, Apple
_______________________________________________
darwin-development mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/darwin-development
Do not post admin requests to the list. They will be ignored.

References: 
 >Re: open fails in daemon (From: Jim Magee <email@hidden>)
 >Re: open fails in daemon (From: "Steve Cox" <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.