Re: Running process as root from Cocoa
Re: Running process as root from Cocoa
- Subject: Re: Running process as root from Cocoa
- From: "Kyle Sluder" <email@hidden>
- Date: Tue, 29 Jan 2008 00:59:51 -0500
On Jan 29, 2008 12:07 AM, Mitchell Hashimoto <email@hidden> wrote:
> NSString *fullPath = @"/Applications/Program.app/Contents/MacOS/Program";
This seems odd. Is Program.app a separate application from your own
(e.g. you're building a launcher to an existing, but separate, app)?
If so, use NSWorkspace to get the path to the application you want.
The Most Correct Way(TM) to do this would be -[NSWorkspace
absolutePathForAppBundleWithIdentifier] (which you would pass the
app's bundle identifier, a string in the form
com.yourcompany.AppName), but -[NSWorkspace fullPathForApplication:]
may suit you just fine (in which case you would pass "AppName", that
is the app bundle's name without the extension).
Once you have the path to the bundle, use NSBundle to get the path to
the actual executable inside the bundle and append it to the string
you got above. This is the path you pass to Authorization Services.
> AuthorizationItem myItems[1];
>
> myItems[0].name = kAuthorizationRightExecute;
> myItems[0].valueLength = [fullPath length];
> myItems[0].value = [fullPath cStringUsingEncoding:NSASCIIStringEncoding];
> myItems[0].flags = 0;
Your use of -[NSString length] and -[NSString cStringUsingEncoding:]
is troublesome. NSString has an instance method specifically for what
you're trying to do: -fileSystemRepresentation. It returns a C string
that's appropriate to be passed to the filesystem, regardless of funky
Unicode characters in paths. You can use the C strlen() function to
get the length of a C string (don't use -[NSString length] for this;
it may not match up).
HTH, and remember to use your powers for good, not evil. Cheaters
just ruin the fun for the rest of us.
--Kyle Sluder
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden