]> git.sur5r.net Git - openldap/blob - servers/slapd/connection.c
bd3f1848ace0e4b2f37c514ec8ba78f3eff3f382
[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 #if 0
651         case LDAP_REQ_EXTENDED:
652                 do_extended( conn, arg->co_op );
653                 break;
654 #endif
655
656         default:
657                 Debug( LDAP_DEBUG_ANY, "unknown request 0x%lx\n",
658                     arg->co_op->o_tag, 0, 0 );
659                 break;
660         }
661
662         ldap_pvt_thread_mutex_lock( &num_ops_mutex );
663         num_ops_completed++;
664         ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
665
666         ldap_pvt_thread_mutex_lock( &conn->c_mutex );
667
668         conn->c_n_ops_executing--;
669         conn->c_n_ops_completed++;
670
671         slap_op_remove( &conn->c_ops, arg->co_op );
672         slap_op_free( arg->co_op );
673         arg->co_op = NULL;
674         arg->co_conn = NULL;
675         free( (char *) arg );
676         arg = NULL;
677
678         switch( tag ) {
679         case LDAP_REQ_UNBIND:
680                 /* c_mutex is locked */
681                 connection_closing( conn );
682                 break;
683
684         case LDAP_REQ_BIND:
685                 if( conn->c_conn_state == SLAP_C_BINDING) {
686                         conn->c_conn_state = SLAP_C_ACTIVE;
687                 }
688         }
689
690         ldap_pvt_thread_mutex_lock( &active_threads_mutex );
691         active_threads--;
692         if( active_threads < 1 ) {
693                 ldap_pvt_thread_cond_signal(&active_threads_cond);
694         }
695         ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
696
697         connection_resched( conn );
698
699         ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
700
701         return NULL;
702 }
703
704 int connection_read(ber_socket_t s)
705 {
706         int rc = 0;
707         Connection *c;
708         assert( connections != NULL );
709
710         ldap_pvt_thread_mutex_lock( &connections_mutex );
711
712         /* get (locked) connection */
713         c = connection_get( s );
714
715         if( c == NULL ) {
716                 Debug( LDAP_DEBUG_ANY,
717                         "connection_read(%ld): no connection!\n",
718                         (long) s, 0, 0 );
719
720                 slapd_remove(s, 0);
721
722                 ldap_pvt_thread_mutex_unlock( &connections_mutex );
723                 return -1;
724         }
725
726         c->c_n_read++;
727
728         if( c->c_conn_state == SLAP_C_CLOSING ) {
729                 Debug( LDAP_DEBUG_TRACE,
730                         "connection_read(%d): closing, ignoring input for id=%ld\n",
731                         s, c->c_connid, 0 );
732
733                 connection_return( c );
734                 ldap_pvt_thread_mutex_unlock( &connections_mutex );
735                 return 0;
736         }
737
738         Debug( LDAP_DEBUG_TRACE,
739                 "connection_read(%d): checking for input on id=%ld\n",
740                 s, c->c_connid, 0 );
741
742 #define CONNECTION_INPUT_LOOP 1
743
744 #ifdef DATA_READY_LOOP
745         while(!rc && ber_pvt_sb_data_ready(&c->c_sb))
746 #elif CONNECTION_INPUT_LOOP
747         while(!rc)
748 #endif
749         {
750                 rc = connection_input( c );
751         }
752
753         if( rc < 0 ) {
754                 Debug( LDAP_DEBUG_TRACE,
755                         "connection_read(%d): input error=%d id=%ld, closing.\n",
756                         s, rc, c->c_connid );
757
758                 /* connections_mutex and c_mutex are locked */
759                 connection_closing( c );
760                 connection_close( c );
761         }
762
763         connection_return( c );
764         ldap_pvt_thread_mutex_unlock( &connections_mutex );
765         return 0;
766 }
767
768 static int
769 connection_input(
770     Connection *conn
771 )
772 {
773         Operation *op;
774         ber_tag_t       tag;
775         ber_len_t       len;
776         ber_int_t       msgid;
777         BerElement      *ber;
778
779         if ( conn->c_currentber == NULL && (conn->c_currentber = ber_alloc())
780             == NULL ) {
781                 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
782                 return -1;
783         }
784
785         errno = 0;
786         if ( (tag = ber_get_next( conn->c_sb, &len, conn->c_currentber ))
787             != LDAP_TAG_MESSAGE )
788         {
789                 int err = errno;
790
791                 Debug( LDAP_DEBUG_TRACE,
792                         "ber_get_next on fd %d failed errno %d (%s)\n",
793                         ber_pvt_sb_get_desc( conn->c_sb ), err,
794                         err > -1 && err < sys_nerr ?  sys_errlist[err] : "unknown" );
795                 Debug( LDAP_DEBUG_TRACE,
796                         "\t*** got %ld of %lu so far\n",
797                         (long)(conn->c_currentber->ber_rwptr - conn->c_currentber->ber_buf),
798                         conn->c_currentber->ber_len, 0 );
799
800                 if ( err != EWOULDBLOCK && err != EAGAIN ) {
801                         /* log, close and send error */
802                         ber_free( conn->c_currentber, 1 );
803                         conn->c_currentber = NULL;
804
805                         return -2;
806                 }
807                 return 1;
808         }
809
810         ber = conn->c_currentber;
811         conn->c_currentber = NULL;
812
813         if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
814                 /* log, close and send error */
815                 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n", tag, 0,
816                     0 );
817                 ber_free( ber, 1 );
818                 return -1;
819         }
820
821         if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
822                 /* log, close and send error */
823                 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n", tag, 0,
824                     0 );
825                 ber_free( ber, 1 );
826
827                 return -1;
828         }
829
830         op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++ );
831
832         if ( conn->c_conn_state == SLAP_C_BINDING
833                 || conn->c_conn_state == SLAP_C_CLOSING )
834         {
835                 Debug( LDAP_DEBUG_ANY, "deferring operation\n", 0, 0, 0 );
836                 conn->c_n_ops_pending++;
837                 slap_op_add( &conn->c_pending_ops, op );
838
839         } else {
840                 conn->c_n_ops_executing++;
841                 connection_op_activate( conn, op );
842         }
843
844 #ifdef NO_THREADS
845         if ( conn->c_struct_state != SLAP_C_USED ) {
846                 /* connection must have got closed underneath us */
847                 return 1;
848         }
849 #endif
850         assert( conn->c_struct_state == SLAP_C_USED );
851
852         return 0;
853 }
854
855 static int
856 connection_resched( Connection *conn )
857 {
858         Operation *op;
859
860         if( conn->c_conn_state == SLAP_C_CLOSING ) {
861                 Debug( LDAP_DEBUG_TRACE,
862                         "connection_resched: attempting closing conn=%ld sd=%d.\n",
863                         conn->c_connid, ber_pvt_sb_get_desc( conn->c_sb ), 0 );
864
865                 connection_close( conn );
866                 return 0;
867         }
868
869         if( conn->c_conn_state != SLAP_C_ACTIVE ) {
870                 /* other states need different handling */
871                 return 0;
872         }
873
874         for( op = slap_op_pop( &conn->c_pending_ops );
875                 op != NULL;
876                 op = slap_op_pop( &conn->c_pending_ops ) )
877         {
878                 /* pending operations should not be marked for abandonment */
879                 assert(!op->o_abandon);
880
881                 conn->c_n_ops_pending--;
882                 conn->c_n_ops_executing++;
883
884                 connection_op_activate( conn, op );
885
886                 if ( conn->c_conn_state == SLAP_C_BINDING ) {
887                         break;
888                 }
889         }
890         return 0;
891 }
892
893 static int connection_op_activate( Connection *conn, Operation *op )
894 {
895         struct co_arg *arg;
896         char *tmpdn;
897         int status;
898         ber_tag_t tag = op->o_tag;
899
900         if ( conn->c_dn != NULL ) {
901                 tmpdn = ch_strdup( conn->c_dn );
902         } else {
903                 tmpdn = NULL;
904         }
905
906         arg = (struct co_arg *) ch_malloc( sizeof(struct co_arg) );
907         arg->co_conn = conn;
908         arg->co_op = op;
909
910         arg->co_op->o_dn = ch_strdup( tmpdn != NULL ? tmpdn : "" );
911         arg->co_op->o_ndn = dn_normalize_case( ch_strdup( arg->co_op->o_dn ) );
912
913         arg->co_op->o_protocol = conn->c_protocol;
914
915         slap_op_add( &conn->c_ops, arg->co_op );
916
917         if(tag == LDAP_REQ_BIND) {
918                 conn->c_conn_state = SLAP_C_BINDING;
919         }
920
921         if ( tmpdn != NULL ) {
922                 free( tmpdn );
923         }
924
925         ldap_pvt_thread_mutex_lock( &active_threads_mutex );
926         active_threads++;
927         ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
928
929         status = ldap_pvt_thread_create( &arg->co_op->o_tid, 1,
930                                          connection_operation, (void *) arg );
931
932         if ( status != 0 ) {
933                 Debug( LDAP_DEBUG_ANY,
934                 "ldap_pvt_thread_create failed (%d)\n", status, 0, 0 );
935
936                 /* should move op to pending list */
937         }
938
939         return status;
940 }
941
942 int connection_write(ber_socket_t s)
943 {
944         Connection *c;
945         assert( connections != NULL );
946
947         ldap_pvt_thread_mutex_lock( &connections_mutex );
948
949         c = connection_get( s );
950
951         slapd_clr_write( s, 0);
952
953         if( c == NULL ) {
954                 Debug( LDAP_DEBUG_ANY,
955                         "connection_write(%ld): no connection!\n",
956                         (long) s, 0, 0 );
957                 slapd_remove(s, 0);
958                 ldap_pvt_thread_mutex_unlock( &connections_mutex );
959                 return -1;
960         }
961
962         c->c_n_write++;
963
964         Debug( LDAP_DEBUG_TRACE,
965                 "connection_write(%d): waking output for id=%ld\n",
966                 s, c->c_connid, 0 );
967
968         ldap_pvt_thread_cond_signal( &c->c_write_cv );
969
970         connection_return( c );
971         ldap_pvt_thread_mutex_unlock( &connections_mutex );
972         return 0;
973 }