将 control+c (SIGINT) 发送到 Objective-C 中的 NSPIPE

发布于 2024-08-29 22:21:34 字数 572 浏览 6 评论 0原文

我正在尝试终止通过 NSTask 生成的 openvpn 任务。

我的问题:

我应该将 ctrl+c (SIGINT) 发送到我的 NSTask 的输入 NSPipe 吗?

inputPipe = [NSPipe pipe]; 
taskInput = [inputPipe fileHandleForWriting];
NSString dataString = @"\cC";
[taskInput writeData:[dataString dataUsingEncoding: [NSString defaultCStringEncoding]]];

或者,我正在考虑使用kill(pid,SIGINT);但它会更复杂,因为进程 ID 必须通过绕道来确定([task processIdentifier] 在这里没有帮助) - 原始 NSTask 调用:

/bin/bash -c sudo -S | mypassword ....

这不好,我知道,但它只被调用一次并且 sudo 密码已经在这种情况下输入了。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

九公里浅绿 2024-09-05 22:21:34

Control-C 和 SIGINT 不是一回事。

对于键盘输入,操作系统中有一个“tty 驱动程序”,它可以执行诸如回显字符、处理删除键等操作。它还可以识别 control-C(或任何 intr 字符)并向进程发送 SIGINT。

当您有到另一个进程的管道时,不涉及 tty 驱动程序*,因此 control-C 只是作为普通数据传递。

您需要找到 pid 并直接向其发送 SIGINT。当然,您实际上可能没有权限向其发送信号(因为您使用 sudo 以 root 身份运行其他任务)....

*除非您使用伪终端 (pty),而 NSPipe 没有无论如何你也不想这样做:-)

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 :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文