using execv in httpd cgi scripts

Matthew Jones (matj@hchworth.demon.co.uk)
Wed, 12 Apr 1995 09:56:31 +0100 (BST)

Can anybody help?

I've run into another problem with cgi scripts. This time I want to
run a python script that uses the posix fork and execv commands to
spawn and run another process. This works if run from the shell but
falls over when run via httpd. I've included the script at the end
of this message, here's what I get when running the script through
httpd:

EXCEPTION

posix.error : (10, 'no children')

Traceback (innermost last):
File "/usr/local/etc/httpd/cgi-bin/update_users.py", line 66, in ?
main()
File "/usr/local/etc/httpd/cgi-bin/update_users.py", line 55, in main
update_users()
File "/usr/local/etc/httpd/cgi-bin/update_users.py", line 37, in update_users
sys.exit(0)
SystemExit: 0

Are you allowed to run a script that spawns other processes or does this
interfere with the httpd signal processor?

Thanks in advance

mat.
matj@hchworth.demon.co.uk

------- cut here --------

#! /usr/local/bin/python
# update_users.py

from os import environ
import cgi, sys, string, regex

# Get WWW form variables
#wwwform = cgi.FormContent()
#dict = wwwform.pars()
dict = {'user_id':['newuser'],'passwd1':['passwd'],'passwd2':['passwd']}

def update_users():
from posix import execv, fork, wait
global dict

try:
fd_pub = open("/home/matj/podsys/data/publisher.key",'r')
except IOError:
sys.exit(-1)

new_user_id = dict['user_id'][0]
new_user_passwd = dict['passwd1'][0]
args = ('ex_update_users.exp',new_user_id,new_user_passwd)

try:
parent = fork()
except:
#raise RunTimeError, 'fork failed'
sys.exit(-1)

if parent:
try:
wait()
except:
print "<H2>EXCEPTION</H2>"
print sys.exc_type, ":", sys.exc_value
sys.exit(0)
else:
try:
execv('/usr/local/etc/httpd/cgi-bin/ex_update_users.exp', ['ex_update_users.exp', new_user_id, new_user_passwd])
except:
print "<H2>EXCEPTION</H2>"
print sys.exc_type, ":", sys.exc_value
sys.exit(0)

def main():
print "Content-type: text/HTML\n"
print "<HTML>"
print "<HEAD>"
print "</HEAD>"

if dict.has_key('passwd1') and dict.has_key('passwd2'):
if dict['passwd1'] == dict['passwd2']:
update_users()
else:
print "<B>Error: passwords are not the same!</B>"
else:
print "<B>Error: both password fields must be filled in!</B>"

print "</HTML>"

if __name__ == '__main__': # Only do this when run as a script
import sys, traceback # Be prepared!
try:
main()
except SystemError, sts: # main() called sys.exit(sts)
sys.exit(sts)
except:
print "\n<XMP>" # Turn off HTML parsing
traceback.print_exc() # Print stack trace