summaryrefslogtreecommitdiff
path: root/security/nss/lib/ssl/sslmutex.c
blob: 7c021ffd71e4720ac063902d3b7a4123d6bea2cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/*
 * The contents of this file are subject to the Mozilla Public
 * License Version 1.1 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a copy of
 * the License at http://www.mozilla.org/MPL/
 * 
 * Software distributed under the License is distributed on an "AS
 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * rights and limitations under the License.
 * 
 * The Original Code is the Netscape security libraries.
 * 
 * The Initial Developer of the Original Code is Netscape
 * Communications Corporation.  Portions created by Netscape are 
 * Copyright (C) 2001 Netscape Communications Corporation.  All
 * Rights Reserved.
 * 
 * Contributor(s): 
 * 
 * Alternatively, the contents of this file may be used under the
 * terms of the GNU General Public License Version 2 or later (the
 * "GPL"), in which case the provisions of the GPL are applicable 
 * instead of those above.  If you wish to allow use of your 
 * version of this file only under the terms of the GPL and not to
 * allow others to use your version of this file under the MPL,
 * indicate your decision by deleting the provisions above and
 * replace them with the notice and other provisions required by
 * the GPL.  If you do not delete the provisions above, a recipient
 * may use your version of this file under either the MPL or the
 * GPL.
 *
 * $Id$
 */

#include "sslmutex.h"
#include "prerr.h"

#if defined(LINUX) || defined(AIX)

#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include "unix_err.h"

#define SSL_MUTEX_MAGIC 0xfeedfd
#define NONBLOCKING_POSTS 1	/* maybe this is faster */

#if NONBLOCKING_POSTS

#ifndef FNONBLOCK
#define FNONBLOCK O_NONBLOCK
#endif

static int
setNonBlocking(int fd, int nonBlocking)
{
    int flags;
    int err;

    flags = fcntl(fd, F_GETFL, 0);
    if (0 > flags)
	return flags;
    if (nonBlocking)
	flags |= FNONBLOCK;
    else
	flags &= ~FNONBLOCK;
    err = fcntl(fd, F_SETFL, flags);
    return err;
}
#endif

SECStatus
sslMutex_Init(sslMutex *pMutex, int shared)
{
    int  err;
    int  mPipes[3];
    char c      = 1;

    memset(pMutex, -1, sizeof *pMutex);
    err = pipe(mPipes);
    if (err) {
	return err;
    }
    /* close-on-exec is false by default */
    if (!shared) {
	err = fcntl(mPipes[0], F_SETFD, FD_CLOEXEC);
	if (err) 
	    goto loser;

	err = fcntl(mPipes[1], F_SETFD, FD_CLOEXEC);
	if (err) 
	    goto loser;
    }

#if NONBLOCKING_POSTS
    err = setNonBlocking(mPipes[1], 1);
    if (err)
	goto loser;
#endif


    do {
	err = write(mPipes[1], &c, 1);
    } while (err < 0 && (errno == EINTR || errno == EAGAIN));
    if (0 > err)
	goto loser;

    mPipes[2] = SSL_MUTEX_MAGIC;

    memcpy(pMutex, mPipes, sizeof mPipes);
    return SECSuccess;

loser:
    nss_MD_unix_map_default_error(errno);
    close(mPipes[0]);
    close(mPipes[1]);
    return SECFailure;
}

SECStatus
sslMutex_Destroy(sslMutex *pMutex)
{
    int * mPipes = (int *)pMutex;
    if (mPipes[2] != SSL_MUTEX_MAGIC) {
	PORT_SetError(PR_INVALID_ARGUMENT_ERROR);
	return SECFailure;
    }
    close(mPipes[0]);
    close(mPipes[1]);
    memset(pMutex, -1, sizeof *pMutex);
    return SECSuccess;
}

SECStatus 
sslMutex_Unlock(sslMutex *pMutex)
{
    int * mPipes = (int *)pMutex;
    int   cc;
    char  c  = 1;

    if (mPipes[2] != SSL_MUTEX_MAGIC) {
	PORT_SetError(PR_INVALID_ARGUMENT_ERROR);
	return SECFailure;
    }
    do {
	cc = write(mPipes[1], &c, 1);
    } while (cc < 0 && (errno == EINTR || errno == EAGAIN));
    if (cc == 1)
	return SECSuccess;
    if (cc < 0)
	nss_MD_unix_map_default_error(errno);
    else
	PORT_SetError(PR_UNKNOWN_ERROR);
    return SECFailure;
}

SECStatus 
sslMutex_Lock(sslMutex *pMutex)
{
    int * mPipes = (int *)pMutex;
    int   cc;
    char  c;

    if (mPipes[2] != SSL_MUTEX_MAGIC) {
	PORT_SetError(PR_INVALID_ARGUMENT_ERROR);
	return SECFailure;
    }
    do {
	cc = read(mPipes[0], &c, 1);
    } while (cc < 0 && errno == EINTR);
    if (cc == 1)
	return SECSuccess;
    if (cc < 0)
	nss_MD_unix_map_default_error(errno);
    else
	PORT_SetError(PR_UNKNOWN_ERROR);
    return SECFailure;
}

#elif defined(WIN32)

#include "win32err.h"

/* The presence of the TRUE element in this struct makes the semaphore
 * inheritable. The NULL means use process's default security descriptor.
 */

SECStatus
sslMutex_Init(sslMutex *pMutex, int shared)
{

    HANDLE hMutex;
    SECURITY_ATTRIBUTES attributes =
                                { sizeof(SECURITY_ATTRIBUTES), NULL, TRUE };

    PR_ASSERT(pMutex != 0 && (*pMutex == 0 || *pMutex == INVALID_HANDLE_VALUE));
    if (!pMutex || ((hMutex = *pMutex) != 0 && hMutex != INVALID_HANDLE_VALUE)) {
	PORT_SetError(PR_INVALID_ARGUMENT_ERROR);
    	return SECFailure;
    }
    attributes.bInheritHandle = (shared ? TRUE : FALSE);
    hMutex = CreateMutex(&attributes, FALSE, NULL);
    if (hMutex == NULL) {
        hMutex = INVALID_HANDLE_VALUE;
        nss_MD_win32_map_default_error(GetLastError());
        return SECFailure;
    }
    *pMutex = hMutex;
    return SECSuccess;
}

int
sslMutex_Destroy(sslMutex *pMutex)
{
    HANDLE hMutex;
    int    rv;

    PR_ASSERT(pMutex != 0 && *pMutex != 0 && *pMutex != INVALID_HANDLE_VALUE);
    if (!pMutex || (hMutex = *pMutex) == 0 || hMutex == INVALID_HANDLE_VALUE) {
	PORT_SetError(PR_INVALID_ARGUMENT_ERROR);
	return SECFailure;
    }
    rv = CloseHandle(hMutex); /* ignore error */
    if (rv) {
	*pMutex = hMutex = INVALID_HANDLE_VALUE;
	return SECSuccess;
    }
    nss_MD_win32_map_default_error(GetLastError());
    return SECFailure;
}

int 
sslMutex_Unlock(sslMutex *pMutex)
{
    BOOL   success = FALSE;
    HANDLE hMutex;

    PR_ASSERT(pMutex != 0 && *pMutex != 0 && *pMutex != INVALID_HANDLE_VALUE);
    if (!pMutex || (hMutex = *pMutex) == 0 || hMutex == INVALID_HANDLE_VALUE) {
	PORT_SetError(PR_INVALID_ARGUMENT_ERROR);
	return SECFailure;
    }
    success = ReleaseMutex(hMutex);
    if (!success) {
        nss_MD_win32_map_default_error(GetLastError());
        return SECFailure;
    }
    return SECSuccess;
}

int 
sslMutex_Lock(sslMutex *pMutex)
{
    HANDLE    hMutex;
    DWORD     event;
    DWORD     lastError;
    SECStatus rv;

    PR_ASSERT(pMutex != 0 && *pMutex != 0 && *pMutex != INVALID_HANDLE_VALUE);
    if (!pMutex || (hMutex = *pMutex) == 0 || hMutex == INVALID_HANDLE_VALUE) {
	PORT_SetError(PR_INVALID_ARGUMENT_ERROR);
        return SECFailure;      /* what else ? */
    }
    event = WaitForSingleObject(hMutex, INFINITE);
    switch (event) {
    case WAIT_OBJECT_0:
    case WAIT_ABANDONED:
        rv = SECSuccess;
        break;

    case WAIT_TIMEOUT:
    case WAIT_IO_COMPLETION:
    default:            /* should never happen. nothing we can do. */
        PR_ASSERT(!("WaitForSingleObject returned invalid value."));
	PORT_SetError(PR_UNKNOWN_ERROR);
	rv = SECFailure;
	break;

    case WAIT_FAILED:           /* failure returns this */
        rv = SECFailure;
        lastError = GetLastError();     /* for debugging */
        nss_MD_win32_map_default_error(lastError);
        break;
    }
    return rv;
}

#elif defined(XP_UNIX)

#include <errno.h>
#include "unix_err.h"

SECStatus 
sslMutex_Init(sslMutex *pMutex, int shared)
{
    int rv;
    do {
	rv = sem_init(pMutex, shared, 1);
    } while (rv < 0 && errno == EINTR);
    if (rv < 0) {
	nss_MD_unix_map_default_error(errno);
	return SECFailure;
    }
    return SECSuccess;
}

SECStatus 
sslMutex_Destroy(sslMutex *pMutex)
{
    int rv;
    do {
	rv = sem_destroy(pMutex);
    } while (rv < 0 && errno == EINTR);
    if (rv < 0) {
	nss_MD_unix_map_default_error(errno);
	return SECFailure;
    }
    return SECSuccess;
}

SECStatus 
sslMutex_Unlock(sslMutex *pMutex)
{
    int rv;
    do {
	rv = sem_post(pMutex);
    } while (rv < 0 && errno == EINTR);
    if (rv < 0) {
	nss_MD_unix_map_default_error(errno);
	return SECFailure;
    }
    return SECSuccess;
}

SECStatus 
sslMutex_Lock(sslMutex *pMutex)
{
    int rv;
    do {
	rv = sem_wait(pMutex);
    } while (rv < 0 && errno == EINTR);
    if (rv < 0) {
	nss_MD_unix_map_default_error(errno);
	return SECFailure;
    }
    return SECSuccess;
}

#else

SECStatus 
sslMutex_Init(sslMutex *pMutex, int shared)
{
    PORT_Assert(!("sslMutex_Init not implemented!"));
    PORT_SetError(PR_NOT_IMPLEMENTED_ERROR);
    return SECFailure;
}

SECStatus 
sslMutex_Destroy(sslMutex *pMutex)
{
    PORT_Assert(!("sslMutex_Destroy not implemented!"));
    PORT_SetError(PR_NOT_IMPLEMENTED_ERROR);
    return SECFailure;
}

SECStatus 
sslMutex_Unlock(sslMutex *pMutex)
{
    PORT_Assert(!("sslMutex_Unlock not implemented!"));
    PORT_SetError(PR_NOT_IMPLEMENTED_ERROR);
    return SECFailure;
}

SECStatus 
sslMutex_Lock(sslMutex *pMutex)
{
    PORT_Assert(!("sslMutex_Lock not implemented!"));
    PORT_SetError(PR_NOT_IMPLEMENTED_ERROR);
    return SECFailure;
}

#endif