我是可可的初学者...
我只想在我的 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); 但它没有设置!
你能帮助我吗?
谢谢