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