Как загрузить список LaunchDaemon из моего приложения Mac

Я пытаюсь загрузить plist LaunchDaemon из своего приложения Mac. Когда я пытаюсь загрузить его в терминал, он загружается успешно, но когда я пытаюсь загрузить его через свой код, он не работает.

Вот мой код:

OSStatus status;
  AuthorizationRef authorizationRef;
  status = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment, kAuthorizationFlagDefaults, &authorizationRef);

  if (status != errAuthorizationSuccess)
  {
     NSString *err = [NSString stringWithFormat:@"Error Creating Initial Authorization : %d", status];
       [self ShowErrorAndExit:err];
  }

  //kAuthorizationRightExecute == "system.privilege.admin"
   AuthorizationItem right = {kAuthorizationRightExecute, 0, NULL, 0};
   AuthorizationRights rights = {1, &right};
   AuthorizationFlags flags = kAuthorizationFlagDefaults |
   kAuthorizationFlagInteractionAllowed |
   kAuthorizationFlagPreAuthorize |
   kAuthorizationFlagExtendRights;

   // Call AuthorizationCopyRights to determine or extend the allowable rights.
   status = AuthorizationCopyRights(authorizationRef, &rights, NULL, flags, NULL);
   if (status != errAuthorizationSuccess)
    {
        NSString *err = [NSString stringWithFormat:@"Sorry, we need Administrator priviliges to Continue"];
        [self ShowErrorAndExit:err];
    }

// After getting the Authorization Reference, executing the launchctl to load my LaunchDaemon
     char *daemonplist="/Library/LaunchDaemons/com.myappdaemon.plist";
     char *launchctl="/bin/launchctl";
     char *launchdArgs[]={"load",daemonplist,NULL};
     FILE *ldpipe=NULL;
     OSStatus;
    status=AuthorizationExecuteWithPrivileges(authorizationRef,launchctl,kAuthorizationFlagDefaults,launchdArgs,&ldpipe);
        if(status!=errAuthorizationSuccess)
    {
    NSLog(@"Error:%d",status);
    }
    if(status==errAuthorizationSuccess)
    {
        NSLog(@" succesfully loaded the daemon plist);
    }
    status=AuthorizationFree(authorizationRef,kAuthorizationFlagDestroyRights);

**

Мой плист:

**

<key> Label</key>
    <string>com.myappdaemon</string>
    <key>Program</key>
    <string>/Applications/myapplication.app/Contents/MacOS/myappdaemon</string>
    <key>ProgramArguments</key>
    <array>
    <string>/Applications/myapplication.app/Content/MacOs/myappdaemon</string>
    </array>
    <key>WorkingDirectory</key>
    <string>/Library/myapplication</string>
    <key>StandardOutPath</key>
    <string>/dev/null</string>
    <key>StandardErrorPath</key>
    <string>/dev/null</string>
    <key>RunAtLoad</key>
    </true>
    <key>keepAlive</key>
    <true/>

Thing is it shows the daemon plist is loaded successfully, but it's not.

И после перезагрузки системы демон запускается нормально. Plist отлично загружается в терминале. Я даже пытался с помощью «-w» и «-F» принудительно загрузить демона, но он вообще не загружается. Странно то, что он просто продолжает говорить, что демон загружен. Теперь, что я делаю неправильно здесь ..?


person Vgay    schedule 18.03.2013    source источник


Ответы (1)


AuthorizationExecuteWithPrivileges Устарело в OS X v10.7. Вы можете использовать applescript

do shell script "launchctl load /Library/LaunchDaemons/com.yourcompany.app.plist" with administrator privileges
person Parag Bafna    schedule 31.05.2013