~ubuntu-branches/ubuntu/maverick/tomcat6/maverick-updates

« back to all changes in this revision

Viewing changes to native/connector/os/unix/uxpipe.c

  • Committer: Bazaar Package Importer
  • Author(s): Torsten Werner
  • Date: 2010-01-23 19:40:38 UTC
  • mfrom: (2.2.6 sid)
  • Revision ID: james.westby@ubuntu.com-20100123194038-0tnrf6kiy0wrjrw9
Tags: 6.0.20-dfsg1-1
* Fix debian/orig-tar.sh to exclude binary only standard.jar and jstl.jar.
  (Closes: #528119)
* Upload a cleaned tarball.
* Add ${misc:Depends} in debian/control.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Licensed to the Apache Software Foundation (ASF) under one or more
2
 
 * contributor license agreements.  See the NOTICE file distributed with
3
 
 * this work for additional information regarding copyright ownership.
4
 
 * The ASF licenses this file to You under the Apache License, Version 2.0
5
 
 * (the "License"); you may not use this file except in compliance with
6
 
 * the License.  You may obtain a copy of the License at
7
 
 *
8
 
 *     http://www.apache.org/licenses/LICENSE-2.0
9
 
 *
10
 
 * Unless required by applicable law or agreed to in writing, software
11
 
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 
 * See the License for the specific language governing permissions and
14
 
 * limitations under the License.
15
 
 */
16
 
 
17
 
/** UNIX AF_LOCAL network wrapper
18
 
 *
19
 
 * @author Mladen Turk
20
 
 * @version $Revision: 466585 $, $Date: 2006-10-22 00:16:34 +0200 (Sun, 22 Oct 2006) $
21
 
 */
22
 
 
23
 
 
24
 
#include "tcn.h"
25
 
#include "apr_thread_mutex.h"
26
 
#include "apr_poll.h"
27
 
 
28
 
/* ### should be tossed in favor of APR */
29
 
#include <sys/stat.h>
30
 
#include <sys/un.h> /* for sockaddr_un */
31
 
 
32
 
#ifdef TCN_DO_STATISTICS
33
 
#include "apr_atomic.h"
34
 
 
35
 
static volatile apr_uint32_t uxp_created  = 0;
36
 
static volatile apr_uint32_t uxp_closed   = 0;
37
 
static volatile apr_uint32_t uxp_cleared  = 0;
38
 
static volatile apr_uint32_t uxp_accepted = 0;
39
 
 
40
 
void uxp_network_dump_statistics()
41
 
{
42
 
    fprintf(stderr, "NT Network Statistics ..\n");
43
 
    fprintf(stderr, "Sockets created         : %d\n", uxp_created);
44
 
    fprintf(stderr, "Sockets accepted        : %d\n", uxp_accepted);
45
 
    fprintf(stderr, "Sockets closed          : %d\n", uxp_closed);
46
 
    fprintf(stderr, "Sockets cleared         : %d\n", uxp_cleared);
47
 
}
48
 
 
49
 
#endif
50
 
 
51
 
#define DEFNAME     "/var/run/tomcatnativesock"
52
 
#define DEFNAME_FMT "/var/run/tomcatnativesock%08x%08x"
53
 
#define DEFSIZE     8192
54
 
#define DEFTIMEOUT  60000
55
 
 
56
 
#define TCN_UXP_UNKNOWN     0
57
 
#define TCN_UXP_CLIENT      1
58
 
#define TCN_UXP_ACCEPTED    2
59
 
#define TCN_UXP_SERVER      3
60
 
 
61
 
#define TCN_UNIX_MAXPATH    1024
62
 
typedef struct {
63
 
    apr_pool_t          *pool;
64
 
    apr_socket_t        *sock;               /* APR socket */
65
 
    int                 sd;
66
 
    struct sockaddr_un  uxaddr;
67
 
    int                 timeout;
68
 
    int                 mode;                 /* Client or server mode */
69
 
    char                name[TCN_UNIX_MAXPATH+1];
70
 
} tcn_uxp_conn_t;
71
 
 
72
 
static apr_status_t APR_THREAD_FUNC
73
 
uxp_socket_timeout_set(apr_socket_t *sock, apr_interval_time_t t)
74
 
{
75
 
    tcn_uxp_conn_t *con = (tcn_uxp_conn_t *)sock;
76
 
    if (t < 0)
77
 
        con->timeout = -1;
78
 
    else
79
 
        con->timeout = (int)(apr_time_as_msec(t));
80
 
    return APR_SUCCESS;
81
 
}
82
 
 
83
 
static apr_status_t APR_THREAD_FUNC
84
 
uxp_socket_timeout_get(apr_socket_t *sock, apr_interval_time_t *t)
85
 
{
86
 
    tcn_uxp_conn_t *con = (tcn_uxp_conn_t*)sock;
87
 
    if (con->timeout < 0)
88
 
        *t = -1;
89
 
    else
90
 
        *t = con->timeout * 1000;
91
 
    return APR_SUCCESS;
92
 
}
93
 
 
94
 
static APR_INLINE apr_status_t APR_THREAD_FUNC
95
 
uxp_socket_opt_set(apr_socket_t *sock, apr_int32_t opt, apr_int32_t on)
96
 
{
97
 
    tcn_uxp_conn_t *con = (tcn_uxp_conn_t *)sock;
98
 
    return apr_socket_opt_set(con->sock, opt, on);
99
 
}
100
 
 
101
 
static APR_INLINE apr_status_t APR_THREAD_FUNC
102
 
uxp_socket_opt_get(apr_socket_t *sock, apr_int32_t opt, apr_int32_t *on)
103
 
{
104
 
    tcn_uxp_conn_t *con = (tcn_uxp_conn_t *)sock;
105
 
    return apr_socket_opt_get(con->sock, opt, on);
106
 
}
107
 
 
108
 
static apr_status_t uxp_cleanup(void *data)
109
 
{
110
 
    tcn_uxp_conn_t *con = (tcn_uxp_conn_t *)data;
111
 
 
112
 
    if (con) {
113
 
        if (con->sock) {
114
 
            apr_socket_close(con->sock);
115
 
            con->sock = NULL;
116
 
        }
117
 
        if (con->mode == TCN_UXP_SERVER) {
118
 
            unlink(con->name);
119
 
            con->mode = TCN_UXP_UNKNOWN;
120
 
        }
121
 
    }
122
 
 
123
 
#ifdef TCN_DO_STATISTICS
124
 
    apr_atomic_inc32(&uxp_cleared);
125
 
#endif
126
 
    return APR_SUCCESS;
127
 
}
128
 
 
129
 
static apr_status_t APR_THREAD_FUNC
130
 
uxp_socket_shutdown(apr_socket_t *sock, apr_shutdown_how_e how)
131
 
{
132
 
    tcn_uxp_conn_t *con = (tcn_uxp_conn_t *)sock;
133
 
    return apr_socket_shutdown(con->sock, how);
134
 
}
135
 
 
136
 
static apr_status_t APR_THREAD_FUNC
137
 
uxp_socket_close(apr_socket_t *sock)
138
 
{
139
 
#ifdef TCN_DO_STATISTICS
140
 
    apr_atomic_inc32(&uxp_closed);
141
 
#endif
142
 
    return uxp_cleanup(sock);
143
 
}
144
 
 
145
 
static apr_status_t APR_THREAD_FUNC
146
 
uxp_socket_recv(apr_socket_t *sock, char *buf, apr_size_t *len)
147
 
{
148
 
    tcn_uxp_conn_t *con = (tcn_uxp_conn_t *)sock;
149
 
    return apr_socket_recv(con->sock, buf, len);
150
 
}
151
 
 
152
 
 
153
 
static apr_status_t APR_THREAD_FUNC
154
 
uxp_socket_send(apr_socket_t *sock, const char *buf,
155
 
                apr_size_t *len)
156
 
{
157
 
    tcn_uxp_conn_t *con = (tcn_uxp_conn_t *)sock;
158
 
    return apr_socket_send(con->sock, buf, len);
159
 
}
160
 
 
161
 
static apr_status_t APR_THREAD_FUNC
162
 
uxp_socket_sendv(apr_socket_t *sock,
163
 
                 const struct iovec *vec,
164
 
                 apr_int32_t nvec, apr_size_t *len)
165
 
{
166
 
    tcn_uxp_conn_t *con = (tcn_uxp_conn_t *)sock;
167
 
    return apr_socket_sendv(con->sock, vec, nvec, len);
168
 
}
169
 
 
170
 
static apr_status_t uxp_socket_cleanup(void *data)
171
 
{
172
 
    tcn_socket_t *s = (tcn_socket_t *)data;
173
 
 
174
 
    if (s->net->cleanup) {
175
 
        (*s->net->cleanup)(s->opaque);
176
 
        s->net->cleanup = NULL;
177
 
    }
178
 
#ifdef TCN_DO_STATISTICS
179
 
    apr_atomic_inc32(&uxp_cleared);
180
 
#endif
181
 
    return APR_SUCCESS;
182
 
}
183
 
 
184
 
static tcn_nlayer_t uxp_socket_layer = {
185
 
    TCN_SOCKET_UNIX,
186
 
    uxp_cleanup,
187
 
    uxp_socket_close,
188
 
    uxp_socket_shutdown,
189
 
    uxp_socket_opt_get,
190
 
    uxp_socket_opt_set,
191
 
    uxp_socket_timeout_get,
192
 
    uxp_socket_timeout_set,
193
 
    uxp_socket_send,
194
 
    uxp_socket_sendv,
195
 
    uxp_socket_recv
196
 
};
197
 
 
198
 
TCN_IMPLEMENT_CALL(jlong, Local, create)(TCN_STDARGS, jstring name,
199
 
                                         jlong pool)
200
 
{
201
 
    apr_pool_t *p = J2P(pool, apr_pool_t *);
202
 
    tcn_socket_t   *s   = NULL;
203
 
    tcn_uxp_conn_t *con = NULL;
204
 
    int sd;
205
 
    TCN_ALLOC_CSTRING(name);
206
 
 
207
 
    UNREFERENCED(o);
208
 
    TCN_ASSERT(pool != 0);
209
 
 
210
 
    if ((sd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
211
 
        tcn_ThrowAPRException(e, apr_get_netos_error());
212
 
        return 0;
213
 
    }
214
 
#ifdef TCN_DO_STATISTICS
215
 
    uxp_created++;
216
 
#endif
217
 
    con = (tcn_uxp_conn_t *)apr_pcalloc(p, sizeof(tcn_uxp_conn_t));
218
 
    con->pool = p;
219
 
    con->mode = TCN_UXP_UNKNOWN;
220
 
    con->timeout = DEFTIMEOUT;
221
 
    con->sd = sd;
222
 
    con->uxaddr.sun_family = AF_UNIX;
223
 
    if (J2S(name)) {
224
 
        strcpy(con->uxaddr.sun_path, J2S(name));
225
 
        TCN_FREE_CSTRING(name);
226
 
    }
227
 
    else
228
 
        strcpy(con->uxaddr.sun_path, DEFNAME);
229
 
    s = (tcn_socket_t *)apr_pcalloc(p, sizeof(tcn_socket_t));
230
 
    s->pool   = p;
231
 
    s->net    = &uxp_socket_layer;
232
 
    s->opaque = con;
233
 
    apr_pool_cleanup_register(p, (const void *)s,
234
 
                              uxp_socket_cleanup,
235
 
                              apr_pool_cleanup_null);
236
 
 
237
 
    apr_os_sock_put(&(con->sock), &(con->sd), p);
238
 
 
239
 
    return P2J(s);
240
 
 
241
 
}
242
 
 
243
 
TCN_IMPLEMENT_CALL(jint, Local, bind)(TCN_STDARGS, jlong sock,
244
 
                                      jlong sa)
245
 
{
246
 
    tcn_socket_t *s = J2P(sock, tcn_socket_t *);
247
 
    UNREFERENCED_STDARGS;
248
 
    UNREFERENCED(sa);
249
 
    TCN_ASSERT(sock != 0);
250
 
    if (s->net->type == TCN_SOCKET_UNIX) {
251
 
        int rc;
252
 
        tcn_uxp_conn_t *c = (tcn_uxp_conn_t *)s->opaque;
253
 
        c->mode = TCN_UXP_SERVER;
254
 
        rc = bind(c->sd, (struct sockaddr *)&(c->uxaddr), sizeof(c->uxaddr));
255
 
        if (rc < 0)
256
 
            return errno;
257
 
        else
258
 
            return APR_SUCCESS;
259
 
    }
260
 
    else
261
 
        return APR_EINVAL;
262
 
}
263
 
 
264
 
TCN_IMPLEMENT_CALL(jint, Local, listen)(TCN_STDARGS, jlong sock,
265
 
                                        jint backlog)
266
 
{
267
 
    tcn_socket_t *s = J2P(sock, tcn_socket_t *);
268
 
    UNREFERENCED_STDARGS;
269
 
 
270
 
    TCN_ASSERT(sock != 0);
271
 
    if (s->net->type == TCN_SOCKET_UNIX) {
272
 
        tcn_uxp_conn_t *c = (tcn_uxp_conn_t *)s->opaque;
273
 
        c->mode = TCN_UXP_SERVER;
274
 
        return apr_socket_listen(c->sock, (apr_int32_t)backlog);
275
 
    }
276
 
    else
277
 
        return APR_EINVAL;
278
 
}
279
 
 
280
 
TCN_IMPLEMENT_CALL(jlong, Local, accept)(TCN_STDARGS, jlong sock)
281
 
{
282
 
    tcn_socket_t *s = J2P(sock, tcn_socket_t *);
283
 
    apr_pool_t   *p = NULL;
284
 
    tcn_socket_t *a = NULL;
285
 
    tcn_uxp_conn_t *con = NULL;
286
 
 
287
 
    UNREFERENCED(o);
288
 
    TCN_ASSERT(sock != 0);
289
 
 
290
 
    TCN_THROW_IF_ERR(apr_pool_create(&p, s->pool), p);
291
 
    if (s->net->type == TCN_SOCKET_UNIX) {
292
 
        apr_socklen_t len;
293
 
        tcn_uxp_conn_t *c = (tcn_uxp_conn_t *)s->opaque;
294
 
        con = (tcn_uxp_conn_t *)apr_pcalloc(p, sizeof(tcn_uxp_conn_t));
295
 
        con->pool = p;
296
 
        con->mode = TCN_UXP_ACCEPTED;
297
 
        con->timeout = c->timeout;
298
 
        len = sizeof(c->uxaddr);
299
 
        /* Block until a client connects */
300
 
        con->sd = accept(c->sd, (struct sockaddr *)&(con->uxaddr), &len);
301
 
        if (con->sd < 0) {
302
 
            tcn_ThrowAPRException(e, apr_get_os_error());
303
 
            goto cleanup;
304
 
        }
305
 
    }
306
 
    else {
307
 
        tcn_ThrowAPRException(e, APR_ENOTIMPL);
308
 
        goto cleanup;
309
 
    }
310
 
    if (con) {
311
 
#ifdef TCN_DO_STATISTICS
312
 
        apr_atomic_inc32(&uxp_accepted);
313
 
#endif
314
 
        a = (tcn_socket_t *)apr_pcalloc(p, sizeof(tcn_socket_t));
315
 
        a->pool   = p;
316
 
            a->net    = &uxp_socket_layer;
317
 
        a->opaque = con;
318
 
        apr_pool_cleanup_register(p, (const void *)a,
319
 
                                  uxp_socket_cleanup,
320
 
                                  apr_pool_cleanup_null);
321
 
        apr_os_sock_put(&(con->sock), &(con->sd), p);
322
 
    }
323
 
    return P2J(a);
324
 
cleanup:
325
 
    if (p)
326
 
        apr_pool_destroy(p);
327
 
    return 0;
328
 
}
329
 
 
330
 
TCN_IMPLEMENT_CALL(jint, Local, connect)(TCN_STDARGS, jlong sock,
331
 
                                         jlong sa)
332
 
{
333
 
    tcn_socket_t *s = J2P(sock, tcn_socket_t *);
334
 
    tcn_uxp_conn_t *con = NULL;
335
 
    int rc;
336
 
 
337
 
    UNREFERENCED(o);
338
 
    UNREFERENCED(sa);
339
 
    TCN_ASSERT(sock != 0);
340
 
    if (s->net->type != TCN_SOCKET_UNIX)
341
 
        return APR_ENOTSOCK;
342
 
    con = (tcn_uxp_conn_t *)s->opaque;
343
 
    if (con->mode != TCN_UXP_UNKNOWN)
344
 
        return APR_EINVAL;
345
 
    do {
346
 
        rc = connect(con->sd, (const struct sockaddr *)&(con->uxaddr),
347
 
                     sizeof(con->uxaddr));
348
 
    } while (rc == -1 && errno == EINTR);
349
 
 
350
 
    if (rc == -1 && errno != EISCONN)
351
 
        return errno;
352
 
    con->mode = TCN_UXP_CLIENT;
353
 
 
354
 
    return APR_SUCCESS;
355
 
}