Reputation: 21
I'm following the project TINY SERVER and there is a peice of code shown as below.
This code is used to passing the cgiargs
to filename
which is a cgi program.
void serve_dynamic(int fd, char *filename, char *cgiargs)
{
char buf[MAXLINE], *emptylist[] = { NULL };
/* Return first part of HTTP response */
sprintf(buf, "HTTP/1.0 200 OK\r\n");
rio_writen(fd, buf, strlen(buf));
sprintf(buf, "Server: Tiny Web Server\r\n");
rio_writen(fd, buf, strlen(buf));
if (Fork() == 0) { /* child */ //line:netp:servedynamic:fork
/* Real server would set all CGI vars here */
setenv("QUERY_STRING", cgiargs, 1); //line:netp:servedynamic:setenv
dup2(fd, STDOUT_FILENO); /* Redirect stdout to client */ //line:netp:servedynamic:dup2
execve(filename, emptylist, environ); /* Run CGI program */ //line:netp:servedynamic:execve
}
wait(NULL); /* Parent waits for and reaps child */ //line:netp:servedynamic:wait
}
In the adder.c which is the target cgi program, the code is shown as below.
int main(void) {
char *buf, *p;
char arg1[MAXLINE], arg2[MAXLINE], content[MAXLINE];
int n1 = 0, n2 = 0;
/* Extract the two arguments */
if ((buf = getenv("QUERY_STRING")) != NULL) {
p = strchr(buf, ’&’);
*p = ’\0’;
strcpy(arg1, buf);
strcpy(arg2, p + 1);
n1 = atoi(arg1);
n2 = atoi(arg2);
}
/* Make the response body */
sprintf(content, "QUERY_STRING=%s", buf);
sprintf(content, "Welcome to add.com: ");
sprintf(content, "%sTHE Internet addition portal.\r\n<p>", content);
sprintf(content, "%sThe answer is: %d + %d = %d\r\n<p>",
content, n1, n2, n1 + n2);
sprintf(content, "%sThanks for visiting!\r\n", content);
/* Generate the HTTP response */
printf("Connection: close\r\n");
printf("Content-length: %d\r\n", (int)strlen(content));
printf("Content-type: text/html\r\n\r\n");
printf("%s", content);
fflush(stdout);
exit(0);
}
In my opinion, the serve_dynamic
function sets the "QUERY_STRING", an environment variable, and the adder.c
gets the "QUERY_STRING" and use it. But actually in adder.c
, the buf
is NULL.
Then I try to set environ
to pass the QUERY_STRING
and it works. But it's very inconvenient.
I didn't think out why setenv
function didn't work, and I hope someone can solve my problem.
Upvotes: 0
Views: 91
Reputation: 11
I'm doing the exact TINY SERVER project as you did, and I had the exact same problem as you did. I think the 'setenv' function didn't work is because --
We set the environment variable 'QUERY_STRING' in the child process we forked, and then we used execve() to start a new process, which is NOT a child process of the calling process, instead it will replace the child process we forked, so the 'QUERY_STRING' was not shared in the new process.
I think the right way to set environment variable (in the child process we forked) to be passed to the program refered by execve() is like this: newenviron[0] = "QUERY_STRING=VALUE";
and then we will be able to use getenv() to access it in the cgi adder program.
You can refer to: https://man7.org/linux/man-pages/man2/execve.2.html
execve(const char *pathname, --,--) executes the program referred to by pathname. This causes the program that is currently being run by the calling process to be replaced with a new program.with newly initialized stack, heap, and (initialized and uninitialized) data segments.
Upvotes: 1