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