]> git.sur5r.net Git - openldap/blob - servers/slapd/connection.c
8204126b91076ebac6beab340ea457b1c8ea8ffa
[openldap] / servers / slapd / connection.c
1 #include "portable.h"
2
3 #include <stdio.h>
4
5 #include <ac/socket.h>
6 #include <ac/errno.h>
7 #include <ac/signal.h>
8 #include <ac/string.h>
9 #include <ac/time.h>
10
11 #include "slap.h"
12
13 /* we need LBER internals */
14 #include "../../libraries/liblber/lber-int.h"
15
16 /* protected by connections_mutex */
17 static ldap_pvt_thread_mutex_t connections_mutex;
18 static Connection *connections = NULL;
19 static long conn_nextid = 0;
20
21 /* structure state (protected by connections_mutex) */
22 #define SLAP_C_UNINITIALIZED    0x00    /* MUST BE ZERO (0) */
23 #define SLAP_C_UNUSED                   0x01
24 #define SLAP_C_USED                             0x02
25
26 /* connection state (protected by c_mutex ) */
27 #define SLAP_C_INVALID                  0x00    /* MUST BE ZERO (0) */
28 #define SLAP_C_INACTIVE                 0x01    /* zero threads */
29 #define SLAP_C_ACTIVE                   0x02    /* one or more threads */
30 #define SLAP_C_BINDING                  0x03    /* binding */
31 #define SLAP_C_CLOSING                  0x04    /* closing */
32
33 static Connection* connection_get( int s );
34
35 static int connection_input( Connection *c );
36 static void connection_close( Connection *c );
37
38 static int connection_op_activate( Connection *conn, Operation *op );
39 static int connection_resched( Connection *conn );
40
41 struct co_arg {
42         Connection      *co_conn;
43         Operation       *co_op;
44 };
45
46 /*
47  * Initialize connection management infrastructure.
48  */
49 int connections_init(void)
50 {
51         assert( connections == NULL );
52
53         if( connections != NULL) {
54                 Debug( LDAP_DEBUG_ANY, "connections_init: already initialized.\n",
55                         0, 0, 0 );
56                 return -1;
57         }
58
59         /* should check return of every call */
60         ldap_pvt_thread_mutex_init( &connections_mutex );
61
62         connections = (Connection *) calloc( dtblsize, sizeof(Connection) );
63
64         if( connections == NULL ) {
65                 Debug( LDAP_DEBUG_ANY,
66                         "connections_init: allocation (%d*%ld) of connection array failed.\n",
67                         dtblsize, (long) sizeof(Connection), 0 );
68                 return -1;
69         }
70
71     assert( connections[0].c_struct_state == SLAP_C_UNINITIALIZED );
72     assert( connections[dtblsize-1].c_struct_state == SLAP_C_UNINITIALIZED );
73
74         /*
75          * per entry initialization of the Connection array initialization
76          * will be done by connection_init()
77          */ 
78
79         return 0;
80 }
81
82 /*
83  * Destroy connection management infrastructure.
84  */
85 int connections_destroy(void)
86 {
87         int i;
88
89         /* should check return of every call */
90
91         if( connections == NULL) {
92                 Debug( LDAP_DEBUG_ANY, "connections_destroy: nothing to destroy.\n",
93                         0, 0, 0 );
94                 return -1;
95         }
96
97         for ( i = 0; i < dtblsize; i++ ) {
98                 if( connections[i].c_struct_state != SLAP_C_UNINITIALIZED ) {
99                         ldap_pvt_thread_mutex_destroy( &connections[i].c_mutex );
100                         ldap_pvt_thread_mutex_destroy( &connections[i].c_write_mutex );
101                         ldap_pvt_thread_cond_destroy( &connections[i].c_write_cv );
102                 }
103         }
104
105         free( connections );
106         connections = NULL;
107
108         ldap_pvt_thread_mutex_destroy( &connections_mutex );
109         return 0;
110 }
111
112 /*
113  * shutdown all connections
114  */
115 int connections_shutdown(void)
116 {
117         int i;
118
119         ldap_pvt_thread_mutex_lock( &connections_mutex );
120
121         for ( i = 0; i < dtblsize; i++ ) {
122                 if( connections[i].c_struct_state != SLAP_C_USED ) {
123                         continue;
124                 }
125
126                 ldap_pvt_thread_mutex_lock( &connections[i].c_mutex );
127
128                 /* connections_mutex and c_mutex are locked */
129                 connection_closing( &connections[i] );
130                 connection_close( &connections[i] );
131
132                 ldap_pvt_thread_mutex_unlock( &connections[i].c_mutex );
133         }
134
135         ldap_pvt_thread_mutex_unlock( &connections_mutex );
136
137         return 0;
138 }
139
140 static Connection* connection_get( int s )
141 {
142         /* connections_mutex should be locked by caller */
143
144         Connection *c;
145
146         Debug( LDAP_DEBUG_ARGS,
147                 "connection_get(%d)\n",
148                 s, 0, 0 );
149
150         assert( connections != NULL );
151
152         if(s < 0) {
153                 return NULL;
154         }
155
156 #ifndef HAVE_WINSOCK
157         c = &connections[s];
158
159         assert( c->c_struct_state == SLAP_C_USED );
160
161         ldap_pvt_thread_mutex_lock( &c->c_mutex );
162
163         assert( c->c_struct_state == SLAP_C_USED );
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, 0 );
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
682                 slapd_remove(s, 0);
683
684                 ldap_pvt_thread_mutex_unlock( &connections_mutex );
685                 return -1;
686         }
687
688         if( c->c_conn_state == SLAP_C_CLOSING ) {
689                 Debug( LDAP_DEBUG_TRACE,
690                         "connection_read(%d): closing, ignoring input for id=%ld\n",
691                         s, c->c_connid, 0 );
692
693                 connection_return( c );
694                 ldap_pvt_thread_mutex_unlock( &connections_mutex );
695                 return 0;
696         }
697
698         Debug( LDAP_DEBUG_TRACE,
699                 "connection_read(%d): checking for input on id=%ld\n",
700                 s, c->c_connid, 0 );
701
702 #define CONNECTION_INPUT_LOOP 1
703
704 #ifdef DATA_READY_LOOP
705         while(!rc && ber_pvt_sb_data_ready(&c->c_sb))
706 #elif CONNECTION_INPUT_LOOP
707         while(!rc)
708 #endif
709         {
710                 rc = connection_input( c );
711         }
712
713         if( rc < 0 ) {
714                 Debug( LDAP_DEBUG_TRACE,
715                         "connection_read(%d): input error=%d id=%ld, closing.\n",
716                         s, rc, c->c_connid );
717
718                 /* connections_mutex and c_mutex are locked */
719                 connection_closing( c );
720                 connection_close( c );
721         }
722
723         connection_return( c );
724         ldap_pvt_thread_mutex_unlock( &connections_mutex );
725         return 0;
726 }
727
728 static int
729 connection_input(
730     Connection *conn
731 )
732 {
733         Operation *op;
734         unsigned long   tag, len;
735         long            msgid;
736         BerElement      *ber;
737
738         if ( conn->c_currentber == NULL && (conn->c_currentber = ber_alloc())
739             == NULL ) {
740                 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
741                 return -1;
742         }
743
744         errno = 0;
745         if ( (tag = ber_get_next( conn->c_sb, &len, conn->c_currentber ))
746             != LDAP_TAG_MESSAGE )
747         {
748                 int err = errno;
749
750                 Debug( LDAP_DEBUG_TRACE,
751                         "ber_get_next on fd %d failed errno %d (%s)\n",
752                         ber_pvt_sb_get_desc( conn->c_sb ), err,
753                         err > -1 && err < sys_nerr ?  sys_errlist[err] : "unknown" );
754                 Debug( LDAP_DEBUG_TRACE,
755                         "\t*** got %ld of %lu so far\n",
756                         (long)(conn->c_currentber->ber_rwptr - conn->c_currentber->ber_buf),
757                         conn->c_currentber->ber_len, 0 );
758
759                 if ( err != EWOULDBLOCK && err != EAGAIN ) {
760                         /* log, close and send error */
761                         ber_free( conn->c_currentber, 1 );
762                         conn->c_currentber = NULL;
763
764                         return -2;
765                 }
766                 return 1;
767         }
768
769         ber = conn->c_currentber;
770         conn->c_currentber = NULL;
771
772         if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
773                 /* log, close and send error */
774                 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n", tag, 0,
775                     0 );
776                 ber_free( ber, 1 );
777                 return -1;
778         }
779
780         if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
781                 /* log, close and send error */
782                 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n", tag, 0,
783                     0 );
784                 ber_free( ber, 1 );
785
786                 return -1;
787         }
788
789 #ifdef LDAP_COMPAT30
790         if ( conn->c_version == 30 ) {
791                 (void) ber_skip_tag( ber, &len );
792         }
793 #endif
794
795         op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++ );
796
797         if ( conn->c_conn_state == SLAP_C_BINDING
798                 || conn->c_conn_state == SLAP_C_CLOSING )
799         {
800                 Debug( LDAP_DEBUG_ANY, "deferring operation\n", 0, 0, 0 );
801                 slap_op_add( &conn->c_pending_ops, op );
802
803         } else {
804                 connection_op_activate( conn, op );
805         }
806
807 #ifdef NO_THREADS
808         if ( conn->c_struct_state != SLAP_C_USED ) {
809                 /* connection must have got closed underneath us */
810                 return 1;
811         }
812 #endif
813         assert( conn->c_struct_state == SLAP_C_USED );
814
815         return 0;
816 }
817
818 static int
819 connection_resched( Connection *conn )
820 {
821         Operation *op;
822
823         if( conn->c_conn_state == SLAP_C_CLOSING ) {
824                 Debug( LDAP_DEBUG_TRACE,
825                         "connection_resched: attempting closing conn=%ld sd=%d.\n",
826                         conn->c_connid, ber_pvt_sb_get_desc( conn->c_sb ), 0 );
827
828                 connection_close( conn );
829                 return 0;
830         }
831
832         if( conn->c_conn_state != SLAP_C_ACTIVE ) {
833                 /* other states need different handling */
834                 return 0;
835         }
836
837         for( op = slap_op_pop( &conn->c_pending_ops );
838                 op != NULL;
839                 op = slap_op_pop( &conn->c_pending_ops ) )
840         {
841                 /* pending operations should not be marked for abandonment */
842                 assert(!op->o_abandon);
843
844                 connection_op_activate( conn, op );
845
846                 if ( conn->c_conn_state == SLAP_C_BINDING ) {
847                         break;
848                 }
849         }
850         return 0;
851 }
852
853 static int connection_op_activate( Connection *conn, Operation *op )
854 {
855         struct co_arg *arg;
856         char *tmpdn;
857         int status;
858         unsigned long tag = op->o_tag;
859
860         if ( conn->c_dn != NULL ) {
861                 tmpdn = ch_strdup( conn->c_dn );
862         } else {
863                 tmpdn = NULL;
864         }
865
866         arg = (struct co_arg *) ch_malloc( sizeof(struct co_arg) );
867         arg->co_conn = conn;
868         arg->co_op = op;
869
870         arg->co_op->o_dn = ch_strdup( tmpdn != NULL ? tmpdn : "" );
871         arg->co_op->o_ndn = dn_normalize_case( ch_strdup( arg->co_op->o_dn ) );
872
873         slap_op_add( &conn->c_ops, arg->co_op );
874
875         if(tag == LDAP_REQ_BIND) {
876                 conn->c_conn_state = SLAP_C_BINDING;
877         }
878
879         if ( tmpdn != NULL ) {
880                 free( tmpdn );
881         }
882
883         ldap_pvt_thread_mutex_lock( &active_threads_mutex );
884         active_threads++;
885         ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
886
887         status = ldap_pvt_thread_create( &arg->co_op->o_tid, 1,
888                                          connection_operation, (void *) arg );
889
890         if ( status != 0 ) {
891                 Debug( LDAP_DEBUG_ANY,
892                 "ldap_pvt_thread_create failed (%d)\n", status, 0, 0 );
893
894                 /* should move op to pending list */
895         }
896
897         return status;
898 }
899
900 int connection_write(int s)
901 {
902         Connection *c;
903         assert( connections != NULL );
904
905         ldap_pvt_thread_mutex_lock( &connections_mutex );
906
907         c = connection_get( s );
908
909         slapd_clr_write( s, 0);
910
911         if( c == NULL ) {
912                 Debug( LDAP_DEBUG_ANY,
913                         "connection_write(%d): no connection!\n",
914                         s, 0, 0 );
915                 slapd_remove(s, 0);
916                 ldap_pvt_thread_mutex_unlock( &connections_mutex );
917                 return -1;
918         }
919
920         Debug( LDAP_DEBUG_TRACE,
921                 "connection_write(%d): waking output for id=%ld\n",
922                 s, c->c_connid, 0 );
923
924         ldap_pvt_thread_cond_signal( &c->c_write_cv );
925
926         connection_return( c );
927         ldap_pvt_thread_mutex_unlock( &connections_mutex );
928         return 0;
929 }