views:

359

answers:

3

If I launched a shell script using AuthorizationExecuteWithPrivileges what would be the easiest way to kill the script and any other processes that it spawned.

Thanks

A: 

It's running as root, so you can't kill it from a regular-user process. You're going to have to ask it nicely to exit on its own.

Peter Hosey
Thanks, how would I ask it to exit nicely? I guess I could always use another "AuthorizationExecuteWithPrivileges" command to kill it, but it would be highly inconvenient for the user to have to enter an admin pass to close the process...
macatomy
Yes it would. You would use any form of IPC.
Peter Hosey
Sorry for my ignorance but what is IPC? I researched it but there are many different things it could mean. Thanks
macatomy
Interprocess communication. One high-level way is Distributed Objects (http://developer.apple.com/documentation/Cocoa/Conceptual/DistrObjects/); a lower-level way is Mach ports, and an even more primitive way (if not exactly lower-level) is UNIX-domain sockets.
Peter Hosey
A: 

Apple has sample code that uses stdout to pass the PID back to the caller.

jeffamaphone
A: 

Use the communications pipe that AuthorizationExecuteWithPrivileges() returns by reference in its last argument, FILE **communicationPipe, to send a message to the child process that asks it to take itself and its descendants out. It can then kill itself and all its descendants using kill(0, SIGINT), or, if more drastic measures are required, SIGKILL.

The message you use can be as simple as closing the file while the child waits for the file to close; at that point, it knows you're done talking to it and it's time to take itself out.

There are some caveats about the descendants that will actually receive this message, for which see the kill(2) manpage. The caveats mostly won't matter so long as the process you started via AEWP hasn't dropped privileges, though one implicit issue is that this approach won't work if any child processes have put themselves in a new process group.

Jeremy W. Sherman