Re: How to create Daemon for Mac 10.4
Re: How to create Daemon for Mac 10.4
- Subject: Re: How to create Daemon for Mac 10.4
- From: Alberto Ricart <email@hidden>
- Date: Thu, 5 Jan 2006 17:36:31 -0600
Actually a daemon is just a standard tool, that behaves differently
than your standard user process. Essentially a daemon:
a) forks off the parent process, if the fork is successful, it exits
the parent process.
b) opens logs for writing
c) sets a session id (setsid)
b) it then does a number of things like setting its current directory
to somewhere specific (typically root)
c) sets its umask
d) and then closes things like stdin, stdout and stderr.
e) Finally it goes into a loop where it does whatever your deamon is
supposed to do.
There might be other bits that should be done to make it 'secure',
but I don't recall from the top of my head. A google search on
writing a daemon should yield some good results.
Note for OS X, if you launch with launchd, you are NOT supposed to do
any of these things.
/a
On Jan 5, 2006, at 4:11 AM, Elango C wrote:
Hi Alberto Ricart,
Can you tell me how to create a Daemon, That is Is this a C/C++ tool
or a standard tool. I am using Xcode version 2.2, I could not see any
template for the Daemon.
I am new to this concepts, please guide me to fix it up.
Thanks,
--Elango
On 1/4/06, Elango C <email@hidden> wrote:
Hi Alberto Ricart,
Thanks for the reply and code snippet, I will try this and let you
know the queries if any.
Thanks,
--Elango
On 1/3/06, Alberto Ricart <email@hidden> wrote:
Hi Elango,
Not during startup. The UI application can only be launched at login
because you need a window server connection. If you are a daemon
process you don't have access to the window sever, thus no ui from
there.
The only solution is to break your functionality into two apps. One
that is the 'server' and a client (the ui).
1) is the daemon. This could be extremely simple if you use DO:
//@todo: might need to handle signals
int main(int argc, char *argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
NSObject <SomeProtocolYouMake> *server = [[NannySessionProvider
alloc]init];
NSConnection *conn = [NSConnection defaultConnection];
[conn setRootObject:server];
if(! [conn registerName:@"com.foo.my.server"])
{
NSLog(@"Unable to register server");
}
else
{
[runLoop run];
}
// if we are here the runloop terminated
[sessionProvider release];
[pool release];
return 0;
}
2) the other is the client:
- (void)applicationDidFinishLaunching:(NSNotification *)
aNotification
{
NSString *name = @"com.foo.my.server";
NSDistantObject *o = [NSConnection
rootProxyForConnectionWithRegisteredName:name host:nil];
if(o == nil)
{
//@todo: Ohoh - the daemon is dead... launch it?
NSLog(@"Unable to connect to the server. Exiting...");
[NSApp terminate:self];
}
[o setProtocolForProxy:@protocol(SessionProvider)];
id <SessionProvider> sessionProvider = (< SomeProtocolYouMake >)o;
// call all sorts of methods defined by your protocol...
/a
On Jan 3, 2006, at 1:21 AM, Elango C wrote:
hi,
Can I launch the UI application (i.e. Cocoa application) during OS
startup?.. Can you send me a code snippets to create
services/daemon/startup items.
Thanks,
--Elango
On 1/3/06, Alberto Ricart <email@hidden> wrote:
The app that does the backup should have backup privileges -
probably
operator, (because of how it is launched), and should probably be
started by launchd services/or be daemon/startup item/etc. This
means
that it would already be running as root, and thus not require
authentication.
/a
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
40gmail.com
This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden