[cs631apue] exec error

Hem Shah hshah42 at stevens.edu
Tue Nov 26 23:03:26 EST 2019


Hello Professor,

It worked with the third argument! I modified the cgi-script to print out the arguments passed to it.

I modified my code and added some statement to printf:

#include <unistd.h>

#include <stdlib.h>

#include <string.h>

#include <errno.h>

#include <stdio.h>


int

main(int argc, char **argv) {

        (void) setenv("something", "something", 1);

        printf("testing\n");

        (void) execl("/cgi-bin/something.cgi", (char *) 0);

        printf("%s\n", strerror(errno));

        return 1;

}

The script got "testing" as the first argument, the output is:

localhost# ./a.out

testing


Content-Type: text/html


ENV=/root/.shrc

BLOCKSIZE=1k

PWD=/root/programs/sws/APUE-Group

MAIL=/var/mail/root

HOME=/root

PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/pkg/sbin:/usr/pkg/bin:/usr/X11R7/bin:/usr/local/sbin:/usr/local/bin

HOST=localhost

SSH_CONNECTION=10.0.2.2 62926 10.0.2.15 22

SSH_TTY=/dev/pts/1

TERM=xterm-256color

OLDPWD=/root/programs/sws

USER=root

SSH_CLIENT=10.0.2.2 62926 22

LOGNAME=root

SHELL=/bin/sh

something=something

Similarly, I ran the code without setenv and printf, the script printed out garbage values as the first argument.

Regards.
Hem Shah
________________________________
From: Jan Schaumann <jschauma at stevens.edu>
Sent: Tuesday, November 26, 2019 7:48 PM
To: Hem Shah <hshah42 at stevens.edu>
Cc: cs631apue at lists.stevens.edu <cs631apue at lists.stevens.edu>
Subject: Re: [cs631apue] exec error

Hem Shah <hshah42 at stevens.edu> wrote:
> int
> main(int argc, char **argv) {
>         (void) setenv("something", "something", 1);
>         (void) execl("/cgi-bin/something.cgi", (char *) 0);
>         printf("%s\n", strerror(errno));
>         return 1;
> }

Are you compiling this program with "-Wall -Werror"?

You do not have enough arguments in your call to
execl(3); the function requires at least a third
argument, per the manual page:

     The const char *arg and subsequent ellipses in the execl(), execlp(),
     execlpe(), and execle() functions can be thought of as arg0, arg1, ...,
     argn.  Together they describe a list of one or more pointers to NUL-
     terminated strings that represent the argument list available to the
     executed program.  The first argument, by convention, should point to the
     file name associated with the file being executed.  The list of arguments
     must be terminated by a NULL pointer.

Use

        execl("/cgi-bin/something.cgi", "something.cgi", (char *) 0);

instead, and it should work.

(I don't know yet why the program works if you don't
call setenv(3) or if you add a printf(3) after
setenv(3), but calling execl(3) without a third
argument means you have undefined behavior, so
anything is fair game.)

-Jan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.stevens.edu/pipermail/cs631apue/attachments/20191127/4a5d433a/attachment.html>


More information about the cs631apue mailing list