1 /*
2  * sqsh_fork.c - Create a child process
3  *
4  * Copyright (C) 1995, 1996 by Scott C. Gray
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, write to the Free Software
18  * Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
19  *
20  * You may contact the author :
21  *   e-mail:  gray@voicenet.com
22  *            grays@xtend-tech.com
23  *            gray@xenotropic.com
24  */
25 #include <stdio.h>
26 #include "sqsh_config.h"
27 #include "sqsh_global.h"
28 #include "sqsh_error.h"
29 #include "sqsh_fork.h"
30 
31 /*-- Current Version --*/
32 #if !defined(lint) && !defined(__LINT__)
33 static char RCS_Id[] = "$Id: sqsh_fork.c,v 1.2 2010/01/26 15:03:50 mwesdorp Exp $" ;
USE(RCS_Id)34 USE(RCS_Id)
35 #endif /* !defined(lint) */
36 
37 /*
38  * sqsh_fork():
39  *
40  * This function behaves exactly like the standard UNIX fork with the
41  * exception that it resets any necessary globals variables within the
42  * context of the child process.
43  */
44 pid_t sqsh_fork()
45 {
46 	pid_t  child_pid ;
47 
48 	switch( (child_pid = fork()) ) {
49 
50 		case -1 :     /* Some sort of error has ocurred */
51 			sqsh_set_error( errno, "fork: %s", strerror(errno) ) ;
52 			return -1 ;
53 
54 		case 0 :      /* Child process */
55 			/*
56 			 * It would be bad news for the child process to carry
57 			 * around the same CS_CONNECTION pointer as the parent, who
58 			 * knows what SQL server would do!
59 			 */
60 			g_connection = NULL;
61 			g_context    = NULL;
62 
63 			/*
64 			 * We don't need to touch g_cmdset or g_env because these
65 			 * should be inherited by the child process.  Also, g_sqlbuf
66 			 * should be inherited, especially because the child has
67 			 * been created in order to do something with it.
68 			 */
69 
70 			/*
71 			 * Since the child process doesn't have any children of it's
72 			 * own (per se), we don't want it to inherit the children of
73 			 * the parent process.
74 			 * sqsh-2.1.7 - Running jobset_clear on the global jobset will
75 			 * eventually unlink deferred output files of still running
76 			 * brother and sister processes. To fix this, just create a
77 			 * new global jobset. Also reset the global history pointer
78 			 * and g_interactive to false, just in case.
79 		       	 * Then allow the parent process some time to administer things
80 			 * by sleeping for a while, before starting the real job.
81 			 */
82 			g_history = NULL ;
83 			g_interactive = False ;
84 			if( (g_jobset = jobset_create( 47 )) == NULL ) {
85 			    sqsh_set_error( sqsh_get_error(), "jobset_create: %s",
86 			    sqsh_get_errstr() ) ;
87 			    exit(255) ;		/* Exit child if something is wrong */
88 			}
89 			sleep (1) ;
90 			break ;
91 
92 		default :    /* Parent process */
93 			break ;
94 	}
95 
96 	sqsh_set_error( SQSH_E_NONE, NULL ) ;
97 	return child_pid ;
98 }
99