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