• Uncategorized

About unix : Calling-exec-returns-errno-14-bad-address-with-absolute-path

Question Detail

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

Question Answer

execl("simple.cgi","simple", NULL);

The null is needed because execl() is a varargs – function.

    execl("/home/dvd/nwebdir/simple.cgi", &ctx->uri[1], (char *)0);

The last argument to execl() must be a null char *. You can usually get away with writing NULL instead of (char *)0, but it might not produce the correct result if you have #define NULL 0 and you are on a machine where sizeof(int) != sizeof(char *), such as a 64-bit system.

BTW, either you copied the code incorrectly or it has logic error. Fork() returns non-zero to parent process, not child one, so condition shall be reverted. (There is no comment button here so making answer.)

You may also like...

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.