MacアプリケーションからLaunchDaemonplistを読み込もうとしています。ターミナルにロードしようとすると正常にロードされますが、コードを介してロードしようとすると機能しません。
これが私のコードです:
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);
**
私のplist:
**
<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'を使用してデーモンを強制的にロードしようとしましたが、まったくロードされません。奇妙なことに、デーモンがロードされていると言い続けます。今、私はここで何が間違っているのですか..?