using fork & execv in cgi scripts

Matthew Jones (matj@hchworth.demon.co.uk)
Wed, 12 Apr 1995 10:23:41 +0100 (BST)

Sorry if you get 2 copies of this message, I had an error when I sent
the first version.

---

Can anybody help?

I've run into another problem whilst developing cgi scripts. This time I want to develop a script that makes calls to fork and execv to spawn another process. The script works fine when run at the shell prompt but won't work through httpd. I've included the script at the end of this message. Here's what I get when running 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

Do I need to do something differently to spawn processes when running a cgi script?

Thanks

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