]> git.sur5r.net Git - openldap/blob - servers/slapd/connection.c
Should clear the write flag upon closing.
[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                 /* update state to closing */
335                 c->c_conn_state = SLAP_C_CLOSING;
336
337                 /* don't listen on this port anymore */
338                 slapd_clr_read( c->c_sb.sb_sd, 1 );
339
340                 /* shutdown I/O -- not yet implemented */
341
342                 /* abandon active operations */
343                 for( o = c->c_ops; o != NULL; o = o->o_next ) {
344                         ldap_pvt_thread_mutex_lock( &o->o_abandonmutex );
345                         o->o_abandon = 1;
346                         ldap_pvt_thread_mutex_unlock( &o->o_abandonmutex );
347                 }
348
349                 /* remove pending operations */
350                 for( o = slap_op_pop( &c->c_pending_ops );
351                         o != NULL;
352                         o = slap_op_pop( &c->c_pending_ops ) )
353                 {
354                         slap_op_free( o );
355                 }
356
357                 /* wake write blocked operations */
358                 slapd_clr_write( c->c_sb.sb_sd, 1 );
359                 ldap_pvt_thread_cond_signal( &c->c_write_cv );
360         }
361 }
362
363 static void connection_close( Connection *c )
364 {
365         assert( connections != NULL );
366         assert( c != NULL );
367         assert( c->c_struct_state == SLAP_C_USED );
368         assert( c->c_conn_state == SLAP_C_CLOSING );
369
370         if( c->c_ops != NULL ) {
371                 Debug( LDAP_DEBUG_TRACE,
372                         "connection_close: deferring conn=%ld sd=%d.\n",
373                         c->c_connid, c->c_sb.sb_sd, 0 );
374
375                 return;
376         }
377
378         Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%ld sd=%d.\n",
379                 c->c_connid, c->c_sb.sb_sd, 0 );
380
381         connection_destroy( c );
382 }
383
384 long connections_nextid(void)
385 {
386         long id;
387         assert( connections != NULL );
388
389         ldap_pvt_thread_mutex_lock( &connections_mutex );
390
391         id = conn_nextid;
392
393         ldap_pvt_thread_mutex_unlock( &connections_mutex );
394
395         return id;
396 }
397
398 Connection* connection_first(void)
399 {
400         assert( connections != NULL );
401
402         ldap_pvt_thread_mutex_lock( &connections_mutex );
403
404         assert( conn_index == -1 );
405         conn_index = 0;
406
407         return connection_next(NULL);
408 }
409
410 Connection* connection_next(Connection *c)
411 {
412         assert( connections != NULL );
413         assert( conn_index != -1 );
414         assert( conn_index <= dtblsize );
415
416         if( c != NULL ) {
417                 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
418         }
419
420         c = NULL;
421
422         for(; conn_index < dtblsize; conn_index++) {
423                 if( connections[conn_index].c_struct_state == SLAP_C_UNINITIALIZED ) {
424                         assert( connections[conn_index].c_conn_state == SLAP_C_INVALID );
425 #ifndef HAVE_WINSOCK
426                         continue;
427 #else
428                         break;
429 #endif
430                 }
431
432                 if( connections[conn_index].c_struct_state == SLAP_C_USED ) {
433                         assert( connections[conn_index].c_conn_state != SLAP_C_INVALID );
434                         c = &connections[conn_index++];
435                         break;
436                 }
437
438                 assert( connections[conn_index].c_struct_state == SLAP_C_UNUSED );
439                 assert( connections[conn_index].c_conn_state == SLAP_C_INVALID );
440         }
441
442         if( c != NULL ) {
443                 ldap_pvt_thread_mutex_lock( &c->c_mutex );
444         }
445
446         return c;
447 }
448
449 void connection_done(Connection *c)
450 {
451         assert( connections != NULL );
452         assert( conn_index != -1 );
453         assert( conn_index <= dtblsize );
454
455         if( c != NULL ) {
456                 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
457         }
458
459         conn_index = -1;
460         ldap_pvt_thread_mutex_unlock( &connections_mutex );
461 }
462
463 /*
464  * connection_activity - handle the request operation op on connection
465  * conn.  This routine figures out what kind of operation it is and
466  * calls the appropriate stub to handle it.
467  */
468
469 static void *
470 connection_operation( void *arg_v )
471 {
472         struct co_arg   *arg = arg_v;
473         int tag = arg->co_op->o_tag;
474         Connection *conn = arg->co_conn;
475
476 #ifdef LDAP_COUNTERS
477         ldap_pvt_thread_mutex_lock( &num_ops_mutex );
478         num_ops_initiated++;
479         ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
480 #endif
481
482         switch ( tag ) {
483         case LDAP_REQ_BIND:
484                 do_bind( conn, arg->co_op );
485                 break;
486
487 #ifdef LDAP_COMPAT30
488         case LDAP_REQ_UNBIND_30:
489 #endif
490         case LDAP_REQ_UNBIND:
491                 do_unbind( conn, arg->co_op );
492                 break;
493
494         case LDAP_REQ_ADD:
495                 do_add( conn, arg->co_op );
496                 break;
497
498 #ifdef LDAP_COMPAT30
499         case LDAP_REQ_DELETE_30:
500 #endif
501         case LDAP_REQ_DELETE:
502                 do_delete( conn, arg->co_op );
503                 break;
504
505         case LDAP_REQ_MODRDN:
506                 do_modrdn( conn, arg->co_op );
507                 break;
508
509         case LDAP_REQ_MODIFY:
510                 do_modify( conn, arg->co_op );
511                 break;
512
513         case LDAP_REQ_COMPARE:
514                 do_compare( conn, arg->co_op );
515                 break;
516
517         case LDAP_REQ_SEARCH:
518                 do_search( conn, arg->co_op );
519                 break;
520
521 #ifdef LDAP_COMPAT30
522         case LDAP_REQ_ABANDON_30:
523 #endif
524         case LDAP_REQ_ABANDON:
525                 do_abandon( conn, arg->co_op );
526                 break;
527
528         default:
529                 Debug( LDAP_DEBUG_ANY, "unknown request 0x%lx\n",
530                     arg->co_op->o_tag, 0, 0 );
531                 break;
532         }
533
534 #ifdef LDAP_COUNTERS
535         ldap_pvt_thread_mutex_lock( &num_ops_mutex );
536         num_ops_completed++;
537         ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
538 #endif
539
540         ldap_pvt_thread_mutex_lock( &conn->c_mutex );
541
542 #ifdef LDAP_COUNTERS
543         conn->c_n_ops_completed++;
544 #endif
545
546         slap_op_remove( &conn->c_ops, arg->co_op );
547         slap_op_free( arg->co_op );
548         arg->co_op = NULL;
549         arg->co_conn = NULL;
550         free( (char *) arg );
551         arg = NULL;
552
553         switch( tag ) {
554 #ifdef LDAP_COMPAT30
555         case LDAP_REQ_UNBIND_30:
556 #endif
557         case LDAP_REQ_UNBIND:
558                 connection_closing( conn );
559                 break;
560
561         case LDAP_REQ_BIND:
562                 if( conn->c_conn_state == SLAP_C_BINDING) {
563                         conn->c_conn_state = SLAP_C_ACTIVE;
564                 }
565         }
566
567         if( conn->c_conn_state == SLAP_C_CLOSING ) {
568                 Debug( LDAP_DEBUG_TRACE,
569                         "connection_operation: attempting closing conn=%ld sd=%d.\n",
570                         conn->c_connid, conn->c_sb.sb_sd, 0 );
571
572                 connection_close( conn );
573         }
574
575         ldap_pvt_thread_mutex_lock( &active_threads_mutex );
576         active_threads--;
577         if( active_threads < 1 ) {
578                 ldap_pvt_thread_cond_signal(&active_threads_cond);
579         }
580         ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
581
582         connection_resched( conn );
583
584         ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
585
586         return NULL;
587 }
588
589 int connection_read(int s)
590 {
591         int rc = 0;
592         Connection *c;
593         assert( connections != NULL );
594
595         ldap_pvt_thread_mutex_lock( &connections_mutex );
596
597         c = connection_get( s );
598         if( c == NULL ) {
599                 Debug( LDAP_DEBUG_ANY,
600                         "connection_read(%d): no connection!\n",
601                         s, 0, 0 );
602                 ldap_pvt_thread_mutex_unlock( &connections_mutex );
603                 return -1;
604         }
605
606         if( c->c_conn_state == SLAP_C_CLOSING ) {
607                 Debug( LDAP_DEBUG_TRACE,
608                         "connection_read(%d): closing, ignoring input for id=%ld\n",
609                         s, c->c_connid, 0 );
610
611                 connection_return( c );
612                 ldap_pvt_thread_mutex_unlock( &connections_mutex );
613                 return 0;
614         }
615
616         Debug( LDAP_DEBUG_TRACE,
617                 "connection_read(%d): checking for input on id=%ld\n",
618                 s, c->c_connid, 0 );
619
620 #define CONNECTION_INPUT_LOOP 1
621
622 #ifdef DATA_READY_LOOP
623         while(!rc && lber_pvt_sb_data_ready(&c->c_sb))
624 #elif CONNECTION_INPUT_LOOP
625         while(!rc)
626 #endif
627         {
628                 rc = connection_input( c );
629         }
630
631         if( rc < 0 ) {
632                 Debug( LDAP_DEBUG_TRACE,
633                         "connection_read(%d): input error=%d id=%ld, closing.\n",
634                         s, rc, c->c_connid );
635
636                 connection_closing( c );
637                 connection_close( c );
638         }
639
640         connection_return( c );
641         ldap_pvt_thread_mutex_unlock( &connections_mutex );
642         return 0;
643 }
644
645 static int
646 connection_input(
647     Connection *conn
648 )
649 {
650         Operation *op;
651         unsigned long   tag, len;
652         long            msgid;
653         BerElement      *ber;
654
655         if ( conn->c_currentber == NULL && (conn->c_currentber = ber_alloc())
656             == NULL ) {
657                 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
658                 return -1;
659         }
660
661         errno = 0;
662         if ( (tag = ber_get_next( &conn->c_sb, &len, conn->c_currentber ))
663             != LDAP_TAG_MESSAGE )
664         {
665                 int err = errno;
666
667                 Debug( LDAP_DEBUG_TRACE,
668                         "ber_get_next on fd %d failed errno %d (%s)\n",
669                         lber_pvt_sb_get_desc(&conn->c_sb), err,
670                         err > -1 && err < sys_nerr ?  sys_errlist[err] : "unknown" );
671                 Debug( LDAP_DEBUG_TRACE,
672                         "\t*** got %ld of %lu so far\n",
673                         (long)(conn->c_currentber->ber_rwptr - conn->c_currentber->ber_buf),
674                         conn->c_currentber->ber_len, 0 );
675
676                 if ( err != EWOULDBLOCK && err != EAGAIN ) {
677                         /* log, close and send error */
678                         ber_free( conn->c_currentber, 1 );
679                         conn->c_currentber = NULL;
680
681                         return -2;
682                 }
683                 return 1;
684         }
685
686         ber = conn->c_currentber;
687         conn->c_currentber = NULL;
688
689         if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
690                 /* log, close and send error */
691                 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n", tag, 0,
692                     0 );
693                 ber_free( ber, 1 );
694                 return -1;
695         }
696
697         if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
698                 /* log, close and send error */
699                 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n", tag, 0,
700                     0 );
701                 ber_free( ber, 1 );
702
703                 return -1;
704         }
705
706 #ifdef LDAP_COMPAT30
707         if ( conn->c_version == 30 ) {
708                 (void) ber_skip_tag( ber, &len );
709         }
710 #endif
711
712         op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++ );
713
714         if ( conn->c_conn_state == SLAP_C_BINDING
715                 || conn->c_conn_state == SLAP_C_CLOSING )
716         {
717                 Debug( LDAP_DEBUG_ANY, "deferring operation\n", 0, 0, 0 );
718                 slap_op_add( &conn->c_pending_ops, op );
719
720         } else {
721                 connection_op_activate( conn, op );
722         }
723
724 #ifdef NO_THREADS
725         if ( conn->c_struct_state != SLAP_C_USED ) {
726                 /* connection must have got closed underneath us */
727                 return 1;
728         }
729 #endif
730         assert( conn->c_struct_state == SLAP_C_USED );
731
732         return 0;
733 }
734
735 static int
736 connection_resched( Connection *conn )
737 {
738         Operation *op;
739
740         if( conn->c_conn_state != SLAP_C_ACTIVE ) {
741                 /* other states need different handling */
742                 return 0;
743         }
744
745         for( op = slap_op_pop( &conn->c_pending_ops );
746                 op != NULL;
747                 op = slap_op_pop( &conn->c_pending_ops ) )
748         {
749                 /* pending operations should not be marked for abandonment */
750                 assert(!op->o_abandon);
751
752                 connection_op_activate( conn, op );
753
754                 if ( conn->c_conn_state == SLAP_C_BINDING ) {
755                         break;
756                 }
757         }
758         return 0;
759 }
760
761 static int connection_op_activate( Connection *conn, Operation *op )
762 {
763         struct co_arg *arg;
764         char *tmpdn;
765         int status;
766         unsigned long tag = op->o_tag;
767
768         if ( conn->c_dn != NULL ) {
769                 tmpdn = ch_strdup( conn->c_dn );
770         } else {
771                 tmpdn = NULL;
772         }
773
774         arg = (struct co_arg *) ch_malloc( sizeof(struct co_arg) );
775         arg->co_conn = conn;
776         arg->co_op = op;
777
778         arg->co_op->o_dn = ch_strdup( tmpdn != NULL ? tmpdn : "" );
779         arg->co_op->o_ndn = dn_normalize_case( ch_strdup( arg->co_op->o_dn ) );
780
781         slap_op_add( &conn->c_ops, arg->co_op );
782
783         if(tag == LDAP_REQ_BIND) {
784                 conn->c_conn_state = SLAP_C_BINDING;
785         }
786
787         if ( tmpdn != NULL ) {
788                 free( tmpdn );
789         }
790
791         ldap_pvt_thread_mutex_lock( &active_threads_mutex );
792         active_threads++;
793         ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
794
795         status = ldap_pvt_thread_create( &arg->co_op->o_tid, 1,
796                                          connection_operation, (void *) arg );
797
798         if ( status != 0 ) {
799                 Debug( LDAP_DEBUG_ANY,
800                 "ldap_pvt_thread_create failed (%d)\n", status, 0, 0 );
801
802                 /* should move op to pending list */
803         }
804
805         return status;
806 }
807
808 int connection_write(int s)
809 {
810         Connection *c;
811         assert( connections != NULL );
812
813         ldap_pvt_thread_mutex_lock( &connections_mutex );
814
815         c = connection_get( s );
816         if( c == NULL ) {
817                 Debug( LDAP_DEBUG_ANY,
818                         "connection_write(%d): no connection!\n",
819                         s, 0, 0 );
820                 ldap_pvt_thread_mutex_unlock( &connections_mutex );
821                 return -1;
822         }
823
824         Debug( LDAP_DEBUG_TRACE,
825                 "connection_write(%d): waking output for id=%ld\n",
826                 s, c->c_connid, 0 );
827
828         ldap_pvt_thread_cond_signal( &c->c_write_cv );
829
830         connection_return( c );
831         ldap_pvt_thread_mutex_unlock( &connections_mutex );
832         return 0;
833 }