1998-08-09 08:43:13 +08:00
|
|
|
/* fork.c - fork and exec a process, connecting stdin/out w/pipes */
|
1999-09-09 03:06:24 +08:00
|
|
|
/* $OpenLDAP$ */
|
2000-05-16 00:35:48 +08:00
|
|
|
/*
|
|
|
|
* Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
|
|
|
|
* COPYING RESTRICTIONS APPLY, see COPYRIGHT file
|
|
|
|
*/
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1998-10-25 09:41:42 +08:00
|
|
|
#include "portable.h"
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
#include <stdio.h>
|
1998-10-25 09:41:42 +08:00
|
|
|
|
|
|
|
#include <ac/string.h>
|
|
|
|
#include <ac/socket.h>
|
Protoized, moved extern definitions to .h files, fixed related bugs.
Most function and variable definitions are now preceded by its extern
definition, for error checking. Retyped a number of functions, usually
to return void. Fixed a number of printf format errors.
API changes (in ldap/include):
Added avl_dup_ok, avl_prefixapply, removed ber_fatten (probably typo
for ber_flatten), retyped ldap_sort_strcasecmp, grew lutil.h.
A number of `extern' declarations are left (some added by protoize), to
be cleaned away later. Mostly strdup(), strcasecmp(), mktemp(), optind,
optarg, errno.
1998-11-16 06:40:11 +08:00
|
|
|
#include <ac/unistd.h>
|
1998-10-25 09:41:42 +08:00
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
#include "slap.h"
|
Protoized, moved extern definitions to .h files, fixed related bugs.
Most function and variable definitions are now preceded by its extern
definition, for error checking. Retyped a number of functions, usually
to return void. Fixed a number of printf format errors.
API changes (in ldap/include):
Added avl_dup_ok, avl_prefixapply, removed ber_fatten (probably typo
for ber_flatten), retyped ldap_sort_strcasecmp, grew lutil.h.
A number of `extern' declarations are left (some added by protoize), to
be cleaned away later. Mostly strdup(), strcasecmp(), mktemp(), optind,
optarg, errno.
1998-11-16 06:40:11 +08:00
|
|
|
#include "shell.h"
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1999-01-26 12:32:26 +08:00
|
|
|
pid_t
|
1998-08-09 08:43:13 +08:00
|
|
|
forkandexec(
|
|
|
|
char **args,
|
|
|
|
FILE **rfp,
|
|
|
|
FILE **wfp
|
|
|
|
)
|
|
|
|
{
|
|
|
|
int p2c[2], c2p[2];
|
1999-01-26 12:32:26 +08:00
|
|
|
pid_t pid;
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
if ( pipe( p2c ) != 0 || pipe( c2p ) != 0 ) {
|
|
|
|
Debug( LDAP_DEBUG_ANY, "pipe failed\n", 0, 0, 0 );
|
|
|
|
return( -1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* what we're trying to set up looks like this:
|
|
|
|
* parent *wfp -> p2c[1] | p2c[0] -> stdin child
|
|
|
|
* parent *rfp <- c2p[0] | c2p[1] <- stdout child
|
|
|
|
*/
|
|
|
|
|
1998-12-24 09:16:22 +08:00
|
|
|
#ifdef HAVE_THR
|
|
|
|
switch ( (pid = fork1()) )
|
|
|
|
#else
|
|
|
|
switch ( (pid = fork()) )
|
|
|
|
#endif
|
|
|
|
{
|
1998-08-09 08:43:13 +08:00
|
|
|
case 0: /* child */
|
1998-12-24 09:16:22 +08:00
|
|
|
/*
|
|
|
|
* child could deadlock here due to resources locked
|
|
|
|
* by our parent
|
|
|
|
*
|
|
|
|
* If so, configure --without-threads or implement forking
|
|
|
|
* via a surrogate parent.
|
|
|
|
*/
|
1998-08-09 08:43:13 +08:00
|
|
|
close( p2c[1] );
|
|
|
|
close( c2p[0] );
|
|
|
|
if ( dup2( p2c[0], 0 ) == -1 || dup2( c2p[1], 1 ) == -1 ) {
|
|
|
|
Debug( LDAP_DEBUG_ANY, "dup2 failed\n", 0, 0, 0 );
|
1999-08-04 02:14:24 +08:00
|
|
|
exit( EXIT_FAILURE );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
execv( args[0], args );
|
|
|
|
|
|
|
|
Debug( LDAP_DEBUG_ANY, "execv failed\n", 0, 0, 0 );
|
1999-08-04 02:14:24 +08:00
|
|
|
exit( EXIT_FAILURE );
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
case -1: /* trouble */
|
|
|
|
Debug( LDAP_DEBUG_ANY, "fork failed\n", 0, 0, 0 );
|
|
|
|
return( -1 );
|
|
|
|
|
|
|
|
default: /* parent */
|
|
|
|
close( p2c[0] );
|
|
|
|
close( c2p[1] );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( (*rfp = fdopen( c2p[0], "r" )) == NULL || (*wfp = fdopen( p2c[1],
|
|
|
|
"w" )) == NULL ) {
|
|
|
|
Debug( LDAP_DEBUG_ANY, "fdopen failed\n", 0, 0, 0 );
|
|
|
|
close( c2p[0] );
|
|
|
|
close( p2c[1] );
|
|
|
|
|
|
|
|
return( -1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
return( pid );
|
|
|
|
}
|