1

I am trying to understand the workflow for CGI. So for example, say I use a unix shell script with CGI, what will be the sequence of steps executed to run this script when a client sends a request to the web server (Apache). I got the following steps by reading link1 and link2, but I want to ask if this is complete:

  1. Apache receives request on port 80 - sees its a request for CGI
  2. Apache forks and execs a CGI process
  3. CGI examines the script to be run and notices that it needs, for e.g., #!/bin/sh (so a shell script)
  4. CGI forks and execs /bin/sh
  5. Shell parses the script and executes it
3
  • 1
    Not quite right. apache just sets the environment variables and then execs the script. The kernel detects that it is a plain text script file (rather than a known binary executable format), examines the #! line and execs the specified interpreter (or the default, /bin/sh if there is no shebang).
    – cas
    Nov 17, 2015 at 2:05
  • 1
    OP overlooks the step where Apache returns the script's response. Nov 17, 2015 at 2:08
  • Could someone please give an answer with the right sequence of steps. I appreciate your time. Thank you.
    – Jake
    Nov 17, 2015 at 2:11

2 Answers 2

4

The "CGI Process" you talk about is the CGI program. The CGI program can be a script in any language or a compiled executable.

The details of how the operating system actually executes the CGI program code (your steps 2, 3, 4, and 5) are not really necessary to understand how CGI works.

This is how I think of it (Your steps 2-5 are compressed into step 2 here):

  1. Apache receives a request - sees it's a request for a CGI program
  2. Apache executes the CGI program, passing the parameters from the request to the CGI program in the environment (as environment variables).
  3. The CGI program gets the parameters from the environment, performs any required processing, and writes the web page on standard output.
  4. Apache receives the web page from the CGI program's standard output and transmits it to the web client (usually your web browser).

Most of the details you discussed in your steps 2-5 are covered in the execve man page (on Linux contains working code examples): man 2 execve. You may also want to look at fork. This is getting off the topic of CGI and into the topic of the Unix process model. Better for another question later.

For an accurate and detailed description of CGI see the internet draft of the specification:
The WWW Common Gateway Interface Version 1.1

See Also
Apache Tutorial: Dynamic Content with CGI

5
  • For steps 2-5 that I mention, would you say they are technically correct ? @cas raises a concern in the comment below the question
    – Jake
    Nov 17, 2015 at 7:17
  • My doubts are: 1) is there a fork and exec between Apache and CGI, 2) is there a fork and exec between CGI and /bin/sh ? Thanks
    – Jake
    Nov 17, 2015 at 7:29
  • 1
    Step 2 is correct, but the CGI process and the script are the same thing and when you say exec that is shorthand for the execve system call. Your steps 3 and 4 are performed by the execve system call, except there is no fork involved. You should really look at the execve man page. When @cas mentioned the kernel, he's talking about the execve system call part of the kernel. I think he's saying that your steps 3&4 are redundant since 3&4 would happen in the kernel when Apache execs the script.
    – RobertL
    Nov 17, 2015 at 7:29
  • 1
    There is only one fork and only one exec. There is not a separate "CGI". The CGI is your script. There are only two processes in the discussion: (1) apache (2) your CGI script. Apache forks then execs your script with the envrionment, then your script writes the web page, and Apache sends the webpage back to the browser. That's it.
    – RobertL
    Nov 17, 2015 at 7:32
  • Great! Check out those links!
    – RobertL
    Nov 17, 2015 at 7:34
0

One of the best explanations I have seen how CGIs work is on this book

http://docstore.mik.ua/orelly/weblinux2/modperl/ch01_02.htm

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.