You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@httpd.apache.org by Lihet Ruben <ru...@my.fmi.unibuc.ro> on 2009/03/20 16:35:26 UTC

[users@httpd] CGI Child Process Question

Hello,

My name is Lihet Ruben, I'm new to this mailing list. I have a question
about a CGI script I want to make.

So, this is the scenario. The CGI script will serve content to users (like a
file download) but I have to keep track of many other things. The most
important thing is I want to know if the person has downloaded the file
completely or canceled the download. So I thought that is the user cancels
the script, the program dies, I cannot log what happened. So I came with the
idea of creating a child for my cgi script, with fork() (btw I'm using c++)
and keep track of the child's activity and make decisions based on childs
return code. That would be ok at a first glance, but the problem is that the
parent cgi script is killed by the apache server. 

Now comes the question:
Q: How does apache kills cgi scripts ? Or, what happens with a cgi script
when a user hits cancel in the browser while the script was still executing
? Does apache send a signal to the cgi-script telling it to die or what ? 

Thanks in advance, 
Lihet Ruben.


---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


[users@httpd] Re: CGI Child Process Question

Posted by Lihet Ruben <ru...@my.fmi.unibuc.ro>.
The nature of this project does allow me to reveal any details. 
This thread is closed, I'm not here to argue with 10s of unknown people on
the Internet. 

-----Original Message-----
From: André Warnier [mailto:aw@ice-sa.com] 
Sent: Saturday, March 21, 2009 4:37 PM
To: users@httpd.apache.org
Subject: Re: [users@httpd] Re: CGI Child Process Question

Lihet Ruben wrote:
> Well, I don't know better. I didn't knew at all before I emailed the list.
> At first, I made an assumption, and I wanted someone to confirm it. Mean
> time, I did reasearch on how it works and I found an answer, then share it
> to the community ;) 
> 
> Don't be mad at me :D
> 
Well, maybe I, and other people on this list, have some reasons to feel 
a bit disappointed, after spending time trying to give you tips ?

"My tests (and the whole cgi logic in a web server) concluded that the
cgi-script is a child of apache. This because the the ppid() of the
cgi-script is the pid() of the apache server daemon."

That may be true under certain MPM's (threaded ?), but is certainly not 
universal.  Anyway, it does not change the fact that the cgi-bin is a 
different process than Apache, and it does not change the fact that your 
script may think that it has sent all the data, but that your client has 
not received it all.

"But anyway, I think that the SIGPIPE signal has the same effect as an 
Apache Filter."

Not quite.  They are two totally different things.

"I'm not sure, but I think Apache already has filters that send this 
kind of singnals to the cgi-script when the socket is closed. That's 
what SIGPIPE is all about."

Not at all. Your cgi-bin may get a SIGPIPE (from the O.S., not from 
Apache) when /it/ finds /its/ own output stream closed when trying to 
write to it.  More likely, he'll just get an I/O error.

"Apache routes the stdout from my cgi-script to the clients socket
through a pipe,"

No, it does not.  Apache reads the output of your script, stores it in 
buffers (bucket brigades), and passes these further down the chain of 
Apache filters, in case anyone else wants to examine the output. And 
finally, it is Apache that sends the data to the client, not your 
script.  That's because it is with Apache that the client has a 
connection, not with your script.

"After all, I am not interested if the client has received 100% of the 
data."

Really ? then what was this ?
"The most important thing is I want to know if the person has downloaded 
the file completely or canceled the download."

I was always under the impression that "completely" and "100%" were kind 
of equivalent terms.
Now, you can ask questions, but if you change the questions after people 
have answered them, it's a bit unfair, isn't it ?

"I am interested to know, how close to 100% did the client received. The
cgi-script I'm writing, is a bit selfish. All he wants to know is yes or 
no, how much data did the client wasted on his request. If the data was 
not 100% received by the client because he cancelled, that's his 
problem, he can request again later."

That paragraph does not really make much sense, and seem to contradict 
itself several times.

So basically, what do you really want to know ?
And are you interested in good tips and explanations, or just in 
confirming your misconceptions ?



---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org



---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


[users@httpd] Re: CGI Child Process Question

Posted by Lihet Ruben <ru...@my.fmi.unibuc.ro>.
Look, I am sorry for misleading you. I am not an Apache geek and I know
little about its internals. I am rephrasing the question:

Data: I have a script that sends data to a client. I want to know how much
data I have send (not how much data did the client received). Now, I assume,
the client cancels the transfer, so my cgi will die. I want to store in a
database how much data has been sent, but I want to do this before my cgi
script dies. 
Q: How does Apache interacts with a cgi-script in such a situation.

PS: Even if I launch a single question, I have the right to launch another
question during the thread. 

-----Original Message-----
From: André Warnier [mailto:aw@ice-sa.com] 
Sent: Saturday, March 21, 2009 4:37 PM
To: users@httpd.apache.org
Subject: Re: [users@httpd] Re: CGI Child Process Question

Lihet Ruben wrote:
> Well, I don't know better. I didn't knew at all before I emailed the list.
> At first, I made an assumption, and I wanted someone to confirm it. Mean
> time, I did reasearch on how it works and I found an answer, then share it
> to the community ;) 
> 
> Don't be mad at me :D
> 
Well, maybe I, and other people on this list, have some reasons to feel 
a bit disappointed, after spending time trying to give you tips ?

"My tests (and the whole cgi logic in a web server) concluded that the
cgi-script is a child of apache. This because the the ppid() of the
cgi-script is the pid() of the apache server daemon."

That may be true under certain MPM's (threaded ?), but is certainly not 
universal.  Anyway, it does not change the fact that the cgi-bin is a 
different process than Apache, and it does not change the fact that your 
script may think that it has sent all the data, but that your client has 
not received it all.

"But anyway, I think that the SIGPIPE signal has the same effect as an 
Apache Filter."

Not quite.  They are two totally different things.

"I'm not sure, but I think Apache already has filters that send this 
kind of singnals to the cgi-script when the socket is closed. That's 
what SIGPIPE is all about."

Not at all. Your cgi-bin may get a SIGPIPE (from the O.S., not from 
Apache) when /it/ finds /its/ own output stream closed when trying to 
write to it.  More likely, he'll just get an I/O error.

"Apache routes the stdout from my cgi-script to the clients socket
through a pipe,"

No, it does not.  Apache reads the output of your script, stores it in 
buffers (bucket brigades), and passes these further down the chain of 
Apache filters, in case anyone else wants to examine the output. And 
finally, it is Apache that sends the data to the client, not your 
script.  That's because it is with Apache that the client has a 
connection, not with your script.

"After all, I am not interested if the client has received 100% of the 
data."

Really ? then what was this ?
"The most important thing is I want to know if the person has downloaded 
the file completely or canceled the download."

I was always under the impression that "completely" and "100%" were kind 
of equivalent terms.
Now, you can ask questions, but if you change the questions after people 
have answered them, it's a bit unfair, isn't it ?

"I am interested to know, how close to 100% did the client received. The
cgi-script I'm writing, is a bit selfish. All he wants to know is yes or 
no, how much data did the client wasted on his request. If the data was 
not 100% received by the client because he cancelled, that's his 
problem, he can request again later."

That paragraph does not really make much sense, and seem to contradict 
itself several times.

So basically, what do you really want to know ?
And are you interested in good tips and explanations, or just in 
confirming your misconceptions ?



---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org



---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


Re: [users@httpd] Re: CGI Child Process Question

Posted by André Warnier <aw...@ice-sa.com>.
Lihet Ruben wrote:
> Well, I don't know better. I didn't knew at all before I emailed the list.
> At first, I made an assumption, and I wanted someone to confirm it. Mean
> time, I did reasearch on how it works and I found an answer, then share it
> to the community ;) 
> 
> Don't be mad at me :D
> 
Well, maybe I, and other people on this list, have some reasons to feel 
a bit disappointed, after spending time trying to give you tips ?

"My tests (and the whole cgi logic in a web server) concluded that the
cgi-script is a child of apache. This because the the ppid() of the
cgi-script is the pid() of the apache server daemon."

That may be true under certain MPM's (threaded ?), but is certainly not 
universal.  Anyway, it does not change the fact that the cgi-bin is a 
different process than Apache, and it does not change the fact that your 
script may think that it has sent all the data, but that your client has 
not received it all.

"But anyway, I think that the SIGPIPE signal has the same effect as an 
Apache Filter."

Not quite.  They are two totally different things.

"I'm not sure, but I think Apache already has filters that send this 
kind of singnals to the cgi-script when the socket is closed. That's 
what SIGPIPE is all about."

Not at all. Your cgi-bin may get a SIGPIPE (from the O.S., not from 
Apache) when /it/ finds /its/ own output stream closed when trying to 
write to it.  More likely, he'll just get an I/O error.

"Apache routes the stdout from my cgi-script to the clients socket
through a pipe,"

No, it does not.  Apache reads the output of your script, stores it in 
buffers (bucket brigades), and passes these further down the chain of 
Apache filters, in case anyone else wants to examine the output. And 
finally, it is Apache that sends the data to the client, not your 
script.  That's because it is with Apache that the client has a 
connection, not with your script.

"After all, I am not interested if the client has received 100% of the 
data."

Really ? then what was this ?
"The most important thing is I want to know if the person has downloaded 
the file completely or canceled the download."

I was always under the impression that "completely" and "100%" were kind 
of equivalent terms.
Now, you can ask questions, but if you change the questions after people 
have answered them, it's a bit unfair, isn't it ?

"I am interested to know, how close to 100% did the client received. The
cgi-script I'm writing, is a bit selfish. All he wants to know is yes or 
no, how much data did the client wasted on his request. If the data was 
not 100% received by the client because he cancelled, that's his 
problem, he can request again later."

That paragraph does not really make much sense, and seem to contradict 
itself several times.

So basically, what do you really want to know ?
And are you interested in good tips and explanations, or just in 
confirming your misconceptions ?



---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


RE: [users@httpd] Re: CGI Child Process Question

Posted by Lihet Ruben <ru...@my.fmi.unibuc.ro>.
Well, I don't know better. I didn't knew at all before I emailed the list.
At first, I made an assumption, and I wanted someone to confirm it. Mean
time, I did reasearch on how it works and I found an answer, then share it
to the community ;) 

Don't be mad at me :D

-----Original Message-----
From: André Warnier [mailto:aw@ice-sa.com] 
Sent: Saturday, March 21, 2009 3:53 PM
To: users@httpd.apache.org
Subject: Re: [users@httpd] Re: CGI Child Process Question

Lihet Ruben wrote:
[...]
Well, if you know better, why do you ask ?

---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org



---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


Re: [users@httpd] Re: CGI Child Process Question

Posted by André Warnier <aw...@ice-sa.com>.
Lihet Ruben wrote:
[...]
Well, if you know better, why do you ask ?

---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


[users@httpd] Re: CGI Child Process Question

Posted by Lihet Ruben <ru...@my.fmi.unibuc.ro>.
André Warnier wrote:
"A cgi-bin program is a separate process from the Apache child that runs
it."

My tests (and the whole cgi logic in a web server) concluded that the
cgi-script is a child of apache. This because the the ppid() of the
cgi-script is the pid() of the apache server daemon. ;)


Thanks for the tip, I haven’t thought on using an Apache Filter. But anyway,
I think that the SIGPIPE signal has the same effect as an Apache Filter. I'm
not sure, but I think Apache already has filters that send this kind of
singnals to the cgi-script when the socket is closed. That's what SIGPIPE is
all about. Apache routes the stdout from my cgi-script to the clients socket
through a pipe, when the socket is closed, apache sends a SIGPIPE the my
cgi-script telling that the client has disconnected.

After all, I am not interested if the client has received 100% of the data.
I am interested to know, how close to 100% did the client received. The
cgi-script I'm writing, is a bit selfish. All he wants to know is yes or no,
how much data did the client wasted on his request. If the data was not 100%
received by the client because he cancelled, that's his problem, he can
request again later. 

-----Original Message-----
From: André Warnier [mailto:aw@ice-sa.com] 
Sent: Saturday, March 21, 2009 1:03 PM
To: users@httpd.apache.org
Subject: Re: [users@httpd] Re: CGI Child Process Question

Lihet Ruben wrote:
[...]
I would look at this in another way.
A cgi-bin program is a separate process from the Apache child that runs
it.  The Apache process starts the cgi-bin process, and arranges to read
back everything that the cgi-bin writes to STDOUT.  Then Apache sends
this to the browser.  In-between, there is buffering, so as someone
pointed out, it is possible that after your cgi-bin has already finished
its job of producing the output and has exited, Apache is still busy
sending some chunks of output to the browser, and then the user cancels.
In that case, your script would think that everything has been sent,
while this is not the case.

What I would thus do, is to implement your logic of verifying that the
user has received everything, in an Apache output filter.  It is still
not perfect, but much "closer" to the browser. If the user interrupts
the connection by hitting cancel, the filter would know, because it
would get an i/o error when writing to the response socket.  And it
could notify your application.

Now understand that if your client is behind a proxy that does
buffering, then the proxy will still hold some data, while Apache is
long done and the filter has told your application that everything was
sent. And the user can still hit cancel then.

So ultimately, to be 100% sure, the only solution is to ask the user,
when he has finished downloading, to acknowledge receipt.

I cannot think of an easy way to do that with a standard browser, but I
can imagine a Java applet which gets the file from your server, writes
it to a local file, and when it is done, makes a POST to the server to
say it was done.




---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org



---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


Re: [users@httpd] Re: CGI Child Process Question

Posted by André Warnier <aw...@ice-sa.com>.
Lihet Ruben wrote:
[...]
I would look at this in another way.
A cgi-bin program is a separate process from the Apache child that runs
it.  The Apache process starts the cgi-bin process, and arranges to read
back everything that the cgi-bin writes to STDOUT.  Then Apache sends
this to the browser.  In-between, there is buffering, so as someone
pointed out, it is possible that after your cgi-bin has already finished
its job of producing the output and has exited, Apache is still busy
sending some chunks of output to the browser, and then the user cancels.
In that case, your script would think that everything has been sent,
while this is not the case.

What I would thus do, is to implement your logic of verifying that the
user has received everything, in an Apache output filter.  It is still
not perfect, but much "closer" to the browser. If the user interrupts
the connection by hitting cancel, the filter would know, because it
would get an i/o error when writing to the response socket.  And it
could notify your application.

Now understand that if your client is behind a proxy that does
buffering, then the proxy will still hold some data, while Apache is
long done and the filter has told your application that everything was
sent. And the user can still hit cancel then.

So ultimately, to be 100% sure, the only solution is to ask the user,
when he has finished downloading, to acknowledge receipt.

I cannot think of an easy way to do that with a standard browser, but I
can imagine a Java applet which gets the file from your server, writes
it to a local file, and when it is done, makes a POST to the server to
say it was done.




---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


[users@httpd] Re: CGI Child Process Question

Posted by Lihet Ruben <ru...@my.fmi.unibuc.ro>.
I am talking about heavy work, something that will take about 5-10 minutes,
and so the user can cancel the job. What I want is this: does apache send
any signal to my cgi script in order to kill it ?

-----Original Message-----
From: news [mailto:news@ger.gmane.org] On Behalf Of Dan Poirier
Sent: Friday, March 20, 2009 8:28 PM
To: users@httpd.apache.org
Subject: [users@httpd] Re: CGI Child Process Question

"Lihet Ruben" <ru...@my.fmi.unibuc.ro> writes:

> The most
> important thing is I want to know if the person has downloaded the file
> completely or canceled the download. So I thought that is the user cancels
> the script, the program dies, I cannot log what happened. 

Problem: your script might have already sent its entire response and
exited before the user canceled.  There's really no way to be sure
whether the client received the complete file.


---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org



---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


[users@httpd] Re: CGI Child Process Question

Posted by Dan Poirier <po...@pobox.com>.
"Lihet Ruben" <ru...@my.fmi.unibuc.ro> writes:

> The most
> important thing is I want to know if the person has downloaded the file
> completely or canceled the download. So I thought that is the user cancels
> the script, the program dies, I cannot log what happened. 

Problem: your script might have already sent its entire response and
exited before the user canceled.  There's really no way to be sure
whether the client received the complete file.


---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org