调用SMJobBless后启动失败

时间:2012-09-25 04:19:22

标签: cocoa

我正在开发一个将更新/ etc / hosts的应用程序。为此,我发现评估权限的首选方法是通过SMJobBless安装帮助工具。以下代码有效:

    BOOL result = NO;

AuthorizationItem authItem      = { kSMRightBlessPrivilegedHelper, 0, NULL, 0 };
AuthorizationRights authRights  = { 1, &authItem };
AuthorizationFlags flags        =   kAuthorizationFlagDefaults              |
                                    kAuthorizationFlagInteractionAllowed    |
                                    kAuthorizationFlagPreAuthorize          |
                                    kAuthorizationFlagExtendRights;

AuthorizationRef authRef = NULL;
CFErrorRef error = NULL;

OSStatus status = AuthorizationCreate(&authRights, kAuthorizationEmptyEnvironment, flags, &authRef);
if (status == errAuthorizationSuccess) {
    result = SMJobBless(kSMDomainSystemLaunchd, (CFStringRef)@"com.fictitiousnonsense.MeddoHelper", authRef, &error);
} else {
    NSLog(@"Failed to authorize");
}

if (error != NULL) {
    NSLog(@"Error: %@", error);
} else {
    NSLog(@"I think it worked");
}

我的帮助工具com.fictitiousnonsense.MeddoHelper安装到/Library/PrivilegedHelperTools/com.fictitiousnonsense.MeddoHelper,并且它的plist文件安装到/ Library / LaunchDaemons,所有这些都根据文档。问题是,这会出现在我的控制台中:

9/24/12 11:04:41.237 PM launchdadd[9082]: Could not open /Library/PrivilegedHelperTools/com.fictitiousnonsense.MeddoHelper (open() error 2: No such file or directory)
9/24/12 11:04:41.237 PM launchdadd[9082]: FAILURE: The path /Library/PrivilegedHelperTools/com.fictitiousnonsense.MeddoHelper does not exist on-disk.

我可以从终端手动运行帮助程序,它可以工作。为什么不启动它呢?我已经运行了Apple提供的SMJobBlessApp示例应用程序,它运行正常,我不会,我找不到与众不同。

供参考,整个代码在此处:https://github.com/varikin/meddo

1 个答案:

答案 0 :(得分:0)

您可以在命令行上使用dtruss来跟踪launchd。它将显示由launchd执行的所有系统调用,其中一个应该尝试访问您的特权帮助程序。跟踪应该显示哪些系统调用正确失败,这使得launchd认为帮助程序不在那里。我猜这是某种许可问题,顺便说一句。