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