• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Filling arguments for AuthorizationExecuteWithPrivileges (very newbie)
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Filling arguments for AuthorizationExecuteWithPrivileges (very newbie)


  • Subject: Re: Filling arguments for AuthorizationExecuteWithPrivileges (very newbie)
  • From: Brendan Younger <email@hidden>
  • Date: Thu, 21 Jun 2001 13:45:47 -0500

char* args[2];
[[prefix stringByAppendingString:minutes] getCString:args[1]];

-getCString expects args[1] to be the pointer to the beginning of a buffer and you haven't allocated any space for it. Better to use
args[1] = [[prefix stringByAppendingString:minutes] cString];
A note with this usage, the cString will at some point be freed (it's autoreleased) and therefore you should be careful how long you use it. It should be fine in the context of execShutdownCountdownAtTime:

Also, a note on your array usage.
char* args[2];
produces an array of actual pointers. You're using them as though they were arrays of characters.
For instance:
args[0] = "-h";
is bad because you have now specified that the first argument is at whatever memory location the ASCII code for "-h" is.
Also, char* args[2]; produces an array with only two elements, args[0] and args[1]. I know it's counterintuitive, but there is no args[2]. If you use it, you're trampling on other memory that might have a different purpose.

In short, here's what you're looking for:
- (void)execShutdownCountdownAtTime:(NSString *)minutes
{
NSString *prefix = @"+";
char* args[2];
char argumentOne[] = "-h";
OSStatus err = 0;

args[0] = &(argumentOne[0]);
args[1] = [[prefix stringByAppendingString:minutes] cString];

NSLog(@"Parameter passed: %s\nArguments: %s %s",minutes,args[0],args[1]);
err = AuthorizationExecuteWithPrivileges(authorizationRef,
"/sbin/shutdown",0,args,NULL);

if(err!=0)
{
NSBeep();
NSLog(@"Error %d in AuthorizationExecuteWithPrivileges",err);
}
}

Don't worry about all the mistakes, I can vividly remember when I was learning this stuff too. Have fun, live long and prosper, and may the hair on your toes never fall out!

Brendan Younger


  • Follow-Ups:
    • Re: Filling arguments for AuthorizationExecuteWithPrivileges (very newbie)
      • From: Brian Hill <email@hidden>
References: 
 >Filling arguments for AuthorizationExecuteWithPrivileges (very newbie) (From: Jorge Salvador Caffarena <email@hidden>)

  • Prev by Date: Re: WWDC Dev Tools / Software update
  • Next by Date: Re: Preferences using NSUserDefaults (newbie)
  • Previous by thread: Filling arguments for AuthorizationExecuteWithPrivileges (very newbie)
  • Next by thread: Re: Filling arguments for AuthorizationExecuteWithPrivileges (very newbie)
  • Index(es):
    • Date
    • Thread