]> git.sur5r.net Git - openldap/blob - servers/slapd/connection.c
3ea80d3200ab77c00aba3e4b284c391aa7c6fedb
[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         pthread_mutex_unlock( &active_threads_mutex );
104         return NULL;
105 }
106
107 void
108 connection_activity(
109     Connection *conn
110 )
111 {
112         pthread_attr_t  attr;
113         struct co_arg   *arg;
114         unsigned long   tag, len;
115         long            msgid;
116         BerElement      *ber;
117         char            *tmpdn;
118
119         if ( conn->c_currentber == NULL && (conn->c_currentber = ber_alloc())
120             == NULL ) {
121                 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
122                 return;
123         }
124
125         errno = 0;
126         if ( (tag = ber_get_next( &conn->c_sb, &len, conn->c_currentber ))
127             != LDAP_TAG_MESSAGE ) {
128                 Debug( LDAP_DEBUG_TRACE,
129                     "ber_get_next on fd %d failed errno %d (%s)\n",
130                     conn->c_sb.sb_sd, errno, errno > -1 && errno < sys_nerr ?
131                     sys_errlist[errno] : "unknown" );
132                 Debug( LDAP_DEBUG_TRACE, "*** got %ld of %lu so far\n",
133                     (long)(conn->c_currentber->ber_rwptr - conn->c_currentber->ber_buf),
134                     conn->c_currentber->ber_len, 0 );
135
136                 if ( errno != EWOULDBLOCK && errno != EAGAIN ) {
137                         /* log, close and send error */
138                         ber_free( conn->c_currentber, 1 );
139                         conn->c_currentber = NULL;
140
141                         close_connection( conn, conn->c_connid, -1 );
142                 }
143
144                 return;
145         }
146         ber = conn->c_currentber;
147         conn->c_currentber = NULL;
148
149         if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
150                 /* log, close and send error */
151                 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n", tag, 0,
152                     0 );
153                 ber_free( ber, 1 );
154
155                 close_connection( conn, conn->c_connid, -1 );
156                 return;
157         }
158
159         if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
160                 /* log, close and send error */
161                 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n", tag, 0,
162                     0 );
163                 ber_free( ber, 1 );
164
165                 close_connection( conn, conn->c_connid, -1 );
166                 return;
167         }
168
169 #ifdef LDAP_COMPAT30
170         if ( conn->c_version == 30 ) {
171                 (void) ber_skip_tag( ber, &len );
172         }
173 #endif
174
175         arg = (struct co_arg *) ch_malloc( sizeof(struct co_arg) );
176         arg->co_conn = conn;
177
178         pthread_mutex_lock( &conn->c_dnmutex );
179         if ( conn->c_dn != NULL ) {
180                 tmpdn = ch_strdup( conn->c_dn );
181         } else {
182                 tmpdn = NULL;
183         }
184         pthread_mutex_unlock( &conn->c_dnmutex );
185
186         pthread_mutex_lock( &conn->c_opsmutex );
187         arg->co_op = slap_op_add( &conn->c_ops, ber, msgid, tag, tmpdn,
188             conn->c_opsinitiated, conn->c_connid );
189         pthread_mutex_unlock( &conn->c_opsmutex );
190
191         if ( tmpdn != NULL ) {
192                 free( tmpdn );
193         }
194
195         pthread_attr_init( &attr );
196         pthread_attr_setdetachstate( &attr, PTHREAD_CREATE_DETACHED );
197 #if !defined(HAVE_PTHREADS_D4)
198         /* POSIX_THREADS or compatible
199          * This is a draft 10 or standard pthreads implementation
200          */
201         if ( pthread_create( &arg->co_op->o_tid, &attr,
202             connection_operation, (void *) arg ) != 0 ) {
203                 Debug( LDAP_DEBUG_ANY, "pthread_create failed\n", 0, 0, 0 );
204         } else {
205                 pthread_mutex_lock( &active_threads_mutex );
206                 active_threads++;
207                 pthread_mutex_unlock( &active_threads_mutex );
208         }
209 #else   /* pthread draft4  */
210         /*
211          * This is a draft 4 or earlier pthreads implementation
212          */
213         if ( pthread_create( &arg->co_op->o_tid, attr,
214             connection_operation, (void *) arg ) != 0 ) {
215                 Debug( LDAP_DEBUG_ANY, "pthread_create failed\n", 0, 0, 0 );
216         } else {
217                 pthread_mutex_lock( &active_threads_mutex );
218                 active_threads++;
219                 pthread_mutex_unlock( &active_threads_mutex );
220         }
221 #endif  /* pthread draft4 */
222         pthread_attr_destroy( &attr );
223 }