【问题标题】:What kind of files can be run with Launch Daemon?Launch Daemon 可以运行哪些类型的文件?
【发布时间】:2012-09-24 04:11:03
【问题描述】:

我正在尝试使用SMJobSubmit 从普通桌面应用程序中启动我在 Xcode 中创建的 Unix 可执行文件。 (GUI 部分应该调用命令行辅助工具。)

这是我用来尝试提交工作的代码。

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    [authView setString:kSMRightModifySystemDaemons];
    [authView setAutoupdate:YES];
    [authView setDelegate:self];
}

- (IBAction)pressedButton:(id)sender
{
    /*
      authView is an instance of SFAuthorizationView. At this point, the user
      already clicked the padlock and entered his password.
    */
    authRef = [[authView authorization] authorizationRef];

    //toolPath points to the file in the app's Resource folder.
    NSArray* call = [NSArray arrayWithObject:toolPath];

    NSDictionary* jobSpec = [NSDictionary dictionaryWithObjectsAndKeys:
                             call, @"ProgramArguments",
                             jobLabel, @"Label",
                             [NSNumber numberWithBool:YES], @"RunAtLoad",
                             nil];

    CFErrorRef submitError = nil;
    BOOL submitResult = SMJobSubmit(kSMDomainSystemLaunchd,
                                    (__bridge CFDictionaryRef)(jobSpec),
                                    authRef,
                                    &submitError);

    if(!submitResult)
    {
        NSLog(@"Job submit failed. %@", submitError);
        return;
    }
}   

submitResult 总是变成true,但我的程序似乎永远不会执行。我已经通过终端手动执行了我的程序,它运行良好;辅助工具应该将文件写入文件系统上的某个位置并且不带任何参数。但是,当我将它发送到启动守护程序时,什么也没有发生。它被标记为可执行。我也验证了路径。没错。

那么,我想知道启动守护程序是否只能运行某些类型的文件?除非我在代码中搞砸了。

【问题讨论】:

    标签: objective-c macos cocoa launchd


    【解决方案1】:

    您创建的授权参考应该具有正确的kSMRightModifySystemDaemons 以便能够使用launchd 运行可执行文件。

    通过使用

    [authview SetAuthorizationRights:kSMRightModifySystemDaemons]

    声明之前的

    authRef = [[authView authorization] authorizationRef];

    事情应该会奏效。对于kSMRightModifySystemDaemons 的定义,您可能需要包含<ServiceManagement/ServiceManagement.h>

    【讨论】:

    • 我已经发布了更多我的代码。我的applicationDidFinishLaunching 方法中有[authView setString:kSMRightModifySystemDaemons];。 API 说:“在正确显示视图之前,必须调用此方法或 setAuthorizationRights: 方法。”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-11
    • 2011-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-24
    相关资源
    最近更新 更多