]> git.sur5r.net Git - openldap/blob - servers/slapd/back-shell/fork.c
Update back-shell to use void* o_private and pid_t forkandexec().
[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 #include <ac/unistd.h>
10
11 #include "slap.h"
12 #include "shell.h"
13
14 pid_t
15 forkandexec(
16     char        **args,
17     FILE        **rfp,
18     FILE        **wfp
19 )
20 {
21         int     p2c[2], c2p[2];
22         pid_t   pid;
23
24         if ( pipe( p2c ) != 0 || pipe( c2p ) != 0 ) {
25                 Debug( LDAP_DEBUG_ANY, "pipe failed\n", 0, 0, 0 );
26                 return( -1 );
27         }
28
29         /*
30          * what we're trying to set up looks like this:
31          *      parent *wfp -> p2c[1] | p2c[0] -> stdin child
32          *      parent *rfp <- c2p[0] | c2p[1] <- stdout child
33          */
34
35 #ifdef HAVE_THR
36         switch ( (pid = fork1()) )
37 #else
38         switch ( (pid = fork()) )
39 #endif
40         {
41         case 0:         /* child */
42                 /*
43                  * child could deadlock here due to resources locked
44                  * by our parent
45                  *
46                  * If so, configure --without-threads or implement forking
47                  * via a surrogate parent.
48                  */
49                 close( p2c[1] );
50                 close( c2p[0] );
51                 if ( dup2( p2c[0], 0 ) == -1 || dup2( c2p[1], 1 ) == -1 ) {
52                         Debug( LDAP_DEBUG_ANY, "dup2 failed\n", 0, 0, 0 );
53                         exit( -1 );
54                 }
55
56                 execv( args[0], args );
57
58                 Debug( LDAP_DEBUG_ANY, "execv failed\n", 0, 0, 0 );
59                 exit( -1 );
60
61         case -1:        /* trouble */
62                 Debug( LDAP_DEBUG_ANY, "fork failed\n", 0, 0, 0 );
63                 return( -1 );
64
65         default:        /* parent */
66                 close( p2c[0] );
67                 close( c2p[1] );
68                 break;
69         }
70
71         if ( (*rfp = fdopen( c2p[0], "r" )) == NULL || (*wfp = fdopen( p2c[1],
72             "w" )) == NULL ) {
73                 Debug( LDAP_DEBUG_ANY, "fdopen failed\n", 0, 0, 0 );
74                 close( c2p[0] );
75                 close( p2c[1] );
76
77                 return( -1 );
78         }
79
80         return( pid );
81 }