~ubuntu-branches/ubuntu/gutsy/stunnel4/gutsy

« back to all changes in this revision

Viewing changes to src/sthreads.c

  • Committer: Bazaar Package Importer
  • Author(s): Julien Lemoine
  • Date: 2006-11-07 20:22:04 UTC
  • mfrom: (3.1.5 feisty)
  • Revision ID: james.westby@ubuntu.com-20061107202204-b8b3rivwi3nla2pz
Tags: 3:4.18-2
* Updated chroot default path in configuration file
* Added LSB section in init script

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
2
 *   stunnel       Universal SSL tunnel
3
 
 *   Copyright (c) 1998-2005 Michal Trojnara <Michal.Trojnara@mirt.net>
 
3
 *   Copyright (c) 1998-2006 Michal Trojnara <Michal.Trojnara@mirt.net>
4
4
 *                 All Rights Reserved
5
5
 *
6
6
 *   This program is free software; you can redistribute it and/or modify
28
28
 *   do so, delete this exception statement from your version.
29
29
 */
30
30
 
 
31
#ifdef USE_OS2
 
32
#define INCL_DOSPROCESS
 
33
#include <os2.h>
 
34
#endif
 
35
 
31
36
#include "common.h"
32
37
#include "prototypes.h"
33
38
 
34
 
#define STACK_SIZE 65536
 
39
#if defined(USE_UCONTEXT) || defined(USE_FORK)
 
40
/* no need for critical sections */
 
41
 
 
42
void enter_critical_section(SECTION_CODE i) {
 
43
    /* empty */
 
44
}
 
45
 
 
46
void leave_critical_section(SECTION_CODE i) {
 
47
    /* empty */
 
48
}
 
49
 
 
50
#endif /* USE_UCONTEXT || USE_FORK */
 
51
 
 
52
#ifdef USE_UCONTEXT
 
53
 
 
54
#if defined(CPU_SPARC) && ( \
 
55
        defined(OS_SOLARIS2_0) || \
 
56
        defined(OS_SOLARIS2_1) || \
 
57
        defined(OS_SOLARIS2_2) || \
 
58
        defined(OS_SOLARIS2_3) || \
 
59
        defined(OS_SOLARIS2_4) || \
 
60
        defined(OS_SOLARIS2_5) || \
 
61
        defined(OS_SOLARIS2_6) || \
 
62
        defined(OS_SOLARIS2_7) || \
 
63
        defined(OS_SOLARIS2_8))
 
64
#define ARGC 2
 
65
#else
 
66
#define ARGC 1
 
67
#endif
 
68
 
 
69
/* first context on the ready list is the active context */
 
70
CONTEXT *ready_head=NULL, *ready_tail=NULL;         /* ready to execute */
 
71
CONTEXT *waiting_head=NULL, *waiting_tail=NULL;     /* waiting on poll() */
 
72
int next_id=1;
 
73
 
 
74
unsigned long stunnel_process_id(void) {
 
75
    return (unsigned long)getpid();
 
76
}
 
77
 
 
78
unsigned long stunnel_thread_id(void) {
 
79
    return ready_head ? ready_head->id : 0;
 
80
}
 
81
 
 
82
static CONTEXT *new_context(void) {
 
83
    CONTEXT *ctx;
 
84
 
 
85
    /* allocate and fill the CONTEXT structure */
 
86
    ctx=malloc(sizeof(CONTEXT));
 
87
    if(!ctx) {
 
88
        s_log(LOG_ERR, "Unable to allocate CONTEXT structure");
 
89
        return NULL;
 
90
    }
 
91
    ctx->id=next_id++;
 
92
    ctx->fds=NULL;
 
93
    ctx->ready=0;
 
94
    /* some manuals claim that initialization of ctx structure is required */
 
95
    if(getcontext(&ctx->ctx)<0) {
 
96
        free(ctx);
 
97
        ioerror("getcontext");
 
98
        return NULL;
 
99
    }
 
100
    ctx->ctx.uc_link=NULL; /* it should never happen */
 
101
#if defined(__sgi) || ARGC==2 /* obsolete ss_sp semantics */
 
102
    ctx->ctx.uc_stack.ss_sp=ctx->stack+STACK_SIZE-8;
 
103
#else
 
104
    ctx->ctx.uc_stack.ss_sp=ctx->stack;
 
105
#endif
 
106
    ctx->ctx.uc_stack.ss_size=STACK_SIZE;
 
107
    ctx->ctx.uc_stack.ss_flags=0;
 
108
 
 
109
    /* attach to the tail of the ready queue */
 
110
    ctx->next=NULL;
 
111
    if(ready_tail)
 
112
        ready_tail->next=ctx;
 
113
    ready_tail=ctx;
 
114
    if(!ready_head)
 
115
        ready_head=ctx;
 
116
    return ctx;
 
117
}
 
118
 
 
119
/* s_log is not initialized here, but we can use log_raw */
 
120
void sthreads_init(void) {
 
121
    /* create the first (listening) context and put it in the running queue */
 
122
    if(!new_context()) {
 
123
        log_raw("Unable create the listening context");
 
124
        exit(1);
 
125
    }
 
126
}
 
127
 
 
128
int create_client(int ls, int s, void *arg, void *(*cli)(void *)) {
 
129
    CONTEXT *ctx;
 
130
 
 
131
    s_log(LOG_DEBUG, "Creating a new context");
 
132
    ctx=new_context();
 
133
    if(!ctx)
 
134
        return -1;
 
135
    s_log(LOG_DEBUG, "Context %ld created", ctx->id);
 
136
    makecontext(&ctx->ctx, (void(*)(void))cli, ARGC, arg);
 
137
    return 0;
 
138
}
 
139
 
 
140
#endif /* USE_UCONTEXT */
 
141
 
 
142
#ifdef USE_FORK
 
143
 
 
144
void sthreads_init(void) {
 
145
    /* empty */
 
146
}
 
147
 
 
148
unsigned long stunnel_process_id(void) {
 
149
    return (unsigned long)getpid();
 
150
}
 
151
 
 
152
unsigned long stunnel_thread_id(void) {
 
153
    return 0L;
 
154
}
 
155
 
 
156
static void null_handler(int sig) {
 
157
    signal(SIGCHLD, null_handler);
 
158
}
 
159
 
 
160
int create_client(int ls, int s, void *arg, void *(*cli)(void *)) {
 
161
    switch(fork()) {
 
162
    case -1:    /* error */
 
163
        if(arg)
 
164
            free(arg);
 
165
        if(s>=0)
 
166
            closesocket(s);
 
167
        return -1;
 
168
    case  0:    /* child */
 
169
        if(ls>=0)
 
170
            closesocket(ls);
 
171
        signal(SIGCHLD, null_handler);
 
172
        cli(arg);
 
173
        exit(0);
 
174
    default:    /* parent */
 
175
        if(arg)
 
176
            free(arg);
 
177
        if(s>=0)
 
178
            closesocket(s);
 
179
    }
 
180
    return 0;
 
181
}
 
182
 
 
183
#endif /* USE_FORK */
35
184
 
36
185
#ifdef USE_PTHREAD
37
186
 
38
 
#include <pthread.h>
39
 
 
40
187
static pthread_mutex_t stunnel_cs[CRIT_SECTIONS];
41
 
 
42
188
static pthread_mutex_t lock_cs[CRYPTO_NUM_LOCKS];
43
189
static pthread_attr_t pth_attr;
44
190
 
116
262
    return 0;
117
263
}
118
264
 
119
 
#endif
 
265
#endif /* USE_PTHREAD */
120
266
 
121
267
#ifdef USE_WIN32
122
268
 
123
 
CRITICAL_SECTION stunnel_cs[CRIT_SECTIONS];
 
269
static CRITICAL_SECTION stunnel_cs[CRIT_SECTIONS];
 
270
static CRITICAL_SECTION lock_cs[CRYPTO_NUM_LOCKS];
124
271
 
125
272
void enter_critical_section(SECTION_CODE i) {
126
273
    EnterCriticalSection(stunnel_cs+i);
130
277
    LeaveCriticalSection(stunnel_cs+i);
131
278
}
132
279
 
 
280
static void locking_callback(int mode, int type,
 
281
#ifdef HAVE_OPENSSL
 
282
    const /* Callback definition has been changed in openssl 0.9.3 */
 
283
#endif
 
284
    char *file, int line) {
 
285
    if(mode&CRYPTO_LOCK)
 
286
        EnterCriticalSection(lock_cs+type);
 
287
    else
 
288
        LeaveCriticalSection(lock_cs+type);
 
289
}
 
290
 
133
291
void sthreads_init(void) {
134
292
    int i;
135
293
 
136
294
    /* Initialize stunnel critical sections */
137
295
    for(i=0; i<CRIT_SECTIONS; i++)
138
296
        InitializeCriticalSection(stunnel_cs+i);
 
297
 
 
298
    /* Initialize OpenSSL locking callback */
 
299
    for(i=0; i<CRYPTO_NUM_LOCKS; i++)
 
300
        InitializeCriticalSection(lock_cs+i);
 
301
    CRYPTO_set_locking_callback(locking_callback);
139
302
}
140
303
 
141
304
unsigned long stunnel_process_id(void) {
158
321
 
159
322
#endif
160
323
 
161
 
#ifdef USE_FORK
 
324
#ifdef USE_OS2
162
325
 
163
326
void enter_critical_section(SECTION_CODE i) {
164
 
    /* empty */
 
327
    DosEnterCritSec();
165
328
}
166
329
 
167
330
void leave_critical_section(SECTION_CODE i) {
168
 
    /* empty */
 
331
    DosExitCritSec();
169
332
}
170
333
 
171
334
void sthreads_init(void) {
172
 
    /* empty */
173
335
}
174
336
 
175
337
unsigned long stunnel_process_id(void) {
176
 
    return (unsigned long)getpid();
 
338
    PTIB ptib=NULL;
 
339
    DosGetInfoBlocks(&ptib, NULL);
 
340
    return (unsigned long)ptib->tib_ordinal;
177
341
}
178
342
 
179
343
unsigned long stunnel_thread_id(void) {
180
 
    return 0L;
181
 
}
182
 
 
183
 
static void null_handler(int sig) {
184
 
    signal(SIGCHLD, null_handler);
 
344
    PPIB ppib=NULL;
 
345
    DosGetInfoBlocks(NULL, &ppib);
 
346
    return (unsigned long)ppib->pib_ulpid;
185
347
}
186
348
 
187
349
int create_client(int ls, int s, void *arg, void *(*cli)(void *)) {
188
 
    switch(fork()) {
189
 
    case -1:    /* error */
190
 
        if(arg)
191
 
            free(arg);
192
 
        if(s>=0)
193
 
            closesocket(s);
 
350
    s_log(LOG_DEBUG, "Creating a new thread");
 
351
    if(_beginthread((void(*)(void *))cli, NULL, STACK_SIZE, arg)==-1) {
 
352
        ioerror("_beginthread");
194
353
        return -1;
195
 
    case  0:    /* child */
196
 
        if(ls>=0)
197
 
            closesocket(ls);
198
 
        signal(SIGCHLD, null_handler);
199
 
        cli(arg);
200
 
        exit(0);
201
 
    default:    /* parent */
202
 
        if(arg)
203
 
            free(arg);
204
 
        if(s>=0)
205
 
            closesocket(s);
206
354
    }
207
 
    return 0;
208
 
}
209
 
 
210
 
#endif
 
355
    s_log(LOG_DEBUG, "New thread created");
 
356
    return 0;
 
357
}
 
358
 
 
359
#ifdef _WIN32_WCE
 
360
 
 
361
int _beginthread(void (*start_address)(void *),
 
362
        int stack_size, void *arglist) {
 
363
    DWORD thread_id;
 
364
    HANDLE handle;
 
365
 
 
366
    handle=CreateThread(NULL, stack_size,
 
367
        (LPTHREAD_START_ROUTINE)start_address, arglist, 0, &thread_id);
 
368
    if(!handle)
 
369
        return -1;
 
370
    CloseHandle(handle);
 
371
    return 0;
 
372
}
 
373
 
 
374
void _endthread(void) {
 
375
    ExitThread(0);
 
376
}
 
377
 
 
378
#endif /* !defined(_WIN32_WCE) */
 
379
 
 
380
#endif /* USE_WIN32 */
211
381
 
212
382
#ifdef DEBUG_STACK_SIZE
213
383
 
214
 
#define STACK_RESERVE (STACK_SIZE/2)
215
 
#define TEST_VALUE 44
216
 
 
217
 
/* Some heuristic to determine the usage of client stack size.  It can
218
 
 * fail on some platforms and/or OSes, so it'is not enabled by default. */
219
 
 
 
384
#define STACK_RESERVE (STACK_SIZE/8)
 
385
#define VERIFY_AREA ((STACK_SIZE-STACK_RESERVE)/sizeof(u32))
 
386
#define TEST_VALUE 0xdeadbeef
 
387
 
 
388
/* some heuristic to determine the usage of client stack size */
220
389
void stack_info(int init) { /* 1-initialize, 0-display */
221
 
    char table[STACK_SIZE-STACK_RESERVE];
222
 
    int i;
 
390
    u32 table[VERIFY_AREA];
 
391
    int i, num;
 
392
    static int min_num=VERIFY_AREA;
223
393
 
224
394
    if(init) {
225
 
        memset(table, TEST_VALUE, STACK_SIZE-STACK_RESERVE);
 
395
        for(i=0; i<VERIFY_AREA; i++)
 
396
            table[i]=TEST_VALUE;
226
397
    } else {
227
 
        i=0;
228
 
        while(i<STACK_SIZE-STACK_RESERVE && table[i]==TEST_VALUE)
229
 
            i++;
230
 
        if(i<64)
231
 
            s_log(LOG_ERR, "STACK_RESERVE is to high");
232
 
        else
233
 
            s_log(LOG_NOTICE, "stack_info: %d of %d bytes used (%d%%)",
234
 
                STACK_SIZE-STACK_RESERVE-i, STACK_SIZE,
235
 
                (STACK_SIZE-STACK_RESERVE-i)*100/STACK_SIZE);
 
398
        /* the stack is growing down */
 
399
        for(i=0; i<VERIFY_AREA; i++)
 
400
            if(table[i]!=TEST_VALUE)
 
401
                break;
 
402
        num=i;
 
403
        /* the stack is growing up */
 
404
        for(i=0; i<VERIFY_AREA; i++)
 
405
            if(table[VERIFY_AREA-i-1]!=TEST_VALUE)
 
406
                break;
 
407
        if(i>num) /* use the higher value */
 
408
            num=i;
 
409
        if(num<64) {
 
410
            s_log(LOG_NOTICE, "STACK_RESERVE is too high");
 
411
            return;
 
412
        }
 
413
        if(num<min_num)
 
414
            min_num=num;
 
415
        s_log(LOG_NOTICE,
 
416
            "stack_info: size=%d, current=%d (%d%%), maximum=%d (%d%%)",
 
417
            STACK_SIZE,
 
418
            (int)((VERIFY_AREA-num)*sizeof(u32)),
 
419
            (int)((VERIFY_AREA-num)*sizeof(u32)*100/STACK_SIZE),
 
420
            (int)((VERIFY_AREA-min_num)*sizeof(u32)),
 
421
            (int)((VERIFY_AREA-min_num)*sizeof(u32)*100/STACK_SIZE));
236
422
    }
237
423
}
238
424