]> git.sur5r.net Git - openldap/blob - servers/slapd/connection.c
Don't have connection_state_closing() lock connection. Already locked
[openldap] / servers / slapd / connection.c
1 #include "portable.h"
2
3 #include <stdio.h>
4
5 #include <ac/errno.h>
6 #include <ac/signal.h>
7 #include <ac/socket.h>
8 #include <ac/string.h>
9 #include <ac/time.h>
10
11 #include "slap.h"
12
13 #ifdef HAVE_WINSOCK
14 #define EWOULDBLOCK WSAEWOULDBLOCK
15 #endif
16
17 /* protected by connections_mutex */
18 static ldap_pvt_thread_mutex_t connections_mutex;
19 static Connection *connections = NULL;
20 static int conn_index = -1;
21 static long conn_nextid = 0;
22
23 /* structure state (protected by connections_mutex) */
24 #define SLAP_C_UNINITIALIZED    0x0     /* MUST BE ZERO (0) */
25 #define SLAP_C_UNUSED                   0x1
26 #define SLAP_C_USED                             0x2
27
28 /* connection state (protected by c_mutex ) */
29 #define SLAP_C_INVALID                  0x0     /* MUST BE ZERO (0) */
30 #define SLAP_C_INACTIVE                 0x1     /* zero threads */
31 #define SLAP_C_ACTIVE                   0x2 /* one or more threads */
32 #define SLAP_C_BINDING                  0x3     /* binding */
33 #define SLAP_C_CLOSING                  0x4     /* closing */
34
35 void slapd_remove(int s);
36 static Connection* connection_get( int s );
37
38 static int connection_input( Connection *c );
39 static void connection_close( Connection *c );
40
41 static int connection_op_activate( Connection *conn, Operation *op );
42 static int connection_resched( Connection *conn );
43
44 struct co_arg {
45         Connection      *co_conn;
46         Operation       *co_op;
47 };
48
49 /*
50  * Initialize connection management infrastructure.
51  */
52 int connections_init(void)
53 {
54         int i;
55
56         assert( connections == NULL );
57
58         if( connections != NULL) {
59                 Debug( LDAP_DEBUG_ANY, "connections_init: already initialized.\n",
60                         0, 0, 0 );
61                 return -1;
62         }
63
64         /* should check return of every call */
65         ldap_pvt_thread_mutex_init( &connections_mutex );
66
67         connections = (Connection *) calloc( dtblsize, sizeof(Connection) );
68
69         if( connections == NULL ) {
70                 Debug( LDAP_DEBUG_ANY,
71                         "connections_init: allocation (%d*%ld) of connection array failed.\n",
72                         dtblsize, (long) sizeof(Connection), 0 );
73                 return -1;
74         }
75
76         for ( i = 0; i < dtblsize; i++ )
77                 memset( &connections[i], 0, sizeof(Connection) );
78
79         /*
80          * per entry initialization of the Connection array initialization
81          * will be done by connection_init()
82          */ 
83
84         return 0;
85 }
86
87 /*
88  * Destroy connection management infrastructure.
89  */
90 int connections_destroy(void)
91 {
92         int i;
93
94         /* should check return of every call */
95
96         if( connections == NULL) {
97                 Debug( LDAP_DEBUG_ANY, "connections_destroy: nothing to destroy.\n",
98                         0, 0, 0 );
99                 return -1;
100         }
101
102         for ( i = 0; i < dtblsize; i++ ) {
103                 ldap_pvt_thread_mutex_destroy( &connections[i].c_mutex );
104                 ldap_pvt_thread_mutex_destroy( &connections[i].c_write_mutex );
105                 ldap_pvt_thread_cond_destroy( &connections[i].c_write_cv );
106
107                 free( &connections[i] );
108         }
109
110         free( connections );
111         connections = NULL;
112
113         ldap_pvt_thread_mutex_destroy( &connections_mutex );
114         return 0;
115 }
116
117 /*
118  * shutdown all connections
119  */
120 int connections_shutdown(void)
121 {
122         int i;
123
124         ldap_pvt_thread_mutex_lock( &connections_mutex );
125
126         for ( i = 0; i < dtblsize; i++ ) {
127                 if( connections[i].c_struct_state != SLAP_C_USED ) {
128                         continue;
129                 }
130
131                 ldap_pvt_thread_mutex_lock( &connections[i].c_mutex );
132                 connection_closing( &connections[i] );
133                 connection_close( &connections[i] );
134                 ldap_pvt_thread_mutex_unlock( &connections[i].c_mutex );
135         }
136
137         ldap_pvt_thread_mutex_unlock( &connections_mutex );
138 }
139
140 static Connection* connection_get( int s )
141 {
142         Connection *c = NULL;
143
144         assert( connections != NULL );
145
146         if(s < 0) {
147                 return NULL;
148         }
149
150 #ifndef HAVE_WINSOCK
151         assert( connections[s].c_struct_state == SLAP_C_USED );
152         assert( connections[s].c_conn_state != SLAP_C_INVALID );
153         assert( connections[s].c_sb.sb_sd != -1 );
154
155         c = &connections[s];
156 #else
157         {
158                 int i;
159
160                 for(i=0; i<dtblsize; i++) {
161                         if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
162                                 assert( connections[i].c_conn_state == SLAP_C_INVALID );
163                                 assert( connections[i].c_sb.sb_sd == 0 );
164                                 break;
165                         }
166
167                         if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
168                                 assert( connections[i].c_conn_state == SLAP_C_INVALID );
169                                 assert( connections[i].c_sb.sb_sd == -1 );
170                                 continue;
171                         }
172
173                         assert( connections[i].c_struct_state == SLAP_C_USED );
174                         assert( connections[i].c_conn_state != SLAP_C_INVALID );
175                         assert( connections[i].c_sb.sb_sd != -1 );
176
177                         if( connections[i].c_sb.sb_sd == s ) {
178                                 c = &connections[i];
179                                 break;
180                         }
181                 }
182         }
183 #endif
184
185         if( c != NULL ) {
186                 /* we do this BEFORE locking to aid in debugging */
187                 Debug( LDAP_DEBUG_TRACE,
188                         "connection_get(%d): got connid=%ld\n",
189                         s, c->c_connid, 0 );
190
191                 ldap_pvt_thread_mutex_lock( &c->c_mutex );
192         }
193         return c;
194 }
195
196 static void connection_return( Connection *c )
197 {
198         ldap_pvt_thread_mutex_unlock( &c->c_mutex );
199 }
200
201 long connection_init(
202         int s,
203         const char* name,
204         const char* addr)
205 {
206         long id;
207         Connection *c;
208         assert( connections != NULL );
209
210         if( s < 0 ) {
211                 return -1;
212         }
213
214         assert( s >= 0 );
215 #ifndef HAVE_WINSOCK
216         assert( s < dtblsize );
217 #endif
218
219         ldap_pvt_thread_mutex_lock( &connections_mutex );
220
221 #ifndef HAVE_WINSOCK
222         c = &connections[s];
223
224 #else
225         {
226                 int i;
227
228         for( i=0; i < dtblsize; i++) {
229             if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
230                 assert( connections[i].c_sb.sb_sd == 0 );
231                 c = &connections[i];
232                 break;
233             }
234
235             if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
236                 assert( connections[i].c_sb.sb_sd == -1 );
237                 c = &connections[i];
238                 break;
239             }
240
241             assert( connections[i].c_struct_state == SLAP_C_USED );
242             assert( connections[i].c_conn_state != SLAP_C_INVALID );
243             assert( connections[i].c_sb.sb_sd != -1 );
244         }
245
246         if( c == NULL ) {
247             ldap_pvt_thread_mutex_unlock( &connections_mutex );
248             return -1;
249         }
250     }
251 #endif
252
253     assert( c != NULL );
254     assert( c->c_struct_state != SLAP_C_USED );
255     assert( c->c_conn_state == SLAP_C_INVALID );
256
257     if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
258         c->c_dn = NULL;
259         c->c_cdn = NULL;
260         c->c_client_name = NULL;
261         c->c_client_addr = NULL;
262         c->c_ops = NULL;
263         c->c_pending_ops = NULL;
264
265         lber_pvt_sb_init( &c->c_sb );
266
267         /* should check status of thread calls */
268         ldap_pvt_thread_mutex_init( &c->c_mutex );
269         ldap_pvt_thread_mutex_init( &c->c_write_mutex );
270         ldap_pvt_thread_cond_init( &c->c_write_cv );
271
272         c->c_struct_state = SLAP_C_UNUSED;
273     }
274
275     ldap_pvt_thread_mutex_lock( &c->c_mutex );
276
277     assert( c->c_struct_state == SLAP_C_UNUSED );
278     assert(     c->c_dn == NULL );
279     assert(     c->c_cdn == NULL );
280     assert( c->c_client_name == NULL );
281     assert( c->c_client_addr == NULL );
282     assert( c->c_ops == NULL );
283     assert( c->c_pending_ops == NULL );
284
285     c->c_client_name = ch_strdup( name == NULL ? "" : name );
286     c->c_client_addr = ch_strdup( addr );
287
288     c->c_n_ops_received = 0;
289 #ifdef LDAP_COUNTERS
290     c->c_n_ops_executing = 0;
291     c->c_n_ops_pending = 0;
292     c->c_n_ops_completed = 0;
293 #endif
294
295     c->c_starttime = slap_get_time();
296
297     lber_pvt_sb_set_desc( &c->c_sb, s );
298     lber_pvt_sb_set_io( &c->c_sb, &lber_pvt_sb_io_tcp, NULL );
299
300     if( lber_pvt_sb_set_nonblock( &c->c_sb, 1 ) < 0 ) {
301         Debug( LDAP_DEBUG_ANY,
302             "connection_init(%d, %s, %s): set nonblocking failed\n",
303             s, c->c_client_name, c->c_client_addr);
304     }
305
306     id = c->c_connid = conn_nextid++;
307
308     c->c_conn_state = SLAP_C_INACTIVE;
309     c->c_struct_state = SLAP_C_USED;
310
311     ldap_pvt_thread_mutex_unlock( &c->c_mutex );
312     ldap_pvt_thread_mutex_unlock( &connections_mutex );
313
314     return id;
315 }
316
317 static void
318 connection_destroy( Connection *c )
319 {
320     assert( connections != NULL );
321     assert( c != NULL );
322     assert( c->c_struct_state != SLAP_C_UNUSED );
323     assert( c->c_conn_state != SLAP_C_INVALID );
324     assert( c->c_ops == NULL );
325
326     c->c_struct_state = SLAP_C_UNUSED;
327     c->c_conn_state = SLAP_C_INVALID;
328
329     c->c_version = 0;
330     c->c_protocol = 0;
331
332     c->c_starttime = 0;
333
334     if(c->c_dn != NULL) {
335         free(c->c_dn);
336         c->c_dn = NULL;
337     }
338         if(c->c_cdn != NULL) {
339                 free(c->c_cdn);
340                 c->c_cdn = NULL;
341         }
342         if(c->c_client_name != NULL) {
343                 free(c->c_client_name);
344                 c->c_client_name = NULL;
345         }
346         if(c->c_client_addr != NULL) {
347                 free(c->c_client_addr);
348                 c->c_client_addr = NULL;
349         }
350
351         if ( lber_pvt_sb_in_use(&c->c_sb) ) {
352                 int sd = lber_pvt_sb_get_desc(&c->c_sb);
353
354                 slapd_remove( sd );
355                 lber_pvt_sb_close( &c->c_sb );
356
357                 Statslog( LDAP_DEBUG_STATS,
358                     "conn=%d fd=%d closed.\n",
359                         c->c_connid, sd, 0, 0, 0 );
360         }
361
362         lber_pvt_sb_destroy( &c->c_sb );
363 }
364
365 int connection_state_closing( Connection *c )
366 {
367         /* connection must be locked by caller */
368         int state;
369         assert( c != NULL );
370         assert( c->c_struct_state == SLAP_C_USED );
371
372         state = c->c_conn_state;
373
374         assert( state != SLAP_C_INVALID );
375
376         return state == SLAP_C_CLOSING;
377 }
378
379 void connection_closing( Connection *c )
380 {
381         assert( connections != NULL );
382         assert( c != NULL );
383         assert( c->c_struct_state == SLAP_C_USED );
384         assert( c->c_conn_state != SLAP_C_INVALID );
385
386         if( c->c_conn_state != SLAP_C_CLOSING ) {
387                 Operation *o;
388
389                 Debug( LDAP_DEBUG_TRACE,
390                         "connection_closing: readying conn=%ld sd=%d for close.\n",
391                         c->c_connid, c->c_sb.sb_sd, 0 );
392
393                 /* update state to closing */
394                 c->c_conn_state = SLAP_C_CLOSING;
395
396                 /* don't listen on this port anymore */
397                 slapd_clr_read( c->c_sb.sb_sd, 1 );
398
399                 /* shutdown I/O -- not yet implemented */
400
401                 /* abandon active operations */
402                 for( o = c->c_ops; o != NULL; o = o->o_next ) {
403                         ldap_pvt_thread_mutex_lock( &o->o_abandonmutex );
404                         o->o_abandon = 1;
405                         ldap_pvt_thread_mutex_unlock( &o->o_abandonmutex );
406                 }
407
408                 /* remove pending operations */
409                 for( o = slap_op_pop( &c->c_pending_ops );
410                         o != NULL;
411                         o = slap_op_pop( &c->c_pending_ops ) )
412                 {
413                         slap_op_free( o );
414                 }
415
416                 /* wake write blocked operations */
417                 slapd_clr_write( c->c_sb.sb_sd, 1 );
418                 ldap_pvt_thread_cond_signal( &c->c_write_cv );
419         }
420 }
421
422 static void connection_close( Connection *c )
423 {
424         assert( connections != NULL );
425         assert( c != NULL );
426         assert( c->c_struct_state == SLAP_C_USED );
427         assert( c->c_conn_state == SLAP_C_CLOSING );
428
429         if( c->c_ops != NULL ) {
430                 Debug( LDAP_DEBUG_TRACE,
431                         "connection_close: deferring conn=%ld sd=%d.\n",
432                         c->c_connid, c->c_sb.sb_sd, 0 );
433
434                 return;
435         }
436
437         Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%ld sd=%d.\n",
438                 c->c_connid, c->c_sb.sb_sd, 0 );
439
440         connection_destroy( c );
441 }
442
443 long connections_nextid(void)
444 {
445         long id;
446         assert( connections != NULL );
447
448         ldap_pvt_thread_mutex_lock( &connections_mutex );
449
450         id = conn_nextid;
451
452         ldap_pvt_thread_mutex_unlock( &connections_mutex );
453
454         return id;
455 }
456
457 Connection* connection_first(void)
458 {
459         assert( connections != NULL );
460
461         ldap_pvt_thread_mutex_lock( &connections_mutex );
462
463         assert( conn_index == -1 );
464         conn_index = 0;
465
466         return connection_next(NULL);
467 }
468
469 Connection* connection_next(Connection *c)
470 {
471         assert( connections != NULL );
472         assert( conn_index != -1 );
473         assert( conn_index <= dtblsize );
474
475         if( c != NULL ) {
476                 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
477         }
478
479         c = NULL;
480
481         for(; conn_index < dtblsize; conn_index++) {
482                 if( connections[conn_index].c_struct_state == SLAP_C_UNINITIALIZED ) {
483                         assert( connections[conn_index].c_conn_state == SLAP_C_INVALID );
484 #ifndef HAVE_WINSOCK
485                         continue;
486 #else
487                         break;
488 #endif
489                 }
490
491                 if( connections[conn_index].c_struct_state == SLAP_C_USED ) {
492                         assert( connections[conn_index].c_conn_state != SLAP_C_INVALID );
493                         c = &connections[conn_index++];
494                         break;
495                 }
496
497                 assert( connections[conn_index].c_struct_state == SLAP_C_UNUSED );
498                 assert( connections[conn_index].c_conn_state == SLAP_C_INVALID );
499         }
500
501         if( c != NULL ) {
502                 ldap_pvt_thread_mutex_lock( &c->c_mutex );
503         }
504
505         return c;
506 }
507
508 void connection_done(Connection *c)
509 {
510         assert( connections != NULL );
511         assert( conn_index != -1 );
512         assert( conn_index <= dtblsize );
513
514         if( c != NULL ) {
515                 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
516         }
517
518         conn_index = -1;
519         ldap_pvt_thread_mutex_unlock( &connections_mutex );
520 }
521
522 /*
523  * connection_activity - handle the request operation op on connection
524  * conn.  This routine figures out what kind of operation it is and
525  * calls the appropriate stub to handle it.
526  */
527
528 static void *
529 connection_operation( void *arg_v )
530 {
531         struct co_arg   *arg = arg_v;
532         int tag = arg->co_op->o_tag;
533         Connection *conn = arg->co_conn;
534
535 #ifdef LDAP_COUNTERS
536         ldap_pvt_thread_mutex_lock( &num_ops_mutex );
537         num_ops_initiated++;
538         ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
539 #endif
540
541         switch ( tag ) {
542         case LDAP_REQ_BIND:
543                 do_bind( conn, arg->co_op );
544                 break;
545
546 #ifdef LDAP_COMPAT30
547         case LDAP_REQ_UNBIND_30:
548 #endif
549         case LDAP_REQ_UNBIND:
550                 do_unbind( conn, arg->co_op );
551                 break;
552
553         case LDAP_REQ_ADD:
554                 do_add( conn, arg->co_op );
555                 break;
556
557 #ifdef LDAP_COMPAT30
558         case LDAP_REQ_DELETE_30:
559 #endif
560         case LDAP_REQ_DELETE:
561                 do_delete( conn, arg->co_op );
562                 break;
563
564         case LDAP_REQ_MODRDN:
565                 do_modrdn( conn, arg->co_op );
566                 break;
567
568         case LDAP_REQ_MODIFY:
569                 do_modify( conn, arg->co_op );
570                 break;
571
572         case LDAP_REQ_COMPARE:
573                 do_compare( conn, arg->co_op );
574                 break;
575
576         case LDAP_REQ_SEARCH:
577                 do_search( conn, arg->co_op );
578                 break;
579
580 #ifdef LDAP_COMPAT30
581         case LDAP_REQ_ABANDON_30:
582 #endif
583         case LDAP_REQ_ABANDON:
584                 do_abandon( conn, arg->co_op );
585                 break;
586
587         default:
588                 Debug( LDAP_DEBUG_ANY, "unknown request 0x%lx\n",
589                     arg->co_op->o_tag, 0, 0 );
590                 break;
591         }
592
593 #ifdef LDAP_COUNTERS
594         ldap_pvt_thread_mutex_lock( &num_ops_mutex );
595         num_ops_completed++;
596         ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
597 #endif
598
599         ldap_pvt_thread_mutex_lock( &conn->c_mutex );
600
601 #ifdef LDAP_COUNTERS
602         conn->c_n_ops_completed++;
603 #endif
604
605         slap_op_remove( &conn->c_ops, arg->co_op );
606         slap_op_free( arg->co_op );
607         arg->co_op = NULL;
608         arg->co_conn = NULL;
609         free( (char *) arg );
610         arg = NULL;
611
612         switch( tag ) {
613 #ifdef LDAP_COMPAT30
614         case LDAP_REQ_UNBIND_30:
615 #endif
616         case LDAP_REQ_UNBIND:
617                 connection_closing( conn );
618                 break;
619
620         case LDAP_REQ_BIND:
621                 if( conn->c_conn_state == SLAP_C_BINDING) {
622                         conn->c_conn_state = SLAP_C_ACTIVE;
623                 }
624         }
625
626         if( conn->c_conn_state == SLAP_C_CLOSING ) {
627                 Debug( LDAP_DEBUG_TRACE,
628                         "connection_operation: attempting closing conn=%ld sd=%d.\n",
629                         conn->c_connid, conn->c_sb.sb_sd, 0 );
630
631                 connection_close( conn );
632         }
633
634         ldap_pvt_thread_mutex_lock( &active_threads_mutex );
635         active_threads--;
636         if( active_threads < 1 ) {
637                 ldap_pvt_thread_cond_signal(&active_threads_cond);
638         }
639         ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
640
641         connection_resched( conn );
642
643         ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
644
645         return NULL;
646 }
647
648 int connection_read(int s)
649 {
650         int rc = 0;
651         Connection *c;
652         assert( connections != NULL );
653
654         ldap_pvt_thread_mutex_lock( &connections_mutex );
655
656         c = connection_get( s );
657         if( c == NULL ) {
658                 Debug( LDAP_DEBUG_ANY,
659                         "connection_read(%d): no connection!\n",
660                         s, 0, 0 );
661                 ldap_pvt_thread_mutex_unlock( &connections_mutex );
662                 return -1;
663         }
664
665         if( c->c_conn_state == SLAP_C_CLOSING ) {
666                 Debug( LDAP_DEBUG_TRACE,
667                         "connection_read(%d): closing, ignoring input for id=%ld\n",
668                         s, c->c_connid, 0 );
669
670                 connection_return( c );
671                 ldap_pvt_thread_mutex_unlock( &connections_mutex );
672                 return 0;
673         }
674
675         Debug( LDAP_DEBUG_TRACE,
676                 "connection_read(%d): checking for input on id=%ld\n",
677                 s, c->c_connid, 0 );
678
679 #define CONNECTION_INPUT_LOOP 1
680
681 #ifdef DATA_READY_LOOP
682         while(!rc && lber_pvt_sb_data_ready(&c->c_sb))
683 #elif CONNECTION_INPUT_LOOP
684         while(!rc)
685 #endif
686         {
687                 rc = connection_input( c );
688         }
689
690         if( rc < 0 ) {
691                 Debug( LDAP_DEBUG_TRACE,
692                         "connection_read(%d): input error=%d id=%ld, closing.\n",
693                         s, rc, c->c_connid );
694
695                 connection_closing( c );
696                 connection_close( c );
697         }
698
699         connection_return( c );
700         ldap_pvt_thread_mutex_unlock( &connections_mutex );
701         return 0;
702 }
703
704 static int
705 connection_input(
706     Connection *conn
707 )
708 {
709         Operation *op;
710         unsigned long   tag, len;
711         long            msgid;
712         BerElement      *ber;
713
714         if ( conn->c_currentber == NULL && (conn->c_currentber = ber_alloc())
715             == NULL ) {
716                 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
717                 return -1;
718         }
719
720         errno = 0;
721         if ( (tag = ber_get_next( &conn->c_sb, &len, conn->c_currentber ))
722             != LDAP_TAG_MESSAGE )
723         {
724                 int err = errno;
725
726                 Debug( LDAP_DEBUG_TRACE,
727                         "ber_get_next on fd %d failed errno %d (%s)\n",
728                         lber_pvt_sb_get_desc(&conn->c_sb), err,
729                         err > -1 && err < sys_nerr ?  sys_errlist[err] : "unknown" );
730                 Debug( LDAP_DEBUG_TRACE,
731                         "\t*** got %ld of %lu so far\n",
732                         (long)(conn->c_currentber->ber_rwptr - conn->c_currentber->ber_buf),
733                         conn->c_currentber->ber_len, 0 );
734
735                 if ( err != EWOULDBLOCK && err != EAGAIN ) {
736                         /* log, close and send error */
737                         ber_free( conn->c_currentber, 1 );
738                         conn->c_currentber = NULL;
739
740                         return -2;
741                 }
742                 return 1;
743         }
744
745         ber = conn->c_currentber;
746         conn->c_currentber = NULL;
747
748         if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
749                 /* log, close and send error */
750                 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n", tag, 0,
751                     0 );
752                 ber_free( ber, 1 );
753                 return -1;
754         }
755
756         if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
757                 /* log, close and send error */
758                 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n", tag, 0,
759                     0 );
760                 ber_free( ber, 1 );
761
762                 return -1;
763         }
764
765 #ifdef LDAP_COMPAT30
766         if ( conn->c_version == 30 ) {
767                 (void) ber_skip_tag( ber, &len );
768         }
769 #endif
770
771         op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++ );
772
773         if ( conn->c_conn_state == SLAP_C_BINDING
774                 || conn->c_conn_state == SLAP_C_CLOSING )
775         {
776                 Debug( LDAP_DEBUG_ANY, "deferring operation\n", 0, 0, 0 );
777                 slap_op_add( &conn->c_pending_ops, op );
778
779         } else {
780                 connection_op_activate( conn, op );
781         }
782
783 #ifdef NO_THREADS
784         if ( conn->c_struct_state != SLAP_C_USED ) {
785                 /* connection must have got closed underneath us */
786                 return 1;
787         }
788 #endif
789         assert( conn->c_struct_state == SLAP_C_USED );
790
791         return 0;
792 }
793
794 static int
795 connection_resched( Connection *conn )
796 {
797         Operation *op;
798
799         if( conn->c_conn_state != SLAP_C_ACTIVE ) {
800                 /* other states need different handling */
801                 return 0;
802         }
803
804         for( op = slap_op_pop( &conn->c_pending_ops );
805                 op != NULL;
806                 op = slap_op_pop( &conn->c_pending_ops ) )
807         {
808                 /* pending operations should not be marked for abandonment */
809                 assert(!op->o_abandon);
810
811                 connection_op_activate( conn, op );
812
813                 if ( conn->c_conn_state == SLAP_C_BINDING ) {
814                         break;
815                 }
816         }
817         return 0;
818 }
819
820 static int connection_op_activate( Connection *conn, Operation *op )
821 {
822         struct co_arg *arg;
823         char *tmpdn;
824         int status;
825         unsigned long tag = op->o_tag;
826
827         if ( conn->c_dn != NULL ) {
828                 tmpdn = ch_strdup( conn->c_dn );
829         } else {
830                 tmpdn = NULL;
831         }
832
833         arg = (struct co_arg *) ch_malloc( sizeof(struct co_arg) );
834         arg->co_conn = conn;
835         arg->co_op = op;
836
837         arg->co_op->o_dn = ch_strdup( tmpdn != NULL ? tmpdn : "" );
838         arg->co_op->o_ndn = dn_normalize_case( ch_strdup( arg->co_op->o_dn ) );
839
840         slap_op_add( &conn->c_ops, arg->co_op );
841
842         if(tag == LDAP_REQ_BIND) {
843                 conn->c_conn_state = SLAP_C_BINDING;
844         }
845
846         if ( tmpdn != NULL ) {
847                 free( tmpdn );
848         }
849
850         ldap_pvt_thread_mutex_lock( &active_threads_mutex );
851         active_threads++;
852         ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
853
854         status = ldap_pvt_thread_create( &arg->co_op->o_tid, 1,
855                                          connection_operation, (void *) arg );
856
857         if ( status != 0 ) {
858                 Debug( LDAP_DEBUG_ANY,
859                 "ldap_pvt_thread_create failed (%d)\n", status, 0, 0 );
860
861                 /* should move op to pending list */
862         }
863
864         return status;
865 }
866
867 int connection_write(int s)
868 {
869         Connection *c;
870         assert( connections != NULL );
871
872         ldap_pvt_thread_mutex_lock( &connections_mutex );
873
874         c = connection_get( s );
875         if( c == NULL ) {
876                 Debug( LDAP_DEBUG_ANY,
877                         "connection_write(%d): no connection!\n",
878                         s, 0, 0 );
879                 ldap_pvt_thread_mutex_unlock( &connections_mutex );
880                 return -1;
881         }
882
883         Debug( LDAP_DEBUG_TRACE,
884                 "connection_write(%d): waking output for id=%ld\n",
885                 s, c->c_connid, 0 );
886
887         ldap_pvt_thread_cond_signal( &c->c_write_cv );
888
889         connection_return( c );
890         ldap_pvt_thread_mutex_unlock( &connections_mutex );
891         return 0;
892 }