fork.c 8.25 KB
Newer Older
Kurt Zeilenga's avatar
Kurt Zeilenga committed
1
/* fork.c - fork and exec a process, connecting stdin/out w/pipes */
2
/* $OpenLDAP$ */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
3
/*
Kurt Zeilenga's avatar
Kurt Zeilenga committed
4
 * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
Kurt Zeilenga's avatar
Kurt Zeilenga committed
5
6
 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
 */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
7

Kurt Zeilenga's avatar
Kurt Zeilenga committed
8
9
#include "portable.h"

Kurt Zeilenga's avatar
Kurt Zeilenga committed
10
#include <stdio.h>
Kurt Zeilenga's avatar
Kurt Zeilenga committed
11

12
#include <ac/errno.h>
Kurt Zeilenga's avatar
Kurt Zeilenga committed
13
14
#include <ac/string.h>
#include <ac/socket.h>
15
#include <ac/unistd.h>
Kurt Zeilenga's avatar
Kurt Zeilenga committed
16

Kurt Zeilenga's avatar
Kurt Zeilenga committed
17
#include "slap.h"
18
#include "shell.h"
Kurt Zeilenga's avatar
Kurt Zeilenga committed
19

20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#ifdef SHELL_SURROGATE_PARENT

#include <sys/uio.h>

/* Use several socketpairs to the surrogate parent, because   *
 * a single communication channel to it could be a bottleneck */
ldap_pvt_thread_mutex_t shell_surrogate_fd_mutex[2];
int                     shell_surrogate_fd[2] = { -1, -1 };
/* Index to shell_surrogate_fd, and its mutex */
ldap_pvt_thread_mutex_t shell_surrogate_index_mutex;
static int              shell_surrogate_index = 1;

pid_t                   shell_surrogate_pid = -1;

#define  nread( fd, buf, len ) n_rw( 0, fd, buf, len )
#define nwrite( fd, buf, len ) n_rw( 1, fd, buf, len )

static int
n_rw(
	int do_write,
	int fd,
	void *buf,
	int len
)
{
	int ret = 0, i;
	while( len ) {
		for(;;) {
			i = (do_write
			     ? write( fd, buf, len )
			     :  read( fd, buf, len ));
			if( i < 0 ) {
				if( errno == EINTR )
					continue;
				if( ret == 0 )
					ret = -1;
			}
			break;
		}
		if( i <= 0 )
			break;
		ret += i;
		buf = (char *)buf + i;
		len -= i;
	}
	return ret;
}

void
make_surrogate_parent( void )
{
	int pair[2][2], io[2], i, j, p, argc;
	ber_len_t len, buflen, offset;
	char *buf, **argv;
	pid_t pid;

	if( socketpair( AF_LOCAL, SOCK_STREAM, 0, pair[0] ) < 0 ||
	    socketpair( AF_LOCAL, SOCK_STREAM, 0, pair[1] ) < 0   ) {
		Debug( LDAP_DEBUG_ANY, "socketpair failed\n", 0, 0, 0 );
		exit( EXIT_FAILURE );
	}
	fflush( NULL );
	switch( fork() ) {
	case -1:
		Debug( LDAP_DEBUG_ANY, "fork failed\n", 0, 0, 0 );
		exit( EXIT_FAILURE );
	case 0:
		break;
	default:
		shell_surrogate_fd[0] = pair[0][0];
		shell_surrogate_fd[1] = pair[1][0];
		close( pair[0][1] );
		close( pair[1][1] );
		return;
	}

	/* Close unused file descriptors */
	for( i = 3, j = 32; j && i < 1024; i++ )
		if( i != pair[0][1] && i != pair[1][1] && close( i ) < 0 )
			--j;
		else if( j < 32 )
			j = 32;

	/* Surrogate parent running */

	buflen = 0;
	buf = NULL;
	argc = 0;
	argv = NULL;
	p = 0;

	for(;;) {
		/* Read file descriptors io[] from socket */ 
		static char dummy;
		static struct iovec iov = { &dummy, 1 };
		struct msghdr msg;
# ifdef CMSG_SPACE
		union {
			struct cmsghdr cm;
			char control[CMSG_SPACE(sizeof(io))];
		} control_un;
		struct cmsghdr *cmptr;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
122
123
124
125
126
127
# endif

		/* clear msghdr */
		memset( &msg, 0, sizeof msg );

# ifdef CMSG_SPACE
128
129
130
131
132
133
		msg.msg_control = control_un.control;
		msg.msg_controllen = sizeof(control_un.control);
# else
		msg.msg_accrights = (caddr_t) io;
		msg.msg_accrightslen = sizeof(io);
# endif
Kurt Zeilenga's avatar
Kurt Zeilenga committed
134

135
136
137
138
		msg.msg_name = NULL;
		msg.msg_namelen = 0;
		msg.msg_iov = &iov;
		msg.msg_iovlen = 1;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
139

140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
		switch( recvmsg( pair[p][1], &msg, MSG_WAITALL ) ) {
		case -1:
			if( errno == EINTR )
				continue;
			_exit( EXIT_FAILURE );
		case 0:
			_exit( EXIT_SUCCESS );
		}
# ifdef CMSG_SPACE
		if( (cmptr = CMSG_FIRSTHDR(&msg)) == NULL   ||
		    cmptr->cmsg_len != CMSG_LEN(sizeof(io)) ||
		    cmptr->cmsg_level != SOL_SOCKET         ||
		    cmptr->cmsg_type != SCM_RIGHTS            ) {
			fputs( "bad descriptor message received\n", stderr );
			exit( EXIT_FAILURE );
		}
		memcpy( io, CMSG_DATA( cmptr ), sizeof(io) );
# else
		if( msg.msg_accrightslen != sizeof(io) ) {
			fputs( "bad descriptor message received\n", stderr );
			exit( EXIT_FAILURE );
		}
# endif

		/* Read length of arguments and then arguments from socket */
		if( nread( pair[p][1], &len, sizeof(len) ) != sizeof(len) ) {
			fputs( "bad descriptor message received\n", stderr );
			exit( EXIT_FAILURE );
		}
		if( buflen < len ) {
			buf = realloc( buf, buflen = len );
			if( buf == NULL ) {
				fputs( "realloc failed\n", stderr );
				exit( EXIT_FAILURE );
			}
		}
		if( nread( pair[p][1], buf, len ) != len ) {
			fputs( "bad descriptor message received\n", stderr );
			exit( EXIT_FAILURE );
		}
		i = 0;
		offset = 0;
		while( offset < len ) {
			if( i >= argc-1 ) {
				argc += i + 10;
				argv = realloc( argv, argc * sizeof(*argv) );
				if( argv == NULL ) {
					fputs( "realloc failed\n", stderr );
					exit( EXIT_FAILURE );
				}
			}
			argv[i++] = buf + offset;
			offset += strlen( buf + offset ) + 1;
		}
		argv[i] = NULL;

		/* Run program */
		pid = fork();
		switch( pid )
		{
		case 0:		/* child */
			if( dup2( io[0], 0 ) == -1 || dup2( io[1], 1 ) == -1 ) {
				fputs( "dup2 failed\n", stderr );
				exit( EXIT_FAILURE );
			}
			close( io[0] );
			close( io[1] );
			close( pair[0][1] );
			close( pair[1][1] );
			execv( argv[0], argv );

			fputs( "execv failed\n", stderr );
			exit( EXIT_FAILURE );

		case -1:	/* trouble */
			fputs( "fork failed\n", stderr );
			break;

		default:	/* parent */
			close( io[0] );
			close( io[1] );
			break;
		}
		if( nwrite( pair[p][1], &pid,
		            sizeof(pid_t) ) != sizeof(pid_t) )  {
			fputs( "could not send pid\n", stderr );
			exit( EXIT_FAILURE );
		}
		p ^= 1;
	}
}
#endif /* SHELL_SURROGATE_PARENT */

233
pid_t
Kurt Zeilenga's avatar
Kurt Zeilenga committed
234
forkandexec(
235
    Cmd_info  args,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
236
237
238
239
    FILE	**rfp,
    FILE	**wfp
)
{
240
	int	p2c[2] = { -1, -1 }, c2p[2];
241
	pid_t	pid;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
242
243
244

	if ( pipe( p2c ) != 0 || pipe( c2p ) != 0 ) {
		Debug( LDAP_DEBUG_ANY, "pipe failed\n", 0, 0, 0 );
245
246
		close( p2c[0] );
		close( p2c[1] );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
247
248
249
250
251
252
253
254
255
		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
	 */

256
257
#ifdef SHELL_SURROGATE_PARENT

258
	{
259
260
261
262
263
264
265
266
267
268
		int io[2] = { p2c[0], c2p[1] }, i, c;
		static char dummy = '\0';
		static struct iovec iov = { &dummy, 1 };
		struct msghdr msg;
# ifdef CMSG_SPACE
		union {
			struct cmsghdr cm;
			char control[CMSG_SPACE(sizeof(io))];
		} control_un;
		struct cmsghdr *cmptr;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
269
270
271
272
273
274
# endif

		/* clear msghdr */
		memset( &msg, 0, sizeof msg );

# ifdef CMSG_SPACE
275
276
277
278
279
280
281
282
283
284
285
		msg.msg_control = control_un.control;
		msg.msg_controllen = sizeof(control_un.control);
		cmptr = CMSG_FIRSTHDR(&msg);
		cmptr->cmsg_len = CMSG_LEN(sizeof(io));
		cmptr->cmsg_level = SOL_SOCKET;
		cmptr->cmsg_type = SCM_RIGHTS;
		memcpy( CMSG_DATA(cmptr), io, sizeof(io) );
# else
		msg.msg_accrights = (caddr_t) io;
		msg.msg_accrightslen = sizeof(io);
# endif
Kurt Zeilenga's avatar
Kurt Zeilenga committed
286

287
288
289
290
		msg.msg_name = NULL;
		msg.msg_namelen = 0;
		msg.msg_iov = &iov;
		msg.msg_iovlen = 1;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
291

292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
		ldap_pvt_thread_mutex_lock( &shell_surrogate_index_mutex );
		i = shell_surrogate_index ^= 1;
		ldap_pvt_thread_mutex_unlock( &shell_surrogate_index_mutex );
		ldap_pvt_thread_mutex_lock( &shell_surrogate_fd_mutex[i] );
		c = (sendmsg( shell_surrogate_fd[i], &msg, 0 ) == 1 &&
			 nwrite( shell_surrogate_fd[i], &args.bv_len,
			         sizeof(args.bv_len) ) == sizeof(args.bv_len) &&
			 nwrite( shell_surrogate_fd[i], args.bv_val,
			         args.bv_len ) == args.bv_len &&
			 nread( shell_surrogate_fd[i], &pid,
			        sizeof(pid) ) == sizeof(pid));
		ldap_pvt_thread_mutex_unlock( &shell_surrogate_fd_mutex[i] );
		close( p2c[0] );
		close( c2p[1] );
		if ( !c ) {
			Debug( LDAP_DEBUG_ANY, "process creation failed\n", 0, 0, 0 );
			close( p2c[1] );
			close( c2p[0] );
			close( shell_surrogate_fd[0] );
			close( shell_surrogate_fd[1] );
			shell_surrogate_fd[0] =
				shell_surrogate_fd[1] = -1;
			return( -1 );
		}
	}

#else /* !SHELL_SURROGATE_PARENT */

	fflush( NULL );
# ifdef HAVE_THR
	pid = fork1();
# else
	pid = fork();
# endif
	if ( pid == 0 ) {		/* child */
327
328
329
330
		/*
		 * child could deadlock here due to resources locked
		 * by our parent
		 *
331
		 * If so, configure --without-threads.
332
		 */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
333
334
		if ( dup2( p2c[0], 0 ) == -1 || dup2( c2p[1], 1 ) == -1 ) {
			Debug( LDAP_DEBUG_ANY, "dup2 failed\n", 0, 0, 0 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
335
			exit( EXIT_FAILURE );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
336
		}
337
338
339
340
341
342
343
344
345
	}
	close( p2c[0] );
	close( c2p[1] );
	if ( pid <= 0 ) {
		close( p2c[1] );
		close( c2p[0] );
	}
	switch ( pid ) {
	case 0:
Kurt Zeilenga's avatar
Kurt Zeilenga committed
346
347
348
		execv( args[0], args );

		Debug( LDAP_DEBUG_ANY, "execv failed\n", 0, 0, 0 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
349
		exit( EXIT_FAILURE );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
350
351
352
353
354
355

	case -1:	/* trouble */
		Debug( LDAP_DEBUG_ANY, "fork failed\n", 0, 0, 0 );
		return( -1 );
	}

356
357
358
#endif /* SHELL_SURROGATE_PARENT */

	/* parent */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
359
360
361
362
363
364
365
366
367
368
369
	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 );
}