【问题标题】:Launchd failure after calling SMJobBless调用 SMJobBless 后启动失败
【发布时间】:2012-09-16 13:40:34
【问题描述】:

我正在开发一个将更新 /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.

我可以从终端手动运行帮助程序,它可以工作。为什么launchd不运行它?我已经运行了 Apple 提供的 SMJobBlessApp 示例应用程序,它运行良好,但我不会,而且我找不到其他的。

供参考,完整代码在这里:https://github.com/varikin/meddo

【问题讨论】:

    标签: cocoa


    【解决方案1】:

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

    【讨论】:

    • 如果我手动运行launchd,我安装的助手会成功启动。只有从 SMJobBless 启动的运行失败。我不知道它是如何工作的。 edit:我认为这可能是帮助程序被复制并启动的时间问题,但我无法确认。
    猜你喜欢
    • 1970-01-01
    • 2015-11-19
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-13
    相关资源
    最近更新 更多