1 /* fork.c - fork and exec a process, connecting stdin/out w/pipes */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2012 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
16 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
17 * All rights reserved.
19 * Redistribution and use in source and binary forms are permitted
20 * provided that this notice is preserved and that due credit is given
21 * to the University of Michigan at Ann Arbor. The name of the University
22 * may not be used to endorse or promote products derived from this
23 * software without specific prior written permission. This software
24 * is provided ``as is'' without express or implied warranty.
27 * This work was originally developed by the University of Michigan
28 * (as part of U-MICH LDAP).
36 #include <ac/string.h>
37 #include <ac/socket.h>
38 #include <ac/unistd.h>
50 int p2c[2] = { -1, -1 }, c2p[2];
53 if ( pipe( p2c ) != 0 || pipe( c2p ) != 0 ) {
54 Debug( LDAP_DEBUG_ANY, "pipe failed\n", 0, 0, 0 );
61 * what we're trying to set up looks like this:
62 * parent *wfp -> p2c[1] | p2c[0] -> stdin child
63 * parent *rfp <- c2p[0] | c2p[1] <- stdout child
72 if ( pid == 0 ) { /* child */
74 * child could deadlock here due to resources locked
77 * If so, configure --without-threads.
79 if ( dup2( p2c[0], 0 ) == -1 || dup2( c2p[1], 1 ) == -1 ) {
80 Debug( LDAP_DEBUG_ANY, "dup2 failed\n", 0, 0, 0 );
92 execv( args[0], args );
94 Debug( LDAP_DEBUG_ANY, "execv failed\n", 0, 0, 0 );
97 case -1: /* trouble */
98 Debug( LDAP_DEBUG_ANY, "fork failed\n", 0, 0, 0 );
103 if ( (*rfp = fdopen( c2p[0], "r" )) == NULL || (*wfp = fdopen( p2c[1],
105 Debug( LDAP_DEBUG_ANY, "fdopen failed\n", 0, 0, 0 );