简体   繁体   中英

What kind of files can be run with Launch Daemon?

I'm trying to use SMJobSubmit to launch a Unix executable file, that I made in Xcode, from inside a normal desktop app. (The GUI part is supposed to call a command-line helper tool.)

Here's the code I use to try to submit a job.

- (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 always turns out to be true , but my program never seems to execute. I've executed my program manually through the Terminal and it works perfectly; the helper tool is supposed to write a file to a location on the file system and takes no arguments. However, when I send it to launch daemon, nothing happens. It is marked as executable. I've also verified the path. It's correct.

So, then I'm left wondering if launch daemon can run only certain kinds of files? Unless I messed something up in the code.

The Authorization Reference you created should have the right kSMRightModifySystemDaemons to be able to run an executable using the launchd.

By using the

[authview SetAuthorizationRights:kSMRightModifySystemDaemons]

statement before the

authRef = [[authView authorization] authorizationRef];

things should work. You might need to include <ServiceManagement/ServiceManagement.h> for the definition of kSMRightModifySystemDaemons .

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