connection.c 5.82 KB
Newer Older
Kurt Zeilenga's avatar
Kurt Zeilenga committed
1
#include "portable.h"
Kurt Zeilenga's avatar
Kurt Zeilenga committed
2
3
4
5
6
7
8
9
10

#include <stdio.h>

#include <ac/errno.h>
#include <ac/signal.h>
#include <ac/socket.h>
#include <ac/string.h>
#include <ac/time.h>

Kurt Zeilenga's avatar
Kurt Zeilenga committed
11
12
13
14
15
16
17
18
19
20
21
22
23
#include "slap.h"

struct co_arg {
	Connection	*co_conn;
	Operation	*co_op;
};

/*
 * connection_activity - handle the request operation op on connection
 * conn.  This routine figures out what kind of operation it is and
 * calls the appropriate stub to handle it.
 */

24
25
static void *
connection_operation( void *arg_v )
Kurt Zeilenga's avatar
Kurt Zeilenga committed
26
{
27
	struct co_arg	*arg = arg_v;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
	unsigned long	len;

	pthread_mutex_lock( &arg->co_conn->c_opsmutex );
	arg->co_conn->c_opsinitiated++;
	pthread_mutex_unlock( &arg->co_conn->c_opsmutex );

	pthread_mutex_lock( &ops_mutex );
	ops_initiated++;
	pthread_mutex_unlock( &ops_mutex );

	switch ( arg->co_op->o_tag ) {
	case LDAP_REQ_BIND:
		do_bind( arg->co_conn, arg->co_op );
		break;

Kurt Zeilenga's avatar
Kurt Zeilenga committed
43
#ifdef LDAP_COMPAT30
Kurt Zeilenga's avatar
Kurt Zeilenga committed
44
45
46
47
48
49
50
51
52
53
	case LDAP_REQ_UNBIND_30:
#endif
	case LDAP_REQ_UNBIND:
		do_unbind( arg->co_conn, arg->co_op );
		break;

	case LDAP_REQ_ADD:
		do_add( arg->co_conn, arg->co_op );
		break;

Kurt Zeilenga's avatar
Kurt Zeilenga committed
54
#ifdef LDAP_COMPAT30
Kurt Zeilenga's avatar
Kurt Zeilenga committed
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
	case LDAP_REQ_DELETE_30:
#endif
	case LDAP_REQ_DELETE:
		do_delete( arg->co_conn, arg->co_op );
		break;

	case LDAP_REQ_MODRDN:
		do_modrdn( arg->co_conn, arg->co_op );
		break;

	case LDAP_REQ_MODIFY:
		do_modify( arg->co_conn, arg->co_op );
		break;

	case LDAP_REQ_COMPARE:
		do_compare( arg->co_conn, arg->co_op );
		break;

	case LDAP_REQ_SEARCH:
		do_search( arg->co_conn, arg->co_op );
		break;

Kurt Zeilenga's avatar
Kurt Zeilenga committed
77
#ifdef LDAP_COMPAT30
Kurt Zeilenga's avatar
Kurt Zeilenga committed
78
79
80
81
82
83
84
	case LDAP_REQ_ABANDON_30:
#endif
	case LDAP_REQ_ABANDON:
		do_abandon( arg->co_conn, arg->co_op );
		break;

	default:
85
		Debug( LDAP_DEBUG_ANY, "unknown request 0x%lx\n",
Kurt Zeilenga's avatar
Kurt Zeilenga committed
86
87
88
89
90
91
		    arg->co_op->o_tag, 0, 0 );
		break;
	}

	pthread_mutex_lock( &arg->co_conn->c_opsmutex );
	arg->co_conn->c_opscompleted++;
92
	slap_op_delete( &arg->co_conn->c_ops, arg->co_op );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
93
94
95
96
97
98
99
100
101
102
	pthread_mutex_unlock( &arg->co_conn->c_opsmutex );

	free( (char *) arg );

	pthread_mutex_lock( &ops_mutex );
	ops_completed++;
	pthread_mutex_unlock( &ops_mutex );

	pthread_mutex_lock( &active_threads_mutex );
	active_threads--;
103
104
105
	if( active_threads < 1 ) {
		pthread_cond_signal(&active_threads_cond);
	}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
106
	pthread_mutex_unlock( &active_threads_mutex );
107
	return NULL;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
108
109
110
111
112
113
114
}

void
connection_activity(
    Connection *conn
)
{
115
#ifndef HAVE_PTHREAD_DETACH
Kurt Zeilenga's avatar
Kurt Zeilenga committed
116
	pthread_attr_t	attr;
117
#endif
118
	int status;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
	struct co_arg	*arg;
	unsigned long	tag, len;
	long		msgid;
	BerElement	*ber;
	char		*tmpdn;

	if ( conn->c_currentber == NULL && (conn->c_currentber = ber_alloc())
	    == NULL ) {
		Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
		return;
	}

	errno = 0;
	if ( (tag = ber_get_next( &conn->c_sb, &len, conn->c_currentber ))
	    != LDAP_TAG_MESSAGE ) {
		Debug( LDAP_DEBUG_TRACE,
		    "ber_get_next on fd %d failed errno %d (%s)\n",
		    conn->c_sb.sb_sd, errno, errno > -1 && errno < sys_nerr ?
		    sys_errlist[errno] : "unknown" );
Hallvard Furuseth's avatar
Hallvard Furuseth committed
138
139
		Debug( LDAP_DEBUG_TRACE, "*** got %ld of %lu so far\n",
		    (long)(conn->c_currentber->ber_rwptr - conn->c_currentber->ber_buf),
Kurt Zeilenga's avatar
Kurt Zeilenga committed
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
		    conn->c_currentber->ber_len, 0 );

		if ( errno != EWOULDBLOCK && errno != EAGAIN ) {
			/* log, close and send error */
			ber_free( conn->c_currentber, 1 );
			conn->c_currentber = NULL;

			close_connection( conn, conn->c_connid, -1 );
		}

		return;
	}
	ber = conn->c_currentber;
	conn->c_currentber = NULL;

	if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
		/* log, close and send error */
157
		Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n", tag, 0,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
158
159
160
161
162
163
164
165
166
		    0 );
		ber_free( ber, 1 );

		close_connection( conn, conn->c_connid, -1 );
		return;
	}

	if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
		/* log, close and send error */
167
		Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n", tag, 0,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
168
169
170
171
172
173
174
		    0 );
		ber_free( ber, 1 );

		close_connection( conn, conn->c_connid, -1 );
		return;
	}

Kurt Zeilenga's avatar
Kurt Zeilenga committed
175
#ifdef LDAP_COMPAT30
Kurt Zeilenga's avatar
Kurt Zeilenga committed
176
177
178
179
180
181
182
183
184
185
	if ( conn->c_version == 30 ) {
		(void) ber_skip_tag( ber, &len );
	}
#endif

	arg = (struct co_arg *) ch_malloc( sizeof(struct co_arg) );
	arg->co_conn = conn;

	pthread_mutex_lock( &conn->c_dnmutex );
	if ( conn->c_dn != NULL ) {
186
		tmpdn = ch_strdup( conn->c_dn );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
187
188
189
190
191
192
	} else {
		tmpdn = NULL;
	}
	pthread_mutex_unlock( &conn->c_dnmutex );

	pthread_mutex_lock( &conn->c_opsmutex );
193
	arg->co_op = slap_op_add( &conn->c_ops, ber, msgid, tag, tmpdn,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
194
195
196
197
198
199
200
	    conn->c_opsinitiated, conn->c_connid );
	pthread_mutex_unlock( &conn->c_opsmutex );

	if ( tmpdn != NULL ) {
		free( tmpdn );
	}

201
#ifdef HAVE_PTHREAD_DETACH
202
	if ( status = pthread_create( &arg->co_op->o_tid, NULL,
203
	    connection_operation, (void *) arg ) != 0 ) {
204
		Debug( LDAP_DEBUG_ANY, "pthread_create failed (%d)\n", status, 0, 0 );
205
206
207
208
209
210
211
212
213
214
215
216
217
218
	} else {
		pthread_mutex_lock( &active_threads_mutex );
		active_threads++;
		pthread_mutex_unlock( &active_threads_mutex );
	}

#if !defined(HAVE_PTHREADS_D4)
	pthread_detach( arg->co_op->o_tid );
#else
	pthread_detach( &arg->co_op->o_tid );
#endif

#else /* !pthread detach */

Kurt Zeilenga's avatar
Kurt Zeilenga committed
219
220
	pthread_attr_init( &attr );
	pthread_attr_setdetachstate( &attr, PTHREAD_CREATE_DETACHED );
221
#if !defined(HAVE_PTHREADS_D4)
222
	/* POSIX_THREADS or compatible
Kurt Zeilenga's avatar
Kurt Zeilenga committed
223
224
	 * This is a draft 10 or standard pthreads implementation
	 */
225
	if ( status = pthread_create( &arg->co_op->o_tid, &attr,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
226
	    connection_operation, (void *) arg ) != 0 ) {
227
		Debug( LDAP_DEBUG_ANY, "pthread_create failed (%d)\n", status, 0, 0 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
228
229
230
231
232
	} else {
		pthread_mutex_lock( &active_threads_mutex );
		active_threads++;
		pthread_mutex_unlock( &active_threads_mutex );
	}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
233
#else	/* pthread draft4  */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
234
235
236
	/*
	 * This is a draft 4 or earlier pthreads implementation
	 */
237
	if ( status = pthread_create( &arg->co_op->o_tid, attr,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
238
	    connection_operation, (void *) arg ) != 0 ) {
239
		Debug( LDAP_DEBUG_ANY, "pthread_create failed (%d)\n", status, 0, 0 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
240
241
242
243
244
	} else {
		pthread_mutex_lock( &active_threads_mutex );
		active_threads++;
		pthread_mutex_unlock( &active_threads_mutex );
	}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
245
#endif	/* pthread draft4 */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
246
	pthread_attr_destroy( &attr );
247
#endif
Kurt Zeilenga's avatar
Kurt Zeilenga committed
248
}