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
|
|
|
/*
|
2002-01-05 05:17:25 +08:00
|
|
|
* Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
|
2000-05-16 00:35:48 +08:00
|
|
|
* 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
|
|
|
|
2002-05-15 14:18:14 +08:00
|
|
|
#include <ac/errno.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(
|
2002-10-11 04:28:36 +08:00
|
|
|
char **args,
|
1998-08-09 08:43:13 +08:00
|
|
|
FILE **rfp,
|
|
|
|
FILE **wfp
|
|
|
|
)
|
|
|
|
{
|
2002-05-15 14:18:14 +08:00
|
|
|
int p2c[2] = { -1, -1 }, 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 );
|
2002-05-15 14:18:14 +08:00
|
|
|
close( p2c[0] );
|
|
|
|
close( p2c[1] );
|
1998-08-09 08:43:13 +08:00
|
|
|
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
|
|
|
|
*/
|
|
|
|
|
2002-05-15 14:18:14 +08:00
|
|
|
fflush( NULL );
|
|
|
|
# ifdef HAVE_THR
|
|
|
|
pid = fork1();
|
|
|
|
# else
|
|
|
|
pid = fork();
|
|
|
|
# endif
|
|
|
|
if ( pid == 0 ) { /* child */
|
1998-12-24 09:16:22 +08:00
|
|
|
/*
|
|
|
|
* child could deadlock here due to resources locked
|
|
|
|
* by our parent
|
|
|
|
*
|
2002-05-15 14:18:14 +08:00
|
|
|
* If so, configure --without-threads.
|
1998-12-24 09:16:22 +08:00
|
|
|
*/
|
1998-08-09 08:43:13 +08:00
|
|
|
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
|
|
|
}
|
2002-05-15 14:18:14 +08:00
|
|
|
}
|
|
|
|
close( p2c[0] );
|
|
|
|
close( c2p[1] );
|
|
|
|
if ( pid <= 0 ) {
|
|
|
|
close( p2c[1] );
|
|
|
|
close( c2p[0] );
|
|
|
|
}
|
|
|
|
switch ( pid ) {
|
|
|
|
case 0:
|
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 );
|
|
|
|
}
|
|
|
|
|
2002-05-15 14:18:14 +08:00
|
|
|
/* parent */
|
1998-08-09 08:43:13 +08:00
|
|
|
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 );
|
|
|
|
}
|