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