]> git.sur5r.net Git - openldap/blob - servers/slapd/connection.c
159f01ef0f96f1300719209493b40b8e4fe60a88
[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 struct co_arg {
14         Connection      *co_conn;
15         Operation       *co_op;
16 };
17
18 /*
19  * connection_activity - handle the request operation op on connection
20  * conn.  This routine figures out what kind of operation it is and
21  * calls the appropriate stub to handle it.
22  */
23
24 static void *
25 connection_operation( void *arg_v )
26 {
27         struct co_arg   *arg = arg_v;
28         unsigned long   len;
29
30         pthread_mutex_lock( &arg->co_conn->c_opsmutex );
31         arg->co_conn->c_opsinitiated++;
32         pthread_mutex_unlock( &arg->co_conn->c_opsmutex );
33
34         pthread_mutex_lock( &ops_mutex );
35         ops_initiated++;
36         pthread_mutex_unlock( &ops_mutex );
37
38         switch ( arg->co_op->o_tag ) {
39         case LDAP_REQ_BIND:
40                 do_bind( arg->co_conn, arg->co_op );
41                 break;
42
43 #ifdef LDAP_COMPAT30
44         case LDAP_REQ_UNBIND_30:
45 #endif
46         case LDAP_REQ_UNBIND:
47                 do_unbind( arg->co_conn, arg->co_op );
48                 break;
49
50         case LDAP_REQ_ADD:
51                 do_add( arg->co_conn, arg->co_op );
52                 break;
53
54 #ifdef LDAP_COMPAT30
55         case LDAP_REQ_DELETE_30:
56 #endif
57         case LDAP_REQ_DELETE:
58                 do_delete( arg->co_conn, arg->co_op );
59                 break;
60
61         case LDAP_REQ_MODRDN:
62                 do_modrdn( arg->co_conn, arg->co_op );
63                 break;
64
65         case LDAP_REQ_MODIFY:
66                 do_modify( arg->co_conn, arg->co_op );
67                 break;
68
69         case LDAP_REQ_COMPARE:
70                 do_compare( arg->co_conn, arg->co_op );
71                 break;
72
73         case LDAP_REQ_SEARCH:
74                 do_search( arg->co_conn, arg->co_op );
75                 break;
76
77 #ifdef LDAP_COMPAT30
78         case LDAP_REQ_ABANDON_30:
79 #endif
80         case LDAP_REQ_ABANDON:
81                 do_abandon( arg->co_conn, arg->co_op );
82                 break;
83
84         default:
85                 Debug( LDAP_DEBUG_ANY, "unknown request 0x%lx\n",
86                     arg->co_op->o_tag, 0, 0 );
87                 break;
88         }
89
90         pthread_mutex_lock( &arg->co_conn->c_opsmutex );
91         arg->co_conn->c_opscompleted++;
92         slap_op_delete( &arg->co_conn->c_ops, arg->co_op );
93         pthread_mutex_unlock( &arg->co_conn->c_opsmutex );
94
95         free( (char *) arg );
96
97         pthread_mutex_lock( &ops_mutex );
98         ops_completed++;
99         pthread_mutex_unlock( &ops_mutex );
100
101         pthread_mutex_lock( &active_threads_mutex );
102         active_threads--;
103         if( active_threads < 1 ) {
104                 pthread_cond_signal(&active_threads_cond);
105         }
106         pthread_mutex_unlock( &active_threads_mutex );
107         return NULL;
108 }
109
110 void
111 connection_activity(
112     Connection *conn
113 )
114 {
115 #ifndef HAVE_PTHREAD_DETACH
116         pthread_attr_t  attr;
117 #endif
118         int status;
119         struct co_arg   *arg;
120         unsigned long   tag, len;
121         long            msgid;
122         BerElement      *ber;
123         char            *tmpdn;
124
125         if ( conn->c_currentber == NULL && (conn->c_currentber = ber_alloc())
126             == NULL ) {
127                 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
128                 return;
129         }
130
131         errno = 0;
132         if ( (tag = ber_get_next( &conn->c_sb, &len, conn->c_currentber ))
133             != LDAP_TAG_MESSAGE ) {
134                 Debug( LDAP_DEBUG_TRACE,
135                     "ber_get_next on fd %d failed errno %d (%s)\n",
136                     conn->c_sb.sb_sd, errno, errno > -1 && errno < sys_nerr ?
137                     sys_errlist[errno] : "unknown" );
138                 Debug( LDAP_DEBUG_TRACE, "*** got %ld of %lu so far\n",
139                     (long)(conn->c_currentber->ber_rwptr - conn->c_currentber->ber_buf),
140                     conn->c_currentber->ber_len, 0 );
141
142                 if ( errno != EWOULDBLOCK && errno != EAGAIN ) {
143                         /* log, close and send error */
144                         ber_free( conn->c_currentber, 1 );
145                         conn->c_currentber = NULL;
146
147                         close_connection( conn, conn->c_connid, -1 );
148                 }
149
150                 return;
151         }
152         ber = conn->c_currentber;
153         conn->c_currentber = NULL;
154
155         if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
156                 /* log, close and send error */
157                 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n", tag, 0,
158                     0 );
159                 ber_free( ber, 1 );
160
161                 close_connection( conn, conn->c_connid, -1 );
162                 return;
163         }
164
165         if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
166                 /* log, close and send error */
167                 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n", tag, 0,
168                     0 );
169                 ber_free( ber, 1 );
170
171                 close_connection( conn, conn->c_connid, -1 );
172                 return;
173         }
174
175 #ifdef LDAP_COMPAT30
176         if ( conn->c_version == 30 ) {
177                 (void) ber_skip_tag( ber, &len );
178         }
179 #endif
180
181         arg = (struct co_arg *) ch_malloc( sizeof(struct co_arg) );
182         arg->co_conn = conn;
183
184         pthread_mutex_lock( &conn->c_dnmutex );
185         if ( conn->c_dn != NULL ) {
186                 tmpdn = ch_strdup( conn->c_dn );
187         } else {
188                 tmpdn = NULL;
189         }
190         pthread_mutex_unlock( &conn->c_dnmutex );
191
192         pthread_mutex_lock( &conn->c_opsmutex );
193         arg->co_op = slap_op_add( &conn->c_ops, ber, msgid, tag, tmpdn,
194             conn->c_opsinitiated, conn->c_connid );
195         pthread_mutex_unlock( &conn->c_opsmutex );
196
197         if ( tmpdn != NULL ) {
198                 free( tmpdn );
199         }
200
201 #ifdef HAVE_PTHREAD_DETACH
202         if ( status = pthread_create( &arg->co_op->o_tid, NULL,
203             connection_operation, (void *) arg ) != 0 ) {
204                 Debug( LDAP_DEBUG_ANY, "pthread_create failed (%d)\n", status, 0, 0 );
205         } else {
206                 pthread_mutex_lock( &active_threads_mutex );
207                 active_threads++;
208                 pthread_mutex_unlock( &active_threads_mutex );
209         }
210
211 #if !defined(HAVE_PTHREADS_D4)
212         pthread_detach( arg->co_op->o_tid );
213 #else
214         pthread_detach( &arg->co_op->o_tid );
215 #endif
216
217 #else /* !pthread detach */
218
219         pthread_attr_init( &attr );
220         pthread_attr_setdetachstate( &attr, PTHREAD_CREATE_DETACHED );
221 #if !defined(HAVE_PTHREADS_D4)
222         /* POSIX_THREADS or compatible
223          * This is a draft 10 or standard pthreads implementation
224          */
225         if ( status = pthread_create( &arg->co_op->o_tid, &attr,
226             connection_operation, (void *) arg ) != 0 ) {
227                 Debug( LDAP_DEBUG_ANY, "pthread_create failed (%d)\n", status, 0, 0 );
228         } else {
229                 pthread_mutex_lock( &active_threads_mutex );
230                 active_threads++;
231                 pthread_mutex_unlock( &active_threads_mutex );
232         }
233 #else   /* pthread draft4  */
234         /*
235          * This is a draft 4 or earlier pthreads implementation
236          */
237         if ( status = pthread_create( &arg->co_op->o_tid, attr,
238             connection_operation, (void *) arg ) != 0 ) {
239                 Debug( LDAP_DEBUG_ANY, "pthread_create failed (%d)\n", status, 0, 0 );
240         } else {
241                 pthread_mutex_lock( &active_threads_mutex );
242                 active_threads++;
243                 pthread_mutex_unlock( &active_threads_mutex );
244         }
245 #endif  /* pthread draft4 */
246         pthread_attr_destroy( &attr );
247 #endif
248 }