]> git.sur5r.net Git - openldap/blob - servers/slapd/back-shell/fork.c
merged with autoconf branch
[openldap] / servers / slapd / back-shell / fork.c
1 /* fork.c - fork and exec a process, connecting stdin/out w/pipes */
2
3 #include "portable.h"
4
5 #include <stdio.h>
6
7 #include <ac/string.h>
8 #include <ac/socket.h>
9
10 #include "slap.h"
11
12 forkandexec(
13     char        **args,
14     FILE        **rfp,
15     FILE        **wfp
16 )
17 {
18         int     p2c[2], c2p[2];
19         int     pid;
20
21         if ( pipe( p2c ) != 0 || pipe( c2p ) != 0 ) {
22                 Debug( LDAP_DEBUG_ANY, "pipe failed\n", 0, 0, 0 );
23                 return( -1 );
24         }
25
26         /*
27          * what we're trying to set up looks like this:
28          *      parent *wfp -> p2c[1] | p2c[0] -> stdin child
29          *      parent *rfp <- c2p[0] | c2p[1] <- stdout child
30          */
31
32         switch ( (pid = fork()) ) {
33         case 0:         /* child */
34                 close( p2c[1] );
35                 close( c2p[0] );
36                 if ( dup2( p2c[0], 0 ) == -1 || dup2( c2p[1], 1 ) == -1 ) {
37                         Debug( LDAP_DEBUG_ANY, "dup2 failed\n", 0, 0, 0 );
38                         exit( -1 );
39                 }
40
41                 execv( args[0], args );
42
43                 Debug( LDAP_DEBUG_ANY, "execv failed\n", 0, 0, 0 );
44                 exit( -1 );
45
46         case -1:        /* trouble */
47                 Debug( LDAP_DEBUG_ANY, "fork failed\n", 0, 0, 0 );
48                 return( -1 );
49
50         default:        /* parent */
51                 close( p2c[0] );
52                 close( c2p[1] );
53                 break;
54         }
55
56         if ( (*rfp = fdopen( c2p[0], "r" )) == NULL || (*wfp = fdopen( p2c[1],
57             "w" )) == NULL ) {
58                 Debug( LDAP_DEBUG_ANY, "fdopen failed\n", 0, 0, 0 );
59                 close( c2p[0] );
60                 close( p2c[1] );
61
62                 return( -1 );
63         }
64
65         return( pid );
66 }