简体   繁体   中英

Mountain Lion Privileged Helper Daemon status

I'm building an app for 10.8+ which among other things has to patch files. Because of this functionality I have created a "PrivilegedHelperTool" and installed as KeepLive = YES RunAtLoad = YES (so it's always running). I also use XPC over mach for communication to a GUI app (menubar)

I'd like to be able to check if the "PrivilegedHelperTool" is installed, and running and so far the best way to check installation i've found is look for the plist in /Library/LaunchDaemons/ and check the binary is in /Library/PrivilegedHelperTools .

Is this really the best way?

Also I have found no reliable way to check if the PrivilegedHelperTool(running as root) without already being root (eg. sudo launchctl list)

I added a wrapper which uses launch.h (/usr/include/launch.h) http://brockerhoff.net/blog/2009/02/02/cocoa-musings-pt-3/ but it only returns user processes and getting the end user to escalate privileges just to check some "plugin" is running seems wrong.

Is it true that the best I can hope for is trying to talk to my service and if its not responding assume it's "damaged or not running"? ( http://mac-os-forge.2317878.n4.nabble.com/Programmatic-interface-to-launchctl-and-some-other-questions-OS-X-10-5-tp189494p189496.html )

when you install the background helper daemon, you need to be root. So either your installer does the job of installing the daemon with appropriate privileges or when you launch the application, you ask the user to authorize himself as administrator so you can install the daemon on the fly.

This is a code snippet to become root out of an application. it will prompt the user with the normal username/password dialogbox you see also when installing something from a pkg.

OSStatus myStatus;
uid_t   uid = -1;
AuthorizationRights myRights;
AuthorizationFlags myFlags;
AuthorizationItem myItems[1];


uid = geteuid();
if(uid != 0)
{
    myItems[0].name = "com.whatever";
    myItems[0].valueLength = 0;
    myItems[0].value = NULL;
    myItems[0].flags = 0;
    myRights.count = sizeof (myItems) / sizeof (myItems[0]);
    myRights.items = myItems;
    myFlags = kAuthorizationFlagDefaults |
        kAuthorizationFlagInteractionAllowed |
        kAuthorizationFlagExtendRights |
        kAuthorizationFlagPartialRights;
    myStatus = AuthorizationCreate (&myRights, kAuthorizationEmptyEnvironment,myFlags,NULL);
}

You can use sysctl to get all running process. Have a look at my answer .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM