• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Question on using launchd from a C program
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Question on using launchd from a C program


  • Subject: Re: Question on using launchd from a C program
  • From: Mike Kobb <email@hidden>
  • Date: Fri, 9 Jun 2006 10:55:46 -0700

Thanks for that!  Pretty interesting.

As it happens, since I really didn't want to introduce big Mac dependencies into the server code, I ended up writing a tiny little faceless background "helper" app that launches the server when it starts and quits it when it exits. Because it's a MacOS app and not a BSD daemon-type process like the server, I can add it to the Login Items, and that works beautifully for me. The helper reads a preference set by my prefs pane so it knows whether to launch the server when it starts or let the user do it manually...

Thanks to all for your help!
--Mike



On Jun 9, 2006, at 6:20 AM, Paul Nelson wrote:

It's probably a real sin to suggest this, but since you are waiting for
lepoard anyway....


Add this, and the associated observeNotices to your background app:

[[NSDistributedNotificationCenter defaultCenter] addObserver:self
selector:@selector(observeNotices:)
name:nil object:nil
suspensionBehavior:NSNotificationSuspensionBehaviorDeliverImmediately ];


In observeNotices, print out the NSNotification name and object values.

You will get everything, since the above addObserver call didn't specify.
Look at what gets printed out. Here are some event names you might be
interested in:


com.apple.userWillLogOut - the object will be a string containing the
user id


com.apple.sessionDidMoveOffConsole - for fast user switching
com.apple.sessionDidMoveOnConsole - for fast user switching

Once you figure out what notifications you like, replace the addObserver
with specific calls to register for specific notifications.


I've used these calls from a "faceless background app". They work fine. If
you don't like objective C, then figure out how to make similar calls from
CoreFoundation.



Of course, these will just get you by until Apple fully documents how to do
this kind of thing in Leapord....


Paul Nelson
Thursby Software Systems, Inc.


From: Mike Kobb <email@hidden>
Date: Thu, 8 Jun 2006 19:09:38 -0700
To: <email@hidden>
Subject: Re: Question on using launchd from a C program

Thanks for that!  I did consider the login/logout hooks, and I think
they could well work, but to install them you need administrator
privileges, and ideally I would not like to have to have the user
have to authenticate to set up the login behavior.

I think what I'm going to do instead is write a very simple faceless
app that will be set as a login item.  It will launch my server when
it launches at login (if the user chose login-time launch), and at
logout it'll find the server and tell it to quit.  Should be very
straightforward, and has the advantage (compared to launchd) of
working on older OSes as well.

Appreciate the suggestion.
--Mike

On Jun 8, 2006, at 6:38 PM, Andre LaBranche wrote:


On Jun 8, 2006, at 6:04 PM, Mike Kobb wrote:

Unfortunately, I need this to work, well, today if possible. ;-)

I'm sorry if I mis-typed.  It's login and logout that I care
about.  Not boot or shutdown (except insofar as shutdown implies
logout).  I want them to be able to choose to launch it at login,
or do it manually.  Then, if it's running when they log out
(whichever of those two methods they chose), I want it to quit.
...

Perhaps a loginhook / logouthook would work for the interim?

http://developer.apple.com/documentation/MacOSX/Conceptual/
BPSystemStartup/Articles/CustomLogin.html

Cheers,
-Andre


On Jun 8, 2006, at 4:37 PM, Dave Zarzycki wrote:

Will you be attending WWDC?

Can you wait for Leopard?

Why the asymmetric requirement of launching at boot, but exiting
at logout (as opposed to at boot/shutdown or login/logout)?

davez

On Jun 8, 2006, at 3:55 PM, Mike Kobb wrote:

Greetings,

I'm working on a MacOS preference pane that is used to configure
the options of a faceless background process (a music server).

This process is a BSD process for historical reasons, but will
run under the privileges of a particular user, and not as root.

I want to have a pop-up in the prefs pane that allows the user
to choose to manually launch the server (by clicking a "Start"
button in the pane, or to have the server launch at startup.
Either way, I want to be sure that the server quits when they
log out.

It seems like launchd is likely to be what I want, but there are
a few things I could use some help with.

I have figured out how to create the appropriate plist to make
the server launch at login and quit at shutdown, and that seems
to work fine.

So, I have two questions:

1) Is it possible to set up launchd to not launch the process at
login, but to shut it down at logout?

2) I want this all to be controlled by my preference pane.
Obviously, it's no trouble to get it to write out the required
plist file, but the part I'm not sure about is how to do the
equivalent of a "launchctl load" or "launchctl unload"
operation.  I could exec this, I guess, but I prefer to use a
proper C API rather than the exec mechanism whenever I can.  I
couldn't make heads or tails out of launch.h, I'm afraid.

Thanks in advance,
--Mike


_______________________________________________
Do not post admin requests to the list. They will be ignored.
Darwin-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: Question on using launchd from a C program
      • From: Andrew White <email@hidden>
References: 
 >Re: Question on using launchd from a C program (From: Paul Nelson <email@hidden>)

  • Prev by Date: Re: Very early load and usage of higher level APIs
  • Next by Date: Re: Question on using launchd from a C program
  • Previous by thread: Re: Question on using launchd from a C program
  • Next by thread: Re: Question on using launchd from a C program
  • Index(es):
    • Date
    • Thread