<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<style type="text/css" style="display:none;"> P {margin-top:0;margin-bottom:0;} </style>
</head>
<body dir="ltr">
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0); background-color: rgb(255, 255, 255);">
Hello Professor,</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0); background-color: rgb(255, 255, 255);">
<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0); background-color: rgb(255, 255, 255);">
It worked with the third argument! I modified the cgi-script to print out the arguments passed to it.</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0); background-color: rgb(255, 255, 255);">
<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0); background-color: rgb(255, 255, 255);">
I modified my code and added some statement to printf:</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0); background-color: rgb(255, 255, 255);">
<p style="font: 11px Menlo; margin: 0px; color: rgb(0, 0, 0);"><span style="font-variant-ligatures: no-common-ligatures; font-variant-ligatures: no-common-ligatures">#include <unistd.h></span></p>
<p style="font: 11px Menlo; margin: 0px; color: rgb(0, 0, 0);"><span style="font-variant-ligatures: no-common-ligatures; font-variant-ligatures: no-common-ligatures">#include <stdlib.h></span></p>
<p style="font: 11px Menlo; margin: 0px; color: rgb(0, 0, 0);"><span style="font-variant-ligatures: no-common-ligatures; font-variant-ligatures: no-common-ligatures">#include <string.h></span></p>
<p style="font: 11px Menlo; margin: 0px; color: rgb(0, 0, 0);"><span style="font-variant-ligatures: no-common-ligatures; font-variant-ligatures: no-common-ligatures">#include <errno.h></span></p>
<p style="font: 11px Menlo; margin: 0px; color: rgb(0, 0, 0);"><span style="font-variant-ligatures: no-common-ligatures; font-variant-ligatures: no-common-ligatures">#include <stdio.h></span></p>
<p style="font: 11px Menlo; margin: 0px; min-height: 13px; color: rgb(0, 0, 0);">
<span style="font-variant-ligatures: no-common-ligatures; font-variant-ligatures: no-common-ligatures"></span><br>
</p>
<p style="font: 11px Menlo; margin: 0px; color: rgb(0, 0, 0);"><span style="font-variant-ligatures: no-common-ligatures; font-variant-ligatures: no-common-ligatures">int</span></p>
<p style="font: 11px Menlo; margin: 0px; color: rgb(0, 0, 0);"><span style="font-variant-ligatures: no-common-ligatures; font-variant-ligatures: no-common-ligatures">main(int argc, char **argv) {</span></p>
<p style="font: 11px Menlo; margin: 0px; color: rgb(0, 0, 0);"><span style="font-variant-ligatures: no-common-ligatures; font-variant-ligatures: no-common-ligatures"><span>
</span>(void) setenv("something", "something", 1);</span></p>
<p style="font: 11px Menlo; margin: 0px; color: rgb(0, 0, 0);"><span style="font-variant-ligatures: no-common-ligatures; font-variant-ligatures: no-common-ligatures"><span>
</span>printf("testing\n");</span></p>
<p style="font: 11px Menlo; margin: 0px; color: rgb(0, 0, 0);"><span style="font-variant-ligatures: no-common-ligatures; font-variant-ligatures: no-common-ligatures"><span>
</span>(void) execl("/cgi-bin/something.cgi", (char *) 0);</span></p>
<p style="font: 11px Menlo; margin: 0px; color: rgb(0, 0, 0);"><span style="font-variant-ligatures: no-common-ligatures; font-variant-ligatures: no-common-ligatures"><span>
</span>printf("%s\n", strerror(errno));<span> </span></span></p>
<p style="font: 11px Menlo; margin: 0px; color: rgb(0, 0, 0);"><span style="font-variant-ligatures: no-common-ligatures; font-variant-ligatures: no-common-ligatures"><span>
</span>return 1;</span></p>
<p style="font: 11px Menlo; margin: 0px; color: rgb(0, 0, 0);"><span style="font-variant-ligatures: no-common-ligatures; font-variant-ligatures: no-common-ligatures">}</span></p>
<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0); background-color: rgb(255, 255, 255);">
The script got "testing" as the first argument, the output is:</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0); background-color: rgb(255, 255, 255);">
<p style="font: 11px Menlo; margin: 0px; color: rgb(0, 0, 0);"><span style="font-variant-ligatures: no-common-ligatures; font-variant-ligatures: no-common-ligatures">localhost# ./a.out<span> </span></span></p>
<p style="font: 11px Menlo; margin: 0px; color: rgb(0, 0, 0);"><span style="font-variant-ligatures: no-common-ligatures; font-variant-ligatures: no-common-ligatures">testing</span></p>
<p style="font: 11px Menlo; margin: 0px; min-height: 13px; color: rgb(0, 0, 0);">
<span style="font-variant-ligatures: no-common-ligatures; font-variant-ligatures: no-common-ligatures"></span><br>
</p>
<p style="font: 11px Menlo; margin: 0px; color: rgb(0, 0, 0);"><span style="font-variant-ligatures: no-common-ligatures; font-variant-ligatures: no-common-ligatures">Content-Type: text/html</span></p>
<p style="font: 11px Menlo; margin: 0px; min-height: 13px; color: rgb(0, 0, 0);">
<span style="font-variant-ligatures: no-common-ligatures; font-variant-ligatures: no-common-ligatures"></span><br>
</p>
<p style="font: 11px Menlo; margin: 0px; color: rgb(0, 0, 0);"><span style="font-variant-ligatures: no-common-ligatures; font-variant-ligatures: no-common-ligatures">ENV=/root/.shrc</span></p>
<p style="font: 11px Menlo; margin: 0px; color: rgb(0, 0, 0);"><span style="font-variant-ligatures: no-common-ligatures; font-variant-ligatures: no-common-ligatures">BLOCKSIZE=1k</span></p>
<p style="font: 11px Menlo; margin: 0px; color: rgb(0, 0, 0);"><span style="font-variant-ligatures: no-common-ligatures; font-variant-ligatures: no-common-ligatures">PWD=/root/programs/sws/APUE-Group</span></p>
<p style="font: 11px Menlo; margin: 0px; color: rgb(0, 0, 0);"><span style="font-variant-ligatures: no-common-ligatures; font-variant-ligatures: no-common-ligatures">MAIL=/var/mail/root</span></p>
<p style="font: 11px Menlo; margin: 0px; color: rgb(0, 0, 0);"><span style="font-variant-ligatures: no-common-ligatures; font-variant-ligatures: no-common-ligatures">HOME=/root</span></p>
<p style="font: 11px Menlo; margin: 0px; color: rgb(0, 0, 0);"><span style="font-variant-ligatures: no-common-ligatures; font-variant-ligatures: no-common-ligatures">PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/pkg/sbin:/usr/pkg/bin:/usr/X11R7/bin:/usr/local/sbin:/usr/local/bin</span></p>
<p style="font: 11px Menlo; margin: 0px; color: rgb(0, 0, 0);"><span style="font-variant-ligatures: no-common-ligatures; font-variant-ligatures: no-common-ligatures">HOST=localhost</span></p>
<p style="font: 11px Menlo; margin: 0px; color: rgb(0, 0, 0);"><span style="font-variant-ligatures: no-common-ligatures; font-variant-ligatures: no-common-ligatures">SSH_CONNECTION=10.0.2.2 62926 10.0.2.15 22</span></p>
<p style="font: 11px Menlo; margin: 0px; color: rgb(0, 0, 0);"><span style="font-variant-ligatures: no-common-ligatures; font-variant-ligatures: no-common-ligatures">SSH_TTY=/dev/pts/1</span></p>
<p style="font: 11px Menlo; margin: 0px; color: rgb(0, 0, 0);"><span style="font-variant-ligatures: no-common-ligatures; font-variant-ligatures: no-common-ligatures">TERM=xterm-256color</span></p>
<p style="font: 11px Menlo; margin: 0px; color: rgb(0, 0, 0);"><span style="font-variant-ligatures: no-common-ligatures; font-variant-ligatures: no-common-ligatures">OLDPWD=/root/programs/sws</span></p>
<p style="font: 11px Menlo; margin: 0px; color: rgb(0, 0, 0);"><span style="font-variant-ligatures: no-common-ligatures; font-variant-ligatures: no-common-ligatures">USER=root</span></p>
<p style="font: 11px Menlo; margin: 0px; color: rgb(0, 0, 0);"><span style="font-variant-ligatures: no-common-ligatures; font-variant-ligatures: no-common-ligatures">SSH_CLIENT=10.0.2.2 62926 22</span></p>
<p style="font: 11px Menlo; margin: 0px; color: rgb(0, 0, 0);"><span style="font-variant-ligatures: no-common-ligatures; font-variant-ligatures: no-common-ligatures">LOGNAME=root</span></p>
<p style="font: 11px Menlo; margin: 0px; color: rgb(0, 0, 0);"><span style="font-variant-ligatures: no-common-ligatures; font-variant-ligatures: no-common-ligatures">SHELL=/bin/sh</span></p>
<p style="font: 11px Menlo; margin: 0px; color: rgb(0, 0, 0);"><span style="font-variant-ligatures: no-common-ligatures; font-variant-ligatures: no-common-ligatures">something=something</span></p>
<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0); background-color: rgb(255, 255, 255);">
Similarly, I ran the code without setenv and printf, the script printed out garbage values as the first argument.</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0); background-color: rgb(255, 255, 255);">
<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0); background-color: rgb(255, 255, 255);">
Regards.</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0); background-color: rgb(255, 255, 255);">
Hem Shah</div>
<div id="appendonsend"></div>
<hr style="display:inline-block;width:98%" tabindex="-1">
<div id="divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" style="font-size:11pt" color="#000000"><b>From:</b> Jan Schaumann <jschauma@stevens.edu><br>
<b>Sent:</b> Tuesday, November 26, 2019 7:48 PM<br>
<b>To:</b> Hem Shah <hshah42@stevens.edu><br>
<b>Cc:</b> cs631apue@lists.stevens.edu <cs631apue@lists.stevens.edu><br>
<b>Subject:</b> Re: [cs631apue] exec error</font>
<div> </div>
</div>
<div class="BodyFragment"><font size="2"><span style="font-size:11pt;">
<div class="PlainText">Hem Shah <hshah42@stevens.edu> wrote:<br>
> int<br>
> main(int argc, char **argv) {<br>
> (void) setenv("something", "something", 1);<br>
> (void) execl("/cgi-bin/something.cgi", (char *) 0);<br>
> printf("%s\n", strerror(errno));<br>
> return 1;<br>
> }<br>
<br>
Are you compiling this program with "-Wall -Werror"?<br>
<br>
You do not have enough arguments in your call to<br>
execl(3); the function requires at least a third<br>
argument, per the manual page:<br>
<br>
The const char *arg and subsequent ellipses in the execl(), execlp(),<br>
execlpe(), and execle() functions can be thought of as arg0, arg1, ...,<br>
argn. Together they describe a list of one or more pointers to NUL-<br>
terminated strings that represent the argument list available to the<br>
executed program. The first argument, by convention, should point to the<br>
file name associated with the file being executed. The list of arguments<br>
must be terminated by a NULL pointer.<br>
<br>
Use<br>
<br>
execl("/cgi-bin/something.cgi", "something.cgi", (char *) 0);<br>
<br>
instead, and it should work.<br>
<br>
(I don't know yet why the program works if you don't<br>
call setenv(3) or if you add a printf(3) after<br>
setenv(3), but calling execl(3) without a third<br>
argument means you have undefined behavior, so<br>
anything is fair game.)<br>
<br>
-Jan<br>
</div>
</span></font></div>
</body>
</html>