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