Mercurial > hg > nginx-quic
annotate src/os/unix/ngx_user.c @ 6022:1fdba317ee6d
Added support for offloading read() in thread pools.
author | Valentin Bartenev <vbart@nginx.com> |
---|---|
date | Sat, 14 Mar 2015 17:37:25 +0300 |
parents | 457ec43dd8d5 |
children | e284f3ff6831 |
rev | line source |
---|---|
503 | 1 |
2 /* | |
3 * Copyright (C) Igor Sysoev | |
4412 | 4 * Copyright (C) Nginx, Inc. |
503 | 5 */ |
6 | |
7 | |
8 #include <ngx_config.h> | |
9 #include <ngx_core.h> | |
10 | |
11 | |
12 /* | |
13 * Solaris has thread-safe crypt() | |
14 * Linux has crypt_r(); "struct crypt_data" is more than 128K | |
15 * FreeBSD needs the mutex to protect crypt() | |
16 * | |
17 * TODO: | |
18 * ngx_crypt_init() to init mutex | |
19 */ | |
20 | |
21 | |
22 #if (NGX_CRYPT) | |
23 | |
527 | 24 #if (NGX_HAVE_GNU_CRYPT_R) |
503 | 25 |
26 ngx_int_t | |
3922
9c057d5e1c27
"$apr1", "{PLAIN}", and "{SSHA}" password methods in auth basic module
Igor Sysoev <igor@sysoev.ru>
parents:
3796
diff
changeset
|
27 ngx_libc_crypt(ngx_pool_t *pool, u_char *key, u_char *salt, u_char **encrypted) |
503 | 28 { |
29 char *value; | |
30 size_t len; | |
31 struct crypt_data cd; | |
32 | |
509 | 33 cd.initialized = 0; |
5955
fd6fd02f6a4d
Fixed building with musl libc (ticket #685).
Maxim Dounin <mdounin@mdounin.ru>
parents:
4982
diff
changeset
|
34 #ifdef __GLIBC__ |
529 | 35 /* work around the glibc bug */ |
36 cd.current_salt[0] = ~salt[0]; | |
5955
fd6fd02f6a4d
Fixed building with musl libc (ticket #685).
Maxim Dounin <mdounin@mdounin.ru>
parents:
4982
diff
changeset
|
37 #endif |
509 | 38 |
503 | 39 value = crypt_r((char *) key, (char *) salt, &cd); |
40 | |
4982
6ccd3a50b40f
Core: crypt_r() error handling fixed.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4499
diff
changeset
|
41 if (value) { |
3796
7dec2852e8fd
allocate last zero byte in ngx_crypt()
Igor Sysoev <igor@sysoev.ru>
parents:
2049
diff
changeset
|
42 len = ngx_strlen(value) + 1; |
503 | 43 |
2049 | 44 *encrypted = ngx_pnalloc(pool, len); |
4982
6ccd3a50b40f
Core: crypt_r() error handling fixed.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4499
diff
changeset
|
45 if (*encrypted == NULL) { |
6ccd3a50b40f
Core: crypt_r() error handling fixed.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4499
diff
changeset
|
46 return NGX_ERROR; |
503 | 47 } |
4982
6ccd3a50b40f
Core: crypt_r() error handling fixed.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4499
diff
changeset
|
48 |
6ccd3a50b40f
Core: crypt_r() error handling fixed.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4499
diff
changeset
|
49 ngx_memcpy(*encrypted, value, len); |
6ccd3a50b40f
Core: crypt_r() error handling fixed.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4499
diff
changeset
|
50 return NGX_OK; |
503 | 51 } |
52 | |
4982
6ccd3a50b40f
Core: crypt_r() error handling fixed.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4499
diff
changeset
|
53 ngx_log_error(NGX_LOG_CRIT, pool->log, ngx_errno, "crypt_r() failed"); |
509 | 54 |
503 | 55 return NGX_ERROR; |
56 } | |
57 | |
58 #else | |
59 | |
60 ngx_int_t | |
3922
9c057d5e1c27
"$apr1", "{PLAIN}", and "{SSHA}" password methods in auth basic module
Igor Sysoev <igor@sysoev.ru>
parents:
3796
diff
changeset
|
61 ngx_libc_crypt(ngx_pool_t *pool, u_char *key, u_char *salt, u_char **encrypted) |
503 | 62 { |
63 char *value; | |
64 size_t len; | |
509 | 65 ngx_err_t err; |
503 | 66 |
6016
457ec43dd8d5
Renamed NGX_THREADS to NGX_OLD_THREADS because of deprecation.
Ruslan Ermilov <ru@nginx.com>
parents:
5955
diff
changeset
|
67 #if (NGX_OLD_THREADS && NGX_NONREENTRANT_CRYPT) |
503 | 68 |
4499
778ef9c3fd2d
Fixed spelling in single-line comments.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
69 /* crypt() is a time consuming function, so we only try to lock */ |
503 | 70 |
71 if (ngx_mutex_trylock(ngx_crypt_mutex) != NGX_OK) { | |
72 return NGX_AGAIN; | |
73 } | |
74 | |
75 #endif | |
76 | |
77 value = crypt((char *) key, (char *) salt); | |
78 | |
79 if (value) { | |
3796
7dec2852e8fd
allocate last zero byte in ngx_crypt()
Igor Sysoev <igor@sysoev.ru>
parents:
2049
diff
changeset
|
80 len = ngx_strlen(value) + 1; |
503 | 81 |
2049 | 82 *encrypted = ngx_pnalloc(pool, len); |
4982
6ccd3a50b40f
Core: crypt_r() error handling fixed.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4499
diff
changeset
|
83 if (*encrypted == NULL) { |
6016
457ec43dd8d5
Renamed NGX_THREADS to NGX_OLD_THREADS because of deprecation.
Ruslan Ermilov <ru@nginx.com>
parents:
5955
diff
changeset
|
84 #if (NGX_OLD_THREADS && NGX_NONREENTRANT_CRYPT) |
4982
6ccd3a50b40f
Core: crypt_r() error handling fixed.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4499
diff
changeset
|
85 ngx_mutex_unlock(ngx_crypt_mutex); |
6ccd3a50b40f
Core: crypt_r() error handling fixed.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4499
diff
changeset
|
86 #endif |
6ccd3a50b40f
Core: crypt_r() error handling fixed.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4499
diff
changeset
|
87 return NGX_ERROR; |
503 | 88 } |
509 | 89 |
4982
6ccd3a50b40f
Core: crypt_r() error handling fixed.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4499
diff
changeset
|
90 ngx_memcpy(*encrypted, value, len); |
6016
457ec43dd8d5
Renamed NGX_THREADS to NGX_OLD_THREADS because of deprecation.
Ruslan Ermilov <ru@nginx.com>
parents:
5955
diff
changeset
|
91 #if (NGX_OLD_THREADS && NGX_NONREENTRANT_CRYPT) |
509 | 92 ngx_mutex_unlock(ngx_crypt_mutex); |
93 #endif | |
94 return NGX_OK; | |
503 | 95 } |
96 | |
509 | 97 err = ngx_errno; |
98 | |
6016
457ec43dd8d5
Renamed NGX_THREADS to NGX_OLD_THREADS because of deprecation.
Ruslan Ermilov <ru@nginx.com>
parents:
5955
diff
changeset
|
99 #if (NGX_OLD_THREADS && NGX_NONREENTRANT_CRYPT) |
503 | 100 ngx_mutex_unlock(ngx_crypt_mutex); |
101 #endif | |
102 | |
509 | 103 ngx_log_error(NGX_LOG_CRIT, pool->log, err, "crypt() failed"); |
104 | |
105 return NGX_ERROR; | |
503 | 106 } |
107 | |
108 #endif | |
109 | |
110 #endif /* NGX_CRYPT */ |