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.
Here’s the code:
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);
}
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.
The test code is the following:
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");
Note I’ve tried both execl and execlp in the server code.
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
Regards
The null is needed because execl() is a varargs – function.