launching a user-context application by root-daemo

2019-07-03 22:55发布

问题:

I'm a noob to objective c and I have a daemon code that runs as root (system-wide application) and communicates with a server. Every once in a while, the server will command that root app to fire up an additional application that runs within the logged on user context (without interrupting the user - silently). I have the user context application written as well. What I'm lacking is the understanding on how my root daemon can launch the user context application.

  1. How can my daemon fire it up on demand?
  2. Should I use an agent?
  3. Does it mean the agent will be run at startup?

I've read https://developer.apple.com/library/mac/#technotes/tn2005/tn2083.html as well as http://developer.apple.com/library/mac/#documentation/MacOSX/Conceptual/BPSystemStartup/Chapters/CreatingLaunchdJobs.html but feel I am missing something out.

BTW - the solution should work on 10.6, 10.7, 10.8 ...

Thank you all in advance :o)

回答1:

This will work if you know current logged in user.

sudo chown -R current_loggedin_user myApp
sudo chmod u+s /Contents/MacOS/myApp
system([@"open myApp" UTF8String]);  //inside root process  

If the s group of permissions has the user bit set (corresponding to u+s), then whenever anyone executes that program, the process takes on the privileges of whoever owns it. So current_loggedin_user is owner of myApp, open will run myApp in current_loggedin_user mode.

s The set-user-ID-on-execution and set-group-ID-on-execution bits.

u The user permission bits in the original mode of the file.

But it will throw following error if user is not logged-in.

com.apple.launchd[1]: System: Bug: launchd_core_logic.c:8760 (24498):0
com.apple.launchd[1]: System: job_mig_spawn() can't find its session!
open[24459]: spawn_via_launchd() failed, errno=5 label=[0x0-0x274274].
rootProcess[23127]: LSOpenURLsWithRole() failed with error -10810 for the file /Applications/myApp.app/.