我在构建“中间人”记录器时遇到了很多麻烦 - 目的是将它放在 /usr/bin 中项目上方的路径上,并捕获进出应用程序的所有内容。(黑盒第 3 方应用程序由于某种原因无法通过 FTP。)一旦运行,中间人将分叉,将标准输出和标准输入重定向到/从父级控制的管道,然后执行 /usr/bin 中的程序。(硬编码;是的,我知道,我很糟糕。)
但是,一旦我运行 poll(),事情就会变得很奇怪。我失去了我的日志文件的句柄,来自孩子的输出管道上的投票引发了错误,猫和狗开始生活在一起,等等。
任何人都可以对此有所了解吗?
这是我目前拥有的... 有问题的 poll() 标有非缩进注释,以便于定位。
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <poll.h>
#include <time.h>
#include <sys/types.h>
#include <fcntl.h>
#define MAX_STR_LEN 1024
static int directionFlag; /* 0 = input, 1 = output */
static int eofFlag;
/* Splits the next char from the stream inFile, with extra
information logged if directionFlag swaps */
void logChar(int inFilDes, int outFilDes, FILE *logFile, int direction)
{
char inChar = 0;
if(read(inFilDes, &inChar, sizeof(char)) > 0)
{
if(direction != directionFlag)
{
directionFlag = direction;
if(direction)
{
fprintf(logFile, "\nOUTPUT: ");
} else {
fprintf(logFile, "\nINPUT: ");
}
}
write(outFilDes, &inChar, sizeof(char));
fputc(inChar, stderr);
fputc(inChar, logFile);
} else {
eofFlag = 1;
}
return;
}
int main(int argc, char* argv[])
{
pid_t pid;
int childInPipe[2];
int childOutPipe[2];
eofFlag = 0;
/* [0] is input, [1] is output*/
if(pipe(childInPipe) < 0 || pipe(childOutPipe) < 0) {
fprintf(stderr,"Pipe error; aborting\n");
exit(1);
}
if((pid = fork()) == -1){
fprintf(stderr,"Fork error; aborting\n");
exit(1);
}
if(pid)
{
/*Parent process*/
int i;
int errcode;
time_t rawtime;
struct tm * timeinfo;
time(&rawtime);
timeinfo=localtime(&rawtime);
struct pollfd pollArray[2] = {
{ .fd = 0, .events = POLLIN, .revents = 0 },
{ .fd = childOutPipe[0], .events = POLLIN, .revents = 0 }
};
/* Yet again, 0 = input, 1 = output */
nfds_t nfds = sizeof(struct pollfd[2]);
close(childInPipe[0]);
close(childOutPipe[1]);
/* We don't want to change around the streams for this one,
as we will be logging everything - and I do mean everything */
FILE *logFile;
if(!(logFile = fopen("/opt/middleman/logfile.txt", "a"))) {
fprintf(stderr, "fopen fail on /opt/middleman/logfile.txt\n");
exit(1);
}
fprintf(logFile, "Commandline: ");
for(i=0; i < argc; i++)
{
fprintf(logFile, "%s ", argv[i]);
}
fprintf(logFile, "\nTIMESTAMP: %s\n", asctime(timeinfo));
while(!eofFlag)
{
// RIGHT HERE is where things go to pot
errcode = poll(pollArray, nfds, 1);
// All following fprintf(logfile)s do nothing
if(errcode < 0) {
fprintf(stderr, "POLL returned with error %d!", errcode);
eofFlag = 1;
}
if((pollArray[0].revents && POLLERR) & errno != EAGAIN ) {
fprintf(stderr, "POLL on input has thrown an exception!\n");
fprintf(stderr, "ERRNO value: %d\n", errno);
fprintf(logFile, "POLL on input has thrown an exception!\n");
eofFlag = 1;
} else if(pollArray[0].revents && POLLIN) {
logChar(pollArray[0].fd, childInPipe[1], logFile, 0);
} else if((pollArray[1].revents && POLLERR) & errno != EAGAIN ) {
fprintf(stderr, "POLL on output has thrown an exception!\n");
fprintf(stderr, "ERRNO value: %d\n", errno);
fprintf(logFile, "POLL on output has thrown an exception!\n");
eofFlag = 1;
} else if(pollArray[1].revents && POLLIN) {
logChar(pollArray[1].fd, 1, logFile, 1);
}
}
fclose(logFile);
}
else
{
/*Child process; switch streams and execute application*/
int i;
int catcherr = 0;
char stmt[MAX_STR_LEN] = "/usr/bin/";
close(childInPipe[1]);
close(childOutPipe[0]);
strcat(stmt, argv[0]);
if(dup2(childInPipe[0],0) < 0) {
fprintf(stderr, "dup2 threw error %d on childInPipe[0] to stdin!\n", errno);
}
// close(childInPipe[0]);
if(dup2(childOutPipe[1],1) < 0)
{
fprintf(stderr, "dup2 threw error %d on childInPipe[1] to stdout!\n", errno);
}
/* Arguments need to be in a different format for execv */
char* args[argc+1];
for(i = 0; i < argc; i++)
{
args[i] = argv[i];
}
args[i] = (char *)0;
fprintf(stderr, "Child setup complete, executing %s\n", stmt);
fprintf(stdout, "Child setup complete, executing %s\n", stmt);
if(execv(stmt, args) == -1) {
fprintf(stderr, "execvP error!\n");
exit(1);
}
}
return 0;
}
编辑 09 年 6 月 23 日下午 12:20
修复后,我尝试通过该程序运行“横幅”,这是我得到的输出......
Child setup complete, executing /usr/bin/banner
POLL on output has thrown an exception!
ERRNO value: 0
日志文件具有以下内容:
Commandline: banner testing
TIMESTAMP: Tue Jun 23 11:21:00 2009
ERRNO 中有一个 0 的原因是因为 poll() 返回得很好;是 pollArray[1].revents 返回错误,这意味着 childOutPipe[0] 被轮询为有错误。据我所知,logChar() 永远不会被调用。
我将尝试将 poll() 拆分为两个不同的调用。
好的,当我 poll() 时——即使在标准输入上,它也不会返回错误消息——它会扼杀我写入日志文件的能力。此外,我发现 while() 循环在输出轮询返回管道错误之前运行了几次。我越来越相信 poll() 只是一个失败的原因。
在 poll() 之后,每次写入 logFile 的尝试都会失败,即使是成功的 poll(),也会将 errno 设置为“错误文件号”。这真的不应该发生。老实说,我看不出它会如何影响我的文件句柄。
好吧,看来我是个白痴。谢谢你让我直截了当;我假设 nfds 是字节大小,而不是数组大小。这是固定的,瞧!它不再杀死我的 logFile 句柄。