7 #include <sys/socket.h>
12 extern Operation *op_add();
13 extern int active_threads;
14 extern pthread_mutex_t active_threads_mutex;
15 extern pthread_mutex_t new_conn_mutex;
16 extern long ops_initiated;
17 extern long ops_completed;
18 extern pthread_mutex_t ops_mutex;
19 extern pthread_t listener_tid;
21 #ifdef DECL_SYS_ERRLIST
23 extern char *sys_errlist[];
32 * connection_activity - handle the request operation op on connection
33 * conn. This routine figures out what kind of operation it is and
34 * calls the appropriate stub to handle it.
38 connection_operation( struct co_arg *arg )
42 pthread_mutex_lock( &arg->co_conn->c_opsmutex );
43 arg->co_conn->c_opsinitiated++;
44 pthread_mutex_unlock( &arg->co_conn->c_opsmutex );
46 pthread_mutex_lock( &ops_mutex );
48 pthread_mutex_unlock( &ops_mutex );
50 switch ( arg->co_op->o_tag ) {
52 do_bind( arg->co_conn, arg->co_op );
56 case LDAP_REQ_UNBIND_30:
59 do_unbind( arg->co_conn, arg->co_op );
63 do_add( arg->co_conn, arg->co_op );
67 case LDAP_REQ_DELETE_30:
70 do_delete( arg->co_conn, arg->co_op );
74 do_modrdn( arg->co_conn, arg->co_op );
78 do_modify( arg->co_conn, arg->co_op );
81 case LDAP_REQ_COMPARE:
82 do_compare( arg->co_conn, arg->co_op );
86 do_search( arg->co_conn, arg->co_op );
90 case LDAP_REQ_ABANDON_30:
92 case LDAP_REQ_ABANDON:
93 do_abandon( arg->co_conn, arg->co_op );
97 Debug( LDAP_DEBUG_ANY, "unknown request 0x%x\n",
98 arg->co_op->o_tag, 0, 0 );
102 pthread_mutex_lock( &arg->co_conn->c_opsmutex );
103 arg->co_conn->c_opscompleted++;
104 op_delete( &arg->co_conn->c_ops, arg->co_op );
105 pthread_mutex_unlock( &arg->co_conn->c_opsmutex );
107 free( (char *) arg );
109 pthread_mutex_lock( &ops_mutex );
111 pthread_mutex_unlock( &ops_mutex );
113 pthread_mutex_lock( &active_threads_mutex );
115 pthread_mutex_unlock( &active_threads_mutex );
125 unsigned long tag, len;
130 if ( conn->c_currentber == NULL && (conn->c_currentber = ber_alloc())
132 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
137 if ( (tag = ber_get_next( &conn->c_sb, &len, conn->c_currentber ))
138 != LDAP_TAG_MESSAGE ) {
139 Debug( LDAP_DEBUG_TRACE,
140 "ber_get_next on fd %d failed errno %d (%s)\n",
141 conn->c_sb.sb_sd, errno, errno > -1 && errno < sys_nerr ?
142 sys_errlist[errno] : "unknown" );
143 Debug( LDAP_DEBUG_TRACE, "*** got %d of %d so far\n",
144 conn->c_currentber->ber_rwptr - conn->c_currentber->ber_buf,
145 conn->c_currentber->ber_len, 0 );
147 if ( errno != EWOULDBLOCK && errno != EAGAIN ) {
148 /* log, close and send error */
149 ber_free( conn->c_currentber, 1 );
150 conn->c_currentber = NULL;
152 close_connection( conn, conn->c_connid, -1 );
157 ber = conn->c_currentber;
158 conn->c_currentber = NULL;
160 if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
161 /* log, close and send error */
162 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%x\n", tag, 0,
166 close_connection( conn, conn->c_connid, -1 );
170 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
171 /* log, close and send error */
172 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%x\n", tag, 0,
176 close_connection( conn, conn->c_connid, -1 );
181 if ( conn->c_version == 30 ) {
182 (void) ber_skip_tag( ber, &len );
186 arg = (struct co_arg *) ch_malloc( sizeof(struct co_arg) );
189 pthread_mutex_lock( &conn->c_dnmutex );
190 if ( conn->c_dn != NULL ) {
191 tmpdn = strdup( conn->c_dn );
195 pthread_mutex_unlock( &conn->c_dnmutex );
197 pthread_mutex_lock( &conn->c_opsmutex );
198 arg->co_op = op_add( &conn->c_ops, ber, msgid, tag, tmpdn,
199 conn->c_opsinitiated, conn->c_connid );
200 pthread_mutex_unlock( &conn->c_opsmutex );
202 if ( tmpdn != NULL ) {
206 pthread_attr_init( &attr );
207 pthread_attr_setdetachstate( &attr, PTHREAD_CREATE_DETACHED );
208 #ifndef THREAD_MIT_PTHREADS
209 /* POSIX_THREADS or compatible
210 * This is a draft 10 or standard pthreads implementation
212 if ( pthread_create( &arg->co_op->o_tid, &attr,
213 (void *) connection_operation, (void *) arg ) != 0 ) {
214 Debug( LDAP_DEBUG_ANY, "pthread_create failed\n", 0, 0, 0 );
216 pthread_mutex_lock( &active_threads_mutex );
218 pthread_mutex_unlock( &active_threads_mutex );
220 #else /* !THREAD_MIT_PTHREAD */
222 * This is a draft 4 or earlier pthreads implementation
224 if ( pthread_create( &arg->co_op->o_tid, attr,
225 (void *) connection_operation, (void *) arg ) != 0 ) {
226 Debug( LDAP_DEBUG_ANY, "pthread_create failed\n", 0, 0, 0 );
228 pthread_mutex_lock( &active_threads_mutex );
230 pthread_mutex_unlock( &active_threads_mutex );
232 #endif /* !THREAD_MIT_PTHREAD */
233 pthread_attr_destroy( &attr );