Error in Loading Daemon Plist programmatically

Vgay

Registered
I'm trying to a Load LaunchDaemon plist from my Mac Application.When i try to load it in terminal it loads successfully, but when i try to load it thru my code, it doesn't work.

Here is my code :
Code:
    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];
    }
    
    [COLOR="#008000"]// kAuthorizationRightExecute == "system.privilege.admin"[/COLOR]
    AuthorizationItem right = {kAuthorizationRightExecute, 0, NULL, 0};
    AuthorizationRights rights = {1, &right};
    AuthorizationFlags flags = kAuthorizationFlagDefaults |
    kAuthorizationFlagInteractionAllowed |
    kAuthorizationFlagPreAuthorize |
    kAuthorizationFlagExtendRights;
  
 [COLOR="#008000"] // Call AuthorizationCopyRights to determine or extend the allowable rights.[/COLOR]
    
    status = AuthorizationCopyRights(authorizationRef, &rights, NULL, flags, NULL);
    if (status != errAuthorizationSuccess)
    {
        NSString *err = [NSString stringWithFormat:@"Sorry, we need Administrator priviliges to Continue"];
        [self ShowErrorAndExit:err];
    }

 [COLOR="#008000"]// After getting the Authorization Reference, executing the launchctl to load my LaunchDaemon[/COLOR]
    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);

My plist :
Code:
         <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. And After system restart the daemon starts fine. The plist loads fine in terminal. I have even tried with '-w' & '-F' to force load the daemon, but it doesn't loads at all. The weird thing is it just keeps saying that the daemon is loaded.
Now what am i doing wrong here ..?
 
Back
Top