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