cocoa – AuthorizationExecuteWithPrivileges()作为root错误

我是可可的初学者……

我只想在我的Cocoa App中启动Apache和其他进程.

这是我的代码:

    OSStatus myStatus;
    AuthorizationFlags myFlags = kAuthorizationFlagDefaults;
    AuthorizationRef myAuthorizationRef;
    FILE *pipe = NULL;
    myStatus = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment, myFlags, &myAuthorizationRef);
    AuthorizationItem myItems = {kAuthorizationRightExecute, 0, NULL, 0};
    AuthorizationRights myRights = {1, &myItems};
    myFlags = kAuthorizationFlagDefaults | 
    kAuthorizationFlagInteractionAllowed |
    kAuthorizationFlagPreAuthorize |
    kAuthorizationFlagExtendRights;
    myStatus = AuthorizationCopyPrivilegedReference (&myAuthorizationRef,kAuthorizationFlagDefaults);
    myStatus = AuthorizationCopyRights (myAuthorizationRef,&myRights, NULL, myFlags, NULL ); 

    char *tool = "/usr/sbin/apachectl";
    char *args[] = { "start",NULL} ;    

    myStatus = AuthorizationExecuteWithPrivileges(myAuthorizationRef, tool, kAuthorizationFlagDefaults, args, &pipe);
    char c[100];
    int n=fread(c,1,100,pipe);
    c[n] = '\0';
    NSLog(@"%s\n",c);

theResult:这个操作需要root当我运行’whoami’时,我是’root’但是当我运行getuid()时,我’501’……

我尝试使用setuid(0);但它没有设置!!
你能帮助我吗?谢谢

最佳答案
我将使用/ etc / sudoers中的/usr/sbin / apachectl的uid = 501访问权限为用户定义访问权限,并在代码中执行“sudo /usr/sbin / apachectl”而不是/usr/sbin / apachectl.

转载注明原文:cocoa – AuthorizationExecuteWithPrivileges()作为root错误 - 代码日志