- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
如果由 shell 执行,以下代码可以按预期运行。
但是如果我将此程序设置为用户的 shell 并通过 ssh 进入主机以将此程序作为 shell 执行,read(0, &buf123, 1);
将返回一个 EIO(Input/输出错误):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <regex.h>
#include <curl/curl.h>
#include <readline/readline.h>
int main() {
char *shell = "/bin/bash";
pid_t child;
if ((child = fork()) < 0) {
perror("vfork");
return;
}
if (child == 0) {
execl(shell, shell + 5, "-c", "exec /bin/bash --login", NULL);
perror("execl");
return;
}
wait(NULL);
char buf123[1024];
read(0, &buf123, 1);
printf("::%s::\n", buf123);
}
但是如果将 execl(bash)
更改为非交互式 bash execl(bash -c "id")
或其他程序而不是 bash,则 read(0, &buf123, 1);
会成功。
所以要重现这个错误,需要满足两个条件:
1. execvl() an interactive bash(system() can also reproduce this error)
2. run as a user's shell using ssh
谁能帮我弄清楚为什么以及如何避免这种情况?
下面是strace
的结果:
wait4(-1, NULL, 0, NULL) = 2
--- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=2, si_status=0, si_utime=0, si_stime=0} ---
read(0, 0x7fff7a4c8cb0, 1) = -1 EIO (Input/output error)
fstat(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 1), ...}) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fcd281f3000
write(1, "::Xv\37(\315\177::\n", 11) = 11
exit_group(11) = ?
+++ exited with 11 +++
提前致谢!
最佳答案
我碰巧是因为你的子 shell 是一个登录交互式 shell,所以它控制了终端(将它设置为它的 session 控制终端)。然后您的进程与终端断开连接,无法再在上面读取。
当然,如果您使用非交互式 shell,它不需要对终端的控制,让您的进程保持原样。
阅读有关 POSIX 终端、 session 和进程组的信息。
关于c - 在 execl() bash 返回 eio(输入/输出错误)后从标准输入读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34824579/
我正在维护用户可以在其上部署 Web 应用程序的 Web 服务器软件。 Web 应用程序作为单独的进程运行:Web 服务器启动 Web 应用程序进程,将 HTTP 请求转发到 Web 应用程序,并将
另一个简单的例子: if (wpa_s->mlme.ssid_len == 0) return -EINVAL; 为什么是一元减号?这(通常)是针对成功时返回 >0 和失败时返回 = 0) {
我有一个使用 MongoDB 的 Express 应用程序。 我在开发时使用 Nodemon 重新启动我的进程。为了在我重新启动我的应用程序时关闭我的数据库连接,我调用: db.close() 关于以
我们有一个家庭作业问题,我们必须编写一个带有两个参数的程序,将它们作为 shell 命令执行,将第一个程序的标准输出通过管道传输到第二个程序的标准输入。 这是我的solution对于这个家庭作业问题。
我正在使用 node.js + socket.io + Winston(记录器)编写一个游戏服务器,我的服务器每天都因为这个异常而死机: Error: write EIO at errnoExcept
我使用 NFS 在我的系统上安装了远程磁盘,并且我正在尝试使用 pwrite() API 写入已安装的远程磁盘上的文件。 这种情况并非每次都会发生,但在某些情况下,在执行 I/O 时 pwrite()
我的应用程序基于 Socket.io,具有聊天功能。 我的应用程序部署在红帽开放类次上。 我不断收到http://url/socket.io/?EIO=3&transport=polling&t=Lj
我在 window7-32bit 上用 nodejs + mongodb + Backbone 制作了一个 webApp,直到昨天一切正常。但是今天早上我收到了这个错误: events.js:72
我正在调查 Android 上的一个问题,其中由于未能关闭文件而引发 IOException: java.io.IOException: close failed: EIO (I/O error)
我使用 Node js Express 和 socket.io 以及 apache 作为我的反向代理。以下是我的反向代理配置。 ProxyPass /chatApp http://localhost:
我在 Linux 上运行 NodeJs 服务器,几个小时后它崩溃了: events.js:72 throw er; // Unhandled 'error' event
如果 close(2) 系统调用因 EIO 而失败,文件描述符是否仍会被删除? 如果是,是否无法通过稍后重试来处理虚假 IO 错误?如果不是,应该如何防止文件描述符泄漏? 最佳答案 这是一个棘手的问题
我正在尝试使用forkpty来execvp一个ls/,然后从父进程读取它的输出并写入stdout . 我让它工作了,并且在 Valgrind 下运行它也没有显示任何错误。 编辑:Valgrind 没有
我正在使用 Angular 2 构建一个投资组合网站。我知道有更简单的方法来实现它,但这对我来说也是一种复习,因为我已经有一段时间没有使用 Angular 2 了。 当我尝试使用“ng serve”来
我在 ExpressJS 和 NodeJS 中的后端曾经运行良好。我刚刚意识到日志爆炸了磁盘(后端仍然可用),因为当后端打开时,它一直在尝试: kpi.js GET /socket.io/?EIO=3
wss://www.mysite.ca/socket.io/?EIO=3&transport=websocket 这是 chrome webdevoloper 工具显示套接字 io 的请求 url 的
close(2) 的 Linux 手册页指出: NOTES Not checking the return value of close() is a common but nevertheless
如果由 shell 执行,以下代码可以按预期运行。 但是如果我将此程序设置为用户的 shell 并通过 ssh 进入主机以将此程序作为 shell 执行,read(0, &buf123, 1); 将返
我在一个node.js站点上工作,该站点在端口8443和端口443上使用socket.io。该站点在本地工作,并对指向生产服务器的dns记录使用namecheap。我最近将名称服务器设置为Cloudf
我正在编写一些代码,它获取一个文件,将该文件传递给多个二进制文件之一进行处理,并监视转换过程中的错误。我已经在 OSX 上编写并测试了以下例程,但 linux 因我不清楚的原因而失败。 #run th
我是一名优秀的程序员,十分优秀!