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