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