I am trying to terminate an openvpn task, spawned via NSTask.
My question:
Should I send ctrl+c (SIGINT) to the input NSPipe for my NSTask?
inputPipe = [NSPipe pipe];
taskInput = [inputPipe fileHandleForWriting];
NSString dataString = @"\cC";
[taskInput writeData:[dataString dataUsingEncoding: [NSString defaultCStringEncoding]]];
Alternatively, I was thinking about using kill( pid, SIGINT ); but it would be much more complicated since the process ID has to be determined via a detour ([task processIdentifier] does not help here) - the original NSTask calls:
/bin/bash -c sudo -S | mypassword ....
That's not nice, I know but it is only called once and the sudo password has been entered in that case already.
Control-C and SIGINT are not the same thing.
In the case of keyboard input, there's a "tty driver" in the OS that does things like echoing characters, processing delete
keys, and so forth. It's also the thing that recognizes the control-C (or whatever your intr
character is) and sends a SIGINT to the process.
When you have a pipe to another process, there's no tty driver involved*, so control-C is just passed through as ordinary data.
You will need to locate the pid and send a SIGINT directly to it. Of course, you might not actually have the permissions to do send it the signal (because you used sudo to run the other task as root)....
*unless you're using a pseudo-terminal (pty), which NSPipe doesn't and you don't want to do anyway :-)