调用exec返回带有绝对路径的errno 14(错误地址) [英] Calling exec returns errno 14 (bad address) with absolute path
问题描述
为课程制作一个简单的cgi服务器。为此,我必须制作一个fork / exec来启动cgi处理程序,问题是exec不断返回errno14。我已经尝试了以下代码的独立版本,无论是否使用绝对值,都可以使用路径。
in making a simple cgi server for a course. To do that in some point I have to make a fork/exec to launch the cgi handler, the problem is that the exec keep returning errno 14. I've tried the following code in a standalone version an it works with and without the absolute path.
这是代码:
static void _process_cgi(int fd, http_context_t* ctx)
{
pid_t childProcess;
int ret;
char returnValue[1024];
log(LOG, "calling cgi", &ctx->uri[1], 0);
if((childProcess = fork()) != 0)
{
///
/// Set the CGI standard output to the socket.
///
dup2(fd, STANDARD_OUTPUT);
//ctx->uri = "/simple.cgi"
execl("/home/dvd/nwebdir/simple.cgi",&ctx->uri[1]);
sprintf(returnValue,"%d",errno);
log(LOG, "exec returned ", returnValue, 0);
return -1;
}
ret = waitpid(childProcess,NULL,0);
sprintf(returnValue,"%d",ret);
log(LOG, "cgi returned", returnValue, 0);
}
这是服务器在到达我的代码之前通过的sys调用列表(按顺序):
-chdir
-叉
-setpqrp
-叉
我不知道这是否有意义,但是在我的测试程序中没有chdir或setpqrp。
Here is the list of sys calls that the server passes before reaching my code (in order): - chdir - fork - setpqrp - fork I don't know if this is relevant or not, but in my test program I don't have chdir nor setpqrp.
测试代码如下:
pid_t pid;
if ((pid = fork()) != 0)
{
execl("simple.cgi","simple");
//execl("/home/dvd/nwebdir/simple.cgi","simple");
return 0;
}
printf("waiting\n");
waitpid(pid, NULL, 0);
printf("Parent exiting\n");
注意我已经在服务器代码中同时尝试了execl和execlp。
Note I've tried both execl and execlp in the server code.
您可以在这里找到基本的服务器实现(没有CGI),我所做的唯一更改是在Web函数中:
http://www.ibm.com/developerworks/systems/library/es-nweb/index.html
You can find the basic server implementation (without CGI) in here, the only changes I made was in the web funcion: http://www.ibm.com/developerworks/systems/library/es-nweb/index.html
致谢
推荐答案
execl("simple.cgi","simple", NULL);
因为execl()是一个varargs函数,所以需要null。
The null is needed because execl() is a varargs - function.
这篇关于调用exec返回带有绝对路径的errno 14(错误地址)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!